|
36 | 36 | #include "mpconfigport.h" |
37 | 37 | #include "shared-bindings/pulseio/PulseOut.h" |
38 | 38 |
|
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 | 39 | 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); |
67 | 41 | } |
68 | 42 |
|
69 | 43 | void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, |
70 | 44 | const pulseio_pwmout_obj_t* carrier) { |
71 | | - self->channel = carrier->channel; |
| 45 | + self->pin = carrier->pin; |
72 | 46 | } |
73 | 47 |
|
74 | 48 | bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) { |
75 | | - return self->channel == NO_CHANNEL; |
| 49 | + return self->pin == NULL; |
76 | 50 | } |
77 | 51 |
|
78 | 52 | 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; |
81 | 54 | pulseout_set(self, true); |
82 | 55 | } |
83 | 56 |
|
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]); |
87 | 62 | } |
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 | 63 | pulseout_set(self, false); |
106 | | - |
107 | | - pulse_buffer = NULL; |
108 | 64 | } |
0 commit comments