@@ -110,137 +110,138 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t *self,
110
110
} else if (clock_pin == & pin_PB11 ) {
111
111
self -> clock_unit = 1 ;
112
112
#endif
113
- #endif
113
+ #else
114
114
#ifdef SAM_D5X_E5X
115
115
if (clock_pin == & pin_PA10 || clock_pin == & pin_PB16 ) {
116
116
self -> clock_unit = 0 ;
117
117
} else if (clock_pin == & pin_PB12
118
118
#if defined(PIN_PB28 ) & & !defined (IGNORE_PIN_PB28 )
119
- || data_pin == & pin_PB28 ) {
120
- #else
119
+ || data_pin == & pin_PB28
120
+ #endif
121
121
) {
122
- #endif
123
122
self -> clock_unit = 1 ;
124
123
#endif
125
- } else {
126
- raise_ValueError_invalid_pin_name (MP_QSTR_clock );
127
- }
124
+ #endif
125
+ } else {
126
+ raise_ValueError_invalid_pin_name (MP_QSTR_clock );
127
+ }
128
128
129
- self -> data_pin = data_pin ; // PA07, PA19 -> SD0, PA08, PB16 -> SD1
129
+ self -> data_pin = data_pin ; // PA07, PA19 -> SD0, PA08, PB16 -> SD1
130
130
131
131
#ifdef SAMD21
132
- if (false
132
+ if (false
133
133
#if defined(PIN_PA07 ) && !defined(IGNORE_PIN_PA07 )
134
- || data_pin == & pin_PA07
134
+ || data_pin == & pin_PA07
135
135
#endif
136
136
#if defined(PIN_PA19 ) && !defined(IGNORE_PIN_PA19 )
137
- || data_pin == & pin_PA19
137
+ || data_pin == & pin_PA19
138
138
#endif
139
- ) {
140
- self -> serializer = 0 ;
141
- } else if (false
142
- #if defined(PIN_PA08 ) && !defined(IGNORE_PIN_PA08 )
143
- || data_pin == & pin_PA08
144
- #endif
145
- #if defined(PIN_PB16 ) && !defined(IGNORE_PIN_PB16 )
146
- || data_pin == & pin_PB16
147
- #endif
148
- ) {
149
- self -> serializer = 1 ;
150
- #endif
139
+ ) {
140
+ self -> serializer = 0 ;
141
+ } else if (false
142
+ #if defined(PIN_PA08 ) && !defined(IGNORE_PIN_PA08 )
143
+ || data_pin == & pin_PA08
144
+ #endif
145
+ #if defined(PIN_PB16 ) && !defined(IGNORE_PIN_PB16 )
146
+ || data_pin == & pin_PB16
147
+ #endif
148
+ ) {
149
+ self -> serializer = 1 ;
150
+ #else
151
151
#ifdef SAM_D5X_E5X
152
- if (data_pin == & pin_PB10 || data_pin == & pin_PA22 ) {
153
- self -> serializer = 1 ;
152
+ if (data_pin == & pin_PB10 || data_pin == & pin_PA22 ) {
153
+ self -> serializer = 1 ;
154
154
#endif
155
- }else {
156
- raise_ValueError_invalid_pin_name (MP_QSTR_data );
157
- }
155
+ #endif
156
+ } else {
157
+ raise_ValueError_invalid_pin_name (MP_QSTR_data );
158
+ }
158
159
159
- if (!(bit_depth == 16 || bit_depth == 8 ) || !mono || oversample != OVERSAMPLING ) {
160
- mp_raise_NotImplementedError (translate ("Only 8 or 16 bit mono with " MP_STRINGIFY (OVERSAMPLING ) "x oversampling is supported." ));
161
- }
160
+ if (!(bit_depth == 16 || bit_depth == 8 ) || !mono || oversample != OVERSAMPLING ) {
161
+ mp_raise_NotImplementedError (translate ("Only 8 or 16 bit mono with " MP_STRINGIFY (OVERSAMPLING ) "x oversampling is supported." ));
162
+ }
162
163
163
- turn_on_i2s ();
164
+ turn_on_i2s ();
164
165
165
- if (I2S -> CTRLA .bit .ENABLE == 0 ) {
166
- I2S -> CTRLA .bit .SWRST = 1 ;
167
- while (I2S -> CTRLA .bit .SWRST == 1 ) {
168
- }
169
- } else {
170
- #ifdef SAMD21
171
- if ((I2S -> CTRLA .vec .SEREN & (1 << self -> serializer )) != 0 ) {
172
- mp_raise_RuntimeError (translate ("Serializer in use" ));
173
- }
174
- #endif
175
- #ifdef SAM_D5X_E5X
176
- if (I2S -> CTRLA .bit .RXEN == 1 ) {
177
- mp_raise_RuntimeError (translate ("Serializer in use" ));
166
+ if (I2S -> CTRLA .bit .ENABLE == 0 ) {
167
+ I2S -> CTRLA .bit .SWRST = 1 ;
168
+ while (I2S -> CTRLA .bit .SWRST == 1 ) {
169
+ }
170
+ } else {
171
+ #ifdef SAMD21
172
+ if ((I2S -> CTRLA .vec .SEREN & (1 << self -> serializer )) != 0 ) {
173
+ mp_raise_RuntimeError (translate ("Serializer in use" ));
174
+ }
175
+ #endif
176
+ #ifdef SAM_D5X_E5X
177
+ if (I2S -> CTRLA .bit .RXEN == 1 ) {
178
+ mp_raise_RuntimeError (translate ("Serializer in use" ));
179
+ }
180
+ #endif
178
181
}
179
- #endif
180
- }
181
182
#ifdef SAM_D5X_E5X
182
183
#define GPIO_I2S_FUNCTION GPIO_PIN_FUNCTION_J
183
184
#endif
184
185
#ifdef SAMD21
185
186
#define GPIO_I2S_FUNCTION GPIO_PIN_FUNCTION_G
186
187
#endif
187
188
188
- uint32_t clock_divisor = (uint32_t )roundf (48000000.0f / sample_rate / oversample );
189
- float mic_clock_freq = 48000000.0f / clock_divisor ;
190
- self -> sample_rate = mic_clock_freq / oversample ;
191
- if (mic_clock_freq < MIN_MIC_CLOCK || clock_divisor == 0 ) {
192
- mp_raise_ValueError (translate ("sampling rate out of range" ));
193
- }
194
- // Find a free GCLK to generate the MCLK signal.
195
- uint8_t gclk = find_free_gclk (clock_divisor );
196
- if (gclk > GCLK_GEN_NUM ) {
197
- mp_raise_RuntimeError (translate ("Unable to find free GCLK" ));
198
- }
199
- self -> gclk = gclk ;
189
+ uint32_t clock_divisor = (uint32_t )roundf (48000000.0f / sample_rate / oversample );
190
+ float mic_clock_freq = 48000000.0f / clock_divisor ;
191
+ self -> sample_rate = mic_clock_freq / oversample ;
192
+ if (mic_clock_freq < MIN_MIC_CLOCK || clock_divisor == 0 ) {
193
+ mp_raise_ValueError (translate ("sampling rate out of range" ));
194
+ }
195
+ // Find a free GCLK to generate the MCLK signal.
196
+ uint8_t gclk = find_free_gclk (clock_divisor );
197
+ if (gclk > GCLK_GEN_NUM ) {
198
+ mp_raise_RuntimeError (translate ("Unable to find free GCLK" ));
199
+ }
200
+ self -> gclk = gclk ;
200
201
201
- enable_clock_generator (self -> gclk , CLOCK_48MHZ , clock_divisor );
202
- connect_gclk_to_peripheral (self -> gclk , I2S_GCLK_ID_0 + self -> clock_unit );
202
+ enable_clock_generator (self -> gclk , CLOCK_48MHZ , clock_divisor );
203
+ connect_gclk_to_peripheral (self -> gclk , I2S_GCLK_ID_0 + self -> clock_unit );
203
204
204
- // Clock unit configuration
205
+ // Clock unit configuration
205
206
206
- uint32_t clkctrl = I2S_CLKCTRL_MCKSEL_GCLK |
207
- I2S_CLKCTRL_NBSLOTS (2 ) |
208
- I2S_CLKCTRL_FSWIDTH_SLOT |
209
- I2S_CLKCTRL_SLOTSIZE_16 ;
207
+ uint32_t clkctrl = I2S_CLKCTRL_MCKSEL_GCLK |
208
+ I2S_CLKCTRL_NBSLOTS (2 ) |
209
+ I2S_CLKCTRL_FSWIDTH_SLOT |
210
+ I2S_CLKCTRL_SLOTSIZE_16 ;
210
211
211
- // Serializer configuration
212
+ // Serializer configuration
212
213
#ifdef SAMD21
213
- uint32_t serctrl = (self -> clock_unit << I2S_SERCTRL_CLKSEL_Pos ) | SERCTRL (SERMODE_PDM2 ) | SERCTRL (DATASIZE_32 );
214
+ uint32_t serctrl = (self -> clock_unit << I2S_SERCTRL_CLKSEL_Pos ) | SERCTRL (SERMODE_PDM2 ) | SERCTRL (DATASIZE_32 );
214
215
#endif
215
216
#ifdef SAM_D5X_E5X
216
- uint32_t serctrl = (self -> clock_unit << I2S_RXCTRL_CLKSEL_Pos ) | SERCTRL (SERMODE_PDM2 ) | SERCTRL (DATASIZE_32 );
217
+ uint32_t serctrl = (self -> clock_unit << I2S_RXCTRL_CLKSEL_Pos ) | SERCTRL (SERMODE_PDM2 ) | SERCTRL (DATASIZE_32 );
217
218
#endif
218
219
219
- // Configure the I2S peripheral
220
- i2s_set_enable (false);
220
+ // Configure the I2S peripheral
221
+ i2s_set_enable (false);
221
222
222
- I2S -> CLKCTRL [self -> clock_unit ].reg = clkctrl ;
223
+ I2S -> CLKCTRL [self -> clock_unit ].reg = clkctrl ;
223
224
#ifdef SAMD21
224
- I2S -> SERCTRL [self -> serializer ].reg = serctrl ;
225
+ I2S -> SERCTRL [self -> serializer ].reg = serctrl ;
225
226
#endif
226
227
#ifdef SAM_D5X_E5X
227
- I2S -> RXCTRL .reg = serctrl ;
228
+ I2S -> RXCTRL .reg = serctrl ;
228
229
#endif
229
230
230
- i2s_set_enable (true);
231
+ i2s_set_enable (true);
231
232
232
- // Run the clock all the time. This eliminates startup delay for the microphone,
233
- // which can be 10-100ms. Turn serializer on as needed.
234
- i2s_set_clock_unit_enable (self -> clock_unit , true);
233
+ // Run the clock all the time. This eliminates startup delay for the microphone,
234
+ // which can be 10-100ms. Turn serializer on as needed.
235
+ i2s_set_clock_unit_enable (self -> clock_unit , true);
235
236
236
- claim_pin (clock_pin );
237
- claim_pin (data_pin );
237
+ claim_pin (clock_pin );
238
+ claim_pin (data_pin );
238
239
239
- gpio_set_pin_function (self -> clock_pin -> number , GPIO_I2S_FUNCTION );
240
- gpio_set_pin_function (self -> data_pin -> number , GPIO_I2S_FUNCTION );
240
+ gpio_set_pin_function (self -> clock_pin -> number , GPIO_I2S_FUNCTION );
241
+ gpio_set_pin_function (self -> data_pin -> number , GPIO_I2S_FUNCTION );
241
242
242
- self -> bytes_per_sample = oversample >> 3 ;
243
- self -> bit_depth = bit_depth ;
243
+ self -> bytes_per_sample = oversample >> 3 ;
244
+ self -> bit_depth = bit_depth ;
244
245
}
245
246
246
247
bool common_hal_audiobusio_pdmin_deinited (audiobusio_pdmin_obj_t * self ) {
0 commit comments