Skip to content

Commit 0fc730b

Browse files
committed
Expand PulseOut API, debug cleanup
1 parent 88fcc19 commit 0fc730b

File tree

12 files changed

+102
-103
lines changed

12 files changed

+102
-103
lines changed

ports/atmel-samd/common-hal/pulseio/PulseOut.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ void pulseout_reset() {
9696
}
9797

9898
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
99-
const pulseio_pwmout_obj_t* carrier) {
99+
const pulseio_pwmout_obj_t* carrier,
100+
const mcu_pin_obj_t* pin,
101+
uint32_t frequency,
102+
uint16_t duty_cycle) {
103+
if (!carrier || pin || frequency) {
104+
mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \
105+
Construct and pass a PWM Carrier instead"));
106+
}
107+
100108
if (refcount == 0) {
101109
// Find a spare timer.
102110
Tc *tc = NULL;

ports/cxd56/common-hal/pulseio/PulseOut.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,16 @@ static bool pulseout_timer_handler(unsigned int *next_interval_us, void *arg)
5858
return true;
5959
}
6060

61-
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
62-
const pulseio_pwmout_obj_t *carrier) {
61+
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
62+
const pulseio_pwmout_obj_t* carrier,
63+
const mcu_pin_obj_t* pin,
64+
uint32_t frequency,
65+
uint16_t duty_cycle) {
66+
if (!carrier || pin || frequency) {
67+
mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \
68+
Construct and pass a PWM Carrier instead"));
69+
}
70+
6371
if (pulse_fd < 0) {
6472
pulse_fd = open("/dev/timer0", O_RDONLY);
6573
}

ports/esp32s2/common-hal/pulseio/PulseIn.c

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "common-hal/pulseio/PulseIn.h"
28+
#include "shared-bindings/microcontroller/__init__.h"
2829
#include "py/runtime.h"
2930

3031
STATIC uint8_t refcount = 0;
@@ -33,17 +34,11 @@ STATIC pulseio_pulsein_obj_t * handles[RMT_CHANNEL_MAX];
3334
// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset
3435

3536
STATIC void update_internal_buffer(pulseio_pulsein_obj_t* self) {
36-
//mp_printf(&mp_plat_print, "Update internal Buffer\n");
3737
uint32_t length = 0;
3838
rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 0);
3939
if (items) {
4040
length /= 4;
41-
//mp_printf(&mp_plat_print, "Length%d\n",length);
42-
// TODO: If the size of the recieve is larger than the buffer, reset it?
43-
4441
for (size_t i=0; i < length; i++) {
45-
//mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3),
46-
// items[i].level0,(items[i].duration1 * 3),items[i].level1);
4742
uint16_t pos = (self->start + self->len) % self->maxlen;
4843
self->buffer[pos] = items[i].duration0 * 3;
4944
// Check if second item exists before incrementing
@@ -69,25 +64,16 @@ STATIC void update_internal_buffer(pulseio_pulsein_obj_t* self) {
6964
// We can't access the RMT interrupt, so we need a global service to prevent
7065
// the ringbuffer from overflowing and crashing the peripheral
7166
void pulsein_background(void) {
72-
//mp_printf(&mp_plat_print, "BG Task!\n");
7367
for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) {
7468
if (handles[i]) {
75-
//mp_printf(&mp_plat_print, "Located viable handle:%d\n",i);
7669
update_internal_buffer(handles[i]);
7770
UBaseType_t items_waiting;
7871
vRingbufferGetInfo(handles[i]->buf_handle, NULL, NULL, NULL, NULL, &items_waiting);
79-
//mp_printf(&mp_plat_print, "items waiting:%d\n",items_waiting);
80-
// if (items_waiting > handles[i]->maxlen) {
81-
// mp_printf(&mp_plat_print, "Overage!\n");
82-
// mp_printf(&mp_plat_print, "Items waiting detected:%d\n", items_waiting);
83-
// update_internal_buffer(handles[i]);
84-
// }
8572
}
8673
}
8774
}
8875

8976
void pulsein_reset(void) {
90-
mp_printf(&mp_plat_print, "Pulsein Reset called\n");
9177
for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) {
9278
handles[i] = NULL;
9379
}
@@ -103,48 +89,48 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
10389
}
10490
self->pin = pin;
10591
self->maxlen = maxlen;
106-
// self->idle_state = idle_state;
92+
self->idle_state = idle_state;
10793
self->start = 0;
10894
self->len = 0;
109-
// self->first_edge = true;
11095
self->paused = false;
111-
// self->last_overflow = 0;
112-
// self->last_count = 0;
11396

114-
rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt();
115-
mp_printf(&mp_plat_print, "Selected Channel:%d!\n",channel);
97+
// Set pull settings
98+
gpio_pullup_dis(pin->number);
99+
gpio_pulldown_dis(pin->number);
100+
if (idle_state) {
101+
gpio_pullup_en(pin->number);
102+
} else {
103+
gpio_pulldown_en(pin->number);
104+
}
116105

117-
// Configure Channel
106+
// Find a free RMT Channel and configure it
107+
rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt();
118108
rmt_config_t config = RMT_DEFAULT_CONFIG_RX(pin->number, channel);
119109
config.rx_config.filter_en = true;
120-
config.rx_config.idle_threshold = 30000;
121-
config.clk_div = 240;
122-
110+
config.rx_config.idle_threshold = 30000; // 30*3=90ms idle required to register a sequence
111+
config.clk_div = 240; // All measurements are divided by 3 to accomodate 65ms pulses
123112
rmt_config(&config);
124-
size_t len = 1000; //TODO: pick a reasonable number for this?
125-
// size_t len = maxlen * 4;
126-
rmt_driver_install(channel, len, 0);
113+
rmt_driver_install(channel, 1000, 0); //TODO: pick a more specific buffer size?
127114

115+
// Store this object and the buffer handle for background updates
128116
self->channel = channel;
129-
130-
// Store handle for background updates
131117
handles[channel] = self;
132-
133118
rmt_get_ringbuf_handle(channel, &(self->buf_handle));
134-
rmt_rx_start(channel, true);
135119

120+
// start RMT RX, and enable ticks so the core doesn't turn off.
121+
rmt_rx_start(channel, true);
136122
supervisor_enable_tick();
137123
refcount++;
138124
}
139125

140126
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
141-
return handles[self->channel] ? true : false;
127+
return handles[self->channel] ? false : true;
142128
}
143129

144130
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
145131
handles[self->channel] = NULL;
146132
esp32s2_peripherals_free_rmt(self->channel);
147-
reset_pin_number(self->pin);
133+
reset_pin_number(self->pin->number);
148134
refcount--;
149135
if (refcount == 0) {
150136
supervisor_disable_tick();
@@ -153,7 +139,7 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
153139

154140
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
155141
self->paused = true;
156-
rmt_rx_stop();
142+
rmt_rx_stop(self->channel);
157143
}
158144

159145
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) {
@@ -162,17 +148,25 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t tri
162148
common_hal_pulseio_pulsein_pause(self);
163149
}
164150

151+
if (trigger_duration > 0) {
152+
gpio_set_direction(self->pin->number, GPIO_MODE_DEF_OUTPUT);
153+
gpio_set_level(self->pin->number, !self->idle_state);
154+
common_hal_mcu_delay_us((uint32_t)trigger_duration);
155+
gpio_set_level(self->pin->number, self->idle_state);
156+
gpio_set_direction(self->pin->number, GPIO_MODE_INPUT); // should revert to pull direction
157+
}
158+
165159
self->paused = false;
166-
rmt_rx_start();
160+
rmt_rx_start(self->channel, false);
167161
}
168162

169163
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
164+
// Buffer only updates in BG tasks or fetches, so no extra protection is needed
170165
self->start = 0;
171166
self->len = 0;
172167
}
173168

174169
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) {
175-
//mp_printf(&mp_plat_print, "Call GetItem\n");
176170
update_internal_buffer(self);
177171
if (index < 0) {
178172
index += self->len;
@@ -185,7 +179,6 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_
185179
}
186180

187181
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
188-
mp_printf(&mp_plat_print, "Call PopLeft\n");
189182
update_internal_buffer(self);
190183

191184
if (self->len == 0) {
@@ -197,28 +190,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
197190
self->len--;
198191

199192
return value;
200-
201-
202-
// uint32_t length = 0;
203-
// rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 10);
204-
// mp_printf(&mp_plat_print, "Length%d\n",length);
205-
// if (items) {
206-
// length /= 4;
207-
// mp_printf(&mp_plat_print, "Length%d\n",length);
208-
// for (size_t i=0; i < length; i++) {
209-
// mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3),
210-
// items[i].level0,(items[i].duration1 * 3),items[i].level1);
211-
// }
212-
// vRingbufferReturnItem(self->buf_handle, (void *) items);
213-
// }
214-
// // while(items) {
215-
// // mp_printf(&mp_plat_print, "Length%d",length);
216-
// // mp_printf(&mp_plat_print, "Item val0:%d, val1:%d, val:%d",items->duration0,
217-
// // items->duration1,items->val);
218-
// // vRingbufferReturnItem(self->buf_handle, (void *) items);
219-
// // items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 1);
220-
// // }
221-
// return items ? items[0].duration0 : false;
222193
}
223194

224195
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {

ports/esp32s2/common-hal/pulseio/PulseIn.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,12 @@ typedef struct {
4242
bool paused;
4343

4444
RingbufHandle_t buf_handle;
45-
volatile bool first_edge;
4645

4746
uint16_t* buffer;
4847
uint16_t maxlen;
4948

5049
volatile uint16_t start;
5150
volatile uint16_t len;
52-
volatile uint32_t last_overflow;
53-
volatile uint16_t last_count;
5451
} pulseio_pulsein_obj_t;
5552

5653
void pulsein_reset(void);

ports/esp32s2/common-hal/pulseio/PulseOut.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@
3232
// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset
3333

3434
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
35-
const mcu_pin_obj_t* pin,
36-
uint32_t frequency) {
35+
const pulseio_pwmout_obj_t* carrier,
36+
const mcu_pin_obj_t* pin,
37+
uint32_t frequency,
38+
uint16_t duty_cycle) {
39+
if (carrier || !pin || !frequency) {
40+
mp_raise_NotImplementedError(translate("Port does not accept PWM carrier. \
41+
Pass a pin, frequency and duty cycle instead"));
42+
}
3743

3844
rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt();
3945

4046
// Configure Channel
4147
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin->number, channel);
4248
config.tx_config.carrier_en = true;
43-
config.tx_config.carrier_duty_percent = 50;
49+
config.tx_config.carrier_duty_percent = (duty_cycle * 100) / (1<<16);
4450
config.tx_config.carrier_freq_hz = frequency;
4551
config.clk_div = 80;
4652

ports/esp32s2/mpconfigport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636

3737
#include "py/circuitpy_mpconfig.h"
3838

39-
#define CPY_PULSEOUT_USES_DIGITALIO (1)
40-
4139
#define MICROPY_PORT_ROOT_POINTERS \
4240
CIRCUITPY_COMMON_ROOT_POINTERS
4341
#define MICROPY_NLR_SETJMP (1)

ports/esp32s2/peripherals/rmt.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
bool rmt_reserved_channels[RMT_CHANNEL_MAX];
3131

3232
void esp32s2_peripherals_rmt_reset(void) {
33-
mp_printf(&mp_plat_print, "RMT Reset called\n");
3433
for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) {
3534
if (rmt_reserved_channels[i]) {
3635
esp32s2_peripherals_free_rmt(i);

ports/mimxrt10xx/common-hal/pulseio/PulseOut.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ void pulseout_reset() {
9494
}
9595

9696
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
97-
const pulseio_pwmout_obj_t* carrier) {
97+
const pulseio_pwmout_obj_t* carrier,
98+
const mcu_pin_obj_t* pin,
99+
uint32_t frequency,
100+
uint16_t duty_cycle) {
98101
// if (refcount == 0) {
99102
// // Find a spare timer.
100103
// Tc *tc = NULL;

ports/nrf/common-hal/pulseio/PulseOut.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,15 @@ void pulseout_reset() {
100100
}
101101

102102
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
103-
const pulseio_pwmout_obj_t* carrier) {
103+
const pulseio_pwmout_obj_t* carrier,
104+
const mcu_pin_obj_t* pin,
105+
uint32_t frequency,
106+
uint16_t duty_cycle) {
107+
if (!carrier || pin || frequency) {
108+
mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \
109+
Construct and pass a PWM Carrier instead"));
110+
}
111+
104112
if (refcount == 0) {
105113
timer = nrf_peripherals_allocate_timer_or_throw();
106114
}

ports/stm/common-hal/pulseio/PulseOut.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@ void pulseout_reset() {
113113
}
114114

115115
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
116-
const pulseio_pwmout_obj_t* carrier) {
116+
const pulseio_pwmout_obj_t* carrier,
117+
const mcu_pin_obj_t* pin,
118+
uint32_t frequency,
119+
uint16_t duty_cycle) {
120+
if (!carrier || pin || frequency) {
121+
mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \
122+
Construct and pass a PWM Carrier instead"));
123+
}
124+
117125
// Add to active PulseOuts
118126
refcount++;
119127
TIM_TypeDef * tim_instance = stm_peripherals_find_timer();

0 commit comments

Comments
 (0)