//*****************************************************************************
//
//! @file
//! @brief SPI
//
//*****************************************************************************

#include "inc/hw_types.h"
#include "driverlib/ssi.h"

#include "spi.h"


void spi_init(void)
{    
    SysCtlPeripheralEnable(SPI_PERIPHERAL);

    SysCtlPeripheralEnable(SPI_PERIPHERAL_PORT);

    GPIOPinTypeSSI(SPI_PORT_BASE, SPI_MOSI | SPI_MISO | SPI_CLK);

    // Configure and enable the SSI port for SPI master mode.  Use SSI0,
    // system clock supply, idle clock level low and active low clock in
    // freescale SPI mode, master mode, 4MHz SSI frequency, and 8-bit data.
    SSIConfigSetExpClk(SPI_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0, SSI_MODE_MASTER, 4000000, 8);

    SSIEnable(SPI_BASE);  

}

void spi_send(unsigned char data)
{
   SSIDataPut(SPI_BASE, (unsigned long) data);	
}



void spi_rx_clean(void)
{
	unsigned long rData;
    while(SSIDataGetNonBlocking(SSI0_BASE, &rData))
    {
    }
}

void spi_getdata(unsigned char *data)
{
 unsigned long tmp;	
 SSIDataGet(SPI_BASE, &tmp);
 *data = (unsigned char)tmp;
}

void spi_busy_wait(void)
{
    while(SSIBusy(SPI_BASE))
    {
    }	
}
