/*! * \brief Write a register value to the transceiver * \param reg Register to write * \param val Value of the register to write */ void RF_WriteRegister(uint8_t reg, uint8_t val) { RF_CSN_LOW(); /* initiate command sequence */ (void)SPI_WriteRead(RF24_W_REGISTER|reg); /* write register command */ (void)SPI_WriteRead(val); /* write value */ RF_CSN_HIGH(); /* end command sequence */ RF_WAIT_US(10); /* insert a delay until next command */ }
/*! * \brief Reads a byte value from a register * \param reg Register to read * \return Register value read */ uint8_t RF_ReadRegister(uint8_t reg) { uint8_t val; RF_CSN_LOW(); (void)SPI_WriteRead(reg); val = SPI_WriteRead(0); /* write dummy */ RF_CSN_HIGH(); RF_WAIT_US(10); return val; }
/** * @brief * @param Device: The device to use * @retval None */ void NRF24L01_ReadRegister(NRF24L01_Device* Device, uint8_t Register, uint8_t* Buffer, uint8_t BufferSize) { SELECT_DEVICE(Device); uint8_t status = SPI_WriteRead(Device->SPIDevice, R_REGISTER | Register); /* R_REGISTER command only have 5 data bytes, datasheet page 51 */ if (BufferSize > 5) BufferSize = 5; /* LSByte first, datasheet page 50 */ for (int32_t i = BufferSize-1; i >= 0; i--) { Buffer[i] = SPI_WriteRead(Device->SPIDevice, NOP); } DESELECT_DEVICE(Device); }
/*! * \brief Read multiple bytes from the bus. * \param reg Register address * \param buf Buffer where to write the data * \param bufSize Buffer size in bytes */ void RF_ReadRegisterData(uint8_t reg, uint8_t *buf, uint8_t bufSize) { RF_CSN_LOW(); (void)SPI_WriteRead(RF24_R_REGISTER|reg); SPI_WriteReadBuffer(buf, buf, bufSize); RF_CSN_HIGH(); RF_WAIT_US(10); }
/*! * \brief Writes multiple bytes to the SPI bus * \param bufOut Buffer to write * \param bufSize Size of buffer */ static void SPI_WriteBuffer(uint8_t *bufOut, uint8_t bufSize) { uint8_t i; for(i=0; i<bufSize; i++) { (void)SPI_WriteRead(bufOut[i]); } }
/*! * \brief Writes a buffer to the SPI bus and the same time reads in the data * \param bufOut Output buffer * \param bufIn Input buffer * \param bufSize Size of input and output buffer */ static void SPI_WriteReadBuffer(uint8_t *bufOut, uint8_t *bufIn, uint8_t bufSize) { uint8_t i; for(i=0; i<bufSize; i++) { bufIn[i] = SPI_WriteRead(bufOut[i]); } }
/*! * \brief Writes a byte and reads back one byte * \param val Byte to write to the SPI shift register * \return Byte read from the SPI shift register */ uint8_t RF_WriteRead(uint8_t val) { RF_CSN_LOW(); val = SPI_WriteRead(val); RF_CSN_HIGH(); RF_WAIT_US(10); return val; }
/*! * \brief Write multiple bytes to the bus. * \param reg Register address * \param buf Buffer of what to write * \param bufSize Buffer size in bytes */ void RF_WriteRegisterData(uint8_t reg, uint8_t *buf, uint8_t bufSize) { RF_CSN_LOW(); (void)SPI_WriteRead(RF24_W_REGISTER|reg); /* not masking registers as it would conflict with RF24_W_TX_PAYLOAD */ SPI_WriteBuffer(buf, bufSize); RF_CSN_HIGH(); RF_WAIT_US(10); }
BYTE SPI_RW(BYTE byte) { BYTE res = SPI_WriteRead(byte); delay_100us();//must be after the transaction is started TODO debug the timing to check with which values commands are correctly handled return(res); }
/** * @brief Reads the STATUS register in the nRF24L01 * @param Device: The device to use * @retval The status register */ uint8_t NRF24L01_GetStatus(NRF24L01_Device* Device) { SELECT_DEVICE(Device); uint8_t status = SPI_WriteRead(Device->SPIDevice, NOP); DESELECT_DEVICE(Device); return status; }
/** * @brief * @param Device: The device to use * @retval None */ void NRF24L01_WriteRegister(NRF24L01_Device* Device, uint8_t Register, uint8_t* Buffer, uint8_t BufferSize) { DISABLE_DEVICE(Device); /* W_REGISTER is executable in power down or standby modes only */ SELECT_DEVICE(Device); uint8_t status = SPI_WriteRead(Device->SPIDevice, W_REGISTER | Register); /* W_REGISTER command only have 5 data bytes, datasheet page 51 */ if (BufferSize > 5) BufferSize = 5; /* LSByte first, datasheet page 50 */ for (int32_t i = BufferSize-1; i >= 0; i--) { /* Have to do WriteRead for some reason, otherwise one byte will be lost */ status = SPI_WriteRead(Device->SPIDevice, Buffer[i]); } DESELECT_DEVICE(Device); ENABLE_DEVICE(Device); }
/** * @brief Get data from the RX buffer * @param Device: The device to use * @param Storage: Pointer to where the data should be stored * @retval The amount of data received */ uint8_t NRF24L01_GetDataFromRxBuffer(NRF24L01_Device* Device, uint8_t* Buffer) { SELECT_DEVICE(Device); SPI_WriteRead(Device->SPIDevice, R_RX_PAYLOAD); uint8_t dataCount = SPI_WriteRead(Device->SPIDevice, NOP); /* * Only get the maximum amount of data which can be stored in one payload (MAX_DATA_COUNT) * When dataCount was used as a limit we got a hardfault because there is no control if * dataCount is > MAX_DATA_COUNT and the data where Buffer exist might become corrupt */ for (uint32_t i = 0; i < MAX_DATA_COUNT; i++) { Buffer[i] = SPI_WriteRead(Device->SPIDevice, NOP); } DESELECT_DEVICE(Device); /* * Flush just in case */ NRF24L01_FlushRxBuffer(Device); return dataCount; }
/** * @brief Write data and send it to the address specified in TX_ADDR * @param Device: The device to use * @param Data: Pointer to where the data is stored * @param ByteCount: The number of bytes in Data * @retval None */ ErrorStatus NRF24L01_WritePayload(NRF24L01_Device* Device, uint8_t* Data, uint8_t DataCount) { /* You can only send the amount of data specified in MAX_DATA_COUNT */ if (DataCount > MAX_DATA_COUNT) return ERROR; /* Try to take the semaphore */ if (xSemaphoreTake(Device->xTxSemaphore, 100 / portTICK_PERIOD_MS) == pdTRUE) { /* Semaphore was taken so we can proceed with sending new data */ DISABLE_DEVICE(Device); /* Disable the device while sending data to TX buffer */ NRF24L01_PowerUpInTxMode(Device); /* Power up in TX mode */ NRF24L01_FlushTxBuffer(Device); /* Flush the TX buffer */ SELECT_DEVICE(Device); SPI_WriteRead(Device->SPIDevice, W_TX_PAYLOAD); /* We want to write the TX payload */ SPI_WriteRead(Device->SPIDevice, DataCount); /* Write the data count */ uint32_t i; for (i = 0; i < DataCount; i++) { SPI_WriteRead(Device->SPIDevice, Data[i]); /* Write the data */ } for (i++; i <= MAX_DATA_COUNT; i++) { SPI_WriteRead(Device->SPIDevice, PAYLOAD_FILLER_DATA); /* Fill the rest of the payload with filler data */ } DESELECT_DEVICE(Device); ENABLE_DEVICE(Device); return SUCCESS; } else return ERROR; }
/** * @brief Flush the RX Buffer * @param Device: The device to use * @retval None */ void NRF24L01_FlushRxBuffer(NRF24L01_Device* Device) { SELECT_DEVICE(Device); SPI_WriteRead(Device->SPIDevice, FLUSH_RX); DESELECT_DEVICE(Device); }
void RF_Write(uint8_t val) { RF_CSN_LOW(); (void)SPI_WriteRead(val); RF_CSN_HIGH(); RF_WAIT_US(10); }