Skip to content

Commit 6af1fba

Browse files
committed
Work on pulseio.PulseOut for #716 ESP8266
Switch to ets_delay_us but the PWM is still way too slow to be useful.
1 parent 011edf2 commit 6af1fba

File tree

2 files changed

+10
-54
lines changed

2 files changed

+10
-54
lines changed

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

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,73 +36,29 @@
3636
#include "mpconfigport.h"
3737
#include "shared-bindings/pulseio/PulseOut.h"
3838

39-
#define NO_CHANNEL (255)
40-
41-
static uint16_t *pulse_buffer = NULL;
42-
static volatile uint16_t pulse_index = 0;
43-
static uint16_t pulse_length;
44-
static volatile uint32_t current_compare = 0;
45-
4639
void pulseout_set(pulseio_pulseout_obj_t *self, bool state) {
47-
// XXX double kludge
48-
//uint32_t duty = state ? pwm_get_period() * 11 : 0;
49-
uint32_t duty = state ? 1000 : 500;
50-
pwm_set_duty(duty, self->channel);
51-
pwm_start();
52-
}
53-
54-
void pulseout_interrupt_handler(void *data) {
55-
pulseio_pulseout_obj_t *self = data;
56-
57-
if (pulse_buffer == NULL || self->channel == NO_CHANNEL) return;
58-
if (pulse_index >= pulse_length) return;
59-
pulse_index++;
60-
pulseout_set(self, pulse_index % 2 == 0);
61-
62-
os_timer_arm(&self->timer, pulse_buffer[pulse_index], 0);
63-
}
64-
65-
void pulseout_reset() {
66-
pulse_buffer = NULL;
40+
PIN_FUNC_SELECT(self->pin->peripheral, state ? self->pin->gpio_function : 0);
6741
}
6842

6943
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
7044
const pulseio_pwmout_obj_t* carrier) {
71-
self->channel = carrier->channel;
45+
self->pin = carrier->pin;
7246
}
7347

7448
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
75-
return self->channel == NO_CHANNEL;
49+
return self->pin == NULL;
7650
}
7751

7852
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
79-
os_timer_disarm(&self->timer);
80-
self->channel = NO_CHANNEL;
53+
self->pin = NULL;
8154
pulseout_set(self, true);
8255
}
8356

84-
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {
85-
if (pulse_buffer != NULL) {
86-
mp_raise_RuntimeError("Another send is already active");
57+
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self,
58+
uint16_t* pulses, uint16_t length) {
59+
for (uint16_t i = 0; i<length; i++) {
60+
pulseout_set(self, i % 2 == 0);
61+
ets_delay_us(pulses[i]);
8762
}
88-
pulse_buffer = pulses;
89-
pulse_index = 0;
90-
pulse_length = length;
91-
92-
os_timer_disarm(&self->timer);
93-
os_timer_setfn(&self->timer, pulseout_interrupt_handler, self);
94-
os_timer_arm(&self->timer, pulse_buffer[0], 0);
95-
pulseout_set(self, true);
96-
97-
// XXX in the circumstances, is it worth messing with os_timer?
98-
// it isn't especially accurate anyway ...
99-
// might it not be simpler to just call mp_hal_delay_us() a lot?
100-
while(pulse_index < length) {
101-
ets_loop_iter();
102-
}
103-
104-
os_timer_disarm(&self->timer);
10563
pulseout_set(self, false);
106-
107-
pulse_buffer = NULL;
10864
}

ports/esp8266/common-hal/pulseio/PulseOut.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
typedef struct {
3737
mp_obj_base_t base;
3838
os_timer_t timer;
39-
uint8_t channel;
39+
const mcu_pin_obj_t *pin;
4040
} pulseio_pulseout_obj_t;
4141

4242
void pulseout_reset(void);

0 commit comments

Comments
 (0)