![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This program plays an internet radio audio stream like Shoutcast.
Dependencies: EthernetNetIf mbed HTTPClient
VS1002.cpp@0:37da71b87d96, 2011-03-01 (annotated)
- Committer:
- parik
- Date:
- Tue Mar 01 06:22:50 2011 +0000
- Revision:
- 0:37da71b87d96
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
parik | 0:37da71b87d96 | 1 | |
parik | 0:37da71b87d96 | 2 | |
parik | 0:37da71b87d96 | 3 | #include "VS1002.h" |
parik | 0:37da71b87d96 | 4 | #include "mbed.h" |
parik | 0:37da71b87d96 | 5 | #include "MSCFileSystem.h" |
parik | 0:37da71b87d96 | 6 | |
parik | 0:37da71b87d96 | 7 | |
parik | 0:37da71b87d96 | 8 | /* ================================================================== |
parik | 0:37da71b87d96 | 9 | * Constructor |
parik | 0:37da71b87d96 | 10 | * =================================================================*/ |
parik | 0:37da71b87d96 | 11 | |
parik | 0:37da71b87d96 | 12 | VS1002::VS1002(PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, PinName dreq, PinName dcs, PinName vol) |
parik | 0:37da71b87d96 | 13 | :_spi(mosi, miso, sck), _CS(cs), _RST(rst), _DREQ(dreq), _DCS(dcs), _VOL(vol) |
parik | 0:37da71b87d96 | 14 | { |
parik | 0:37da71b87d96 | 15 | |
parik | 0:37da71b87d96 | 16 | } |
parik | 0:37da71b87d96 | 17 | |
parik | 0:37da71b87d96 | 18 | |
parik | 0:37da71b87d96 | 19 | /*=================================================================== |
parik | 0:37da71b87d96 | 20 | * Functions |
parik | 0:37da71b87d96 | 21 | *==================================================================*/ |
parik | 0:37da71b87d96 | 22 | |
parik | 0:37da71b87d96 | 23 | void VS1002::cs_low(void) |
parik | 0:37da71b87d96 | 24 | { |
parik | 0:37da71b87d96 | 25 | _CS = 0; |
parik | 0:37da71b87d96 | 26 | } |
parik | 0:37da71b87d96 | 27 | void VS1002::cs_high(void) |
parik | 0:37da71b87d96 | 28 | { |
parik | 0:37da71b87d96 | 29 | _CS = 1; |
parik | 0:37da71b87d96 | 30 | } |
parik | 0:37da71b87d96 | 31 | void VS1002::dcs_low(void) |
parik | 0:37da71b87d96 | 32 | { |
parik | 0:37da71b87d96 | 33 | _DCS = 0; |
parik | 0:37da71b87d96 | 34 | } |
parik | 0:37da71b87d96 | 35 | void VS1002::dcs_high(void) |
parik | 0:37da71b87d96 | 36 | { |
parik | 0:37da71b87d96 | 37 | _DCS = 1; |
parik | 0:37da71b87d96 | 38 | } |
parik | 0:37da71b87d96 | 39 | void VS1002::sci_en(void) //SCI enable |
parik | 0:37da71b87d96 | 40 | { |
parik | 0:37da71b87d96 | 41 | cs_high(); |
parik | 0:37da71b87d96 | 42 | dcs_high(); |
parik | 0:37da71b87d96 | 43 | cs_low(); |
parik | 0:37da71b87d96 | 44 | } |
parik | 0:37da71b87d96 | 45 | void VS1002::sci_dis(void) //SCI disable |
parik | 0:37da71b87d96 | 46 | { |
parik | 0:37da71b87d96 | 47 | cs_high(); |
parik | 0:37da71b87d96 | 48 | } |
parik | 0:37da71b87d96 | 49 | void VS1002::sdi_en(void) //SDI enable |
parik | 0:37da71b87d96 | 50 | { |
parik | 0:37da71b87d96 | 51 | dcs_high(); |
parik | 0:37da71b87d96 | 52 | cs_high(); |
parik | 0:37da71b87d96 | 53 | dcs_low(); |
parik | 0:37da71b87d96 | 54 | } |
parik | 0:37da71b87d96 | 55 | void VS1002::sdi_dis(void) //SDI disable |
parik | 0:37da71b87d96 | 56 | { |
parik | 0:37da71b87d96 | 57 | dcs_high(); |
parik | 0:37da71b87d96 | 58 | } |
parik | 0:37da71b87d96 | 59 | void VS1002::reset(void) //hardware reset |
parik | 0:37da71b87d96 | 60 | { |
parik | 0:37da71b87d96 | 61 | wait(0.01); |
parik | 0:37da71b87d96 | 62 | _RST = 0; |
parik | 0:37da71b87d96 | 63 | wait(0.01); |
parik | 0:37da71b87d96 | 64 | _RST = 1; |
parik | 0:37da71b87d96 | 65 | wait(0.10); |
parik | 0:37da71b87d96 | 66 | } |
parik | 0:37da71b87d96 | 67 | void VS1002::power_down(void) //hardware and software reset |
parik | 0:37da71b87d96 | 68 | { |
parik | 0:37da71b87d96 | 69 | cs_low(); |
parik | 0:37da71b87d96 | 70 | reset(); |
parik | 0:37da71b87d96 | 71 | sci_write(0x00, SM_PDOWN); |
parik | 0:37da71b87d96 | 72 | wait(0.01); |
parik | 0:37da71b87d96 | 73 | reset(); |
parik | 0:37da71b87d96 | 74 | } |
parik | 0:37da71b87d96 | 75 | void VS1002::sci_initialise(void) |
parik | 0:37da71b87d96 | 76 | { |
parik | 0:37da71b87d96 | 77 | _RST = 1; //no reset |
parik | 0:37da71b87d96 | 78 | _spi.format(8,0); //spi 8bit interface, steady state low |
parik | 0:37da71b87d96 | 79 | _spi.frequency(1000000); //rising edge data record, freq. 1Mhz |
parik | 0:37da71b87d96 | 80 | |
parik | 0:37da71b87d96 | 81 | cs_low(); |
parik | 0:37da71b87d96 | 82 | for(int i=0; i<4; i++) |
parik | 0:37da71b87d96 | 83 | { |
parik | 0:37da71b87d96 | 84 | _spi.write(0xFF); //clock the chip a bit |
parik | 0:37da71b87d96 | 85 | } |
parik | 0:37da71b87d96 | 86 | cs_high(); |
parik | 0:37da71b87d96 | 87 | dcs_high(); |
parik | 0:37da71b87d96 | 88 | wait_us(5); |
parik | 0:37da71b87d96 | 89 | } |
parik | 0:37da71b87d96 | 90 | void VS1002::sdi_initialise(void) |
parik | 0:37da71b87d96 | 91 | { |
parik | 0:37da71b87d96 | 92 | _spi.format(8,0); |
parik | 0:37da71b87d96 | 93 | _spi.frequency(7000000); //set to 7MHz |
parik | 0:37da71b87d96 | 94 | |
parik | 0:37da71b87d96 | 95 | cs_high(); |
parik | 0:37da71b87d96 | 96 | dcs_high(); |
parik | 0:37da71b87d96 | 97 | } |
parik | 0:37da71b87d96 | 98 | void VS1002::sci_write(unsigned char address, unsigned short int data) |
parik | 0:37da71b87d96 | 99 | { |
parik | 0:37da71b87d96 | 100 | sci_en(); //enables SCI/disables SDI |
parik | 0:37da71b87d96 | 101 | |
parik | 0:37da71b87d96 | 102 | while(!_DREQ); //wait unitl data request is high |
parik | 0:37da71b87d96 | 103 | _spi.write(0x02); //SCI write |
parik | 0:37da71b87d96 | 104 | _spi.write(address); //register address |
parik | 0:37da71b87d96 | 105 | _spi.write((data >> 8) & 0xFF); //write out first half of data word |
parik | 0:37da71b87d96 | 106 | _spi.write(data & 0xFF); //write out second half of data word |
parik | 0:37da71b87d96 | 107 | |
parik | 0:37da71b87d96 | 108 | sci_dis(); //enables SDI/disables SCI |
parik | 0:37da71b87d96 | 109 | wait_us(5); |
parik | 0:37da71b87d96 | 110 | } |
parik | 0:37da71b87d96 | 111 | void VS1002::sdi_write(unsigned char datum) |
parik | 0:37da71b87d96 | 112 | { |
parik | 0:37da71b87d96 | 113 | sdi_en(); |
parik | 0:37da71b87d96 | 114 | |
parik | 0:37da71b87d96 | 115 | while(!_DREQ); |
parik | 0:37da71b87d96 | 116 | _spi.write(datum); |
parik | 0:37da71b87d96 | 117 | |
parik | 0:37da71b87d96 | 118 | sci_dis(); |
parik | 0:37da71b87d96 | 119 | } |
parik | 0:37da71b87d96 | 120 | unsigned short int VS1002::read(unsigned short int address) |
parik | 0:37da71b87d96 | 121 | { |
parik | 0:37da71b87d96 | 122 | cs_low(); //enables SCI/disables SDI |
parik | 0:37da71b87d96 | 123 | |
parik | 0:37da71b87d96 | 124 | while(!_DREQ); //wait unitl data request is high |
parik | 0:37da71b87d96 | 125 | _spi.write(0x03); //SCI write |
parik | 0:37da71b87d96 | 126 | _spi.write(address); //register address |
parik | 0:37da71b87d96 | 127 | unsigned short int received = _spi.write(0x00); //write out dummy byte |
parik | 0:37da71b87d96 | 128 | received <<= 8; |
parik | 0:37da71b87d96 | 129 | received += _spi.write(0x00); //write out dummy byte |
parik | 0:37da71b87d96 | 130 | |
parik | 0:37da71b87d96 | 131 | cs_high(); //enables SDI/disables SCI |
parik | 0:37da71b87d96 | 132 | |
parik | 0:37da71b87d96 | 133 | return received; //return received word |
parik | 0:37da71b87d96 | 134 | } |
parik | 0:37da71b87d96 | 135 | void VS1002::sine_test_activate(unsigned char wave) |
parik | 0:37da71b87d96 | 136 | { |
parik | 0:37da71b87d96 | 137 | cs_high(); //enables SDI/disables SCI |
parik | 0:37da71b87d96 | 138 | |
parik | 0:37da71b87d96 | 139 | while(!_DREQ); //wait unitl data request is high |
parik | 0:37da71b87d96 | 140 | _spi.write(0x53); //SDI write |
parik | 0:37da71b87d96 | 141 | _spi.write(0xEF); //SDI write |
parik | 0:37da71b87d96 | 142 | _spi.write(0x6E); //SDI write |
parik | 0:37da71b87d96 | 143 | _spi.write(wave); //SDI write |
parik | 0:37da71b87d96 | 144 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 145 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 146 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 147 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 148 | |
parik | 0:37da71b87d96 | 149 | cs_low(); //enables SCI/disables SDI |
parik | 0:37da71b87d96 | 150 | } |
parik | 0:37da71b87d96 | 151 | void VS1002::sine_test_deactivate(void) |
parik | 0:37da71b87d96 | 152 | { |
parik | 0:37da71b87d96 | 153 | cs_high(); |
parik | 0:37da71b87d96 | 154 | |
parik | 0:37da71b87d96 | 155 | while(!_DREQ); |
parik | 0:37da71b87d96 | 156 | _spi.write(0x45); //SDI write |
parik | 0:37da71b87d96 | 157 | _spi.write(0x78); //SDI write |
parik | 0:37da71b87d96 | 158 | _spi.write(0x69); //SDI write |
parik | 0:37da71b87d96 | 159 | _spi.write(0x74); //SDI write |
parik | 0:37da71b87d96 | 160 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 161 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 162 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 163 | _spi.write(0x00); //filler byte |
parik | 0:37da71b87d96 | 164 | } |
parik | 0:37da71b87d96 | 165 | void VS1002::volume(void) |
parik | 0:37da71b87d96 | 166 | { |
parik | 0:37da71b87d96 | 167 | unsigned char volumize = (_VOL * 255); |
parik | 0:37da71b87d96 | 168 | while(!_DREQ); |
parik | 0:37da71b87d96 | 169 | |
parik | 0:37da71b87d96 | 170 | unsigned short int attenuation = ((256 * volumize) + volumize); |
parik | 0:37da71b87d96 | 171 | sci_write(0x0B, attenuation); |
parik | 0:37da71b87d96 | 172 | } |
parik | 0:37da71b87d96 | 173 | |
parik | 0:37da71b87d96 | 174 | |
parik | 0:37da71b87d96 | 175 | //Play the mp3 file from the USB filesystem (stream.mp3 on usb system) |
parik | 0:37da71b87d96 | 176 | void VS1002::play_song() |
parik | 0:37da71b87d96 | 177 | { |
parik | 0:37da71b87d96 | 178 | |
parik | 0:37da71b87d96 | 179 | |
parik | 0:37da71b87d96 | 180 | /*====== File Transfer ======*/ |
parik | 0:37da71b87d96 | 181 | |
parik | 0:37da71b87d96 | 182 | FILE *song; |
parik | 0:37da71b87d96 | 183 | unsigned char array[512]; |
parik | 0:37da71b87d96 | 184 | song = fopen("/msc/stream.mp3", "rb"); |
parik | 0:37da71b87d96 | 185 | |
parik | 0:37da71b87d96 | 186 | if(!song) |
parik | 0:37da71b87d96 | 187 | { |
parik | 0:37da71b87d96 | 188 | exit(1); |
parik | 0:37da71b87d96 | 189 | } |
parik | 0:37da71b87d96 | 190 | |
parik | 0:37da71b87d96 | 191 | while(!feof(song)) |
parik | 0:37da71b87d96 | 192 | { |
parik | 0:37da71b87d96 | 193 | fread(&array, 1, 512, song); |
parik | 0:37da71b87d96 | 194 | for(int i=0; i<512; i++) |
parik | 0:37da71b87d96 | 195 | { |
parik | 0:37da71b87d96 | 196 | sdi_write(array[i]); |
parik | 0:37da71b87d96 | 197 | } |
parik | 0:37da71b87d96 | 198 | volume(); |
parik | 0:37da71b87d96 | 199 | } |
parik | 0:37da71b87d96 | 200 | for(int n=0; n<2048; n++) |
parik | 0:37da71b87d96 | 201 | { |
parik | 0:37da71b87d96 | 202 | sdi_write(0x00); |
parik | 0:37da71b87d96 | 203 | } |
parik | 0:37da71b87d96 | 204 | fclose(song); |
parik | 0:37da71b87d96 | 205 | |
parik | 0:37da71b87d96 | 206 | |
parik | 0:37da71b87d96 | 207 | } |
parik | 0:37da71b87d96 | 208 |