-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdafruit_VS1053.cpp
770 lines (614 loc) · 18.1 KB
/
Adafruit_VS1053.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/***************************************************
This is a library for the Adafruit VS1053 Codec Breakout
Designed specifically to work with the Adafruit VS1053 Codec Breakout
----> https://www.adafruit.com/products/1381
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_VS1053.h>
#include <SD.h>
#if defined(ARDUINO_STM32_FEATHER)
#define digitalPinToInterrupt(x) x
#endif
static Adafruit_VS1053_FilePlayer *myself;
#ifndef _BV
#define _BV(x) (1<<(x))
#endif
#if defined(__AVR__)
SIGNAL(TIMER0_COMPA_vect) {
myself->feedBuffer();
}
#endif
volatile boolean feedBufferLock = false;
#ifdef ESP32
SemaphoreHandle_t bufferSemaphore;
static void feedTask(void *param) {
while (true) {
xSemaphoreTake(bufferSemaphore, portMAX_DELAY);
myself->feedBuffer();
}
}
static void IRAM_ATTR feeder(void) {
xSemaphoreGiveFromISR(bufferSemaphore, NULL);
}
#else
static void feeder(void) {
myself->feedBuffer();
}
#endif
#define VS1053_CONTROL_SPI_SETTING SPISettings(250000, MSBFIRST, SPI_MODE0)
#define VS1053_DATA_SPI_SETTING SPISettings(8000000, MSBFIRST, SPI_MODE0)
boolean Adafruit_VS1053_FilePlayer::useInterrupt(uint8_t type) {
myself = this; // oy vey
if (type == VS1053_FILEPLAYER_TIMER0_INT) {
#if defined(__AVR__)
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
return true;
#elif defined(__arm__) && defined(CORE_TEENSY)
IntervalTimer *t = new IntervalTimer();
return (t && t->begin(feeder, 1024)) ? true : false;
#elif defined(ARDUINO_STM32_FEATHER)
HardwareTimer timer(3);
// Pause the timer while we're configuring it
timer.pause();
// Set up period
timer.setPeriod(25000); // in microseconds
// Set up an interrupt on channel 1
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
timer.attachCompare1Interrupt(feeder);
// Refresh the timer's count, prescale, and overflow
timer.refresh();
// Start the timer counting
timer.resume();
#else
return false;
#endif
}
if (type == VS1053_FILEPLAYER_PIN_INT) {
#if defined(ESP32)
bufferSemaphore = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(feedTask, "VS1053Feeder", 1536, NULL, 10, NULL, 0); // Arduino main loop is on core 1, so process on the 'other' core
#endif
int8_t irq = digitalPinToInterrupt(_dreq);
//Serial.print("Using IRQ "); Serial.println(irq);
if (irq == -1)
return false;
#if defined(SPI_HAS_TRANSACTION) && !defined(ESP8266) && !defined(ESP32) && !defined(ARDUINO_STM32_FEATHER)
SPI.usingInterrupt(irq);
#endif
attachInterrupt(irq, feeder, CHANGE);
return true;
}
return false;
}
Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(
int8_t rst, int8_t cs, int8_t dcs, int8_t dreq,
int8_t cardcs)
: Adafruit_VS1053(rst, cs, dcs, dreq) {
playingMusic = false;
_cardCS = cardcs;
}
Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(
int8_t cs, int8_t dcs, int8_t dreq,
int8_t cardcs)
: Adafruit_VS1053(-1, cs, dcs, dreq) {
playingMusic = false;
_cardCS = cardcs;
}
Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(
int8_t mosi, int8_t miso, int8_t clk,
int8_t rst, int8_t cs, int8_t dcs, int8_t dreq,
int8_t cardcs)
: Adafruit_VS1053(mosi, miso, clk, rst, cs, dcs, dreq) {
playingMusic = false;
_cardCS = cardcs;
}
boolean Adafruit_VS1053_FilePlayer::begin(void) {
// Set the card to be disabled while we get the VS1053 up
pinMode(_cardCS, OUTPUT);
digitalWrite(_cardCS, HIGH);
uint8_t v = Adafruit_VS1053::begin();
//dumpRegs();
//Serial.print("Version = "); Serial.println(v);
return (v == 4);
}
boolean Adafruit_VS1053_FilePlayer::playFullFile(const char *trackname) {
if (! startPlayingFile(trackname)) return false;
while (playingMusic) {
// twiddle thumbs
feedBuffer();
delay(5); // give IRQs a chance
}
// music file finished!
return true;
}
void Adafruit_VS1053_FilePlayer::stopPlaying(void) {
// cancel all playback
sciWrite(VS1053_REG_MODE, VS1053_MODE_SM_LINE1 | VS1053_MODE_SM_SDINEW | VS1053_MODE_SM_CANCEL);
// wrap it up!
playingMusic = false;
currentTrack.close();
}
void Adafruit_VS1053_FilePlayer::pausePlaying(boolean pause) {
if (pause)
playingMusic = false;
else {
playingMusic = true;
feedBuffer();
}
}
boolean Adafruit_VS1053_FilePlayer::paused(void) {
return (!playingMusic && currentTrack);
}
boolean Adafruit_VS1053_FilePlayer::stopped(void) {
return (!playingMusic && !currentTrack);
}
boolean Adafruit_VS1053_FilePlayer::startPlayingFile(const char *trackname) {
// reset playback
sciWrite(VS1053_REG_MODE, VS1053_MODE_SM_LINE1 | VS1053_MODE_SM_SDINEW);
// resync
sciWrite(VS1053_REG_WRAMADDR, 0x1e29);
sciWrite(VS1053_REG_WRAM, 0);
currentTrack = SD.open(trackname);
if (!currentTrack) {
return false;
}
// don't let the IRQ get triggered by accident here
noInterrupts();
// As explained in datasheet, set twice 0 in REG_DECODETIME to set time back to 0
sciWrite(VS1053_REG_DECODETIME, 0x00);
sciWrite(VS1053_REG_DECODETIME, 0x00);
playingMusic = true;
// wait till its ready for data
while (! readyForData() ) {
#if defined(ESP8266)
yield();
#endif
}
// fill it up!
while (playingMusic && readyForData()) {
feedBuffer();
}
// ok going forward, we can use the IRQ
interrupts();
return true;
}
void Adafruit_VS1053_FilePlayer::feedBuffer(void) {
noInterrupts();
// dont run twice in case interrupts collided
// This isn't a perfect lock as it may lose one feedBuffer request if
// an interrupt occurs before feedBufferLock is reset to false. This
// may cause a glitch in the audio but at least it will not corrupt
// state.
if (feedBufferLock) {
interrupts();
return;
}
feedBufferLock = true;
interrupts();
feedBuffer_noLock();
feedBufferLock = false;
}
void Adafruit_VS1053_FilePlayer::feedBuffer_noLock(void) {
if ((! playingMusic) // paused or stopped
|| (! currentTrack)
|| (! readyForData())) {
return; // paused or stopped
}
// Feed the hungry buffer! :)
while (readyForData()) {
// Read some audio data from the SD card file
int bytesread = currentTrack.read(mp3buffer, VS1053_DATABUFFERLEN);
if (bytesread == 0) {
// must be at the end of the file, wrap it up!
playingMusic = false;
currentTrack.close();
break;
}
playData(mp3buffer, bytesread);
}
}
/***************************************************************/
/* VS1053 'low level' interface */
static volatile PortReg *clkportreg, *misoportreg, *mosiportreg;
static PortMask clkpin, misopin, mosipin;
Adafruit_VS1053::Adafruit_VS1053(int8_t mosi, int8_t miso, int8_t clk,
int8_t rst, int8_t cs, int8_t dcs, int8_t dreq) {
_mosi = mosi;
_miso = miso;
_clk = clk;
_reset = rst;
_cs = cs;
_dcs = dcs;
_dreq = dreq;
#ifdef ESP32
useHardwareSPI = true; // hardware pins can be define by esp32
#else
useHardwareSPI = false;
#endif
clkportreg = portOutputRegister(digitalPinToPort(_clk));
clkpin = digitalPinToBitMask(_clk);
misoportreg = portInputRegister(digitalPinToPort(_miso));
misopin = digitalPinToBitMask(_miso);
mosiportreg = portOutputRegister(digitalPinToPort(_mosi));
mosipin = digitalPinToBitMask(_mosi);
}
Adafruit_VS1053::Adafruit_VS1053(int8_t rst, int8_t cs, int8_t dcs, int8_t dreq) {
_mosi = 0;
_miso = 0;
_clk = 0;
useHardwareSPI = true;
_reset = rst;
_cs = cs;
_dcs = dcs;
_dreq = dreq;
}
void Adafruit_VS1053::applyPatch(const uint16_t *patch, uint16_t patchsize) {
uint16_t i = 0;
// Serial.print("Patch size: "); Serial.println(patchsize);
while ( i < patchsize ) {
uint16_t addr, n, val;
addr = pgm_read_word(patch++);
n = pgm_read_word(patch++);
i += 2;
//Serial.println(addr, HEX);
if (n & 0x8000U) { // RLE run, replicate n samples
n &= 0x7FFF;
val = pgm_read_word(patch++);
i++;
while (n--) {
sciWrite(addr, val);
}
} else { // Copy run, copy n samples
while (n--) {
val = pgm_read_word(patch++);
i++;
sciWrite(addr, val);
}
}
}
}
uint16_t Adafruit_VS1053::loadPlugin(char *plugname) {
File plugin = SD.open(plugname);
if (!plugin) {
Serial.println("Couldn't open the plugin file");
Serial.println(plugin);
return 0xFFFF;
}
if ((plugin.read() != 'P') ||
(plugin.read() != '&') ||
(plugin.read() != 'H'))
return 0xFFFF;
uint16_t type;
// Serial.print("Patch size: "); Serial.println(patchsize);
while ((type = plugin.read()) >= 0) {
uint16_t offsets[] = {0x8000UL, 0x0, 0x4000UL};
uint16_t addr, len;
//Serial.print("type: "); Serial.println(type, HEX);
if (type >= 4) {
plugin.close();
return 0xFFFF;
}
len = plugin.read(); len <<= 8;
len |= plugin.read() & ~1;
addr = plugin.read(); addr <<= 8;
addr |= plugin.read();
//Serial.print("len: "); Serial.print(len);
//Serial.print(" addr: $"); Serial.println(addr, HEX);
if (type == 3) {
// execute rec!
plugin.close();
return addr;
}
// set address
sciWrite(VS1053_REG_WRAMADDR, addr + offsets[type]);
// write data
do {
uint16_t data;
data = plugin.read(); data <<= 8;
data |= plugin.read();
sciWrite(VS1053_REG_WRAM, data);
} while ((len -=2));
}
plugin.close();
return 0xFFFF;
}
boolean Adafruit_VS1053::readyForData(void) {
return digitalRead(_dreq);
}
void Adafruit_VS1053::playData(uint8_t *buffer, uint8_t buffsiz) {
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.beginTransaction(VS1053_DATA_SPI_SETTING);
#endif
digitalWrite(_dcs, LOW);
spiwrite(buffer, buffsiz);
digitalWrite(_dcs, HIGH);
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.endTransaction();
#endif
}
void Adafruit_VS1053::setVolume(uint8_t left, uint8_t right) {
uint16_t v;
v = left;
v <<= 8;
v |= right;
noInterrupts(); //cli();
sciWrite(VS1053_REG_VOLUME, v);
interrupts(); //sei();
}
uint16_t Adafruit_VS1053::decodeTime() {
noInterrupts(); //cli();
uint16_t t = sciRead(VS1053_REG_DECODETIME);
interrupts(); //sei();
return t;
}
void Adafruit_VS1053::softReset(void) {
sciWrite(VS1053_REG_MODE, VS1053_MODE_SM_SDINEW | VS1053_MODE_SM_RESET);
delay(100);
}
void Adafruit_VS1053::reset() {
// TODO: http://www.vlsi.fi/player_vs1011_1002_1003/modularplayer/vs10xx_8c.html#a3
// hardware reset
if (_reset >= 0) {
digitalWrite(_reset, LOW);
delay(100);
digitalWrite(_reset, HIGH);
}
digitalWrite(_cs, HIGH);
digitalWrite(_dcs, HIGH);
delay(100);
softReset();
delay(100);
sciWrite(VS1053_REG_CLOCKF, 0x6000);
setVolume(40, 40);
}
uint8_t Adafruit_VS1053::begin(void) {
if (_reset >= 0) {
pinMode(_reset, OUTPUT);
digitalWrite(_reset, LOW);
}
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
pinMode(_dcs, OUTPUT);
digitalWrite(_dcs, HIGH);
pinMode(_dreq, INPUT);
if (! useHardwareSPI) {
pinMode(_mosi, OUTPUT);
pinMode(_clk, OUTPUT);
pinMode(_miso, INPUT);
} else {
# ifdef ESP32 // esp32 can use any pins as hardware SPI so if supplied we need to assign
if (_clk == 0)
SPI.begin();
else
SPI.begin(_clk, _miso, _mosi, _cs);
# else
SPI.begin();
# endif
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV128);
}
reset();
return (sciRead(VS1053_REG_STATUS) >> 4) & 0x0F;
}
void Adafruit_VS1053::dumpRegs(void) {
Serial.print("Mode = 0x"); Serial.println(sciRead(VS1053_REG_MODE), HEX);
Serial.print("Stat = 0x"); Serial.println(sciRead(VS1053_REG_STATUS), HEX);
Serial.print("ClkF = 0x"); Serial.println(sciRead(VS1053_REG_CLOCKF), HEX);
Serial.print("Vol. = 0x"); Serial.println(sciRead(VS1053_REG_VOLUME), HEX);
}
uint16_t Adafruit_VS1053::recordedWordsWaiting(void) {
return sciRead(VS1053_REG_HDAT1);
}
uint16_t Adafruit_VS1053::recordedReadWord(void) {
return sciRead(VS1053_REG_HDAT0);
}
boolean Adafruit_VS1053::prepareRecordOgg(char *plugname) {
sciWrite(VS1053_REG_CLOCKF, 0xC000); // set max clock
delay(1); while (! readyForData() );
sciWrite(VS1053_REG_BASS, 0); // clear Bass
softReset();
delay(1); while (! readyForData() );
sciWrite(VS1053_SCI_AIADDR, 0);
// disable all interrupts except SCI
sciWrite(VS1053_REG_WRAMADDR, VS1053_INT_ENABLE);
sciWrite(VS1053_REG_WRAM, 0x02);
int pluginStartAddr = loadPlugin(plugname);
if (pluginStartAddr == 0xFFFF) return false;
Serial.print("Plugin at $"); Serial.println(pluginStartAddr, HEX);
if (pluginStartAddr != 0x34) return false;
return true;
}
void Adafruit_VS1053::stopRecordOgg(void) {
sciWrite(VS1053_SCI_AICTRL3, 1);
}
void Adafruit_VS1053::startRecordOgg(boolean mic) {
/* Set VS1053 mode bits as instructed in the VS1053b Ogg Vorbis Encoder
manual. Note: for microphone input, leave SMF_LINE1 unset! */
if (mic) {
sciWrite(VS1053_REG_MODE, VS1053_MODE_SM_ADPCM | VS1053_MODE_SM_SDINEW);
} else {
sciWrite(VS1053_REG_MODE, VS1053_MODE_SM_LINE1 |
VS1053_MODE_SM_ADPCM | VS1053_MODE_SM_SDINEW);
}
sciWrite(VS1053_SCI_AICTRL0, 1024);
/* Rec level: 1024 = 1. If 0, use AGC */
sciWrite(VS1053_SCI_AICTRL1, 1024);
/* Maximum AGC level: 1024 = 1. Only used if SCI_AICTRL1 is set to 0. */
sciWrite(VS1053_SCI_AICTRL2, 0);
/* Miscellaneous bits that also must be set before recording. */
sciWrite(VS1053_SCI_AICTRL3, 0);
sciWrite(VS1053_SCI_AIADDR, 0x34);
delay(1); while (! readyForData() );
}
void Adafruit_VS1053::GPIO_pinMode(uint8_t i, uint8_t dir) {
if (i > 7) return;
sciWrite(VS1053_REG_WRAMADDR, VS1053_GPIO_DDR);
uint16_t ddr = sciRead(VS1053_REG_WRAM);
if (dir == INPUT)
ddr &= ~_BV(i);
if (dir == OUTPUT)
ddr |= _BV(i);
sciWrite(VS1053_REG_WRAMADDR, VS1053_GPIO_DDR);
sciWrite(VS1053_REG_WRAM, ddr);
}
void Adafruit_VS1053::GPIO_digitalWrite(uint8_t val) {
sciWrite(VS1053_REG_WRAMADDR, VS1053_GPIO_ODATA);
sciWrite(VS1053_REG_WRAM, val);
}
void Adafruit_VS1053::GPIO_digitalWrite(uint8_t i, uint8_t val) {
if (i > 7) return;
sciWrite(VS1053_REG_WRAMADDR, VS1053_GPIO_ODATA);
uint16_t pins = sciRead(VS1053_REG_WRAM);
if (val == LOW)
pins &= ~_BV(i);
if (val == HIGH)
pins |= _BV(i);
sciWrite(VS1053_REG_WRAMADDR, VS1053_GPIO_ODATA);
sciWrite(VS1053_REG_WRAM, pins);
}
uint16_t Adafruit_VS1053::GPIO_digitalRead(void) {
sciWrite(VS1053_REG_WRAMADDR, VS1053_GPIO_IDATA);
return sciRead(VS1053_REG_WRAM) & 0xFF;
}
boolean Adafruit_VS1053::GPIO_digitalRead(uint8_t i) {
if (i > 7) return 0;
sciWrite(VS1053_REG_WRAMADDR, VS1053_GPIO_IDATA);
uint16_t val = sciRead(VS1053_REG_WRAM);
if (val & _BV(i)) return true;
return false;
}
uint16_t Adafruit_VS1053::sciRead(uint8_t addr) {
uint16_t data;
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.beginTransaction(VS1053_CONTROL_SPI_SETTING);
#endif
digitalWrite(_cs, LOW);
spiwrite(VS1053_SCI_READ);
spiwrite(addr);
delayMicroseconds(10);
data = spiread();
data <<= 8;
data |= spiread();
digitalWrite(_cs, HIGH);
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.endTransaction();
#endif
return data;
}
void Adafruit_VS1053::sciWrite(uint8_t addr, uint16_t data) {
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.beginTransaction(VS1053_CONTROL_SPI_SETTING);
#endif
digitalWrite(_cs, LOW);
spiwrite(VS1053_SCI_WRITE);
spiwrite(addr);
spiwrite(data >> 8);
spiwrite(data & 0xFF);
digitalWrite(_cs, HIGH);
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.endTransaction();
#endif
}
uint8_t Adafruit_VS1053::spiread(void)
{
int8_t i, x;
x = 0;
// MSB first, clock low when inactive (CPOL 0), data valid on leading edge (CPHA 0)
// Make sure clock starts low
if (useHardwareSPI) {
x = SPI.transfer(0x00);
} else {
for (i=7; i>=0; i--) {
if ((*misoportreg) & misopin)
x |= (1<<i);
*clkportreg |= clkpin;
*clkportreg &= ~clkpin;
// asm("nop; nop");
}
// Make sure clock ends low
*clkportreg &= ~clkpin;
}
return x;
}
void Adafruit_VS1053::spiwrite(uint8_t c)
{
uint8_t x __attribute__ ((aligned (32))) = c;
spiwrite(&x, 1);
}
void Adafruit_VS1053::spiwrite(uint8_t *c, uint16_t num)
{
// MSB first, clock low when inactive (CPOL 0), data valid on leading edge (CPHA 0)
// Make sure clock starts low
if (useHardwareSPI) {
//#if defined(ESP32) // optimized
// SPI.writeBytes(c, num);
// return;
//#endif
while (num--) {
SPI.transfer(c[0]);
c++;
}
} else {
while (num--) {
for (int8_t i=7; i>=0; i--) {
*clkportreg &= ~clkpin;
if (c[0] & (1<<i)) {
*mosiportreg |= mosipin;
} else {
*mosiportreg &= ~mosipin;
}
*clkportreg |= clkpin;
}
*clkportreg &= ~clkpin; // Make sure clock ends low
c++;
}
}
}
void Adafruit_VS1053::sineTest(uint8_t n, uint16_t ms) {
reset();
uint16_t mode = sciRead(VS1053_REG_MODE);
mode |= 0x0020;
sciWrite(VS1053_REG_MODE, mode);
while (!digitalRead(_dreq));
// delay(10);
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.beginTransaction(VS1053_DATA_SPI_SETTING);
#endif
digitalWrite(_dcs, LOW);
spiwrite(0x53);
spiwrite(0xEF);
spiwrite(0x6E);
spiwrite(n);
spiwrite(0x00);
spiwrite(0x00);
spiwrite(0x00);
spiwrite(0x00);
digitalWrite(_dcs, HIGH);
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.endTransaction();
#endif
delay(ms);
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.beginTransaction(VS1053_DATA_SPI_SETTING);
#endif
digitalWrite(_dcs, LOW);
spiwrite(0x45);
spiwrite(0x78);
spiwrite(0x69);
spiwrite(0x74);
spiwrite(0x00);
spiwrite(0x00);
spiwrite(0x00);
spiwrite(0x00);
digitalWrite(_dcs, HIGH);
#ifdef SPI_HAS_TRANSACTION
if (useHardwareSPI) SPI.endTransaction();
#endif
}