1
\$\begingroup\$

I am converting arduino-nrf905 library for stm8s. There is one spi function on the arduino library, it is SPI.Transfer(uint8_t reg). This function can write and read spi. But, there is two function on the stm8s peripharel library. This functions are SPI_SendData(uint8_t reg) and SPI_ReceiveData(void).

SPI.transfer code of arduino library:

byte SPIClass::transfer(byte _data) {
  SPDR = _data;
  while (!(SPSR & _BV(SPIF)))
    ;
  return SPDR;
}

SPI_SendData of STM8S library:

void SPI_SendData(uint8_t Data)
{
  SPI->DR = Data; /* Write in the DR register the data to be sent*/
}

SPI_ReceiveData of stm8s

uint8_t SPI_ReceiveData(void)
{
  return ((uint8_t)SPI->DR); /* Return the data in the DR register*/
}

I want to convert this code to stm8s, but it does not work.

Arduino Code:

((uint8_t*)data)[i] = spi_transfer(NRF905_CMD_NOP);

STM8S Code:

SPI_SendData(NRF905_CMD_NOP);
((uint8_t*)data)[i] = SPI_ReceiveData();

Thank you!

\$\endgroup\$
  • \$\begingroup\$ What do you mean with 'doesn't work'? \$\endgroup\$ – Vladimir Cravero Jan 17 '16 at 23:37
1
\$\begingroup\$

There are flags that you must wait on, flags that will indicate when SPI transmission is done or a byte has been received. Try this:

while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET); // wait for any previous data to be sent; checks if the Transmit Register is empty
SPI_SendData(NRF905_CMD_NOP);
while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);  // wait for a byte to come in; checks if the Receive register is not empty
((uint8_t*)data)[i] = SPI_ReceiveData();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.