mokhwasomssi / stm32_hal_nrf24l01p

nRF24L01+ library with STM32 HAL driver
70 stars 9 forks source link
nrf24l01 spi stm32 stm32cubeide stm32cubemx stm32f4 stm32hal

stm32_hal_nrf24l01p

Brief

nRF24L01+ is a single chip 2.4GHz transceiver.
TAG - nRF24L01+ SPI STM32 HAL

Library Features

Dev Environment

STM32CubeMX

Example

nrf24l01p.h

/* User Configurations */
#define NRF24L01P_SPI                     (&hspi2)

#define NRF24L01P_SPI_CS_PIN_PORT         GPIOB 
#define NRF24L01P_SPI_CS_PIN_NUMBER       GPIO_PIN_13

#define NRF24L01P_CE_PIN_PORT             GPIOB
#define NRF24L01P_CE_PIN_NUMBER           GPIO_PIN_12

#define NRF24L01P_IRQ_PIN_PORT            GPIOA
#define NRF24L01P_IRQ_PIN_NUMBER          GPIO_PIN_8

#define NRF24L01P_PAYLOAD_LENGTH          8     // 1 - 32bytes

main.c

Transmitter

#include "nrf24l01p.h"

// data array to be sent
uint8_t tx_data[NRF24L01P_PAYLOAD_LENGTH] = {0, 1, 2, 3, 4, 5, 6, 7};

// for rx interrupt
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);

int main(void)
{
    nrf24l01p_tx_init(2500, _1Mbps);

    while (1)
    {
        // change tx datas
        for(int i= 0; i < 8; i++)
            tx_data[i]++;

        // transmit
        nrf24l01p_tx_transmit(tx_data);
        HAL_Delay(100);
    }
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin == NRF24L01P_IRQ_PIN_NUMBER)
        nrf24l01p_tx_irq(); // clear interrupt flag
}

Receiver

#include "nrf24l01p.h"

// data array to be read
uint8_t rx_data[NRF24L01P_PAYLOAD_LENGTH] = { 0, };

// for tx interrupt
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);

int main(void)
{
    nrf24l01p_rx_init(2500, _1Mbps);

    while (1)
    {
        // Nothing to do
        HAL_Delay(100);
    }
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin == NRF24L01P_IRQ_PIN_NUMBER)
        nrf24l01p_rx_receive(rx_data); // read data when data ready flag is set
}