// ***************************************************************************** // @fn spi_string // @brief Write a string through SPI // @param unsigned char* TxData String to send // @param unsigned char* RxData Reception buffer // @param unsigned int length Length of the string // @return none // ***************************************************************************** void spi_string(unsigned char* TxData, unsigned char* RxData, unsigned int length) { #ifdef SPI_USE_DMA spiDmaTransfer( (uint8_t*) TxData, (uint8_t*) RxData, length ); sleepUntilDmaDone(); #else uint16_t i = 0; if( RxData != NULL && TxData != NULL ) // two way transmition { while( i < length ) { RxData[i] = spi_byte( TxData[i] ); i++; } } else if( RxData == NULL && TxData != NULL ) // send only { while( i < length ) { spi_byte( TxData[i] ); i++; } } else if( RxData != NULL && TxData == NULL ) // recieve only { while( i < length ) { RxData[i] = spi_byte( 0 ); i++; } } #endif }
// ***************************************************************************** // @fn spi_byte // @brief Write a byte through SPI // @param unsigned char data Byte to send // @return unsigned char Recieved byte // ***************************************************************************** unsigned char spi_byte(unsigned char data) { #ifdef SPI_USE_DMA unsigned char receive = NULL; spiDmaTransfer( (uint8_t*) &data, (uint8_t*) &receive, 1 ); sleepUntilDmaDone(); return receive; #else return USART_SpiTransfer( SPI_CHANNEL, data ); #endif }
/*---------------------------------------------------------------------------*/ int spi2_writebuf(uint8_t *buf, int len) { #ifdef WITH_DMA spiDmaTransfer(buf,NULL,len); while(txActive) { clock_delay(1); }; #else int i=0; for(i=0; i<len; i++) { spi2_readwrite(buf[i]); } #endif return len; }
/*---------------------------------------------------------------------------*/ int spi2_readbuf(uint8_t *buf, int readlen) { #ifdef WITH_DMA //memset(dummy_byte, 0, sizeof(dummy_byte)); spiDmaTransfer(&dummy_byte,buf,readlen); irq_disable(); while(rxActive) { EMU_EnterEM1(); irq_enable(); irq_disable(); } irq_enable(); #else int i = 0; for(i=0;i<readlen;i++) { buf[i] = spi2_readwrite(0x00); } #endif return readlen; }
/**************************************************************************//** * @brief Main function * This example sets up the DMA to transfer outbound and incoming data from the * SPI (USART1) to/from the source/destination buffers. Three tests are done: * 1) Transmit data (string) without reading received data * 2) Transmit data (string) and transfer received data to RAM buffer * 3) Transmit dummy data and transfer received data to RAM buffer *****************************************************************************/ int main(void) { /* Initialize chip */ CHIP_Init(); /* Configuring clocks in the Clock Management Unit (CMU) */ setupCmu(); /* Configura USART for SPI */ setupSpi(); /* Configure DMA transfer from RAM to SPI using ping-pong */ setupDma(); /* Send data to slave, no data reception */ spiDmaTransfer((uint8_t*) spiTxData, NULL, SPI_TRANSFER_SIZE); /* Sleep until transfer is done */ sleepUntilTransferDone(); /* Send data to slave and save received data in buffer */ spiDmaTransfer((uint8_t*) spiTxData, (uint8_t*) spiRxData1, SPI_TRANSFER_SIZE); /* Sleep until transfer is done */ sleepUntilTransferDone(); /* Send dummy data to slave and save received data in buffer */ spiDmaTransfer(NULL, (uint8_t*) spiRxData2, SPI_TRANSFER_SIZE); /* Sleep until transfer is done */ sleepUntilTransferDone(); /* Send dummy data to slave and save received data in buffer */ spiDmaTransfer(NULL, (uint8_t*) spiRxData2, SPI_TRANSFER_SIZE); /* Sleep until transfer is done */ sleepUntilTransferDone(); /* Send data to slave, no data reception */ spiDmaTransfer((uint8_t*) spiTxData, NULL, SPI_TRANSFER_SIZE); /* Sleep until transfer is done */ sleepUntilTransferDone(); /* Send data to slave and save received data in buffer */ spiDmaTransfer((uint8_t*) spiTxData, (uint8_t*) spiRxData1, SPI_TRANSFER_SIZE); /* Sleep until transfer is done */ sleepUntilTransferDone(); /* Send dummy data to slave and save received data in buffer */ spiDmaTransfer(NULL, (uint8_t*) spiRxData2, SPI_TRANSFER_SIZE); /* Sleep until transfer is done */ sleepUntilTransferDone(); /* Cleaning up after DMA transfers */ DMA_Reset(); /* Done */ while (1); }