@@ -46,32 +46,30 @@ volatile uint16_t current_duty_cycle;
46
46
static uint32_t min_pulse = 0 ;
47
47
static alarm_id_t cur_alarm ;
48
48
49
- void pulse_finish (void ) {
49
+ void pulse_finish (pwmio_pwmout_obj_t * carrier ) {
50
50
pulse_index ++ ;
51
- // Turn off the current alarm
52
- cancel_alarm (cur_alarm );
53
51
// Turn pwm pin off by setting duty cyle to 1.
54
- common_hal_pwmio_pwmout_set_duty_cycle (pwmout_obj ,1 );
52
+ common_hal_pwmio_pwmout_set_duty_cycle (carrier ,1 );
55
53
if (pulse_index >= pulse_length ) {
56
54
return ;
57
55
}
56
+ if (pulse_index % 2 == 0 ) {
57
+ common_hal_pwmio_pwmout_set_duty_cycle (carrier ,current_duty_cycle );
58
+ }
58
59
uint64_t delay = pulse_buffer [pulse_index ];
59
60
if (delay < min_pulse ) {
60
61
delay = min_pulse ;
61
62
}
62
63
cur_alarm = 0 ;
63
64
// if the alarm cannot be set, try again with a longer delay
64
65
while (cur_alarm == 0 ) {
65
- cur_alarm = add_alarm_in_us (delay , pulseout_interrupt_handler , NULL , false);
66
+ cur_alarm = add_alarm_in_us (delay , pulseout_interrupt_handler , carrier , false);
66
67
delay = delay + 1 ;
67
68
}
68
- if (pulse_index % 2 == 0 ) {
69
- common_hal_pwmio_pwmout_set_duty_cycle (pwmout_obj ,current_duty_cycle );
70
- }
71
69
}
72
70
73
71
int64_t pulseout_interrupt_handler (alarm_id_t id , void * user_data ) {
74
- pulse_finish ();
72
+ pulse_finish (user_data );
75
73
return 0 ;
76
74
}
77
75
@@ -88,10 +86,12 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
88
86
refcount ++ ;
89
87
pwmout_obj = (pwmio_pwmout_obj_t * )carrier ;
90
88
current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle (pwmout_obj );
89
+ pwm_set_enabled (carrier -> slice ,false);
90
+ common_hal_pwmio_pwmout_set_duty_cycle (pwmout_obj ,1 );
91
91
self -> pin = carrier -> pin -> number ;
92
92
self -> slice = carrier -> slice ;
93
- pwm_set_enabled ( pwmout_obj -> slice ,false) ;
94
- min_pulse = (1000000 / pwmout_obj -> actual_frequency ) / 2 ;
93
+ self -> carrier = pwmout_obj ;
94
+ min_pulse = (1000000 / carrier -> actual_frequency ) / 2 ;
95
95
}
96
96
97
97
bool common_hal_pulseio_pulseout_deinited (pulseio_pulseout_obj_t * self ) {
@@ -111,9 +111,19 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
111
111
pulse_index = 0 ;
112
112
pulse_length = length ;
113
113
114
- add_alarm_in_us (pulses [0 ], pulseout_interrupt_handler , NULL , false);
115
- common_hal_pwmio_pwmout_set_duty_cycle (pwmout_obj ,current_duty_cycle );
116
- pwm_set_enabled (pwmout_obj -> slice ,true);
114
+ common_hal_pwmio_pwmout_set_duty_cycle (self -> carrier ,current_duty_cycle );
115
+ pwm_set_enabled (self -> slice ,true);
116
+ uint64_t delay = pulse_buffer [0 ];
117
+ if (delay < min_pulse ) {
118
+ delay = min_pulse ;
119
+ }
120
+ alarm_id_t init_alarm = 0 ;
121
+ // if the alarm cannot be set, try again with a longer delay
122
+ while (init_alarm == 0 ) {
123
+ init_alarm = add_alarm_in_us (delay , pulseout_interrupt_handler , self -> carrier , false);
124
+ delay = delay + 1 ;
125
+ }
126
+ cur_alarm = init_alarm ;
117
127
118
128
while (pulse_index < length ) {
119
129
// Do other things while we wait. The interrupts will handle sending the
@@ -122,5 +132,5 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu
122
132
}
123
133
// Short delay to give pin time to settle before disabling PWM
124
134
common_hal_mcu_delay_us (min_pulse );
125
- pwm_set_enabled (pwmout_obj -> slice ,false);
135
+ pwm_set_enabled (self -> slice ,false);
126
136
}
0 commit comments