zh_nrf24/include/zh_rf24.h

176 lines
6.3 KiB
C
Raw Normal View History

2024-05-14 16:11:08 +03:00
/**
* @file
* Header file for the zh_rf24 component.
*
*/
#pragma once
#include "string.h"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "esp_heap_caps.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
2024-05-15 13:38:12 +03:00
#include "esp_event.h"
2024-05-14 16:11:08 +03:00
/**
* @brief Unique identifier of RF24 interface events base. Used when registering the event handler.
*
*/
#define ESP_EVENT_BASE ZH_RF24
/**
* @brief Default values for zh_rf24_init_config_t structure for initial initialization of RF24 interface.
*
*/
2024-05-16 16:02:27 +03:00
#define ZH_RF24_INIT_CONFIG_DEFAULT() \
{ \
.ce_pin = 1, \
.csn_pin = 2, \
.sck_pin = 3, \
.mosi_pin = 4, \
.miso_pin = 5, \
.irq_pin = 6, \
.channel = 100, \
.tx_address = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7}, \
.rx_pipe_1_address = {0xC2, 0xC2, 0xC2, 0xC2, 0xC2}, \
.rx_pipe_2_address = 0xC3, \
.rx_pipe_3_address = 0xC4, \
.rx_pipe_4_address = 0xC5, \
.rx_pipe_5_address = 0xC6, \
.task_priority = 3, \
.stack_size = 2048, \
.queue_size = 16 \
2024-05-14 16:11:08 +03:00
}
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Structure for initial initialization of RF24 interface.
*
* @note Before initialize cc interface recommend initialize zh_rf24_init_config_t structure with default values.
*
* @code
* zh_rf24_init_config_t config = ZH_RF24_INIT_CONFIG_DEFAULT()
* @endcode
*/
typedef struct
{
uint8_t ce_pin;
uint8_t csn_pin;
uint8_t sck_pin;
uint8_t mosi_pin;
uint8_t miso_pin;
uint8_t irq_pin;
uint8_t channel;
uint8_t tx_address[5];
2024-05-16 16:02:27 +03:00
uint8_t rx_pipe_1_address[5];
2024-05-14 16:11:08 +03:00
uint8_t rx_pipe_2_address;
uint8_t rx_pipe_3_address;
uint8_t rx_pipe_4_address;
uint8_t rx_pipe_5_address;
uint8_t task_priority; ///< Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
uint16_t stack_size; ///< Stack size for task for the ESP-NOW messages processing. @note The minimum size is 3072 bytes.
uint8_t queue_size; ///< Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 32.
} __attribute__((packed)) zh_rf24_init_config_t;
/// \cond
ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE);
/// \endcond
/**
* @brief Enumeration of possible RF24 events.
*
*/
typedef enum
{
ZH_RF24_ON_RECV_EVENT, ///< The event when the RF24 message was received.
ZH_RF24_ON_SEND_EVENT ///< The event when the RF24 message was sent.
} __attribute__((packed)) zh_rf24_event_type_t;
/**
* @brief Enumeration of possible status of sent ESP-NOW message.
*
*/
typedef enum
{
ZH_RF24_SEND_SUCCESS, ///< If RF24 message was sent success.
ZH_RF24_SEND_FAIL ///< If RF24 message was sent fail.
} __attribute__((packed)) zh_rf24_on_send_event_type_t;
/**
* @brief Structure for sending data to the event handler when an ESP-NOW message was sent.
*
* @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_SEND_EVENT event.
*
*/
typedef struct
{
2024-05-15 10:12:40 +03:00
zh_rf24_on_send_event_type_t status; ///< Status of sent RF24 message. @note
2024-05-14 16:11:08 +03:00
} __attribute__((packed)) zh_rf24_event_on_send_t;
/**
* @brief Structure for sending data to the event handler when an ESP-NOW message was received.
*
* @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_RECV_EVENT event.
*/
typedef struct
{
uint8_t pipe; ///< MAC address of the sender ESP-NOW message. @note
uint8_t *data; ///< Pointer to the data of the received ESP-NOW message. @note
uint8_t data_len; ///< Size of the received ESP-NOW message. @note
2024-05-15 10:12:40 +03:00
} __attribute__((packed)) zh_rf24_event_on_recv_t;
2024-05-14 16:11:08 +03:00
/**
* @brief Initialize ESP-NOW interface.
*
* @note Before initialize ESP-NOW interface recommend initialize zh_network_init_config_t structure with default values.
*
* @code
* zh_network_init_config_t config = ZH_NETWORK_INIT_CONFIG_DEFAULT()
* @endcode
*
* @param[in] config Pointer to ESP-NOW initialized configuration structure. Can point to a temporary variable.
*
* @return
* - ESP_OK if initialization was success
* - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized
* - ESP_FAIL if any internal error
*/
2024-05-15 10:12:40 +03:00
esp_err_t zh_rf24_init(zh_rf24_init_config_t *config);
2024-05-14 16:11:08 +03:00
/**
* @brief Deinitialize ESP-NOW interface.
*
* @return
* - ESP_OK if deinitialization was success
*/
2024-05-15 10:12:40 +03:00
esp_err_t zh_rf24_deinit(void);
2024-05-14 16:11:08 +03:00
/**
* @brief Send ESP-NOW data.
*
* @param[in] target Pointer to a buffer containing an eight-byte target MAC. Can be NULL for broadcast.
* @param[in] data Pointer to a buffer containing the data for send.
* @param[in] data_len Sending data length.
*
* @note The function will return an ESP_ERR_INVALID_STATE error if less than 50% of the size set at initialization remains in the message queue.
*
* @return
* - ESP_OK if sent was success
* - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_INVALID_STATE if queue for outgoing data is almost full
* - ESP_FAIL if ESP-NOW is not initialized
*/
2024-05-16 16:02:27 +03:00
esp_err_t zh_rf24_send(const uint8_t *data, const uint8_t data_len);
2024-05-14 16:11:08 +03:00
#ifdef __cplusplus
}
#endif