@@ -64,23 +64,25 @@ STATIC uint32_t calculate_pwm_parameters(uint32_t sample_rate, uint32_t *top_out
64
64
}
65
65
66
66
STATIC void activate_audiopwmout_obj (audiopwmio_pwmaudioout_obj_t * self ) {
67
- for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ ) {
68
- if (!active_audio [i ]) {
67
+ for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ ) {
68
+ if (!active_audio [i ]) {
69
69
active_audio [i ] = self ;
70
70
break ;
71
71
}
72
72
}
73
73
}
74
74
STATIC void deactivate_audiopwmout_obj (audiopwmio_pwmaudioout_obj_t * self ) {
75
- for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ ) {
76
- if (active_audio [i ] == self )
75
+ for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ ) {
76
+ if (active_audio [i ] == self ) {
77
77
active_audio [i ] = NULL ;
78
+ }
78
79
}
79
80
}
80
81
81
82
void audiopwmout_reset () {
82
- for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ )
83
+ for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ ) {
83
84
active_audio [i ] = NULL ;
85
+ }
84
86
}
85
87
86
88
STATIC void fill_buffers (audiopwmio_pwmaudioout_obj_t * self , int buf ) {
@@ -97,25 +99,25 @@ STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) {
97
99
}
98
100
uint32_t num_samples = buffer_length / self -> bytes_per_sample / self -> spacing ;
99
101
100
- if (self -> bytes_per_sample == 1 ) {
102
+ if (self -> bytes_per_sample == 1 ) {
101
103
uint8_t offset = self -> signed_to_unsigned ? 0x80 : 0 ;
102
104
uint16_t scale = self -> scale ;
103
- for (uint32_t i = 0 ; i < buffer_length /self -> spacing ; i ++ ) {
105
+ for (uint32_t i = 0 ; i < buffer_length /self -> spacing ; i ++ ) {
104
106
uint8_t rawval = (* buffer ++ + offset );
105
107
uint16_t val = (uint16_t )(((uint32_t )rawval * (uint32_t )scale ) >> 8 );
106
108
* dev_buffer ++ = val ;
107
- if (self -> spacing == 1 )
109
+ if (self -> spacing == 1 )
108
110
* dev_buffer ++ = val ;
109
111
}
110
112
} else {
111
113
uint16_t offset = self -> signed_to_unsigned ? 0x8000 : 0 ;
112
114
uint16_t scale = self -> scale ;
113
115
uint16_t * buffer16 = (uint16_t * )buffer ;
114
- for (uint32_t i = 0 ; i < buffer_length /2 /self -> spacing ; i ++ ) {
116
+ for (uint32_t i = 0 ; i < buffer_length /2 /self -> spacing ; i ++ ) {
115
117
uint16_t rawval = (* buffer16 ++ + offset );
116
118
uint16_t val = (uint16_t )((rawval * (uint32_t )scale ) >> 16 );
117
119
* dev_buffer ++ = val ;
118
- if (self -> spacing == 1 )
120
+ if (self -> spacing == 1 )
119
121
* dev_buffer ++ = val ;
120
122
}
121
123
}
@@ -124,30 +126,30 @@ STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) {
124
126
125
127
if (self -> loop && get_buffer_result == GET_BUFFER_DONE ) {
126
128
audiosample_reset_buffer (self -> sample , false, 0 );
127
- } else if (get_buffer_result == GET_BUFFER_DONE ) {
129
+ } else if (get_buffer_result == GET_BUFFER_DONE ) {
128
130
self -> pwm -> SHORTS = NRF_PWM_SHORT_SEQEND0_STOP_MASK | NRF_PWM_SHORT_SEQEND1_STOP_MASK ;
129
131
self -> stopping = true;
130
132
}
131
133
}
132
134
133
135
STATIC void audiopwmout_background_obj (audiopwmio_pwmaudioout_obj_t * self ) {
134
- if (!common_hal_audiopwmio_pwmaudioout_get_playing (self ))
136
+ if (!common_hal_audiopwmio_pwmaudioout_get_playing (self ))
135
137
return ;
136
- if (self -> stopping ) {
138
+ if (self -> stopping ) {
137
139
bool stopped =
138
140
(self -> pwm -> EVENTS_SEQEND [0 ] || !self -> pwm -> EVENTS_SEQSTARTED [0 ]) &&
139
141
(self -> pwm -> EVENTS_SEQEND [1 ] || !self -> pwm -> EVENTS_SEQSTARTED [1 ]);
140
- if (stopped )
142
+ if (stopped )
141
143
self -> pwm -> TASKS_STOP = 1 ;
142
- } else if (!self -> paused && !self -> single_buffer ) {
143
- if (self -> pwm -> EVENTS_SEQSTARTED [0 ]) fill_buffers (self , 1 );
144
- if (self -> pwm -> EVENTS_SEQSTARTED [1 ]) fill_buffers (self , 0 );
144
+ } else if (!self -> paused && !self -> single_buffer ) {
145
+ if (self -> pwm -> EVENTS_SEQSTARTED [0 ]) fill_buffers (self , 1 );
146
+ if (self -> pwm -> EVENTS_SEQSTARTED [1 ]) fill_buffers (self , 0 );
145
147
}
146
148
}
147
149
148
150
void audiopwmout_background () {
149
- for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ ) {
150
- if (!active_audio [i ]) continue ;
151
+ for (size_t i = 0 ; i < MP_ARRAY_SIZE (active_audio ); i ++ ) {
152
+ if (!active_audio [i ]) continue ;
151
153
audiopwmout_background_obj (active_audio [i ]);
152
154
}
153
155
}
@@ -157,7 +159,7 @@ void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t* s
157
159
assert_pin_free (left_channel );
158
160
assert_pin_free (right_channel );
159
161
self -> pwm = pwmout_allocate (256 , PWM_PRESCALER_PRESCALER_DIV_1 , true, NULL , NULL );
160
- if (!self -> pwm ) {
162
+ if (!self -> pwm ) {
161
163
mp_raise_RuntimeError (translate ("All timers in use" ));
162
164
}
163
165
@@ -172,7 +174,7 @@ void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t* s
172
174
self -> pwm -> PSEL .OUT [0 ] = self -> left_channel_number = left_channel -> number ;
173
175
claim_pin (left_channel );
174
176
175
- if (right_channel )
177
+ if (right_channel )
176
178
{
177
179
self -> pwm -> PSEL .OUT [2 ] = self -> right_channel_number = right_channel -> number ;
178
180
claim_pin (right_channel );
@@ -192,12 +194,14 @@ void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t* self
192
194
if (common_hal_audiopwmio_pwmaudioout_deinited (self )) {
193
195
return ;
194
196
}
197
+ deactivate_audiopwmout_obj (self );
198
+
195
199
// TODO: ramp the pwm down from quiescent value to 0
196
200
self -> pwm -> ENABLE = 0 ;
197
201
198
- if (self -> left_channel_number )
202
+ if (self -> left_channel_number )
199
203
reset_pin_number (self -> left_channel_number );
200
- if (self -> right_channel_number )
204
+ if (self -> right_channel_number )
201
205
reset_pin_number (self -> right_channel_number );
202
206
203
207
pwmout_free_channel (self -> pwm , 0 );
@@ -230,12 +234,12 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t* self,
230
234
audiosample_get_buffer_structure (sample , /* single channel */ false,
231
235
& self -> single_buffer , & self -> signed_to_unsigned , & max_buffer_length ,
232
236
& self -> spacing );
233
- if (max_buffer_length > UINT16_MAX ) {
237
+ if (max_buffer_length > UINT16_MAX ) {
234
238
mp_raise_ValueError_varg (translate ("Buffer length %d too big. It must be less than %d" ), max_buffer_length , UINT16_MAX );
235
239
}
236
240
self -> buffer_length = (uint16_t )max_buffer_length ;
237
241
self -> buffers [0 ] = m_malloc (self -> buffer_length * 2 * sizeof (uint16_t ), false);
238
- if (!self -> single_buffer )
242
+ if (!self -> single_buffer )
239
243
self -> buffers [1 ] = m_malloc (self -> buffer_length * 2 * sizeof (uint16_t ), false);
240
244
241
245
@@ -274,7 +278,7 @@ void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t* self)
274
278
}
275
279
276
280
bool common_hal_audiopwmio_pwmaudioout_get_playing (audiopwmio_pwmaudioout_obj_t * self ) {
277
- if (!self -> paused && self -> pwm -> EVENTS_STOPPED ) {
281
+ if (!self -> paused && self -> pwm -> EVENTS_STOPPED ) {
278
282
self -> playing = false;
279
283
self -> pwm -> EVENTS_STOPPED = 0 ;
280
284
}
0 commit comments