components/esp32-owb/owb_rmt.c

Wed, 26 Jun 2024 21:48:49 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 26 Jun 2024 21:48:49 +0200
branch
idf 5.1
changeset 136
89fc3c57282e
parent 129
31f9d3e4a85f
permissions
-rw-r--r--

Reverted gpio_pad_select_gpio() calls, added the correct include. Starting the unit works better now. Reverted some other small changes. In some parts the controller is very slow, possible spiffs issue?

129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
1 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
2 * Copyright (c) 2023 mjcross
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
3 *
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
4 * SPDX-License-Identifier: MIT
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
5 **/
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
7 #include "esp_log.h"
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
8 #include "driver/rmt_tx.h"
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
9 #include "driver/rmt_rx.h"
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 #include "owb.h"
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
12 #include "owb_rmt_bus_timings.h"
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
13 #include "owb_rmt_bus_symbols.h"
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
15 #define OWB_RMT_CLK_HZ 1000000 // run the RMT at 1MHz to get 1us ticks
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
16 #define OWB_RMT_TX_MEM_BLOCK_SYMBOLS 64 // size of TX memory block in units of rmt_symbol_word_t (must be even)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
17 #define OWB_RMT_TX_QUEUE_DEPTH 4 // max pending TX transfers
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
18 #define OWB_RMT_MAX_READ_BITS 64 // maximum number of bits that will be read at once (used to calculate buffer size)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
19 #define OWB_RMT_RX_MEM_BLOCK_SYMBOLS (OWB_RMT_MAX_READ_BITS + 2) // size of RX memory block in units of rmt_symbol_word_t (must be even)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
20 #define OWB_RMT_RX_MIN_NS 1000 // RMT receive channel glitch rejection threshold (ns)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
21 #define OWB_RMT_TIMEOUT_MS 1000 // timeout threshold for an RMT task (ms)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
22 #define OWB_TIMING_MARGIN 3 // timing variation permitted by our event parsing functions (in microsec)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
23
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
24 // debug parsing of RMT raw symbols
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
25 //#define OWB_RMT_DEBUG
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
26
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
27 // tag for log messages
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
28 static const char * TAG = "owb_rmt";
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
29
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
30
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
31 //------
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
32 // private API functions and constants
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
33 //------
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
35 // onewire bus symbols as rmt_symbol_word_t
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
36 static const rmt_symbol_word_t owb_rmt_symbol_0bit = OWB_RMT_SYMBOL_0BIT;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
37 static const rmt_symbol_word_t owb_rmt_symbol_1bit = OWB_RMT_SYMBOL_1BIT;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
38 static const rmt_symbol_word_t owb_rmt_symbol_reset = OWB_RMT_SYMBOL_RESET;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
39
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
40 // RMT transmit configuration for the OWB: transmit symbols once then release the bus
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
41 static const rmt_transmit_config_t owb_rmt_transmit_config = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
42 .loop_count = 0, // don't send any repeats
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
43 .flags = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
44 .eot_level = OWB_RMT_BUS_RELEASED // release the bus after the transmission
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
45 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
46 };
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
47
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
48 // RMT receiver configuration for a onewire reset pulse
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
49 static const rmt_receive_config_t rx_config_owb_reset = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
50 .signal_range_min_ns = OWB_RMT_RX_MIN_NS, // glitch rejection threshold (ns)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
51 .signal_range_max_ns = (OWB_TIMING_PARAM_H + OWB_TIMING_PARAM_I) * 1000 // stop condition (ns)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
52 };
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
53
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
54 // RMT receiver configuration for a sequence of onewire data bits
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
55 static const rmt_receive_config_t rx_config_owb_bits = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
56 .signal_range_min_ns = OWB_RMT_RX_MIN_NS, // glitch rejection threshold (ns)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
57 .signal_range_max_ns = (OWB_TIMING_PARAM_A + OWB_TIMING_PARAM_B) * 1000 // stop condition (ns)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
58 };
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
61 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
62 * @brief Uninstalls a onewire bus driver and releases the associated resources.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
63 * @param bus A previously-initialised OneWireBus.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
64 * @return owb_status OWB_STATUS_OK on success, otherwise an error code (see owb.h)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
65 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
66 static owb_status _uninitialize(const OneWireBus *bus) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
67
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
68 // fetch the parent `owb_rmt_driver_info` structure for `bus`
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
69 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus); // (pointer, type, member)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
70 if (info == NULL) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
71 ESP_LOGE(TAG, "err uninitialize: no bus container");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
72 return OWB_STATUS_PARAMETER_NULL;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
73 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
74
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
75 // release RMT device symbol buffer and queue
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
76 free (info->rx_buffer);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
77 vQueueDelete (info->rx_queue);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
78
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
79 // disable and release RMT resources
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
80 if (rmt_disable (info->rx_channel_handle) == ESP_OK &&
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
81 rmt_del_channel (info->rx_channel_handle) == ESP_OK &&
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
82 rmt_disable (info->tx_channel_handle) == ESP_OK &&
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
83 rmt_del_channel (info->tx_channel_handle) == ESP_OK &&
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
84 rmt_del_encoder (info->copy_encoder_handle) == ESP_OK &&
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
85 rmt_del_encoder (info->bytes_encoder_handle) == ESP_OK ) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
86 // all resources successfully released
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
87 return OWB_STATUS_OK;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
88 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
89
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
90 // an error occurred
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
91 ESP_LOGE(TAG, "err uninitializing");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
92 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
93 }
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
96 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
97 * @brief Parses the RMT symbols received during a onewire bus reset.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
98 * @param[in] num_symbols The number of symbols passed.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
99 * @param[in] symbols An array of RMT symbols.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
100 * @param[out] slave_is_present Whether a slave presence signal was detected.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
101 * @return OWB_STATUS_OK if the symbols pass basic valdation; otherwise an error code (see owb.h).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
102 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
103 static owb_status _parse_reset_symbols (size_t num_symbols, rmt_symbol_word_t *symbols, bool *slave_is_present) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
104 *slave_is_present = false;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
106 if (num_symbols == 0 || symbols == NULL) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
107 return OWB_STATUS_PARAMETER_NULL;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
108 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
109
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
110 #ifdef OWB_RMT_DEBUG
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
111 // display raw RMT symbols
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
112 ESP_LOGI(TAG, "parse reset: %d symbols", (int)num_symbols);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
113 for (int i = 0; i < num_symbols; i += 1) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
114 ESP_LOGI (TAG, "\t%u (%uus), %u (%uus)", symbols->level0, symbols->duration0,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
115 symbols->level1, symbols->duration1);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
116 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
117 #endif
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
119 // check the duration of the reset pulse
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
120 if (abs (symbols[0].duration0 - OWB_TIMING_PARAM_H) > OWB_TIMING_MARGIN) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
121 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
122 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
123
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
124 // check for a valid 'no slave' event
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
125 if (num_symbols == 1 && symbols[0].duration1 == 0) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
126 *slave_is_present = false;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
127 return OWB_STATUS_OK;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
128 }
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
130 // check for a valid 'slave present' event
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
131 if (num_symbols == 2 && // no 'extra' symbols after the presence pulse
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
132 symbols[0].duration1 < OWB_TIMING_PARAM_I && // presence pulse must arrive before the sample point
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
133 (symbols[1].duration0 + symbols[0].duration1) >= OWB_TIMING_PARAM_I // presence pulse must not finish before the sample point
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
134 ) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
135 *slave_is_present = true;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
136 return OWB_STATUS_OK;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
137 }
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
139 // anything else is invalid
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
140 return OWB_STATUS_HW_ERROR;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
144 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
145 * @brief Parses the RMT symbols received during the transmission of up to 64 onewire bits.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
146 * @param[in] num_symbols The number of symbols passed.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
147 * @param[in] symbols An array of RMT symbols.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
148 * @param[out] result The decoded bits (max 64, lsb first)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
149 * @return int The number of bits decoded
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
150 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
151 static int _parse_bit_symbols (size_t num_symbols, rmt_symbol_word_t *p_symbol, uint64_t *result) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
152 *result = 0;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
153 int bit_count = 0;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
154 rmt_symbol_word_t *p_last_symbol = p_symbol + num_symbols;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
156 #ifdef OWB_RMT_DEBUG
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
157 // display raw RMT symbols
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
158 ESP_LOGI(TAG, "parse bits: %d symbols", (int)num_symbols);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
159 #endif
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
161 while (p_symbol < p_last_symbol && bit_count < 64) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
162 #ifdef OWB_RMT_DEBUG
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
163 ESP_LOGI (TAG, "\t%u (%uus), %u (%uus)", p_symbol->level0, p_symbol->duration0,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
164 p_symbol->level1, p_symbol->duration1);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
165 #endif
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
166 if (abs (p_symbol->duration0 - OWB_TIMING_PARAM_A) <= OWB_TIMING_MARGIN &&
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
167 (p_symbol->duration1 == 0 || p_symbol->duration1 >= OWB_TIMING_PARAM_E)) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
168 // bus was released at the sample point: detect a '1'
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
169 *result |= (1ull << bit_count);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
170 bit_count += 1;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
172 #ifdef OWB_RMT_DEBUG
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
173 ESP_LOGI (TAG, "\t\tdetect '1' -> 0x%llx", *result);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
174 #endif
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
176 } else if (p_symbol->duration0 >= (OWB_TIMING_PARAM_A + OWB_TIMING_PARAM_E)) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
177 // bus was asserted at the sample point: detect a '0'
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
178 bit_count += 1;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
179
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
180 #ifdef OWB_RMT_DEBUG
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
181 ESP_LOGI (TAG, "\t\tdetect '0' -> 0x%llx", *result);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
182 #endif
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
183 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
184 p_symbol += 1; // next symbol
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
185 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
186
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
187 return bit_count;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
188 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
189
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
191 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
192 * @brief Sends a onewire bus reset pulse and listens for slave presence responses.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
193 * @param[in] bus Points to the OneWireBus structure (see owb.h).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
194 * @param[out] is_present Points to a bool that will receive the detection result.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
195 * @return OWB_STATUS_OK if the call succeeded; otherwise an owb_status error code (see owb.h).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
196 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
197 static owb_status _reset (const OneWireBus *bus, bool *is_present) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
198
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
199 esp_err_t esp_status;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
201 // identify the rmt_driver_info structure that contains `bus`
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
202 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
203
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
204 // start the receiver before the transmitter so that it sees the leading edge of the pulse
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
205 esp_status = rmt_receive (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
206 info->rx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
207 info->rx_buffer,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
208 info->rx_buffer_size_in_bytes,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
209 &rx_config_owb_reset);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
210 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
211 ESP_LOGE(TAG, "owb_reset: rx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
212 return OWB_STATUS_HW_ERROR;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
215 // encode and transmit the reset pulse using the RMT 'copy' encoder
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
216 esp_status = rmt_transmit (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
217 info->tx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
218 info->copy_encoder_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
219 &owb_rmt_symbol_reset,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
220 sizeof (owb_rmt_symbol_reset),
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
221 &owb_rmt_transmit_config);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
222 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
223 ESP_LOGE(TAG, "owb_reset: tx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
224 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
225 }
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
227 // wait for the transmission to finish (or timeout with an error)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
228 if (rmt_tx_wait_all_done (info->tx_channel_handle, OWB_RMT_TIMEOUT_MS) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
229 ESP_LOGE(TAG, "owb_reset: tx timeout");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
230 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
231 }
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
233 // wait for the recv_done event data from our callback
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
234 rmt_rx_done_event_data_t rx_done_event_data;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
235 if (xQueueReceive (info->rx_queue, &rx_done_event_data, pdMS_TO_TICKS(OWB_RMT_TIMEOUT_MS)) != pdTRUE) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
236 ESP_LOGE(TAG, "owb_reset: no rx symbol"); // rx timeout
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
237 return OWB_STATUS_DEVICE_NOT_RESPONDING;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
238 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
239
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
240 // parse the event data and return the result
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
241 return _parse_reset_symbols (rx_done_event_data.num_symbols, rx_done_event_data.received_symbols, is_present);
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
244 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
245 * @brief Writes a number of bytes to the onewire bus (slightly more efficient than sending them individually).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
246 * @param bus A previously-initialised OneWireBus.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
247 * @param bytes The bytes to be sent.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
248 * @param number_of_bytes_to_write How many bytes to send.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
249 * @return owb_status OWB_STATUS_OK on success, otherwise an error code (see owb.h).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
250 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
251 static owb_status _write_bytes(const OneWireBus *bus, uint8_t *bytes, int number_of_bytes_to_write) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
252 esp_err_t esp_status;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
253
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
254 // identify the rmt_driver_info structure that contains `bus`
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
255 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
256
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
257 // encode and transmit the bits using the RMT 'bytes' encoder
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
258 esp_status = rmt_transmit (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
259 info->tx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
260 info->bytes_encoder_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
261 bytes,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
262 (size_t)number_of_bytes_to_write,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
263 &owb_rmt_transmit_config);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
264 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
265 ESP_LOGE(TAG, "owb_write: tx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
266 return OWB_STATUS_HW_ERROR;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
267 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
268
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
269 // wait for the transmission to finish (or timeout with an error)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
270 if (rmt_tx_wait_all_done (info->tx_channel_handle, OWB_RMT_TIMEOUT_MS) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
271 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
272 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
273 return OWB_STATUS_OK;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
274 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
275
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
276
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
277 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
278 * @brief Writes 1-8 bits to the onewire bus.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
279 * @param bus A previously-initialised OneWireBus.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
280 * @param bytes A byte with the bits to be sent (lsb first).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
281 * @param number_of_bits_to_write How many bits to send (maximum 8).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
282 * @return owb_status OWB_STATUS_OK on success, otherwise an error code (see owb.h).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
283 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
284 static owb_status _write_bits(const OneWireBus *bus, uint8_t out, int number_of_bits_to_write) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
285
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
286 // send 8 bits as a byte instead
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
287 if (number_of_bits_to_write == 8) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
288 return _write_bytes (bus, &out, 1);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
289 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
290
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
291 if (number_of_bits_to_write < 1 || number_of_bits_to_write > 8) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
292 ESP_LOGE(TAG, "owb_write_bits: bad num of bits (%d)", number_of_bits_to_write);
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
293 return OWB_STATUS_TOO_MANY_BITS;
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
294 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
295
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
296 // identify the rmt_driver_info structure that contains `bus`
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
297 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
298
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
299 // send data as individual bits using the `copy` encoder
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
300 const rmt_symbol_word_t *symbol_ptr;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
301 esp_err_t esp_status;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
302 for (int b = 0; b < number_of_bits_to_write; b += 1) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
303 if ((out & (1 << b)) == 0) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
304 symbol_ptr = &owb_rmt_symbol_0bit;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
305 } else {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
306 symbol_ptr = &owb_rmt_symbol_1bit;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
307 }
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
308
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
309 // send bit symbol
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
310 esp_status = rmt_transmit (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
311 info->tx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
312 info->copy_encoder_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
313 symbol_ptr,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
314 sizeof (rmt_symbol_word_t),
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
315 &owb_rmt_transmit_config);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
316 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
317 ESP_LOGE(TAG, "owb_write_bit: tx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
318 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
319 }
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
320 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
321
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
322 // wait for the transmission to finish (or timeout with an error)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
323 if (rmt_tx_wait_all_done (info->tx_channel_handle, OWB_RMT_TIMEOUT_MS) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
324 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
325 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
326
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
327 return OWB_STATUS_OK;
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
328 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
329
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
330
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
331 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
332 * @brief Reads up to 8 bytes from the onewire bus (this is faster than reading individual bits).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
333 * @param bus A previously-initialised OneWireBus.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
334 * @param result The resulting data, stored lsb first in a uint64_t.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
335 * @param number_of_bytes_to_read The number of bytes to read.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
336 * @return owb_status OWB_STATUS_OK on success, otherwise and error code (see owb.h)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
337 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
338 static owb_status _read_bytes(const OneWireBus *bus, uint64_t *result_ptr, int number_of_bytes_to_read) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
339 static uint8_t ff_bytes[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
340 esp_err_t esp_status;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
341
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
342 if (number_of_bytes_to_read > 8) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
343 ESP_LOGE(TAG, "owb_read_bytes: max 8");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
344 return OWB_STATUS_TOO_MANY_BITS;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
345 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
346
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
347 // identify the rmt_driver_info structure that contains `bus`
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
348 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
349
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
350 // start the receiver before the transmitter so that it sees the first edge
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
351 esp_status = rmt_receive (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
352 info->rx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
353 info->rx_buffer,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
354 info->rx_buffer_size_in_bytes,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
355 &rx_config_owb_bits);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
356 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
357 ESP_LOGE(TAG, "owb_read_bytes: rx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
358 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
359 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
360
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
361 // generate read slots
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
362 esp_status = rmt_transmit (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
363 info->tx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
364 info->bytes_encoder_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
365 ff_bytes,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
366 (size_t)number_of_bytes_to_read,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
367 &owb_rmt_transmit_config);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
368 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
369 ESP_LOGE(TAG, "owb_read_bytes: tx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
370 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
371 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
372
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
373 // wait for the transmission to finish (or timeout with an error)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
374 if (rmt_tx_wait_all_done (info->tx_channel_handle, OWB_RMT_TIMEOUT_MS) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
375 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
376 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
377
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
378 // wait for the recv_done event data from our callback
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
379 rmt_rx_done_event_data_t rx_done_event_data;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
380 if (xQueueReceive (info->rx_queue, &rx_done_event_data, pdMS_TO_TICKS(OWB_RMT_TIMEOUT_MS)) != pdTRUE) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
381 ESP_LOGE(TAG, "owb_read_bytes: no rx symbols"); // rx timeout
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
382 return OWB_STATUS_DEVICE_NOT_RESPONDING;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
383 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
384
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
385 // decode upto 64 data bits from the received RMT symbols
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
386 if (_parse_bit_symbols(rx_done_event_data.num_symbols, rx_done_event_data.received_symbols, result_ptr) == 0) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
387 ESP_LOGE(TAG, "owb_read_bytes: no bits");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
388 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
389
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
390 return OWB_STATUS_OK;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
391 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
392
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
393
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
394 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
395 * @brief Reads up to 8 bits from the onewire bus.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
396 * @param bus A previously-initialised OneWireBus.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
397 * @param result A byte containing the bits read (lsb first).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
398 * @param number_of_bits_to_read The number of bits to read.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
399 * @return owb_status OWB_STATUS_OK on success, otherwise an error code (see owb.h)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
400 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
401 static owb_status _read_bits(const OneWireBus *bus, uint8_t *result, int number_of_bits_to_read) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
402 esp_err_t esp_status;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
403
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
404 if (number_of_bits_to_read > 8) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
405 ESP_LOGE(TAG, "owb_read_bits: max 8");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
406 return OWB_STATUS_TOO_MANY_BITS;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
407 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
408
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
409 // it's quicker to read 8 bits as a whole byte
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
410 if (number_of_bits_to_read == 8) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
411 uint64_t result_64;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
412 owb_status status;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
413 status = _read_bytes (bus, &result_64, 1);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
414 *result = (uint8_t)result_64;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
415 return status;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
416 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
417
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
418 // identify the rmt_driver_info structure that contains `bus`
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
419 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
420
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
421 // with the copy encoder then it's most efficient to receive each bit individually
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
422 // because we don't accurately know the interval between bits.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
423 // It would be nice to use `rmt_transmit_config.loop_count` here, but it's not supported
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
424 // on all chips. In any case the user almost certainly only wants a single bit.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
425 *result = 0;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
426 for (int bit_index = 0; bit_index < number_of_bits_to_read; bit_index += 1) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
427
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
428 // start the receiver before the transmitter so that it sees the first edge
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
429 esp_status = rmt_receive (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
430 info->rx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
431 info->rx_buffer,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
432 info->rx_buffer_size_in_bytes,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
433 &rx_config_owb_bits);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
434 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
435 ESP_LOGE(TAG, "owb_read_bits: rx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
436 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
437 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
438
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
439 // send a '1' symbol to generate a read slot
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
440 esp_status = rmt_transmit (
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
441 info->tx_channel_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
442 info->copy_encoder_handle,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
443 &owb_rmt_symbol_1bit,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
444 sizeof (rmt_symbol_word_t),
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
445 &owb_rmt_transmit_config);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
446 if (esp_status != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
447 ESP_LOGE(TAG, "owb_read_bits: tx err");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
448 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
449 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
450
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
451 // wait for the transmission to finish (or timeout with an error)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
452 if (rmt_tx_wait_all_done (info->tx_channel_handle, OWB_RMT_TIMEOUT_MS) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
453 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
454 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
455
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
456 // wait for the recv_done event data from our callback
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
457 rmt_rx_done_event_data_t rx_done_event_data;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
458 if (xQueueReceive (info->rx_queue, &rx_done_event_data, pdMS_TO_TICKS(OWB_RMT_TIMEOUT_MS)) != pdTRUE) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
459 ESP_LOGE(TAG, "owb_read_bits: no rx symbol"); // rx timeout
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
460 return OWB_STATUS_DEVICE_NOT_RESPONDING;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
461 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
462
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
463 // parse the event data
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
464 uint64_t bits = 0;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
465 if (_parse_bit_symbols (rx_done_event_data.num_symbols, rx_done_event_data.received_symbols, &bits) == 0) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
466 ESP_LOGE(TAG, "owb_read_bits: no bits");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
467 return OWB_STATUS_HW_ERROR;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
468 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
469
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
470 // add the bit to `result` (lsb is received first)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
471 if ((bits & 1) != 0) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
472 *result |= (1 << bit_index);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
473 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
474 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
475
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
476 return OWB_STATUS_OK;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
477 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
478
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
479
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
480 /**
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
481 * @brief Handle the RMT `recv_done` event by copying the event data structure to the specified queue.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
482 * @param[in] channel The handle of the RMT channel that generated the event.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
483 * @param[in] edata A pointer to the RMT event data structure (the pointer is valid only within this function).
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
484 * @param[in] context A pointer to the user-provided context, in this case the queue handle.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
485 * @return True if sending to the queue caused a higher priority task to unblock; otherwise False.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
486 */
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
487 static bool IRAM_ATTR _recv_done_callback (rmt_channel_handle_t channel, const rmt_rx_done_event_data_t *event_data, void *user_data) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
488 // Copy a pointer to the event data structure to the queue identified in the user_data.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
489 //* NOTE: this is an interrupt handler so it needs IRAM_ATTR, may only use `ISR` calls and must return promptly.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
490 //
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
491 BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
492
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
493 xQueueSendFromISR ((QueueHandle_t)user_data, event_data, &pxHigherPriorityTaskWoken);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
494 if (pxHigherPriorityTaskWoken == pdTRUE) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
495 return true;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
496 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
497 return false;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
498 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
499
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
500
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
501 //-----
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
502 // Public API functions
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
503 //-----
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
504
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
505 // RMT version of the OWB driver api (will be stored as info->bus->driver)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
506 //
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
507 static struct owb_driver rmt_driver_functions = {
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
508 .name = "owb_rmt",
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
509 .uninitialize = _uninitialize,
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
510 .reset = _reset,
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
511 .write_bits = _write_bits,
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
512 .write_bytes = _write_bytes, // new addition to the API
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
513 .read_bits = _read_bits,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
514 .read_bytes = _read_bytes // new addition to the API
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
515 };
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
516
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
517
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
518 // configure and allocate resources
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
519 //
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
520 OneWireBus* owb_rmt_initialize (owb_rmt_driver_info *info, gpio_num_t gpio_num, int tx_channel, int rx_channel)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
521 {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
522 //* The function now ignores tx_channel and rx_channel as the new RMT driver allocates channels on demand.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
523 //* The parameters are kept in the call to preserve compatibility with previous versions.
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
524
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
525 // the steps to enable the RMT resources are documented in:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
526 // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/rmt.html
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
527
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
528 // Note: keeping the TX and RX initialisations together in one function simplifies the error handling
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
529
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
530 (void)tx_channel; // avoid compiler warning about unused parameter
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
531 (void)rx_channel; // avoid compiler warning about unused parameter
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
532
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
533 // sanity check
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
534 if (info == NULL) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
535 ESP_LOGE(TAG, "info is NULL");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
536 goto exit_err;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
537 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
538
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
539 // ----- receive channel -----
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
540
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
541 // channel config
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
542 const rmt_rx_channel_config_t rx_channel_config = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
543 .gpio_num = (int)gpio_num,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
544 .clk_src = RMT_CLK_SRC_APB, // use the APB clock (might reduce during light sleep)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
545 .resolution_hz = OWB_RMT_CLK_HZ,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
546 .mem_block_symbols = (size_t)OWB_RMT_RX_MEM_BLOCK_SYMBOLS,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
547 .flags = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
548 .invert_in = 0, // don't hardware invert the input
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
549 .with_dma = 0, // don't enable DMA
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
550 .io_loop_back = 0 // we define the loopback in the tx config
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
551 }
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
552 };
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
553
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
554 // request channel
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
555 //* note: to get a wired-OR bus you must apply the rx_config first, _then_ the rx_config
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
556 if (rmt_new_rx_channel (&rx_channel_config, &(info->rx_channel_handle)) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
557 ESP_LOGE(TAG, "err requesting rx_channel");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
558 goto exit_err;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
559 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
560
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
561 // create queue for RMT `rx_done` event data struct (from callback)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
562 info->rx_queue = xQueueCreate (1, sizeof (rmt_rx_done_event_data_t));
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
563 if (info->rx_queue == NULL) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
564 ESP_LOGE(TAG, "err creating rx_queue");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
565 goto exit_delete_rx_channel;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
566 }
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
567
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
568 // allocate rx symbol buffer for RMT driver
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
569 info->rx_buffer_size_in_bytes = OWB_RMT_MAX_READ_BITS * sizeof (rmt_symbol_word_t);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
570 info->rx_buffer = (rmt_symbol_word_t *)malloc (info->rx_buffer_size_in_bytes);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
571 if (info->rx_buffer == NULL) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
572 ESP_LOGE(TAG, "err allocating rx_buffer");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
573 goto exit_delete_rx_queue;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
574 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
575
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
576 // register rx channel callback (rx_queue is passed as user context)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
577 const rmt_rx_event_callbacks_t rmt_rx_event_callbacks = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
578 .on_recv_done = _recv_done_callback
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
579 };
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
580 if (rmt_rx_register_event_callbacks (info->rx_channel_handle, &rmt_rx_event_callbacks, info->rx_queue) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
581 ESP_LOGE(TAG, "err registering rx_callbacks");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
582 goto exit_release_rx_buffer;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
583 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
584
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
585 // enable channel
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
586 if (rmt_enable (info->rx_channel_handle) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
587 ESP_LOGE(TAG, "err enabling rx_channel");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
588 goto exit_release_rx_buffer;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
589 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
590
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
591 // ----- transmit channel -----
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
592
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
593 // channel config
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
594 const rmt_tx_channel_config_t tx_channel_config = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
595 .gpio_num = (int)gpio_num,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
596 .clk_src = RMT_CLK_SRC_APB, // use the APB clock (might reduce during light sleep)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
597 .resolution_hz = OWB_RMT_CLK_HZ,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
598 .mem_block_symbols = (size_t)OWB_RMT_TX_MEM_BLOCK_SYMBOLS,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
599 .trans_queue_depth = OWB_RMT_TX_QUEUE_DEPTH,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
600 .flags = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
601 .invert_out = 1, // invert the output (so that the bus is initially released)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
602 .with_dma = 0, // don't enable DMA
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
603 .io_loop_back = 1, // enable reading of actual voltage of output pin
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
604 .io_od_mode = 1 // enable open-drain output, so as to achieve a 'wired-OR' bus
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
605 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
606 };
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
607
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
608 // request channel
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
609 if (rmt_new_tx_channel (&tx_channel_config, &(info->tx_channel_handle)) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
610 ESP_LOGE(TAG, "err requesting tx_channel");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
611 goto exit_disable_rx_channel;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
612 }
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
613
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
614 // enable channel
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
615 if (rmt_enable (info->tx_channel_handle) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
616 ESP_LOGE(TAG, "err enabling tx_channel");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
617 goto exit_delete_tx_channel;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
618 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
619
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
620 // obtain a 'copy' encoder (an RMT built-in used for sending fixed bit patterns)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
621 const rmt_copy_encoder_config_t rmt_copy_encoder_config = {}; // config is "reserved for future expansion"
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
622 if (rmt_new_copy_encoder (&rmt_copy_encoder_config, &(info->copy_encoder_handle)) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
623 ESP_LOGE(TAG, "err requesting copy encoder");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
624 goto exit_disable_tx_channel;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
625 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
626
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
627 // otain a 'bytes' encoder (an RMT built-in used for sending variable bit patterns)
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
628 const rmt_bytes_encoder_config_t rmt_bytes_encoder_config = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
629 .bit0 = OWB_RMT_SYMBOL_0BIT,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
630 .bit1 = OWB_RMT_SYMBOL_1BIT,
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
631 .flags = {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
632 .msb_first = 0 // onewire bus on-the-wire bit order is lsb first
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
633 }
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
634 };
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
635 if (rmt_new_bytes_encoder(&rmt_bytes_encoder_config, &info->bytes_encoder_handle) != ESP_OK) {
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
636 ESP_LOGE(TAG, "err requesting bytes encoder");
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
637 goto exit_delete_copy_encoder;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
638 }
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
639
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
640
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
641 // ----- success ------
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
642 info->gpio = gpio_num;
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
643 info->bus.driver = &rmt_driver_functions; // route driver API calls to the functions in this file
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
644 ESP_LOGI(TAG, "%s: OK", __func__);
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
645 return &(info->bus);
129
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
646
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
647 // ----- error: unwind allocated resources -----
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
648 exit_delete_copy_encoder:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
649 ESP_ERROR_CHECK(rmt_del_encoder(info->copy_encoder_handle));
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
650 exit_disable_tx_channel:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
651 ESP_ERROR_CHECK(rmt_disable (info->tx_channel_handle));
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
652 exit_delete_tx_channel:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
653 ESP_ERROR_CHECK(rmt_del_channel (info->tx_channel_handle));
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
654 exit_disable_rx_channel:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
655 ESP_ERROR_CHECK(rmt_disable (info->rx_channel_handle));
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
656 exit_release_rx_buffer:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
657 free (info->rx_buffer);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
658 exit_delete_rx_queue:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
659 vQueueDelete (info->rx_queue);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
660 exit_delete_rx_channel:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
661 ESP_ERROR_CHECK(rmt_del_channel (info->rx_channel_handle));
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
662 exit_err:
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
663 ESP_LOGE(TAG, "%s: failed", __func__);
31f9d3e4a85f Start migration to IDF 5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
664 return NULL;
0
b74b0e4902c3 Initial checkin brewboard
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
665 }

mercurial