39
39
#include "src/common/pico_time/include/pico/time.h"
40
40
41
41
static uint8_t refcount = 0 ;
42
- static uint16_t * pulse_buffer = NULL ;
43
- static volatile uint16_t pulse_index = 0 ;
44
- static uint16_t pulse_length ;
45
- pwmio_pwmout_obj_t * pwmout_obj ;
46
- volatile uint16_t current_duty_cycle ;
47
- static uint32_t min_pulse = 0 ;
48
- static alarm_id_t cur_alarm ;
42
+ volatile alarm_id_t cur_alarm = 0 ;
49
43
50
44
void turn_off (uint8_t slice ) {
51
45
pwm_hw -> slice [slice ].ctr = 0 ;
@@ -56,24 +50,24 @@ void turn_off(uint8_t slice) {
56
50
pwm_hw -> slice [slice ].csr = 0 ;
57
51
}
58
52
59
- void pulse_finish (pwmio_pwmout_obj_t * carrier ) {
60
- pulse_index ++ ;
53
+ void pulse_finish (pulseio_pulseout_obj_t * self ) {
54
+ self -> pulse_index ++ ;
61
55
// Turn pwm pin off by setting duty cyle to 1.
62
- common_hal_pwmio_pwmout_set_duty_cycle (carrier ,1 );
63
- if (pulse_index >= pulse_length ) {
56
+ common_hal_pwmio_pwmout_set_duty_cycle (self -> carrier ,1 );
57
+ if (self -> pulse_index >= self -> pulse_length ) {
64
58
return ;
65
59
}
66
- if (pulse_index % 2 == 0 ) {
67
- common_hal_pwmio_pwmout_set_duty_cycle (carrier ,current_duty_cycle );
60
+ if (self -> pulse_index % 2 == 0 ) {
61
+ common_hal_pwmio_pwmout_set_duty_cycle (self -> carrier ,self -> current_duty_cycle );
68
62
}
69
- uint64_t delay = pulse_buffer [pulse_index ];
70
- if (delay < min_pulse ) {
71
- delay = min_pulse ;
63
+ uint64_t delay = self -> pulse_buffer [self -> pulse_index ];
64
+ if (delay < self -> min_pulse ) {
65
+ delay = self -> min_pulse ;
72
66
}
73
67
cur_alarm = 0 ;
74
68
// if the alarm cannot be set, try again with a longer delay
75
69
while (cur_alarm == 0 ) {
76
- cur_alarm = add_alarm_in_us (delay , pulseout_interrupt_handler , carrier , false);
70
+ cur_alarm = add_alarm_in_us (delay , pulseout_interrupt_handler , self , false);
77
71
delay = delay + 1 ;
78
72
}
79
73
}
@@ -94,15 +88,14 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
94
88
uint16_t duty_cycle ) {
95
89
96
90
refcount ++ ;
97
- pwmout_obj = (pwmio_pwmout_obj_t * )carrier ;
98
- current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle (pwmout_obj );
99
- pwm_set_enabled (carrier -> slice ,false);
100
- turn_off (carrier -> slice );
101
- common_hal_pwmio_pwmout_set_duty_cycle (pwmout_obj ,1 );
102
- self -> pin = carrier -> pin -> number ;
103
- self -> slice = carrier -> slice ;
104
- self -> carrier = pwmout_obj ;
105
- min_pulse = (1000000 / carrier -> actual_frequency );
91
+ self -> carrier = (pwmio_pwmout_obj_t * )carrier ;
92
+ self -> current_duty_cycle = common_hal_pwmio_pwmout_get_duty_cycle (self -> carrier );
93
+ pwm_set_enabled (self -> carrier -> slice ,false);
94
+ turn_off (self -> carrier -> slice );
95
+ common_hal_pwmio_pwmout_set_duty_cycle (self -> carrier ,1 );
96
+ self -> pin = self -> carrier -> pin -> number ;
97
+ self -> slice = self -> carrier -> slice ;
98
+ self -> min_pulse = (1000000 / self -> carrier -> actual_frequency );
106
99
}
107
100
108
101
bool common_hal_pulseio_pulseout_deinited (pulseio_pulseout_obj_t * self ) {
@@ -118,25 +111,24 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
118
111
}
119
112
120
113
void common_hal_pulseio_pulseout_send (pulseio_pulseout_obj_t * self , uint16_t * pulses , uint16_t length ) {
121
- pulse_buffer = pulses ;
122
- pulse_index = 0 ;
123
- pulse_length = length ;
114
+ self -> pulse_buffer = pulses ;
115
+ self -> pulse_index = 0 ;
116
+ self -> pulse_length = length ;
124
117
125
- common_hal_pwmio_pwmout_set_duty_cycle (self -> carrier ,current_duty_cycle );
118
+ common_hal_pwmio_pwmout_set_duty_cycle (self -> carrier ,self -> current_duty_cycle );
126
119
pwm_set_enabled (self -> slice ,true);
127
- uint64_t delay = pulse_buffer [0 ];
128
- if (delay < min_pulse ) {
129
- delay = min_pulse ;
120
+ uint64_t delay = self -> pulse_buffer [0 ];
121
+ if (delay < self -> min_pulse ) {
122
+ delay = self -> min_pulse ;
130
123
}
131
- alarm_id_t init_alarm = 0 ;
124
+ cur_alarm = 0 ;
132
125
// if the alarm cannot be set, try again with a longer delay
133
- while (init_alarm == 0 ) {
134
- init_alarm = add_alarm_in_us (delay , pulseout_interrupt_handler , self -> carrier , false);
126
+ while (cur_alarm == 0 ) {
127
+ cur_alarm = add_alarm_in_us (delay , pulseout_interrupt_handler , self , false);
135
128
delay = delay + 1 ;
136
129
}
137
- cur_alarm = init_alarm ;
138
130
139
- while (pulse_index < length ) {
131
+ while (self -> pulse_index < length ) {
140
132
// Do other things while we wait. The interrupts will handle sending the
141
133
// signal.
142
134
RUN_BACKGROUND_TASKS ;
0 commit comments