diff --git a/include/zh_rf24.h b/include/zh_rf24.h index c3906c5..48c9127 100644 --- a/include/zh_rf24.h +++ b/include/zh_rf24.h @@ -25,26 +25,28 @@ * @brief Default values for zh_rf24_init_config_t structure for initial initialization of RF24 interface. * */ -#define ZH_RF24_INIT_CONFIG_DEFAULT() \ - { \ - .ce_pin = 1, \ - .csn_pin = 2, \ - .sck_pin = 3, \ - .mosi_pin = 4, \ - .miso_pin = 5, \ - .irq_pin = 6, \ - .work_mode = ZH_RF24_RECEIVER, \ - .channel = 100, \ - .pa_level = ZH_RF24_PA_MAX, \ - .data_rate = ZH_RF24_250KBPS, \ - .network_id = 0xFAFBFCFD, \ - .task_priority = 4, \ - .stack_size = 3072, \ - .queue_size = 32, \ - .max_waiting_time = 1000, \ - .id_vector_size = 100, \ - .route_vector_size = 100, \ - .wifi_interface = WIFI_IF_STA \ +#define ZH_RF24_INIT_CONFIG_DEFAULT() \ + { \ + .ce_pin = 1, \ + .csn_pin = 2, \ + .sck_pin = 3, \ + .mosi_pin = 4, \ + .miso_pin = 5, \ + .irq_pin = 6, \ + .work_mode = ZH_RF24_RECEIVER, \ + .channel = 100, \ + .pa_level = ZH_RF24_PA_MAX, \ + .data_rate = ZH_RF24_250KBPS, \ + .tx_address = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7}, \ + .rx_address_preamble = {0xC2, 0xC2, 0xC2, 0xC2}, \ + .rx_pipe_1_address = 0xC2, \ + .rx_pipe_2_address = 0xC3, \ + .rx_pipe_3_address = 0xC4, \ + .rx_pipe_4_address = 0xC5, \ + .rx_pipe_5_address = 0xC6, \ + .task_priority = 4, \ + .stack_size = 2048, \ + .queue_size = 16 \ } #ifdef __cplusplus @@ -153,8 +155,7 @@ extern "C" */ typedef struct { - uint8_t pipe; ///< MAC address of the device to which the ESP-NOW message was sent. @note - zh_rf24_on_send_event_type_t status; ///< Status of sent ESP-NOW message. @note + zh_rf24_on_send_event_type_t status; ///< Status of sent RF24 message. @note } __attribute__((packed)) zh_rf24_event_on_send_t; /** @@ -167,7 +168,7 @@ extern "C" 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 - } __attribute__((packed)) zh_network_event_on_recv_t; + } __attribute__((packed)) zh_rf24_event_on_recv_t; /** * @brief Initialize ESP-NOW interface. @@ -186,7 +187,7 @@ extern "C" * - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized * - ESP_FAIL if any internal error */ - esp_err_t zh_network_init(zh_rf24_init_config_t *config); + esp_err_t zh_rf24_init(zh_rf24_init_config_t *config); /** * @brief Deinitialize ESP-NOW interface. @@ -194,7 +195,7 @@ extern "C" * @return * - ESP_OK if deinitialization was success */ - esp_err_t zh_network_deinit(void); + esp_err_t zh_rf24_deinit(void); /** * @brief Send ESP-NOW data. @@ -211,7 +212,7 @@ extern "C" * - ESP_ERR_INVALID_STATE if queue for outgoing data is almost full * - ESP_FAIL if ESP-NOW is not initialized */ - esp_err_t zh_network_send(const uint8_t *data, const uint8_t data_len, const bool confirm); + esp_err_t zh_rf24_send(const uint8_t *data, const uint8_t data_len, const bool confirm); #ifdef __cplusplus } diff --git a/zh_rf24.c b/zh_rf24.c index 2805ac7..ae39c61 100644 --- a/zh_rf24.c +++ b/zh_rf24.c @@ -5,7 +5,68 @@ #include "zh_rf24.h" - +/// \cond +#define ZH_RF24_DATA_SEND_SUCCESS BIT0 +#define ZH_RF24_DATA_SEND_FAIL BIT1 #define ZH_RF24_MAX_MESSAGE_SIZE 32 +/// \endcond -ESP_EVENT_DEFINE_BASE(ZH_RF24); \ No newline at end of file +static void s_zh_rf24_processing(void *pvParameter); + +static const char *TAG = "zh_rf24"; + +static EventGroupHandle_t s_zh_rf24_send_cb_status_event_group_handle = {0}; +static QueueHandle_t s_zh_rf24_queue_handle = {0}; +static TaskHandle_t s_zh_rf24_processing_task_handle = {0}; +static zh_rf24_init_config_t s_zh_rf24_init_config = {0}; +static spi_device_handle_t s_zh_spi_device_handle = {0}; +static bool s_zh_rf24_is_initialized = false; + +/// \cond +typedef enum +{ + ZH_RF24_RECV, + ZH_RF24_SEND, +} __attribute__((packed)) zh_rf24_queue_id_t; + +typedef struct +{ + uint8_t pipe; + uint8_t *data; + uint8_t data_len; +} __attribute__((packed)) zh_rf24_queue_data_t; + +typedef struct +{ + zh_rf24_queue_id_t id; + zh_rf24_queue_data_t data; +} __attribute__((packed)) zh_rf24_queue_t; + +ESP_EVENT_DEFINE_BASE(ZH_RF24); +/// \endcond + +esp_err_t zh_rf24_init(zh_rf24_init_config_t *config) +{ + ESP_LOGI(TAG, "RF24 initialization begin."); + if (config == NULL) + { + ESP_LOGE(TAG, "RF24 initialization fail. Invalid argument."); + return ESP_ERR_INVALID_ARG; + } + s_zh_rf24_init_config = *config; + s_zh_rf24_send_cb_status_event_group_handle = xEventGroupCreate(); + s_zh_rf24_queue_handle = xQueueCreate(s_zh_rf24_init_config.queue_size, sizeof(zh_rf24_queue_t)); + // if (esp_now_init() != ESP_OK || esp_now_register_send_cb(s_zh_espnow_send_cb) != ESP_OK || esp_now_register_recv_cb(s_zh_espnow_recv_cb) != ESP_OK) + // { + // ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error."); + // return ESP_FAIL; + // } + // if (xTaskCreatePinnedToCore(&s_zh_espnow_processing, "NULL", s_zh_espnow_init_config.stack_size, NULL, s_zh_espnow_init_config.task_priority, &s_zh_espnow_processing_task_handle, tskNO_AFFINITY) != pdPASS) + // { + // ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error."); + // return ESP_FAIL; + // } + s_zh_rf24_is_initialized = true; + ESP_LOGI(TAG, "RF24 initialization success."); + return ESP_OK; +} \ No newline at end of file