From de18ca99f50f9b8b35daafef6c2f2d1fecee19a5 Mon Sep 17 00:00:00 2001 From: dernasherbrezon Date: Tue, 6 Aug 2024 14:14:55 +0200 Subject: [PATCH] tune memory usage 1. disable SPI Cache 2. tune temp buffer size for incoming and outgoing packets Overall ~30% reduce in memory usage. Not that it was very high, but still. --- Kconfig | 11 +++++++++++ include/sx127x.h | 12 +++++++++++- src/sx127x.c | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 Kconfig diff --git a/Kconfig b/Kconfig new file mode 100644 index 0000000..8e84359 --- /dev/null +++ b/Kconfig @@ -0,0 +1,11 @@ +menu "sx127x" + config SX127X_DISABLE_SPI_CACHE + bool "Disable SPI cache" + help + Disable SPI cache to reduce memory footprint at a cost of longer SPI communication + config SX127X_MAX_PACKET_SIZE + int "Max packet size" + default 2047 + help + Expected max packet size. Used to initialize internal buffer. Can be fine-tuned to reduce memory footprint. +endmenu \ No newline at end of file diff --git a/include/sx127x.h b/include/sx127x.h index a0b9836..e922512 100644 --- a/include/sx127x.h +++ b/include/sx127x.h @@ -18,6 +18,10 @@ extern "C" { #endif +// optional standard esp-idf configuration +#ifdef IDF_VER +#include "sdkconfig.h" +#endif #include #include @@ -31,6 +35,10 @@ extern "C" { #define SX127X_ERR_NOT_FOUND 0x105 /*!< Requested resource not found */ #define SX127X_ERR_INVALID_VERSION 0x10A /*!< Version was invalid */ +#ifndef CONFIG_SX127X_MAX_PACKET_SIZE +#define CONFIG_SX127X_MAX_PACKET_SIZE MAX_PACKET_SIZE_FSK_FIXED +#endif + /* * This structure used to change mode */ @@ -311,8 +319,10 @@ typedef enum { */ typedef struct { void *spi_device; +#ifndef CONFIG_SX127X_DISABLE_SPI_CACHE uint8_t shadow_registers[MAX_NUMBER_OF_REGISTERS]; uint8_t shadow_registers_sync[MAX_NUMBER_OF_REGISTERS]; +#endif } shadow_spi_device_t; /** @@ -332,7 +342,7 @@ struct sx127x_t { void (*cad_callback)(sx127x *, int); - uint8_t packet[MAX_PACKET_SIZE_FSK_FIXED]; + uint8_t packet[CONFIG_SX127X_MAX_PACKET_SIZE]; uint16_t expected_packet_length; uint16_t fsk_ook_packet_sent_received; bool fsk_rssi_available; diff --git a/src/sx127x.c b/src/sx127x.c index 9ad66b5..ea4d66b 100644 --- a/src/sx127x.c +++ b/src/sx127x.c @@ -14,7 +14,6 @@ #include "sx127x.h" #include -#include #include #include @@ -177,6 +176,9 @@ typedef enum { } sx127x_header_mode_t; int sx127x_shadow_spi_read_registers(int reg, shadow_spi_device_t *spi_device, size_t data_length, uint32_t *result) { +#ifdef CONFIG_SX127X_DISABLE_SPI_CACHE + return sx127x_spi_read_registers(reg, spi_device->spi_device, data_length, result); +#else if (spi_device->shadow_registers_sync[reg] == SHADOW_IGNORE) { return sx127x_spi_read_registers(reg, spi_device->spi_device, data_length, result); } @@ -204,6 +206,7 @@ int sx127x_shadow_spi_read_registers(int reg, shadow_spi_device_t *spi_device, s memcpy(spi_device->shadow_registers + reg, pointer, data_length); memset(spi_device->shadow_registers_sync + reg, SHADOW_CACHED, data_length); return code; +#endif } int sx127x_shadow_spi_read_buffer(int reg, uint8_t *buffer, size_t buffer_length, shadow_spi_device_t *spi_device) { @@ -213,25 +216,35 @@ int sx127x_shadow_spi_read_buffer(int reg, uint8_t *buffer, size_t buffer_length int sx127x_shadow_spi_write_register(int reg, const uint8_t *data, size_t data_length, shadow_spi_device_t *spi_device) { int code = sx127x_spi_write_register(reg, data, data_length, spi_device->spi_device); +#ifndef CONFIG_SX127X_DISABLE_SPI_CACHE if (code != SX127X_OK || spi_device->shadow_registers_sync[reg] == SHADOW_IGNORE) { return code; } memcpy(spi_device->shadow_registers + reg, data, data_length); memset(spi_device->shadow_registers_sync + reg, SHADOW_CACHED, data_length); +#endif return code; } int sx127x_shadow_spi_write_buffer(int reg, const uint8_t *buffer, size_t buffer_length, shadow_spi_device_t *spi_device) { int code = sx127x_spi_write_buffer(reg, buffer, buffer_length, spi_device->spi_device); +#ifndef CONFIG_SX127X_DISABLE_SPI_CACHE if (code != SX127X_OK || spi_device->shadow_registers_sync[reg] == SHADOW_IGNORE) { return code; } memcpy(spi_device->shadow_registers + reg, buffer, buffer_length); memset(spi_device->shadow_registers_sync + reg, SHADOW_CACHED, buffer_length); +#endif return code; } int sx127x_read_register(int reg, shadow_spi_device_t *spi_device, uint8_t *result) { +#ifdef CONFIG_SX127X_DISABLE_SPI_CACHE + uint32_t value; + ERROR_CHECK(sx127x_spi_read_registers(reg, spi_device->spi_device, 1, &value)); + *result = (uint8_t) value; + return SX127X_OK; +#else if (spi_device->shadow_registers_sync[reg] == SHADOW_IGNORE) { uint32_t value; ERROR_CHECK(sx127x_spi_read_registers(reg, spi_device->spi_device, 1, &value)); @@ -248,6 +261,7 @@ int sx127x_read_register(int reg, shadow_spi_device_t *spi_device, uint8_t *resu spi_device->shadow_registers_sync[reg] = SHADOW_CACHED; spi_device->shadow_registers[reg] = *result; return SX127X_OK; +#endif } int sx127x_append_register(int reg, uint8_t value, uint8_t mask, shadow_spi_device_t *spi_device) { @@ -578,6 +592,7 @@ void sx127x_handle_interrupt(sx127x *device) { int sx127x_create(void *spi_device, sx127x *result) { memset(result, 0, sizeof(struct sx127x_t)); result->spi_device.spi_device = spi_device; +#ifndef CONFIG_SX127X_DISABLE_SPI_CACHE result->spi_device.shadow_registers_sync[REG_FIFO] = SHADOW_IGNORE; result->spi_device.shadow_registers_sync[REG_FIFO_RX_CURRENT_ADDR] = SHADOW_IGNORE; result->spi_device.shadow_registers_sync[REG_RSSI_VALUE_FSK] = SHADOW_IGNORE; @@ -592,6 +607,7 @@ int sx127x_create(void *spi_device, sx127x *result) { result->spi_device.shadow_registers_sync[REG_TEMP] = SHADOW_IGNORE; result->spi_device.shadow_registers_sync[REG_IRQ_FLAGS_1] = SHADOW_IGNORE; result->spi_device.shadow_registers_sync[REG_IRQ_FLAGS_2] = SHADOW_IGNORE; +#endif uint8_t version; int code = sx127x_read_register(REG_VERSION, &result->spi_device, &version);