/* Pseudo Code for Microcontroller <-> STA013 MP3 Decoder communication */

#define clock_div_2		10		// mp3 data clock period div 2
#define MP3_DATAREQ		PIN_PA2
#define MP3_DATA		PIN_PA6
#define MP3_CLOCK		PIN_PA5

void give_decoder_data() {
	int byte_to_send;		// assumed to be an 8-bit word

	/* execute this loop while the decoder wants data */
	while(MP3_DATAREQ) {		
		/* get 8 bits to send to decoder */
		byte_to_send = get_next_mp3_byte();

		/* clock data out on MP3_DATA line using MP3_CLOCK line */
		for (i = 7; i >= 0; i--) {
			output_bit(MP3_DATA, (byte_to_send >> i) & 0x1);	// select a bit of the byte and put it on the line
			output_high(MP3_CLOCK);								// raise the clock line
			delay_us(clock_div_2);								// wait during high period of clock
			output_low(MP3_CLOCK);								// drop the clock line
			delay_us(clock_div_2);								// wait during low period of clock
		}
	}
}
