Part Number: TM4C123GH6PM
I am using Tiva C launchpad and trying to use SPI in loopback mode. But I am confused on send and receive. I thought the data kept in SPI data register(DR) can be read by just reading DR, in
SPI_transfer func, when I check to transmit FIFO status it always read as "not empty". Why transmit FIFO is not getting clear. Can anyone check my implementation,
void SPIpin_Init (void)
{
volatile unsigned long delay;
SYSCTL_RCGCSSI_R |= 0x02; // 1) activate clock for SSI1
delay =SYSCTL_RCGCSSI_R;
SYSCTL_RCGC2_R |= 0x02; // activate clock for Port B
delay = SYSCTL_RCGC2_R;
GPIO_PORTB_PCTL_R = 0x22220000;
GPIO_PORTB_AFSEL_R|= 0x3C;
}
void SPIconf_Init (void)
{
SSI1_CR1_R &=~ 0x02; // SSI Disable, configuration mode
SSI1_CR0_R|=(SPO_EN|SCR|SixteenBitSize); //SPO=1 and SPH=0, Freescale , 16 bit size
SSI1_CR1_R |= 0x05; //Master | Loopback enable
SSI1_CC_R|=0x0;
SSI1_CR1_R|= 0x02; //Enable SPI
}
int SPI_transfer(int data)
{
int receive=0;
//Send data
SSI1_DR_R = data;
while((SSI1_SR_R & 0x01) ==0 ) // wait till Transmit FIFO is empty
{
}
//Receive
while(SSI1_SR_R & 0x04) //Wait till Receive FIFO is empty
{
receive = SSI1_DR_R;
}
return receive;
}
int main(void){
int val=0x3333;
int rec=0;
SPIpin_Init(); // initialize PA2 and make it output
SPIconf_Init();
while(1)
{
rec=SPI_transfer(val);
val++;
}
}