Skip to content

Commit 130640f

Browse files
committed
Add RMT cleanup
1 parent 3e72b9b commit 130640f

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/m5_unit_component/adapter_gpio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_analog(uint16_t& value,
462462
}
463463

464464
m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::pulse_in(uint32_t& duration, const gpio_num_t pin, const int state,
465-
const uint32_t timeout_us = 30000)
465+
const uint32_t timeout_us)
466466
{
467467
duration = 0;
468468
auto start = esp_timer_get_time();

src/m5_unit_component/adapter_gpio.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AdapterGPIOBase : public Adapter {
8787
}
8888

8989
inline virtual m5::hal::error::error_t pulseInRX(uint32_t& duration, const int state,
90-
const uint32_t timeout_us) override
90+
const uint32_t timeout_us = 30000) override
9191
{
9292
return pulse_in(duration, rx_pin(), state, timeout_us);
9393
}
@@ -114,7 +114,7 @@ class AdapterGPIOBase : public Adapter {
114114
return read_analog(v, tx_pin());
115115
}
116116
inline virtual m5::hal::error::error_t pulseInTX(uint32_t& duration, const int state,
117-
const uint32_t timeout_us) override
117+
const uint32_t timeout_us = 30000) override
118118
{
119119
return pulse_in(duration, tx_pin(), state, timeout_us);
120120
}

src/m5_unit_component/adapter_gpio_v1.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ bool declrare_use_rmt_channel(const int ch)
3838
return false;
3939
}
4040

41+
void clear_use_rmt_channel(const int ch)
42+
{
43+
if (ch >= 0 && ch < RMT_CHANNEL_MAX) {
44+
using_rmt_channel_bits &= ~(1U << ch);
45+
}
46+
}
47+
4148
rmt_config_t to_rmt_config_tx(const adapter_config_t& cfg, const uint32_t apb_freq_hz)
4249
{
4350
rmt_config_t out{};
@@ -78,6 +85,16 @@ class GPIOImplV1 : public AdapterGPIOBase::GPIOImpl {
7885
GPIOImplV1(const int8_t rx_pin, const int8_t tx_pin) : AdapterGPIOBase::GPIOImpl(rx_pin, tx_pin)
7986
{
8087
}
88+
virtual ~GPIOImplV1()
89+
{
90+
rmt_tx_stop(_tx_config.channel);
91+
rmt_driver_uninstall(_tx_config.channel);
92+
clear_use_rmt_channel(_tx_config.channel);
93+
94+
rmt_rx_stop(_rx_config.channel);
95+
rmt_driver_uninstall(_rx_config.channel);
96+
clear_use_rmt_channel(_rx_config.channel);
97+
}
8198

8299
virtual bool begin(const gpio::adapter_config_t& cfg) override
83100
{
@@ -137,8 +154,8 @@ class GPIOImplV1 : public AdapterGPIOBase::GPIOImpl {
137154
}
138155
return err == ESP_OK ? m5::hal::error::error_t::OK : m5::hal::error::error_t::UNKNOWN_ERROR;
139156
}
140-
M5_LIB_LOGE("Failed invalid config");
141157

158+
M5_LIB_LOGE("Failed invalid config");
142159
return m5::hal::error::error_t::UNKNOWN_ERROR;
143160
}
144161

src/m5_unit_component/adapter_gpio_v2.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ class GPIOImplV2 : public AdapterGPIO::GPIOImpl {
4848
GPIOImplV2(const int8_t rx_pin, const int8_t tx_pin) : AdapterGPIOBase::GPIOImpl(rx_pin, tx_pin)
4949
{
5050
}
51+
virtual ~GPIOImplV2()
52+
{
53+
if (_tx_handle) {
54+
rmt_disable(_tx_handle);
55+
rmt_del_channel(_tx_handle);
56+
}
57+
if (_rx_handle) {
58+
rmt_disable(_rx_handle);
59+
rmt_del_channel(_rx_handle);
60+
}
61+
}
5162

5263
bool begin(const gpio::adapter_config_t& cfg)
5364
{
@@ -88,6 +99,9 @@ class GPIOImplV2 : public AdapterGPIO::GPIOImpl {
8899
rmt_transmit_config_t tx_config = {};
89100
auto err = rmt_transmit(_tx_handle, copy_encoder, (gpio::m5_rmt_item_t*)data, len * sizeof(gpio::m5_rmt_item_t),
90101
&tx_config);
102+
if (err != ESP_OK) {
103+
M5_LIB_LOGE("Failed to transmit %d:%s", err, esp_err_to_name(err));
104+
}
91105
if (err == ESP_OK && waitMs) {
92106
err = rmt_tx_wait_all_done(_tx_handle, waitMs);
93107
if (err != ESP_OK) {

0 commit comments

Comments
 (0)