components/esp32-owb/owb_rmt.c

Tue, 26 Sep 2023 14:57:18 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 26 Sep 2023 14:57:18 +0200
changeset 72
acc1904cd70d
parent 0
88d965579617
permissions
-rw-r--r--

Migrated to isp-idf v5.1

72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
1 /**
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
2 * Copyright (c) 2023 mjcross
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
3 *
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
4 * SPDX-License-Identifier: MIT
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
5 **/
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
7 #include "esp_log.h"
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
8 #include "driver/rmt_tx.h"
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
9 #include "driver/rmt_rx.h"
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 #include "owb.h"
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
12 #include "owb_rmt_bus_timings.h"
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
13 #include "owb_rmt_bus_symbols.h"
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
72
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
17 #define OWB_RMT_TX_QUEUE_DEPTH 4 // max pending TX transfers
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
21 #define OWB_RMT_TIMEOUT_MS 1000 // timeout threshold for an RMT task (ms)
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
23
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
24 // debug parsing of RMT raw symbols
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
25 //#define OWB_RMT_DEBUG
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
26
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
27 // tag for log messages
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
28 static const char * TAG = "owb_rmt";
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
29
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
30
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
31 //------
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
32 // private API functions and constants
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
33 //------
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
35 // onewire bus symbols as rmt_symbol_word_t
acc1904cd70d Migrated to isp-idf v5.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;
acc1904cd70d Migrated to isp-idf v5.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;
acc1904cd70d Migrated to isp-idf v5.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;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
39
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
40 // RMT transmit configuration for the OWB: transmit symbols once then release the bus
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
41 static const rmt_transmit_config_t owb_rmt_transmit_config = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
42 .loop_count = 0, // don't send any repeats
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
43 .flags = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
44 .eot_level = OWB_RMT_BUS_RELEASED // release the bus after the transmission
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
45 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
46 };
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
47
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
48 // RMT receiver configuration for a onewire reset pulse
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
49 static const rmt_receive_config_t rx_config_owb_reset = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
50 .signal_range_min_ns = OWB_RMT_RX_MIN_NS, // glitch rejection threshold (ns)
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
52 };
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
53
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
54 // RMT receiver configuration for a sequence of onewire data bits
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
55 static const rmt_receive_config_t rx_config_owb_bits = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
56 .signal_range_min_ns = OWB_RMT_RX_MIN_NS, // glitch rejection threshold (ns)
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
58 };
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
61 /**
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
62 * @brief Uninstalls a onewire bus driver and releases the associated resources.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
63 * @param bus A previously-initialised OneWireBus.
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
65 */
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
66 static owb_status _uninitialize(const OneWireBus *bus) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
67
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
68 // fetch the parent `owb_rmt_driver_info` structure for `bus`
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
70 if (info == NULL) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
71 ESP_LOGE(TAG, "err uninitialize: no bus container");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
72 return OWB_STATUS_PARAMETER_NULL;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
73 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
74
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
75 // release RMT device symbol buffer and queue
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
76 free (info->rx_buffer);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
77 vQueueDelete (info->rx_queue);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
78
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
79 // disable and release RMT resources
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
80 if (rmt_disable (info->rx_channel_handle) == ESP_OK &&
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
81 rmt_del_channel (info->rx_channel_handle) == ESP_OK &&
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
82 rmt_disable (info->tx_channel_handle) == ESP_OK &&
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
83 rmt_del_channel (info->tx_channel_handle) == ESP_OK &&
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
84 rmt_del_encoder (info->copy_encoder_handle) == ESP_OK &&
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
85 rmt_del_encoder (info->bytes_encoder_handle) == ESP_OK ) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
86 // all resources successfully released
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
87 return OWB_STATUS_OK;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
88 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
89
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
90 // an error occurred
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
91 ESP_LOGE(TAG, "err uninitializing");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
92 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
93 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
96 /**
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
97 * @brief Parses the RMT symbols received during a onewire bus reset.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
98 * @param[in] num_symbols The number of symbols passed.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
99 * @param[in] symbols An array of RMT symbols.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
100 * @param[out] slave_is_present Whether a slave presence signal was detected.
acc1904cd70d Migrated to isp-idf v5.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).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
102 */
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
104 *slave_is_present = false;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
106 if (num_symbols == 0 || symbols == NULL) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
107 return OWB_STATUS_PARAMETER_NULL;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
108 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
109
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
110 #ifdef OWB_RMT_DEBUG
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
111 // display raw RMT symbols
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
112 ESP_LOGI(TAG, "parse reset: %d symbols", (int)num_symbols);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
113 for (int i = 0; i < num_symbols; i += 1) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
114 ESP_LOGI (TAG, "\t%u (%uus), %u (%uus)", symbols->level0, symbols->duration0,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
115 symbols->level1, symbols->duration1);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
116 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
117 #endif
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
119 // check the duration of the reset pulse
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
120 if (abs (symbols[0].duration0 - OWB_TIMING_PARAM_H) > OWB_TIMING_MARGIN) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
121 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
122 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
123
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
124 // check for a valid 'no slave' event
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
125 if (num_symbols == 1 && symbols[0].duration1 == 0) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
126 *slave_is_present = false;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
127 return OWB_STATUS_OK;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
128 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
130 // check for a valid 'slave present' event
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
131 if (num_symbols == 2 && // no 'extra' symbols after the presence pulse
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
134 ) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
135 *slave_is_present = true;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
136 return OWB_STATUS_OK;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
137 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
139 // anything else is invalid
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
140 return OWB_STATUS_HW_ERROR;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
144 /**
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
146 * @param[in] num_symbols The number of symbols passed.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
147 * @param[in] symbols An array of RMT symbols.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
148 * @param[out] result The decoded bits (max 64, lsb first)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
149 * @return int The number of bits decoded
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
150 */
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
152 *result = 0;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
153 int bit_count = 0;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
154 rmt_symbol_word_t *p_last_symbol = p_symbol + num_symbols;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
156 #ifdef OWB_RMT_DEBUG
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
157 // display raw RMT symbols
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
158 ESP_LOGI(TAG, "parse bits: %d symbols", (int)num_symbols);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
159 #endif
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
161 while (p_symbol < p_last_symbol && bit_count < 64) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
162 #ifdef OWB_RMT_DEBUG
acc1904cd70d Migrated to isp-idf v5.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,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
164 p_symbol->level1, p_symbol->duration1);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
165 #endif
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
166 if (abs (p_symbol->duration0 - OWB_TIMING_PARAM_A) <= OWB_TIMING_MARGIN &&
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
167 (p_symbol->duration1 == 0 || p_symbol->duration1 >= OWB_TIMING_PARAM_E)) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
168 // bus was released at the sample point: detect a '1'
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
169 *result |= (1ull << bit_count);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
170 bit_count += 1;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
172 #ifdef OWB_RMT_DEBUG
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
173 ESP_LOGI (TAG, "\t\tdetect '1' -> 0x%llx", *result);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
174 #endif
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
176 } else if (p_symbol->duration0 >= (OWB_TIMING_PARAM_A + OWB_TIMING_PARAM_E)) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
177 // bus was asserted at the sample point: detect a '0'
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
178 bit_count += 1;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
179
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
180 #ifdef OWB_RMT_DEBUG
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
181 ESP_LOGI (TAG, "\t\tdetect '0' -> 0x%llx", *result);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
182 #endif
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
183 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
184 p_symbol += 1; // next symbol
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
185 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
186
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
187 return bit_count;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
188 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
189
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
191 /**
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
192 * @brief Sends a onewire bus reset pulse and listens for slave presence responses.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
193 * @param[in] bus Points to the OneWireBus structure (see owb.h).
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.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).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
196 */
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
197 static owb_status _reset (const OneWireBus *bus, bool *is_present) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
198
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
199 esp_err_t esp_status;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
201 // identify the rmt_driver_info structure that contains `bus`
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
202 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
203
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
205 esp_status = rmt_receive (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
206 info->rx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
207 info->rx_buffer,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
208 info->rx_buffer_size_in_bytes,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
209 &rx_config_owb_reset);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
210 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
211 ESP_LOGE(TAG, "owb_reset: rx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
212 return OWB_STATUS_HW_ERROR;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
215 // encode and transmit the reset pulse using the RMT 'copy' encoder
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
216 esp_status = rmt_transmit (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
217 info->tx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
218 info->copy_encoder_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
219 &owb_rmt_symbol_reset,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
220 sizeof (owb_rmt_symbol_reset),
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
221 &owb_rmt_transmit_config);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
222 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
223 ESP_LOGE(TAG, "owb_reset: tx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
224 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
225 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
227 // wait for the transmission to finish (or timeout with an error)
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
229 ESP_LOGE(TAG, "owb_reset: tx timeout");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
230 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
231 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
233 // wait for the recv_done event data from our callback
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
234 rmt_rx_done_event_data_t rx_done_event_data;
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
236 ESP_LOGE(TAG, "owb_reset: no rx symbol"); // rx timeout
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
237 return OWB_STATUS_DEVICE_NOT_RESPONDING;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
238 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
239
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
240 // parse the event data and return the result
acc1904cd70d Migrated to isp-idf v5.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
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
244 /**
acc1904cd70d Migrated to isp-idf v5.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).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
246 * @param bus A previously-initialised OneWireBus.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
247 * @param bytes The bytes to be sent.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
248 * @param number_of_bytes_to_write How many bytes to send.
acc1904cd70d Migrated to isp-idf v5.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).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
250 */
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
252 esp_err_t esp_status;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
253
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
254 // identify the rmt_driver_info structure that contains `bus`
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
255 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
256
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
257 // encode and transmit the bits using the RMT 'bytes' encoder
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
258 esp_status = rmt_transmit (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
259 info->tx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
260 info->bytes_encoder_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
261 bytes,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
262 (size_t)number_of_bytes_to_write,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
263 &owb_rmt_transmit_config);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
264 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
265 ESP_LOGE(TAG, "owb_write: tx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
266 return OWB_STATUS_HW_ERROR;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
267 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
268
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
269 // wait for the transmission to finish (or timeout with an error)
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
271 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
272 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
273 return OWB_STATUS_OK;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
274 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
275
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
276
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
277 /**
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
278 * @brief Writes 1-8 bits to the onewire bus.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
279 * @param bus A previously-initialised OneWireBus.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
280 * @param bytes A byte with the bits to be sent (lsb first).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
281 * @param number_of_bits_to_write How many bits to send (maximum 8).
acc1904cd70d Migrated to isp-idf v5.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).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
283 */
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
285
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
286 // send 8 bits as a byte instead
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
287 if (number_of_bits_to_write == 8) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
288 return _write_bytes (bus, &out, 1);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
289 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
290
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
291 if (number_of_bits_to_write < 1 || number_of_bits_to_write > 8) {
acc1904cd70d Migrated to isp-idf v5.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
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
293 return OWB_STATUS_TOO_MANY_BITS;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
294 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
295
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
296 // identify the rmt_driver_info structure that contains `bus`
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
297 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
298
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
299 // send data as individual bits using the `copy` encoder
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
300 const rmt_symbol_word_t *symbol_ptr;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
301 esp_err_t esp_status;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
302 for (int b = 0; b < number_of_bits_to_write; b += 1) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
303 if ((out & (1 << b)) == 0) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
304 symbol_ptr = &owb_rmt_symbol_0bit;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
305 } else {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
306 symbol_ptr = &owb_rmt_symbol_1bit;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
307 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
308
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
309 // send bit symbol
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
310 esp_status = rmt_transmit (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
311 info->tx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
312 info->copy_encoder_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
313 symbol_ptr,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
314 sizeof (rmt_symbol_word_t),
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
315 &owb_rmt_transmit_config);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
316 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
317 ESP_LOGE(TAG, "owb_write_bit: tx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
318 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
319 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
320 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
321
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
322 // wait for the transmission to finish (or timeout with an error)
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
324 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
325 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
326
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
327 return OWB_STATUS_OK;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
328 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
329
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
330
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
331 /**
acc1904cd70d Migrated to isp-idf v5.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).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
333 * @param bus A previously-initialised OneWireBus.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
334 * @param result The resulting data, stored lsb first in a uint64_t.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
335 * @param number_of_bytes_to_read The number of bytes to read.
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
337 */
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
339 static uint8_t ff_bytes[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
340 esp_err_t esp_status;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
341
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
342 if (number_of_bytes_to_read > 8) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
343 ESP_LOGE(TAG, "owb_read_bytes: max 8");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
344 return OWB_STATUS_TOO_MANY_BITS;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
345 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
346
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
347 // identify the rmt_driver_info structure that contains `bus`
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
348 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
349
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
350 // start the receiver before the transmitter so that it sees the first edge
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
351 esp_status = rmt_receive (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
352 info->rx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
353 info->rx_buffer,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
354 info->rx_buffer_size_in_bytes,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
355 &rx_config_owb_bits);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
356 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
357 ESP_LOGE(TAG, "owb_read_bytes: rx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
358 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
359 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
360
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
361 // generate read slots
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
362 esp_status = rmt_transmit (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
363 info->tx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
364 info->bytes_encoder_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
365 ff_bytes,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
366 (size_t)number_of_bytes_to_read,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
367 &owb_rmt_transmit_config);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
368 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
369 ESP_LOGE(TAG, "owb_read_bytes: tx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
370 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
371 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
372
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
373 // wait for the transmission to finish (or timeout with an error)
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
375 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
376 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
377
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
378 // wait for the recv_done event data from our callback
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
379 rmt_rx_done_event_data_t rx_done_event_data;
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
381 ESP_LOGE(TAG, "owb_read_bytes: no rx symbols"); // rx timeout
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
382 return OWB_STATUS_DEVICE_NOT_RESPONDING;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
383 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
384
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
385 // decode upto 64 data bits from the received RMT symbols
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
387 ESP_LOGE(TAG, "owb_read_bytes: no bits");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
388 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
389
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
390 return OWB_STATUS_OK;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
391 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
392
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
393
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
394 /**
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
395 * @brief Reads up to 8 bits from the onewire bus.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
396 * @param bus A previously-initialised OneWireBus.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
397 * @param result A byte containing the bits read (lsb first).
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
398 * @param number_of_bits_to_read The number of bits to read.
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
400 */
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
402 esp_err_t esp_status;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
403
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
404 if (number_of_bits_to_read > 8) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
405 ESP_LOGE(TAG, "owb_read_bits: max 8");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
406 return OWB_STATUS_TOO_MANY_BITS;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
407 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
408
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
409 // it's quicker to read 8 bits as a whole byte
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
410 if (number_of_bits_to_read == 8) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
411 uint64_t result_64;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
412 owb_status status;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
413 status = _read_bytes (bus, &result_64, 1);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
414 *result = (uint8_t)result_64;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
415 return status;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
416 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
417
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
418 // identify the rmt_driver_info structure that contains `bus`
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
419 owb_rmt_driver_info *info = __containerof(bus, owb_rmt_driver_info, bus);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
420
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
422 // because we don't accurately know the interval between bits.
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
425 *result = 0;
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
427
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
428 // start the receiver before the transmitter so that it sees the first edge
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
429 esp_status = rmt_receive (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
430 info->rx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
431 info->rx_buffer,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
432 info->rx_buffer_size_in_bytes,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
433 &rx_config_owb_bits);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
434 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
435 ESP_LOGE(TAG, "owb_read_bits: rx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
436 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
437 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
438
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
439 // send a '1' symbol to generate a read slot
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
440 esp_status = rmt_transmit (
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
441 info->tx_channel_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
442 info->copy_encoder_handle,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
443 &owb_rmt_symbol_1bit,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
444 sizeof (rmt_symbol_word_t),
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
445 &owb_rmt_transmit_config);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
446 if (esp_status != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
447 ESP_LOGE(TAG, "owb_read_bits: tx err");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
448 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
449 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
450
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
451 // wait for the transmission to finish (or timeout with an error)
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
453 return OWB_STATUS_DEVICE_NOT_RESPONDING; // tx timeout
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
454 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
455
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
456 // wait for the recv_done event data from our callback
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
457 rmt_rx_done_event_data_t rx_done_event_data;
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
459 ESP_LOGE(TAG, "owb_read_bits: no rx symbol"); // rx timeout
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
460 return OWB_STATUS_DEVICE_NOT_RESPONDING;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
461 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
462
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
463 // parse the event data
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
464 uint64_t bits = 0;
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
466 ESP_LOGE(TAG, "owb_read_bits: no bits");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
467 return OWB_STATUS_HW_ERROR;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
468 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
469
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
470 // add the bit to `result` (lsb is received first)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
471 if ((bits & 1) != 0) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
472 *result |= (1 << bit_index);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
473 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
474 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
475
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
476 return OWB_STATUS_OK;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
477 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
478
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
479
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
480 /**
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
482 * @param[in] channel The handle of the RMT channel that generated the event.
acc1904cd70d Migrated to isp-idf v5.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).
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
486 */
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
490 //
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
491 BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
492
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
493 xQueueSendFromISR ((QueueHandle_t)user_data, event_data, &pxHigherPriorityTaskWoken);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
494 if (pxHigherPriorityTaskWoken == pdTRUE) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
495 return true;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
496 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
497 return false;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
498 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
499
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
500
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
501 //-----
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
502 // Public API functions
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
503 //-----
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
504
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
506 //
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
507 static struct owb_driver rmt_driver_functions = {
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
508 .name = "owb_rmt",
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
509 .uninitialize = _uninitialize,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
510 .reset = _reset,
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
511 .write_bits = _write_bits,
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
512 .write_bytes = _write_bytes, // new addition to the API
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
513 .read_bits = _read_bits,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
514 .read_bytes = _read_bytes // new addition to the API
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
515 };
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
516
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
517
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
518 // configure and allocate resources
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
519 //
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
521 {
acc1904cd70d Migrated to isp-idf v5.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.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
523 //* The parameters are kept in the call to preserve compatibility with previous versions.
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
524
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
525 // the steps to enable the RMT resources are documented in:
acc1904cd70d Migrated to isp-idf v5.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
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
527
72
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
529
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
530 (void)tx_channel; // avoid compiler warning about unused parameter
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
531 (void)rx_channel; // avoid compiler warning about unused parameter
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
532
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
533 // sanity check
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
534 if (info == NULL) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
535 ESP_LOGE(TAG, "info is NULL");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
536 goto exit_err;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
537 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
538
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
539 // ----- receive channel -----
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
540
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
541 // channel config
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
542 const rmt_rx_channel_config_t rx_channel_config = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
543 .gpio_num = (int)gpio_num,
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
545 .resolution_hz = OWB_RMT_CLK_HZ,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
546 .mem_block_symbols = (size_t)OWB_RMT_RX_MEM_BLOCK_SYMBOLS,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
547 .flags = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
548 .invert_in = 0, // don't hardware invert the input
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
549 .with_dma = 0, // don't enable DMA
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
550 .io_loop_back = 0 // we define the loopback in the tx config
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
551 }
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
552 };
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
553
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
554 // request channel
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
557 ESP_LOGE(TAG, "err requesting rx_channel");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
558 goto exit_err;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
559 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
560
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
561 // create queue for RMT `rx_done` event data struct (from callback)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
562 info->rx_queue = xQueueCreate (1, sizeof (rmt_rx_done_event_data_t));
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
563 if (info->rx_queue == NULL) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
564 ESP_LOGE(TAG, "err creating rx_queue");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
565 goto exit_delete_rx_channel;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
566 }
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
567
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
568 // allocate rx symbol buffer for RMT driver
acc1904cd70d Migrated to isp-idf v5.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);
acc1904cd70d Migrated to isp-idf v5.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);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
571 if (info->rx_buffer == NULL) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
572 ESP_LOGE(TAG, "err allocating rx_buffer");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
573 goto exit_delete_rx_queue;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
574 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
575
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
576 // register rx channel callback (rx_queue is passed as user context)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
577 const rmt_rx_event_callbacks_t rmt_rx_event_callbacks = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
578 .on_recv_done = _recv_done_callback
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
579 };
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
581 ESP_LOGE(TAG, "err registering rx_callbacks");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
582 goto exit_release_rx_buffer;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
583 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
584
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
585 // enable channel
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
586 if (rmt_enable (info->rx_channel_handle) != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
587 ESP_LOGE(TAG, "err enabling rx_channel");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
588 goto exit_release_rx_buffer;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
589 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
590
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
591 // ----- transmit channel -----
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
592
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
593 // channel config
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
594 const rmt_tx_channel_config_t tx_channel_config = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
595 .gpio_num = (int)gpio_num,
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
597 .resolution_hz = OWB_RMT_CLK_HZ,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
598 .mem_block_symbols = (size_t)OWB_RMT_TX_MEM_BLOCK_SYMBOLS,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
599 .trans_queue_depth = OWB_RMT_TX_QUEUE_DEPTH,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
600 .flags = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
601 .invert_out = 1, // invert the output (so that the bus is initially released)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
602 .with_dma = 0, // don't enable DMA
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
603 .io_loop_back = 1, // enable reading of actual voltage of output pin
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
605 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
606 };
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
607
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
608 // request channel
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
610 ESP_LOGE(TAG, "err requesting tx_channel");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
611 goto exit_disable_rx_channel;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
612 }
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
613
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
614 // enable channel
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
615 if (rmt_enable (info->tx_channel_handle) != ESP_OK) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
616 ESP_LOGE(TAG, "err enabling tx_channel");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
617 goto exit_delete_tx_channel;
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
618 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
619
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.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"
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
623 ESP_LOGE(TAG, "err requesting copy encoder");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
624 goto exit_disable_tx_channel;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
625 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
626
72
acc1904cd70d Migrated to isp-idf v5.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)
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
628 const rmt_bytes_encoder_config_t rmt_bytes_encoder_config = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
629 .bit0 = OWB_RMT_SYMBOL_0BIT,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
630 .bit1 = OWB_RMT_SYMBOL_1BIT,
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
631 .flags = {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
632 .msb_first = 0 // onewire bus on-the-wire bit order is lsb first
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
633 }
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
634 };
acc1904cd70d Migrated to isp-idf v5.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) {
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
636 ESP_LOGE(TAG, "err requesting bytes encoder");
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
637 goto exit_delete_copy_encoder;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
638 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
639
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
640
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
641 // ----- success ------
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
642 info->gpio = gpio_num;
acc1904cd70d Migrated to isp-idf v5.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
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
644 ESP_LOGI(TAG, "%s: OK", __func__);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
645 return &(info->bus);
72
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
646
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
647 // ----- error: unwind allocated resources -----
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
648 exit_delete_copy_encoder:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
649 ESP_ERROR_CHECK(rmt_del_encoder(info->copy_encoder_handle));
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
650 exit_disable_tx_channel:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
651 ESP_ERROR_CHECK(rmt_disable (info->tx_channel_handle));
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
652 exit_delete_tx_channel:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
653 ESP_ERROR_CHECK(rmt_del_channel (info->tx_channel_handle));
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
654 exit_disable_rx_channel:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
655 ESP_ERROR_CHECK(rmt_disable (info->rx_channel_handle));
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
656 exit_release_rx_buffer:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
657 free (info->rx_buffer);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
658 exit_delete_rx_queue:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
659 vQueueDelete (info->rx_queue);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
660 exit_delete_rx_channel:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
661 ESP_ERROR_CHECK(rmt_del_channel (info->rx_channel_handle));
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
662 exit_err:
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
663 ESP_LOGE(TAG, "%s: failed", __func__);
acc1904cd70d Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
664 return NULL;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
665 }

mercurial