wip:
This commit is contained in:
1 parent
81a2eb04ac
commit
98fb08cd6f
4 files changed
+45
-44
No files matched your search
@@ -1 +1,2 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
|
.vscode
|
||||||
+1
-1
@@ -1 +1 @@
|
|||||||
idf_component_register(SRCS "zh_rf_gateway.c" INCLUDE_DIRS "include" REQUIRES esp_driver_spi esp_driver_gpio esp_event nvs_flash zh_rf_config)
|
idf_component_register(SRCS "zh_nrf24_gateway.c" INCLUDE_DIRS "include" REQUIRES esp_driver_spi esp_driver_gpio esp_event nvs_flash zh_nrf24_config)
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* @file zh_rf_gateway.h
|
* @file zh_nrf24_gateway.h
|
||||||
*
|
*
|
||||||
* nRF24L01+ gateway for ESP-IDF.
|
* nRF24L01+ gateway for ESP-IDF.
|
||||||
*
|
*
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "driver/spi_master.h"
|
#include "driver/spi_master.h"
|
||||||
#include "zh_rf_config.h"
|
#include "zh_nrf24_config.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -27,7 +27,7 @@ extern "C" {
|
|||||||
* @brief Event base for gateway events.
|
* @brief Event base for gateway events.
|
||||||
*
|
*
|
||||||
* Event data for ZH_NRF_GATEWAY_EVENT_SENSOR_DATA is a copy of
|
* Event data for ZH_NRF_GATEWAY_EVENT_SENSOR_DATA is a copy of
|
||||||
* zh_rf_sensor_transmitted_data_t.
|
* zh_nrf24_sensor_transmitted_data_t.
|
||||||
*/
|
*/
|
||||||
ESP_EVENT_DECLARE_BASE(ZH_NRF_GATEWAY_EVENTS);
|
ESP_EVENT_DECLARE_BASE(ZH_NRF_GATEWAY_EVENTS);
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ ESP_EVENT_DECLARE_BASE(ZH_NRF_GATEWAY_EVENTS);
|
|||||||
*/
|
*/
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
/** Sensor telemetry received (data: zh_rf_sensor_transmitted_data_t) */
|
/** Sensor telemetry received (data: zh_nrf24_sensor_transmitted_data_t) */
|
||||||
ZH_NRF_GATEWAY_EVENT_SENSOR_DATA,
|
ZH_NRF_GATEWAY_EVENT_SENSOR_DATA,
|
||||||
} zh_nrf_gateway_event_t;
|
} zh_nrf_gateway_event_t;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "zh_rf_gateway.h"
|
#include "zh_nrf24_gateway.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -233,9 +233,9 @@ static uint8_t allocate_device_id(void)
|
|||||||
* ============================================================ */
|
* ============================================================ */
|
||||||
static void send_pairing_ack(uint8_t req_type, uint16_t token, uint8_t new_id)
|
static void send_pairing_ack(uint8_t req_type, uint16_t token, uint8_t new_id)
|
||||||
{
|
{
|
||||||
uint8_t pkt[ZH_RF_PAIRING_PACKET_SIZE] = {
|
uint8_t pkt[ZH_NRF24_PAIRING_PACKET_SIZE] = {
|
||||||
req_type,
|
req_type,
|
||||||
ZH_RF_PAIRING_ACK_MAGIC,
|
ZH_NRF24_PAIRING_ACK_MAGIC,
|
||||||
(uint8_t)(token >> 8),
|
(uint8_t)(token >> 8),
|
||||||
(uint8_t)(token & 0xFF),
|
(uint8_t)(token & 0xFF),
|
||||||
new_id};
|
new_id};
|
||||||
@@ -250,11 +250,11 @@ static void send_pairing_ack(uint8_t req_type, uint16_t token, uint8_t new_id)
|
|||||||
nrf_send_command(NRF_CMD_FLUSH_TX);
|
nrf_send_command(NRF_CMD_FLUSH_TX);
|
||||||
|
|
||||||
/* 3. Записать payload и запустить TX */
|
/* 3. Записать payload и запустить TX */
|
||||||
uint8_t tx[1 + ZH_RF_PAIRING_PACKET_SIZE];
|
uint8_t tx[1 + ZH_NRF24_PAIRING_PACKET_SIZE];
|
||||||
tx[0] = NRF_CMD_W_TX_PAYLOAD;
|
tx[0] = NRF_CMD_W_TX_PAYLOAD;
|
||||||
memcpy(&tx[1], pkt, ZH_RF_PAIRING_PACKET_SIZE);
|
memcpy(&tx[1], pkt, ZH_NRF24_PAIRING_PACKET_SIZE);
|
||||||
spi_transaction_t t = {
|
spi_transaction_t t = {
|
||||||
.length = (1 + ZH_RF_PAIRING_PACKET_SIZE) * 8,
|
.length = (1 + ZH_NRF24_PAIRING_PACKET_SIZE) * 8,
|
||||||
.tx_buffer = tx,
|
.tx_buffer = tx,
|
||||||
};
|
};
|
||||||
nrf_csn_low();
|
nrf_csn_low();
|
||||||
@@ -307,7 +307,7 @@ static void handle_pairing_request(const uint8_t *data)
|
|||||||
|
|
||||||
ESP_LOGI(TAG, "Pairing request: type=%u (%s), token=0x%04X",
|
ESP_LOGI(TAG, "Pairing request: type=%u (%s), token=0x%04X",
|
||||||
req_type,
|
req_type,
|
||||||
zh_get_rf_sensor_type_name((zh_rf_sensor_type_t)req_type),
|
zh_get_nrf24_sensor_type_name((zh_nrf24_sensor_type_t)req_type),
|
||||||
token);
|
token);
|
||||||
|
|
||||||
uint8_t new_id = allocate_device_id();
|
uint8_t new_id = allocate_device_id();
|
||||||
@@ -317,18 +317,18 @@ static void handle_pairing_request(const uint8_t *data)
|
|||||||
/* Data packet: parse and post to event loop. */
|
/* Data packet: parse and post to event loop. */
|
||||||
static void handle_sensor_data(const uint8_t *buf, uint8_t len)
|
static void handle_sensor_data(const uint8_t *buf, uint8_t len)
|
||||||
{
|
{
|
||||||
if (len < ZH_RF_DATA_HEADER_SIZE)
|
if (len < ZH_NRF24_DATA_HEADER_SIZE)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Too short data packet (%u)", len);
|
ESP_LOGW(TAG, "Too short data packet (%u)", len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
zh_rf_sensor_transmitted_data_t d = {0};
|
zh_nrf24_sensor_transmitted_data_t d = {0};
|
||||||
d.id = zh_rf_sensor_read_i16_le_at(buf, 0);
|
d.id = zh_nrf24_sensor_read_i16_le_at(buf, 0);
|
||||||
d.type = zh_rf_sensor_read_i16_le_at(buf, 2);
|
d.type = zh_nrf24_sensor_read_i16_le_at(buf, 2);
|
||||||
d.battery = zh_rf_sensor_read_i16_le_at(buf, 4);
|
d.battery = zh_nrf24_sensor_read_i16_le_at(buf, 4);
|
||||||
|
|
||||||
uint8_t expected = zh_get_rf_sensor_payload_size(d.type);
|
uint8_t expected = zh_get_nrf24_sensor_payload_size(d.type);
|
||||||
if (expected == 0)
|
if (expected == 0)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Unknown sensor type %d, dropping", d.type);
|
ESP_LOGW(TAG, "Unknown sensor type %d, dropping", d.type);
|
||||||
@@ -338,45 +338,45 @@ static void handle_sensor_data(const uint8_t *buf, uint8_t len)
|
|||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Length %u != expected %u for type %d (%s)",
|
ESP_LOGW(TAG, "Length %u != expected %u for type %d (%s)",
|
||||||
len, expected, d.type,
|
len, expected, d.type,
|
||||||
zh_get_rf_sensor_type_name((zh_rf_sensor_type_t)d.type));
|
zh_get_nrf24_sensor_type_name((zh_nrf24_sensor_type_t)d.type));
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (d.type)
|
switch (d.type)
|
||||||
{
|
{
|
||||||
case ZH_RF_SENSOR_TOUCH_SWITCH:
|
case ZH_NRF24_SENSOR_TOUCH_SWITCH:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ZH_RF_SENSOR_WATER_LEAKAGE:
|
case ZH_NRF24_SENSOR_WATER_LEAKAGE:
|
||||||
case ZH_RF_SENSOR_OPEN_CLOSE:
|
case ZH_NRF24_SENSOR_OPEN_CLOSE:
|
||||||
case ZH_RF_SENSOR_PLANT_HUMIDITY:
|
case ZH_NRF24_SENSOR_PLANT_HUMIDITY:
|
||||||
if (len >= ZH_RF_DATA_HEADER_SIZE + ZH_RF_DATA_FIELD_SIZE)
|
if (len >= ZH_NRF24_DATA_HEADER_SIZE + ZH_NRF24_DATA_FIELD_SIZE)
|
||||||
d.value_1 = zh_rf_sensor_read_i16_le_at(buf, 6);
|
d.value_1 = zh_nrf24_sensor_read_i16_le_at(buf, 6);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ZH_RF_SENSOR_BMP280:
|
case ZH_NRF24_SENSOR_BMP280:
|
||||||
if (len >= ZH_RF_DATA_HEADER_SIZE + 2 * ZH_RF_DATA_FIELD_SIZE)
|
if (len >= ZH_NRF24_DATA_HEADER_SIZE + 2 * ZH_NRF24_DATA_FIELD_SIZE)
|
||||||
{
|
{
|
||||||
d.value_1 = zh_rf_sensor_read_i16_le_at(buf, 6);
|
d.value_1 = zh_nrf24_sensor_read_i16_le_at(buf, 6);
|
||||||
d.value_2 = zh_rf_sensor_read_i16_le_at(buf, 8);
|
d.value_2 = zh_nrf24_sensor_read_i16_le_at(buf, 8);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ZH_RF_SENSOR_BME280:
|
case ZH_NRF24_SENSOR_BME280:
|
||||||
if (len >= ZH_RF_DATA_HEADER_SIZE + 3 * ZH_RF_DATA_FIELD_SIZE)
|
if (len >= ZH_NRF24_DATA_HEADER_SIZE + 3 * ZH_NRF24_DATA_FIELD_SIZE)
|
||||||
{
|
{
|
||||||
d.value_1 = zh_rf_sensor_read_i16_le_at(buf, 6);
|
d.value_1 = zh_nrf24_sensor_read_i16_le_at(buf, 6);
|
||||||
d.value_2 = zh_rf_sensor_read_i16_le_at(buf, 8);
|
d.value_2 = zh_nrf24_sensor_read_i16_le_at(buf, 8);
|
||||||
d.value_3 = zh_rf_sensor_read_i16_le_at(buf, 10);
|
d.value_3 = zh_nrf24_sensor_read_i16_le_at(buf, 10);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ZH_RF_SENSOR_BME680:
|
case ZH_NRF24_SENSOR_BME680:
|
||||||
if (len >= ZH_RF_DATA_HEADER_SIZE + 4 * ZH_RF_DATA_FIELD_SIZE)
|
if (len >= ZH_NRF24_DATA_HEADER_SIZE + 4 * ZH_NRF24_DATA_FIELD_SIZE)
|
||||||
{
|
{
|
||||||
d.value_1 = zh_rf_sensor_read_i16_le_at(buf, 6);
|
d.value_1 = zh_nrf24_sensor_read_i16_le_at(buf, 6);
|
||||||
d.value_2 = zh_rf_sensor_read_i16_le_at(buf, 8);
|
d.value_2 = zh_nrf24_sensor_read_i16_le_at(buf, 8);
|
||||||
d.value_3 = zh_rf_sensor_read_i16_le_at(buf, 10);
|
d.value_3 = zh_nrf24_sensor_read_i16_le_at(buf, 10);
|
||||||
d.value_4 = zh_rf_sensor_read_i16_le_at(buf, 12);
|
d.value_4 = zh_nrf24_sensor_read_i16_le_at(buf, 12);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -402,7 +402,7 @@ static bool process_one_packet(void)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint8_t width = nrf_read_payload_width();
|
uint8_t width = nrf_read_payload_width();
|
||||||
if (width > ZH_RF_MAX_PAYLOAD_SIZE)
|
if (width > ZH_NRF24_MAX_PAYLOAD_SIZE)
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Corrupt payload width %u, FLUSH_RX", width);
|
ESP_LOGW(TAG, "Corrupt payload width %u, FLUSH_RX", width);
|
||||||
nrf_send_command(NRF_CMD_FLUSH_RX);
|
nrf_send_command(NRF_CMD_FLUSH_RX);
|
||||||
@@ -410,12 +410,12 @@ static bool process_one_packet(void)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t buf[ZH_RF_MAX_PAYLOAD_SIZE] = {0};
|
uint8_t buf[ZH_NRF24_MAX_PAYLOAD_SIZE] = {0};
|
||||||
nrf_read_payload(buf, width);
|
nrf_read_payload(buf, width);
|
||||||
nrf_write_register(NRF_REG_STATUS, NRF_STATUS_RX_DR);
|
nrf_write_register(NRF_REG_STATUS, NRF_STATUS_RX_DR);
|
||||||
|
|
||||||
/* Pairing: 5 bytes, magic @ offset 1. Data: >=6 bytes, id @ offset 0. */
|
/* Pairing: 5 bytes, magic @ offset 1. Data: >=6 bytes, id @ offset 0. */
|
||||||
if (width == ZH_RF_PAIRING_PACKET_SIZE && buf[1] == ZH_RF_PAIRING_REQ_MAGIC)
|
if (width == ZH_NRF24_PAIRING_PACKET_SIZE && buf[1] == ZH_NRF24_PAIRING_REQ_MAGIC)
|
||||||
{
|
{
|
||||||
handle_pairing_request(buf);
|
handle_pairing_request(buf);
|
||||||
}
|
}
|
||||||
@@ -491,7 +491,7 @@ esp_err_t zh_nrf_gateway_init(const zh_nrf_gateway_config_t *config)
|
|||||||
.sclk_io_num = s_ctx.cfg.pin_sck,
|
.sclk_io_num = s_ctx.cfg.pin_sck,
|
||||||
.quadwp_io_num = -1,
|
.quadwp_io_num = -1,
|
||||||
.quadhd_io_num = -1,
|
.quadhd_io_num = -1,
|
||||||
.max_transfer_sz = ZH_RF_MAX_PAYLOAD_SIZE,
|
.max_transfer_sz = ZH_NRF24_MAX_PAYLOAD_SIZE,
|
||||||
};
|
};
|
||||||
err = spi_bus_initialize(s_ctx.cfg.spi_host, &buscfg, SPI_DMA_CH_AUTO);
|
err = spi_bus_initialize(s_ctx.cfg.spi_host, &buscfg, SPI_DMA_CH_AUTO);
|
||||||
if (err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
Reference in new issue
Block a user