|
24 | 24 | * THE SOFTWARE.
|
25 | 25 | */
|
26 | 26 |
|
| 27 | +#include "common-hal/pulseio/PulseOut.h" |
27 | 28 |
|
28 | 29 | #include <stdint.h>
|
29 | 30 |
|
| 31 | +#include <pwm.h> |
| 32 | + |
| 33 | +#include "ets_alt_task.h" |
| 34 | +#include "py/obj.h" |
30 | 35 | #include "py/runtime.h"
|
| 36 | +#include "mpconfigport.h" |
31 | 37 | #include "shared-bindings/pulseio/PulseOut.h"
|
32 | 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 | +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 | + |
33 | 69 | void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
|
34 | 70 | 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; |
36 | 72 | }
|
37 | 73 |
|
38 | 74 | bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
|
39 |
| - return true; |
| 75 | + return self->channel == NO_CHANNEL; |
40 | 76 | }
|
41 | 77 |
|
42 | 78 | 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); |
43 | 82 | }
|
44 | 83 |
|
45 | 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"); |
| 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; |
46 | 108 | }
|
0 commit comments