NRF24L+ STM8 Compatible Library
Macros | Functions
mirf.c File Reference

This file contains the NRF24 library ported to the STM8 using SDCC. More...

#include "stdio.h"
#include "mirf.h"
#include "delay.h"
Include dependency graph for mirf.c:

Macros

#define NRF_CHANNEL_NR   114
 Channel Number for the NRF, must be the same in all devices. More...
 
#define NRF_PAYLOAD_SIZE   32
 Payload size for the NRF, must be the same in all devices. More...
 
#define CE_PIN_LETTER   GPIOC
 Pin letter the NRF CE is connected to. More...
 
#define CE_PIN_NUMBER   GPIO_PIN_4
 Pin number the NRF CE is connected to. More...
 
#define CSN_PIN_LETTER   GPIOC
 Pin letter the NRF CSN is connected to. More...
 
#define CSN_PIN_NUMBER   GPIO_PIN_3
 Pin number the NRF CSN is connected to. More...
 

Functions

void Nrf24_init ()
 This function initializes the CE/CS Pins and SPI. More...
 
bool spi_write_byte (uint8_t *Dataout, uint8_t DataLength)
 This function writes data to MOSI. It checks if it is being used. More...
 
bool spi_read_byte (uint8_t *Datain, uint8_t DataLength)
 Reads data from MISO. More...
 
uint8_t spi_transfer (uint8_t address)
 An SPI "transfer", used in this library to simply send an address or command to the NRF. More...
 
void spi_csnHi ()
 Set CSN High. More...
 
void spi_csnLow ()
 Set CSN Low. More...
 
void Nrf24_config (bool *PTX)
 This sends the necessary registers to the NRF. Initially on RX mode. More...
 
ErrorStatus Nrf24_setRADDR (uint8_t *adr)
 Sets the recieving address (5) bytes! More...
 
ErrorStatus Nrf24_setTADDR (uint8_t *adr)
 Sets the transmitting device address. More...
 
void Nrf24_addRADDR (uint8_t pipe, uint8_t adr)
 Add more device addresses to recieve (NOT TESTED). More...
 
bool Nrf24_dataReady ()
 Checks if data is available for reading. More...
 
uint8_t Nrf24_getDataPipe ()
 Get pipe number for reading. More...
 
bool Nrf24_rxFifoEmpty ()
 Checks if FIFO is empty. More...
 
void Nrf24_getData (uint8_t *data)
 Reads NRF payload bytes into data array. More...
 
void Nrf24_configRegister (uint8_t reg, uint8_t value)
 Clocks only one byte into the given MiRF register. More...
 
void Nrf24_readRegister (uint8_t reg, uint8_t *value, uint8_t len)
 Reads an array of bytes from the given start position in the MiRF registers. More...
 
void Nrf24_writeRegister (uint8_t reg, uint8_t *value, uint8_t len)
 Writes an array of bytes into inte the MiRF registers. More...
 
void Nrf24_send (uint8_t *value, bool *PTX)
 Sends a data package to the default address. Be sure to send the correct amount of bytes as configured as payload on the receiver. More...
 
bool Nrf24_isSending (bool *PTX)
 Test if chip is still sending. When sending has finished return chip to listening. More...
 
bool Nrf24_isSend (int timeout, bool *PTX)
 Test if Sending has finished or retry is over. More...
 
uint8_t Nrf24_getStatus ()
 Get the status of the NRF. More...
 
void Nrf24_powerUpRx (bool *PTX)
 Starts up the NRF to be RX, the default. More...
 
void Nrf24_flushRx ()
 Flushes RX FIFO. More...
 
void Nrf24_powerUpTx (bool *PTX)
 Sets the NRF to TX mode. More...
 
void Nrf24_ceHi ()
 Set CE High. More...
 
void Nrf24_ceLow ()
 Set CE Low. More...
 
void Nrf24_powerDown ()
 Power down the NRF. More...
 
void Nrf24_SetOutputRF_PWR (uint8_t val)
 Set TX Power. More...
 
void Nrf24_SetSpeedDataRates (uint8_t val)
 Select between the high speed data rates. More...
 
void Nrf24_setRetransmitDelay (uint8_t val)
 Set Auto Retransmit Delay. More...
 

Detailed Description

This file contains the NRF24 library ported to the STM8 using SDCC.

Author
nopnop2002, nicogutz
Version
V1
Date
9-December-2023

MIT License Copyright (c) 2020 nopnop2002 Copyright (c) 2023 nicogutz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Macro Definition Documentation

◆ CE_PIN_LETTER

#define CE_PIN_LETTER   GPIOC

Pin letter the NRF CE is connected to.

◆ CE_PIN_NUMBER

#define CE_PIN_NUMBER   GPIO_PIN_4

Pin number the NRF CE is connected to.

◆ CSN_PIN_LETTER

#define CSN_PIN_LETTER   GPIOC

Pin letter the NRF CSN is connected to.

◆ CSN_PIN_NUMBER

#define CSN_PIN_NUMBER   GPIO_PIN_3

Pin number the NRF CSN is connected to.

◆ NRF_CHANNEL_NR

#define NRF_CHANNEL_NR   114

Channel Number for the NRF, must be the same in all devices.

◆ NRF_PAYLOAD_SIZE

#define NRF_PAYLOAD_SIZE   32

Payload size for the NRF, must be the same in all devices.

Function Documentation

◆ Nrf24_addRADDR()

void Nrf24_addRADDR ( uint8_t  pipe,
uint8_t  adr 
)

Add more device addresses to recieve (NOT TESTED).

Parameters
pipeSelected pipe (2-5).
adrThe reciving address.
Here is the call graph for this function:

◆ Nrf24_ceHi()

void Nrf24_ceHi ( )

Set CE High.

Here is the caller graph for this function:

◆ Nrf24_ceLow()

void Nrf24_ceLow ( )

Set CE Low.

Here is the caller graph for this function:

◆ Nrf24_config()

void Nrf24_config ( bool *  PTX)

This sends the necessary registers to the NRF. Initially on RX mode.

Parameters
PTXA pointer to a boolean that stores the state of the NRF (TRUE == TX).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_configRegister()

void Nrf24_configRegister ( uint8_t  reg,
uint8_t  value 
)

Clocks only one byte into the given MiRF register.

Parameters
regRegister to change
valueValue to set
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_dataReady()

bool Nrf24_dataReady ( )

Checks if data is available for reading.

Returns
TRUE if ready
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_flushRx()

void Nrf24_flushRx ( )

Flushes RX FIFO.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_getData()

void Nrf24_getData ( uint8_t *  data)

Reads NRF payload bytes into data array.

Parameters
dataAn array to store the data based on NRF_PAYLOAD_SIZE
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_getDataPipe()

uint8_t Nrf24_getDataPipe ( )

Get pipe number for reading.

Returns
The pipe number
Here is the call graph for this function:

◆ Nrf24_getStatus()

uint8_t Nrf24_getStatus ( )

Get the status of the NRF.

Returns
STATUS register value
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_init()

void Nrf24_init ( )

This function initializes the CE/CS Pins and SPI.

Here is the caller graph for this function:

◆ Nrf24_isSend()

bool Nrf24_isSend ( int  timeout,
bool *  PTX 
)

Test if Sending has finished or retry is over.

Parameters
timeoutHow long to timeout in ms
PTXThe boolean holding the RX/TX state of the NRF
Returns
When sending has finished return TRUE When maximum number of TX retries return FALSE
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_isSending()

bool Nrf24_isSending ( bool *  PTX)

Test if chip is still sending. When sending has finished return chip to listening.

Parameters
PTXThe boolean holding the RX/TX state of the NRF
Returns
TRUE if is sending
Here is the call graph for this function:

◆ Nrf24_powerDown()

void Nrf24_powerDown ( )

Power down the NRF.

Here is the call graph for this function:

◆ Nrf24_powerUpRx()

void Nrf24_powerUpRx ( bool *  PTX)

Starts up the NRF to be RX, the default.

Parameters
PTXThe boolean holding the RX/TX state of the NRF
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_powerUpTx()

void Nrf24_powerUpTx ( bool *  PTX)

Sets the NRF to TX mode.

Parameters
PTXThe boolean holding the RX/TX state of the NRF
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_readRegister()

void Nrf24_readRegister ( uint8_t  reg,
uint8_t *  value,
uint8_t  len 
)

Reads an array of bytes from the given start position in the MiRF registers.

Parameters
regThe register to read
valueThe array to store the value
lenThe length of the register
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_rxFifoEmpty()

bool Nrf24_rxFifoEmpty ( )

Checks if FIFO is empty.

Returns
True if empty
Here is the call graph for this function:

◆ Nrf24_send()

void Nrf24_send ( uint8_t *  value,
bool *  PTX 
)

Sends a data package to the default address. Be sure to send the correct amount of bytes as configured as payload on the receiver.

Parameters
valueThe value to send, length based on NRF_PAYLOAD_SIZE
PTXA bool containing the RX/TX state of the NRF
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_SetOutputRF_PWR()

void Nrf24_SetOutputRF_PWR ( uint8_t  val)

Set TX Power.

Parameters
val0=-18dBm,1=-12dBm,2=-6dBm,3=0dBm
Here is the call graph for this function:

◆ Nrf24_setRADDR()

ErrorStatus Nrf24_setRADDR ( uint8_t *  adr)

Sets the recieving address (5) bytes!

Parameters
adrPointer to the address.
Returns
SUCCESS if it worked.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_setRetransmitDelay()

void Nrf24_setRetransmitDelay ( uint8_t  val)

Set Auto Retransmit Delay.

Parameters
val0=250us, 1=500us, ... 15=4000us
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_SetSpeedDataRates()

void Nrf24_SetSpeedDataRates ( uint8_t  val)

Select between the high speed data rates.

Parameters
val0=1Mbps (Recommended), 1=2Mbps, 2=250Kbps
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_setTADDR()

ErrorStatus Nrf24_setTADDR ( uint8_t *  adr)

Sets the transmitting device address.

Parameters
adrThe address (5 Byte) to write.
Returns
SUCESS if it worked.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Nrf24_writeRegister()

void Nrf24_writeRegister ( uint8_t  reg,
uint8_t *  value,
uint8_t  len 
)

Writes an array of bytes into inte the MiRF registers.

Parameters
regRegister to write
valueArray containing the value to store
lenLength of the register
Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_csnHi()

void spi_csnHi ( )

Set CSN High.

Here is the caller graph for this function:

◆ spi_csnLow()

void spi_csnLow ( )

Set CSN Low.

Here is the caller graph for this function:

◆ spi_read_byte()

bool spi_read_byte ( uint8_t *  Datain,
uint8_t  DataLength 
)

Reads data from MISO.

Parameters
DatainThe uint8_t to be written to.
DataLengthThe length of the data
Returns
TRUE if successful.
Here is the caller graph for this function:

◆ spi_transfer()

uint8_t spi_transfer ( uint8_t  address)

An SPI "transfer", used in this library to simply send an address or command to the NRF.

Parameters
addressThe byte to be sent.
Returns
The data recieved (not actually used anywhere).
Here is the caller graph for this function:

◆ spi_write_byte()

bool spi_write_byte ( uint8_t *  Dataout,
uint8_t  DataLength 
)

This function writes data to MOSI. It checks if it is being used.

Parameters
DataoutA uint8_t Array
DataLengthThe length of the Array
Returns
TRUE if it worked
Here is the caller graph for this function: