// *****************************************************************************
// @fn          spi_init
// @brief       Initialize SPI
// @param       none
// @return      none
// *****************************************************************************
void spi_init(void)
{
    setupCmu();
    setupSpi();
#ifdef SPI_USE_DMA
    setupDma();
#endif
    DPRINT("SPI Init.");
}
/**************************************************************************//**
 * @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);
}