@@ -45,7 +45,10 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t *digitalinout,
45
45
uint8_t * pixels , uint32_t num_bytes ) {
46
46
// Wait to make sure we don't append onto the last transmission. This should only be a tick or
47
47
// two.
48
- while (port_get_raw_ticks (NULL ) < next_start_raw_ticks ) {
48
+ int icnt ;
49
+ while ((port_get_raw_ticks (NULL ) < next_start_raw_ticks ) &&
50
+ (next_start_raw_ticks - port_get_raw_ticks (NULL ) < 100 )) {
51
+ RUN_BACKGROUND_TASKS ;
49
52
}
50
53
51
54
BP_Function_Enum alt_function = GPIO_FUNCTION_OUTPUT ;
@@ -92,7 +95,8 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t *digitalinout,
92
95
93
96
// Wait for the clock to start up.
94
97
COMPLETE_MEMORY_READS ;
95
- while (CM_PWM -> CS_b .BUSY == 0 ) {
98
+ icnt = 0 ;
99
+ while ((CM_PWM -> CS_b .BUSY == 0 ) && (icnt ++ < 1000 )) {
96
100
}
97
101
}
98
102
@@ -134,24 +138,45 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t *digitalinout,
134
138
expanded |= 0x80000000 ;
135
139
}
136
140
}
137
- while (pwm -> STA_b .FULL1 == 1 ) {
138
- RUN_BACKGROUND_TASKS ;
139
- }
140
141
if (channel == 1 ) {
142
+ icnt = 0 ;
143
+ while ((pwm -> STA_b .FULL1 == 1 ) && (icnt ++ < 150 )) {
144
+ RUN_BACKGROUND_TASKS ;
145
+ }
141
146
// Dummy value for the first channel.
142
147
pwm -> FIF1 = 0x000000 ;
143
148
}
149
+ icnt = 0 ;
150
+ while ((pwm -> STA_b .FULL1 == 1 ) && (icnt ++ < 150 )) {
151
+ RUN_BACKGROUND_TASKS ;
152
+ }
144
153
pwm -> FIF1 = expanded ;
145
154
if (channel == 0 ) {
155
+ icnt = 0 ;
156
+ while ((pwm -> STA_b .FULL1 == 1 ) && (icnt ++ < 150 )) {
157
+ RUN_BACKGROUND_TASKS ;
158
+ }
146
159
// Dummy value for the second channel.
147
160
pwm -> FIF1 = 0x000000 ;
148
161
}
149
162
}
150
- // Wait just a little bit so that transmission can start.
151
- common_hal_mcu_delay_us (2 );
152
- while (pwm -> STA_b .STA1 == 1 ) {
163
+
164
+ icnt = 0 ;
165
+ while ((pwm -> STA_b .EMPT1 == 0 ) && (icnt ++ < 2500 )) {
166
+ RUN_BACKGROUND_TASKS ;
167
+ }
168
+ // Wait for transmission to start.
169
+ icnt = 0 ;
170
+ while (((pwm -> STA_b .STA1 == 0 ) && (pwm -> STA_b .STA2 == 0 )) && (icnt ++ < 150 )) {
153
171
RUN_BACKGROUND_TASKS ;
154
172
}
173
+ // Wait for transmission to complete.
174
+ icnt = 0 ;
175
+ while (((pwm -> STA_b .STA1 == 1 ) || (pwm -> STA_b .STA2 == 1 )) && (icnt ++ < 150 )) {
176
+ RUN_BACKGROUND_TASKS ;
177
+ }
178
+ // Shouldn't be anything left in queue but clear it so the clock doesn't crash if there is
179
+ pwm -> CTL = PWM0_CTL_CLRF1_Msk ;
155
180
156
181
gpio_set_function (digitalinout -> pin -> number , GPIO_FUNCTION_OUTPUT );
157
182
0 commit comments