add sx127x nanopb send method
This commit is contained in:
parent
d837a9f40d
commit
541667b0f0
@ -1,7 +1,9 @@
|
|||||||
set(COMPONENT_SRCS
|
set(COMPONENT_SRCS
|
||||||
"sx127x_driver.c"
|
"sx127x_driver.c"
|
||||||
"sx127x_registers.c")
|
"sx127x_registers.c"
|
||||||
|
"sx127x_pb.c")
|
||||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||||
|
set(COMPONENT_REQUIRES "nanopb")
|
||||||
|
|
||||||
register_component()
|
register_component()
|
||||||
|
|
||||||
|
@ -38,4 +38,8 @@ config SX127X_SPI_DMA_CHAN
|
|||||||
There are two DMA channels that can be used by the ESP32 SPI driver. One
|
There are two DMA channels that can be used by the ESP32 SPI driver. One
|
||||||
which is not in use must be picked (1 or 2). Set to 0 to disable SPI DMA.
|
which is not in use must be picked (1 or 2). Set to 0 to disable SPI DMA.
|
||||||
|
|
||||||
|
config SX127X_USE_NANOPB
|
||||||
|
bool "Use nanopb"
|
||||||
|
default true
|
||||||
|
|
||||||
endmenu
|
endmenu
|
||||||
|
@ -7,11 +7,6 @@
|
|||||||
|
|
||||||
const char *SX127X_TAG = "sx127x";
|
const char *SX127X_TAG = "sx127x";
|
||||||
|
|
||||||
typedef struct sx127x_packet {
|
|
||||||
char * data;
|
|
||||||
size_t data_len;
|
|
||||||
} sx127x_packet_t;
|
|
||||||
|
|
||||||
sx127x_config_t sx127x_config_default() {
|
sx127x_config_t sx127x_config_default() {
|
||||||
sx127x_config_t config = SX127X_CONFIG_DEFAULT;
|
sx127x_config_t config = SX127X_CONFIG_DEFAULT;
|
||||||
return config;
|
return config;
|
||||||
|
@ -100,3 +100,5 @@ esp_err_t sx127x_recv_packet(sx127x_hndl hndl, sx127x_rx_packet_t *packet_out,
|
|||||||
QueueHandle_t sx127x_get_recv_queue(sx127x_hndl hndl);
|
QueueHandle_t sx127x_get_recv_queue(sx127x_hndl hndl);
|
||||||
|
|
||||||
void sx127x_packet_rx_free(sx127x_rx_packet_t *packet);
|
void sx127x_packet_rx_free(sx127x_rx_packet_t *packet);
|
||||||
|
|
||||||
|
#include "sx127x_pb.h"
|
||||||
|
@ -69,6 +69,11 @@ typedef enum sx127x_task_state {
|
|||||||
SX127X_TASK_STOPPING = 2,
|
SX127X_TASK_STOPPING = 2,
|
||||||
} sx127x_task_state_t;
|
} sx127x_task_state_t;
|
||||||
|
|
||||||
|
typedef struct sx127x_packet {
|
||||||
|
char * data;
|
||||||
|
size_t data_len;
|
||||||
|
} sx127x_packet_t;
|
||||||
|
|
||||||
typedef struct sx127x {
|
typedef struct sx127x {
|
||||||
sx127x_config_t config;
|
sx127x_config_t config;
|
||||||
spi_device_handle_t device_handle;
|
spi_device_handle_t device_handle;
|
||||||
|
36
components/sx127x_driver/sx127x_pb.c
Normal file
36
components/sx127x_driver/sx127x_pb.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include "sx127x_pb.h"
|
||||||
|
#include "sx127x_internal.h"
|
||||||
|
|
||||||
|
#include "sdkconfig.h"
|
||||||
|
|
||||||
|
#include <pb_encode.h>
|
||||||
|
|
||||||
|
#ifdef CONFIG_SX127X_USE_NANOPB
|
||||||
|
|
||||||
|
esp_err_t sx127x_send_packet_pb(sx127x_hndl hndl, const pb_field_t fields[],
|
||||||
|
void *src_struct, TickType_t ticks_to_wait) {
|
||||||
|
SX127X_CHECK(atomic_load(&hndl->task_state) == SX127X_TASK_RUNNING,
|
||||||
|
"task not running", ESP_ERR_INVALID_STATE);
|
||||||
|
|
||||||
|
size_t data_len;
|
||||||
|
bool pb_ret = pb_get_encoded_size(&data_len, fields, src_struct);
|
||||||
|
SX127X_CHECK(pb_ret == true, "pb_get_encoded_size fail", ESP_ERR_INVALID_ARG);
|
||||||
|
SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d",
|
||||||
|
ESP_ERR_INVALID_ARG, data_len);
|
||||||
|
|
||||||
|
pb_byte_t *data = heap_caps_malloc(
|
||||||
|
data_len, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT | MALLOC_CAP_DMA);
|
||||||
|
SX127X_CHECK(data != NULL, "malloc error", ESP_ERR_NO_MEM);
|
||||||
|
pb_ostream_t ostream = pb_ostream_from_buffer(data, data_len);
|
||||||
|
pb_encode(&ostream, fields, src_struct);
|
||||||
|
ESP_LOGV(SX127X_TAG, "encoded pb message %d bytes", data_len);
|
||||||
|
|
||||||
|
sx127x_packet_t packet;
|
||||||
|
packet.data_len = data_len;
|
||||||
|
packet.data = (char *)data;
|
||||||
|
BaseType_t pdRet = xQueueSend(hndl->tx_packet_queue, &packet, ticks_to_wait);
|
||||||
|
SX127X_CHECK(pdRet == pdTRUE, "tx queue full", ESP_ERR_TIMEOUT);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
14
components/sx127x_driver/sx127x_pb.h
Normal file
14
components/sx127x_driver/sx127x_pb.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "sdkconfig.h"
|
||||||
|
|
||||||
|
#ifdef CONFIG_SX127X_USE_NANOPB
|
||||||
|
|
||||||
|
#include "sx127x_driver.h"
|
||||||
|
|
||||||
|
#include <pb_common.h>
|
||||||
|
|
||||||
|
esp_err_t sx127x_send_packet_pb(sx127x_hndl hndl, const pb_field_t fields[],
|
||||||
|
void *src_struct, TickType_t ticks_to_wait);
|
||||||
|
|
||||||
|
#endif
|
@ -1,5 +1,7 @@
|
|||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package uas.ugv;
|
||||||
|
|
||||||
enum UGV_State {
|
enum UGV_State {
|
||||||
IDLE = 0;
|
IDLE = 0;
|
||||||
AQUIRING = 1;
|
AQUIRING = 1;
|
||||||
|
@ -2,10 +2,14 @@
|
|||||||
#include "ugv_config.h"
|
#include "ugv_config.h"
|
||||||
|
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
|
#include <pb_decode.h>
|
||||||
|
#include <pb_encode.h>
|
||||||
|
|
||||||
|
#include "messages.pb.h"
|
||||||
|
|
||||||
static const char *TAG = "ugv_comms";
|
static const char *TAG = "ugv_comms";
|
||||||
|
|
||||||
static void ugv_comms_task(void *arg);
|
static void ugv_comms_task(void *arg);
|
||||||
static uint16_t packet_num;
|
static uint16_t packet_num;
|
||||||
|
|
||||||
void ugv_comms_init() {
|
void ugv_comms_init() {
|
||||||
@ -40,15 +44,22 @@ void ugv_comms_init() {
|
|||||||
|
|
||||||
packet_num = 0;
|
packet_num = 0;
|
||||||
|
|
||||||
xTaskCreate(ugv_comms_task, "ugv_comms", 2 * 1024, NULL, 2, &ugv_comms_task_hndl);
|
xTaskCreate(ugv_comms_task, "ugv_comms", 2 * 1024, NULL, 2,
|
||||||
|
&ugv_comms_task_hndl);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ugv_comms_task(void *params) {
|
void ugv_comms_task(void *params) {
|
||||||
char tx_buf[20];
|
TickType_t send_period = pdMS_TO_TICKS(2000);
|
||||||
const size_t tx_buf_len = (sizeof(tx_buf) / sizeof(tx_buf[0]));
|
TickType_t current_time = xTaskGetTickCount();
|
||||||
TickType_t send_period = pdMS_TO_TICKS(2000);
|
TickType_t next_send = current_time + send_period;
|
||||||
TickType_t current_time = xTaskGetTickCount();
|
|
||||||
TickType_t next_send = current_time + send_period;
|
uas_ugv_UGV_Message ugv_message = uas_ugv_UGV_Message_init_default;
|
||||||
|
ugv_message.which_ugv_message = uas_ugv_UGV_Message_status_tag;
|
||||||
|
ugv_message.ugv_message.status.location.fix_quality = 0;
|
||||||
|
ugv_message.ugv_message.status.location.latitude = 43.65;
|
||||||
|
ugv_message.ugv_message.status.location.longitude = -116.20;
|
||||||
|
ugv_message.ugv_message.status.location.altitude = 2730;
|
||||||
|
ugv_message.ugv_message.status.state = uas_ugv_UGV_State_IDLE;
|
||||||
while (true) {
|
while (true) {
|
||||||
TickType_t delay_ticks = next_send - current_time;
|
TickType_t delay_ticks = next_send - current_time;
|
||||||
vTaskDelay(delay_ticks);
|
vTaskDelay(delay_ticks);
|
||||||
@ -56,19 +67,14 @@ void ugv_comms_task(void *params) {
|
|||||||
if (current_time < next_send) {
|
if (current_time < next_send) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int written_bytes =
|
|
||||||
snprintf(tx_buf, tx_buf_len, "hello world %d", packet_num);
|
|
||||||
if (written_bytes < 0) {
|
|
||||||
ESP_LOGE(TAG, "snprintf error: %d", written_bytes);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
packet_num++;
|
packet_num++;
|
||||||
esp_err_t ret = sx127x_send_packet(lora, tx_buf, written_bytes,
|
esp_err_t ret =
|
||||||
0); // 0 means error if queue full
|
sx127x_send_packet_pb(lora, uas_ugv_UGV_Message_fields, &ugv_message,
|
||||||
|
0); // 0 means error if queue full
|
||||||
if (ret != ESP_OK) {
|
if (ret != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "error sending packet: %d", ret);
|
ESP_LOGE(TAG, "error sending packet: %d", ret);
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "lora wrote %d bytes", written_bytes);
|
ESP_LOGI(TAG, "lora wrote UGV_Message packet");
|
||||||
}
|
}
|
||||||
|
|
||||||
current_time = xTaskGetTickCount();
|
current_time = xTaskGetTickCount();
|
||||||
|
@ -9,9 +9,6 @@
|
|||||||
#include "u8g2_esp32_hal.h"
|
#include "u8g2_esp32_hal.h"
|
||||||
#include "ugv_comms.h"
|
#include "ugv_comms.h"
|
||||||
#include "ugv_config.h"
|
#include "ugv_config.h"
|
||||||
#include "messages.pb.h"
|
|
||||||
|
|
||||||
#include <pb_decode.h>
|
|
||||||
|
|
||||||
static const char *TAG = "ugv_main";
|
static const char *TAG = "ugv_main";
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user