Skip to content

Commit

Permalink
tune memory usage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dernasherbrezon committed Aug 6, 2024
1 parent cb0168e commit de18ca9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
@@ -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
12 changes: 11 additions & 1 deletion include/sx127x.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
extern "C" {
#endif

// optional standard esp-idf configuration
#ifdef IDF_VER
#include "sdkconfig.h"
#endif
#include <stdbool.h>
#include <stdint.h>

Expand All @@ -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
*/
Expand Down Expand Up @@ -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;

/**
Expand All @@ -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;
Expand Down
18 changes: 17 additions & 1 deletion src/sx127x.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "sx127x.h"

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sx127x_spi.h>

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit de18ca9

Please sign in to comment.