I want to use a GPIO (specifically GPIO34) as chip select for one of the SPI devices connected to the board. I found a solution here on the forums that involves tying the timing of the custom CS pins to GPIO19, but in my case it's not an option (no free pins). So I tried using SPIATX interrupt to handle CS, but with my implementation the timing is way off (see below).
Here are the signals from the board (red - CLK, blue - MOSI, green - CS[GPIO34]):
What I'm basically doing here, is:
1. Pull down CS/GPIO34.
2. Writing data (multiple calls to SPI_write8).
3. Enabling the SPIATX interrupt, which pulls CS/GPIO34 back up.
At first I thought it might have something to do with the interrupt being triggered after the first byte being sent, but as you can see in the picture, it's not even waiting for one byte...
Here are the relevant bits of code:
void spi_init(){ CLK_enableSpiaClock(clkHdl); SPI_setCharLength(spiHdl, SPI_CharLength_8_Bits); SPI_setMode(spiHdl, SPI_Mode_Master); SPI_enableTx(spiHdl); SPI_setBaudRate(spiHdl, SPI_BaudRate_1_MBaud); SPI_enableLoopBack(spiHdl); SPI_enable(spiHdl); SPI_setPriority(spiHdl, SPI_Priority_FreeRun); return; } void spi_fifo_init(){ SPI_enableChannels(spiHdl); SPI_enableFifoEnh(spiHdl); SPI_resetTxFifo(spiHdl); SPI_clearTxFifoInt(spiHdl); SPI_setTxFifoIntLevel(spiHdl, SPI_FifoLevel_4_Words); SPI_enableTxFifoInt(spiHdl); SPI_resetRxFifo(spiHdl); SPI_clearRxFifoInt(spiHdl); SPI_setRxFifoIntLevel(spiHdl, SPI_FifoLevel_4_Words); SPI_enableRxFifoInt(spiHdl); PIE_registerPieIntHandler(pieHdl, PIE_GroupNumber_6, PIE_SubGroupNumber_2, (intVec_t)&spiTxFifoIsr); //PIE_enableInt(pieHdl, PIE_GroupNumber_6, PIE_InterruptSource_SPIATX); CPU_enableInt(cpuHdl, CPU_IntNumber_6); return; } interrupt void spiTxFifoIsr(void){ PIE_disableInt(pieHdl, PIE_GroupNumber_6, PIE_InterruptSource_SPIATX); GPIO_setHigh(gpioHdl, GPIO_Number_34); SPI_clearTxFifoInt(spiHdl); PIE_clearInt(pieHdl, PIE_GroupNumber_6); } void setRegister(const uint8_t address, const uint8_t value){ GPIO_setLow(gpioHdl, GPIO_Number_34); SPI_write8(spiHdl, 0x02); SPI_write8(spiHdl, address); SPI_write8(spiHdl, value); PIE_enableInt(pieHdl, PIE_GroupNumber_6, PIE_InterruptSource_SPIATX); }
Is there a way to make it work with this approach? If not, what do you think of the idea of using timer interrupt based on the length of the data being sent? Any other alternatives that don't use additional pins?