@@ -139,11 +139,15 @@ bool convert_frequency(uint32_t frequency, uint16_t *countertop, nrf_pwm_clk_t *
139139 return false;
140140}
141141
142+ // We store these in an array because we cannot compute them.
143+ static IRQn_Type pwm_irqs [4 ] = {PWM0_IRQn , PWM1_IRQn , PWM2_IRQn , PWM3_IRQn };
144+
142145NRF_PWM_Type * pwmout_allocate (uint16_t countertop , nrf_pwm_clk_t base_clock ,
143- bool variable_frequency , int8_t * channel_out , bool * pwm_already_in_use_out ) {
146+ bool variable_frequency , int8_t * channel_out , bool * pwm_already_in_use_out ,
147+ IRQn_Type * irq ) {
144148 for (size_t pwm_index = 0 ; pwm_index < MP_ARRAY_SIZE (pwms ); pwm_index ++ ) {
145149 NRF_PWM_Type * pwm = pwms [pwm_index ];
146- bool pwm_already_in_use = pwm -> ENABLE & SPIM_ENABLE_ENABLE_Msk ;
150+ bool pwm_already_in_use = pwm -> ENABLE & PWM_ENABLE_ENABLE_Msk ;
147151 if (pwm_already_in_use ) {
148152 if (variable_frequency ) {
149153 // Variable frequency requires exclusive use of a PWM, so try the next one.
@@ -156,20 +160,30 @@ NRF_PWM_Type *pwmout_allocate(uint16_t countertop, nrf_pwm_clk_t base_clock,
156160 for (size_t chan = 0 ; chan < CHANNELS_PER_PWM ; chan ++ ) {
157161 if (pwm -> PSEL .OUT [chan ] == 0xFFFFFFFF ) {
158162 // Channel is free.
159- if (channel_out )
163+ if (channel_out ) {
160164 * channel_out = chan ;
161- if (pwm_already_in_use_out )
165+ }
166+ if (pwm_already_in_use_out ) {
162167 * pwm_already_in_use_out = pwm_already_in_use ;
168+ }
169+ if (irq ) {
170+ * irq = pwm_irqs [pwm_index ];
171+ }
163172 return pwm ;
164173 }
165174 }
166175 }
167176 } else {
168177 // PWM not yet in use, so we can start to use it. Use channel 0.
169- if (channel_out )
178+ if (channel_out ) {
170179 * channel_out = 0 ;
171- if (pwm_already_in_use_out )
180+ }
181+ if (pwm_already_in_use_out ) {
172182 * pwm_already_in_use_out = pwm_already_in_use ;
183+ }
184+ if (irq ) {
185+ * irq = pwm_irqs [pwm_index ];
186+ }
173187 return pwm ;
174188 }
175189 }
@@ -208,7 +222,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
208222 int8_t channel ;
209223 bool pwm_already_in_use ;
210224 self -> pwm = pwmout_allocate (countertop , base_clock , variable_frequency ,
211- & channel , & pwm_already_in_use );
225+ & channel , & pwm_already_in_use , NULL );
212226
213227 if (self -> pwm == NULL ) {
214228 return PWMOUT_ALL_TIMERS_IN_USE ;
0 commit comments