Skip to content

Commit 5dba719

Browse files
committed
refresh app + update Memfault SDK to 1.11.4
1 parent 26d2511 commit 5dba719

25 files changed

+1258
-236
lines changed

CMakeLists.txt

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,33 @@ if(DEFINED IDF_VERSION_MAJOR)
1515
endif()
1616
endif()
1717

18-
# Look for the Memfault SDK in a subdirectory first
19-
get_filename_component(memfault_firmare_sdk_dir third-party/memfault-firmware-sdk ABSOLUTE)
20-
if(NOT EXISTS ${memfault_firmare_sdk_dir})
21-
get_filename_component(memfault_firmare_sdk_dir ../../../../ ABSOLUTE)
18+
# Look for the Memfault SDK in a subdirectory first, when this app is used
19+
# standalone (not from within the Memfault SDK)
20+
get_filename_component(memfault_firmware_sdk_dir third-party/memfault-firmware-sdk ABSOLUTE)
21+
if(NOT EXISTS ${memfault_firmware_sdk_dir})
22+
get_filename_component(memfault_firmware_sdk_dir ../../../../ ABSOLUTE)
2223
endif()
23-
include(${memfault_firmare_sdk_dir}/ports/esp_idf/memfault.cmake)
24+
include(${memfault_firmware_sdk_dir}/ports/esp_idf/memfault.cmake)
2425

2526
# NOTE: This include also applies global compiler options, make sure
2627
# this happens first before defining other targets!
2728
# The esp-idf project() macro creates a project_name.elf target:
2829
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
30+
31+
# Wrap the mbedtls netif read/write functions, for instrumentation.
32+
# These MUST be before the project() function below, or they are ignored
33+
if(IDF_VERSION_MAJOR VERSION_GREATER_EQUAL 4)
34+
idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=mbedtls_net_send,--wrap=mbedtls_net_recv" APPEND)
35+
endif()
36+
2937
project(${PROJECT_NAME})
3038

39+
# Enable a compilation error if any deprecated APIs are used. This helps detect
40+
# bleeding edge changes in the ESP-IDF.
41+
if (IDF_VERSION_MAJOR VERSION_GREATER_EQUAL 5)
42+
idf_build_set_property(COMPILE_OPTIONS "-Werror=deprecated-declarations" APPEND)
43+
endif()
44+
3145
# Check for invalid partition table configurations
3246
if (
3347
CONFIG_APP_MEMFAULT_TRANSPORT_HTTP AND
@@ -50,16 +64,20 @@ if (INVALID_PARTITION_TABLE)
5064
)
5165
endif()
5266

53-
# Add the Memfault Build ID so each build can have a unique version.
54-
set(IDF_PROJECT_EXECUTABLE ${PROJECT_NAME}.elf)
55-
add_custom_command(TARGET ${IDF_PROJECT_EXECUTABLE}
56-
POST_BUILD
57-
# Compute and insert the build id
58-
COMMAND python ${memfault_firmare_sdk_dir}/scripts/fw_build_id.py ${IDF_PROJECT_EXECUTABLE}
59-
# Save a copy of the ELF that includes the 'log_fmt' section
60-
BYPRODUCTS ${IDF_PROJECT_EXECUTABLE}.memfault_log_fmt
61-
COMMAND ${CMAKE_COMMAND} -E copy ${IDF_PROJECT_EXECUTABLE} ${IDF_PROJECT_EXECUTABLE}.memfault_log_fmt
62-
COMMAND ${CMAKE_COMMAND} -E echo "*** NOTE: the symbol file to upload to app.memfault.com is ${IDF_PROJECT_EXECUTABLE}.memfault_log_fmt ***"
63-
# Remove the 'log_fmt' compact log section, which confuses elf2image
64-
COMMAND ${CMAKE_OBJCOPY} --remove-section log_fmt ${IDF_PROJECT_EXECUTABLE}
65-
)
67+
if (CONFIG_MEMFAULT)
68+
# Add the Memfault Build ID so each build can have a unique version.
69+
set(IDF_PROJECT_EXECUTABLE ${PROJECT_NAME}.elf)
70+
add_custom_command(TARGET ${IDF_PROJECT_EXECUTABLE}
71+
POST_BUILD
72+
# Compute and insert the build id
73+
COMMAND python ${memfault_firmware_sdk_dir}/scripts/fw_build_id.py ${IDF_PROJECT_EXECUTABLE}
74+
# Save a copy of the ELF that includes the 'log_fmt' section
75+
BYPRODUCTS ${IDF_PROJECT_EXECUTABLE}.memfault_log_fmt
76+
# Compress debug sections; this reduces the elf file size from ~10MB -> ~4.8MB
77+
COMMAND ${CMAKE_OBJCOPY} --compress-debug-sections ${IDF_PROJECT_EXECUTABLE}
78+
COMMAND ${CMAKE_COMMAND} -E copy ${IDF_PROJECT_EXECUTABLE} ${IDF_PROJECT_EXECUTABLE}.memfault_log_fmt
79+
COMMAND ${CMAKE_COMMAND} -E echo "*** NOTE: the symbol file to upload to app.memfault.com is ${IDF_PROJECT_EXECUTABLE}.memfault_log_fmt ***"
80+
# Remove the 'log_fmt' compact log section, which confuses elf2image
81+
COMMAND ${CMAKE_OBJCOPY} --remove-section log_fmt ${IDF_PROJECT_EXECUTABLE}
82+
)
83+
endif() # NOT CONFIG_MEMFAULT_DISABLE

main/CMakeLists.txt

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
list(APPEND
22
COMPONENT_SRCS
3-
cmd_app.c
4-
cmd_system.c
5-
console_example_main.c
3+
button.c
64
led.c
5+
main.c
76
)
87

9-
if (CONFIG_APP_MEMFAULT_TRANSPORT_HTTP)
10-
list(APPEND COMPONENT_SRCS app_memfault_transport_http.c)
11-
elseif(CONFIG_APP_MEMFAULT_TRANSPORT_MQTT)
12-
list(APPEND COMPONENT_SRCS app_memfault_transport_mqtt.c)
13-
endif()
14-
15-
# the 'cmd_wifi.c' implementation is different for ESP-IDF v5+
16-
if("${IDF_VERSION_MAJOR}" VERSION_GREATER_EQUAL 5)
8+
if (CONFIG_MEMFAULT)
179
list(APPEND
1810
COMPONENT_SRCS
19-
cmd_wifi.c
11+
cmd_app.c
12+
cmd_system.c
13+
ota_session_metrics.c
2014
)
21-
else()
22-
list(APPEND
23-
COMPONENT_SRCS
24-
cmd_wifi_legacy.c
25-
)
26-
endif()
2715

28-
# include settings.c only on idf >= 4
29-
if("${IDF_VERSION_MAJOR}" VERSION_GREATER_EQUAL 4)
30-
list(APPEND
31-
COMPONENT_SRCS
32-
settings.c
16+
if (CONFIG_APP_MEMFAULT_TRANSPORT_HTTP)
17+
list(APPEND COMPONENT_SRCS app_memfault_transport_http.c)
18+
elseif(CONFIG_APP_MEMFAULT_TRANSPORT_MQTT)
19+
list(APPEND COMPONENT_SRCS app_memfault_transport_mqtt.c)
20+
endif()
21+
22+
# the 'cmd_wifi.c' implementation is different for ESP-IDF v5+
23+
if("${IDF_VERSION_MAJOR}" VERSION_GREATER_EQUAL 5)
24+
list(APPEND
25+
COMPONENT_SRCS
26+
cmd_wifi.c
27+
)
28+
else()
29+
list(APPEND
30+
COMPONENT_SRCS
31+
cmd_wifi_legacy.c
32+
)
33+
endif()
34+
35+
# include settings.c only on idf >= 4
36+
if("${IDF_VERSION_MAJOR}" VERSION_GREATER_EQUAL 4)
37+
list(APPEND
38+
COMPONENT_SRCS
39+
settings.c
40+
)
41+
endif()
42+
43+
set(COMPONENT_ADD_INCLUDEDIRS
44+
.
45+
config
3346
)
3447
endif()
3548

36-
set(COMPONENT_ADD_INCLUDEDIRS
37-
.
38-
memfault
39-
)
40-
4149
register_component()

main/Kconfig.projbuild

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ menu "Memfault App Configuration"
22

33
config STORE_HISTORY
44
bool "Store command history in flash"
5-
default y
5+
default n
66
help
77
Linenoise line editing library provides functions to save and load
8-
command history. If this option is enabled, initalizes a FAT filesystem
8+
command history. If this option is enabled, initializes a FAT filesystem
99
and uses it to store command history.
10+
Note that this adds a ~22kB heap allocation on system boot.
1011

1112
config MEMFAULT_APP_OTA
1213
bool "Enable automatic periodic check+update for OTA"
1314
default y
15+
depends on MEMFAULT
1416
help
1517
Enables the Memfault OTA subsystem. This will periodically check for
1618
updates and apply them if available.
1719

1820
config MEMFAULT_APP_WIFI_AUTOJOIN
1921
bool "Enable automatic WiFi connection"
2022
default y
23+
depends on MEMFAULT
2124
help
2225
Automatically join if credentials are configured.
2326

@@ -30,6 +33,7 @@ choice APP_MEMFAULT_TRANSPORT
3033
config APP_MEMFAULT_TRANSPORT_MQTT
3134
bool "MQTT"
3235
select MQTT_PROTOCOL_5
36+
depends on !MEMFAULT_HTTP_PERIODIC_UPLOAD
3337
endchoice
3438

3539
if APP_MEMFAULT_TRANSPORT_MQTT
@@ -78,7 +82,22 @@ config BLINK_GPIO
7882
GPIO number (IOxx) to blink on and off or the RMT signal for the addressable LED.
7983
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink.
8084

81-
# esp32c3 specific settings
85+
config BUTTON_GPIO
86+
int "Input pin for the EN button on the dev board"
87+
# https://dl.espressif.com/dl/schematics/esp32_devkitc_v4_sch.pdf
88+
# https://dl.espressif.com/dl/schematics/esp-idf/SCH_ESP32-S2-DEVKITC-1_V1_20220817.pdf
89+
# https://dl.espressif.com/dl/schematics/SCH_ESP32-S3-DevKitC-1_V1.1_20221130.pdf
90+
default 0 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
91+
# https://dl.espressif.com/dl/schematics/SCH_ESP32-C3-DEVKITC-02_V1_1_20210126A.pdf
92+
# https://dl.espressif.com/dl/schematics/SCH_ESP32-C3-DEVKITM-1_V1_20200915A.pdf
93+
# https://docs.espressif.com/projects/esp-dev-kits/en/latest/_static/esp32-c6-devkitc-1/schematics/esp32-c6-devkitc-1-schematics_v1.4.pdf
94+
default 9 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C6
95+
default 0
96+
help
97+
GPIO pin number for the BOOT button. Used to trigger a crash for testing.
98+
99+
100+
# esp32c3/c6 specific settings
82101
if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C6
83102
# Default optimize for size due to increase in binary size
84103
# This can be changed in root -> Compiler Options

main/app_memfault_transport_http.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "app_memfault_transport.h"
77
#include "memfault/esp_port/http_client.h"
88

9-
void app_memfault_transport_init(void) {}
9+
void app_memfault_transport_init(void) { }
1010

1111
int app_memfault_transport_send_chunks(void) {
1212
return memfault_esp_port_http_client_post_data();

main/app_memfault_transport_mqtt.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ static esp_mqtt_client_handle_t s_mqtt_client = NULL;
2929
static esp_mqtt5_publish_property_config_t s_publish_property = {
3030
.topic_alias = 1,
3131
};
32-
static char s_topic_string[128] = {0};
32+
static char s_topic_string[128] = { 0 };
3333

34-
static uint8_t s_chunk_data[1024] = {0};
34+
static uint8_t s_chunk_data[1024] = { 0 };
3535

3636
static void mqtt_event_handler(MEMFAULT_UNUSED void *handler_args,
3737
MEMFAULT_UNUSED esp_event_base_t base,
@@ -59,7 +59,7 @@ static void prv_close_client(void) {
5959
if (rv) {
6060
ESP_LOGW(TAG, "Failed to destroy client[%d]", rv);
6161
}
62-
memfault_metrics_heartbeat_timer_stop(MEMFAULT_METRICS_KEY(mqtt_conn_uptime));
62+
MEMFAULT_METRIC_TIMER_STOP(mqtt_conn_uptime);
6363

6464
s_mqtt_client = NULL;
6565
}
@@ -91,14 +91,14 @@ static int prv_create_client(void) {
9191
rv = xSemaphoreTake(s_mqtt_connected, (1000 * 10) / portTICK_PERIOD_MS);
9292
if (rv != pdTRUE) {
9393
ESP_LOGE(TAG, "MQTT client failed to connect[%d]", rv);
94-
memfault_metrics_heartbeat_timer_start(MEMFAULT_METRICS_KEY(mqtt_conn_downtime));
94+
MEMFAULT_METRIC_TIMER_START(mqtt_conn_downtime);
9595
prv_close_client();
9696
return -1;
9797
}
9898

9999
// Update connection metrics when connected
100-
memfault_metrics_heartbeat_timer_stop(MEMFAULT_METRICS_KEY(mqtt_conn_downtime));
101-
memfault_metrics_heartbeat_timer_start(MEMFAULT_METRICS_KEY(mqtt_conn_uptime));
100+
MEMFAULT_METRIC_TIMER_STOP(mqtt_conn_downtime);
101+
MEMFAULT_METRIC_TIMER_START(mqtt_conn_uptime);
102102

103103
// Set topic alias before publishing
104104
rv = esp_mqtt5_client_set_publish_property(s_mqtt_client, &s_publish_property);
@@ -109,7 +109,7 @@ static int prv_create_client(void) {
109109
}
110110

111111
static const char *prv_get_device_serial(void) {
112-
sMemfaultDeviceInfo info = {0};
112+
sMemfaultDeviceInfo info = { 0 };
113113
memfault_platform_get_device_info(&info);
114114
return info.device_serial;
115115
}
@@ -161,8 +161,8 @@ int app_memfault_transport_send_chunks(void) {
161161
break;
162162
}
163163

164-
memfault_metrics_heartbeat_add(MEMFAULT_METRICS_KEY(mqtt_publish_bytes), chunk_size);
165-
memfault_metrics_heartbeat_add(MEMFAULT_METRICS_KEY(mqtt_publish_count), 1);
164+
MEMFAULT_METRIC_ADD(mqtt_publish_bytes, chunk_size);
165+
MEMFAULT_METRIC_ADD(mqtt_publish_count, 1);
166166
ESP_LOGD(TAG, "chunk[%d], len[%zu] published to %s", rv, chunk_size, s_topic_string);
167167
}
168168

main/button.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! @file
2+
//!
3+
//! Copyright (c) Memfault, Inc.
4+
//! See License.txt for details
5+
//!
6+
//! Button setup and handling
7+
8+
#include "button.h"
9+
10+
#include "driver/gpio.h"
11+
#include "esp_attr.h"
12+
#include "esp_log.h"
13+
#include "esp_system.h"
14+
15+
static const char *TAG = "button";
16+
17+
static void IRAM_ATTR prv_gpio_isr_handler(void *arg) {
18+
uint32_t gpio_num = (uint32_t)arg;
19+
20+
// dereference a null point to trigger a crash
21+
volatile uint32_t *ptr = NULL;
22+
*ptr = gpio_num;
23+
}
24+
25+
// The flex glitch filter is only available on 5.1. Skip it for earlier SDKs.
26+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
27+
#include "driver/gpio_filter.h"
28+
static void prv_button_glitch_filter_enable(void) {
29+
#if SOC_GPIO_FLEX_GLITCH_FILTER_NUM > 0
30+
gpio_glitch_filter_handle_t filter;
31+
gpio_flex_glitch_filter_config_t filter_cfg = {
32+
.clk_src = GLITCH_FILTER_CLK_SRC_DEFAULT,
33+
.gpio_num = CONFIG_BUTTON_GPIO,
34+
.window_thres_ns = 500,
35+
.window_width_ns = 500,
36+
};
37+
esp_err_t err = gpio_new_flex_glitch_filter(&filter_cfg, &filter);
38+
if (err != ESP_OK) {
39+
ESP_LOGE(TAG, "Failed to create glitch filter: %s", esp_err_to_name(err));
40+
return;
41+
}
42+
43+
err = gpio_glitch_filter_enable(filter);
44+
if (err != ESP_OK) {
45+
ESP_LOGE(TAG, "Failed to enable glitch filter: %s", esp_err_to_name(err));
46+
return;
47+
}
48+
#endif
49+
}
50+
#else // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
51+
static void prv_button_glitch_filter_enable(void) {
52+
// No-op
53+
}
54+
#endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
55+
56+
void button_setup(void) {
57+
// configure the button as an input
58+
gpio_config_t io_conf = {
59+
.intr_type = GPIO_INTR_NEGEDGE,
60+
.mode = GPIO_MODE_INPUT,
61+
.pin_bit_mask = 1ULL << CONFIG_BUTTON_GPIO,
62+
.pull_down_en = GPIO_PULLDOWN_DISABLE,
63+
.pull_up_en = GPIO_PULLUP_ENABLE,
64+
};
65+
esp_err_t err = gpio_config(&io_conf);
66+
if (err != ESP_OK) {
67+
ESP_LOGE(TAG, "Failed to configure button: %s", esp_err_to_name(err));
68+
return;
69+
}
70+
71+
prv_button_glitch_filter_enable();
72+
73+
// install gpio isr service
74+
err = gpio_install_isr_service(0);
75+
if (err != ESP_OK) {
76+
ESP_LOGE(TAG, "Failed to install gpio isr service: %s", esp_err_to_name(err));
77+
return;
78+
}
79+
80+
// install isr handler for specific gpio pin
81+
err = gpio_isr_handler_add(CONFIG_BUTTON_GPIO, prv_gpio_isr_handler, (void *)CONFIG_BUTTON_GPIO);
82+
if (err != ESP_OK) {
83+
ESP_LOGE(TAG, "Failed to add isr handler for button: %s", esp_err_to_name(err));
84+
return;
85+
}
86+
}

main/button.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! @file
2+
//!
3+
//! Copyright (c) Memfault, Inc.
4+
//! See License.txt for details
5+
//!
6+
//! Button setup and handling
7+
8+
#pragma once
9+
10+
void button_setup(void);

0 commit comments

Comments
 (0)