-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvs10xx.cpp
229 lines (211 loc) · 6.18 KB
/
vs10xx.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
/*
* vs10xx.cpp
* A library for MusicShield 2.0
*
* Copyright (c) 2012 seeed technology inc.
* Website : www.seeed.cc
* Author : Jack Shao (jacky.shaoxg@gmail.com)
* Create Time: Mar 2014
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <SPI.h>
#include "vs10xx.h"
//software patch for MIDI Play
const unsigned short gVS1053_MIDI_Patch[28]={
/*if you don't let GPIO1 = H,please send this patch by spi*/
0x0007, 0x0001, 0x8050, 0x0006, 0x0014, 0x0030, 0x0715, 0xb080, /* 0 */
0x3400, 0x0007, 0x9255, 0x3d00, 0x0024, 0x0030, 0x0295, 0x6890, /* 8 */
0x3400, 0x0030, 0x0495, 0x3d00, 0x0024, 0x2908, 0x4d40, 0x0030, /* 10 */
0x0200, 0x000a, 0x0001, 0x0050,
};
VS10XX::VS10XX(uint8_t _VS_XRESET, uint8_t _VS_DREQ, uint8_t _VS_XDCS, uint8_t _VS_XCS):
VS_XRESET(_VS_XRESET), VS_DREQ(_VS_DREQ), VS_XDCS(_VS_XDCS), VS_XCS(_VS_XCS)
{
}
/*
**@ function name: init
**@ usage:init the vs10xx chip
**@ input:none
**@ retval:none
*/
void VS10XX::init(void)
{
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16);
initIO();
reset();
}
/*
**@ function name: initForMidiFmt
**@ usage:init the vs10xx chip for midi Mode
**@ input:none
**@ retval:none
*/
void VS10XX::initForMidiFmt(void)
{
//init IO
initIO();
//init SPI
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV16);
//SPI.transfer(0xFF); //transfer a dump
//reset the chip
reset();
//load MIDI plugin
loadMidiPlugin();
}
void VS10XX::initIO(void)
{
pinMode(VS_XRESET, OUTPUT);
pinMode(VS_DREQ, INPUT);
pinMode(VS_XDCS, OUTPUT);
pinMode(VS_XCS, OUTPUT);
digitalWrite(VS_XDCS, HIGH);
digitalWrite(VS_XCS, HIGH);
}
void VS10XX::writeRegister(unsigned char addressbyte, unsigned char highbyte, unsigned char lowbyte)
{
// while (!readDREQ());
deselectDataBus();
selectControlBus();
SPI.transfer(VS_WRITE_COMMAND);
SPI.transfer((addressbyte));
SPI.transfer((highbyte));
SPI.transfer((lowbyte));
deselectControlBus();
}
void VS10XX::writeRegister(unsigned char addressbyte, unsigned int value)
{
writeRegister(addressbyte, value >>8, value & 0xff);
}
void VS10XX::writeData(unsigned char *databuf, unsigned char datalen)
{
selectDataBus();
while (!readDREQ());
while (datalen--)
{
SPI.transfer(*databuf++);
}
deselectDataBus();
}
unsigned int VS10XX::readRegister(unsigned char addressbyte)
{
unsigned int result = 0;
unsigned char highbyte = 0;
unsigned char lowbyte = 0;
deselectDataBus();
selectControlBus(); //XCS = 0
SPI.transfer(VS_READ_COMMAND); //send read command
SPI.transfer(addressbyte); //send register address
//be careful: when read 0xff,dump it,and read agin
//while((h = SPIGetChar())== 0xff);
highbyte = SPI.transfer(0xff);
//while((l = VsReadByte())== 0xff);
lowbyte = SPI.transfer(0xff);
result = (((unsigned int)highbyte << 8) | lowbyte);
deselectControlBus();
return result;
}
// reset for vs10xx
void VS10XX::reset(void)
{
putInReset();
delay(100); //it is a must
/* Send dummy SPI byte to initialize atmel SPI */
////SPIPutCharWithoutWaiting(0xFF);
deselectControlBus();
deselectDataBus();
releaseFromReset();
while (!readDREQ());
/* Set clock register, doubler etc. */
writeRegister(SPI_CLOCKF, 0xc0, 0x00);
Serial.print("\r\nVS10xx Clock Frq: 0x");
Serial.println(readRegister(SPI_CLOCKF), HEX);
/* Wait for DREQ */
while (!readDREQ());
softReset(); //comment this, as it will be executed everytime playing a music file.
writeRegister(SPI_WRAMADDR, 0xc0, 0x13);
/* Switch on the analog parts */
setVolume(40, 40);
//setVolume(0xff,0xff);
//SPISetFastClock();
}
/** Soft Reset of VS10xx (Between songs) */
void VS10XX::softReset(void)
{
/* Soft Reset of VS10xx */
writeRegister(SPI_MODE, 0x08, 0x04); /* Newmode, Reset, No L1-2 */
//Delay(1);
while (!readDREQ()); /* wait for startup */
//setup I2S (see page77 of the datasheet of vs1053 )
//set GPIO0 as output
writeRegister(SPI_WRAMADDR, 0xc0, 0x17);
writeRegister(SPI_WRAM, 0x00, 0xf0);
//enable I2S (MCLK enabled, 48kHz sample rate)
writeRegister(SPI_WRAMADDR, 0xc0, 0x40);
writeRegister(SPI_WRAM, 0x00, 0x0C);
while (!readDREQ());
//LoadUserPatch();
}
/** Set VS10xx Volume Register */
void VS10XX::setVolume(unsigned char leftchannel, unsigned char rightchannel)
{
writeRegister(11, (leftchannel), (rightchannel));
}
void VS10XX::sendZerosToVS10xx(void)
{
unsigned int i;
selectDataBus();
for (i = 0; i < 2048; i++)
{
while (!readDREQ());
SPI.transfer(0);
}
deselectDataBus();
}
/*
**@ function name: loadMidiPatch
**@ usage:load a software patch for vs10xx
**@ input:none
**@ retval:none
*/
void VS10XX::loadMidiPlugin(void)
{
int i=0;
Serial.print("load MIDI Plugin...\r\n");
while(i < sizeof(gVS1053_MIDI_Patch)/sizeof(gVS1053_MIDI_Patch[0]))
{
unsigned short addr, n, val;
addr = gVS1053_MIDI_Patch[i++];
n = gVS1053_MIDI_Patch[i++];
while(n--)
{
val = gVS1053_MIDI_Patch[i++];
writeRegister(addr, val >> 8, val&0xff);
}
}
Serial.print("done\r\n");
}
/****************The end***********************************************/