forked from adafruit/Adafruit_ZeroI2S
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_ZeroI2S.cpp
executable file
·548 lines (485 loc) · 15.7 KB
/
Adafruit_ZeroI2S.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*!
* @file Adafruit_ZeroI2S.cpp
*
* @mainpage Adafruit I2S peripheral driver for SAMD21 and SAMD51 chips
*
* @section intro_sec Introduction
*
* I2S peripheral driver for SAMD21 and SAMD51 chips
*
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section author Author
*
* Written by Dean Miller for Adafruit Industries.
*
* @section license License
*
* BSD license, all text here must be included in any redistribution.
*
*/
#include "Adafruit_ZeroI2S.h"
#include "wiring_private.h"
#ifndef DEBUG_PRINTLN
#define DEBUG_PRINTLN Serial.println ///< where to print the debug output
#endif
/**************************************************************************/
/*!
@brief Class Constructor
@param FS_PIN frame sync pin
@param SCK_PIN bit clock pin
@param TX_PIN data output pin
@param RX_PIN data input pin
*/
/**************************************************************************/
Adafruit_ZeroI2S::Adafruit_ZeroI2S(uint8_t FS_PIN, uint8_t SCK_PIN, uint8_t TX_PIN, uint8_t RX_PIN) : _fs(FS_PIN), _sck(SCK_PIN), _tx(TX_PIN), _rx(RX_PIN) {}
#if defined(PIN_I2S_SDI) && defined(PIN_I2S_SDO)
/**************************************************************************/
/*!
@brief Class Constructor with defaults
*/
/**************************************************************************/
Adafruit_ZeroI2S::Adafruit_ZeroI2S() : _fs(PIN_I2S_FS), _sck(PIN_I2S_SCK), _tx(PIN_I2S_SDO), _rx(PIN_I2S_SDI) {}
#else
/**************************************************************************/
/*!
@brief Class Constructor with defaults
*/
/**************************************************************************/
Adafruit_ZeroI2S::Adafruit_ZeroI2S() : _fs(PIN_I2S_FS), _sck(PIN_I2S_SCK), _tx(PIN_I2S_SD) {
_rx = -1;
}
#endif
/**************************************************************************/
/*!
@brief start up the I2S peripheral
@param width the width of each I2S frame
@param fs_freq the frame sync frequency (a.k.a. sample rate)
@param mck_mult master clock output will be fs_freq * mck_mult for chips that have a mclk.
@returns true on success, false on any error
*/
/**************************************************************************/
bool Adafruit_ZeroI2S::begin(I2SSlotSize width, int fs_freq, int mck_mult)
{
#if defined(__SAMD51__)
pinPeripheral(_fs, PIO_I2S);
pinPeripheral(_sck, PIO_I2S);
pinPeripheral(_rx, PIO_I2S);
pinPeripheral(_tx, PIO_I2S);
I2S->CTRLA.bit.ENABLE = 0;
//initialize clock control
MCLK->APBDMASK.reg |= MCLK_APBDMASK_I2S;
uint32_t mckFreq = (fs_freq * mck_mult);
uint32_t sckFreq = fs_freq * I2S_NUM_SLOTS * ( (width + 1) << 3);
uint32_t gclkval = GCLK_PCHCTRL_GEN_GCLK1_Val;
uint32_t gclkFreq = VARIANT_GCLK1_FREQ;
uint8_t mckoutdiv = min( (gclkFreq/mckFreq) - 1, 63);
uint8_t mckdiv = min( (gclkFreq/sckFreq) - 1, 63 );
if(((VARIANT_GCLK1_FREQ/mckFreq) - 1) > 63){
gclkval = GCLK_PCHCTRL_GEN_GCLK4_Val;
gclkFreq = 12000000;
}
GCLK->PCHCTRL[I2S_GCLK_ID_0].reg = gclkval | (1 << GCLK_PCHCTRL_CHEN_Pos);
GCLK->PCHCTRL[I2S_GCLK_ID_1].reg = gclkval | (1 << GCLK_PCHCTRL_CHEN_Pos);
//software reset
I2S->CTRLA.bit.SWRST = 1;
while(I2S->SYNCBUSY.bit.SWRST || I2S->SYNCBUSY.bit.ENABLE); //wait for sync
//CLKCTRL[0] is used for the tx channel
I2S->CLKCTRL[0].reg = I2S_CLKCTRL_MCKSEL_GCLK |
I2S_CLKCTRL_MCKOUTDIV(mckoutdiv) |
I2S_CLKCTRL_MCKDIV(mckdiv) |
I2S_CLKCTRL_SCKSEL_MCKDIV |
I2S_CLKCTRL_MCKEN |
I2S_CLKCTRL_FSSEL_SCKDIV |
I2S_CLKCTRL_BITDELAY_I2S |
I2S_CLKCTRL_FSWIDTH_HALF |
I2S_CLKCTRL_NBSLOTS(I2S_NUM_SLOTS - 1) |
I2S_CLKCTRL_SLOTSIZE(width);
uint8_t wordSize;
switch(width){
case I2S_8_BIT:
wordSize = I2S_TXCTRL_DATASIZE_8_Val;
break;
case I2S_16_BIT:
wordSize = I2S_TXCTRL_DATASIZE_16_Val;
break;
case I2S_24_BIT:
wordSize = I2S_TXCTRL_DATASIZE_24_Val;
break;
case I2S_32_BIT:
wordSize = I2S_TXCTRL_DATASIZE_32_Val;
break;
}
I2S->TXCTRL.reg = I2S_TXCTRL_DMA_SINGLE |
I2S_TXCTRL_MONO_STEREO |
I2S_TXCTRL_BITREV_MSBIT |
I2S_TXCTRL_EXTEND_ZERO |
I2S_TXCTRL_WORDADJ_RIGHT |
I2S_TXCTRL_DATASIZE(wordSize) |
I2S_TXCTRL_TXSAME_ZERO |
I2S_TXCTRL_TXDEFAULT_ZERO;
I2S->RXCTRL.reg = I2S_RXCTRL_DMA_SINGLE |
I2S_RXCTRL_MONO_STEREO |
I2S_RXCTRL_BITREV_MSBIT |
I2S_RXCTRL_EXTEND_ZERO |
I2S_RXCTRL_WORDADJ_RIGHT |
I2S_RXCTRL_DATASIZE(wordSize) |
I2S_RXCTRL_SLOTADJ_RIGHT |
I2S_RXCTRL_CLKSEL_CLK0 |
I2S_RXCTRL_SERMODE_RX;
while(I2S->SYNCBUSY.bit.ENABLE); //wait for sync
I2S->CTRLA.bit.ENABLE = 1;
return true;
#else //SAMD21
_i2sserializer = -1;
_i2sclock = -1;
uint32_t _clk_pin, _clk_mux, _data_pin, _data_mux, _fs_pin, _fs_mux;
// Clock pin, can only be one of 3 options
uint32_t clockport = g_APinDescription[_sck].ulPort;
uint32_t clockpin = g_APinDescription[_sck].ulPin;
if ((clockport == 0) && (clockpin == 10)) {
// PA10
_i2sclock = 0;
_clk_pin = PIN_PA10G_I2S_SCK0;
_clk_mux = MUX_PA10G_I2S_SCK0;
} else if ((clockport == 1) && (clockpin == 10)) {
// PB11
_i2sclock = 1;
_clk_pin = PIN_PB11G_I2S_SCK1;
_clk_mux = MUX_PB11G_I2S_SCK1;
} else if ((clockport == 0) && (clockpin == 20)) {
// PA20
_i2sclock = 0;
_clk_pin = PIN_PA20G_I2S_SCK0;
_clk_mux = MUX_PA20G_I2S_SCK0;
} else {
DEBUG_PRINTLN("Clock isnt on a valid pin");
return false;
}
pinPeripheral(_sck, (EPioType)_clk_mux);
// FS pin, can only be one of 2 options
uint32_t fsport = g_APinDescription[_fs].ulPort;
uint32_t fspin = g_APinDescription[_fs].ulPin;
if ((fsport == 0) && (fspin == 11)) {
// PA11
_fs_pin = PIN_PA11G_I2S_FS0;
_fs_mux = MUX_PA11G_I2S_FS0;
} else if ((fsport == 0) && (fspin == 21)) {
// PA20
_fs_pin = PIN_PA21G_I2S_FS0;
_fs_mux = MUX_PA21G_I2S_FS0;
} else {
DEBUG_PRINTLN("FS isnt on a valid pin");
return false;
}
pinPeripheral(_fs, (EPioType)_fs_mux);
uint32_t i2sGCLK;
if(_i2sclock == 0) i2sGCLK = I2S_GCLK_ID_0;
else i2sGCLK = I2S_GCLK_ID_1;
uint32_t divider = fs_freq * 2 * (width + 1) * 8;
// configure the clock divider
while (GCLK->STATUS.bit.SYNCBUSY);
GCLK->GENDIV.bit.ID = I2S_CLOCK_GENERATOR;
GCLK->GENDIV.bit.DIV = SystemCoreClock / divider;
// use the DFLL as the source
while (GCLK->STATUS.bit.SYNCBUSY);
GCLK->GENCTRL.bit.ID = I2S_CLOCK_GENERATOR;
GCLK->GENCTRL.bit.SRC = GCLK_GENCTRL_SRC_DFLL48M_Val;
GCLK->GENCTRL.bit.IDC = 1;
GCLK->GENCTRL.bit.GENEN = 1;
// enable
while (GCLK->STATUS.bit.SYNCBUSY);
GCLK->CLKCTRL.bit.ID = i2sGCLK;
GCLK->CLKCTRL.bit.GEN = I2S_CLOCK_GENERATOR;
GCLK->CLKCTRL.bit.CLKEN = 1;
while (GCLK->STATUS.bit.SYNCBUSY);
// Data pin, can only be one of 3 options
uint32_t datapin = g_APinDescription[_tx].ulPin;
uint32_t dataport = g_APinDescription[_tx].ulPort;
if ((dataport == 0) && (datapin == 7)) {
// PA07
_i2sserializer = 0;
_data_pin = PIN_PA07G_I2S_SD0;
_data_mux = MUX_PA07G_I2S_SD0;
} else if ((dataport == 0) && (datapin == 8)) {
// PA08
_i2sserializer = 1;
_data_pin = PIN_PA08G_I2S_SD1;
_data_mux = MUX_PA08G_I2S_SD1;
} else if ((dataport == 0) && (datapin == 19)) {
// PA19
_i2sserializer = 0;
_data_pin = PIN_PA19G_I2S_SD0;
_data_mux = MUX_PA19G_I2S_SD0;
} else {
DEBUG_PRINTLN("Data isnt on a valid pin");
return false;
}
pinPeripheral(_tx, (EPioType)_data_mux);
PM->APBCMASK.reg |= PM_APBCMASK_I2S;
I2S->CTRLA.bit.ENABLE = 0;
while(I2S->SYNCBUSY.bit.ENABLE);
if(_i2sclock == 0) I2S->CTRLA.bit.CKEN0 = 0;
else I2S->CTRLA.bit.CKEN1 = 0;
while(I2S->SYNCBUSY.bit.CKEN0 || I2S->SYNCBUSY.bit.CKEN1);
I2S->CLKCTRL[_i2sclock].reg = I2S_CLKCTRL_MCKSEL_GCLK |
I2S_CLKCTRL_SCKSEL_MCKDIV |
I2S_CLKCTRL_FSSEL_SCKDIV |
I2S_CLKCTRL_BITDELAY_I2S |
I2S_CLKCTRL_NBSLOTS(I2S_NUM_SLOTS - 1) |
I2S_CLKCTRL_SLOTSIZE(width);
uint8_t wordSize;
switch(width){
case I2S_8_BIT:
wordSize = I2S_SERCTRL_DATASIZE_8;
break;
case I2S_16_BIT:
wordSize = I2S_SERCTRL_DATASIZE_16;
break;
case I2S_24_BIT:
wordSize = I2S_SERCTRL_DATASIZE_24;
break;
case I2S_32_BIT:
wordSize = I2S_SERCTRL_DATASIZE_32;
break;
default:
DEBUG_PRINTLN("invalid width!");
return false;
}
if(_i2sserializer == 0) I2S->CTRLA.bit.SEREN0 = 0;
else I2S->CTRLA.bit.SEREN1 = 0;
while(I2S->SYNCBUSY.bit.SEREN0 || I2S->SYNCBUSY.bit.SEREN1);
I2S->SERCTRL[_i2sserializer].reg = I2S_SERCTRL_DMA_SINGLE |
I2S_SERCTRL_MONO_STEREO |
I2S_SERCTRL_BITREV_MSBIT |
I2S_SERCTRL_EXTEND_ZERO |
I2S_SERCTRL_WORDADJ_RIGHT |
I2S_SERCTRL_DATASIZE(wordSize) |
I2S_SERCTRL_SLOTADJ_RIGHT |
((uint32_t)_i2sclock << I2S_SERCTRL_CLKSEL_Pos);
return true;
#endif
}
/**************************************************************************/
/*!
@brief enable data output. Note that on SAMD21 chips either rx or tx can be enabled on an Adafruit_ZeroI2S instance,
while on SAMD51 the same Adafruit_ZeroI2S instance can have both rx and tx channels enabled.
*/
/**************************************************************************/
void Adafruit_ZeroI2S::enableTx()
{
#if defined(__SAMD51__)
I2S->CTRLA.bit.CKEN0 = 1;
while(I2S->SYNCBUSY.bit.CKEN0);
I2S->CTRLA.bit.TXEN = 1;
while(I2S->SYNCBUSY.bit.TXEN);
#else
if(_i2sserializer > -1 && _i2sclock > -1){
I2S->CTRLA.bit.ENABLE = 0;
while(I2S->SYNCBUSY.bit.ENABLE);
I2S->SERCTRL[_i2sserializer].bit.SERMODE = I2S_SERCTRL_SERMODE_TX;
if(_i2sserializer == 0) I2S->CTRLA.bit.SEREN0 = 1;
else I2S->CTRLA.bit.SEREN1 = 1;
if(_i2sclock == 0) I2S->CTRLA.bit.CKEN0 = 1;
else I2S->CTRLA.bit.CKEN1 = 1;
I2S->CTRLA.bit.ENABLE = 1;
while(I2S->SYNCBUSY.bit.ENABLE || I2S->SYNCBUSY.bit.CKEN0 ||
I2S->SYNCBUSY.bit.CKEN1 || I2S->SYNCBUSY.bit.SEREN0 ||
I2S->SYNCBUSY.bit.SEREN1);
}
#endif
}
/**************************************************************************/
/*!
@brief disable data output
*/
/**************************************************************************/
void Adafruit_ZeroI2S::disableTx()
{
#if defined(__SAMD51__)
I2S->CTRLA.bit.TXEN = 0;
while(I2S->SYNCBUSY.bit.TXEN);
#else
#endif
}
/**************************************************************************/
/*!
@brief enable data input. Note that on SAMD21 chips either rx or tx can be enabled on an Adafruit_ZeroI2S instance,
while on SAMD51 the same Adafruit_ZeroI2S instance can have both rx and tx channels enabled.
*/
/**************************************************************************/
void Adafruit_ZeroI2S::enableRx()
{
#if defined(__SAMD51__)
I2S->CTRLA.bit.CKEN0 = 1;
while(I2S->SYNCBUSY.bit.CKEN0);
I2S->CTRLA.bit.RXEN = 1;
while(I2S->SYNCBUSY.bit.RXEN);
#else
if(_i2sserializer > -1 && _i2sclock > -1){
I2S->CTRLA.bit.ENABLE = 0;
while(I2S->SYNCBUSY.bit.ENABLE);
I2S->SERCTRL[_i2sserializer].bit.SERMODE = I2S_SERCTRL_SERMODE_RX;
if(_i2sserializer == 0) I2S->CTRLA.bit.SEREN0 = 1;
else I2S->CTRLA.bit.SEREN1 = 1;
if(_i2sclock == 0) I2S->CTRLA.bit.CKEN0 = 1;
else I2S->CTRLA.bit.CKEN1 = 1;
I2S->CTRLA.bit.ENABLE = 1;
while(I2S->SYNCBUSY.bit.ENABLE || I2S->SYNCBUSY.bit.CKEN0 ||
I2S->SYNCBUSY.bit.CKEN1 || I2S->SYNCBUSY.bit.SEREN0 ||
I2S->SYNCBUSY.bit.SEREN1);
}
#endif
}
/**************************************************************************/
/*!
@brief disable data input
*/
/**************************************************************************/
void Adafruit_ZeroI2S::disableRx()
{
#if defined(__SAMD51__)
I2S->CTRLA.bit.RXEN = 0;
while(I2S->SYNCBUSY.bit.RXEN);
#else
#endif
}
/**************************************************************************/
/*!
@brief enable master clock output on devices that have a master clock output.
*/
/**************************************************************************/
void Adafruit_ZeroI2S::enableMCLK()
{
#ifdef PIN_I2S_MCK
pinPeripheral(PIN_I2S_MCK, PIO_I2S);
#endif
}
/**************************************************************************/
/*!
@brief disable master clock output on devices that have a master clock output.
*/
/**************************************************************************/
void Adafruit_ZeroI2S::disableMCLK()
{
#ifdef PIN_I2S_MCK
pinMode(PIN_I2S_MCK, INPUT);
#endif
}
/**************************************************************************/
/*!
@brief check if data can be written to the TX data register
@returns true if data can be written, false otherwise
*/
/**************************************************************************/
bool Adafruit_ZeroI2S::txReady()
{
#if defined(__SAMD51__)
return !((!I2S->INTFLAG.bit.TXRDY0) || I2S->SYNCBUSY.bit.TXDATA );
#else
if(_i2sserializer > -1) {
if(_i2sserializer == 0){
return !( (!I2S->INTFLAG.bit.TXRDY0) || I2S->SYNCBUSY.bit.DATA0 );
}
else{
return !( (!I2S->INTFLAG.bit.TXRDY1) || I2S->SYNCBUSY.bit.DATA1 );
}
}
else return false;
#endif
}
/**************************************************************************/
/*!
@brief check if data is available to be read from the RX data register
@returns true if data is available, false otherwise
*/
/**************************************************************************/
bool Adafruit_ZeroI2S::rxReady()
{
#if defined(__SAMD51__)
return !( (!I2S->INTFLAG.bit.RXRDY0) || I2S->SYNCBUSY.bit.RXDATA );
#else
if(_i2sserializer > -1) {
if(_i2sserializer == 0){
return !( (!I2S->INTFLAG.bit.RXRDY0) || I2S->SYNCBUSY.bit.DATA0 );
}
else{
return !( (!I2S->INTFLAG.bit.RXRDY1) || I2S->SYNCBUSY.bit.DATA1 );
}
}
else return false;
#endif
}
/**************************************************************************/
/*!
@brief perform a blocking write to the I2S peripheral. This function will only return once
all data has been sent.
@param left the left channel data
@param right the right channel data
*/
/**************************************************************************/
void Adafruit_ZeroI2S::write(int32_t left, int32_t right)
{
#if defined(__SAMD51__)
while( (!I2S->INTFLAG.bit.TXRDY0) || I2S->SYNCBUSY.bit.TXDATA );
I2S->INTFLAG.bit.TXUR0 = 1;
I2S->TXDATA.reg = left;
while( (!I2S->INTFLAG.bit.TXRDY0) || I2S->SYNCBUSY.bit.TXDATA );
I2S->INTFLAG.bit.TXUR0 = 1;
I2S->TXDATA.reg = right;
#else
if(_i2sserializer > -1) {
if(_i2sserializer == 0){
while( (!I2S->INTFLAG.bit.TXRDY0) || I2S->SYNCBUSY.bit.DATA0 );
I2S->INTFLAG.bit.TXUR0 = 1;
I2S->DATA[0].reg = left;
while( (!I2S->INTFLAG.bit.TXRDY0) || I2S->SYNCBUSY.bit.DATA0 );
I2S->INTFLAG.bit.TXUR0 = 1;
I2S->DATA[0].reg = right;
}
else{
while( (!I2S->INTFLAG.bit.TXRDY1) || I2S->SYNCBUSY.bit.DATA1 );
I2S->INTFLAG.bit.TXUR1 = 1;
I2S->DATA[1].reg = left;
while( (!I2S->INTFLAG.bit.TXRDY1) || I2S->SYNCBUSY.bit.DATA1 );
I2S->INTFLAG.bit.TXUR1 = 1;
I2S->DATA[1].reg = right;
}
}
#endif
}
/**************************************************************************/
/*!
@brief perform a blocking read to the I2S peripheral. This function will only return once
all data has been read.
@param left pointer to where the left channel data will be written
@param right pointer to where the right channel data will be written
*/
/**************************************************************************/
void Adafruit_ZeroI2S::read(int32_t *left, int32_t *right)
{
#if defined(__SAMD51__)
while( (!I2S->INTFLAG.bit.RXRDY0) || I2S->SYNCBUSY.bit.RXDATA );
*left = I2S->RXDATA.reg;
while( (!I2S->INTFLAG.bit.RXRDY0) || I2S->SYNCBUSY.bit.RXDATA );
*right = I2S->RXDATA.reg;
#else
if(_i2sserializer > -1) {
if(_i2sserializer == 0){
while( (!I2S->INTFLAG.bit.RXRDY0) || I2S->SYNCBUSY.bit.DATA0 );
*left = I2S->DATA[0].reg;
while( (!I2S->INTFLAG.bit.RXRDY0) || I2S->SYNCBUSY.bit.DATA0 );
*right = I2S->DATA[0].reg;
}
else{
while( (!I2S->INTFLAG.bit.RXRDY1) || I2S->SYNCBUSY.bit.DATA1 );
*left = I2S->DATA[1].reg;
while( (!I2S->INTFLAG.bit.RXRDY1) || I2S->SYNCBUSY.bit.DATA1 );
*right = I2S->DATA[1].reg;
}
}
#endif
}