Hey guys,
I created a new topic as I believe that this question has deviated from the other topic. I'm going to attach my code at the end. It might be sloppy, but I'm just trying to get it working.
Ok, still have problems. I got the buffer working, using a length of around 1500 characters right now. I make it circular such that the index goes back to the beginning and if there's an overrun, I have it freeze and notify me.
I checked my algorithm by sending a preset string of 32 characters repeatedly and counted hits and misses. No misses were counted and to make sure test was not invalid, I changed algorithm slightly and misses came up whenever buffer looped.
I also implemented software flow control by sending XON and XOFF characters from the AVR. I've tested and can confirm that it works.
The problem is that for some reason the output clicks and pops and plays too fast with a lot of distortion. I'll post the section of code that performs it, maybe there's something wrong?
for (;;) { if (length >= 32) { if bit_is_set(PINB, DREQ) { sendMP3(); count1++; } } if (count1 >= 1000) { count1=0; printf("Length = %u\n",length); } if ((length <= 600) && (send == 0)) { USART_Transmit(XON); send = 1; } }
I should note that I have it sending XOFF in the ISR if the buffer gets more full than 1200. My buffer size is 1536.
Whether I get any output or not really seems to depend on the numbers I'm using. For example, if I send if the buffer is greater than 32 characters only, it doesn't play, but if I make keep a mandatory buffer of more than 128 characters it doesn't play either.
It plays really jerky and the length is always 0. Any ideas? Also, a lot of times in the middle of the song it'll stop decoding and freeze.
I finally got an o-scope and can see that the data going out in 32 byte packets about every 2ms.
I'm using 2Mbaud UART transmission which I've tested and is error free. I'm using a 1MHz SPI connection which is 1/4 the maximum stated in the manual of the MP3 device of 4MHz. I'm using the VS1002 MP3 chip.
I'm assuming that the MP3 chip reads the bitrate and sampling frequency from the MP3 data and sets those as playback settings. Then, I'm assuming, it would accept data into its buffer and send DREQ low whenever that buffer is full and can't accept another 32 bytes. My program essentially monitors this pin and whenever it goes low, sends a chunk of 32 bytes using the following code:
void sendMP3(void) { for (i=0; i<32; i++) { SPI_MasterTransmit(buffer[start]); start++; length--; } if (start == (buffsize-1)) start=0; }
For completeness' sake, here's my ISR code:
ISR(USART0_RX_vect) { cli(); buffer[end]=UDR0; end++; length++; if (length>buffsize)//buffer overflow, loop forever { printf("overflow"); for (;;) {} } if ((length >= 1200) && (send == 1)) { USART_Transmit(XOFF); send = 0; } if (end == (buffsize-1)) end=0; sei(); }
Anything I'm overlooking?
Thanks,
JH