The data sheet for KL25z says that to clear the SPTEF bit on the SPI0_S register
SPTEF is cleared by reading the S register with SPTEF set and then writing a data value
to the transmit buffer at D. The S register must be read with SPTEF set to 1 before writing data to the D
register; otherwise, the D write is ignored
So does that mean that to send data over SPI I would need to wait for SPTEF to be set to 1 then write to to the data register or is another read of the status register required.
For example is the below definition sufficient
void SPI_write_byte(uint8_t data)
{
while (!(SPI0_S & SPI_S_SPTEF_MASK));
SPI0_D = data;
}
or do I need to do something like
void SPI_write_byte(uint8_t data)
{
while (!(SPI0_S & SPI_S_SPTEF_MASK));
SPI0_D = data;
while (!(SPI0_S & SPI_S_SPRF_MASK));
data = SPI0_D;
}
Also if I want to use SS pin as a manually controlled pin for slave selection and I have MODFEN = 0 and SSOE = 0, then do I just pull the Port C Pin 4 (alternate function 2) low before starting a data transfer and assert it high once the transfer is complete?
Hi Subhradeep Dutta,
1. SPI byte write and read, you can use this code:
uint8_t SPI_WriteReadData(uint8_t *pRead,uint8_t *pWrite,uint32_t uiLength)
{
uint16_t i;
for( i=0;i<uiLength;i++)
{
while(!(SPI0_S & SPI_S_SPTEF_MASK ) );
SPI0_D = pWrite[i];
while(!(SPI0_S & SPI_S_SPRF_MASK ) );
pRead[i] = SPI0_D;
}
return 1;
}
This is the polling mode, I have attached the code which wrote by myself for your reference.
2. SS pin as a manually controlled pin
As you know, if the SS is automatically controlled by the SPI module, the SS will go to high after each byte sending.
If you want to manually control, it is very easy, you just need to configure the SS pin as the GPIO, then when you want to send the data, pull low, after sending the data, pull high.
GPIO mux is ALT1, you can configure it in the PORTx_PCRn[MUX], then configure the GPIO, use GPIO to control it.
Wish it helps you!
Have a great day,
Kerry
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
martynhunt Could you please answer the above question