I’ve recently been writing code for the TI Stellaris cortex-m3 line of parts (Both the lm3s9b96 and the lm3s9l71). One particular facet of the project calls for Interrupt driven SPI comms to a slave device. Amazingly, all of the example and project code shipped by TI is for polled/blocking interaction with the SSI peripheral.
Generally, I have been using the reference code and the well commented driver library to find my way around the peripherals. I have only really referred to the actual datasheet to find out what’s actually there and observe any caveats. Now that I actually needed the datasheet for something, I found it woefully lacking in the SSI interrupts area. In particular the EOT (End of Transmission) interrupt.
The EOT Interrupt appears to have been an addition at some point in the devices life and feels a bit bodgy. It does not really seem to conform to the style that all the other interrupts have. This is compounded by no description of operation in the text of the data sheet.
After a *lot* of messing about writing small test cases, I got the interrupt firing in a useful way. Example code below.
The key things are:
- Set the SSI_CR1_EOT bit in SSI_O_CR1.
This changes the TXFF interrupt into the EOT interrupt
- Enable the TXFF interrupt by setting its interrupt mask bit
Don’t be fooled (I was), this doesn’t mean the TXFF interrupt flag is now the EOT flag(this seems likely from the register layout).
- In the interrupt handler, check the RNE bit of the SSISR (ssi status reg)
If this set, read from the RX FIFO until it clears. This also appears to clear the EOT interrupt so the interrupt does not hit as soon as you return from the handler.
With hindsight, this doesn’t seem too surprising but it was very non-obvious as I tried to get it going.
Warning this may all be a load of tosh but it seems to work ok for me.
Peripheral init:
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);
ROM_GPIOPinTypeGPIOInput(GPIO_PORTH_BASE,GDO0_PIN);
GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE,SCK_PIN);
GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE,MOSI_PIN);
GPIOPinTypeGPIOInput(GPIO_PORTH_BASE,MISO_PIN);
GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE,CSN_PIN);
GPIOPinConfigure(GPIO_PH4_SSI1CLK);
GPIOPinConfigure(GPIO_PH7_SSI1TX );
GPIOPinConfigure(GPIO_PH6_SSI1RX );
GPIOPinTypeSSI(GPIO_PORTH_BASE,SCK_PIN | MISO_PIN |MOSI_PIN);
/* Dont use ROM_ see errata */
SSIConfigSetExpClk(SSI1_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
SSI_MODE_MASTER, 1000000, 8);
SSIEnable(SSI1_BASE);
while(SSIDataGetNonBlocking(SSI1_BASE, &ulTemp)) /* empty buffer */
{
}
/* switch to interrupt driven SPI */
SSIIntDisable(SSI1_BASE, SSI_TXFF | SSI_RXFF | SSI_RXTO | SSI_RXOR );
SSIIntClear(SSI1_BASE, SSI_TXFF | SSI_RXFF | SSI_RXTO | SSI_RXOR );
HWREG(SSI1_BASE + SSI_O_CR1) |= SSI_CR1_EOT; /* switch tx interrupt to eot int */
SSIIntEnable(SSI1_BASE, SSI_TXFF ); /* SSI_TXFF | SSI_RXFF | SSI_RXTO | SSI_RXOR */
ROM_IntEnable(INT_SSI1);
IntMasterEnable();
UARTprintf(“End init\n”);
And the interupt handler:
void SSI1IntHandler(void)
{
volatile unsigned long status=0;
unsigned long data=0;
status = SSIIntStatus(SSI1_BASE, true);
SSIIntClear(SSI1_BASE, SSI_TXFF | SSI_RXFF | SSI_RXTO | SSI_RXOR );
while( HWREG(SSI1_BASE + SSI_O_SR) & 4)
{
SSIDataGetNonBlocking(SSI1_BASE, &data);
UARTprintf(“|got %x “,data);
}
/* Check other flags here if needed */
}