Skip to content

Commit c4cf1c5

Browse files
committed
Initial pulseio.PulseOut implementation for #716 ESP8266
1 parent 7adc69b commit c4cf1c5

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,85 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "common-hal/pulseio/PulseOut.h"
2728

2829
#include <stdint.h>
2930

31+
#include <pwm.h>
32+
33+
#include "ets_alt_task.h"
34+
#include "py/obj.h"
3035
#include "py/runtime.h"
36+
#include "mpconfigport.h"
3137
#include "shared-bindings/pulseio/PulseOut.h"
3238

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+
46+
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;
67+
}
68+
3369
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
3470
const pulseio_pwmout_obj_t* carrier) {
35-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut."));
71+
self->channel = carrier->channel;
3672
}
3773

3874
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
39-
return true;
75+
return self->channel == NO_CHANNEL;
4076
}
4177

4278
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
79+
os_timer_disarm(&self->timer);
80+
self->channel = NO_CHANNEL;
81+
pulseout_set(self, true);
4382
}
4483

4584
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");
87+
}
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);
105+
pulseout_set(self, false);
106+
107+
pulse_buffer = NULL;
46108
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@
2727
#ifndef MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H
2828
#define MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H
2929

30+
#include "common-hal/microcontroller/Pin.h"
31+
32+
#include "esp_mphal.h"
33+
3034
#include "py/obj.h"
3135

3236
typedef struct {
3337
mp_obj_base_t base;
38+
os_timer_t timer;
39+
uint8_t channel;
3440
} pulseio_pulseout_obj_t;
3541

36-
void pwmout_reset(void);
42+
void pulseout_reset(void);
43+
void pulseout_interrupt_handler(void *);
3744

3845
#endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H

0 commit comments

Comments
 (0)