int main() { cli(); // Set system clock setClockExternal16MHzHiRes( 8, 1); DMA.CTRL = 0b10000011; // Enable DMA controller, no double buffers, priority CH0 > CH1 > CH2 > CH3 // USARTD0 connected to 595 shift register PORTD.OUTSET = 0b00000001; // SS/Latch default to high = not selected PORTD.DIRSET = 0b00000001; // SS/Latch drive output PORTD.OUTCLR = 0b00000010; // CLK default to low PORTD.DIRSET = 0b00000010; // CLK drive output PORTD.DIRCLR = 0b00000100; // MISO sense input PORTD.DIRSET = 0b00001000; // MOSI/Data drive output USARTD0.BAUDCTRLA = 0x00; // BSEL=0 (SPI clock = 16MHz) .. BSEL=1 (SPI clock = 8MHz) USARTD0.CTRLC = 0b11000000; // Master SPI mode 0, MSB first USARTD0.CTRLB = 0b00011000; // Enable TX and RX // Interrupts and Programmable Multilevel Interrupt Controller PMIC.INTPRI = 0; // Reset round-robin PMIC.CTRL = 0b1 << 7 | 0b0 << 6 | 0b111; // Round-robin Scheduling Enabled, Vectors in application flash, section, all levels enabled sei(); uint8_t buffer[] = { 'H', 'i', 'H', 'i', 'H', 'i', 'H', 'i', 'H', 'i', 'H', 'i', 'H', 'i' }; while (true) { // 0. select slave PORTD.OUTCLR = 0b00000001; // SS/Latch low = selected dmaRAMtoSPI(DMA.CH0, buffer, USARTD0, sizeof(buffer), false, false); // Do something here.. // 1. wait dma transfer complete dmaWait(DMA.CH0); // 2. wait last byte actually transferred waitSPI(USARTD0); // 3. latch 595s PORTD.OUTSET = 0b00000001; // SS/Latch all bits } return -1; // This should never happen }
//****************************************************// //MP3_sineTest //Prototype: void MP3_sineTest() //Parameters: None. //Description: test the sine wave function of vs1011e. //Returns: None. //****************************************************// void MP3_sineTest() { MP3_reset(); /* Pull xRESET low -> hardware reset */ Delay_ms(100); /* 100 ms delay */ deselect_MP3(); /* Pull xCS high */ set_BSYNC(); /* Pull xDCS high */ //Mp3DeselectData ( ) {MP3_XDCS=1;} Delay_ms(100); /* 100 ms delay */ /* VS10xx Application Notes, chapter 4.8 ---------------------------------*/ /* As an example, let's write value 0x0820 to register 00 byte by byte */ select_MP3(); /* Now SPI writes go to SCI port */ SPI0_Write(0x02); /* Send SPI Byte, then wait for byte to be sent. */ SPI0_Write(0x00); /* 0x02 was WRITE command, 0x00 is register number */ SPI0_Write(0x08); /* This byte goes to MSB */ SPI0_Write(0x20); /* ..and this is LSB. (0x20=Allow Test Mode) */ waitSPI(); /* Wait until Atmel MCU signals SPI write complete */ deselect_MP3(); /* Now SPI writes don't go to SCI port */ while (DREQ==0) {;} /* Wait for DREQ = 1 */ /* Do nothing while waiting for DREQ = 1 */ /* Send a Sine Test Header to Data port */ /* Now SPI writes go to SDI port */ UARTprintf("in sine test \n"); while(1) { MP3_SDI_Write(0x53);//SPI0_Write(0x53); /* - This is a special VLSI Solution test header - */ MP3_SDI_Write(0xef);//SPI0_Write(0xef); /* - that starts a sine sound. It's good for - */ MP3_SDI_Write(0x6e);//SPI0_Write(0x6e); /* - testing your code, the chip and also for - */ MP3_SDI_Write(0x44);//SPI0_Write(0x44); /* - seeing if your MP3 decoder was manufactured - */ MP3_SDI_Write(0x00);//SPI0_Write(0x00); /* - by VLSI Solution oy. ------------------------ */ MP3_SDI_Write(0x00);// SPI0_Write(0x00); MP3_SDI_Write(0x00);//SPI0_Write(0x00); MP3_SDI_Write(0x00);//SPI0_Write(0x00); Delay_ms(500); /* Stop the sine test sound */ MP3_SDI_Write(0x45); //SPI0_Write(0x45); MP3_SDI_Write(0x78);//SPI0_Write(0x78); MP3_SDI_Write(0x69);//SPI0_Write(0x69); MP3_SDI_Write(0x74);// SPI0_Write(0x74); MP3_SDI_Write(0x00);//SPI0_Write(0x00); MP3_SDI_Write(0x00);// SPI0_Write(0x00); MP3_SDI_Write(0x00);//SPI0_Write(0x00); MP3_SDI_Write(0x00);//SPI0_Write(0x00); Delay_ms(500); /* 500 ms delay */ } }
/************************************************************************************************** * Function MP3_SDI_Write_32 * ------------------------------------------------------------------------------------------------- * Overview: Function Write 32 bytes to MP3 SDI * Input: data buffer * Output: Nothing **************************************************************************************************/ void MP3_SDI_Write_32(char *data_) { char i; deselect_MP3(); clear_BSYNC(); while (!DREQ) // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI { //other stuff ? } for (i=0; i<32; i++) SPI0_Write(data_[i]); waitSPI(); set_BSYNC(); }
unsigned int Mp3ReadRegister ( unsigned char addressbyte ) { unsigned int resultvalue = 0; unsigned char temp1,temp2; select_MP3(); SPI0_Write(READ_CODE); SPI0_Write((addressbyte)); temp1 = SPI0_Read(); resultvalue = temp1; resultvalue = resultvalue<<8; temp2 = SPI0_Read(); waitSPI(); resultvalue |= (temp2); deselect_MP3(); return resultvalue; }
/************************************************************************************************** * Function MP3_SCI_Read() * ------------------------------------------------------------------------------------------------- * Overview: Function reads words_count words from MP3 SCI * Input: start address, word count to be read * Output: words are stored to data_buffer **************************************************************************************************/ void MP3_SCI_Read(char start_address, char words_count, unsigned int *data_buffer) { unsigned int temp; select_MP3();// MP3_CS = 0; // select MP3 SCI SPI0_Write(READ_CODE); SPI0_Write(start_address); waitSPI(); while (words_count--) { // read words_count words byte per byte temp = SPI0_Read(); temp <<= 8; temp += SPI0_Read(); *(data_buffer++) = temp; } deselect_MP3(); //MP3_CS = 1;// deselect MP3 SCI while (DREQ == 0); // wait until DREQ becomes 1, see MP3 codec datasheet, Serial Protocol for SCI }