Skip to content

Commit

Permalink
Various changes to decrease RAM usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nebkat committed Nov 2, 2019
1 parent cc84823 commit 3391a56
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 48 deletions.
2 changes: 1 addition & 1 deletion main/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,5 @@ static void config_restart_task() {
void config_restart() {
uart_nmea("$PESP,CFG,RESTARTING");

xTaskCreate(config_restart_task, "config_restart_task", 512, NULL, TASK_PRIORITY_MAX, NULL);
xTaskCreate(config_restart_task, "config_restart_task", 4096, NULL, TASK_PRIORITY_MAX, NULL);
}
2 changes: 1 addition & 1 deletion main/include/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

ESP_EVENT_DECLARE_BASE(UART_EVENTS);

#define UART_BUFFER_SIZE 8192
#define UART_BUFFER_SIZE 4096

typedef struct uart_data {
int len;
Expand Down
2 changes: 1 addition & 1 deletion main/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static const char *TAG = "LOG";
static RingbufHandle_t ringbuf_handle;

esp_err_t log_init() {
ringbuf_handle = xRingbufferCreate(32768, RINGBUF_TYPE_BYTEBUF);
ringbuf_handle = xRingbufferCreate(4096, RINGBUF_TYPE_BYTEBUF);
if (ringbuf_handle == NULL) {
ESP_LOGE(TAG, "Could not create log ring buffer");
return ESP_FAIL;
Expand Down
24 changes: 23 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void sntp_time_set_handler(struct timeval *tv) {
void app_main()
{
status_led_init();
status_led_add(0xFFFFFF33, STATUS_LED_BLINK, 100, 1000, 0);
status_led_handle_t status_led = status_led_add(0xFFFFFF33, STATUS_LED_BLINK, 100, 1000, 0);

log_init();
esp_log_set_vprintf(log_vprintf);
Expand Down Expand Up @@ -100,6 +100,15 @@ void app_main()

web_server_init();

if (reset_reason != ESP_RST_POWERON && reset_reason != ESP_RST_SW) {
status_led->active = false;
status_led_handle_t error_led = status_led_add(0xFF000033, STATUS_LED_BLINK, 50, 10000, 0);

vTaskDelay(pdMS_TO_TICKS(10000));

status_led_remove(error_led);
}

ntrip_caster_init();
ntrip_server_init();
ntrip_client_init();
Expand All @@ -116,6 +125,19 @@ void app_main()
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
sntp_set_time_sync_notification_cb(sntp_time_set_handler);
sntp_init();

#ifdef DEBUG_HEAP
while (true) {
vTaskDelay(pdMS_TO_TICKS(2000));

multi_heap_info_t info;
heap_caps_get_info(&info, MALLOC_CAP_DEFAULT);

uart_nmea("$PESP,HEAP,FREE,%d/%d,%d%%", info.total_free_bytes,
info.total_allocated_bytes + info.total_free_bytes,
100 * info.total_free_bytes / (info.total_allocated_bytes + info.total_free_bytes));
}
#endif
}

static char *reset_reason_name(esp_reset_reason_t reason) {
Expand Down
2 changes: 1 addition & 1 deletion main/ntrip_caster.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,5 @@ static void ntrip_caster_task(void *ctx) {
void ntrip_caster_init() {
if (!config_get_bool1(CONF_ITEM(KEY_CONFIG_NTRIP_CASTER_ACTIVE))) return;

xTaskCreate(ntrip_caster_task, "ntrip_caster_task", 16384, NULL, TASK_PRIORITY_NTRIP, NULL);
xTaskCreate(ntrip_caster_task, "ntrip_caster_task", 4096, NULL, TASK_PRIORITY_NTRIP, NULL);
}
2 changes: 1 addition & 1 deletion main/ntrip_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ static void ntrip_client_task(void *ctx) {
void ntrip_client_init() {
if (!config_get_bool1(CONF_ITEM(KEY_CONFIG_NTRIP_CLIENT_ACTIVE))) return;

xTaskCreate(ntrip_client_task, "ntrip_client_task", 16384, NULL, TASK_PRIORITY_NTRIP, NULL);
xTaskCreate(ntrip_client_task, "ntrip_client_task", 4096, NULL, TASK_PRIORITY_NTRIP, NULL);
}
2 changes: 1 addition & 1 deletion main/ntrip_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ static void ntrip_server_task(void *ctx) {
void ntrip_server_init() {
if (!config_get_bool1(CONF_ITEM(KEY_CONFIG_NTRIP_SERVER_ACTIVE))) return;

xTaskCreate(ntrip_server_task, "ntrip_server_task", 16384, NULL, TASK_PRIORITY_NTRIP, NULL);
xTaskCreate(ntrip_server_task, "ntrip_server_task", 4096, NULL, TASK_PRIORITY_NTRIP, NULL);
}
2 changes: 1 addition & 1 deletion main/socket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ static void socket_client_task(void *ctx) {
void socket_client_init() {
if (!config_get_bool1(CONF_ITEM(KEY_CONFIG_SOCKET_CLIENT_ACTIVE))) return;

xTaskCreate(socket_client_task, "socket_client_task", 32768, NULL, 2, NULL);
xTaskCreate(socket_client_task, "socket_client_task", 4096, NULL, 2, NULL);
}
2 changes: 1 addition & 1 deletion main/socket_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,5 @@ static void socket_server_task(void *ctx) {
void socket_server_init() {
if (!config_get_bool1(CONF_ITEM(KEY_CONFIG_SOCKET_SERVER_ACTIVE))) return;

xTaskCreate(socket_server_task, "socket_server_task", 32768, NULL, 2, NULL);
xTaskCreate(socket_server_task, "socket_server_task", 4096, NULL, 2, NULL);
}
2 changes: 1 addition & 1 deletion main/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void uart_init() {
));
ESP_ERROR_CHECK(uart_driver_install(uart_port, UART_BUFFER_SIZE, UART_BUFFER_SIZE, 0, NULL, 0));

xTaskCreate(uart_task, "uart_task", 16384, NULL, TASK_PRIORITY_UART, NULL);
xTaskCreate(uart_task, "uart_task", 8192, NULL, TASK_PRIORITY_UART, NULL);
}

void uart_task(void *ctx) {
Expand Down
63 changes: 41 additions & 22 deletions main/web_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + CONFIG_SPIFFS_OBJ_NAME_LEN)

#define WWW_PARTITION_PATH "/www"
#define BUFFER_SIZE 8192
#define BUFFER_SIZE 2048

static const char *TAG = "WEB";

Expand Down Expand Up @@ -120,6 +120,27 @@ static char* get_path_from_uri(char *dest, const char *base_path, const char *ur
return dest + base_pathlen;
}

static esp_err_t json_response(httpd_req_t *req, cJSON *root) {
// Set mime type
esp_err_t err = httpd_resp_set_type(req, "application/json");
if (err != ESP_OK) return err;

// Convert to string
bool success = cJSON_PrintPreallocated(root, buffer, BUFFER_SIZE, false);
cJSON_Delete(root);
if (!success) {
ESP_LOGE(TAG, "Not enough space in buffer to output JSON");
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Not enough space in buffer to output JSON");
return ESP_FAIL;
}

// Send as response
err = httpd_resp_send(req, buffer, strlen(buffer));
if (err != ESP_OK) return err;

return ESP_OK;
}

static esp_err_t log_get_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "text/plain");

Expand Down Expand Up @@ -169,6 +190,23 @@ static esp_err_t core_dump_get_handler(httpd_req_t *req) {
return ESP_OK;
}

static esp_err_t heap_info_get_handler(httpd_req_t *req) {
multi_heap_info_t info;
heap_caps_get_info(&info, MALLOC_CAP_DEFAULT);

cJSON *root = cJSON_CreateObject();

cJSON_AddNumberToObject(root, "total_free_bytes", info.total_free_bytes);
cJSON_AddNumberToObject(root, "total_allocated_bytes", info.total_allocated_bytes);
cJSON_AddNumberToObject(root, "largest_free_block", info.largest_free_block);
cJSON_AddNumberToObject(root, "minimum_free_bytes", info.minimum_free_bytes);
cJSON_AddNumberToObject(root, "allocated_blocks", info.allocated_blocks);
cJSON_AddNumberToObject(root, "free_blocks", info.free_blocks);
cJSON_AddNumberToObject(root, "total_blocks", info.total_blocks);

return json_response(req, root);
}

static esp_err_t file_get_handler(httpd_req_t *req) {
char filepath[FILE_PATH_MAX];
FILE *fd = NULL;
Expand Down Expand Up @@ -233,32 +271,12 @@ static esp_err_t file_get_handler(httpd_req_t *req) {

/* Close file after sending complete */
fclose(fd);
ESP_LOGI(TAG, "File sending complete");

/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
return ESP_OK;
}

static esp_err_t json_response(httpd_req_t *req, cJSON *root) {
// Set mime type
esp_err_t err = httpd_resp_set_type(req, "application/json");
if (err != ESP_OK) return err;

// Convert to string
char *json = cJSON_Print(root);

// Send as response
err = httpd_resp_send(req, json, strlen(json));
if (err != ESP_OK) return err;

// Free resources
cJSON_Delete(root);
free(json);

return ESP_OK;
}

static esp_err_t config_get_handler(httpd_req_t *req) {
cJSON *root = cJSON_CreateObject();

Expand Down Expand Up @@ -494,7 +512,7 @@ static httpd_handle_t web_server_start(void)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.max_open_sockets = 2;
config.max_open_sockets = 1;
config.uri_match_fn = httpd_uri_match_wildcard;

// Start the httpd server
Expand All @@ -508,6 +526,7 @@ static httpd_handle_t web_server_start(void)

register_uri_handler(server, "/log", HTTP_GET, log_get_handler, NULL);
register_uri_handler(server, "/core_dump", HTTP_GET, core_dump_get_handler, NULL);
register_uri_handler(server, "/heap_info", HTTP_GET, heap_info_get_handler, NULL);

register_uri_handler(server, "/wifi/status", HTTP_GET, wifi_status_get_handler, NULL);
register_uri_handler(server, "/wifi/scan", HTTP_GET, wifi_scan_get_handler, NULL);
Expand Down
32 changes: 16 additions & 16 deletions sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,12 @@ CONFIG_LOG_DEFAULT_LEVEL=3
# CONFIG_LOG_COLORS is not set
# CONFIG_LOG_TIMESTAMP_SOURCE_RTOS is not set
CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM=y
CONFIG_LWIP_LOCAL_HOSTNAME="espressif"
CONFIG_LWIP_LOCAL_HOSTNAME="esp32xbee"
CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
# CONFIG_LWIP_L2_TO_L3_COPY is not set
# CONFIG_LWIP_IRAM_OPTIMIZATION is not set
CONFIG_LWIP_TIMERS_ONDEMAND=y
CONFIG_LWIP_MAX_SOCKETS=16
CONFIG_LWIP_MAX_SOCKETS=12
# CONFIG_LWIP_USE_ONLY_LWIP_SELECT is not set
CONFIG_LWIP_SO_REUSE=y
CONFIG_LWIP_SO_REUSE_RXTOALL=y
Expand All @@ -369,22 +369,22 @@ CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8
# CONFIG_LWIP_AUTOIP is not set
CONFIG_LWIP_NETIF_LOOPBACK=y
CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8
CONFIG_LWIP_MAX_ACTIVE_TCP=16
CONFIG_LWIP_MAX_LISTENING_TCP=16
CONFIG_LWIP_MAX_ACTIVE_TCP=12
CONFIG_LWIP_MAX_LISTENING_TCP=12
CONFIG_LWIP_TCP_MAXRTX=12
CONFIG_LWIP_TCP_SYNMAXRTX=6
CONFIG_LWIP_TCP_MSS=1436
CONFIG_LWIP_TCP_MSS=1460
CONFIG_LWIP_TCP_MSL=60000
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=32768
CONFIG_LWIP_TCP_WND_DEFAULT=32768
CONFIG_LWIP_TCP_RECVMBOX_SIZE=12
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=8192
CONFIG_LWIP_TCP_WND_DEFAULT=8192
CONFIG_LWIP_TCP_RECVMBOX_SIZE=6
CONFIG_LWIP_TCP_QUEUE_OOSEQ=y
# CONFIG_LWIP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES is not set
CONFIG_LWIP_TCP_OVERSIZE_MSS=y
# CONFIG_LWIP_TCP_OVERSIZE_QUARTER_MSS is not set
# CONFIG_LWIP_TCP_OVERSIZE_DISABLE is not set
CONFIG_LWIP_MAX_UDP_PCBS=16
CONFIG_LWIP_UDP_RECVMBOX_SIZE=32
CONFIG_LWIP_MAX_UDP_PCBS=12
CONFIG_LWIP_UDP_RECVMBOX_SIZE=6
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=3072
CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY=y
# CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU0 is not set
Expand All @@ -393,7 +393,7 @@ CONFIG_LWIP_TCPIP_TASK_AFFINITY=0x7FFFFFFF
# CONFIG_LWIP_PPP_SUPPORT is not set
# CONFIG_LWIP_MULTICAST_PING is not set
# CONFIG_LWIP_BROADCAST_PING is not set
CONFIG_LWIP_MAX_RAW_PCBS=16
CONFIG_LWIP_MAX_RAW_PCBS=8
CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1
CONFIG_LWIP_SNTP_UPDATE_DELAY=3600000
CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC=y
Expand Down Expand Up @@ -661,17 +661,17 @@ CONFIG_GARP_TMR_INTERVAL=60
CONFIG_TCPIP_RECVMBOX_SIZE=32
CONFIG_TCP_MAXRTX=12
CONFIG_TCP_SYNMAXRTX=6
CONFIG_TCP_MSS=1436
CONFIG_TCP_MSS=1460
CONFIG_TCP_MSL=60000
CONFIG_TCP_SND_BUF_DEFAULT=32768
CONFIG_TCP_WND_DEFAULT=32768
CONFIG_TCP_RECVMBOX_SIZE=12
CONFIG_TCP_SND_BUF_DEFAULT=8192
CONFIG_TCP_WND_DEFAULT=8192
CONFIG_TCP_RECVMBOX_SIZE=6
CONFIG_TCP_QUEUE_OOSEQ=y
# CONFIG_ESP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES is not set
CONFIG_TCP_OVERSIZE_MSS=y
# CONFIG_TCP_OVERSIZE_QUARTER_MSS is not set
# CONFIG_TCP_OVERSIZE_DISABLE is not set
CONFIG_UDP_RECVMBOX_SIZE=32
CONFIG_UDP_RECVMBOX_SIZE=6
CONFIG_TCPIP_TASK_STACK_SIZE=3072
CONFIG_TCPIP_TASK_AFFINITY_NO_AFFINITY=y
# CONFIG_TCPIP_TASK_AFFINITY_CPU0 is not set
Expand Down

0 comments on commit 3391a56

Please sign in to comment.