Browse Source

format everything and lots of work on sx127x driver

ugv_io
Alex Mikhalev 6 years ago
parent
commit
594c16ed41
  1. 118
      .clang-format
  2. 2
      main/CMakeLists.txt
  3. 437
      main/sx127x_driver/sx127x_driver.c
  4. 101
      main/sx127x_driver/sx127x_driver.h
  5. 116
      main/u8g2_esp32_hal.c
  6. 8
      main/u8g2_esp32_hal.h
  7. 109
      main/ugv_main.c

118
.clang-format

@ -0,0 +1,118 @@ @@ -0,0 +1,118 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

2
main/CMakeLists.txt

@ -6,3 +6,5 @@ set(COMPONENT_ADD_INCLUDEDIRS "." @@ -6,3 +6,5 @@ set(COMPONENT_ADD_INCLUDEDIRS "."
set(COMPONENT_REQUIRES "u8g2")
register_component()
component_compile_options("-Werror=incompatible-pointer-types")

437
main/sx127x_driver/sx127x_driver.c

@ -1,69 +1,151 @@ @@ -1,69 +1,151 @@
#include "sx127x_driver.h"
#include <string.h>
#include <freertos/task.h>
#include <esp_log.h>
#include <freertos/task.h>
#include <string.h>
static const char *TAG = "sx127x";
#define MAX_PKT_LENGTH 255
#define RESET_DELAY (pdMS_TO_TICKS(15))
// 8mhz
#define SPI_CLOCK_HZ (8 * 1000 * 1000)
#define TASK_STACK_SIZE (2 * 1024)
#define TASK_PRIORITY 3
#define REG_FIFO 0x00
#define REG_OP_MODE 0x01
#define REG_FRF_MSB 0x06
#define REG_FRF_MID 0x07
#define REG_FRF_LSB 0x08
#define REG_PA_CONFIG 0x09
#define REG_OCP 0x0b
#define REG_LNA 0x0c
#define REG_FIFO_ADDR_PTR 0x0d
#define REG_FIFO_TX_BASE_ADDR 0x0e
#define REG_FIFO_RX_BASE_ADDR 0x0f
#define REG_FIFO_RX_CURRENT_ADDR 0x10
#define REG_IRQ_FLAGS 0x12
#define REG_RX_NB_BYTES 0x13
#define REG_PKT_SNR_VALUE 0x19
#define REG_PKT_RSSI_VALUE 0x1a
#define REG_MODEM_CONFIG_1 0x1d
#define REG_MODEM_CONFIG_2 0x1e
#define REG_PREAMBLE_MSB 0x20
#define REG_PREAMBLE_LSB 0x21
#define REG_PAYLOAD_LENGTH 0x22
#define REG_MODEM_CONFIG_3 0x26
#define REG_FREQ_ERROR_MSB 0x28
#define REG_FREQ_ERROR_MID 0x29
#define REG_FREQ_ERROR_LSB 0x2a
#define REG_RSSI_WIDEBAND 0x2c
#define REG_DETECTION_OPTIMIZE 0x31
#define REG_INVERTIQ 0x33
#define REG_DETECTION_THRESHOLD 0x37
#define REG_SYNC_WORD 0x39
#define REG_INVERTIQ2 0x3b
#define REG_DIO_MAPPING_1 0x40
#define REG_VERSION 0x42
#define REG_PA_DAC 0x4d
// modes
#define MODE_LONG_RANGE_MODE 0x80
#define MODE_SLEEP 0x00
#define MODE_STDBY 0x01
#define MODE_TX 0x03
#define MODE_RX_CONTINUOUS 0x05
#define MODE_RX_SINGLE 0x06
const char *SX127X_TAG = "sx127x";
#define CONFIG2_CRC 0x04
#define CONFIG3_AUTO_AGC 0x04
// PA config
#define PA_BOOST 0x80
// IRQ masks
#define IRQ_TX_DONE_MASK 0x08
#define IRQ_PAYLOAD_CRC_ERROR_MASK 0x20
#define IRQ_RX_DONE_MASK 0x40
#ifdef NODEBUG
#define SX127X_CHECK(check, str, ret_val, ...) \
if (!(check)) \
{ \
ESP_LOGE(SX127X_TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
if (!(check)) { \
ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
return (ret_val); \
}
#ifdef NODEBUG
#define SX127X_ERROR_CHECK(ret, fun) \
#define SX127X_ERROR_CHECK(ret) \
{ \
esp_err_t _error_code = (ret); \
if (_error_code != ESP_OK) \
{ \
if (_error_code != ESP_OK) { \
return _error_code; \
} \
}
#define SX127X_ERROR_CHECK2(ret, fun) SX127X_CHECK(ret)
#else
#define SX127X_ERROR_CHECK(ret, fun) \
#define SX127X_CHECK(check, str, ret_val, ...) \
if (!(check)) { \
ESP_LOGE(TAG, "%s(%d): " #check ": " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
return (ret_val); \
}
#define SX127X_ERROR_CHECK(ret) \
{ \
esp_err_t _error_code = (ret); \
if (_error_code != ESP_OK) \
if (_error_code != ESP_OK) { \
const char *_error_name = esp_err_to_name(_error_code); \
ESP_LOGE(TAG, "%s(%d): %s (%d)", __FUNCTION__, __LINE__, _error_name, _error_code); \
return _error_code; \
} \
}
#define SX127X_ERROR_CHECK2(ret, fun) \
{ \
esp_err_t _error_code = (ret); \
if (_error_code != ESP_OK) { \
const char *_error_name = esp_err_to_name(_error_code); \
ESP_LOGE(SX127X_TAG, "%s(%d): " fun ": %s (%d)", \
__FUNCTION__, __LINE__, _error_name, _error_code); \
ESP_LOGE(TAG, "%s(%d): " #fun ": %s (%d)", __FUNCTION__, __LINE__, _error_name, _error_code); \
return _error_code; \
} \
}
#endif
esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t *handle)
{
struct sx127x {
sx127x_config_t config;
spi_device_handle_t device_handle;
TaskHandle_t task_handle;
bool task_running;
};
static esp_err_t sx127x_read_register(sx127x_t *handle, uint8_t reg, uint8_t *value);
static esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value);
static esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slave, uint8_t *from_slave);
static uint8_t sx127x_bw_to_reg(uint64_t bandwidth);
static uint64_t sx127x_reg_to_bw(uint8_t bandwidth_reg);
esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t **handle_ptr) {
esp_err_t ret;
sx127x_t *handle = malloc(sizeof(sx127x_t));
SX127X_CHECK(handle != NULL, "malloc error", ESP_ERR_NO_MEM);
handle->task_handle = NULL;
memcpy(&handle->config, config, sizeof(sx127x_config_t));
ret = gpio_set_direction(config->rst_io_num, GPIO_MODE_OUTPUT);
SX127X_ERROR_CHECK(ret, "gpio_set_direction")
ret = gpio_set_direction(config->irq_io_num, GPIO_MODE_OUTPUT);
SX127X_ERROR_CHECK(ret, "gpio_set_direction")
gpio_set_level(config->cs_io_num, 1);
SX127X_ERROR_CHECK2(ret, gpio_set_direction)
// perform reset
gpio_set_level(config->rst_io_num, 0);
vTaskDelay(pdMS_TO_TICKS(10));
vTaskDelay(RESET_DELAY);
gpio_set_level(config->rst_io_num, 1);
vTaskDelay(pdMS_TO_TICKS(10));
vTaskDelay(RESET_DELAY);
spi_bus_config_t bus_config = {
.mosi_io_num = config->mosi_io_num,
spi_bus_config_t bus_config = {.mosi_io_num = config->mosi_io_num,
.miso_io_num = config->miso_io_num,
.sclk_io_num = config->sck_io_num,
.quadhd_io_num = -1,
.quadwp_io_num = -1,
.max_transfer_sz = SX127X_MAX_TRANSFER};
ret = spi_bus_initialize(config->spi_host, &bus_config, 1);
SX127X_ERROR_CHECK(ret, "spi_bus_initialize")
SX127X_ERROR_CHECK2(ret, spi_bus_initialize)
spi_device_interface_config_t device_config = {
.command_bits = 0,
@ -71,9 +153,9 @@ esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t *handle) @@ -71,9 +153,9 @@ esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t *handle)
.dummy_bits = 0,
.mode = 0,
.duty_cycle_pos = 0,
.cs_ena_pretrans = 2,
.cs_ena_posttrans = 2,
.clock_speed_hz = 8, // 80mhz / 8 = 10mhz
.cs_ena_pretrans = 0,
.cs_ena_posttrans = 0,
.clock_speed_hz = SPI_CLOCK_HZ, // 8mhz
.input_delay_ns = 0,
.spics_io_num = config->cs_io_num,
.flags = 0,
@ -82,20 +164,305 @@ esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t *handle) @@ -82,20 +164,305 @@ esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t *handle)
.post_cb = NULL,
};
ret = spi_bus_add_device(config->spi_host, &device_config, &handle->device_handle);
SX127X_ERROR_CHECK(ret, "spi_bus_add_device")
SX127X_ERROR_CHECK2(ret, spi_bus_add_device)
// read version and check that it is compatible
uint8_t version;
ret = sx127x_read_register(handle, REG_VERSION, &version);
SX127X_ERROR_CHECK2(ret, sx127x_read_register);
SX127X_CHECK(version == 0x12, "unsupported version %#x", ESP_ERR_INVALID_VERSION, version);
ret = sx127x_sleep(handle);
SX127X_ERROR_CHECK(ret);
ret = sx127x_set_frequency(handle, config->frequency);
SX127X_ERROR_CHECK(ret);
ret = sx127x_write_register(handle, REG_FIFO_TX_BASE_ADDR, 0);
SX127X_ERROR_CHECK(ret);
sx127x_write_register(handle, REG_FIFO_RX_BASE_ADDR, 0);
SX127X_ERROR_CHECK(ret);
uint8_t reg_lna;
ret = sx127x_read_register(handle, REG_LNA, &reg_lna);
SX127X_ERROR_CHECK(ret);
reg_lna |= 0x03; // set LNA boost
ret = sx127x_write_register(handle, REG_LNA, reg_lna);
SX127X_ERROR_CHECK(ret);
// set auto AGC
ret = sx127x_write_register(handle, REG_MODEM_CONFIG_3, CONFIG3_AUTO_AGC);
SX127X_ERROR_CHECK(ret);
ret = sx127x_set_tx_power(handle, config->tx_power, true);
SX127X_ERROR_CHECK(ret);
ret = sx127x_set_spreading_factor(handle, config->spreading_factor);
SX127X_ERROR_CHECK(ret);
ret = sx127x_set_sync_word(handle, config->sync_word);
SX127X_ERROR_CHECK(ret);
ret = sx127x_set_crc(handle, config->crc);
SX127X_ERROR_CHECK(ret);
ret = sx127x_standby(handle);
ESP_ERROR_CHECK(ret);
*handle_ptr = handle;
return ESP_OK;
}
esp_err_t sx127x_free(sx127x_t *handle)
{
esp_err_t sx127x_free(sx127x_t *handle) {
esp_err_t ret;
if (handle->task_handle) {
ret = sx127x_stop(handle);
SX127X_ERROR_CHECK(ret);
}
ret = spi_bus_remove_device(handle->device_handle);
SX127X_ERROR_CHECK(ret, "spi_bus_remove_device")
SX127X_ERROR_CHECK2(ret, spi_bus_remove_device)
ret = spi_bus_free(handle->config.spi_host);
SX127X_ERROR_CHECK(ret, "spi_bus_free")
SX127X_ERROR_CHECK2(ret, spi_bus_free)
free(handle);
return ESP_OK;
}
static esp_err_t sx127x_read_register(sx127x_t *handle, uint8_t reg, uint8_t *value) {
return sx127x_single_transfer(handle, reg & 0x7f, 0x00, value);
}
static esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value) {
return sx127x_single_transfer(handle, reg | 0x80, value, NULL);
}
static esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slave, uint8_t *from_slave) {
spi_transaction_t trans;
memset(&trans, 0, sizeof(trans));
trans.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA;
trans.addr = addr;
trans.length = 8;
trans.rxlength = 8;
trans.tx_data[0] = to_slave;
esp_err_t ret = spi_device_transmit(handle->device_handle, &trans);
SX127X_ERROR_CHECK2(ret, spi_device_transmit);
if (from_slave) {
*from_slave = trans.rx_data[0];
}
ESP_LOGV(TAG, "sx127x_single_transfer(%#x, %#x, %#x)", addr, trans.tx_data[0], trans.rx_data[0]);
return ESP_OK;
}
esp_err_t sx127x_sleep(sx127x_t *handle) {
return sx127x_write_register(handle, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_SLEEP);
}
esp_err_t sx127x_standby(sx127x_t *handle) {
return sx127x_write_register(handle, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_STDBY);
}
esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency) {
uint64_t frf = ((uint64_t)frequency << 19) / 32000000;
esp_err_t ret;
ret = sx127x_write_register(handle, REG_FRF_MSB, (uint8_t)(frf));
SX127X_ERROR_CHECK(ret);
frf >>= 8;
ret = sx127x_write_register(handle, REG_FRF_MID, (uint8_t)(frf));
SX127X_ERROR_CHECK(ret);
frf >>= 8;
ret = sx127x_write_register(handle, REG_FRF_LSB, (uint8_t)(frf));
SX127X_ERROR_CHECK(ret);
handle->config.frequency = frequency;
return ESP_OK;
}
esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boost_t pa_boost) {
esp_err_t ret;
if (pa_boost == SX127X_PA_BOOST_ENABLED) {
// PA BOOST
SX127X_CHECK(tx_power >= 2 && tx_power <= 20, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power);
ret = sx127x_write_register(handle, REG_PA_CONFIG, PA_BOOST | (tx_power - 2));
} else {
// RFO
SX127X_CHECK(tx_power <= 14, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power);
ret = sx127x_write_register(handle, REG_PA_CONFIG, 0x70 | tx_power);
}
SX127X_ERROR_CHECK(ret);
handle->config.tx_power = tx_power;
return ESP_OK;
}
esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor) {
SX127X_CHECK(spreading_factor >= 6 && spreading_factor <= 12, "invalid spreading_factor", ESP_ERR_INVALID_ARG);
// section 4.1.1.2 in SX1276 datasheet
uint8_t detection_optimize, detection_threshold;
if (spreading_factor == 6) {
detection_optimize = 0xc5;
detection_threshold = 0x0c;
} else {
detection_optimize = 0xc3;
detection_threshold = 0x0a;
}
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_OPTIMIZE, detection_optimize));
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_THRESHOLD, detection_threshold));
uint8_t modem_config_3;
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3));
modem_config_3 = (modem_config_3 & 0x03) | ((spreading_factor << 4) & 0xf0);
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3));
handle->config.spreading_factor = spreading_factor;
return ESP_OK;
}
esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidth) {
uint8_t bw_reg = sx127x_bw_to_reg(signal_bandwidth);
uint8_t modem_config_1;
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_1, &modem_config_1));
modem_config_1 = (modem_config_1 & 0x0f) | (bw_reg << 4);
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_1, modem_config_1));
handle->config.signal_bandwidth = signal_bandwidth;
// set low data rate optimization flag
uint64_t bw = sx127x_reg_to_bw(bw_reg);
uint8_t sf = handle->config.spreading_factor;
// section 4.1.1.5
uint64_t symbol_duration_ms = 1000 / (bw / (1L << sf));
// section 4.1.1.6
bool must_have_ldo = (symbol_duration_ms > 16);
bool ldo = must_have_ldo || (handle->config.ldo == SX127X_LDO_ENABLED);
uint8_t modem_config_3;
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3));
if (ldo) {
modem_config_3 |= (1 << 3);
} else {
modem_config_3 &= ~(1 << 3);
}
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3));
return ESP_OK;
}
static uint8_t sx127x_bw_to_reg(uint64_t bandwidth) {
if (bandwidth <= 7.8E3) {
return 0;
} else if (bandwidth <= 10.4E3) {
return 1;
} else if (bandwidth <= 15.6E3) {
return 2;
} else if (bandwidth <= 20.8E3) {
return 3;
} else if (bandwidth <= 31.25E3) {
return 4;
} else if (bandwidth <= 41.7E3) {
return 5;
} else if (bandwidth <= 62.5E3) {
return 6;
} else if (bandwidth <= 125E3) {
return 7;
} else if (bandwidth <= 250E3) {
return 8;
} else /* if (bandwidth <= 500E3) */ {
return 9;
}
}
static uint64_t sx127x_reg_to_bw(uint8_t bandwidth_reg) {
switch (bandwidth_reg) {
case 0: return 7.8E3;
case 1: return 10.4E3;
case 2: return 15.6E3;
case 3: return 20.8E3;
case 4: return 31.25E3;
case 5: return 41.7E3;
case 6: return 62.5E3;
case 7: return 125E3;
case 8: return 250E3;
default:
case 9: return 500E3;
}
}
esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word) {
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_SYNC_WORD, sync_word));
handle->config.sync_word = sync_word;
return ESP_OK;
}
esp_err_t sx127x_set_crc(sx127x_t *handle, sx127x_crc_t crc) {
uint8_t modem_config_2;
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_2, &modem_config_2));
if (crc == SX127X_CRC_ENABLED) {
modem_config_2 |= CONFIG2_CRC;
} else {
modem_config_2 &= ~CONFIG2_CRC;
}
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_2, modem_config_2));
handle->config.crc = crc;
return ESP_OK;
}
void sx127x_isr(void *arg) {
sx127x_t *handle = (sx127x_t *)arg;
ESP_LOGI(TAG, "sx127x_isr");
}
void sx127x_task(void *arg) {
sx127x_t *handle = (sx127x_t *)arg;
while (handle->task_running) {
vTaskDelay(pdMS_TO_TICKS(10));
}
}
esp_err_t sx127x_start(sx127x_t *handle) {
esp_err_t ret;
SX127X_CHECK(handle->task_handle == NULL, "task already running", ESP_ERR_INVALID_STATE);
ret = sx127x_write_register(handle, REG_DIO_MAPPING_1, 0x00);
SX127X_ERROR_CHECK(ret);
gpio_config_t irq_io_config;
irq_io_config.intr_type = GPIO_INTR_POSEDGE;
irq_io_config.mode = GPIO_MODE_INPUT;
irq_io_config.pin_bit_mask = (1ULL << handle->config.irq_io_num);
irq_io_config.pull_down_en = 0;
irq_io_config.pull_up_en = 0;
ret = gpio_config(&irq_io_config);
SX127X_ERROR_CHECK2(ret, gpio_config)
ret = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1);
SX127X_ERROR_CHECK2(ret, gpio_install_isr_service);
ret = gpio_isr_handler_add(handle->config.irq_io_num, sx127x_isr, (void *)handle);
SX127X_ERROR_CHECK2(ret, gpio_isr_handler_add);
handle->task_running = true;
BaseType_t pdRet =
xTaskCreate(sx127x_task, "sx127x_task", TASK_STACK_SIZE, (void *)handle, TASK_PRIORITY, &handle->task_handle);
SX127X_CHECK(pdRet == pdPASS, "failed to create task", ESP_FAIL);
return ESP_OK;
}
esp_err_t sx127x_stop(sx127x_t *handle) {
esp_err_t ret;
SX127X_CHECK(handle->task_handle != NULL, "task has not been started", ESP_ERR_INVALID_STATE);
handle->task_running = false;
xTaskNotify(handle->task_handle, 0, eNoAction);
ret = gpio_isr_handler_remove(handle->config.irq_io_num);
SX127X_ERROR_CHECK2(ret, gpio_isr_handler_remove);
gpio_uninstall_isr_service();
return ESP_OK;
}

101
main/sx127x_driver/sx127x_driver.h

@ -1,25 +1,39 @@ @@ -1,25 +1,39 @@
#pragma once
#include "driver/spi_master.h"
#include <driver/spi_master.h>
#include <driver/gpio.h>
#include <stdint.h>
const char *SX127X_TAG;
#define SX127X_MAX_TRANSFER (256)
#define SX127X_MAX_TRANSFER (1024)
// carrier frequency type
typedef uint64_t sx127x_freq_t;
#define SX127X_CONFIG_DEFAULT \
sx127x_config_t \
{ \
.spi_host = VSPI_HOST, \
.mosi_io_num = 19, \
.miso_io_num = 27, \
.sck_io_num = 5, \
.cs_io_num = 18, \
.rst_io_num = 14, \
.irq_io_num = 26 \
}
#define SX127X_FREQ_433 ((sx127x_freq_t)433E6)
#define SX127X_FREQ_915 ((sx127x_freq_t)915E6)
// signal bandwidth type
typedef uint64_t sx127x_bw_t;
// spreading factor type
typedef uint8_t sx127x_sf_t;
typedef struct sx127x_config
{
typedef enum sx127x_pa_boost {
SX127X_PA_BOOST_DISABLED = 0,
SX127X_PA_BOOST_ENABLED = 1,
} sx127x_pa_boost_t;
typedef enum sx127x_crc {
SX127X_CRC_DISABLED = 0,
SX127X_CRC_ENABLED = 1,
} sx127x_crc_t;
// low data rate optimization
typedef enum sx127x_ldo {
SX127X_LDO_DISABLED = 0,
SX127X_LDO_ENABLED = 1,
} sx127x_ldo_t;
typedef struct sx127x_config {
spi_host_device_t spi_host;
gpio_num_t mosi_io_num;
gpio_num_t miso_io_num;
@ -27,14 +41,57 @@ typedef struct sx127x_config @@ -27,14 +41,57 @@ typedef struct sx127x_config
gpio_num_t cs_io_num;
gpio_num_t rst_io_num;
gpio_num_t irq_io_num;
uint8_t tx_power;
sx127x_freq_t frequency;
sx127x_bw_t signal_bandwidth;
sx127x_sf_t spreading_factor;
uint8_t sync_word;
sx127x_crc_t crc;
sx127x_ldo_t ldo;
} sx127x_config_t;
typedef struct sx127x
{
sx127x_config_t config;
spi_device_handle_t device_handle;
} sx127x_t;
// clang-format off
#define SX127X_CONFIG_DEFAULT \
{ \
.spi_host = VSPI_HOST, \
.mosi_io_num = 27, \
.miso_io_num = 19, \
.sck_io_num = 5, \
.cs_io_num = 18, \
.rst_io_num = 14, \
.irq_io_num = 26, \
.tx_power = 17, \
.frequency = SX127X_FREQ_433, \
.signal_bandwidth = 125E3, \
.spreading_factor = 11, \
.sync_word = 0x34, \
.crc = SX127X_CRC_ENABLED, \
.ldo = SX127X_LDO_DISABLED \
}
// clang-format on
typedef struct sx127x sx127x_t;
esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t *handle);
esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t **handle_ptr);
esp_err_t sx127x_free(sx127x_t *handle);
esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency);
esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boost_t pa_boost);
esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor);
esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidth);
esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word);
esp_err_t sx127x_set_crc(sx127x_t *handle, sx127x_crc_t crc);
esp_err_t sx127x_sleep(sx127x_t *handle);
esp_err_t sx127x_standby(sx127x_t *handle);
esp_err_t sx127x_start(sx127x_t *handle);
esp_err_t sx127x_stop(sx127x_t *handle);

116
main/u8g2_esp32_hal.c

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -18,11 +18,9 @@ static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data. @@ -18,11 +18,9 @@ static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
#undef ESP_ERROR_CHECK
#define ESP_ERROR_CHECK(x) \
do \
{ \
do { \
esp_err_t rc = (x); \
if (rc != ESP_OK) \
{ \
if (rc != ESP_OK) { \
ESP_LOGE("err", "esp_err_t = %d", rc); \
assert(0 && #x); \
} \
@ -31,33 +29,26 @@ static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data. @@ -31,33 +29,26 @@ static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
/*
* Initialze the ESP32 HAL.
*/
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param)
{
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) {
u8g2_esp32_hal = u8g2_esp32_hal_param;
} // u8g2_esp32_hal_init
/*
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
* to handle SPI communications.
* HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle SPI communications.
*/
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
ESP_LOGV(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
switch (msg)
{
switch (msg) {
case U8X8_MSG_BYTE_SET_DC:
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
}
break;
case U8X8_MSG_BYTE_INIT:
{
if (u8g2_esp32_hal.clk == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.mosi == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.cs == U8G2_ESP32_HAL_UNDEFINED)
{
case U8X8_MSG_BYTE_INIT: {
if (u8g2_esp32_hal.clk == U8G2_ESP32_HAL_UNDEFINED || u8g2_esp32_hal.mosi == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.cs == U8G2_ESP32_HAL_UNDEFINED) {
break;
}
@ -91,8 +82,7 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void @@ -91,8 +82,7 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
break;
}
case U8X8_MSG_BYTE_SEND:
{
case U8X8_MSG_BYTE_SEND: {
spi_transaction_t trans_desc;
trans_desc.addr = 0;
trans_desc.cmd = 0;
@ -110,33 +100,26 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void @@ -110,33 +100,26 @@ uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
return 0;
} // u8g2_esp32_spi_byte_cb
/*
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
* to handle I2C communications.
* HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle I2C communications.
*/
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
#define TXBUF_SIZE 32
static uint8_t txbuf[TXBUF_SIZE];
static uint8_t *txbuf_ptr;
// ESP_LOGV(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
switch (msg)
{
case U8X8_MSG_BYTE_SET_DC:
{
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
{
// ESP_LOGV(TAG, "i2c_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg,
// arg_int, arg_ptr);
switch (msg) {
case U8X8_MSG_BYTE_SET_DC: {
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
}
break;
}
case U8X8_MSG_BYTE_INIT:
{
if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED)
{
case U8X8_MSG_BYTE_INIT: {
if (u8g2_esp32_hal.sda == U8G2_ESP32_HAL_UNDEFINED || u8g2_esp32_hal.scl == U8G2_ESP32_HAL_UNDEFINED) {
break;
}
@ -153,12 +136,12 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void @@ -153,12 +136,12 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
ESP_LOGD(TAG, "i2c_param_config %d", conf.mode);
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
ESP_LOGD(TAG, "i2c_driver_install %d", I2C_MASTER_NUM);
ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
ESP_ERROR_CHECK(
i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
break;
}
case U8X8_MSG_BYTE_SEND:
{
case U8X8_MSG_BYTE_SEND: {
uint8_t *data_ptr = (uint8_t *)arg_ptr;
size_t data_len = (size_t)arg_int;
// ESP_LOGV(TAG, "U8x8_MSG_BYTE_SEND. txbuf len: %d", txbuf_ptr - txbuf);
@ -173,8 +156,7 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void @@ -173,8 +156,7 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
break;
}
case U8X8_MSG_BYTE_START_TRANSFER:
{
case U8X8_MSG_BYTE_START_TRANSFER: {
uint8_t i2c_address = u8x8_GetI2CAddress(u8x8);
handle_i2c = i2c_cmd_link_create();
// ESP_LOGV(TAG, "Start I2C transfer to %02X.", i2c_address >> 1);
@ -184,8 +166,7 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void @@ -184,8 +166,7 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
break;
}
case U8X8_MSG_BYTE_END_TRANSFER:
{
case U8X8_MSG_BYTE_END_TRANSFER: {
// ESP_LOGV(TAG, "End I2C transfer. txbuf len: %d", txbuf_ptr - txbuf);
// ESP_LOG_BUFFER_HEXDUMP(TAG, txbuf, txbuf_ptr - txbuf, ESP_LOG_VERBOSE);
ESP_ERROR_CHECK(i2c_master_write(handle_i2c, txbuf, txbuf_ptr - txbuf, ACK_CHECK_EN));
@ -199,35 +180,28 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void @@ -199,35 +180,28 @@ uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void
} // u8g2_esp32_i2c_byte_cb
/*
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
* to handle callbacks for GPIO and delay functions.
* HAL callback function as prescribed by the U8G2 library. This callback is
* invoked to handle callbacks for GPIO and delay functions.
*/
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
ESP_LOGV(TAG, "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
switch (msg)
{
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been
// specified then we define those pins as GPIO outputs.
case U8X8_MSG_GPIO_AND_DELAY_INIT:
{
switch (msg) {
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and
// RESET have been specified then we define those pins as GPIO outputs.
case U8X8_MSG_GPIO_AND_DELAY_INIT: {
uint64_t bitmask = 0;
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull << u8g2_esp32_hal.dc);
}
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull << u8g2_esp32_hal.reset);
}
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull << u8g2_esp32_hal.cs);
}
if (bitmask == 0)
{
if (bitmask == 0) {
break;
}
gpio_config_t gpioConfig;
@ -242,30 +216,26 @@ uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, @@ -242,30 +216,26 @@ uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int,
// Set the GPIO reset pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_RESET:
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.reset, arg_int);
}
break;
// Set the GPIO client select pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_CS:
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.cs, arg_int);
}
break;
// Set the Software I²C pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_I2C_CLOCK:
if (u8g2_esp32_hal.scl != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.scl != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.scl, arg_int);
// printf("%c",(arg_int==1?'C':'c'));
}
break;
// Set the Software I²C pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_I2C_DATA:
if (u8g2_esp32_hal.sda != U8G2_ESP32_HAL_UNDEFINED)
{
if (u8g2_esp32_hal.sda != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.sda, arg_int);
// printf("%c",(arg_int==1?'D':'d'));
}

8
main/u8g2_esp32_hal.h

@ -10,8 +10,8 @@ @@ -10,8 +10,8 @@
#include "u8g2.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "driver/i2c.h"
#include "driver/spi_master.h"
#define U8G2_ESP32_HAL_UNDEFINED (-1)
@ -22,8 +22,7 @@ @@ -22,8 +22,7 @@
#define ACK_CHECK_EN 0x1 // I2C master will check ack from slave
#define ACK_CHECK_DIS 0x0 // I2C master will not check ack from slave
typedef struct
{
typedef struct {
gpio_num_t clk;
gpio_num_t mosi;
gpio_num_t sda; // data for I²C
@ -35,7 +34,8 @@ typedef struct @@ -35,7 +34,8 @@ typedef struct
#define U8G2_ESP32_HAL_DEFAULT \
{ \
U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED \
U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, \
U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED \
}
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param);

109
main/ugv_main.c

@ -1,14 +1,12 @@ @@ -1,14 +1,12 @@
#define U8X8_USE_PINS
// #include <Arduino.h>
#include <u8g2.h>
// #include <SPI.h>
// #include <LoRa.h>
#include <driver/uart.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <esp_log.h>
#include <driver/uart.h>
#include <string.h>
#include <u8g2.h>
#include "sx127x_driver.h"
#include "u8g2_esp32_hal.h"
const char *TAG = "uas-ugv";
@ -31,8 +29,7 @@ u8g2_t u8g2; @@ -31,8 +29,7 @@ u8g2_t u8g2;
#define OLED_H 64
#define OLED_W 128
struct Packet
{
struct Packet {
int rssi;
double snr;
size_t buffer_len;
@ -45,13 +42,14 @@ void loraTask(void *params); @@ -45,13 +42,14 @@ void loraTask(void *params);
void loraOnReceive(int packetSize);
TaskHandle_t lora_task_hndl;
QueueHandle_t lora_packet_recv_queue; // packets recieved (type Packet)
QueueHandle_t lora_packet_isr_queue; // packet lengths from the recieve isr (type int)
// packets recieved (type Packet)
QueueHandle_t lora_packet_recv_queue;
// packet lengths from the recieve isr (type int)
QueueHandle_t lora_packet_isr_queue;
struct Packet packet;
void setup_serial(void)
{
void setup_serial(void) {
const uart_port_t uart_num = UART_NUM_2;
uart_config_t uart_config = {
.baud_rate = 115200,
@ -65,8 +63,7 @@ void setup_serial(void) @@ -65,8 +63,7 @@ void setup_serial(void)
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
}
void setup_oled(void)
{
void setup_oled(void) {
u8g2_esp32_hal_t u8g2_hal_config = {
.scl = 15,
.sda = 4,
@ -74,17 +71,13 @@ void setup_oled(void) @@ -74,17 +71,13 @@ void setup_oled(void)
};
u8g2_esp32_hal_init(u8g2_hal_config);
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2,
U8G2_R0,
u8g2_esp32_i2c_byte_cb,
u8g2_esp32_gpio_and_delay_cb);
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8g2_InitDisplay(&u8g2);
u8g2_ClearDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, false);
}
void setup(void)
{
void setup(void) {
setup_serial();
ESP_LOGI(TAG, "setup");
@ -96,24 +89,28 @@ void setup(void) @@ -96,24 +89,28 @@ void setup(void)
configASSERT(lora_packet_recv_queue != 0);
configASSERT(lora_packet_isr_queue != 0);
// lora_spi.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
// LoRa.setSPI(lora_spi);
// LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
// int res = LoRa.begin(LORA_FREQ); // 433MHz
// if (!res)
// {
ESP_LOGE(TAG, "LoRa init failed");
// }
// LoRa.setTxPower(17);
// LoRa.setSpreadingFactor(11);
// LoRa.setSignalBandwidth(125E3);
// LoRa.setSyncWord(0x34);
// LoRa.enableCrc();
sx127x_config_t lora_config = SX127X_CONFIG_DEFAULT;
lora_config.tx_power = 17;
lora_config.spreading_factor = 11;
lora_config.signal_bandwidth = 125E3;
lora_config.sync_word = 0x34;
lora_config.crc = SX127X_CRC_ENABLED;
sx127x_t* lora;
esp_err_t ret = sx127x_init(&lora_config, &lora);
if (ret != ESP_OK) {
const char *err_name = esp_err_to_name(ret);
ESP_LOGE(TAG, "LoRa init failed: %s", err_name);
} else {
ESP_LOGI(TAG, "LoRa initialized");
ret = sx127x_start(lora);
if (ret != ESP_OK) {
ESP_LOGI(TAG, "LoRa start failed: %d", ret);
}
}
// LoRa.onReceive(loraOnReceive);
// LoRa.receive(0);
// LoRa.dumpRegisters(Serial);
packet_num = 0;
xTaskCreate(loraTask, "loraTask", 1024 * 10, NULL, 2, &lora_task_hndl);
@ -122,16 +119,13 @@ void setup(void) @@ -122,16 +119,13 @@ void setup(void)
#define XO 10
void loraOnReceive(int packetSize)
{
if (packetSize == 0)
return;
void loraOnReceive(int packetSize) {
if (packetSize == 0) return;
ESP_LOGV(TAG, "loraOnReceive");
xQueueSendFromISR(lora_packet_isr_queue, &packetSize, NULL);
}
void loraTask(void *params)
{
void loraTask(void *params) {
char outBuf[20];
const size_t outBufLen = (sizeof(outBuf) / sizeof(uint8_t));
int packet_len;
@ -139,12 +133,10 @@ void loraTask(void *params) @@ -139,12 +133,10 @@ void loraTask(void *params)
TickType_t current_time = xTaskGetTickCount();
TickType_t next_send = current_time + send_period;
struct Packet recvd_packet;
while (true)
{
while (true) {
TickType_t delay_ticks = next_send - current_time;
BaseType_t didReceive = xQueueReceive(lora_packet_isr_queue, &packet_len, delay_ticks);
if (didReceive)
{
if (didReceive) {
int packetSize = (packet_len > LORA_BUF_LEN - 1) ? (LORA_BUF_LEN - 1) : (packet_len);
// LoRa.setTimeout(50);
// LoRa.readBytes(recvd_packet.buffer, packetSize);
@ -156,8 +148,7 @@ void loraTask(void *params) @@ -156,8 +148,7 @@ void loraTask(void *params)
xQueueSend(lora_packet_recv_queue, &recvd_packet, 10);
}
current_time = xTaskGetTickCount();
if (current_time >= next_send)
{
if (current_time >= next_send) {
sprintf(outBuf, "hello world %d", packet_num);
packet_num++;
// LoRa.beginPacket();
@ -172,13 +163,11 @@ void loraTask(void *params) @@ -172,13 +163,11 @@ void loraTask(void *params)
}
}
void loop(void)
{
void loop(void) {
ESP_LOGI(TAG, "loop");
u8g2_FirstPage(&u8g2);
bool recieved_packet = xQueueReceive(lora_packet_recv_queue, &packet, 10);
do
{
do {
u8g2_DrawRFrame(&u8g2, 0, 0, OLED_W, OLED_H, 4);
uint32_t free_heap = xPortGetFreeHeapSize();
@ -188,10 +177,9 @@ void loop(void) @@ -188,10 +177,9 @@ void loop(void)
snprintf(buf, 40, "free heap: %d", free_heap);
u8g2_DrawStr(&u8g2, 4, 8 + 8, buf);
if (packet.buffer_len)
{
ESP_LOGI(TAG, "lora received packet (len %d, rssi: %d, snr: %f): %s\n",
packet.buffer_len, packet.rssi, packet.snr, packet.buffer);
if (packet.buffer_len) {
ESP_LOGI(TAG, "lora received packet (len %d, rssi: %d, snr: %f): %s\n", packet.buffer_len, packet.rssi,
packet.snr, packet.buffer);
u8g2_SetFont(&u8g2, u8g2_font_4x6_mf);
snprintf(buf, 40, "lora pkt(rssi: %d, snr: %f)", packet.rssi, packet.snr);
u8g2_DrawStr(&u8g2, 4, 8 + 8 + 8, buf);
@ -202,16 +190,11 @@ void loop(void) @@ -202,16 +190,11 @@ void loop(void)
vTaskDelay(pdMS_TO_TICKS(1000));
}
void loopTask(void *pvUser)
{
void loopTask(void *pvUser) {
setup();
while (1)
{
while (1) {
loop();
}
}
void app_main()
{
xTaskCreatePinnedToCore(loopTask, "loopTask", 8192, NULL, 1, NULL, 1);
}
void app_main() { xTaskCreatePinnedToCore(loopTask, "loopTask", 8192, NULL, 1, NULL, 1); }

Loading…
Cancel
Save