[f80c36]: vs1053.c  Maximize  Restore  History

Download this file

291 lines (221 with data), 6.4 kB

  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
#include "stm8s.h"
#include "stm8s_spi.h"
#include "stm8s_gpio.h"
#include "includes.h"
unsigned char vs1053stm;
code const char decibel_vol[8] = {
2, //-30db
6, //-20db
16, //-12db
32, //-6db
45, //-3db
64, //0db
90, //3 db
113, //5 db
};
void InitVS1053(void)
{
InitSPI(SPI_BAUDRATEPRESCALER_256); //Slow SPI for Initialization
VS1053Reset();
VS1053WriteRegister(VS1053_REG_MODE, VS1053_SM_SDINEW | VS1053_SM_SDISHARE);
//Set GPIOs as Output
VS1053WriteWram(VS1053_ADDR_DDR,0xff);
VS1053WriteRegister(VS1053_REG_CLOCKF,VS1053_CLOCK_4_5); // set to 55,296 Mhz Operation
InitSPI(SPI_BAUDRATEPRESCALER_4); //fast SPI approx 3,68 M/bits (Datasheet allows max. 3,5M/Bits)
}
void VS1053LoadPlugin(void)
{
unsigned short mode;
mode = VS1053ReadRegister(VS1053_REG_MODE);
VS1053WriteRegister(VS1053_REG_MODE, mode & ~VS1053_SM_ADPCM);
VS1053WriteRegister(VS1053_REG_BASS,0);
VS1053WriteRegister(VS1053_REG_AIADDR,0);
VS1053WriteWram(VS1053_ADDR_INT_ENABLE, 0x02); //disable all but SCI Interrupt
PluginLoadUserCode();
VS1053WriteRegister(VS1053_REG_MODE, mode | VS1053_SM_ADPCM | VS1053_SM_LINE1 | VS1053_SM_LAYER12 );
VS1053WriteRegister(VS1053_REG_AICTRL1, 1024); //no AGC function
VS1053WriteRegister(VS1053_REG_AICTRL2, 0);
VS1053WriteRegister(VS1053_REG_AICTRL0, 0x8080); //use Stereo VU-Meter
VS1053WriteRegister(VS1053_REG_AICTRL3, 0);
VS1053WriteRegister(VS1053_REG_AIADDR,0x34); //start ogg encoding
}
void VS1053Reset(void)
{
VS1053_GPIO_CTRL->ODR &= ~VS1053_PIN_RESET;
VS1053_GPIO_CTRL->ODR |= VS1053_PIN_RESET;
}
void VS1053WriteWram(unsigned short addr, unsigned short val)
{
VS1053WriteRegister(VS1053_REG_WRAMADDR, addr);
VS1053WriteRegister(VS1053_REG_WRAM, val);
}
unsigned short VS1053ReadWram(unsigned short addr)
{
VS1053WriteRegister(VS1053_REG_WRAMADDR, addr);
return VS1053ReadRegister(VS1053_REG_WRAM);
}
void VS1053WriteIWram(unsigned short addr, unsigned long val)
{
unsigned short d;
VS1053WriteRegister(VS1053_REG_WRAMADDR, addr);
d = val >> 16;
VS1053WriteRegister(VS1053_REG_WRAM, d);
d = val & 0xFFFF;
VS1053WriteRegister(VS1053_REG_WRAM, d);
}
unsigned long VS1053ReadIWram(unsigned short addr)
{
unsigned long d = 0;
VS1053WriteRegister(VS1053_REG_WRAMADDR, addr);
d = ((unsigned long)VS1053ReadRegister(VS1053_REG_WRAM))<< 16;
d |= (unsigned long)VS1053ReadRegister(VS1053_REG_WRAM);
return d;
}
void VS1053SetGPIO(unsigned char gpio)
{
VS1053WriteWram(VS1053_ADDR_ODATA, gpio);
}
unsigned char VS1053GetGPIO(void)
{
return (unsigned char)VS1053ReadWram(VS1053_ADDR_IDATA);
}
void VS1053WriteRegister(unsigned char reg, unsigned short val) {
while(!VS1053_CHECK_DREQ);
// SPI->CR1 |= 0x40; //Enable SPI;
VS1053_SPI_CS_ON;
SPI->DR = VS1053_CMD_WRITE;
while(!(SPI->SR&SPI_FLAG_TXE));
SPI->DR = reg;
while(!(SPI->SR&SPI_FLAG_TXE));
SPI->DR = MSB(val);
while(!(SPI->SR&SPI_FLAG_TXE));
SPI->DR = LSB(val);
while(!(SPI->SR&SPI_FLAG_TXE));
while((SPI->SR&SPI_FLAG_BSY));
VS1053_SPI_CS_OFF;
// SPI->CR1 &= ~0x40; //Disable SPI;
}
unsigned short VS1053ReadRegister(unsigned char reg)
{
unsigned char c,d;
unsigned short val;
while(!VS1053_CHECK_DREQ);
VS1053_SPI_CS_ON;
c = SPI->DR; //DummyRead
SPI->DR = VS1053_CMD_READ;
while(!(SPI->SR&SPI_FLAG_TXE));
SPI->DR = reg;
while(!(SPI->SR&SPI_FLAG_RXNE));
c = SPI->DR; //Dummy Read
while(!(SPI->SR&SPI_FLAG_TXE));
SPI->DR = 0;
while(!(SPI->SR&SPI_FLAG_RXNE));
c = SPI->DR; //Dummy Read
while(!(SPI->SR&SPI_FLAG_TXE));
SPI->DR = 0;
while(!(SPI->SR&SPI_FLAG_RXNE));
c = SPI->DR;
while(!(SPI->SR&SPI_FLAG_RXNE));
d = SPI->DR;
while(!(SPI->SR&SPI_FLAG_TXE));
while((SPI->SR&SPI_FLAG_BSY));
VS1053_SPI_CS_OFF;
val = c << 8 | d ;
return val;
}
void VS1053StreamOgg(void)
{
unsigned short num_words,d;
num_words=VS1053ReadRegister(VS1053_REG_HDAT1);
if(num_words > VS1053_STREAMOGG_PARAM_MAXBLOCKSIZE)
num_words = VS1053_STREAMOGG_PARAM_MAXBLOCKSIZE;
while(num_words--)
{
d = VS1053ReadRegister(VS1053_REG_HDAT0);
UART2_WaitTX();
UART2_SendData8(MSB(d));
UART2_WaitTX();
UART2_SendData8(LSB(d));
}
}
void VS1053StreamOggClose(void)
{
unsigned short num_words,d;
num_words=VS1053ReadRegister(VS1053_REG_HDAT1);
num_words--; //last word is processed differently
while(num_words--)
{
d = VS1053ReadRegister(VS1053_REG_HDAT0);
UART2_WaitTX();
UART2_SendData8(MSB(d));
UART2_WaitTX();
UART2_SendData8(LSB(d));
}
//last word
d = VS1053ReadRegister(VS1053_REG_HDAT0);
VS1053ReadRegister(VS1053_REG_AICTRL3);
UART2_WaitTX();
UART2_SendData8(MSB(d));
if(!(VS1053ReadRegister(VS1053_REG_AICTRL3) & BIT(3)))
{
UART2_WaitTX();
UART2_SendData8(LSB(d));
}
}
unsigned char VS1053LevelMeter(unsigned char l)
{
unsigned char i,n;
i=7;
do {
if(l > decibel_vol[i])
{
n = 0xff;
n >>= (7-i);
return n;
}
} while(i--);
return 0;
}
void VS1053ReadVol(void)
{
static unsigned short last_loopcounter;
static unsigned char ReadVolStm = VS1053_READVOL_STM_START;
static unsigned short val;
switch(ReadVolStm)
{
case VS1053_READVOL_STM_START:
VS1053WriteRegister(VS1053_REG_AICTRL0, 0x8080);
ReadVolStm = VS1053_READVOL_STM_WAIT;
last_loopcounter = loopcounter;
break;
case VS1053_READVOL_STM_WAIT:
if( GetElapsedLoops(last_loopcounter) > VS1053_READVOL_PARAM_INITIALWAIT )
{
val = VS1053ReadRegister(VS1053_REG_AICTRL0);
if(val & 0x8080)
{
ReadVolStm = VS1053_READVOL_STM_WAITMORE;
last_loopcounter = loopcounter;
} else {
ReadVolStm = VS1053_READVOL_STM_SETLEDS;
}
}
break;
case VS1053_READVOL_STM_WAITMORE:
if( GetElapsedLoops(last_loopcounter) > VS1053_READVOL_PARAM_ADDITIONALWAIT )
{
val = VS1053ReadRegister(VS1053_REG_AICTRL0);
if(val & 0x8080)
{
last_loopcounter = loopcounter;
} else {
ReadVolStm = VS1053_READVOL_STM_SETLEDS;
}
}
break;
case VS1053_READVOL_STM_SETLEDS:
SetLedLeft(VS1053LevelMeter(val&0x7F));
SetLedRight(VS1053LevelMeter((val>>8)&0x7F));
ReadVolStm = VS1053_READVOL_STM_START;
break;
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks