On MK10FN1M0VLQ12 I did this way
uint32_t SPI_TransferByte(uint32_t spi_num, uint8_t tx_data, uint8_t *rx_data)
{
uint32_t timeout;
SPIx[spi_num]->PUSHR = SPI_PUSHR_CTAS(0) | SPI_PUSHR_TXDATA((uint32_t)tx_data);
timeout = 0;
while (!( SPIx[spi_num]->SR & SPI_SR_TCF_MASK))
{
if (timeout++ > SPI_TIMEOUT)
{
SPIx[spi_num]->SR = SPI_SR_TCF_MASK;
return SPI_TIMEOUT_ERROR;
}
}
timeout = 0;
while (!(SPIx[spi_num]->SR & SPI_SR_RFDF_MASK))
{
if (timeout++ > SPI_TIMEOUT)
{
SPIx[spi_num]->SR = SPI_SR_RFDF_MASK;
return SPI_TIMEOUT_ERROR;
}
}
*rx_data = (uint8_t)SPIx[spi_num]->POPR;
SPIx[spi_num]->SR = SPI_SR_TCF_MASK | SPI_SR_RFDF_MASK;
return 0;
}
And it work very good.
So I did the same on MKV58F1M0VLQ24
uint8_t SPI_TransferByte(SPI_Type *base, uint8_t tx_data)
{
uint32_t timeout = 0;
base->PUSHR = SPI_PUSHR_CTAS(0) | SPI_PUSHR_TXDATA((uint32_t)tx_data);
while ((base->SR & SPI_SR_TCF_MASK) == 0)
{
if (timeout++ > 100000)
{
base->SR |= SPI_SR_TCF_MASK;
break;
}
}
base->SR |= SPI_SR_TCF_MASK;
timeout = 0;
while ((base->SR & SPI_SR_RFDF_MASK)==0)
{
if (timeout++ > 100000)
{
base->SR |= SPI_SR_RFDF_MASK;
break;
}
}
base->SR |= SPI_SR_RFDF_MASK;
return (uint8_t)(base->POPR);
}
And I stay in
while ((base->SR & SPI_SR_TCF_MASK) == 0)
the flag always low.
What I did wrong?
Solved! Go to Solution.
Hello
Before writing data to PUSHR you need to clear the MCR[HALT] field to start the transmission, also please check if the module is enabled, otherwise writing to PUSHR has no effect.
The code stays in "while ((base->SR & SPI_SR_TCF_MASK) == 0)" while the transfer is still in progress.
I suggest you refer to the SDK examples, there you can functions that will help you with this.
Let me know if this is helpful, if you have more questions do not hesitate to ask me.
Best regards,
Omar
Hello
Before writing data to PUSHR you need to clear the MCR[HALT] field to start the transmission, also please check if the module is enabled, otherwise writing to PUSHR has no effect.
The code stays in "while ((base->SR & SPI_SR_TCF_MASK) == 0)" while the transfer is still in progress.
I suggest you refer to the SDK examples, there you can functions that will help you with this.
Let me know if this is helpful, if you have more questions do not hesitate to ask me.
Best regards,
Omar