From 49c2fb6629a685fbb5b94a128acc4a31925fcce6 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sun, 19 Oct 2025 11:59:40 -0400 Subject: [PATCH 01/43] solids project setup --- app/other/grim_reefer/CMakeLists.txt | 7 +++ app/other/grim_reefer/Kconfig | 1 + .../grim_reefer/boards/grim_reefer.overlay | 16 +++++++ app/other/grim_reefer/prj.conf | 35 +++++++++++++++ app/other/grim_reefer/sample.yaml | 9 ++++ app/other/grim_reefer/src/main.c | 43 +++++++++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 app/other/grim_reefer/CMakeLists.txt create mode 100644 app/other/grim_reefer/Kconfig create mode 100644 app/other/grim_reefer/boards/grim_reefer.overlay create mode 100644 app/other/grim_reefer/prj.conf create mode 100644 app/other/grim_reefer/sample.yaml create mode 100644 app/other/grim_reefer/src/main.c diff --git a/app/other/grim_reefer/CMakeLists.txt b/app/other/grim_reefer/CMakeLists.txt new file mode 100644 index 000000000..ce17aabdb --- /dev/null +++ b/app/other/grim_reefer/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(grim_reefer LANGUAGES C) + +FILE(GLOB sources src/*.c) +target_sources(app PRIVATE ${sources}) \ No newline at end of file diff --git a/app/other/grim_reefer/Kconfig b/app/other/grim_reefer/Kconfig new file mode 100644 index 000000000..0ffe17553 --- /dev/null +++ b/app/other/grim_reefer/Kconfig @@ -0,0 +1 @@ +source "Kconfig.zephyr" \ No newline at end of file diff --git a/app/other/grim_reefer/boards/grim_reefer.overlay b/app/other/grim_reefer/boards/grim_reefer.overlay new file mode 100644 index 000000000..fe858a015 --- /dev/null +++ b/app/other/grim_reefer/boards/grim_reefer.overlay @@ -0,0 +1,16 @@ +/delete-node/ &adc; + +&spi2 { + status = "okay"; + adc: mcp3561@0 { // new and cool driver + compatible = "microchip,mcp356xr"; + status = "okay"; + reg = <0>; + #io-channel-cells = <1>; + address = <1>; + analog-clock-prescaler = <0>; + boost-current-bias = <0>; + spi-max-frequency = ; + irq-gpios = <&gpioc 0 GPIO_ACTIVE_LOW>; + }; +}; \ No newline at end of file diff --git a/app/other/grim_reefer/prj.conf b/app/other/grim_reefer/prj.conf new file mode 100644 index 000000000..c2037cbde --- /dev/null +++ b/app/other/grim_reefer/prj.conf @@ -0,0 +1,35 @@ +CONFIG_LOG=y + +CONFIG_CRC=y + +CONFIG_SMF=y +CONFIG_EVENTS=y +CONFIG_BASE64=y + +CONFIG_GPIO=y +CONFIG_SPI=y +CONFIG_I2C=y + +CONFIG_ADC=y +# CONFIG_MCP356X=n # old driver +CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr +CONFIG_INA260=y + +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_CBPRINTF_FP_SUPPORT=y + +CONFIG_SPI_NOR_SFDP_RUNTIME=y +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_FLASH_PAGE_LAYOUT=y +CONFIG_DISK_ACCESS=y +CONFIG_FILE_SYSTEM=y +CONFIG_FILE_SYSTEM_LITTLEFS=y + +CONFIG_FS_LITTLEFS_FMP_DEV=y + +CONFIG_SHELL=y +CONFIG_FLASH_SHELL=y +CONFIG_SHELL_BACKEND_SERIAL=y +CONFIG_UART_CONSOLE=y \ No newline at end of file diff --git a/app/other/grim_reefer/sample.yaml b/app/other/grim_reefer/sample.yaml new file mode 100644 index 000000000..169ef60c1 --- /dev/null +++ b/app/other/grim_reefer/sample.yaml @@ -0,0 +1,9 @@ +sample: + description: + name: grim_reefer +common: + build_only: true + platform_allow: + - grim_reefer +tests: + solids_test.default: {} diff --git a/app/other/grim_reefer/src/main.c b/app/other/grim_reefer/src/main.c new file mode 100644 index 000000000..2d7412609 --- /dev/null +++ b/app/other/grim_reefer/src/main.c @@ -0,0 +1,43 @@ +/* + * :wa: + */ + +#include +#include +#include + +// 1000 msec = 1 sec +#define SLEEP_TIME_MS 1000 + +// blinky for debugging +#define LED0_NODE DT_ALIAS(led0) +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); + +#define ADC_NODE DT_ALIAS(adc) + +int main(void) +{ + int ret; + bool led_state = true; + + if (!gpio_is_ready_dt(&led)) { + return 0; + } + + ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); + if (ret < 0) { + return 0; + } + + while (1) { + ret = gpio_pin_toggle_dt(&led); + if (ret < 0) { + return 0; + } + + led_state = !led_state; + printf("LED state: %s\n", led_state ? "ON" : "OFF"); + k_msleep(SLEEP_TIME_MS); + } + return 0; +} From 4693d9f77f923a5305376ba43be76886be0f5204 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 01:16:49 -0400 Subject: [PATCH 02/43] solids test code builds, untested --- app/other/grim_reefer/CMakeLists.txt | 3 +- .../grim_reefer/boards/grim_reefer.overlay | 6 + app/other/grim_reefer/include/adc_reading.h | 16 ++ app/other/grim_reefer/include/calibration.h | 5 + app/other/grim_reefer/include/config.h | 15 ++ app/other/grim_reefer/include/control.h | 13 ++ app/other/grim_reefer/include/flash_storage.h | 12 ++ app/other/grim_reefer/src/adc_reading.c | 112 +++++++++++ app/other/grim_reefer/src/calibration.c | 2 + app/other/grim_reefer/src/control.c | 58 ++++++ app/other/grim_reefer/src/flash_storage.c | 182 ++++++++++++++++++ app/other/grim_reefer/src/main.c | 45 ++--- app/other/grim_reefer/src/shell_cmds.c | 41 ++++ 13 files changed, 477 insertions(+), 33 deletions(-) create mode 100644 app/other/grim_reefer/include/adc_reading.h create mode 100644 app/other/grim_reefer/include/calibration.h create mode 100644 app/other/grim_reefer/include/config.h create mode 100644 app/other/grim_reefer/include/control.h create mode 100644 app/other/grim_reefer/include/flash_storage.h create mode 100644 app/other/grim_reefer/src/adc_reading.c create mode 100644 app/other/grim_reefer/src/calibration.c create mode 100644 app/other/grim_reefer/src/control.c create mode 100644 app/other/grim_reefer/src/flash_storage.c create mode 100644 app/other/grim_reefer/src/shell_cmds.c diff --git a/app/other/grim_reefer/CMakeLists.txt b/app/other/grim_reefer/CMakeLists.txt index ce17aabdb..a3208d3f3 100644 --- a/app/other/grim_reefer/CMakeLists.txt +++ b/app/other/grim_reefer/CMakeLists.txt @@ -4,4 +4,5 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(grim_reefer LANGUAGES C) FILE(GLOB sources src/*.c) -target_sources(app PRIVATE ${sources}) \ No newline at end of file +target_sources(app PRIVATE ${sources}) +target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) \ No newline at end of file diff --git a/app/other/grim_reefer/boards/grim_reefer.overlay b/app/other/grim_reefer/boards/grim_reefer.overlay index fe858a015..46dd5dcc0 100644 --- a/app/other/grim_reefer/boards/grim_reefer.overlay +++ b/app/other/grim_reefer/boards/grim_reefer.overlay @@ -1,5 +1,11 @@ /delete-node/ &adc; +/ { + aliases { + adc0 = &adc; + }; +}; + &spi2 { status = "okay"; adc: mcp3561@0 { // new and cool driver diff --git a/app/other/grim_reefer/include/adc_reading.h b/app/other/grim_reefer/include/adc_reading.h new file mode 100644 index 000000000..696c772fd --- /dev/null +++ b/app/other/grim_reefer/include/adc_reading.h @@ -0,0 +1,16 @@ +#ifndef ADC_READING_H +#define ADC_READING_H + +#include + +struct adc_sample { + uint32_t timestamp; + uint32_t value; +}; + +void adc_init(); +void adc_reading_task(); +void adc_start_reading(); +void adc_stop_recording(); + +#endif // ADC_READING_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/calibration.h b/app/other/grim_reefer/include/calibration.h new file mode 100644 index 000000000..4fbda050c --- /dev/null +++ b/app/other/grim_reefer/include/calibration.h @@ -0,0 +1,5 @@ +#ifndef CALIBRATION_H +#define CALIBRATION_H + + +#endif // CALIBRATION_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/config.h b/app/other/grim_reefer/include/config.h new file mode 100644 index 000000000..e45cd1cf4 --- /dev/null +++ b/app/other/grim_reefer/include/config.h @@ -0,0 +1,15 @@ +#ifndef REEFER_INCLUDE_CONFIG_H +#define REEFER_INCLUDE_CONFIG_H + +#define SAMPLE_RATE_HZ 1000 +#define TEST_DURATION 10 +#define MAX_TESTS 30 + +#define STORAGE_THREAD_PRIORITY 1 +#define ADC_READ_PRIORITY -1 +#define THREAD_START_DELAY 100 + +#define ADC_BUFFER_SIZE (SAMPLE_RATE_HZ* TEST_DURATION_SEC) +#define FLASH_BLOCK_SIZE (ADC_BUFFER_SIZE* sizeof(uint32_t)) + +#endif // REEFER_INCLUDE_CONFIG_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/control.h b/app/other/grim_reefer/include/control.h new file mode 100644 index 000000000..3f9c3195c --- /dev/null +++ b/app/other/grim_reefer/include/control.h @@ -0,0 +1,13 @@ +#ifndef CONTROL_H +#define CONTROL_H + +#include +#include + +void control_init(); +void control_start_test(); +void control_stop_test(); +void control_dump_data(const struct shell *shell); +bool control_is_running(); + +#endif // CONTROL_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/flash_storage.h b/app/other/grim_reefer/include/flash_storage.h new file mode 100644 index 000000000..dc8b95d82 --- /dev/null +++ b/app/other/grim_reefer/include/flash_storage.h @@ -0,0 +1,12 @@ +#ifndef FLASH_STORAGE_H +#define FLASH_STORAGE_H + +#include + +int start_flash_storage(); +void stop_flash_storage(); +int flash_dump_all(const struct shell *shell); + +extern struct k_msgq storage_control_queue; + +#endif // FLASH_STORAGE_H \ No newline at end of file diff --git a/app/other/grim_reefer/src/adc_reading.c b/app/other/grim_reefer/src/adc_reading.c new file mode 100644 index 000000000..118df7e55 --- /dev/null +++ b/app/other/grim_reefer/src/adc_reading.c @@ -0,0 +1,112 @@ +#include "adc_reading.h" +#include "config.h" +#include "flash_storage.h" + +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(adc_reader, LOG_LEVEL_INF); + +#define ADC_NODE DT_ALIAS(adc0) + +#if !DT_NODE_HAS_STATUS(ADC_NODE, okay) +#error "No ADC node found" +#endif + +static const struct device *adc_dev = DEVICE_DT_GET(ADC_NODE); + +extern struct k_msgq adc_data_queue; + +#define BEGIN_READING_EVENT 1 +#define STOP_READING_EVENT 2 + +static K_EVENT_DEFINE(adc_control_event); +static K_TIMER_DEFINE(adc_timer, NULL, NULL); + +void adc_reading_task(void); + +K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, ADC_READ_PRIORITY, 0, THREAD_START_DELAY); + +static uint32_t adc_buffer; +static struct adc_sequence sequence = { + .buffer = &adc_buffer, + .buffer_size = sizeof(adc_buffer), + .resolution = 24 +}; + +static struct adc_channel_cfg channel_cfg = { + .gain = ADC_GAIN_1, + .reference = ADC_REF_INTERNAL, + .acquisition_time = ADC_ACQ_TIME_DEFAULT, + .channel_id = 0, +}; + +void adc_init(){ + if(!device_is_ready(adc_dev)){ + LOG_ERR("ADC device not ready"); + return; + } + + int ret = adc_channel_setup(adc_dev, &channel_cfg); + if(ret < 0){ + LOG_ERR("Failed to configure ADC channel (%d)", ret); + return; + } + + LOG_INF("ADC initialized"); +} + +void adc_reading_task(){ + int ret; + uint32_t adc_val = 0; + struct adc_sample sample = {0}; + + LOG_INF("ADC reading task waiting to start..."); + + // Wait for start event + k_event_wait(&adc_control_event, BEGIN_READING_EVENT, false, K_FOREVER); + + LOG_INF("ADC reading started"); + + k_timer_start(&adc_timer, K_MSEC(1), K_MSEC(1)); // 1kHz loop + + while(true){ + uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); + if(events & STOP_READING_EVENT){ + LOG_INF("ADC reading stopped"); + break; + } + + k_timer_status_sync(&adc_timer); + + sequence.buffer = &adc_val; + sequence.buffer_size = sizeof(adc_val); + + // Read from ADC + ret = adc_read(adc_dev, &sequence); + if(ret < 0){ + LOG_ERR("ADC read failed (%d)", ret); + continue; + } + + sample.timestamp = k_uptime_get_32(); + sample.value = adc_val; + + k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); + } + + k_timer_stop(&adc_timer); +} + +void adc_start_reading(){ + k_event_set(&adc_control_event, BEGIN_READING_EVENT); + LOG_INF("ADC reading started"); +} + +void adc_stop_recording(){ + k_event_set(&adc_control_event, STOP_READING_EVENT); + LOG_INF("ADC reading stopped"); +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/calibration.c b/app/other/grim_reefer/src/calibration.c new file mode 100644 index 000000000..b12f85166 --- /dev/null +++ b/app/other/grim_reefer/src/calibration.c @@ -0,0 +1,2 @@ +#include "calibration.h" +#include "config.h" \ No newline at end of file diff --git a/app/other/grim_reefer/src/control.c b/app/other/grim_reefer/src/control.c new file mode 100644 index 000000000..2d6cefa63 --- /dev/null +++ b/app/other/grim_reefer/src/control.c @@ -0,0 +1,58 @@ +#include "control.h" +#include "config.h" +#include "adc_reading.h" +#include "flash_storage.h" + +#include +#include +#include +#include + +LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); + +static int test_number = 0; +static bool test_running = false; + +void control_init(){ + LOG_INF("Initializing..."); + adc_init(); +} + +void control_start_test(){ + if(test_running){ + LOG_WRN("Test already running"); + return; + } + + LOG_INF("Starting test run #%d", test_number + 1); + test_running = true; + + start_flash_storage(); + adc_start_reading(); + + test_number++; + + // Run test for 10 seconds + k_sleep(K_SECONDS(TEST_DURATION)); + control_stop_test(); +} + +void control_stop_test(){ + if(!test_running){ + LOG_WRN("No test running"); + return; + } + + LOG_INF("Stopping test..."); + adc_stop_recording(); + stop_flash_storage(); + test_running = false; +} + +void control_dump_data(const struct shell *shell){ + flash_dump_all(shell); +} + +bool control_is_running(){ + return test_running; +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/flash_storage.c b/app/other/grim_reefer/src/flash_storage.c new file mode 100644 index 000000000..9f8b59033 --- /dev/null +++ b/app/other/grim_reefer/src/flash_storage.c @@ -0,0 +1,182 @@ +#include "flash_storage.h" +#include "config.h" +#include "adc_reading.h" + +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(flash_storage, LOG_LEVEL_INF); + +#define SPI_FLASH_ADDR 0x00000000 +#define FLASH_METADATA_ADDR SPI_FLASH_ADDR +#define SPI_FLASH_BLOCK_SIZE (64 * 1024) // 64KB +#define FLASH_METADATA_SIZE (4 * 1024) // 4KB +#define SPI_FLASH_START_ADDR (FLASH_METADATA_ADDR + FLASH_METADATA_SIZE) + +enum storage_event {BEGIN_STORAGE, END_STORAGE}; + +K_MSGQ_DEFINE(storage_control_queue, sizeof(enum storage_event), 5, 1); + +K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), SAMPLE_RATE_HZ * TEST_DURATION, 4); + +static const struct device *flash_dev = DEVICE_DT_GET(DT_ALIAS(storage)); +static uint32_t current_test_number = 0; +static off_t current_write_addr = 0; + +static void flash_storage_thread_entry(void); + +K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, + STORAGE_THREAD_PRIORITY, 0, 1000); + +// Check if flash block is all 0xFF +static bool flash_block_is_empty(off_t addr){ + uint8_t buf[16]; + if(flash_read(flash_dev, addr, buf, sizeof(buf)) < 0){ + LOG_ERR("Flash read failed"); + return false; + } + + for(int i = 0; i < sizeof(buf); i++){ + if(buf[i] != 0xFF){ + return false; + } + } + return true; +} + +// Get and update current test number +static void load_metadata(){ + uint8_t buf[4]; + if(flash_read(flash_dev, FLASH_METADATA_ADDR, buf, sizeof(buf)) < 0){ + LOG_ERR("Failed to read metadata"); + current_test_number = 0; + return; + } + + // If all are erased (0xFF), assume no tests yet + if(buf[0] == 0xFF && buf[1] == 0xFF && buf[2] == 0xFF && buf[3] == 0xFF){ + current_test_number = 0; + } else { + memcpy(¤t_test_number, buf, sizeof(current_test_number)); + } + + LOG_INF("Loaded test number %d", current_test_number); +} + +static void save_metadata(){ + int ret; + ret = flash_erase(flash_dev, FLASH_METADATA_ADDR, FLASH_METADATA_SIZE); + + if(ret < 0){ + LOG_ERR("flash_erase(metadata) failed: %d", ret); + return; + } + + ret = flash_write(flash_dev, FLASH_METADATA_ADDR, ¤t_test_number, sizeof(current_test_number)); + + if(ret < 0){ + LOG_ERR("flash_write(metadata) failed: %d", ret); + } else { + LOG_INF("Saved current_test_number = %u", current_test_number); + } +} + +static off_t get_test_block_addr(uint32_t test_index){ + return (off_t)(SPI_FLASH_START_ADDR + (test_index * SPI_FLASH_BLOCK_SIZE)); +} + +static void flash_storage_thread_entry(){ + struct adc_sample sample; + enum storage_event event; + + if(!device_is_ready(flash_dev)){ + LOG_ERR("Flash device not ready"); + return; + } + + // Get current test number + load_metadata(); + + while(1){ + // Wait for start command + k_msgq_get(&storage_control_queue, &event, K_FOREVER); + if (event != BEGIN_STORAGE) continue; + + if(current_test_number >= MAX_TESTS){ + LOG_ERR("Maximum number of test reached"); + continue; + } + + off_t test_block_addr = get_test_block_addr(current_test_number); + LOG_INF("Starting test %d at addr 0x%08x", current_test_number, (long)test_block_addr); + + // Erase block + int ret = flash_erase(flash_dev, test_block_addr, SPI_FLASH_BLOCK_SIZE); + if(ret < 0){ + LOG_ERR("flash_erase(test block) failed: %d", ret); + continue; + } + + current_write_addr = test_block_addr; + + while(1){ + if(k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE){ + LOG_INF("Test %d complete", current_test_number); + current_test_number++; + save_metadata(); + break; + } + + if(k_msgq_get(&adc_data_queue, &sample, K_MSEC(50)) == 0){ + int ret = flash_write(flash_dev, current_write_addr, &sample, sizeof(sample)); + if(ret < 0){ + LOG_ERR("Flash write failed (%d)", ret); + } else { + current_write_addr += sizeof(sample); + } + } + } + } +} + +int start_flash_storage(){ + enum storage_event event = BEGIN_STORAGE; + return k_msgq_put(&storage_control_queue, &event, K_FOREVER); +} + +void stop_flash_storage(){ + enum storage_event event = END_STORAGE; + k_msgq_put(&storage_control_queue, &event, K_FOREVER); +} + +int flash_dump_all(const struct shell *shell){ + struct adc_sample sample; + uint32_t test_index = 0; + + if(!device_is_ready(flash_dev)){ + shell_error(shell, "Flash not ready"); + return -1; + } + + for(test_index = 0; test_index < current_test_number; test_index++){ + off_t block_addr = get_test_block_addr(test_index); + if (flash_block_is_empty(block_addr)) continue; + + shell_print(shell, "Dumping Test %d:", test_index); + off_t addr = block_addr; + + for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ + if (flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0) break; + if (sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF) break; + shell_print(shell, "%u,%u", sample.timestamp, sample.value); + addr += sizeof(sample); + } + } + + return 0; +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/main.c b/app/other/grim_reefer/src/main.c index 2d7412609..ad389686e 100644 --- a/app/other/grim_reefer/src/main.c +++ b/app/other/grim_reefer/src/main.c @@ -1,43 +1,24 @@ -/* - * :wa: - */ +#include "control.h" -#include #include -#include +#include +#include +#include -// 1000 msec = 1 sec -#define SLEEP_TIME_MS 1000 - -// blinky for debugging -#define LED0_NODE DT_ALIAS(led0) -static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); - -#define ADC_NODE DT_ALIAS(adc) +LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); int main(void) { - int ret; - bool led_state = true; + LOG_INF("Solids Test Start"); - if (!gpio_is_ready_dt(&led)) { - return 0; - } + control_init(); - ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); - if (ret < 0) { - return 0; - } + LOG_INF("Use 'test start' to begin test"); + LOG_INF("Commands: test start | test stop | test dump"); - while (1) { - ret = gpio_pin_toggle_dt(&led); - if (ret < 0) { - return 0; - } + while(1){ + k_sleep(K_MSEC(10)); + } - led_state = !led_state; - printf("LED state: %s\n", led_state ? "ON" : "OFF"); - k_msleep(SLEEP_TIME_MS); - } return 0; -} +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/shell_cmds.c b/app/other/grim_reefer/src/shell_cmds.c new file mode 100644 index 000000000..aa1b55460 --- /dev/null +++ b/app/other/grim_reefer/src/shell_cmds.c @@ -0,0 +1,41 @@ +#include "control.h" +#include "flash_storage.h" +#include "adc_reading.h" + +#include +#include + +LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); + +static int cmd_test_start(const struct shell *shell, size_t argc, char **argv){ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Starting test..."); + control_start_test(); + return 0; +} + +static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv){ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Stopping test..."); + control_stop_test(); + return 0; +} + +static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv){ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Dumping stored test data..."); + control_dump_data(shell); + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, + SHELL_CMD(start, NULL, "Start test", cmd_test_start), + SHELL_CMD(stop, NULL, "Stop test", cmd_test_stop), + SHELL_CMD(dump, NULL, "Dump all flash data", cmd_test_dump), + SHELL_SUBCMD_SET_END +); + +SHELL_CMD_REGISTER(test, &sub_test, "Solids Test Board control commands", NULL); \ No newline at end of file From 94a8979480dca3c2d82ed861732e545369633080 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 17:31:54 -0400 Subject: [PATCH 03/43] new overlay and adc channel cfg --- .../grim_reefer/boards/grim_reefer.overlay | 21 ++++++++++++ app/other/grim_reefer/src/adc_reading.c | 33 ++++++++++++------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/other/grim_reefer/boards/grim_reefer.overlay b/app/other/grim_reefer/boards/grim_reefer.overlay index 46dd5dcc0..16739018a 100644 --- a/app/other/grim_reefer/boards/grim_reefer.overlay +++ b/app/other/grim_reefer/boards/grim_reefer.overlay @@ -1,6 +1,10 @@ /delete-node/ &adc; / { + zephyr,user { + io-channels = <&adc 0>, <&adc 0>; + }; + aliases { adc0 = &adc; }; @@ -13,10 +17,27 @@ status = "okay"; reg = <0>; #io-channel-cells = <1>; + #address-cells = <1>; + #size-cells = <0>; address = <1>; analog-clock-prescaler = <0>; boost-current-bias = <0>; spi-max-frequency = ; irq-gpios = <&gpioc 0 GPIO_ACTIVE_LOW>; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_8"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + + zephyr,resolution = <24>; + + zephyr,differential; + + zephyr,input-positive = <0x1>; + zephyr,input-negative = <0x0>; + + }; }; }; \ No newline at end of file diff --git a/app/other/grim_reefer/src/adc_reading.c b/app/other/grim_reefer/src/adc_reading.c index 118df7e55..12535cfaf 100644 --- a/app/other/grim_reefer/src/adc_reading.c +++ b/app/other/grim_reefer/src/adc_reading.c @@ -37,25 +37,34 @@ static struct adc_sequence sequence = { .resolution = 24 }; -static struct adc_channel_cfg channel_cfg = { - .gain = ADC_GAIN_1, - .reference = ADC_REF_INTERNAL, - .acquisition_time = ADC_ACQ_TIME_DEFAULT, - .channel_id = 0, +static const struct adc_dt_spec adc_channels[] = { + DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, + DT_SPEC_AND_COMMA) }; void adc_init(){ - if(!device_is_ready(adc_dev)){ - LOG_ERR("ADC device not ready"); - return; + if(!adc_is_ready_dt(&adc_channels[0])){ + LOG_ERR("ADC controller device %s not ready\n", adc_channels[i].dev->name); + return 0; } - int ret = adc_channel_setup(adc_dev, &channel_cfg); - if(ret < 0){ - LOG_ERR("Failed to configure ADC channel (%d)", ret); - return; + int err = adc_channel_setup_dt(&adc_channels[0]); + if(err < 0){ + LOG_ERR("Could not setup channel (%d)\n", err); + return 0; } + // if(!device_is_ready(adc_dev)){ + // LOG_ERR("ADC device not ready"); + // return; + // } + + // int ret = adc_channel_setup(adc_dev, adc_channels[0]); + // if(ret < 0){ + // LOG_ERR("Failed to configure ADC channel (%d)", ret); + // return; + // } + LOG_INF("ADC initialized"); } From e2b3276cc9e63d8ffb105c76167e83af26057da9 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 17:32:27 -0400 Subject: [PATCH 04/43] folder renamed --- app/other/{grim_reefer => solids_test}/CMakeLists.txt | 0 app/other/{grim_reefer => solids_test}/Kconfig | 0 app/other/{grim_reefer => solids_test}/boards/grim_reefer.overlay | 0 app/other/{grim_reefer => solids_test}/include/adc_reading.h | 0 app/other/{grim_reefer => solids_test}/include/calibration.h | 0 app/other/{grim_reefer => solids_test}/include/config.h | 0 app/other/{grim_reefer => solids_test}/include/control.h | 0 app/other/{grim_reefer => solids_test}/include/flash_storage.h | 0 app/other/{grim_reefer => solids_test}/prj.conf | 0 app/other/{grim_reefer => solids_test}/sample.yaml | 0 app/other/{grim_reefer => solids_test}/src/adc_reading.c | 0 app/other/{grim_reefer => solids_test}/src/calibration.c | 0 app/other/{grim_reefer => solids_test}/src/control.c | 0 app/other/{grim_reefer => solids_test}/src/flash_storage.c | 0 app/other/{grim_reefer => solids_test}/src/main.c | 0 app/other/{grim_reefer => solids_test}/src/shell_cmds.c | 0 16 files changed, 0 insertions(+), 0 deletions(-) rename app/other/{grim_reefer => solids_test}/CMakeLists.txt (100%) rename app/other/{grim_reefer => solids_test}/Kconfig (100%) rename app/other/{grim_reefer => solids_test}/boards/grim_reefer.overlay (100%) rename app/other/{grim_reefer => solids_test}/include/adc_reading.h (100%) rename app/other/{grim_reefer => solids_test}/include/calibration.h (100%) rename app/other/{grim_reefer => solids_test}/include/config.h (100%) rename app/other/{grim_reefer => solids_test}/include/control.h (100%) rename app/other/{grim_reefer => solids_test}/include/flash_storage.h (100%) rename app/other/{grim_reefer => solids_test}/prj.conf (100%) rename app/other/{grim_reefer => solids_test}/sample.yaml (100%) rename app/other/{grim_reefer => solids_test}/src/adc_reading.c (100%) rename app/other/{grim_reefer => solids_test}/src/calibration.c (100%) rename app/other/{grim_reefer => solids_test}/src/control.c (100%) rename app/other/{grim_reefer => solids_test}/src/flash_storage.c (100%) rename app/other/{grim_reefer => solids_test}/src/main.c (100%) rename app/other/{grim_reefer => solids_test}/src/shell_cmds.c (100%) diff --git a/app/other/grim_reefer/CMakeLists.txt b/app/other/solids_test/CMakeLists.txt similarity index 100% rename from app/other/grim_reefer/CMakeLists.txt rename to app/other/solids_test/CMakeLists.txt diff --git a/app/other/grim_reefer/Kconfig b/app/other/solids_test/Kconfig similarity index 100% rename from app/other/grim_reefer/Kconfig rename to app/other/solids_test/Kconfig diff --git a/app/other/grim_reefer/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay similarity index 100% rename from app/other/grim_reefer/boards/grim_reefer.overlay rename to app/other/solids_test/boards/grim_reefer.overlay diff --git a/app/other/grim_reefer/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h similarity index 100% rename from app/other/grim_reefer/include/adc_reading.h rename to app/other/solids_test/include/adc_reading.h diff --git a/app/other/grim_reefer/include/calibration.h b/app/other/solids_test/include/calibration.h similarity index 100% rename from app/other/grim_reefer/include/calibration.h rename to app/other/solids_test/include/calibration.h diff --git a/app/other/grim_reefer/include/config.h b/app/other/solids_test/include/config.h similarity index 100% rename from app/other/grim_reefer/include/config.h rename to app/other/solids_test/include/config.h diff --git a/app/other/grim_reefer/include/control.h b/app/other/solids_test/include/control.h similarity index 100% rename from app/other/grim_reefer/include/control.h rename to app/other/solids_test/include/control.h diff --git a/app/other/grim_reefer/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h similarity index 100% rename from app/other/grim_reefer/include/flash_storage.h rename to app/other/solids_test/include/flash_storage.h diff --git a/app/other/grim_reefer/prj.conf b/app/other/solids_test/prj.conf similarity index 100% rename from app/other/grim_reefer/prj.conf rename to app/other/solids_test/prj.conf diff --git a/app/other/grim_reefer/sample.yaml b/app/other/solids_test/sample.yaml similarity index 100% rename from app/other/grim_reefer/sample.yaml rename to app/other/solids_test/sample.yaml diff --git a/app/other/grim_reefer/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c similarity index 100% rename from app/other/grim_reefer/src/adc_reading.c rename to app/other/solids_test/src/adc_reading.c diff --git a/app/other/grim_reefer/src/calibration.c b/app/other/solids_test/src/calibration.c similarity index 100% rename from app/other/grim_reefer/src/calibration.c rename to app/other/solids_test/src/calibration.c diff --git a/app/other/grim_reefer/src/control.c b/app/other/solids_test/src/control.c similarity index 100% rename from app/other/grim_reefer/src/control.c rename to app/other/solids_test/src/control.c diff --git a/app/other/grim_reefer/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c similarity index 100% rename from app/other/grim_reefer/src/flash_storage.c rename to app/other/solids_test/src/flash_storage.c diff --git a/app/other/grim_reefer/src/main.c b/app/other/solids_test/src/main.c similarity index 100% rename from app/other/grim_reefer/src/main.c rename to app/other/solids_test/src/main.c diff --git a/app/other/grim_reefer/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c similarity index 100% rename from app/other/grim_reefer/src/shell_cmds.c rename to app/other/solids_test/src/shell_cmds.c From a178bd6348c59e56ca34730a4eeb535dcc50237d Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 18:29:36 -0400 Subject: [PATCH 05/43] broken :( --- .../solids_test/boards/grim_reefer.overlay | 4 ++- app/other/solids_test/include/config.h | 2 +- app/other/solids_test/src/adc_reading.c | 25 +++++++---------- app/other/solids_test/src/flash_storage.c | 28 +++++++++++++++++-- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 16739018a..4bdcf4a41 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -7,6 +7,7 @@ aliases { adc0 = &adc; + buzzer = &led1; }; }; @@ -23,7 +24,7 @@ analog-clock-prescaler = <0>; boost-current-bias = <0>; spi-max-frequency = ; - irq-gpios = <&gpioc 0 GPIO_ACTIVE_LOW>; + irq-gpios = <&gpiob 14 GPIO_ACTIVE_LOW>; channel@0 { reg = <0>; @@ -38,6 +39,7 @@ zephyr,input-positive = <0x1>; zephyr,input-negative = <0x0>; + zephyr,oversampling = <8>; }; }; }; \ No newline at end of file diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index e45cd1cf4..97d109645 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -2,7 +2,7 @@ #define REEFER_INCLUDE_CONFIG_H #define SAMPLE_RATE_HZ 1000 -#define TEST_DURATION 10 +#define TEST_DURATION 1 #define MAX_TESTS 30 #define STORAGE_THREAD_PRIORITY 1 diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 12535cfaf..6329693ff 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -2,11 +2,12 @@ #include "config.h" #include "flash_storage.h" +#include #include #include #include #include -#include +#include LOG_MODULE_REGISTER(adc_reader, LOG_LEVEL_INF); @@ -37,14 +38,16 @@ static struct adc_sequence sequence = { .resolution = 24 }; +#define DT_SPEC_AND_COMMA(node_id, prop, idx) \ + ADC_DT_SPEC_GET_BY_IDX(node_id, idx), + static const struct adc_dt_spec adc_channels[] = { - DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, - DT_SPEC_AND_COMMA) + DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA) }; void adc_init(){ if(!adc_is_ready_dt(&adc_channels[0])){ - LOG_ERR("ADC controller device %s not ready\n", adc_channels[i].dev->name); + LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); return 0; } @@ -54,17 +57,6 @@ void adc_init(){ return 0; } - // if(!device_is_ready(adc_dev)){ - // LOG_ERR("ADC device not ready"); - // return; - // } - - // int ret = adc_channel_setup(adc_dev, adc_channels[0]); - // if(ret < 0){ - // LOG_ERR("Failed to configure ADC channel (%d)", ret); - // return; - // } - LOG_INF("ADC initialized"); } @@ -95,6 +87,7 @@ void adc_reading_task(){ sequence.buffer_size = sizeof(adc_val); // Read from ADC + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); ret = adc_read(adc_dev, &sequence); if(ret < 0){ LOG_ERR("ADC read failed (%d)", ret); @@ -104,6 +97,8 @@ void adc_reading_task(){ sample.timestamp = k_uptime_get_32(); sample.value = adc_val; + LOG_INF("%u", sample.value); + k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); } diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 9f8b59033..b3aa39e06 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -171,12 +171,36 @@ int flash_dump_all(const struct shell *shell){ off_t addr = block_addr; for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ - if (flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0) break; - if (sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF) break; + if (flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ + shell_print(shell, "Flash read failed"); + break; + } + if (sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ + shell_print(shell, "Flash block empty"); + break; + }; shell_print(shell, "%u,%u", sample.timestamp, sample.value); addr += sizeof(sample); } } + // flash_erase_all(); + + return 0; +} + +int flash_erase_all(){ + for(uint32_t i = 0; i < MAX_TESTS; i++){ + off_t curr_add = get_test_block_addr(i); + int ret = flash_erase(flash_dev, curr_add, SPI_FLASH_BLOCK_SIZE); + if(ret < 0){ + LOG_ERR("flash_erase failed: %d", ret); + continue; + } + else{ + LOG_INF("Flash block %d erased", i); + } + } + return 0; } \ No newline at end of file From 0904818554a7ceac08a34dfddcd6f0eff1d2839c Mon Sep 17 00:00:00 2001 From: cowsed Date: Thu, 23 Oct 2025 18:57:13 -0400 Subject: [PATCH 06/43] fixes n things --- app/other/solids_test/boards/grim_reefer.overlay | 2 +- app/other/solids_test/include/adc_reading.h | 2 +- app/other/solids_test/prj.conf | 10 +++++----- app/other/solids_test/src/adc_reading.c | 3 ++- app/other/solids_test/src/flash_storage.c | 14 +++++++++----- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 4bdcf4a41..873685a78 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -7,7 +7,7 @@ aliases { adc0 = &adc; - buzzer = &led1; + // buzzer = &led1; }; }; diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index 696c772fd..d234aabfe 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -8,7 +8,7 @@ struct adc_sample { uint32_t value; }; -void adc_init(); +int adc_init(); void adc_reading_task(); void adc_start_reading(); void adc_stop_recording(); diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index c2037cbde..909e798ad 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -1,5 +1,8 @@ -CONFIG_LOG=y +CONFIG_DEBUG=y +CONFIG_DEBUG_THREAD_INFO=y +CONFIG_LOG=y +CONFIG_LOG_MODE_IMMEDIATE=y CONFIG_CRC=y CONFIG_SMF=y @@ -25,11 +28,8 @@ CONFIG_FLASH_MAP=y CONFIG_FLASH_PAGE_LAYOUT=y CONFIG_DISK_ACCESS=y CONFIG_FILE_SYSTEM=y -CONFIG_FILE_SYSTEM_LITTLEFS=y - -CONFIG_FS_LITTLEFS_FMP_DEV=y CONFIG_SHELL=y CONFIG_FLASH_SHELL=y CONFIG_SHELL_BACKEND_SERIAL=y -CONFIG_UART_CONSOLE=y \ No newline at end of file +CONFIG_UART_CONSOLE=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 6329693ff..fc1b05712 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -45,7 +45,7 @@ static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA) }; -void adc_init(){ +int adc_init(){ if(!adc_is_ready_dt(&adc_channels[0])){ LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); return 0; @@ -58,6 +58,7 @@ void adc_init(){ } LOG_INF("ADC initialized"); + return 0; } void adc_reading_task(){ diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index b3aa39e06..0f4ba3fcb 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -113,7 +113,7 @@ static void flash_storage_thread_entry(){ } off_t test_block_addr = get_test_block_addr(current_test_number); - LOG_INF("Starting test %d at addr 0x%08x", current_test_number, (long)test_block_addr); + LOG_INF("Starting test %u at addr 0x%08lx", current_test_number, (long)test_block_addr); // Erase block int ret = flash_erase(flash_dev, test_block_addr, SPI_FLASH_BLOCK_SIZE); @@ -154,6 +154,8 @@ void stop_flash_storage(){ k_msgq_put(&storage_control_queue, &event, K_FOREVER); } +int flash_erase_all(); + int flash_dump_all(const struct shell *shell){ struct adc_sample sample; uint32_t test_index = 0; @@ -172,19 +174,19 @@ int flash_dump_all(const struct shell *shell){ for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ if (flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ - shell_print(shell, "Flash read failed"); + // shell_print(shell, "Flash read failed"); break; } if (sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ - shell_print(shell, "Flash block empty"); + // shell_print(shell, "Flash block empty"); break; - }; + } shell_print(shell, "%u,%u", sample.timestamp, sample.value); addr += sizeof(sample); } } - // flash_erase_all(); + flash_erase_all(); return 0; } @@ -201,6 +203,8 @@ int flash_erase_all(){ LOG_INF("Flash block %d erased", i); } } + current_test_number = 0; + save_metadata(); return 0; } \ No newline at end of file From abea238186793acc8430d8f5024ad51a2c63b82a Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 18:57:42 -0400 Subject: [PATCH 07/43] merge conflict :( --- app/other/solids_test/src/flash_storage.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index b3aa39e06..9fd842b0b 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -171,14 +171,14 @@ int flash_dump_all(const struct shell *shell){ off_t addr = block_addr; for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ - if (flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ + if(flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ shell_print(shell, "Flash read failed"); break; } - if (sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ + if(sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ shell_print(shell, "Flash block empty"); break; - }; + } shell_print(shell, "%u,%u", sample.timestamp, sample.value); addr += sizeof(sample); } @@ -189,7 +189,7 @@ int flash_dump_all(const struct shell *shell){ return 0; } -int flash_erase_all(){ +static int flash_erase_all(){ for(uint32_t i = 0; i < MAX_TESTS; i++){ off_t curr_add = get_test_block_addr(i); int ret = flash_erase(flash_dev, curr_add, SPI_FLASH_BLOCK_SIZE); @@ -202,5 +202,8 @@ int flash_erase_all(){ } } + current_test_number = 0; + save_metadata(); + return 0; } \ No newline at end of file From f04f221af91550c789616e3fd7b35a09e37a2e9b Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 19:48:18 -0400 Subject: [PATCH 08/43] halfway working maybe --- app/other/grim_reefer/CMakeLists.txt | 8 + app/other/grim_reefer/Kconfig | 1 + .../grim_reefer/boards/grim_reefer.overlay | 46 ++++ app/other/grim_reefer/include/adc_reading.h | 16 ++ app/other/grim_reefer/include/calibration.h | 5 + app/other/grim_reefer/include/config.h | 15 ++ app/other/grim_reefer/include/control.h | 13 ++ app/other/grim_reefer/include/flash_storage.h | 12 + app/other/grim_reefer/prj.conf | 38 ++++ app/other/grim_reefer/sample.yaml | 9 + app/other/grim_reefer/src/adc_reading.c | 119 ++++++++++ app/other/grim_reefer/src/calibration.c | 2 + app/other/grim_reefer/src/control.c | 58 +++++ app/other/grim_reefer/src/flash_storage.c | 211 ++++++++++++++++++ app/other/grim_reefer/src/main.c | 24 ++ app/other/grim_reefer/src/shell_cmds.c | 41 ++++ 16 files changed, 618 insertions(+) create mode 100644 app/other/grim_reefer/CMakeLists.txt create mode 100644 app/other/grim_reefer/Kconfig create mode 100644 app/other/grim_reefer/boards/grim_reefer.overlay create mode 100644 app/other/grim_reefer/include/adc_reading.h create mode 100644 app/other/grim_reefer/include/calibration.h create mode 100644 app/other/grim_reefer/include/config.h create mode 100644 app/other/grim_reefer/include/control.h create mode 100644 app/other/grim_reefer/include/flash_storage.h create mode 100644 app/other/grim_reefer/prj.conf create mode 100644 app/other/grim_reefer/sample.yaml create mode 100644 app/other/grim_reefer/src/adc_reading.c create mode 100644 app/other/grim_reefer/src/calibration.c create mode 100644 app/other/grim_reefer/src/control.c create mode 100644 app/other/grim_reefer/src/flash_storage.c create mode 100644 app/other/grim_reefer/src/main.c create mode 100644 app/other/grim_reefer/src/shell_cmds.c diff --git a/app/other/grim_reefer/CMakeLists.txt b/app/other/grim_reefer/CMakeLists.txt new file mode 100644 index 000000000..a3208d3f3 --- /dev/null +++ b/app/other/grim_reefer/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(grim_reefer LANGUAGES C) + +FILE(GLOB sources src/*.c) +target_sources(app PRIVATE ${sources}) +target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) \ No newline at end of file diff --git a/app/other/grim_reefer/Kconfig b/app/other/grim_reefer/Kconfig new file mode 100644 index 000000000..0ffe17553 --- /dev/null +++ b/app/other/grim_reefer/Kconfig @@ -0,0 +1 @@ +source "Kconfig.zephyr" \ No newline at end of file diff --git a/app/other/grim_reefer/boards/grim_reefer.overlay b/app/other/grim_reefer/boards/grim_reefer.overlay new file mode 100644 index 000000000..d07393126 --- /dev/null +++ b/app/other/grim_reefer/boards/grim_reefer.overlay @@ -0,0 +1,46 @@ +/delete-node/ &adc; + +/ { + zephyr,user { + io-channels = <&adc 0>, <&adc 0>; + }; + + aliases { + adc0 = &adc; + // buzzer = &led1; + }; +}; + +&spi2 { + status = "okay"; + adc: mcp3561@0 { // new and cool driver + compatible = "microchip,mcp356xr"; + status = "okay"; + reg = <0>; + #io-channel-cells = <1>; + #address-cells = <1>; + #size-cells = <0>; + address = <1>; + analog-clock-prescaler = <0>; + boost-current-bias = <0>; + spi-max-frequency = ; + irq-gpios = <&gpiob 14 GPIO_ACTIVE_LOW>; + use-internal-clock; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_8"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + + zephyr,resolution = <24>; + + zephyr,differential; + + zephyr,input-positive = <0x1>; + zephyr,input-negative = <0x0>; + + zephyr,oversampling = <8>; + }; + }; +}; \ No newline at end of file diff --git a/app/other/grim_reefer/include/adc_reading.h b/app/other/grim_reefer/include/adc_reading.h new file mode 100644 index 000000000..d234aabfe --- /dev/null +++ b/app/other/grim_reefer/include/adc_reading.h @@ -0,0 +1,16 @@ +#ifndef ADC_READING_H +#define ADC_READING_H + +#include + +struct adc_sample { + uint32_t timestamp; + uint32_t value; +}; + +int adc_init(); +void adc_reading_task(); +void adc_start_reading(); +void adc_stop_recording(); + +#endif // ADC_READING_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/calibration.h b/app/other/grim_reefer/include/calibration.h new file mode 100644 index 000000000..4fbda050c --- /dev/null +++ b/app/other/grim_reefer/include/calibration.h @@ -0,0 +1,5 @@ +#ifndef CALIBRATION_H +#define CALIBRATION_H + + +#endif // CALIBRATION_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/config.h b/app/other/grim_reefer/include/config.h new file mode 100644 index 000000000..c15809da3 --- /dev/null +++ b/app/other/grim_reefer/include/config.h @@ -0,0 +1,15 @@ +#ifndef REEFER_INCLUDE_CONFIG_H +#define REEFER_INCLUDE_CONFIG_H + +#define SAMPLE_RATE_HZ 1000 +#define TEST_DURATION 5 +#define MAX_TESTS 30 + +#define STORAGE_THREAD_PRIORITY 1 +#define ADC_READ_PRIORITY -1 +#define THREAD_START_DELAY 100 + +#define ADC_BUFFER_SIZE (SAMPLE_RATE_HZ* TEST_DURATION_SEC) +#define FLASH_BLOCK_SIZE (ADC_BUFFER_SIZE* sizeof(uint32_t)) + +#endif // REEFER_INCLUDE_CONFIG_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/control.h b/app/other/grim_reefer/include/control.h new file mode 100644 index 000000000..3f9c3195c --- /dev/null +++ b/app/other/grim_reefer/include/control.h @@ -0,0 +1,13 @@ +#ifndef CONTROL_H +#define CONTROL_H + +#include +#include + +void control_init(); +void control_start_test(); +void control_stop_test(); +void control_dump_data(const struct shell *shell); +bool control_is_running(); + +#endif // CONTROL_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/flash_storage.h b/app/other/grim_reefer/include/flash_storage.h new file mode 100644 index 000000000..dc8b95d82 --- /dev/null +++ b/app/other/grim_reefer/include/flash_storage.h @@ -0,0 +1,12 @@ +#ifndef FLASH_STORAGE_H +#define FLASH_STORAGE_H + +#include + +int start_flash_storage(); +void stop_flash_storage(); +int flash_dump_all(const struct shell *shell); + +extern struct k_msgq storage_control_queue; + +#endif // FLASH_STORAGE_H \ No newline at end of file diff --git a/app/other/grim_reefer/prj.conf b/app/other/grim_reefer/prj.conf new file mode 100644 index 000000000..28445e183 --- /dev/null +++ b/app/other/grim_reefer/prj.conf @@ -0,0 +1,38 @@ +CONFIG_DEBUG=y +CONFIG_DEBUG_THREAD_INFO=y + +CONFIG_LOG=y +CONFIG_LOG_MODE_IMMEDIATE=y +CONFIG_CRC=y + +CONFIG_SMF=y +CONFIG_EVENTS=y +CONFIG_BASE64=y + +CONFIG_GPIO=y +CONFIG_SPI=y +CONFIG_I2C=y + +CONFIG_ADC=y +CONFIG_ADC_LOG_LEVEL_DBG=y +# CONFIG_MCP356X=n # old driver +CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr +CONFIG_ADC_MCP356XR_POLL=y +CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 +CONFIG_INA260=y + +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_CBPRINTF_FP_SUPPORT=y + +CONFIG_SPI_NOR_SFDP_RUNTIME=y +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_FLASH_PAGE_LAYOUT=y +CONFIG_DISK_ACCESS=y +CONFIG_FILE_SYSTEM=y + +CONFIG_SHELL=y +CONFIG_FLASH_SHELL=y +CONFIG_SHELL_BACKEND_SERIAL=y +CONFIG_UART_CONSOLE=y diff --git a/app/other/grim_reefer/sample.yaml b/app/other/grim_reefer/sample.yaml new file mode 100644 index 000000000..169ef60c1 --- /dev/null +++ b/app/other/grim_reefer/sample.yaml @@ -0,0 +1,9 @@ +sample: + description: + name: grim_reefer +common: + build_only: true + platform_allow: + - grim_reefer +tests: + solids_test.default: {} diff --git a/app/other/grim_reefer/src/adc_reading.c b/app/other/grim_reefer/src/adc_reading.c new file mode 100644 index 000000000..2a88ed866 --- /dev/null +++ b/app/other/grim_reefer/src/adc_reading.c @@ -0,0 +1,119 @@ +#include "adc_reading.h" +#include "config.h" +#include "flash_storage.h" + +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(adc_reader, LOG_LEVEL_INF); + +#define ADC_NODE DT_ALIAS(adc0) + +#if !DT_NODE_HAS_STATUS(ADC_NODE, okay) +#error "No ADC node found" +#endif + +static const struct device *adc_dev = DEVICE_DT_GET(ADC_NODE); + +extern struct k_msgq adc_data_queue; + +#define BEGIN_READING_EVENT 1 +#define STOP_READING_EVENT 2 + +static K_EVENT_DEFINE(adc_control_event); +static K_TIMER_DEFINE(adc_timer, NULL, NULL); + +void adc_reading_task(void); + +K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, ADC_READ_PRIORITY, 0, THREAD_START_DELAY); + +static uint32_t adc_buffer; +static struct adc_sequence sequence = { + .buffer = &adc_buffer, + .buffer_size = sizeof(adc_buffer), + .resolution = 24 +}; + +#define DT_SPEC_AND_COMMA(node_id, prop, idx) \ + ADC_DT_SPEC_GET_BY_IDX(node_id, idx), + +static const struct adc_dt_spec adc_channels[] = { + DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA) +}; + +int adc_init(){ + if(!adc_is_ready_dt(&adc_channels[0])){ + LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); + return 0; + } + + int err = adc_channel_setup_dt(&adc_channels[0]); + if(err < 0){ + LOG_ERR("Could not setup channel (%d)\n", err); + return 0; + } + + LOG_INF("ADC initialized"); + return 0; +} + +void adc_reading_task(){ + int ret; + uint32_t adc_val = 0; + struct adc_sample sample = {0}; + + LOG_INF("ADC reading task waiting to start..."); + + // Wait for start event + k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); + + LOG_INF("ADC reading started"); + + k_timer_start(&adc_timer, K_MSEC(1), K_MSEC(1)); // 1kHz loop + + int x = 0; + + while(true){ + uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); + if(events & STOP_READING_EVENT){ + break; + } + + k_timer_status_sync(&adc_timer); + + sequence.buffer = &adc_val; + sequence.buffer_size = sizeof(adc_val); + + // Read from ADC + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + ret = adc_read(adc_dev, &sequence); + if(ret < 0){ + LOG_ERR("ADC read failed (%d)", ret); + continue; + } + + sample.timestamp = k_uptime_get_32(); + sample.value = adc_val; + + if(x % 100 == 0){ + LOG_INF("sample value: %u", sample.value); + } + x++; + + k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); + } + + k_timer_stop(&adc_timer); +} + +void adc_start_reading(){ + k_event_set(&adc_control_event, BEGIN_READING_EVENT); +} + +void adc_stop_recording(){ + k_event_set(&adc_control_event, STOP_READING_EVENT); +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/calibration.c b/app/other/grim_reefer/src/calibration.c new file mode 100644 index 000000000..b12f85166 --- /dev/null +++ b/app/other/grim_reefer/src/calibration.c @@ -0,0 +1,2 @@ +#include "calibration.h" +#include "config.h" \ No newline at end of file diff --git a/app/other/grim_reefer/src/control.c b/app/other/grim_reefer/src/control.c new file mode 100644 index 000000000..2d6cefa63 --- /dev/null +++ b/app/other/grim_reefer/src/control.c @@ -0,0 +1,58 @@ +#include "control.h" +#include "config.h" +#include "adc_reading.h" +#include "flash_storage.h" + +#include +#include +#include +#include + +LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); + +static int test_number = 0; +static bool test_running = false; + +void control_init(){ + LOG_INF("Initializing..."); + adc_init(); +} + +void control_start_test(){ + if(test_running){ + LOG_WRN("Test already running"); + return; + } + + LOG_INF("Starting test run #%d", test_number + 1); + test_running = true; + + start_flash_storage(); + adc_start_reading(); + + test_number++; + + // Run test for 10 seconds + k_sleep(K_SECONDS(TEST_DURATION)); + control_stop_test(); +} + +void control_stop_test(){ + if(!test_running){ + LOG_WRN("No test running"); + return; + } + + LOG_INF("Stopping test..."); + adc_stop_recording(); + stop_flash_storage(); + test_running = false; +} + +void control_dump_data(const struct shell *shell){ + flash_dump_all(shell); +} + +bool control_is_running(){ + return test_running; +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/flash_storage.c b/app/other/grim_reefer/src/flash_storage.c new file mode 100644 index 000000000..3f2ced009 --- /dev/null +++ b/app/other/grim_reefer/src/flash_storage.c @@ -0,0 +1,211 @@ +#include "flash_storage.h" +#include "config.h" +#include "adc_reading.h" + +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(flash_storage, LOG_LEVEL_INF); + +#define SPI_FLASH_ADDR 0x00000000 +#define FLASH_METADATA_ADDR SPI_FLASH_ADDR +#define SPI_FLASH_BLOCK_SIZE (64 * 1024) // 64KB +#define FLASH_METADATA_SIZE (4 * 1024) // 4KB +#define SPI_FLASH_START_ADDR (FLASH_METADATA_ADDR + FLASH_METADATA_SIZE) + +enum storage_event {BEGIN_STORAGE, END_STORAGE}; + +K_MSGQ_DEFINE(storage_control_queue, sizeof(enum storage_event), 5, 1); + +K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), SAMPLE_RATE_HZ * TEST_DURATION, 4); + +static const struct device *flash_dev = DEVICE_DT_GET(DT_ALIAS(storage)); +static uint32_t current_test_number = 0; +static off_t current_write_addr = 0; + +static void flash_storage_thread_entry(void); + +K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, + STORAGE_THREAD_PRIORITY, 0, 1000); + +// Check if flash block is all 0xFF +static bool flash_block_is_empty(off_t addr){ + uint8_t buf[16]; + if(flash_read(flash_dev, addr, buf, sizeof(buf)) < 0){ + LOG_ERR("Flash read failed"); + return false; + } + + for(int i = 0; i < sizeof(buf); i++){ + if(buf[i] != 0xFF){ + return false; + } + } + return true; +} + +// Get and update current test number +static void load_metadata(){ + uint8_t buf[4]; + if(flash_read(flash_dev, FLASH_METADATA_ADDR, buf, sizeof(buf)) < 0){ + LOG_ERR("Failed to read metadata"); + current_test_number = 0; + return; + } + + // If all are erased (0xFF), assume no tests yet + if(buf[0] == 0xFF && buf[1] == 0xFF && buf[2] == 0xFF && buf[3] == 0xFF){ + current_test_number = 0; + } else { + memcpy(¤t_test_number, buf, sizeof(current_test_number)); + } + + LOG_INF("Loaded test number %d", current_test_number); +} + +static void save_metadata(){ + int ret; + ret = flash_erase(flash_dev, FLASH_METADATA_ADDR, FLASH_METADATA_SIZE); + + if(ret < 0){ + LOG_ERR("flash_erase(metadata) failed: %d", ret); + return; + } + + ret = flash_write(flash_dev, FLASH_METADATA_ADDR, ¤t_test_number, sizeof(current_test_number)); + + if(ret < 0){ + LOG_ERR("flash_write(metadata) failed: %d", ret); + } else { + LOG_INF("Saved current_test_number = %u", current_test_number); + } +} + +static off_t get_test_block_addr(uint32_t test_index){ + return (off_t)(SPI_FLASH_START_ADDR + (test_index * SPI_FLASH_BLOCK_SIZE)); +} + +static void flash_storage_thread_entry(){ + struct adc_sample sample; + enum storage_event event; + + if(!device_is_ready(flash_dev)){ + LOG_ERR("Flash device not ready"); + return; + } + + // Get current test number + load_metadata(); + + while(1){ + // Wait for start command + k_msgq_get(&storage_control_queue, &event, K_FOREVER); + if (event != BEGIN_STORAGE) continue; + + if(current_test_number >= MAX_TESTS){ + LOG_ERR("Maximum number of test reached"); + continue; + } + + off_t test_block_addr = get_test_block_addr(current_test_number); + LOG_INF("Starting test %u at addr 0x%08lx", current_test_number, (long)test_block_addr); + + // Erase block + int ret = flash_erase(flash_dev, test_block_addr, SPI_FLASH_BLOCK_SIZE); + if(ret < 0){ + LOG_ERR("flash_erase(test block) failed: %d", ret); + continue; + } + + current_write_addr = test_block_addr; + + while(1){ + if(k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE){ + LOG_INF("Test %d complete", current_test_number); + current_test_number++; + save_metadata(); + break; + } + + if(k_msgq_get(&adc_data_queue, &sample, K_MSEC(50)) == 0){ + int ret = flash_write(flash_dev, current_write_addr, &sample, sizeof(sample)); + if(ret < 0){ + LOG_ERR("Flash write failed (%d)", ret); + } else { + current_write_addr += sizeof(sample); + } + } + } + } +} + +int start_flash_storage(){ + enum storage_event event = BEGIN_STORAGE; + return k_msgq_put(&storage_control_queue, &event, K_FOREVER); +} + +void stop_flash_storage(){ + enum storage_event event = END_STORAGE; + k_msgq_put(&storage_control_queue, &event, K_FOREVER); +} + +int flash_erase_all(); + +int flash_dump_all(const struct shell *shell){ + struct adc_sample sample; + uint32_t test_index = 0; + + if(!device_is_ready(flash_dev)){ + shell_error(shell, "Flash not ready"); + return -1; + } + + for(test_index = 0; test_index < current_test_number; test_index++){ + off_t block_addr = get_test_block_addr(test_index); + if (flash_block_is_empty(block_addr)) continue; + + shell_print(shell, "Dumping Test %d:", test_index); + off_t addr = block_addr; + + for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ + if(flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ + shell_print(shell, "Flash read failed"); + break; + } + if(sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ + shell_print(shell, "Flash block empty"); + break; + } + shell_print(shell, "%u,%u", sample.timestamp, sample.value); + addr += sizeof(sample); + } + } + + flash_erase_all(); + + return 0; +} + +int flash_erase_all(){ + for(uint32_t i = 0; i < MAX_TESTS; i++){ + off_t curr_add = get_test_block_addr(i); + int ret = flash_erase(flash_dev, curr_add, SPI_FLASH_BLOCK_SIZE); + if(ret < 0){ + LOG_ERR("flash_erase failed: %d", ret); + continue; + } + else{ + LOG_INF("Flash block %d erased", i); + } + } + + current_test_number = 0; + save_metadata(); + + return 0; +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/main.c b/app/other/grim_reefer/src/main.c new file mode 100644 index 000000000..ad389686e --- /dev/null +++ b/app/other/grim_reefer/src/main.c @@ -0,0 +1,24 @@ +#include "control.h" + +#include +#include +#include +#include + +LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); + +int main(void) +{ + LOG_INF("Solids Test Start"); + + control_init(); + + LOG_INF("Use 'test start' to begin test"); + LOG_INF("Commands: test start | test stop | test dump"); + + while(1){ + k_sleep(K_MSEC(10)); + } + + return 0; +} \ No newline at end of file diff --git a/app/other/grim_reefer/src/shell_cmds.c b/app/other/grim_reefer/src/shell_cmds.c new file mode 100644 index 000000000..aa1b55460 --- /dev/null +++ b/app/other/grim_reefer/src/shell_cmds.c @@ -0,0 +1,41 @@ +#include "control.h" +#include "flash_storage.h" +#include "adc_reading.h" + +#include +#include + +LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); + +static int cmd_test_start(const struct shell *shell, size_t argc, char **argv){ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Starting test..."); + control_start_test(); + return 0; +} + +static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv){ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Stopping test..."); + control_stop_test(); + return 0; +} + +static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv){ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Dumping stored test data..."); + control_dump_data(shell); + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, + SHELL_CMD(start, NULL, "Start test", cmd_test_start), + SHELL_CMD(stop, NULL, "Stop test", cmd_test_stop), + SHELL_CMD(dump, NULL, "Dump all flash data", cmd_test_dump), + SHELL_SUBCMD_SET_END +); + +SHELL_CMD_REGISTER(test, &sub_test, "Solids Test Board control commands", NULL); \ No newline at end of file From 6b274fe54f37911aea191b573c318220186b8c1a Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 19:49:43 -0400 Subject: [PATCH 09/43] renamed fr this time --- app/other/solids_test/boards/grim_reefer.overlay | 1 + app/other/solids_test/include/config.h | 2 +- app/other/solids_test/prj.conf | 3 +++ app/other/solids_test/src/adc_reading.c | 12 +++++++----- app/other/solids_test/src/flash_storage.c | 13 +------------ 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 873685a78..d07393126 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -25,6 +25,7 @@ boost-current-bias = <0>; spi-max-frequency = ; irq-gpios = <&gpiob 14 GPIO_ACTIVE_LOW>; + use-internal-clock; channel@0 { reg = <0>; diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index 97d109645..c15809da3 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -2,7 +2,7 @@ #define REEFER_INCLUDE_CONFIG_H #define SAMPLE_RATE_HZ 1000 -#define TEST_DURATION 1 +#define TEST_DURATION 5 #define MAX_TESTS 30 #define STORAGE_THREAD_PRIORITY 1 diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index 909e798ad..28445e183 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -14,8 +14,11 @@ CONFIG_SPI=y CONFIG_I2C=y CONFIG_ADC=y +CONFIG_ADC_LOG_LEVEL_DBG=y # CONFIG_MCP356X=n # old driver CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr +CONFIG_ADC_MCP356XR_POLL=y +CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 CONFIG_INA260=y CONFIG_SERIAL=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index fc1b05712..2a88ed866 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -69,16 +69,17 @@ void adc_reading_task(){ LOG_INF("ADC reading task waiting to start..."); // Wait for start event - k_event_wait(&adc_control_event, BEGIN_READING_EVENT, false, K_FOREVER); + k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); LOG_INF("ADC reading started"); k_timer_start(&adc_timer, K_MSEC(1), K_MSEC(1)); // 1kHz loop + int x = 0; + while(true){ uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); if(events & STOP_READING_EVENT){ - LOG_INF("ADC reading stopped"); break; } @@ -98,7 +99,10 @@ void adc_reading_task(){ sample.timestamp = k_uptime_get_32(); sample.value = adc_val; - LOG_INF("%u", sample.value); + if(x % 100 == 0){ + LOG_INF("sample value: %u", sample.value); + } + x++; k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); } @@ -108,10 +112,8 @@ void adc_reading_task(){ void adc_start_reading(){ k_event_set(&adc_control_event, BEGIN_READING_EVENT); - LOG_INF("ADC reading started"); } void adc_stop_recording(){ k_event_set(&adc_control_event, STOP_READING_EVENT); - LOG_INF("ADC reading stopped"); } \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index efc4271d6..3f2ced009 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -173,21 +173,12 @@ int flash_dump_all(const struct shell *shell){ off_t addr = block_addr; for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ -<<<<<<< HEAD if(flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ shell_print(shell, "Flash read failed"); break; } if(sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ shell_print(shell, "Flash block empty"); -======= - if (flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ - // shell_print(shell, "Flash read failed"); - break; - } - if (sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ - // shell_print(shell, "Flash block empty"); ->>>>>>> 0904818554a7ceac08a34dfddcd6f0eff1d2839c break; } shell_print(shell, "%u,%u", sample.timestamp, sample.value); @@ -200,7 +191,7 @@ int flash_dump_all(const struct shell *shell){ return 0; } -static int flash_erase_all(){ +int flash_erase_all(){ for(uint32_t i = 0; i < MAX_TESTS; i++){ off_t curr_add = get_test_block_addr(i); int ret = flash_erase(flash_dev, curr_add, SPI_FLASH_BLOCK_SIZE); @@ -212,8 +203,6 @@ static int flash_erase_all(){ LOG_INF("Flash block %d erased", i); } } - current_test_number = 0; - save_metadata(); current_test_number = 0; save_metadata(); From 93401a265203e8d3009360478157c7460e3a4b16 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 23 Oct 2025 20:52:16 -0400 Subject: [PATCH 10/43] flash dump works, adc reading not working fast enough :( --- .../solids_test/boards/grim_reefer.overlay | 2 +- app/other/solids_test/include/config.h | 2 +- app/other/solids_test/include/control.h | 1 + app/other/solids_test/include/flash_storage.h | 1 + app/other/solids_test/prj.conf | 1 - app/other/solids_test/src/adc_reading.c | 63 ++++++++++--------- app/other/solids_test/src/control.c | 4 ++ app/other/solids_test/src/flash_storage.c | 38 ++++++++++- app/other/solids_test/src/shell_cmds.c | 17 ++++- 9 files changed, 92 insertions(+), 37 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index d07393126..3674142cf 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -29,7 +29,7 @@ channel@0 { reg = <0>; - zephyr,gain = "ADC_GAIN_8"; + zephyr,gain = "ADC_GAIN_64"; zephyr,reference = "ADC_REF_INTERNAL"; zephyr,acquisition-time = ; diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index c15809da3..e45cd1cf4 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -2,7 +2,7 @@ #define REEFER_INCLUDE_CONFIG_H #define SAMPLE_RATE_HZ 1000 -#define TEST_DURATION 5 +#define TEST_DURATION 10 #define MAX_TESTS 30 #define STORAGE_THREAD_PRIORITY 1 diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 3f9c3195c..06c16b3ed 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -8,6 +8,7 @@ void control_init(); void control_start_test(); void control_stop_test(); void control_dump_data(const struct shell *shell); +void control_dump_one(const struct shell *shell, uint32_t test_index); bool control_is_running(); #endif // CONTROL_H \ No newline at end of file diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index dc8b95d82..aef753476 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -6,6 +6,7 @@ int start_flash_storage(); void stop_flash_storage(); int flash_dump_all(const struct shell *shell); +int flash_dump_one(const struct shell *shell, uint32_t test_index); extern struct k_msgq storage_control_queue; diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index 28445e183..c2c824cda 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -17,7 +17,6 @@ CONFIG_ADC=y CONFIG_ADC_LOG_LEVEL_DBG=y # CONFIG_MCP356X=n # old driver CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr -CONFIG_ADC_MCP356XR_POLL=y CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 CONFIG_INA260=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 2a88ed866..0746db180 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -66,48 +66,53 @@ void adc_reading_task(){ uint32_t adc_val = 0; struct adc_sample sample = {0}; - LOG_INF("ADC reading task waiting to start..."); + while(true){ + LOG_INF("ADC reading task waiting to start..."); - // Wait for start event - k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); + // Wait for start event + k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); - LOG_INF("ADC reading started"); + LOG_INF("ADC reading started"); - k_timer_start(&adc_timer, K_MSEC(1), K_MSEC(1)); // 1kHz loop + k_timer_start(&adc_timer, K_USEC(100), K_USEC(100)); // 1kHz loop - int x = 0; + int x = 0; - while(true){ - uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); - if(events & STOP_READING_EVENT){ - break; - } + uint64_t start_time = k_uptime_get(); - k_timer_status_sync(&adc_timer); + while(true){ + uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); + if(events & STOP_READING_EVENT){ + break; + } - sequence.buffer = &adc_val; - sequence.buffer_size = sizeof(adc_val); + k_timer_status_sync(&adc_timer); - // Read from ADC - (void)adc_sequence_init_dt(&adc_channels[0], &sequence); - ret = adc_read(adc_dev, &sequence); - if(ret < 0){ - LOG_ERR("ADC read failed (%d)", ret); - continue; - } + sequence.buffer = &adc_val; + sequence.buffer_size = sizeof(adc_val); - sample.timestamp = k_uptime_get_32(); - sample.value = adc_val; + // Read from ADC + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + ret = adc_read(adc_dev, &sequence); + if(ret < 0){ + LOG_ERR("ADC read failed (%d)", ret); + continue; + } - if(x % 100 == 0){ - LOG_INF("sample value: %u", sample.value); + sample.timestamp = k_uptime_get() - start_time; + sample.value = adc_val; + + if(x % 25 == 0){ + LOG_INF("sample value: %u", sample.value); + } + x++; + + k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); } - x++; - k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); + k_timer_stop(&adc_timer); + LOG_INF("number of samples: %d", x); } - - k_timer_stop(&adc_timer); } void adc_start_reading(){ diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index 2d6cefa63..bca2e1ea5 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -53,6 +53,10 @@ void control_dump_data(const struct shell *shell){ flash_dump_all(shell); } +void control_dump_one(const struct shell *shell, uint32_t test_index){ + flash_dump_one(shell, test_index); +} + bool control_is_running(){ return test_running; } \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 3f2ced009..48a718516 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -14,7 +14,7 @@ LOG_MODULE_REGISTER(flash_storage, LOG_LEVEL_INF); #define SPI_FLASH_ADDR 0x00000000 #define FLASH_METADATA_ADDR SPI_FLASH_ADDR -#define SPI_FLASH_BLOCK_SIZE (64 * 1024) // 64KB +#define SPI_FLASH_BLOCK_SIZE (64 * 2048) // 128KB #define FLASH_METADATA_SIZE (4 * 1024) // 4KB #define SPI_FLASH_START_ADDR (FLASH_METADATA_ADDR + FLASH_METADATA_SIZE) @@ -156,6 +156,38 @@ void stop_flash_storage(){ int flash_erase_all(); +int flash_dump_one(const struct shell *shell, uint32_t test_index){ + struct adc_sample sample; + + if(!device_is_ready(flash_dev)){ + shell_error(shell, "Flash not ready"); + return -1; + } + + off_t block_addr = get_test_block_addr(test_index); + if(flash_block_is_empty(block_addr)){ + shell_print(shell, "Flash block empty"); + return 0; + } + + shell_print(shell, "Dumping Test %d:", test_index); + + for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ + if(flash_read(flash_dev, block_addr, &sample, sizeof(sample)) < 0){ + shell_print(shell, "Flash read failed"); + break; + } + if(sample.value == 0xFFFFFFFF && sample.timestamp == 0xFFFFFFFF){ + shell_print(shell, "Flash block unwritten"); + break; + } + shell_print(shell, "%u,%u", sample.timestamp, sample.value); + block_addr += sizeof(sample); + } + + return 0; +} + int flash_dump_all(const struct shell *shell){ struct adc_sample sample; uint32_t test_index = 0; @@ -177,8 +209,8 @@ int flash_dump_all(const struct shell *shell){ shell_print(shell, "Flash read failed"); break; } - if(sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ - shell_print(shell, "Flash block empty"); + if(sample.value == 0xFFFFFFFF && sample.timestamp == 0xFFFFFFFF){ + shell_print(shell, "Flash block unwritten"); break; } shell_print(shell, "%u,%u", sample.timestamp, sample.value); diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index aa1b55460..e98f7d4ba 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -4,6 +4,8 @@ #include #include +#include +#include LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); @@ -26,8 +28,19 @@ static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv){ static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv){ ARG_UNUSED(argc); ARG_UNUSED(argv); - shell_print(shell, "Dumping stored test data..."); - control_dump_data(shell); + + if(argc == 1){ + shell_print(shell, "Dumping all test data..."); + control_dump_data(shell); + } else if(argc == 2) { + uint32_t test_num = atoi(argv[1]); + shell_print(shell, "Dumping test %u data...", test_num); + control_dump_one(shell, test_num); + } else { + shell_print(shell, "Enter or "); + return -1; + } + return 0; } From 1ec5a0cf890e50fcf8f561320ee93101fc5c04d0 Mon Sep 17 00:00:00 2001 From: cowsed Date: Thu, 23 Oct 2025 21:17:50 -0400 Subject: [PATCH 11/43] got left bc of merge --- app/other/grim_reefer/CMakeLists.txt | 8 - app/other/grim_reefer/Kconfig | 1 - .../grim_reefer/boards/grim_reefer.overlay | 46 ---- app/other/grim_reefer/include/adc_reading.h | 16 -- app/other/grim_reefer/include/calibration.h | 5 - app/other/grim_reefer/include/config.h | 15 -- app/other/grim_reefer/include/control.h | 13 -- app/other/grim_reefer/include/flash_storage.h | 12 - app/other/grim_reefer/prj.conf | 38 ---- app/other/grim_reefer/sample.yaml | 9 - app/other/grim_reefer/src/adc_reading.c | 119 ---------- app/other/grim_reefer/src/calibration.c | 2 - app/other/grim_reefer/src/control.c | 58 ----- app/other/grim_reefer/src/flash_storage.c | 211 ------------------ app/other/grim_reefer/src/main.c | 24 -- app/other/grim_reefer/src/shell_cmds.c | 41 ---- app/other/solids_test/src/flash_storage.c | 2 +- 17 files changed, 1 insertion(+), 619 deletions(-) delete mode 100644 app/other/grim_reefer/CMakeLists.txt delete mode 100644 app/other/grim_reefer/Kconfig delete mode 100644 app/other/grim_reefer/boards/grim_reefer.overlay delete mode 100644 app/other/grim_reefer/include/adc_reading.h delete mode 100644 app/other/grim_reefer/include/calibration.h delete mode 100644 app/other/grim_reefer/include/config.h delete mode 100644 app/other/grim_reefer/include/control.h delete mode 100644 app/other/grim_reefer/include/flash_storage.h delete mode 100644 app/other/grim_reefer/prj.conf delete mode 100644 app/other/grim_reefer/sample.yaml delete mode 100644 app/other/grim_reefer/src/adc_reading.c delete mode 100644 app/other/grim_reefer/src/calibration.c delete mode 100644 app/other/grim_reefer/src/control.c delete mode 100644 app/other/grim_reefer/src/flash_storage.c delete mode 100644 app/other/grim_reefer/src/main.c delete mode 100644 app/other/grim_reefer/src/shell_cmds.c diff --git a/app/other/grim_reefer/CMakeLists.txt b/app/other/grim_reefer/CMakeLists.txt deleted file mode 100644 index a3208d3f3..000000000 --- a/app/other/grim_reefer/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.20.0) - -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) -project(grim_reefer LANGUAGES C) - -FILE(GLOB sources src/*.c) -target_sources(app PRIVATE ${sources}) -target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) \ No newline at end of file diff --git a/app/other/grim_reefer/Kconfig b/app/other/grim_reefer/Kconfig deleted file mode 100644 index 0ffe17553..000000000 --- a/app/other/grim_reefer/Kconfig +++ /dev/null @@ -1 +0,0 @@ -source "Kconfig.zephyr" \ No newline at end of file diff --git a/app/other/grim_reefer/boards/grim_reefer.overlay b/app/other/grim_reefer/boards/grim_reefer.overlay deleted file mode 100644 index d07393126..000000000 --- a/app/other/grim_reefer/boards/grim_reefer.overlay +++ /dev/null @@ -1,46 +0,0 @@ -/delete-node/ &adc; - -/ { - zephyr,user { - io-channels = <&adc 0>, <&adc 0>; - }; - - aliases { - adc0 = &adc; - // buzzer = &led1; - }; -}; - -&spi2 { - status = "okay"; - adc: mcp3561@0 { // new and cool driver - compatible = "microchip,mcp356xr"; - status = "okay"; - reg = <0>; - #io-channel-cells = <1>; - #address-cells = <1>; - #size-cells = <0>; - address = <1>; - analog-clock-prescaler = <0>; - boost-current-bias = <0>; - spi-max-frequency = ; - irq-gpios = <&gpiob 14 GPIO_ACTIVE_LOW>; - use-internal-clock; - - channel@0 { - reg = <0>; - zephyr,gain = "ADC_GAIN_8"; - zephyr,reference = "ADC_REF_INTERNAL"; - zephyr,acquisition-time = ; - - zephyr,resolution = <24>; - - zephyr,differential; - - zephyr,input-positive = <0x1>; - zephyr,input-negative = <0x0>; - - zephyr,oversampling = <8>; - }; - }; -}; \ No newline at end of file diff --git a/app/other/grim_reefer/include/adc_reading.h b/app/other/grim_reefer/include/adc_reading.h deleted file mode 100644 index d234aabfe..000000000 --- a/app/other/grim_reefer/include/adc_reading.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef ADC_READING_H -#define ADC_READING_H - -#include - -struct adc_sample { - uint32_t timestamp; - uint32_t value; -}; - -int adc_init(); -void adc_reading_task(); -void adc_start_reading(); -void adc_stop_recording(); - -#endif // ADC_READING_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/calibration.h b/app/other/grim_reefer/include/calibration.h deleted file mode 100644 index 4fbda050c..000000000 --- a/app/other/grim_reefer/include/calibration.h +++ /dev/null @@ -1,5 +0,0 @@ -#ifndef CALIBRATION_H -#define CALIBRATION_H - - -#endif // CALIBRATION_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/config.h b/app/other/grim_reefer/include/config.h deleted file mode 100644 index c15809da3..000000000 --- a/app/other/grim_reefer/include/config.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef REEFER_INCLUDE_CONFIG_H -#define REEFER_INCLUDE_CONFIG_H - -#define SAMPLE_RATE_HZ 1000 -#define TEST_DURATION 5 -#define MAX_TESTS 30 - -#define STORAGE_THREAD_PRIORITY 1 -#define ADC_READ_PRIORITY -1 -#define THREAD_START_DELAY 100 - -#define ADC_BUFFER_SIZE (SAMPLE_RATE_HZ* TEST_DURATION_SEC) -#define FLASH_BLOCK_SIZE (ADC_BUFFER_SIZE* sizeof(uint32_t)) - -#endif // REEFER_INCLUDE_CONFIG_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/control.h b/app/other/grim_reefer/include/control.h deleted file mode 100644 index 3f9c3195c..000000000 --- a/app/other/grim_reefer/include/control.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef CONTROL_H -#define CONTROL_H - -#include -#include - -void control_init(); -void control_start_test(); -void control_stop_test(); -void control_dump_data(const struct shell *shell); -bool control_is_running(); - -#endif // CONTROL_H \ No newline at end of file diff --git a/app/other/grim_reefer/include/flash_storage.h b/app/other/grim_reefer/include/flash_storage.h deleted file mode 100644 index dc8b95d82..000000000 --- a/app/other/grim_reefer/include/flash_storage.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef FLASH_STORAGE_H -#define FLASH_STORAGE_H - -#include - -int start_flash_storage(); -void stop_flash_storage(); -int flash_dump_all(const struct shell *shell); - -extern struct k_msgq storage_control_queue; - -#endif // FLASH_STORAGE_H \ No newline at end of file diff --git a/app/other/grim_reefer/prj.conf b/app/other/grim_reefer/prj.conf deleted file mode 100644 index 28445e183..000000000 --- a/app/other/grim_reefer/prj.conf +++ /dev/null @@ -1,38 +0,0 @@ -CONFIG_DEBUG=y -CONFIG_DEBUG_THREAD_INFO=y - -CONFIG_LOG=y -CONFIG_LOG_MODE_IMMEDIATE=y -CONFIG_CRC=y - -CONFIG_SMF=y -CONFIG_EVENTS=y -CONFIG_BASE64=y - -CONFIG_GPIO=y -CONFIG_SPI=y -CONFIG_I2C=y - -CONFIG_ADC=y -CONFIG_ADC_LOG_LEVEL_DBG=y -# CONFIG_MCP356X=n # old driver -CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr -CONFIG_ADC_MCP356XR_POLL=y -CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 -CONFIG_INA260=y - -CONFIG_SERIAL=y -CONFIG_UART_INTERRUPT_DRIVEN=y -CONFIG_CBPRINTF_FP_SUPPORT=y - -CONFIG_SPI_NOR_SFDP_RUNTIME=y -CONFIG_FLASH=y -CONFIG_FLASH_MAP=y -CONFIG_FLASH_PAGE_LAYOUT=y -CONFIG_DISK_ACCESS=y -CONFIG_FILE_SYSTEM=y - -CONFIG_SHELL=y -CONFIG_FLASH_SHELL=y -CONFIG_SHELL_BACKEND_SERIAL=y -CONFIG_UART_CONSOLE=y diff --git a/app/other/grim_reefer/sample.yaml b/app/other/grim_reefer/sample.yaml deleted file mode 100644 index 169ef60c1..000000000 --- a/app/other/grim_reefer/sample.yaml +++ /dev/null @@ -1,9 +0,0 @@ -sample: - description: - name: grim_reefer -common: - build_only: true - platform_allow: - - grim_reefer -tests: - solids_test.default: {} diff --git a/app/other/grim_reefer/src/adc_reading.c b/app/other/grim_reefer/src/adc_reading.c deleted file mode 100644 index 2a88ed866..000000000 --- a/app/other/grim_reefer/src/adc_reading.c +++ /dev/null @@ -1,119 +0,0 @@ -#include "adc_reading.h" -#include "config.h" -#include "flash_storage.h" - -#include -#include -#include -#include -#include -#include - -LOG_MODULE_REGISTER(adc_reader, LOG_LEVEL_INF); - -#define ADC_NODE DT_ALIAS(adc0) - -#if !DT_NODE_HAS_STATUS(ADC_NODE, okay) -#error "No ADC node found" -#endif - -static const struct device *adc_dev = DEVICE_DT_GET(ADC_NODE); - -extern struct k_msgq adc_data_queue; - -#define BEGIN_READING_EVENT 1 -#define STOP_READING_EVENT 2 - -static K_EVENT_DEFINE(adc_control_event); -static K_TIMER_DEFINE(adc_timer, NULL, NULL); - -void adc_reading_task(void); - -K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, ADC_READ_PRIORITY, 0, THREAD_START_DELAY); - -static uint32_t adc_buffer; -static struct adc_sequence sequence = { - .buffer = &adc_buffer, - .buffer_size = sizeof(adc_buffer), - .resolution = 24 -}; - -#define DT_SPEC_AND_COMMA(node_id, prop, idx) \ - ADC_DT_SPEC_GET_BY_IDX(node_id, idx), - -static const struct adc_dt_spec adc_channels[] = { - DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA) -}; - -int adc_init(){ - if(!adc_is_ready_dt(&adc_channels[0])){ - LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); - return 0; - } - - int err = adc_channel_setup_dt(&adc_channels[0]); - if(err < 0){ - LOG_ERR("Could not setup channel (%d)\n", err); - return 0; - } - - LOG_INF("ADC initialized"); - return 0; -} - -void adc_reading_task(){ - int ret; - uint32_t adc_val = 0; - struct adc_sample sample = {0}; - - LOG_INF("ADC reading task waiting to start..."); - - // Wait for start event - k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); - - LOG_INF("ADC reading started"); - - k_timer_start(&adc_timer, K_MSEC(1), K_MSEC(1)); // 1kHz loop - - int x = 0; - - while(true){ - uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); - if(events & STOP_READING_EVENT){ - break; - } - - k_timer_status_sync(&adc_timer); - - sequence.buffer = &adc_val; - sequence.buffer_size = sizeof(adc_val); - - // Read from ADC - (void)adc_sequence_init_dt(&adc_channels[0], &sequence); - ret = adc_read(adc_dev, &sequence); - if(ret < 0){ - LOG_ERR("ADC read failed (%d)", ret); - continue; - } - - sample.timestamp = k_uptime_get_32(); - sample.value = adc_val; - - if(x % 100 == 0){ - LOG_INF("sample value: %u", sample.value); - } - x++; - - k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); - } - - k_timer_stop(&adc_timer); -} - -void adc_start_reading(){ - k_event_set(&adc_control_event, BEGIN_READING_EVENT); -} - -void adc_stop_recording(){ - k_event_set(&adc_control_event, STOP_READING_EVENT); -} \ No newline at end of file diff --git a/app/other/grim_reefer/src/calibration.c b/app/other/grim_reefer/src/calibration.c deleted file mode 100644 index b12f85166..000000000 --- a/app/other/grim_reefer/src/calibration.c +++ /dev/null @@ -1,2 +0,0 @@ -#include "calibration.h" -#include "config.h" \ No newline at end of file diff --git a/app/other/grim_reefer/src/control.c b/app/other/grim_reefer/src/control.c deleted file mode 100644 index 2d6cefa63..000000000 --- a/app/other/grim_reefer/src/control.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "control.h" -#include "config.h" -#include "adc_reading.h" -#include "flash_storage.h" - -#include -#include -#include -#include - -LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); - -static int test_number = 0; -static bool test_running = false; - -void control_init(){ - LOG_INF("Initializing..."); - adc_init(); -} - -void control_start_test(){ - if(test_running){ - LOG_WRN("Test already running"); - return; - } - - LOG_INF("Starting test run #%d", test_number + 1); - test_running = true; - - start_flash_storage(); - adc_start_reading(); - - test_number++; - - // Run test for 10 seconds - k_sleep(K_SECONDS(TEST_DURATION)); - control_stop_test(); -} - -void control_stop_test(){ - if(!test_running){ - LOG_WRN("No test running"); - return; - } - - LOG_INF("Stopping test..."); - adc_stop_recording(); - stop_flash_storage(); - test_running = false; -} - -void control_dump_data(const struct shell *shell){ - flash_dump_all(shell); -} - -bool control_is_running(){ - return test_running; -} \ No newline at end of file diff --git a/app/other/grim_reefer/src/flash_storage.c b/app/other/grim_reefer/src/flash_storage.c deleted file mode 100644 index 3f2ced009..000000000 --- a/app/other/grim_reefer/src/flash_storage.c +++ /dev/null @@ -1,211 +0,0 @@ -#include "flash_storage.h" -#include "config.h" -#include "adc_reading.h" - -#include -#include -#include -#include -#include -#include -#include - -LOG_MODULE_REGISTER(flash_storage, LOG_LEVEL_INF); - -#define SPI_FLASH_ADDR 0x00000000 -#define FLASH_METADATA_ADDR SPI_FLASH_ADDR -#define SPI_FLASH_BLOCK_SIZE (64 * 1024) // 64KB -#define FLASH_METADATA_SIZE (4 * 1024) // 4KB -#define SPI_FLASH_START_ADDR (FLASH_METADATA_ADDR + FLASH_METADATA_SIZE) - -enum storage_event {BEGIN_STORAGE, END_STORAGE}; - -K_MSGQ_DEFINE(storage_control_queue, sizeof(enum storage_event), 5, 1); - -K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), SAMPLE_RATE_HZ * TEST_DURATION, 4); - -static const struct device *flash_dev = DEVICE_DT_GET(DT_ALIAS(storage)); -static uint32_t current_test_number = 0; -static off_t current_write_addr = 0; - -static void flash_storage_thread_entry(void); - -K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, - STORAGE_THREAD_PRIORITY, 0, 1000); - -// Check if flash block is all 0xFF -static bool flash_block_is_empty(off_t addr){ - uint8_t buf[16]; - if(flash_read(flash_dev, addr, buf, sizeof(buf)) < 0){ - LOG_ERR("Flash read failed"); - return false; - } - - for(int i = 0; i < sizeof(buf); i++){ - if(buf[i] != 0xFF){ - return false; - } - } - return true; -} - -// Get and update current test number -static void load_metadata(){ - uint8_t buf[4]; - if(flash_read(flash_dev, FLASH_METADATA_ADDR, buf, sizeof(buf)) < 0){ - LOG_ERR("Failed to read metadata"); - current_test_number = 0; - return; - } - - // If all are erased (0xFF), assume no tests yet - if(buf[0] == 0xFF && buf[1] == 0xFF && buf[2] == 0xFF && buf[3] == 0xFF){ - current_test_number = 0; - } else { - memcpy(¤t_test_number, buf, sizeof(current_test_number)); - } - - LOG_INF("Loaded test number %d", current_test_number); -} - -static void save_metadata(){ - int ret; - ret = flash_erase(flash_dev, FLASH_METADATA_ADDR, FLASH_METADATA_SIZE); - - if(ret < 0){ - LOG_ERR("flash_erase(metadata) failed: %d", ret); - return; - } - - ret = flash_write(flash_dev, FLASH_METADATA_ADDR, ¤t_test_number, sizeof(current_test_number)); - - if(ret < 0){ - LOG_ERR("flash_write(metadata) failed: %d", ret); - } else { - LOG_INF("Saved current_test_number = %u", current_test_number); - } -} - -static off_t get_test_block_addr(uint32_t test_index){ - return (off_t)(SPI_FLASH_START_ADDR + (test_index * SPI_FLASH_BLOCK_SIZE)); -} - -static void flash_storage_thread_entry(){ - struct adc_sample sample; - enum storage_event event; - - if(!device_is_ready(flash_dev)){ - LOG_ERR("Flash device not ready"); - return; - } - - // Get current test number - load_metadata(); - - while(1){ - // Wait for start command - k_msgq_get(&storage_control_queue, &event, K_FOREVER); - if (event != BEGIN_STORAGE) continue; - - if(current_test_number >= MAX_TESTS){ - LOG_ERR("Maximum number of test reached"); - continue; - } - - off_t test_block_addr = get_test_block_addr(current_test_number); - LOG_INF("Starting test %u at addr 0x%08lx", current_test_number, (long)test_block_addr); - - // Erase block - int ret = flash_erase(flash_dev, test_block_addr, SPI_FLASH_BLOCK_SIZE); - if(ret < 0){ - LOG_ERR("flash_erase(test block) failed: %d", ret); - continue; - } - - current_write_addr = test_block_addr; - - while(1){ - if(k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE){ - LOG_INF("Test %d complete", current_test_number); - current_test_number++; - save_metadata(); - break; - } - - if(k_msgq_get(&adc_data_queue, &sample, K_MSEC(50)) == 0){ - int ret = flash_write(flash_dev, current_write_addr, &sample, sizeof(sample)); - if(ret < 0){ - LOG_ERR("Flash write failed (%d)", ret); - } else { - current_write_addr += sizeof(sample); - } - } - } - } -} - -int start_flash_storage(){ - enum storage_event event = BEGIN_STORAGE; - return k_msgq_put(&storage_control_queue, &event, K_FOREVER); -} - -void stop_flash_storage(){ - enum storage_event event = END_STORAGE; - k_msgq_put(&storage_control_queue, &event, K_FOREVER); -} - -int flash_erase_all(); - -int flash_dump_all(const struct shell *shell){ - struct adc_sample sample; - uint32_t test_index = 0; - - if(!device_is_ready(flash_dev)){ - shell_error(shell, "Flash not ready"); - return -1; - } - - for(test_index = 0; test_index < current_test_number; test_index++){ - off_t block_addr = get_test_block_addr(test_index); - if (flash_block_is_empty(block_addr)) continue; - - shell_print(shell, "Dumping Test %d:", test_index); - off_t addr = block_addr; - - for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ - if(flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ - shell_print(shell, "Flash read failed"); - break; - } - if(sample.value == 0xFFFF && sample.timestamp == 0xFFFFFFFF){ - shell_print(shell, "Flash block empty"); - break; - } - shell_print(shell, "%u,%u", sample.timestamp, sample.value); - addr += sizeof(sample); - } - } - - flash_erase_all(); - - return 0; -} - -int flash_erase_all(){ - for(uint32_t i = 0; i < MAX_TESTS; i++){ - off_t curr_add = get_test_block_addr(i); - int ret = flash_erase(flash_dev, curr_add, SPI_FLASH_BLOCK_SIZE); - if(ret < 0){ - LOG_ERR("flash_erase failed: %d", ret); - continue; - } - else{ - LOG_INF("Flash block %d erased", i); - } - } - - current_test_number = 0; - save_metadata(); - - return 0; -} \ No newline at end of file diff --git a/app/other/grim_reefer/src/main.c b/app/other/grim_reefer/src/main.c deleted file mode 100644 index ad389686e..000000000 --- a/app/other/grim_reefer/src/main.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "control.h" - -#include -#include -#include -#include - -LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); - -int main(void) -{ - LOG_INF("Solids Test Start"); - - control_init(); - - LOG_INF("Use 'test start' to begin test"); - LOG_INF("Commands: test start | test stop | test dump"); - - while(1){ - k_sleep(K_MSEC(10)); - } - - return 0; -} \ No newline at end of file diff --git a/app/other/grim_reefer/src/shell_cmds.c b/app/other/grim_reefer/src/shell_cmds.c deleted file mode 100644 index aa1b55460..000000000 --- a/app/other/grim_reefer/src/shell_cmds.c +++ /dev/null @@ -1,41 +0,0 @@ -#include "control.h" -#include "flash_storage.h" -#include "adc_reading.h" - -#include -#include - -LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); - -static int cmd_test_start(const struct shell *shell, size_t argc, char **argv){ - ARG_UNUSED(argc); - ARG_UNUSED(argv); - shell_print(shell, "Starting test..."); - control_start_test(); - return 0; -} - -static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv){ - ARG_UNUSED(argc); - ARG_UNUSED(argv); - shell_print(shell, "Stopping test..."); - control_stop_test(); - return 0; -} - -static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv){ - ARG_UNUSED(argc); - ARG_UNUSED(argv); - shell_print(shell, "Dumping stored test data..."); - control_dump_data(shell); - return 0; -} - -SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, - SHELL_CMD(start, NULL, "Start test", cmd_test_start), - SHELL_CMD(stop, NULL, "Stop test", cmd_test_stop), - SHELL_CMD(dump, NULL, "Dump all flash data", cmd_test_dump), - SHELL_SUBCMD_SET_END -); - -SHELL_CMD_REGISTER(test, &sub_test, "Solids Test Board control commands", NULL); \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 48a718516..533115746 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -22,7 +22,7 @@ enum storage_event {BEGIN_STORAGE, END_STORAGE}; K_MSGQ_DEFINE(storage_control_queue, sizeof(enum storage_event), 5, 1); -K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), SAMPLE_RATE_HZ * TEST_DURATION, 4); +K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), 100, 4); static const struct device *flash_dev = DEVICE_DT_GET(DT_ALIAS(storage)); static uint32_t current_test_number = 0; From 4ffc9bbbd4896bd0da0e3e99d05c31bd5dcf451a Mon Sep 17 00:00:00 2001 From: cowsed Date: Thu, 23 Oct 2025 22:25:53 -0400 Subject: [PATCH 12/43] erase command, writing in blocks --- .../solids_test/boards/grim_reefer.overlay | 2 +- app/other/solids_test/include/config.h | 3 - app/other/solids_test/include/control.h | 1 + app/other/solids_test/include/flash_storage.h | 1 + app/other/solids_test/prj.conf | 13 ++ app/other/solids_test/src/adc_reading.c | 6 +- app/other/solids_test/src/control.c | 6 +- app/other/solids_test/src/flash_storage.c | 170 +++++++++--------- app/other/solids_test/src/shell_cmds.c | 37 ++-- 9 files changed, 131 insertions(+), 108 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 3674142cf..9b1c8377b 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -7,7 +7,7 @@ aliases { adc0 = &adc; - // buzzer = &led1; + buzzer = &led1; }; }; diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index e45cd1cf4..fcbf5eb01 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -9,7 +9,4 @@ #define ADC_READ_PRIORITY -1 #define THREAD_START_DELAY 100 -#define ADC_BUFFER_SIZE (SAMPLE_RATE_HZ* TEST_DURATION_SEC) -#define FLASH_BLOCK_SIZE (ADC_BUFFER_SIZE* sizeof(uint32_t)) - #endif // REEFER_INCLUDE_CONFIG_H \ No newline at end of file diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 06c16b3ed..3946ae428 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -9,6 +9,7 @@ void control_start_test(); void control_stop_test(); void control_dump_data(const struct shell *shell); void control_dump_one(const struct shell *shell, uint32_t test_index); +void control_erase_all(); bool control_is_running(); #endif // CONTROL_H \ No newline at end of file diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index aef753476..9eaebdba2 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -7,6 +7,7 @@ int start_flash_storage(); void stop_flash_storage(); int flash_dump_all(const struct shell *shell); int flash_dump_one(const struct shell *shell, uint32_t test_index); +int flash_erase_all(const struct shell *shell); extern struct k_msgq storage_control_queue; diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index c2c824cda..9d7c89219 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -1,6 +1,17 @@ +#CONFIG_DEBUG=y +#CONFIG_DEBUG_THREAD_INFO=y CONFIG_DEBUG=y +CONFIG_THREAD_NAME=y CONFIG_DEBUG_THREAD_INFO=y +CONFIG_THREAD_ANALYZER=y +CONFIG_THREAD_ANALYZER_USE_PRINTK=y +CONFIG_THREAD_ANALYZER_AUTO=y +CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=15 + +CONFIG_THREAD_ANALYZER_AUTO_STACK_SIZE=2048 + + CONFIG_LOG=y CONFIG_LOG_MODE_IMMEDIATE=y CONFIG_CRC=y @@ -25,6 +36,8 @@ CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_SPI_NOR_SFDP_RUNTIME=y +CONFIG_SPI_NOR_SLEEP_ERASE_MS=10 + CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_FLASH_PAGE_LAYOUT=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 0746db180..6b6b21aef 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -102,9 +102,9 @@ void adc_reading_task(){ sample.timestamp = k_uptime_get() - start_time; sample.value = adc_val; - if(x % 25 == 0){ - LOG_INF("sample value: %u", sample.value); - } + // if(x % 25 == 0){ + // LOG_INF("sample value: %u", sample.value); + // } x++; k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index bca2e1ea5..c95da2e3e 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -24,7 +24,7 @@ void control_start_test(){ return; } - LOG_INF("Starting test run #%d", test_number + 1); + LOG_INF("Starting test run"); test_running = true; start_flash_storage(); @@ -57,6 +57,10 @@ void control_dump_one(const struct shell *shell, uint32_t test_index){ flash_dump_one(shell, test_index); } +void control_erase_all(const struct shell *shell){ + flash_erase_all(shell); +} + bool control_is_running(){ return test_running; } \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 533115746..04575e6ed 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -1,28 +1,32 @@ #include "flash_storage.h" -#include "config.h" + #include "adc_reading.h" +#include "config.h" -#include +#include +#include +#include #include #include +#include #include #include -#include -#include LOG_MODULE_REGISTER(flash_storage, LOG_LEVEL_INF); -#define SPI_FLASH_ADDR 0x00000000 -#define FLASH_METADATA_ADDR SPI_FLASH_ADDR -#define SPI_FLASH_BLOCK_SIZE (64 * 2048) // 128KB -#define FLASH_METADATA_SIZE (4 * 1024) // 4KB -#define SPI_FLASH_START_ADDR (FLASH_METADATA_ADDR + FLASH_METADATA_SIZE) +#define SPI_FLASH_ADDR 0x00000000 +#define FLASH_METADATA_ADDR SPI_FLASH_ADDR +#define PAGE_SIZE 256 +#define SAMPLE_PER_PAGE (PAGE_SIZE / sizeof(struct adc_sample)) +#define SPI_FLASH_BLOCK_SIZE (64 * 2048) // 128KB +#define FLASH_METADATA_SIZE (4 * 1024) // 4KB +#define SPI_FLASH_START_ADDR (FLASH_METADATA_ADDR + FLASH_METADATA_SIZE) -enum storage_event {BEGIN_STORAGE, END_STORAGE}; +enum storage_event { BEGIN_STORAGE, END_STORAGE }; K_MSGQ_DEFINE(storage_control_queue, sizeof(enum storage_event), 5, 1); -K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), 100, 4); +K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), 1000, alignof(struct adc_sample)); static const struct device *flash_dev = DEVICE_DT_GET(DT_ALIAS(storage)); static uint32_t current_test_number = 0; @@ -30,19 +34,18 @@ static off_t current_write_addr = 0; static void flash_storage_thread_entry(void); -K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, - STORAGE_THREAD_PRIORITY, 0, 1000); +K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, STORAGE_THREAD_PRIORITY, 0, 1000); // Check if flash block is all 0xFF -static bool flash_block_is_empty(off_t addr){ +static bool flash_block_is_empty(off_t addr) { uint8_t buf[16]; - if(flash_read(flash_dev, addr, buf, sizeof(buf)) < 0){ + if (flash_read(flash_dev, addr, buf, sizeof(buf)) < 0) { LOG_ERR("Flash read failed"); return false; } - for(int i = 0; i < sizeof(buf); i++){ - if(buf[i] != 0xFF){ + for (int i = 0; i < sizeof(buf); i++) { + if (buf[i] != 0xFF) { return false; } } @@ -50,16 +53,16 @@ static bool flash_block_is_empty(off_t addr){ } // Get and update current test number -static void load_metadata(){ +static void load_metadata() { uint8_t buf[4]; - if(flash_read(flash_dev, FLASH_METADATA_ADDR, buf, sizeof(buf)) < 0){ + if (flash_read(flash_dev, FLASH_METADATA_ADDR, buf, sizeof(buf)) < 0) { LOG_ERR("Failed to read metadata"); current_test_number = 0; return; } // If all are erased (0xFF), assume no tests yet - if(buf[0] == 0xFF && buf[1] == 0xFF && buf[2] == 0xFF && buf[3] == 0xFF){ + if (buf[0] == 0xFF && buf[1] == 0xFF && buf[2] == 0xFF && buf[3] == 0xFF) { current_test_number = 0; } else { memcpy(¤t_test_number, buf, sizeof(current_test_number)); @@ -68,33 +71,32 @@ static void load_metadata(){ LOG_INF("Loaded test number %d", current_test_number); } -static void save_metadata(){ +static void save_metadata() { int ret; ret = flash_erase(flash_dev, FLASH_METADATA_ADDR, FLASH_METADATA_SIZE); - if(ret < 0){ + if (ret < 0) { LOG_ERR("flash_erase(metadata) failed: %d", ret); return; } - + ret = flash_write(flash_dev, FLASH_METADATA_ADDR, ¤t_test_number, sizeof(current_test_number)); - if(ret < 0){ + if (ret < 0) { LOG_ERR("flash_write(metadata) failed: %d", ret); } else { LOG_INF("Saved current_test_number = %u", current_test_number); } } -static off_t get_test_block_addr(uint32_t test_index){ - return (off_t)(SPI_FLASH_START_ADDR + (test_index * SPI_FLASH_BLOCK_SIZE)); +static off_t get_test_block_addr(uint32_t test_index) { + return (off_t) (SPI_FLASH_START_ADDR + (test_index * SPI_FLASH_BLOCK_SIZE)); } -static void flash_storage_thread_entry(){ - struct adc_sample sample; +static void flash_storage_thread_entry() { enum storage_event event; - - if(!device_is_ready(flash_dev)){ + + if (!device_is_ready(flash_dev)) { LOG_ERR("Flash device not ready"); return; } @@ -102,83 +104,104 @@ static void flash_storage_thread_entry(){ // Get current test number load_metadata(); - while(1){ + while (1) { // Wait for start command k_msgq_get(&storage_control_queue, &event, K_FOREVER); if (event != BEGIN_STORAGE) continue; - if(current_test_number >= MAX_TESTS){ + if (current_test_number >= MAX_TESTS) { LOG_ERR("Maximum number of test reached"); continue; } off_t test_block_addr = get_test_block_addr(current_test_number); - LOG_INF("Starting test %u at addr 0x%08lx", current_test_number, (long)test_block_addr); + LOG_INF("Starting test %u at addr 0x%08lx", current_test_number, (long) test_block_addr); // Erase block int ret = flash_erase(flash_dev, test_block_addr, SPI_FLASH_BLOCK_SIZE); - if(ret < 0){ + if (ret < 0) { LOG_ERR("flash_erase(test block) failed: %d", ret); continue; } current_write_addr = test_block_addr; - while(1){ - if(k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE){ + static struct adc_sample page[SAMPLE_PER_PAGE] = {0}; + size_t i = 0; + size_t pages = 0; + while (1) { + if (k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE) { LOG_INF("Test %d complete", current_test_number); current_test_number++; save_metadata(); break; } - - if(k_msgq_get(&adc_data_queue, &sample, K_MSEC(50)) == 0){ - int ret = flash_write(flash_dev, current_write_addr, &sample, sizeof(sample)); - if(ret < 0){ + struct adc_sample *target = &page[i % SAMPLE_PER_PAGE]; + if (k_msgq_get(&adc_data_queue, target, K_MSEC(50)) == 0) { + i++; + } + if (i % SAMPLE_PER_PAGE == 0 && i != 0) { + int ret = flash_write(flash_dev, current_write_addr, page, PAGE_SIZE); + if (ret < 0) { LOG_ERR("Flash write failed (%d)", ret); } else { - current_write_addr += sizeof(sample); + current_write_addr += PAGE_SIZE; + pages++; } } } + size_t j = i % SAMPLE_PER_PAGE; + while (j < SAMPLE_PER_PAGE) { + page[j] = (struct adc_sample) {0xFFFFFFFF, 0xFFFFFFFF}; + j++; + } + ret = flash_write(flash_dev, current_write_addr, page, PAGE_SIZE); + if (ret < 0) { + LOG_ERR("Final page flash write failed (%d)", ret); + } else { + current_write_addr += PAGE_SIZE; + pages++; + } + + LOG_INF("Saved %u packets to %u pages", i, pages); } } -int start_flash_storage(){ +int start_flash_storage() { enum storage_event event = BEGIN_STORAGE; return k_msgq_put(&storage_control_queue, &event, K_FOREVER); } -void stop_flash_storage(){ +void stop_flash_storage() { enum storage_event event = END_STORAGE; k_msgq_put(&storage_control_queue, &event, K_FOREVER); } int flash_erase_all(); -int flash_dump_one(const struct shell *shell, uint32_t test_index){ +int flash_dump_one(const struct shell *shell, uint32_t test_index) { struct adc_sample sample; - if(!device_is_ready(flash_dev)){ + if (!device_is_ready(flash_dev)) { shell_error(shell, "Flash not ready"); return -1; } off_t block_addr = get_test_block_addr(test_index); - if(flash_block_is_empty(block_addr)){ + if (flash_block_is_empty(block_addr)) { shell_print(shell, "Flash block empty"); return 0; } shell_print(shell, "Dumping Test %d:", test_index); - for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ - if(flash_read(flash_dev, block_addr, &sample, sizeof(sample)) < 0){ + for (int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++) { + if (flash_read(flash_dev, block_addr, &sample, sizeof(sample)) < 0) { shell_print(shell, "Flash read failed"); break; - } - if(sample.value == 0xFFFFFFFF && sample.timestamp == 0xFFFFFFFF){ - shell_print(shell, "Flash block unwritten"); + } + if (sample.value == 0xFFFFFFFF && sample.timestamp == 0xFFFFFFFF) { + shell_print(shell, "Flash block unwritten. Read %d packets", i); break; } shell_print(shell, "%u,%u", sample.timestamp, sample.value); @@ -188,51 +211,28 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index){ return 0; } -int flash_dump_all(const struct shell *shell){ - struct adc_sample sample; - uint32_t test_index = 0; - - if(!device_is_ready(flash_dev)){ +int flash_dump_all(const struct shell *shell) { + if (!device_is_ready(flash_dev)) { shell_error(shell, "Flash not ready"); return -1; } - for(test_index = 0; test_index < current_test_number; test_index++){ - off_t block_addr = get_test_block_addr(test_index); - if (flash_block_is_empty(block_addr)) continue; - - shell_print(shell, "Dumping Test %d:", test_index); - off_t addr = block_addr; - - for(int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++){ - if(flash_read(flash_dev, addr, &sample, sizeof(sample)) < 0){ - shell_print(shell, "Flash read failed"); - break; - } - if(sample.value == 0xFFFFFFFF && sample.timestamp == 0xFFFFFFFF){ - shell_print(shell, "Flash block unwritten"); - break; - } - shell_print(shell, "%u,%u", sample.timestamp, sample.value); - addr += sizeof(sample); - } + for (uint32_t test_index = 0; test_index < MAX_TESTS; test_index++) { + flash_dump_one(shell, test_index); } - flash_erase_all(); - return 0; } -int flash_erase_all(){ - for(uint32_t i = 0; i < MAX_TESTS; i++){ +int flash_erase_all(const struct shell *shell) { + for (uint32_t i = 0; i < MAX_TESTS; i++) { off_t curr_add = get_test_block_addr(i); int ret = flash_erase(flash_dev, curr_add, SPI_FLASH_BLOCK_SIZE); - if(ret < 0){ - LOG_ERR("flash_erase failed: %d", ret); + if (ret < 0) { + shell_print(shell, "flash_erase failed: %d", ret); continue; - } - else{ - LOG_INF("Flash block %d erased", i); + } else { + shell_error(shell, "Flash block %d erased", i); } } @@ -240,4 +240,4 @@ int flash_erase_all(){ save_metadata(); return 0; -} \ No newline at end of file +} diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index e98f7d4ba..fc8000ec4 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -1,15 +1,15 @@ +#include "adc_reading.h" #include "control.h" #include "flash_storage.h" -#include "adc_reading.h" -#include -#include #include #include +#include +#include LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); -static int cmd_test_start(const struct shell *shell, size_t argc, char **argv){ +static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); shell_print(shell, "Starting test..."); @@ -17,7 +17,7 @@ static int cmd_test_start(const struct shell *shell, size_t argc, char **argv){ return 0; } -static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv){ +static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); shell_print(shell, "Stopping test..."); @@ -25,14 +25,22 @@ static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv){ return 0; } -static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv){ +static int cmd_test_erase(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); + shell_print(shell, "Erasing all test data......"); - if(argc == 1){ + control_erase_all(shell); + return 0; +} +static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv) { + ARG_UNUSED(argc); + ARG_UNUSED(argv); + + if (argc == 1) { shell_print(shell, "Dumping all test data..."); control_dump_data(shell); - } else if(argc == 2) { + } else if (argc == 2) { uint32_t test_num = atoi(argv[1]); shell_print(shell, "Dumping test %u data...", test_num); control_dump_one(shell, test_num); @@ -40,15 +48,14 @@ static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv){ shell_print(shell, "Enter or "); return -1; } - + return 0; } -SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, - SHELL_CMD(start, NULL, "Start test", cmd_test_start), - SHELL_CMD(stop, NULL, "Stop test", cmd_test_stop), - SHELL_CMD(dump, NULL, "Dump all flash data", cmd_test_dump), - SHELL_SUBCMD_SET_END -); +SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, SHELL_CMD(start, NULL, "Start test", cmd_test_start), + SHELL_CMD(stop, NULL, "Stop test preemptively", cmd_test_stop), + SHELL_CMD(dump, NULL, "Dump flash data. Optional arg [test #]", cmd_test_dump), + SHELL_CMD(erase, NULL, "Erase all flash data, prepare for new tests", cmd_test_erase), + SHELL_SUBCMD_SET_END); SHELL_CMD_REGISTER(test, &sub_test, "Solids Test Board control commands", NULL); \ No newline at end of file From 56739fc0d63682985b070c73e0ea7aa0f9efa3b0 Mon Sep 17 00:00:00 2001 From: cowsed Date: Thu, 23 Oct 2025 23:29:21 -0400 Subject: [PATCH 13/43] adc fine tuning/optimiaztion buzzer accidentally auto formatted (sorrry) --- .../solids_test/boards/grim_reefer.overlay | 14 ++-- app/other/solids_test/include/buzzer.h | 6 ++ app/other/solids_test/prj.conf | 15 +--- app/other/solids_test/src/adc_reading.c | 68 +++++++++---------- app/other/solids_test/src/buzzer.c | 51 ++++++++++++++ app/other/solids_test/src/flash_storage.c | 4 +- app/other/solids_test/src/main.c | 4 +- 7 files changed, 102 insertions(+), 60 deletions(-) create mode 100644 app/other/solids_test/include/buzzer.h create mode 100644 app/other/solids_test/src/buzzer.c diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 9b1c8377b..b1d973138 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -2,28 +2,29 @@ / { zephyr,user { - io-channels = <&adc 0>, <&adc 0>; + io-channels = <&adc 0>; }; aliases { adc0 = &adc; buzzer = &led1; + // buzzer = &buzzer; }; }; &spi2 { status = "okay"; adc: mcp3561@0 { // new and cool driver - compatible = "microchip,mcp356xr"; - status = "okay"; - reg = <0>; #io-channel-cells = <1>; #address-cells = <1>; #size-cells = <0>; + compatible = "microchip,mcp356xr"; + status = "okay"; + reg = <0>; address = <1>; analog-clock-prescaler = <0>; boost-current-bias = <0>; - spi-max-frequency = ; + spi-max-frequency = ; irq-gpios = <&gpiob 14 GPIO_ACTIVE_LOW>; use-internal-clock; @@ -36,11 +37,10 @@ zephyr,resolution = <24>; zephyr,differential; - zephyr,input-positive = <0x1>; zephyr,input-negative = <0x0>; - zephyr,oversampling = <8>; + zephyr,oversampling = <5>; }; }; }; \ No newline at end of file diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h new file mode 100644 index 000000000..b03243b22 --- /dev/null +++ b/app/other/solids_test/include/buzzer.h @@ -0,0 +1,6 @@ +void buzzer_init(); +void beep_full(); + +void test_start_beep(); +void test_end_beep(); + diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index 9d7c89219..b2b81aff6 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -1,19 +1,8 @@ -#CONFIG_DEBUG=y -#CONFIG_DEBUG_THREAD_INFO=y -CONFIG_DEBUG=y -CONFIG_THREAD_NAME=y -CONFIG_DEBUG_THREAD_INFO=y -CONFIG_THREAD_ANALYZER=y -CONFIG_THREAD_ANALYZER_USE_PRINTK=y -CONFIG_THREAD_ANALYZER_AUTO=y -CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=15 -CONFIG_THREAD_ANALYZER_AUTO_STACK_SIZE=2048 CONFIG_LOG=y -CONFIG_LOG_MODE_IMMEDIATE=y CONFIG_CRC=y CONFIG_SMF=y @@ -25,8 +14,8 @@ CONFIG_SPI=y CONFIG_I2C=y CONFIG_ADC=y -CONFIG_ADC_LOG_LEVEL_DBG=y -# CONFIG_MCP356X=n # old driver +CONFIG_ADC_LOG_LEVEL_ERR=y # Will spam annoying error at wrn level + CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 CONFIG_INA260=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 6b6b21aef..56e487716 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -1,13 +1,15 @@ #include "adc_reading.h" + +#include "buzzer.h" #include "config.h" #include "flash_storage.h" #include #include -#include +#include #include +#include #include -#include LOG_MODULE_REGISTER(adc_reader, LOG_LEVEL_INF); @@ -32,27 +34,21 @@ void adc_reading_task(void); K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, ADC_READ_PRIORITY, 0, THREAD_START_DELAY); static uint32_t adc_buffer; -static struct adc_sequence sequence = { - .buffer = &adc_buffer, - .buffer_size = sizeof(adc_buffer), - .resolution = 24 -}; +static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = sizeof(adc_buffer), .resolution = 24}; -#define DT_SPEC_AND_COMMA(node_id, prop, idx) \ - ADC_DT_SPEC_GET_BY_IDX(node_id, idx), +#define DT_SPEC_AND_COMMA(node_id, prop, idx) ADC_DT_SPEC_GET_BY_IDX(node_id, idx), static const struct adc_dt_spec adc_channels[] = { - DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA) -}; + DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; -int adc_init(){ - if(!adc_is_ready_dt(&adc_channels[0])){ +int adc_init() { + if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); return 0; } int err = adc_channel_setup_dt(&adc_channels[0]); - if(err < 0){ + if (err < 0) { LOG_ERR("Could not setup channel (%d)\n", err); return 0; } @@ -61,28 +57,30 @@ int adc_init(){ return 0; } -void adc_reading_task(){ +void adc_reading_task() { int ret; uint32_t adc_val = 0; struct adc_sample sample = {0}; - while(true){ + while (true) { LOG_INF("ADC reading task waiting to start..."); // Wait for start event k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); - + test_start_beep(); LOG_INF("ADC reading started"); - k_timer_start(&adc_timer, K_USEC(100), K_USEC(100)); // 1kHz loop + k_timer_start(&adc_timer, K_USEC(920), K_USEC(920)); // 1kHz loop int x = 0; - + uint32_t dropped_samples = 0; uint64_t start_time = k_uptime_get(); - - while(true){ - uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); - if(events & STOP_READING_EVENT){ + uint64_t total_adc_time = 0; + (void) adc_sequence_init_dt(&adc_channels[0], &sequence); + while (true) { + uint32_t events = + k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); + if (events & STOP_READING_EVENT) { break; } @@ -92,33 +90,31 @@ void adc_reading_task(){ sequence.buffer_size = sizeof(adc_val); // Read from ADC - (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + uint32_t start_read = k_uptime_get_32(); ret = adc_read(adc_dev, &sequence); - if(ret < 0){ + if (ret < 0) { LOG_ERR("ADC read failed (%d)", ret); continue; } + total_adc_time += k_uptime_get_32() - start_read; sample.timestamp = k_uptime_get() - start_time; sample.value = adc_val; - // if(x % 25 == 0){ - // LOG_INF("sample value: %u", sample.value); - // } x++; - k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT); + if (k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT) != 0) { + dropped_samples++; + } } k_timer_stop(&adc_timer); - LOG_INF("number of samples: %d", x); + LOG_INF("number of samples: %d, %u dropped, read time %llu, ms per = %.2f", x, dropped_samples, total_adc_time, + (double) total_adc_time / (double) x); + test_end_beep(); } } -void adc_start_reading(){ - k_event_set(&adc_control_event, BEGIN_READING_EVENT); -} +void adc_start_reading() { k_event_set(&adc_control_event, BEGIN_READING_EVENT); } -void adc_stop_recording(){ - k_event_set(&adc_control_event, STOP_READING_EVENT); -} \ No newline at end of file +void adc_stop_recording() { k_event_set(&adc_control_event, STOP_READING_EVENT); } \ No newline at end of file diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c new file mode 100644 index 000000000..98a9af104 --- /dev/null +++ b/app/other/solids_test/src/buzzer.c @@ -0,0 +1,51 @@ +#include +#include + +#define LDO_EN_NODE DT_NODELABEL(ldo_enable) + +#define CAM_EN_NODE DT_NODELABEL(cam_enable) + +const struct gpio_dt_spec ldo_enable = GPIO_DT_SPEC_GET(LDO_EN_NODE, gpios); +static const struct gpio_dt_spec buzzer = GPIO_DT_SPEC_GET(DT_ALIAS(buzzer), gpios); + +void set_buzz(int which) { + gpio_pin_set_dt(&buzzer, which); + gpio_pin_set_dt(&ldo_enable, which); +} + +void buzzer_init() { + int ret = gpio_pin_configure_dt(&buzzer, GPIO_OUTPUT_INACTIVE); + if (ret < 0) { + printk("Failed to conf buzzer pin:("); + return; + } +} +void beep_full() { + for (int i = 0; i < 10; i++) { + printk("BEEP"); + set_buzz(1); + k_msleep(1000); + set_buzz(0); + k_msleep(1000); + } +} + +void test_start_beep() { + for (int i = 0; i < 3; i++) { + printk("BEEP"); + set_buzz(1); + k_msleep(100); + set_buzz(0); + k_msleep(100); + } +} +void test_end_beep() { + printk("BEEP"); + set_buzz(1); + k_msleep(100); + set_buzz(0); + k_msleep(100); + set_buzz(1); + k_msleep(1000); + set_buzz(0); +} \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 04575e6ed..f96c63125 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -1,5 +1,5 @@ #include "flash_storage.h" - +#include "buzzer.h" #include "adc_reading.h" #include "config.h" @@ -111,6 +111,7 @@ static void flash_storage_thread_entry() { if (current_test_number >= MAX_TESTS) { LOG_ERR("Maximum number of test reached"); + beep_full(); continue; } @@ -177,7 +178,6 @@ void stop_flash_storage() { k_msgq_put(&storage_control_queue, &event, K_FOREVER); } -int flash_erase_all(); int flash_dump_one(const struct shell *shell, uint32_t test_index) { struct adc_sample sample; diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index ad389686e..c56c73c13 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -1,5 +1,5 @@ #include "control.h" - +#include "buzzer.h" #include #include #include @@ -12,7 +12,7 @@ int main(void) LOG_INF("Solids Test Start"); control_init(); - + buzzer_init(); LOG_INF("Use 'test start' to begin test"); LOG_INF("Commands: test start | test stop | test dump"); From 226d09586bd5b2401bdece3764cf7f62a923cb5c Mon Sep 17 00:00:00 2001 From: cowsed Date: Fri, 24 Oct 2025 17:00:24 -0400 Subject: [PATCH 14/43] instrumenting --- .../solids_test/boards/grim_reefer.overlay | 4 +-- app/other/solids_test/include/adc_reading.h | 2 +- app/other/solids_test/prj.conf | 15 ++++++++--- app/other/solids_test/src/adc_reading.c | 25 +++++++++++-------- app/other/solids_test/src/buzzer.c | 7 +++++- app/other/solids_test/src/flash_storage.c | 2 +- app/other/solids_test/src/main.c | 4 --- 7 files changed, 37 insertions(+), 22 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index b1d973138..6403fd149 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -30,7 +30,7 @@ channel@0 { reg = <0>; - zephyr,gain = "ADC_GAIN_64"; + zephyr,gain = "ADC_GAIN_2"; zephyr,reference = "ADC_REF_INTERNAL"; zephyr,acquisition-time = ; @@ -40,7 +40,7 @@ zephyr,input-positive = <0x1>; zephyr,input-negative = <0x0>; - zephyr,oversampling = <5>; + zephyr,oversampling = <7>; }; }; }; \ No newline at end of file diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index d234aabfe..6637833d3 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -5,7 +5,7 @@ struct adc_sample { uint32_t timestamp; - uint32_t value; + int32_t value; }; int adc_init(); diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index b2b81aff6..4ef41840f 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -1,6 +1,14 @@ - - - +CONFIG_THREAD_NAME=y +CONFIG_SEGGER_SYSTEMVIEW=y +CONFIG_USE_SEGGER_RTT=y #see point 1 above +CONFIG_TRACING=y +CONFIG_TRACING_BACKEND_RAM=y # see point 2 above + +CONFIG_SENSOR=y +CONFIG_SENSOR_SHELL=y +CONFIG_INA260=y +CONFIG_SPEED_OPTIMIZATIONS=y +CONFIG_SYS_CLOCK_TICKS_PER_SEC=100000 CONFIG_LOG=y CONFIG_CRC=y @@ -18,6 +26,7 @@ CONFIG_ADC_LOG_LEVEL_ERR=y # Will spam annoying error at wrn level CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 +CONFIG_ADC_MCP356XR_THREAD_PRIORITY=21 CONFIG_INA260=y CONFIG_SERIAL=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 56e487716..df3c92136 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -31,7 +31,7 @@ static K_TIMER_DEFINE(adc_timer, NULL, NULL); void adc_reading_task(void); -K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, ADC_READ_PRIORITY, 0, THREAD_START_DELAY); +K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, 15, 0, THREAD_START_DELAY); static uint32_t adc_buffer; static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = sizeof(adc_buffer), .resolution = 24}; @@ -70,14 +70,16 @@ void adc_reading_task() { test_start_beep(); LOG_INF("ADC reading started"); - k_timer_start(&adc_timer, K_USEC(920), K_USEC(920)); // 1kHz loop + k_timer_start(&adc_timer, K_USEC(1000), K_USEC(1000)); // 1kHz loop int x = 0; uint32_t dropped_samples = 0; - uint64_t start_time = k_uptime_get(); - uint64_t total_adc_time = 0; + uint64_t start_time_ticks = k_uptime_ticks(); + uint64_t total_adc_ticks = 0; (void) adc_sequence_init_dt(&adc_channels[0], &sequence); + uint64_t total_loop_ticks = 0; while (true) { + uint32_t start_loop_ticks = k_uptime_ticks(); uint32_t events = k_event_wait(&adc_control_event, BEGIN_READING_EVENT | STOP_READING_EVENT, false, K_NO_WAIT); if (events & STOP_READING_EVENT) { @@ -90,27 +92,30 @@ void adc_reading_task() { sequence.buffer_size = sizeof(adc_val); // Read from ADC - uint32_t start_read = k_uptime_get_32(); + uint32_t start_read = k_uptime_ticks(); ret = adc_read(adc_dev, &sequence); if (ret < 0) { LOG_ERR("ADC read failed (%d)", ret); continue; } - total_adc_time += k_uptime_get_32() - start_read; + total_adc_ticks += k_uptime_ticks() - start_read; - sample.timestamp = k_uptime_get() - start_time; + sample.timestamp = k_ticks_to_us_near32(k_uptime_ticks() - start_time_ticks); sample.value = adc_val; - + if (x % 100 == 0){ + printk("Reading: %d\n", sample.value); + } x++; if (k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT) != 0) { dropped_samples++; } + total_loop_ticks += k_uptime_ticks() - start_loop_ticks; } k_timer_stop(&adc_timer); - LOG_INF("number of samples: %d, %u dropped, read time %llu, ms per = %.2f", x, dropped_samples, total_adc_time, - (double) total_adc_time / (double) x); + LOG_INF("number of samples: %d, %u dropped, read time %llu, ms per = %.2f, loop time: %llu, loop time ticks: %llu", x, dropped_samples, + k_ticks_to_ms_near64(total_adc_ticks), (double) k_ticks_to_ms_near64(total_adc_ticks) / (double) x, k_ticks_to_ms_near64(total_loop_ticks), total_loop_ticks); test_end_beep(); } } diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 98a9af104..3663357bd 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -10,7 +10,6 @@ static const struct gpio_dt_spec buzzer = GPIO_DT_SPEC_GET(DT_ALIAS(buzzer), gpi void set_buzz(int which) { gpio_pin_set_dt(&buzzer, which); - gpio_pin_set_dt(&ldo_enable, which); } void buzzer_init() { @@ -19,6 +18,12 @@ void buzzer_init() { printk("Failed to conf buzzer pin:("); return; } + ret = gpio_pin_configure_dt(&ldo_enable, GPIO_OUTPUT_INACTIVE); + if (ret < 0) { + printk("Failed to conf ldo enable pin:("); + return; + } + gpio_pin_set_dt(&ldo_enable, 1); } void beep_full() { for (int i = 0; i < 10; i++) { diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index f96c63125..a5cf9a306 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -204,7 +204,7 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index) { shell_print(shell, "Flash block unwritten. Read %d packets", i); break; } - shell_print(shell, "%u,%u", sample.timestamp, sample.value); + shell_print(shell, "%u,%d", sample.timestamp, sample.value); block_addr += sizeof(sample); } diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index c56c73c13..2dc4c137e 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -16,9 +16,5 @@ int main(void) LOG_INF("Use 'test start' to begin test"); LOG_INF("Commands: test start | test stop | test dump"); - while(1){ - k_sleep(K_MSEC(10)); - } - return 0; } \ No newline at end of file From 170f331b3af511a6031c080884d4934f66962767 Mon Sep 17 00:00:00 2001 From: cowsed Date: Fri, 24 Oct 2025 18:06:36 -0400 Subject: [PATCH 15/43] DMAing it up --- app/other/solids_test/boards/grim_reefer.overlay | 2 +- app/other/solids_test/prj.conf | 12 +++++++----- app/other/solids_test/src/adc_reading.c | 3 --- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 6403fd149..c8582f942 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -30,7 +30,7 @@ channel@0 { reg = <0>; - zephyr,gain = "ADC_GAIN_2"; + zephyr,gain = "ADC_GAIN_32"; zephyr,reference = "ADC_REF_INTERNAL"; zephyr,acquisition-time = ; diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index 4ef41840f..f5e7ac703 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -1,8 +1,8 @@ -CONFIG_THREAD_NAME=y -CONFIG_SEGGER_SYSTEMVIEW=y -CONFIG_USE_SEGGER_RTT=y #see point 1 above -CONFIG_TRACING=y -CONFIG_TRACING_BACKEND_RAM=y # see point 2 above +#CONFIG_THREAD_NAME=y +#CONFIG_SEGGER_SYSTEMVIEW=y +#CONFIG_USE_SEGGER_RTT=y #see point 1 above +#CONFIG_TRACING=y +#CONFIG_TRACING_BACKEND_RAM=y # see point 2 above CONFIG_SENSOR=y CONFIG_SENSOR_SHELL=y @@ -17,6 +17,8 @@ CONFIG_SMF=y CONFIG_EVENTS=y CONFIG_BASE64=y +CONFIG_SPI_STM32_DMA=y + CONFIG_GPIO=y CONFIG_SPI=y CONFIG_I2C=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index df3c92136..a8e55eafb 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -102,9 +102,6 @@ void adc_reading_task() { sample.timestamp = k_ticks_to_us_near32(k_uptime_ticks() - start_time_ticks); sample.value = adc_val; - if (x % 100 == 0){ - printk("Reading: %d\n", sample.value); - } x++; if (k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT) != 0) { From 0a85a5d61a9df35b077ec5bea542e2742a267808 Mon Sep 17 00:00:00 2001 From: cowsed Date: Fri, 24 Oct 2025 18:24:23 -0400 Subject: [PATCH 16/43] ematches and reading misses --- app/other/solids_test/include/buzzer.h | 2 ++ app/other/solids_test/src/adc_reading.c | 25 ++++++++++++++++++++----- app/other/solids_test/src/buzzer.c | 15 +++++++++++++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index b03243b22..31ca683dc 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -4,3 +4,5 @@ void beep_full(); void test_start_beep(); void test_end_beep(); +void set_ematch(int level); +void set_ldo(int level); \ No newline at end of file diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index a8e55eafb..2211c6657 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -61,7 +61,6 @@ void adc_reading_task() { int ret; uint32_t adc_val = 0; struct adc_sample sample = {0}; - while (true) { LOG_INF("ADC reading task waiting to start..."); @@ -69,6 +68,8 @@ void adc_reading_task() { k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); test_start_beep(); LOG_INF("ADC reading started"); + set_ldo(1); + k_msleep(1); k_timer_start(&adc_timer, K_USEC(1000), K_USEC(1000)); // 1kHz loop @@ -78,6 +79,7 @@ void adc_reading_task() { uint64_t total_adc_ticks = 0; (void) adc_sequence_init_dt(&adc_channels[0], &sequence); uint64_t total_loop_ticks = 0; + uint32_t num_missed_expires = 0; while (true) { uint32_t start_loop_ticks = k_uptime_ticks(); uint32_t events = @@ -85,8 +87,17 @@ void adc_reading_task() { if (events & STOP_READING_EVENT) { break; } - - k_timer_status_sync(&adc_timer); + uint32_t num_expiries = k_timer_status_get(&adc_timer); + if (num_expiries == 0) { + k_timer_status_sync(&adc_timer); + } else { + num_missed_expires += num_expiries-1; + } + if (x == 250) { + set_ematch(1); + } else if (x == 500) { + set_ematch(0); + } sequence.buffer = &adc_val; sequence.buffer_size = sizeof(adc_val); @@ -110,9 +121,13 @@ void adc_reading_task() { total_loop_ticks += k_uptime_ticks() - start_loop_ticks; } + set_ldo(0); k_timer_stop(&adc_timer); - LOG_INF("number of samples: %d, %u dropped, read time %llu, ms per = %.2f, loop time: %llu, loop time ticks: %llu", x, dropped_samples, - k_ticks_to_ms_near64(total_adc_ticks), (double) k_ticks_to_ms_near64(total_adc_ticks) / (double) x, k_ticks_to_ms_near64(total_loop_ticks), total_loop_ticks); + LOG_INF( + "number of samples: %d, %u missed, %u dropped, read time %llu, ms per = %.2f, loop time: %llu, loop time ticks: %llu", + x, num_missed_expires, dropped_samples, k_ticks_to_ms_near64(total_adc_ticks), + (double) k_ticks_to_ms_near64(total_adc_ticks) / (double) x, k_ticks_to_ms_near64(total_loop_ticks), + total_loop_ticks); test_end_beep(); } } diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 3663357bd..d08e46167 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -2,16 +2,26 @@ #include #define LDO_EN_NODE DT_NODELABEL(ldo_enable) - #define CAM_EN_NODE DT_NODELABEL(cam_enable) const struct gpio_dt_spec ldo_enable = GPIO_DT_SPEC_GET(LDO_EN_NODE, gpios); static const struct gpio_dt_spec buzzer = GPIO_DT_SPEC_GET(DT_ALIAS(buzzer), gpios); +static const struct gpio_dt_spec ematch = GPIO_DT_SPEC_GET(CAM_EN_NODE, gpios); void set_buzz(int which) { + gpio_pin_set_dt(&ldo_enable, which); gpio_pin_set_dt(&buzzer, which); } +void set_ldo(int level) { + gpio_pin_set_dt(&ldo_enable, level); +} + +void set_ematch(int level) { + gpio_pin_set_dt(&ematch, level); +} + + void buzzer_init() { int ret = gpio_pin_configure_dt(&buzzer, GPIO_OUTPUT_INACTIVE); if (ret < 0) { @@ -23,8 +33,9 @@ void buzzer_init() { printk("Failed to conf ldo enable pin:("); return; } - gpio_pin_set_dt(&ldo_enable, 1); } + + void beep_full() { for (int i = 0; i < 10; i++) { printk("BEEP"); From b9bb43f0a8a29eb3100ea587784167483fa75d9e Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Fri, 24 Oct 2025 19:06:57 -0400 Subject: [PATCH 17/43] stashing changes - button interrupt (not done) --- .../solids_test/boards/grim_reefer.overlay | 15 +++++++++ app/other/solids_test/src/main.c | 31 +++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 6403fd149..059fd2d83 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -10,6 +10,21 @@ buzzer = &led1; // buzzer = &buzzer; }; + + leds: leds { + tx: tx{ + gpios = <&gpioa 0 GPIO_ACTIVE_HIGH>; + label = "TX" + }; + rx: rx{ + gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; + label = "RX" + }; + } +}; + +&uart4 { + status = "disabled"; }; &spi2 { diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 2dc4c137e..6a27832df 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -7,8 +7,15 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); -int main(void) -{ +static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(DT_ALIAS(tx), gpios); +static struct gpio_callback button_cb_data; + +void button_pressed (const struct device *dev, struct gpio_callback *cb, uint32_t pins) { + shell_print(shell, "Starting test..."); + control_start_test(); +} + +int main (void) { LOG_INF("Solids Test Start"); control_init(); @@ -16,5 +23,25 @@ int main(void) LOG_INF("Use 'test start' to begin test"); LOG_INF("Commands: test start | test stop | test dump"); + if (!gpio_is_ready_dt(&button)) { + printk("Error: button device %s is not ready\n", button.port->name); + return 0; + } + + int ret = gpio_pin_configure_dt(&button, GPIO_INPUT); + if (ret != 0) { + printk("Error %d: failed to configure %s pin %d\n", ret, button.port->name, button.pin); + return 0; + } + + ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE); + if (ret != 0) { + printk("Error %d: failed to configure interrupt on %s pin %d\n", ret, button.port->name, button.pin); + return 0; + } + + gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin)); + gpio_add_callback(button.port, &button_cb_data); + return 0; } \ No newline at end of file From 8618a1f6813ffa78b48ebc0b71b06b0ebd0c304d Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Fri, 24 Oct 2025 22:44:11 -0400 Subject: [PATCH 18/43] =?UTF-8?q?solids=20test=20probably=20done.=20buzzer?= =?UTF-8?q?=20unfortunately=20on.=20tx/rx=20button=20working.=20test=20rea?= =?UTF-8?q?d=20one=20implemented=20=F0=9F=8E=B7=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../solids_test/boards/grim_reefer.overlay | 10 ++--- app/other/solids_test/include/adc_reading.h | 1 + app/other/solids_test/include/calibration.h | 5 --- app/other/solids_test/include/config.h | 2 +- app/other/solids_test/include/control.h | 3 +- app/other/solids_test/prj.conf | 1 + app/other/solids_test/src/adc_reading.c | 38 +++++++++++----- app/other/solids_test/src/buzzer.c | 3 +- app/other/solids_test/src/calibration.c | 2 - app/other/solids_test/src/control.c | 15 +++++-- app/other/solids_test/src/main.c | 45 +++++++++++++------ app/other/solids_test/src/shell_cmds.c | 10 +++++ 12 files changed, 91 insertions(+), 44 deletions(-) delete mode 100644 app/other/solids_test/include/calibration.h delete mode 100644 app/other/solids_test/src/calibration.c diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index feb8b558d..a613715cb 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -7,20 +7,20 @@ aliases { adc0 = &adc; - buzzer = &led1; - // buzzer = &buzzer; + buzzer = &buzzer; + // buzzer = &led1; }; leds: leds { tx: tx{ gpios = <&gpioa 0 GPIO_ACTIVE_HIGH>; - label = "TX" + label = "TX"; }; rx: rx{ gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; - label = "RX" + label = "RX"; }; - } + }; }; &uart4 { diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index 6637833d3..b5911469d 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -9,6 +9,7 @@ struct adc_sample { }; int adc_init(); +void adc_read_one(uint32_t *adc_val); void adc_reading_task(); void adc_start_reading(); void adc_stop_recording(); diff --git a/app/other/solids_test/include/calibration.h b/app/other/solids_test/include/calibration.h deleted file mode 100644 index 4fbda050c..000000000 --- a/app/other/solids_test/include/calibration.h +++ /dev/null @@ -1,5 +0,0 @@ -#ifndef CALIBRATION_H -#define CALIBRATION_H - - -#endif // CALIBRATION_H \ No newline at end of file diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index fcbf5eb01..5e4ffae13 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -2,7 +2,7 @@ #define REEFER_INCLUDE_CONFIG_H #define SAMPLE_RATE_HZ 1000 -#define TEST_DURATION 10 +#define TEST_DURATION 10000 // 10 seconds, 10000 ms #define MAX_TESTS 30 #define STORAGE_THREAD_PRIORITY 1 diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 3946ae428..84c1cfa75 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -7,9 +7,10 @@ void control_init(); void control_start_test(); void control_stop_test(); +void control_print_one(const struct shell *shell); void control_dump_data(const struct shell *shell); void control_dump_one(const struct shell *shell, uint32_t test_index); -void control_erase_all(); +void control_erase_all(const struct shell *shell); bool control_is_running(); #endif // CONTROL_H \ No newline at end of file diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index f5e7ac703..46176cb54 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -11,6 +11,7 @@ CONFIG_SPEED_OPTIMIZATIONS=y CONFIG_SYS_CLOCK_TICKS_PER_SEC=100000 CONFIG_LOG=y +CONFIG_LOG_MODE_IMMEDIATE=y CONFIG_CRC=y CONFIG_SMF=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 2211c6657..9bf9f7ce4 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -3,6 +3,7 @@ #include "buzzer.h" #include "config.h" #include "flash_storage.h" +#include "control.h" #include #include @@ -42,6 +43,7 @@ static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; int adc_init() { + if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); return 0; @@ -53,12 +55,24 @@ int adc_init() { return 0; } + (void) adc_sequence_init_dt(&adc_channels[0], &sequence); + LOG_INF("ADC initialized"); return 0; } +void adc_read_one(uint32_t *adc_val) { + sequence.buffer = adc_val; + sequence.buffer_size = sizeof(*adc_val); + + int ret = adc_read(adc_dev, &sequence); + if (ret < 0) { + LOG_ERR("ADC read failed (%d)", ret); + return; + } +} + void adc_reading_task() { - int ret; uint32_t adc_val = 0; struct adc_sample sample = {0}; while (true) { @@ -69,17 +83,18 @@ void adc_reading_task() { test_start_beep(); LOG_INF("ADC reading started"); set_ldo(1); + k_msleep(1000); k_msleep(1); k_timer_start(&adc_timer, K_USEC(1000), K_USEC(1000)); // 1kHz loop int x = 0; uint32_t dropped_samples = 0; - uint64_t start_time_ticks = k_uptime_ticks(); uint64_t total_adc_ticks = 0; - (void) adc_sequence_init_dt(&adc_channels[0], &sequence); uint64_t total_loop_ticks = 0; uint32_t num_missed_expires = 0; + uint64_t start_time_ticks = k_uptime_ticks(); + while (true) { uint32_t start_loop_ticks = k_uptime_ticks(); uint32_t events = @@ -87,28 +102,23 @@ void adc_reading_task() { if (events & STOP_READING_EVENT) { break; } + uint32_t num_expiries = k_timer_status_get(&adc_timer); if (num_expiries == 0) { k_timer_status_sync(&adc_timer); } else { num_missed_expires += num_expiries-1; } + if (x == 250) { set_ematch(1); } else if (x == 500) { set_ematch(0); } - sequence.buffer = &adc_val; - sequence.buffer_size = sizeof(adc_val); - // Read from ADC uint32_t start_read = k_uptime_ticks(); - ret = adc_read(adc_dev, &sequence); - if (ret < 0) { - LOG_ERR("ADC read failed (%d)", ret); - continue; - } + adc_read_one(&adc_val); total_adc_ticks += k_uptime_ticks() - start_read; sample.timestamp = k_ticks_to_us_near32(k_uptime_ticks() - start_time_ticks); @@ -119,6 +129,12 @@ void adc_reading_task() { dropped_samples++; } total_loop_ticks += k_uptime_ticks() - start_loop_ticks; + + // Run test for 10 seconds + if ((k_ticks_to_ms_near32(k_uptime_ticks()) - k_ticks_to_ms_near32(start_time_ticks)) >= TEST_DURATION) { + control_stop_test(); + break; + } } set_ldo(0); diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index d08e46167..725104bf5 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -21,7 +21,6 @@ void set_ematch(int level) { gpio_pin_set_dt(&ematch, level); } - void buzzer_init() { int ret = gpio_pin_configure_dt(&buzzer, GPIO_OUTPUT_INACTIVE); if (ret < 0) { @@ -35,7 +34,6 @@ void buzzer_init() { } } - void beep_full() { for (int i = 0; i < 10; i++) { printk("BEEP"); @@ -55,6 +53,7 @@ void test_start_beep() { k_msleep(100); } } + void test_end_beep() { printk("BEEP"); set_buzz(1); diff --git a/app/other/solids_test/src/calibration.c b/app/other/solids_test/src/calibration.c deleted file mode 100644 index b12f85166..000000000 --- a/app/other/solids_test/src/calibration.c +++ /dev/null @@ -1,2 +0,0 @@ -#include "calibration.h" -#include "config.h" \ No newline at end of file diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index c95da2e3e..79269889d 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -2,11 +2,13 @@ #include "config.h" #include "adc_reading.h" #include "flash_storage.h" +#include "buzzer.h" #include #include #include #include +#include LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); @@ -31,10 +33,6 @@ void control_start_test(){ adc_start_reading(); test_number++; - - // Run test for 10 seconds - k_sleep(K_SECONDS(TEST_DURATION)); - control_stop_test(); } void control_stop_test(){ @@ -49,6 +47,15 @@ void control_stop_test(){ test_running = false; } +void control_print_one(const struct shell *shell){ + set_ldo(1); + k_msleep(5000); + uint32_t adc_val = 0; + adc_read_one(&adc_val); + shell_print(shell, "%d", adc_val); + set_ldo(0); +} + void control_dump_data(const struct shell *shell){ flash_dump_all(shell); } diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 6a27832df..62ab6b51b 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -4,44 +4,63 @@ #include #include #include +#include LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); -static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(DT_ALIAS(tx), gpios); +#define TX_NODE DT_NODELABEL(tx) +#define RX_NODE DT_NODELABEL(rx) +static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(TX_NODE, gpios); +static const struct gpio_dt_spec rx = GPIO_DT_SPEC_GET(RX_NODE, gpios); static struct gpio_callback button_cb_data; void button_pressed (const struct device *dev, struct gpio_callback *cb, uint32_t pins) { - shell_print(shell, "Starting test..."); + LOG_INF("Starting test..."); control_start_test(); } +void button_init() { + int ret = gpio_pin_configure_dt(&rx, GPIO_OUTPUT_ACTIVE); + if (ret < 0) { + printk("Failed to conf rx:("); + return; + } + ret = gpio_pin_configure_dt(&tx, GPIO_INPUT); + if (ret < 0) { + printk("Failed to conf tx:("); + return; + } +} + int main (void) { LOG_INF("Solids Test Start"); control_init(); buzzer_init(); + button_init(); + LOG_INF("Use 'test start' to begin test"); LOG_INF("Commands: test start | test stop | test dump"); - if (!gpio_is_ready_dt(&button)) { - printk("Error: button device %s is not ready\n", button.port->name); - return 0; + if (!gpio_is_ready_dt(&tx)) { + LOG_INF("Error: button device %s is not ready\n", tx.port->name); + // return 0; } - int ret = gpio_pin_configure_dt(&button, GPIO_INPUT); + int ret = gpio_pin_configure_dt(&tx, GPIO_INPUT); if (ret != 0) { - printk("Error %d: failed to configure %s pin %d\n", ret, button.port->name, button.pin); - return 0; + LOG_INF("Error %d: failed to configure %s pin %d\n", ret, tx.port->name, tx.pin); + // return 0; } - ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE); + ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { - printk("Error %d: failed to configure interrupt on %s pin %d\n", ret, button.port->name, button.pin); - return 0; + LOG_INF("Error %d: failed to configure interrupt on %s pin %d\n", ret, tx.port->name, tx.pin); + // return 0; } - gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin)); - gpio_add_callback(button.port, &button_cb_data); + gpio_init_callback(&button_cb_data, button_pressed, BIT(tx.pin)); + gpio_add_callback(tx.port, &button_cb_data); return 0; } \ No newline at end of file diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index fc8000ec4..b703651d2 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -9,6 +9,14 @@ LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); +static int cmd_test_print_one(const struct shell *shell, size_t argc, char **argv) { + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Getting one sample..."); + control_print_one(shell); + return 0; +} + static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); @@ -33,6 +41,7 @@ static int cmd_test_erase(const struct shell *shell, size_t argc, char **argv) { control_erase_all(shell); return 0; } + static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); @@ -56,6 +65,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, SHELL_CMD(start, NULL, "Start test", cm SHELL_CMD(stop, NULL, "Stop test preemptively", cmd_test_stop), SHELL_CMD(dump, NULL, "Dump flash data. Optional arg [test #]", cmd_test_dump), SHELL_CMD(erase, NULL, "Erase all flash data, prepare for new tests", cmd_test_erase), + SHELL_CMD(read, NULL, "Read one sample", cmd_test_print_one), SHELL_SUBCMD_SET_END); SHELL_CMD_REGISTER(test, &sub_test, "Solids Test Board control commands", NULL); \ No newline at end of file From 6736b06cc4a18607f0360274fa6257f2e6b5b014 Mon Sep 17 00:00:00 2001 From: cowsed Date: Sat, 25 Oct 2025 10:10:30 -0400 Subject: [PATCH 19/43] checkl on flash test --- app/other/solids_test/src/flash_storage.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index a5cf9a306..954453cd3 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -180,6 +180,10 @@ void stop_flash_storage() { int flash_dump_one(const struct shell *shell, uint32_t test_index) { + if (test_index >= MAX_TESTS ){ + shell_print(shell, "Pick a valid test, 0-29"); + return -1; + } struct adc_sample sample; if (!device_is_ready(flash_dev)) { From a43ff19db19a2b77147aa8a6ae0c6a9c011c54e1 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sat, 25 Oct 2025 13:17:07 -0400 Subject: [PATCH 20/43] set ematch/estop shell cmds, ematch set 500ms into adc reading instead of 250 --- app/other/solids_test/include/control.h | 2 ++ app/other/solids_test/src/adc_reading.c | 8 ++++---- app/other/solids_test/src/buzzer.c | 2 ++ app/other/solids_test/src/control.c | 10 ++++++++++ app/other/solids_test/src/shell_cmds.c | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 84c1cfa75..2345cd9aa 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -12,5 +12,7 @@ void control_dump_data(const struct shell *shell); void control_dump_one(const struct shell *shell, uint32_t test_index); void control_erase_all(const struct shell *shell); bool control_is_running(); +void control_set_ematch(const struct shell *shell); +void control_stop_ematch(const struct shell *shell); #endif // CONTROL_H \ No newline at end of file diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 9bf9f7ce4..fcc589368 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -83,10 +83,10 @@ void adc_reading_task() { test_start_beep(); LOG_INF("ADC reading started"); set_ldo(1); - k_msleep(1000); + k_msleep(2000); k_msleep(1); - k_timer_start(&adc_timer, K_USEC(1000), K_USEC(1000)); // 1kHz loop + k_timer_start(&adc_timer, K_USEC(SAMPLE_RATE_HZ), K_USEC(SAMPLE_RATE_HZ)); // 1kHz loop int x = 0; uint32_t dropped_samples = 0; @@ -110,9 +110,9 @@ void adc_reading_task() { num_missed_expires += num_expiries-1; } - if (x == 250) { + if (x == 500) { set_ematch(1); - } else if (x == 500) { + } else if (x == 900) { set_ematch(0); } diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 725104bf5..19424aadd 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -1,3 +1,5 @@ +#include "buzzer.h" + #include #include diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index 79269889d..fa92d6fba 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -70,4 +70,14 @@ void control_erase_all(const struct shell *shell){ bool control_is_running(){ return test_running; +} + +void control_set_ematch(const struct shell *shell){ + set_ematch(1); + shell_print(shell, "ematch: 1"); +} + +void control_stop_ematch(const struct shell *shell){ + set_ematch(0); + shell_print(shell, "ematch: 0"); } \ No newline at end of file diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index b703651d2..904c272d6 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -17,6 +17,22 @@ static int cmd_test_print_one(const struct shell *shell, size_t argc, char **arg return 0; } +static int cmd_test_ematch(const struct shell *shell, size_t argc, char **argv) { + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Setting ematch..."); + control_set_ematch(shell); + return 0; +} + +static int cmd_test_estop(const struct shell *shell, size_t argc, char **argv) { + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Stopping ematch..."); + control_stop_ematch(shell); + return 0; +} + static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); @@ -66,6 +82,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, SHELL_CMD(start, NULL, "Start test", cm SHELL_CMD(dump, NULL, "Dump flash data. Optional arg [test #]", cmd_test_dump), SHELL_CMD(erase, NULL, "Erase all flash data, prepare for new tests", cmd_test_erase), SHELL_CMD(read, NULL, "Read one sample", cmd_test_print_one), + SHELL_CMD(ematch, NULL, "Set ematch high", cmd_test_ematch), + SHELL_CMD(estop, NULL, "Set ematch low", cmd_test_estop), SHELL_SUBCMD_SET_END); SHELL_CMD_REGISTER(test, &sub_test, "Solids Test Board control commands", NULL); \ No newline at end of file From 554c8f09133e3e1349726faf792acab560fc2d42 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sat, 25 Oct 2025 14:39:50 -0400 Subject: [PATCH 21/43] formatting --- app/other/solids_test/src/flash_storage.c | 2 +- app/other/solids_test/src/shell_cmds.c | 44 +++++++++++------------ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 954453cd3..235acf60a 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -197,7 +197,7 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index) { return 0; } - shell_print(shell, "Dumping Test %d:", test_index); + shell_print(shell, "==============Dumping Test %d==============", test_index); for (int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++) { if (flash_read(flash_dev, block_addr, &sample, sizeof(sample)) < 0) { diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index 904c272d6..540f312b5 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -9,35 +9,19 @@ LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); -static int cmd_test_print_one(const struct shell *shell, size_t argc, char **argv) { - ARG_UNUSED(argc); - ARG_UNUSED(argv); - shell_print(shell, "Getting one sample..."); - control_print_one(shell); - return 0; -} - -static int cmd_test_ematch(const struct shell *shell, size_t argc, char **argv) { - ARG_UNUSED(argc); - ARG_UNUSED(argv); - shell_print(shell, "Setting ematch..."); - control_set_ematch(shell); - return 0; -} - -static int cmd_test_estop(const struct shell *shell, size_t argc, char **argv) { +static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); - shell_print(shell, "Stopping ematch..."); - control_stop_ematch(shell); + shell_print(shell, "Starting test..."); + control_start_test(); return 0; } -static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { +static int cmd_test_print_one(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); - shell_print(shell, "Starting test..."); - control_start_test(); + shell_print(shell, "Getting one sample..."); + control_print_one(shell); return 0; } @@ -77,6 +61,22 @@ static int cmd_test_dump(const struct shell *shell, size_t argc, char **argv) { return 0; } +static int cmd_test_ematch(const struct shell *shell, size_t argc, char **argv) { + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Setting ematch..."); + control_set_ematch(shell); + return 0; +} + +static int cmd_test_estop(const struct shell *shell, size_t argc, char **argv) { + ARG_UNUSED(argc); + ARG_UNUSED(argv); + shell_print(shell, "Stopping ematch..."); + control_stop_ematch(shell); + return 0; +} + SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, SHELL_CMD(start, NULL, "Start test", cmd_test_start), SHELL_CMD(stop, NULL, "Stop test preemptively", cmd_test_stop), SHELL_CMD(dump, NULL, "Dump flash data. Optional arg [test #]", cmd_test_dump), From d82bf52c544ce2b9da9322b08ff4224f2f5b2977 Mon Sep 17 00:00:00 2001 From: cowsed Date: Sun, 26 Oct 2025 11:23:54 -0400 Subject: [PATCH 22/43] shell print n --- app/other/solids_test/include/control.h | 2 +- app/other/solids_test/src/control.c | 51 ++++++++++++------------- app/other/solids_test/src/shell_cmds.c | 12 +++--- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 2345cd9aa..1e3137028 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -7,7 +7,7 @@ void control_init(); void control_start_test(); void control_stop_test(); -void control_print_one(const struct shell *shell); +void control_print_n(const struct shell *shell, int num); void control_dump_data(const struct shell *shell); void control_dump_one(const struct shell *shell, uint32_t test_index); void control_erase_all(const struct shell *shell); diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index fa92d6fba..de4c73e8d 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -1,27 +1,28 @@ #include "control.h" -#include "config.h" + #include "adc_reading.h" -#include "flash_storage.h" #include "buzzer.h" +#include "config.h" +#include "flash_storage.h" +#include +#include #include #include #include -#include -#include LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); static int test_number = 0; static bool test_running = false; -void control_init(){ +void control_init() { LOG_INF("Initializing..."); adc_init(); } -void control_start_test(){ - if(test_running){ +void control_start_test() { + if (test_running) { LOG_WRN("Test already running"); return; } @@ -35,8 +36,8 @@ void control_start_test(){ test_number++; } -void control_stop_test(){ - if(!test_running){ +void control_stop_test() { + if (!test_running) { LOG_WRN("No test running"); return; } @@ -47,37 +48,35 @@ void control_stop_test(){ test_running = false; } -void control_print_one(const struct shell *shell){ +void control_print_n(const struct shell *shell, int num) { set_ldo(1); k_msleep(5000); uint32_t adc_val = 0; - adc_read_one(&adc_val); - shell_print(shell, "%d", adc_val); + uint32_t start = k_uptime_ticks(); + for (int i = 0; i < num; i++) { + + adc_read_one(&adc_val); + uint32_t t = k_uptime_ticks() - start; + shell_print(shell, "%u, %d", k_ticks_to_us_near32(t), adc_val); + k_msleep(1); + } set_ldo(0); } -void control_dump_data(const struct shell *shell){ - flash_dump_all(shell); -} +void control_dump_data(const struct shell *shell) { flash_dump_all(shell); } -void control_dump_one(const struct shell *shell, uint32_t test_index){ - flash_dump_one(shell, test_index); -} +void control_dump_one(const struct shell *shell, uint32_t test_index) { flash_dump_one(shell, test_index); } -void control_erase_all(const struct shell *shell){ - flash_erase_all(shell); -} +void control_erase_all(const struct shell *shell) { flash_erase_all(shell); } -bool control_is_running(){ - return test_running; -} +bool control_is_running() { return test_running; } -void control_set_ematch(const struct shell *shell){ +void control_set_ematch(const struct shell *shell) { set_ematch(1); shell_print(shell, "ematch: 1"); } -void control_stop_ematch(const struct shell *shell){ +void control_stop_ematch(const struct shell *shell) { set_ematch(0); shell_print(shell, "ematch: 0"); } \ No newline at end of file diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index 540f312b5..8833da4a8 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -18,10 +18,12 @@ static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { } static int cmd_test_print_one(const struct shell *shell, size_t argc, char **argv) { - ARG_UNUSED(argc); - ARG_UNUSED(argv); - shell_print(shell, "Getting one sample..."); - control_print_one(shell); + int num = 1; + if (argc == 2) { + num = atoi(argv[1]); + } + shell_print(shell, "Getting %d sample(s)...", num); + control_print_n(shell, num); return 0; } @@ -81,7 +83,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, SHELL_CMD(start, NULL, "Start test", cm SHELL_CMD(stop, NULL, "Stop test preemptively", cmd_test_stop), SHELL_CMD(dump, NULL, "Dump flash data. Optional arg [test #]", cmd_test_dump), SHELL_CMD(erase, NULL, "Erase all flash data, prepare for new tests", cmd_test_erase), - SHELL_CMD(read, NULL, "Read one sample", cmd_test_print_one), + SHELL_CMD(read, NULL, "Read one (or more) samples", cmd_test_print_one), SHELL_CMD(ematch, NULL, "Set ematch high", cmd_test_ematch), SHELL_CMD(estop, NULL, "Set ematch low", cmd_test_estop), SHELL_SUBCMD_SET_END); From 7d246a525a2dd06fd520b1f66849c38008b4dc5b Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 27 Oct 2025 22:26:08 -0400 Subject: [PATCH 23/43] formatting, comments, button functions moved --- .../solids_test/boards/grim_reefer.overlay | 4 +- app/other/solids_test/include/adc_reading.h | 2 +- app/other/solids_test/include/button.h | 11 +++++ app/other/solids_test/include/buzzer.h | 7 ++- app/other/solids_test/include/config.h | 12 ++--- app/other/solids_test/include/flash_storage.h | 1 + app/other/solids_test/src/adc_reading.c | 29 ++++++----- app/other/solids_test/src/button.c | 49 +++++++++++++++++++ app/other/solids_test/src/buzzer.c | 7 ++- app/other/solids_test/src/control.c | 13 ++--- app/other/solids_test/src/flash_storage.c | 2 +- app/other/solids_test/src/main.c | 46 +---------------- app/other/solids_test/src/shell_cmds.c | 2 +- 13 files changed, 103 insertions(+), 82 deletions(-) create mode 100644 app/other/solids_test/include/button.h create mode 100644 app/other/solids_test/src/button.c diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index a613715cb..d3e35060d 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -7,8 +7,8 @@ aliases { adc0 = &adc; - buzzer = &buzzer; - // buzzer = &led1; + // buzzer = &buzzer; + buzzer = &led1; }; leds: leds { diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index b5911469d..fe92616c8 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -8,7 +8,7 @@ struct adc_sample { int32_t value; }; -int adc_init(); +void adc_init(); void adc_read_one(uint32_t *adc_val); void adc_reading_task(); void adc_start_reading(); diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h new file mode 100644 index 000000000..0e85a701c --- /dev/null +++ b/app/other/solids_test/include/button.h @@ -0,0 +1,11 @@ +#ifndef BUTTON_H +#define BUTTON_H + +#include +#include +#include + +void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); +void button_init(); + +#endif // BUTTON_H \ No newline at end of file diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index 31ca683dc..2045e1ed3 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -1,3 +1,6 @@ +#ifndef BUZZER_H +#define BUZZER_H + void buzzer_init(); void beep_full(); @@ -5,4 +8,6 @@ void test_start_beep(); void test_end_beep(); void set_ematch(int level); -void set_ldo(int level); \ No newline at end of file +void set_ldo(int level); + +#endif // BUZZER_H \ No newline at end of file diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index 5e4ffae13..de3b1387e 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -1,12 +1,12 @@ -#ifndef REEFER_INCLUDE_CONFIG_H -#define REEFER_INCLUDE_CONFIG_H +#ifndef CONFIG_H +#define CONFIG_H #define SAMPLE_RATE_HZ 1000 -#define TEST_DURATION 10000 // 10 seconds, 10000 ms +#define TEST_DURATION 10000 // ms #define MAX_TESTS 30 -#define STORAGE_THREAD_PRIORITY 1 -#define ADC_READ_PRIORITY -1 +#define STORAGE_THREAD_PRIORITY 1 +#define ADC_READ_PRIORITY -1 #define THREAD_START_DELAY 100 -#endif // REEFER_INCLUDE_CONFIG_H \ No newline at end of file +#endif // CONFIG_H \ No newline at end of file diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index 9eaebdba2..338d7cf91 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -2,6 +2,7 @@ #define FLASH_STORAGE_H #include +#include int start_flash_storage(); void stop_flash_storage(); diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index fcc589368..c98fb33de 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -1,5 +1,4 @@ #include "adc_reading.h" - #include "buzzer.h" #include "config.h" #include "flash_storage.h" @@ -42,25 +41,24 @@ static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = siz static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; -int adc_init() { - +void adc_init() { if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); - return 0; + return; } int err = adc_channel_setup_dt(&adc_channels[0]); if (err < 0) { LOG_ERR("Could not setup channel (%d)\n", err); - return 0; + return; } (void) adc_sequence_init_dt(&adc_channels[0], &sequence); LOG_INF("ADC initialized"); - return 0; } +// Read one adc sample void adc_read_one(uint32_t *adc_val) { sequence.buffer = adc_val; sequence.buffer_size = sizeof(*adc_val); @@ -80,13 +78,13 @@ void adc_reading_task() { // Wait for start event k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); - test_start_beep(); LOG_INF("ADC reading started"); - set_ldo(1); + // set_ldo(1); + // Delay test 2 seconds, beep when test actually starts k_msleep(2000); - k_msleep(1); + test_start_beep(); - k_timer_start(&adc_timer, K_USEC(SAMPLE_RATE_HZ), K_USEC(SAMPLE_RATE_HZ)); // 1kHz loop + k_timer_start(&adc_timer, K_USEC(SAMPLE_RATE_HZ), K_USEC(SAMPLE_RATE_HZ)); // 1000Hz periods int x = 0; uint32_t dropped_samples = 0; @@ -107,9 +105,10 @@ void adc_reading_task() { if (num_expiries == 0) { k_timer_status_sync(&adc_timer); } else { - num_missed_expires += num_expiries-1; + num_missed_expires += num_expiries - 1; } + // Set ematch 500ms into test if (x == 500) { set_ematch(1); } else if (x == 900) { @@ -123,21 +122,21 @@ void adc_reading_task() { sample.timestamp = k_ticks_to_us_near32(k_uptime_ticks() - start_time_ticks); sample.value = adc_val; - x++; - + if (k_msgq_put(&adc_data_queue, &sample, K_NO_WAIT) != 0) { dropped_samples++; } + x++; total_loop_ticks += k_uptime_ticks() - start_loop_ticks; - // Run test for 10 seconds + // Stop test after 10 seconds if ((k_ticks_to_ms_near32(k_uptime_ticks()) - k_ticks_to_ms_near32(start_time_ticks)) >= TEST_DURATION) { control_stop_test(); break; } } - set_ldo(0); + // set_ldo(0); k_timer_stop(&adc_timer); LOG_INF( "number of samples: %d, %u missed, %u dropped, read time %llu, ms per = %.2f, loop time: %llu, loop time ticks: %llu", diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c new file mode 100644 index 000000000..3fa914523 --- /dev/null +++ b/app/other/solids_test/src/button.c @@ -0,0 +1,49 @@ +#include "button.h" +#include "control.h" + +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(button, LOG_LEVEL_INF); + +#define TX_NODE DT_NODELABEL(tx) +#define RX_NODE DT_NODELABEL(rx) +static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(TX_NODE, gpios); +static const struct gpio_dt_spec rx = GPIO_DT_SPEC_GET(RX_NODE, gpios); +static struct gpio_callback button_cb_data; + +void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { + LOG_INF("Starting test..."); + control_start_test(); +} + +void button_init() { + int ret = gpio_pin_configure_dt(&rx, GPIO_OUTPUT_ACTIVE); + if (ret < 0) { + LOG_ERR("Failed to conf rx :("); + return; + } + + ret = gpio_pin_configure_dt(&tx, GPIO_INPUT); + if (ret < 0) { + LOG_ERR("Failed to conf tx :("); + return; + } + + if (!gpio_is_ready_dt(&tx)) { + LOG_ERR("Error: button device %s is not ready\n", tx.port->name); + return; + } + + ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_TO_ACTIVE); + if (ret != 0) { + LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, tx.port->name, tx.pin); + return; + } + + gpio_init_callback(&button_cb_data, button_pressed, BIT(tx.pin)); + gpio_add_callback(tx.port, &button_cb_data); +} \ No newline at end of file diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 19424aadd..11aaec613 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -2,6 +2,9 @@ #include #include +#include + +LOG_MODULE_REGISTER(buzzer, LOG_LEVEL_INF); #define LDO_EN_NODE DT_NODELABEL(ldo_enable) #define CAM_EN_NODE DT_NODELABEL(cam_enable) @@ -26,12 +29,12 @@ void set_ematch(int level) { void buzzer_init() { int ret = gpio_pin_configure_dt(&buzzer, GPIO_OUTPUT_INACTIVE); if (ret < 0) { - printk("Failed to conf buzzer pin:("); + LOG_ERR("Failed to conf buzzer pin :("); return; } ret = gpio_pin_configure_dt(&ldo_enable, GPIO_OUTPUT_INACTIVE); if (ret < 0) { - printk("Failed to conf ldo enable pin:("); + LOG_ERR("Failed to conf ldo enable pin :("); return; } } diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index de4c73e8d..2c9caa8dc 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -1,5 +1,4 @@ #include "control.h" - #include "adc_reading.h" #include "buzzer.h" #include "config.h" @@ -13,7 +12,6 @@ LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); -static int test_number = 0; static bool test_running = false; void control_init() { @@ -27,13 +25,10 @@ void control_start_test() { return; } - LOG_INF("Starting test run"); test_running = true; start_flash_storage(); adc_start_reading(); - - test_number++; } void control_stop_test() { @@ -49,7 +44,7 @@ void control_stop_test() { } void control_print_n(const struct shell *shell, int num) { - set_ldo(1); + // set_ldo(1); k_msleep(5000); uint32_t adc_val = 0; uint32_t start = k_uptime_ticks(); @@ -60,7 +55,7 @@ void control_print_n(const struct shell *shell, int num) { shell_print(shell, "%u, %d", k_ticks_to_us_near32(t), adc_val); k_msleep(1); } - set_ldo(0); + // set_ldo(0); } void control_dump_data(const struct shell *shell) { flash_dump_all(shell); } @@ -73,10 +68,10 @@ bool control_is_running() { return test_running; } void control_set_ematch(const struct shell *shell) { set_ematch(1); - shell_print(shell, "ematch: 1"); + shell_print(shell, "Ematch: 1"); } void control_stop_ematch(const struct shell *shell) { set_ematch(0); - shell_print(shell, "ematch: 0"); + shell_print(shell, "Ematch: 0"); } \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 235acf60a..ea1004372 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -244,4 +244,4 @@ int flash_erase_all(const struct shell *shell) { save_metadata(); return 0; -} +} \ No newline at end of file diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 62ab6b51b..62397fbd8 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -1,5 +1,7 @@ #include "control.h" #include "buzzer.h" +#include "button.h" + #include #include #include @@ -8,30 +10,6 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); -#define TX_NODE DT_NODELABEL(tx) -#define RX_NODE DT_NODELABEL(rx) -static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(TX_NODE, gpios); -static const struct gpio_dt_spec rx = GPIO_DT_SPEC_GET(RX_NODE, gpios); -static struct gpio_callback button_cb_data; - -void button_pressed (const struct device *dev, struct gpio_callback *cb, uint32_t pins) { - LOG_INF("Starting test..."); - control_start_test(); -} - -void button_init() { - int ret = gpio_pin_configure_dt(&rx, GPIO_OUTPUT_ACTIVE); - if (ret < 0) { - printk("Failed to conf rx:("); - return; - } - ret = gpio_pin_configure_dt(&tx, GPIO_INPUT); - if (ret < 0) { - printk("Failed to conf tx:("); - return; - } -} - int main (void) { LOG_INF("Solids Test Start"); @@ -42,25 +20,5 @@ int main (void) { LOG_INF("Use 'test start' to begin test"); LOG_INF("Commands: test start | test stop | test dump"); - if (!gpio_is_ready_dt(&tx)) { - LOG_INF("Error: button device %s is not ready\n", tx.port->name); - // return 0; - } - - int ret = gpio_pin_configure_dt(&tx, GPIO_INPUT); - if (ret != 0) { - LOG_INF("Error %d: failed to configure %s pin %d\n", ret, tx.port->name, tx.pin); - // return 0; - } - - ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_TO_ACTIVE); - if (ret != 0) { - LOG_INF("Error %d: failed to configure interrupt on %s pin %d\n", ret, tx.port->name, tx.pin); - // return 0; - } - - gpio_init_callback(&button_cb_data, button_pressed, BIT(tx.pin)); - gpio_add_callback(tx.port, &button_cb_data); - return 0; } \ No newline at end of file diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index 8833da4a8..8f8d182b8 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -19,7 +19,7 @@ static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { static int cmd_test_print_one(const struct shell *shell, size_t argc, char **argv) { int num = 1; - if (argc == 2) { + if (argc == 2) { num = atoi(argv[1]); } shell_print(shell, "Getting %d sample(s)...", num); From 2bbf7ef207ee40949b22ed42469980ae7247c39a Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Tue, 28 Oct 2025 14:01:47 -0400 Subject: [PATCH 24/43] sys init --- app/other/solids_test/include/adc_reading.h | 2 +- app/other/solids_test/include/button.h | 2 +- app/other/solids_test/include/buzzer.h | 2 +- app/other/solids_test/include/control.h | 2 +- app/other/solids_test/src/adc_reading.c | 7 ++++--- app/other/solids_test/src/button.c | 22 +++++++++++---------- app/other/solids_test/src/buzzer.c | 8 +++++--- app/other/solids_test/src/control.c | 4 ++-- app/other/solids_test/src/main.c | 13 +++++------- 9 files changed, 32 insertions(+), 30 deletions(-) diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index fe92616c8..7b2936dd1 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -8,7 +8,7 @@ struct adc_sample { int32_t value; }; -void adc_init(); +int adc_init(void); void adc_read_one(uint32_t *adc_val); void adc_reading_task(); void adc_start_reading(); diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index 0e85a701c..026a4efc3 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -5,7 +5,7 @@ #include #include +int button_init(void); void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); -void button_init(); #endif // BUTTON_H \ No newline at end of file diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index 2045e1ed3..cc7829e4f 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -1,7 +1,7 @@ #ifndef BUZZER_H #define BUZZER_H -void buzzer_init(); +int buzzer_init(void); void beep_full(); void test_start_beep(); diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 1e3137028..8fd8f5b87 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -4,7 +4,7 @@ #include #include -void control_init(); +int control_init(void); void control_start_test(); void control_stop_test(); void control_print_n(const struct shell *shell, int num); diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index c98fb33de..8752953b5 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -41,21 +41,22 @@ static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = siz static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; -void adc_init() { +int adc_init(void) { if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); - return; + return -1; } int err = adc_channel_setup_dt(&adc_channels[0]); if (err < 0) { LOG_ERR("Could not setup channel (%d)\n", err); - return; + return -1; } (void) adc_sequence_init_dt(&adc_channels[0], &sequence); LOG_INF("ADC initialized"); + return 0; } // Read one adc sample diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index 3fa914523..32232f5fe 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -15,35 +15,37 @@ static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(TX_NODE, gpios); static const struct gpio_dt_spec rx = GPIO_DT_SPEC_GET(RX_NODE, gpios); static struct gpio_callback button_cb_data; -void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { - LOG_INF("Starting test..."); - control_start_test(); -} - -void button_init() { +int button_init(void) { int ret = gpio_pin_configure_dt(&rx, GPIO_OUTPUT_ACTIVE); if (ret < 0) { LOG_ERR("Failed to conf rx :("); - return; + return -1; } ret = gpio_pin_configure_dt(&tx, GPIO_INPUT); if (ret < 0) { LOG_ERR("Failed to conf tx :("); - return; + return -1; } if (!gpio_is_ready_dt(&tx)) { LOG_ERR("Error: button device %s is not ready\n", tx.port->name); - return; + return -1; } ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, tx.port->name, tx.pin); - return; + return -1; } gpio_init_callback(&button_cb_data, button_pressed, BIT(tx.pin)); gpio_add_callback(tx.port, &button_cb_data); + + return 0; +} + +void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { + LOG_INF("Starting test..."); + control_start_test(); } \ No newline at end of file diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 11aaec613..aef398238 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -26,17 +26,19 @@ void set_ematch(int level) { gpio_pin_set_dt(&ematch, level); } -void buzzer_init() { +int buzzer_init(void) { int ret = gpio_pin_configure_dt(&buzzer, GPIO_OUTPUT_INACTIVE); if (ret < 0) { LOG_ERR("Failed to conf buzzer pin :("); - return; + return -1; } ret = gpio_pin_configure_dt(&ldo_enable, GPIO_OUTPUT_INACTIVE); if (ret < 0) { LOG_ERR("Failed to conf ldo enable pin :("); - return; + return -1; } + + return 0; } void beep_full() { diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index 2c9caa8dc..e0577c5e1 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -14,9 +14,9 @@ LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); static bool test_running = false; -void control_init() { +int control_init(void) { LOG_INF("Initializing..."); - adc_init(); + return adc_init(); } void control_start_test() { diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 62397fbd8..10e959989 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -2,20 +2,17 @@ #include "buzzer.h" #include "button.h" -#include -#include -#include #include -#include +#include LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); +SYS_INIT(control_init, APPLICATION, 0); +SYS_INIT(buzzer_init, APPLICATION, 0); +SYS_INIT(button_init, APPLICATION, 0); + int main (void) { LOG_INF("Solids Test Start"); - - control_init(); - buzzer_init(); - button_init(); LOG_INF("Use 'test start' to begin test"); LOG_INF("Commands: test start | test stop | test dump"); From 9837e91469af77ad5c2637c6c253903d0347df3c Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Tue, 28 Oct 2025 14:04:42 -0400 Subject: [PATCH 25/43] main commands print update --- app/other/solids_test/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 10e959989..69ab7bd8f 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -15,7 +15,7 @@ int main (void) { LOG_INF("Solids Test Start"); LOG_INF("Use 'test start' to begin test"); - LOG_INF("Commands: test start | test stop | test dump"); + LOG_INF("Commands: test start | test stop | test dump [optional test #] | test erase | test read [optional #]"); return 0; } \ No newline at end of file From a48a8bcdd1dd40a0646dbd9aa2693c8d8103d6ff Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Thu, 30 Oct 2025 13:20:09 -0400 Subject: [PATCH 26/43] function definitions and sys_init --- .../solids_test/boards/grim_reefer.overlay | 2 +- app/other/solids_test/include/adc_reading.h | 28 ++++++++++++- app/other/solids_test/include/button.h | 10 ++++- app/other/solids_test/include/buzzer.h | 26 +++++++++++- app/other/solids_test/include/control.h | 41 ++++++++++++++++++- app/other/solids_test/include/flash_storage.h | 26 +++++++++++- app/other/solids_test/src/adc_reading.c | 4 +- app/other/solids_test/src/button.c | 2 +- app/other/solids_test/src/buzzer.c | 2 +- app/other/solids_test/src/control.c | 9 +--- app/other/solids_test/src/main.c | 4 +- 11 files changed, 133 insertions(+), 21 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index d3e35060d..4ec6e417e 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -8,7 +8,7 @@ aliases { adc0 = &adc; // buzzer = &buzzer; - buzzer = &led1; + buzzer = &led1; // delete before pr }; leds: leds { diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index 7b2936dd1..654fcd280 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -3,15 +3,41 @@ #include +/** + * @brief Struct to hold one ADC sample + */ struct adc_sample { + /** Timestamp the sample was recorded in ms */ uint32_t timestamp; + /** Value of the sample */ int32_t value; }; -int adc_init(void); +/** + * @brief Initialize the ADC device and channel + * @return 0 if successful + */ +int adc_init(); + +/** + * @brief Read one ADC sample + * @param adc_val Pointer to value where sample will be written + */ void adc_read_one(uint32_t *adc_val); + +/** + * @brief Waits for ADC reading event to start, then reads ADC samples for 10 seconds + */ void adc_reading_task(); + +/** + * @brief Sets ADC control event to start + */ void adc_start_reading(); + +/** + * @brief Sets ADC control event to end + */ void adc_stop_recording(); #endif // ADC_READING_H \ No newline at end of file diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index 026a4efc3..c20852295 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -5,7 +5,15 @@ #include #include -int button_init(void); +/** + * @brief Configures TX and RX pins as gpio + * @return 0 if successful + */ +int button_init(); + +/** + * @brief Interrupt function that starts a test when TX pin is pulled high + */ void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); #endif // BUTTON_H \ No newline at end of file diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index cc7829e4f..c46955a68 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -1,13 +1,37 @@ #ifndef BUZZER_H #define BUZZER_H -int buzzer_init(void); +/** + * @brief Configure buzzer and ldo gpio pins + * @return 0 if successful + */ +int buzzer_init(); + +/** + * @brief Beep loudly in 1 second intervals for 10 seconds to indicate that flash is full (max tests reached) + */ void beep_full(); +/** + * @brief Beep loudly 3 times to indicate test start + */ void test_start_beep(); + +/** + * @brief Beep loudly 2 times to indicate test end + */ void test_end_beep(); +/** + * @brief Set ematch gpio pin + * @param level Value assigned to pin + */ void set_ematch(int level); + +/** + * @brief Set ldo gpio pin + * @param level Value assigned to pin + */ void set_ldo(int level); #endif // BUZZER_H \ No newline at end of file diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 8fd8f5b87..f03860e4a 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -4,15 +4,52 @@ #include #include -int control_init(void); +/** + * @brief Starts flash storage and ADC reading + */ void control_start_test(); + +/** + * @brief Stops flash storage and ADC reading + */ void control_stop_test(); + +/** + * @brief Reads and prints n number of ADC samples + * @param shell Pointer to shell instance + * @param num Number of samples to read + */ void control_print_n(const struct shell *shell, int num); + +/** + * @brief Dumps all ADC data from flash storage + * @param shell Pointer to shell instance + */ void control_dump_data(const struct shell *shell); + +/** + * @brief Dumps one ADC test from flash storage + * @param shell Pointer to shell instance + * @param test_index The test number to dump + */ void control_dump_one(const struct shell *shell, uint32_t test_index); + +/** + * @brief Clear all flash blocks + * @param shell Pointer to shell instance + */ void control_erase_all(const struct shell *shell); -bool control_is_running(); + +/** + * @brief Set ematch gpio high + * @param shell Pointer to shell instance + */ void control_set_ematch(const struct shell *shell); + +/** + * @brief Set ematch gpio low + * @param shell Pointer to shell instance + */ void control_stop_ematch(const struct shell *shell); #endif // CONTROL_H \ No newline at end of file diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index 338d7cf91..935b5cd6f 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -4,10 +4,34 @@ #include #include +/** + * @brief Begin flash storage event + * @return 0 if message queue put successful + */ int start_flash_storage(); + +/** + * @brief End flash storage event + */ void stop_flash_storage(); -int flash_dump_all(const struct shell *shell); + +/** + * @brief Dumps one ADC test from flash storage + * @param shell Pointer to shell instance + * @param test_index The test number to dump + */ int flash_dump_one(const struct shell *shell, uint32_t test_index); + +/** + * @brief Dumps all ADC data from flash storage + * @param shell Pointer to shell instance + */ +int flash_dump_all(const struct shell *shell); + +/** + * @brief Clear all flash blocks + * @param shell Pointer to shell instance + */ int flash_erase_all(const struct shell *shell); extern struct k_msgq storage_control_queue; diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 8752953b5..8e3511006 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -41,7 +41,8 @@ static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = siz static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; -int adc_init(void) { +int adc_init() { + LOG_INF("Initializing..."); if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); return -1; @@ -59,7 +60,6 @@ int adc_init(void) { return 0; } -// Read one adc sample void adc_read_one(uint32_t *adc_val) { sequence.buffer = adc_val; sequence.buffer_size = sizeof(*adc_val); diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index 32232f5fe..51b8346df 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -15,7 +15,7 @@ static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(TX_NODE, gpios); static const struct gpio_dt_spec rx = GPIO_DT_SPEC_GET(RX_NODE, gpios); static struct gpio_callback button_cb_data; -int button_init(void) { +int button_init() { int ret = gpio_pin_configure_dt(&rx, GPIO_OUTPUT_ACTIVE); if (ret < 0) { LOG_ERR("Failed to conf rx :("); diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index aef398238..084db74ed 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -26,7 +26,7 @@ void set_ematch(int level) { gpio_pin_set_dt(&ematch, level); } -int buzzer_init(void) { +int buzzer_init() { int ret = gpio_pin_configure_dt(&buzzer, GPIO_OUTPUT_INACTIVE); if (ret < 0) { LOG_ERR("Failed to conf buzzer pin :("); diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index e0577c5e1..29f34473e 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -14,11 +14,6 @@ LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); static bool test_running = false; -int control_init(void) { - LOG_INF("Initializing..."); - return adc_init(); -} - void control_start_test() { if (test_running) { LOG_WRN("Test already running"); @@ -48,8 +43,8 @@ void control_print_n(const struct shell *shell, int num) { k_msleep(5000); uint32_t adc_val = 0; uint32_t start = k_uptime_ticks(); + for (int i = 0; i < num; i++) { - adc_read_one(&adc_val); uint32_t t = k_uptime_ticks() - start; shell_print(shell, "%u, %d", k_ticks_to_us_near32(t), adc_val); @@ -64,8 +59,6 @@ void control_dump_one(const struct shell *shell, uint32_t test_index) { flash_du void control_erase_all(const struct shell *shell) { flash_erase_all(shell); } -bool control_is_running() { return test_running; } - void control_set_ematch(const struct shell *shell) { set_ematch(1); shell_print(shell, "Ematch: 1"); diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 69ab7bd8f..0f1829028 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -1,4 +1,4 @@ -#include "control.h" +#include "adc_reading.h" #include "buzzer.h" #include "button.h" @@ -7,7 +7,7 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); -SYS_INIT(control_init, APPLICATION, 0); +SYS_INIT(adc_init, APPLICATION, 0); SYS_INIT(buzzer_init, APPLICATION, 0); SYS_INIT(button_init, APPLICATION, 0); From 5dcc0ee415c853ec4d4544dd3590cfb7edeb4584 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 3 Nov 2025 19:32:04 -0500 Subject: [PATCH 27/43] even more formatting --- app/other/solids_test/include/button.h | 3 +++ app/other/solids_test/include/buzzer.h | 4 ++-- app/other/solids_test/include/config.h | 2 +- app/other/solids_test/src/adc_reading.c | 5 ++--- app/other/solids_test/src/buzzer.c | 4 +++- app/other/solids_test/src/flash_storage.c | 11 ++++++----- app/other/solids_test/src/main.c | 10 ++++++---- app/other/solids_test/src/shell_cmds.c | 2 +- 8 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index c20852295..59ff9da03 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -13,6 +13,9 @@ int button_init(); /** * @brief Interrupt function that starts a test when TX pin is pulled high + * @param dev GPIO device + * @param cb Callback structure pointer + * @param pins */ void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index c46955a68..d28b71f26 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -24,13 +24,13 @@ void test_end_beep(); /** * @brief Set ematch gpio pin - * @param level Value assigned to pin + * @param level Value to assign to pin */ void set_ematch(int level); /** * @brief Set ldo gpio pin - * @param level Value assigned to pin + * @param level Value to assign to pin */ void set_ldo(int level); diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index de3b1387e..d8385ef6c 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -6,7 +6,7 @@ #define MAX_TESTS 30 #define STORAGE_THREAD_PRIORITY 1 -#define ADC_READ_PRIORITY -1 #define THREAD_START_DELAY 100 +#define SYS_INIT_PRIORITY 1 #endif // CONFIG_H \ No newline at end of file diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 8e3511006..89687a85a 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -36,7 +36,7 @@ K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, 15, 0, THR static uint32_t adc_buffer; static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = sizeof(adc_buffer), .resolution = 24}; -#define DT_SPEC_AND_COMMA(node_id, prop, idx) ADC_DT_SPEC_GET_BY_IDX(node_id, idx), +#define DT_SPEC_AND_COMMA(node_id, prop, idx) ADC_DT_SPEC_GET_BY_IDX(node_id, idx), // ? static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; @@ -106,10 +106,9 @@ void adc_reading_task() { if (num_expiries == 0) { k_timer_status_sync(&adc_timer); } else { - num_missed_expires += num_expiries - 1; + num_missed_expires += num_expiries -1; } - // Set ematch 500ms into test if (x == 500) { set_ematch(1); } else if (x == 900) { diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 084db74ed..a299bda86 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -49,6 +49,7 @@ void beep_full() { set_buzz(0); k_msleep(1000); } + printk("\n"); } void test_start_beep() { @@ -59,10 +60,11 @@ void test_start_beep() { set_buzz(0); k_msleep(100); } + printk("\n"); } void test_end_beep() { - printk("BEEP"); + printk("BEEEEEP\n"); set_buzz(1); k_msleep(100); set_buzz(0); diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index ea1004372..f65156c61 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -84,8 +84,6 @@ static void save_metadata() { if (ret < 0) { LOG_ERR("flash_write(metadata) failed: %d", ret); - } else { - LOG_INF("Saved current_test_number = %u", current_test_number); } } @@ -110,7 +108,7 @@ static void flash_storage_thread_entry() { if (event != BEGIN_STORAGE) continue; if (current_test_number >= MAX_TESTS) { - LOG_ERR("Maximum number of test reached"); + LOG_ERR("Maximum number of tests reached!"); beep_full(); continue; } @@ -181,7 +179,7 @@ void stop_flash_storage() { int flash_dump_one(const struct shell *shell, uint32_t test_index) { if (test_index >= MAX_TESTS ){ - shell_print(shell, "Pick a valid test, 0-29"); + shell_print(shell, "Pick a valid test [0-29]"); return -1; } struct adc_sample sample; @@ -197,17 +195,20 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index) { return 0; } - shell_print(shell, "==============Dumping Test %d==============", test_index); + shell_print(shell, "==============Dumping Test #%d===============", test_index); + shell_print(shell, "==============timestamp, value=============="); for (int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++) { if (flash_read(flash_dev, block_addr, &sample, sizeof(sample)) < 0) { shell_print(shell, "Flash read failed"); break; } + if (sample.value == 0xFFFFFFFF && sample.timestamp == 0xFFFFFFFF) { shell_print(shell, "Flash block unwritten. Read %d packets", i); break; } + shell_print(shell, "%u,%d", sample.timestamp, sample.value); block_addr += sizeof(sample); } diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 0f1829028..07393e877 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -1,17 +1,19 @@ #include "adc_reading.h" #include "buzzer.h" #include "button.h" +#include "config.h" #include #include LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); -SYS_INIT(adc_init, APPLICATION, 0); -SYS_INIT(buzzer_init, APPLICATION, 0); -SYS_INIT(button_init, APPLICATION, 0); +// APPLICATION: Executed just before application code (main) +SYS_INIT(adc_init, APPLICATION, SYS_INIT_PRIORITY); +SYS_INIT(buzzer_init, APPLICATION, SYS_INIT_PRIORITY); +SYS_INIT(button_init, APPLICATION, SYS_INIT_PRIORITY); -int main (void) { +int main(void) { LOG_INF("Solids Test Start"); LOG_INF("Use 'test start' to begin test"); diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index 8f8d182b8..c36e8f77f 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -38,7 +38,7 @@ static int cmd_test_stop(const struct shell *shell, size_t argc, char **argv) { static int cmd_test_erase(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); - shell_print(shell, "Erasing all test data......"); + shell_print(shell, "Erasing all test data..."); control_erase_all(shell); return 0; From 3955b5579c5475e69c3664cc251b6becead0a53f Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 10 Nov 2025 19:31:47 -0500 Subject: [PATCH 28/43] logging fixed --- app/other/solids_test/include/flash_storage.h | 5 ++++- app/other/solids_test/prj.conf | 9 ++++----- app/other/solids_test/src/adc_reading.c | 3 +-- app/other/solids_test/src/buzzer.c | 6 +++--- app/other/solids_test/src/flash_storage.c | 4 +++- app/other/solids_test/src/main.c | 3 +-- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index 935b5cd6f..bcfee3839 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -6,7 +6,7 @@ /** * @brief Begin flash storage event - * @return 0 if message queue put successful + * @return 0 if successful */ int start_flash_storage(); @@ -19,18 +19,21 @@ void stop_flash_storage(); * @brief Dumps one ADC test from flash storage * @param shell Pointer to shell instance * @param test_index The test number to dump + * @return 0 if successful */ int flash_dump_one(const struct shell *shell, uint32_t test_index); /** * @brief Dumps all ADC data from flash storage * @param shell Pointer to shell instance + * @return 0 if successful */ int flash_dump_all(const struct shell *shell); /** * @brief Clear all flash blocks * @param shell Pointer to shell instance + * @return 0 if successful */ int flash_erase_all(const struct shell *shell); diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index 46176cb54..2fc06896b 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -11,18 +11,17 @@ CONFIG_SPEED_OPTIMIZATIONS=y CONFIG_SYS_CLOCK_TICKS_PER_SEC=100000 CONFIG_LOG=y -CONFIG_LOG_MODE_IMMEDIATE=y +CONFIG_LOG_MODE_IMMEDIATE=n CONFIG_CRC=y CONFIG_SMF=y CONFIG_EVENTS=y CONFIG_BASE64=y -CONFIG_SPI_STM32_DMA=y - CONFIG_GPIO=y -CONFIG_SPI=y CONFIG_I2C=y +CONFIG_SPI=y +CONFIG_SPI_STM32_DMA=y CONFIG_ADC=y CONFIG_ADC_LOG_LEVEL_ERR=y # Will spam annoying error at wrn level @@ -48,4 +47,4 @@ CONFIG_FILE_SYSTEM=y CONFIG_SHELL=y CONFIG_FLASH_SHELL=y CONFIG_SHELL_BACKEND_SERIAL=y -CONFIG_UART_CONSOLE=y +CONFIG_UART_CONSOLE=y \ No newline at end of file diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 89687a85a..7e16d9633 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -36,13 +36,12 @@ K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, 15, 0, THR static uint32_t adc_buffer; static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = sizeof(adc_buffer), .resolution = 24}; -#define DT_SPEC_AND_COMMA(node_id, prop, idx) ADC_DT_SPEC_GET_BY_IDX(node_id, idx), // ? +#define DT_SPEC_AND_COMMA(node_id, prop, idx) ADC_DT_SPEC_GET_BY_IDX(node_id, idx), static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; int adc_init() { - LOG_INF("Initializing..."); if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); return -1; diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index a299bda86..6e83f189a 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -37,13 +37,13 @@ int buzzer_init() { LOG_ERR("Failed to conf ldo enable pin :("); return -1; } - + return 0; } void beep_full() { for (int i = 0; i < 10; i++) { - printk("BEEP"); + printk("BEEP "); set_buzz(1); k_msleep(1000); set_buzz(0); @@ -54,7 +54,7 @@ void beep_full() { void test_start_beep() { for (int i = 0; i < 3; i++) { - printk("BEEP"); + printk("BEEP "); set_buzz(1); k_msleep(100); set_buzz(0); diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index f65156c61..a34de03d4 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -68,7 +68,7 @@ static void load_metadata() { memcpy(¤t_test_number, buf, sizeof(current_test_number)); } - LOG_INF("Loaded test number %d", current_test_number); + LOG_INF("Next test: %d", current_test_number); } static void save_metadata() { @@ -85,6 +85,8 @@ static void save_metadata() { if (ret < 0) { LOG_ERR("flash_write(metadata) failed: %d", ret); } + + LOG_INF("Next test: %d", current_test_number); } static off_t get_test_block_addr(uint32_t test_index) { diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 07393e877..3ae1d464c 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -17,7 +17,6 @@ int main(void) { LOG_INF("Solids Test Start"); LOG_INF("Use 'test start' to begin test"); - LOG_INF("Commands: test start | test stop | test dump [optional test #] | test erase | test read [optional #]"); - + LOG_INF("Use 'test help' to see all available commands"); return 0; } \ No newline at end of file From d2b1cf42181b25440db8a43d706433c60db956da Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 1 Dec 2025 11:32:13 -0500 Subject: [PATCH 29/43] don't blow up solids members code (calibration name, key switch, very long beep) --- .../solids_test/boards/grim_reefer.overlay | 4 +- app/other/solids_test/include/adc_reading.h | 10 ++-- app/other/solids_test/include/button.h | 17 +++++- app/other/solids_test/include/buzzer.h | 29 +++++++--- app/other/solids_test/include/config.h | 2 +- app/other/solids_test/include/control.h | 10 +++- app/other/solids_test/include/flash_storage.h | 3 +- app/other/solids_test/prj.conf | 21 +++---- app/other/solids_test/src/adc_reading.c | 25 ++++++--- app/other/solids_test/src/button.c | 55 ++++++++++++++++++- app/other/solids_test/src/buzzer.c | 13 +++++ app/other/solids_test/src/control.c | 22 ++++++-- app/other/solids_test/src/flash_storage.c | 35 ++++++++++-- app/other/solids_test/src/main.c | 5 +- app/other/solids_test/src/shell_cmds.c | 17 +++++- 15 files changed, 209 insertions(+), 59 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 4ec6e417e..0bf38e709 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -12,11 +12,11 @@ }; leds: leds { - tx: tx{ + tx: tx{ // button gpios = <&gpioa 0 GPIO_ACTIVE_HIGH>; label = "TX"; }; - rx: rx{ + rx: rx{ // key switch gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; label = "RX"; }; diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index 654fcd280..eefa19e28 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -2,15 +2,14 @@ #define ADC_READING_H #include +#include /** * @brief Struct to hold one ADC sample */ struct adc_sample { - /** Timestamp the sample was recorded in ms */ - uint32_t timestamp; - /** Value of the sample */ - int32_t value; + uint32_t timestamp; /** Timestamp the sample was recorded in ms */ + int32_t value; /** Value of the sample */ }; /** @@ -32,8 +31,9 @@ void adc_reading_task(); /** * @brief Sets ADC control event to start + * @param terminal_test Whether test was triggered by terminal cmd or meep */ -void adc_start_reading(); +void adc_start_reading(bool terminal_test); /** * @brief Sets ADC control event to end diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index 59ff9da03..848cfba80 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -9,14 +9,27 @@ * @brief Configures TX and RX pins as gpio * @return 0 if successful */ -int button_init(); +int button_switch_init(); /** * @brief Interrupt function that starts a test when TX pin is pulled high * @param dev GPIO device * @param cb Callback structure pointer - * @param pins + * @param pins Interrupt pins */ void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); +/** + * @brief Interrupt function that toggles ematch and does something else + * @param dev GPIO device + * @param cb Callback structure pointer + * @param pins Interrupt pins + */ +void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32_t pins); + +/** + * @brief Thread to beep continuously if key switch is closed and test is not started + */ +void buzzer_task(); + #endif // BUTTON_H \ No newline at end of file diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index d28b71f26..f14cbc3fa 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -7,6 +7,24 @@ */ int buzzer_init(); +/** + * @brief Start or stop buzzer + * @param which Value to set buzzer + */ +void set_buzz(int which); + +/** + * @brief Set ematch gpio pin + * @param level Value to assign to pin + */ +void set_ematch(int level); + +/** + * @brief Set ldo gpio pin + * @param level Value to assign to pin + */ +void set_ldo(int level); + /** * @brief Beep loudly in 1 second intervals for 10 seconds to indicate that flash is full (max tests reached) */ @@ -23,15 +41,8 @@ void test_start_beep(); void test_end_beep(); /** - * @brief Set ematch gpio pin - * @param level Value to assign to pin - */ -void set_ematch(int level); - -/** - * @brief Set ldo gpio pin - * @param level Value to assign to pin + * @brief Beep forever */ -void set_ldo(int level); +void continuous_beep(); #endif // BUZZER_H \ No newline at end of file diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index d8385ef6c..6c4328294 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG_H #define CONFIG_H -#define SAMPLE_RATE_HZ 1000 +#define SAMPLE_RATE 1000 #define TEST_DURATION 10000 // ms #define MAX_TESTS 30 diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index f03860e4a..e478c6065 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -6,8 +6,10 @@ /** * @brief Starts flash storage and ADC reading + * @param calib_name Name of calibration to store in flash storage + * @param terminal_test Whether test was triggered by terminal cmd or meep */ -void control_start_test(); +void control_start_test(char calib_name[], bool terminal_test); /** * @brief Stops flash storage and ADC reading @@ -52,4 +54,10 @@ void control_set_ematch(const struct shell *shell); */ void control_stop_ematch(const struct shell *shell); +/** + * @brief Get status of test + * @return Whether a test is running or not + */ +bool control_get_test_status(); + #endif // CONTROL_H \ No newline at end of file diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index bcfee3839..8479a9a94 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -6,9 +6,10 @@ /** * @brief Begin flash storage event + * @param calib_name Name of calibration to store. Defaults to "Test [#]" if empty or default string passed in * @return 0 if successful */ -int start_flash_storage(); +int start_flash_storage(char calib_name[]); /** * @brief End flash storage event diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index 2fc06896b..d60109548 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -1,12 +1,5 @@ -#CONFIG_THREAD_NAME=y -#CONFIG_SEGGER_SYSTEMVIEW=y -#CONFIG_USE_SEGGER_RTT=y #see point 1 above -#CONFIG_TRACING=y -#CONFIG_TRACING_BACKEND_RAM=y # see point 2 above - CONFIG_SENSOR=y CONFIG_SENSOR_SHELL=y -CONFIG_INA260=y CONFIG_SPEED_OPTIMIZATIONS=y CONFIG_SYS_CLOCK_TICKS_PER_SEC=100000 @@ -14,10 +7,10 @@ CONFIG_LOG=y CONFIG_LOG_MODE_IMMEDIATE=n CONFIG_CRC=y -CONFIG_SMF=y CONFIG_EVENTS=y CONFIG_BASE64=y +# GPIO and sensor enables CONFIG_GPIO=y CONFIG_I2C=y CONFIG_SPI=y @@ -31,20 +24,22 @@ CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 CONFIG_ADC_MCP356XR_THREAD_PRIORITY=21 CONFIG_INA260=y -CONFIG_SERIAL=y -CONFIG_UART_INTERRUPT_DRIVEN=y -CONFIG_CBPRINTF_FP_SUPPORT=y - CONFIG_SPI_NOR_SFDP_RUNTIME=y CONFIG_SPI_NOR_SLEEP_ERASE_MS=10 +# File system CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_FLASH_PAGE_LAYOUT=y CONFIG_DISK_ACCESS=y CONFIG_FILE_SYSTEM=y +# Data dumping CONFIG_SHELL=y CONFIG_FLASH_SHELL=y CONFIG_SHELL_BACKEND_SERIAL=y -CONFIG_UART_CONSOLE=y \ No newline at end of file +CONFIG_UART_CONSOLE=y + +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_CBPRINTF_FP_SUPPORT=y \ No newline at end of file diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 7e16d9633..0c1f71b02 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -41,6 +42,8 @@ static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = siz static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; +bool terminal = true; + int adc_init() { if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); @@ -84,7 +87,7 @@ void adc_reading_task() { k_msleep(2000); test_start_beep(); - k_timer_start(&adc_timer, K_USEC(SAMPLE_RATE_HZ), K_USEC(SAMPLE_RATE_HZ)); // 1000Hz periods + k_timer_start(&adc_timer, K_USEC(SAMPLE_RATE), K_USEC(SAMPLE_RATE)); int x = 0; uint32_t dropped_samples = 0; @@ -102,16 +105,19 @@ void adc_reading_task() { } uint32_t num_expiries = k_timer_status_get(&adc_timer); - if (num_expiries == 0) { + if (num_expiries == 0) { // timer still running k_timer_status_sync(&adc_timer); - } else { + } else { // timer expired num_missed_expires += num_expiries -1; } - if (x == 500) { - set_ematch(1); - } else if (x == 900) { - set_ematch(0); + // Only set off ematch if test is triggered by meep + if (!terminal) { + if (x == 500) { + set_ematch(1); + } else if (x == 900) { + set_ematch(0); + } } // Read from ADC @@ -146,6 +152,9 @@ void adc_reading_task() { } } -void adc_start_reading() { k_event_set(&adc_control_event, BEGIN_READING_EVENT); } +void adc_start_reading(bool terminal_test) { + terminal = terminal_test; // Whether test was triggered by terminal command or meep + k_event_set(&adc_control_event, BEGIN_READING_EVENT); +} void adc_stop_recording() { k_event_set(&adc_control_event, STOP_READING_EVENT); } \ No newline at end of file diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index 51b8346df..8cd52c987 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -1,6 +1,8 @@ #include "button.h" #include "control.h" +#include "buzzer.h" +#include #include #include #include @@ -13,10 +15,18 @@ LOG_MODULE_REGISTER(button, LOG_LEVEL_INF); #define RX_NODE DT_NODELABEL(rx) static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(TX_NODE, gpios); static const struct gpio_dt_spec rx = GPIO_DT_SPEC_GET(RX_NODE, gpios); + static struct gpio_callback button_cb_data; +static struct gpio_callback switch_cb_data; + +static bool key_switched = false; +static bool buzzing = false; -int button_init() { - int ret = gpio_pin_configure_dt(&rx, GPIO_OUTPUT_ACTIVE); +K_THREAD_DEFINE(buzz_thread, 512, buzzer_task, NULL, NULL, NULL, 10, 0, 0); + +// tx = button, rx = key switch +int button_switch_init() { + int ret = gpio_pin_configure_dt(&rx, GPIO_INPUT); if (ret < 0) { LOG_ERR("Failed to conf rx :("); return -1; @@ -39,13 +49,52 @@ int button_init() { return -1; } + ret = gpio_pin_interrupt_configure_dt(&rx, GPIO_INT_EDGE_BOTH); + if (ret != 0) { + LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, rx.port->name, rx.pin); + return -1; + } + gpio_init_callback(&button_cb_data, button_pressed, BIT(tx.pin)); gpio_add_callback(tx.port, &button_cb_data); + gpio_init_callback(&switch_cb_data, key_switch_state, BIT(rx.pin)); + gpio_add_callback(rx.port, &switch_cb_data); + return 0; } void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { LOG_INF("Starting test..."); - control_start_test(); + control_start_test("", false); +} + +void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { + int val = gpio_pin_get_dt(&rx); + if (val > 0) { + key_switched = true; + set_ematch(1); + LOG_INF("Key switch closed"); + + if (!control_get_test_status()) { + buzzing = true; // scream until test start + } + } else { + key_switched = false; + buzzing = false; + set_buzz(0); + set_ematch(0); + LOG_INF("Key switch open"); + } +} + +void buzzer_task() { + while (1) { + if (buzzing && !control_get_test_status()) { + continuous_beep(); + } else { + set_buzz(0); + k_msleep(50); + } + } } \ No newline at end of file diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 6e83f189a..fdb0c81b9 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -13,6 +13,8 @@ const struct gpio_dt_spec ldo_enable = GPIO_DT_SPEC_GET(LDO_EN_NODE, gpios); static const struct gpio_dt_spec buzzer = GPIO_DT_SPEC_GET(DT_ALIAS(buzzer), gpios); static const struct gpio_dt_spec ematch = GPIO_DT_SPEC_GET(CAM_EN_NODE, gpios); +bool test_running = false; + void set_buzz(int which) { gpio_pin_set_dt(&ldo_enable, which); gpio_pin_set_dt(&buzzer, which); @@ -53,6 +55,7 @@ void beep_full() { } void test_start_beep() { + test_running = true; for (int i = 0; i < 3; i++) { printk("BEEP "); set_buzz(1); @@ -64,6 +67,7 @@ void test_start_beep() { } void test_end_beep() { + test_running = false; printk("BEEEEEP\n"); set_buzz(1); k_msleep(100); @@ -72,4 +76,13 @@ void test_end_beep() { set_buzz(1); k_msleep(1000); set_buzz(0); +} + +void continuous_beep() { + printk("BEEEEEEEEEEEEEEEEEEEEEEEP\n"); + while (!test_running) { + set_buzz(1); + k_msleep(10); + } + set_buzz(0); } \ No newline at end of file diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index 29f34473e..9e115301f 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -13,8 +14,15 @@ LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); static bool test_running = false; +static char curr_name[32]; + +void control_start_test(char calib_name[], bool terminal_test) { + // Set calibration name. If string is empty, get calibration name from previous test + // Assume name passed in from shell cmd before actual test, otherwise default name will be set in flash_storage + if (calib_name && calib_name[0] != '\0') { + memcpy(curr_name, calib_name, sizeof(curr_name)); + } -void control_start_test() { if (test_running) { LOG_WRN("Test already running"); return; @@ -22,8 +30,8 @@ void control_start_test() { test_running = true; - start_flash_storage(); - adc_start_reading(); + start_flash_storage(curr_name); + adc_start_reading(terminal_test); } void control_stop_test() { @@ -61,10 +69,14 @@ void control_erase_all(const struct shell *shell) { flash_erase_all(shell); } void control_set_ematch(const struct shell *shell) { set_ematch(1); - shell_print(shell, "Ematch: 1"); + shell_print(shell, "Ematch enabled"); } void control_stop_ematch(const struct shell *shell) { set_ematch(0); - shell_print(shell, "Ematch: 0"); + shell_print(shell, "Ematch disabled"); +} + +bool control_get_test_status() { + return test_running; } \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index a34de03d4..7df10fafb 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -1,10 +1,12 @@ #include "flash_storage.h" -#include "buzzer.h" #include "adc_reading.h" +#include "buzzer.h" #include "config.h" #include #include +#include +#include #include #include #include @@ -36,6 +38,8 @@ static void flash_storage_thread_entry(void); K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, STORAGE_THREAD_PRIORITY, 0, 1000); +char calibration[32]; + // Check if flash block is all 0xFF static bool flash_block_is_empty(off_t addr) { uint8_t buf[16]; @@ -101,7 +105,6 @@ static void flash_storage_thread_entry() { return; } - // Get current test number load_metadata(); while (1) { @@ -127,9 +130,16 @@ static void flash_storage_thread_entry() { current_write_addr = test_block_addr; + flash_write(flash_dev, current_write_addr, calibration, 32); + current_write_addr += 32; + + // flash_write(flash_dev, current_write_addr, &curr_test_type, sizeof(curr_test_type)); + // current_write_addr += 4; + static struct adc_sample page[SAMPLE_PER_PAGE] = {0}; size_t i = 0; size_t pages = 0; + // buffer here, write calibration name to flash block while (1) { if (k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE) { LOG_INF("Test %d complete", current_test_number); @@ -168,7 +178,16 @@ static void flash_storage_thread_entry() { } } -int start_flash_storage() { +int start_flash_storage(char calib_name[]) { + // Set calibration name + int res1 = strcmp(calib_name, "default"); + int res2 = strcmp(calib_name, ""); + if (res1 == 0 || res2 == 0) { + snprintf(calibration, sizeof(calibration), "Test %u", current_test_number); + } else { + snprintf(calibration, sizeof(calibration), "%s", calib_name); + } + enum storage_event event = BEGIN_STORAGE; return k_msgq_put(&storage_control_queue, &event, K_FOREVER); } @@ -178,7 +197,6 @@ void stop_flash_storage() { k_msgq_put(&storage_control_queue, &event, K_FOREVER); } - int flash_dump_one(const struct shell *shell, uint32_t test_index) { if (test_index >= MAX_TESTS ){ shell_print(shell, "Pick a valid test [0-29]"); @@ -198,6 +216,15 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index) { } shell_print(shell, "==============Dumping Test #%d===============", test_index); + // print calibration name from flash block + // should we print if a test was a terminal or meep test? + char calib_name[32]; + + flash_read(flash_dev, block_addr, calib_name, sizeof(calib_name)); + calib_name[31] = '\0'; + block_addr += 32; + + shell_print(shell, "==============CALIBRATION: %s==============", calib_name); shell_print(shell, "==============timestamp, value=============="); for (int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++) { diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 3ae1d464c..1573b1ff3 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -11,12 +11,11 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); // APPLICATION: Executed just before application code (main) SYS_INIT(adc_init, APPLICATION, SYS_INIT_PRIORITY); SYS_INIT(buzzer_init, APPLICATION, SYS_INIT_PRIORITY); -SYS_INIT(button_init, APPLICATION, SYS_INIT_PRIORITY); +SYS_INIT(button_switch_init, APPLICATION, SYS_INIT_PRIORITY); int main(void) { LOG_INF("Solids Test Start"); - - LOG_INF("Use 'test start' to begin test"); + LOG_INF("Use 'test start [calibration name]' to begin test"); LOG_INF("Use 'test help' to see all available commands"); return 0; } \ No newline at end of file diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index c36e8f77f..6ab638c16 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -3,6 +3,7 @@ #include "flash_storage.h" #include +#include #include #include #include @@ -12,8 +13,20 @@ LOG_MODULE_REGISTER(shell_cmds, LOG_LEVEL_INF); static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); + + // If solids members need more than 32 characters then they need better naming standards + char calib_name[32] = "default"; // If no name arg, set to default. Name will be set to "Test [#]" in flash_storage + if (argc >= 2) { + calib_name[0] = '\0'; // clear "default" + for (int i = 1; i < argc; i++) { + snprintf(calib_name + strlen(calib_name), + sizeof(calib_name) - strlen(calib_name), + i == 1 ? "%s" : " %s", argv[i]); + } + } + shell_print(shell, "Starting test..."); - control_start_test(); + control_start_test(calib_name, true); return 0; } @@ -79,7 +92,7 @@ static int cmd_test_estop(const struct shell *shell, size_t argc, char **argv) { return 0; } -SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, SHELL_CMD(start, NULL, "Start test", cmd_test_start), +SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, SHELL_CMD(start, NULL, "Start test. Arg [calibration name]", cmd_test_start), SHELL_CMD(stop, NULL, "Stop test preemptively", cmd_test_stop), SHELL_CMD(dump, NULL, "Dump flash data. Optional arg [test #]", cmd_test_dump), SHELL_CMD(erase, NULL, "Erase all flash data, prepare for new tests", cmd_test_erase), From 2c33dde717cb6c3b00afc61a3f825b60ebda315f Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 1 Dec 2025 11:47:12 -0500 Subject: [PATCH 30/43] buzzer task tested --- app/other/solids_test/src/button.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index 8cd52c987..1b84a3ac6 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -43,13 +43,13 @@ int button_switch_init() { return -1; } - ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_TO_ACTIVE); + ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_BOTH); if (ret != 0) { LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, tx.port->name, tx.pin); return -1; } - ret = gpio_pin_interrupt_configure_dt(&rx, GPIO_INT_EDGE_BOTH); + ret = gpio_pin_interrupt_configure_dt(&rx, GPIO_INT_EDGE_BOTH); // GPIO_INT_EDGE_TO_ACTIVE? if (ret != 0) { LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, rx.port->name, rx.pin); return -1; @@ -79,6 +79,7 @@ void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32 if (!control_get_test_status()) { buzzing = true; // scream until test start } + } else { key_switched = false; buzzing = false; From 827931fdfb0133d55d58eb49d565696b1d102fec Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 1 Dec 2025 12:00:29 -0500 Subject: [PATCH 31/43] comments, formatting, updated continuous_beep --- app/other/solids_test/include/buzzer.h | 2 +- app/other/solids_test/include/control.h | 2 +- app/other/solids_test/prj.conf | 12 +++++++----- app/other/solids_test/src/buzzer.c | 2 ++ app/other/solids_test/src/flash_storage.c | 7 ++++--- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index f14cbc3fa..57c38b475 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -41,7 +41,7 @@ void test_start_beep(); void test_end_beep(); /** - * @brief Beep forever + * @brief Beep until test starts */ void continuous_beep(); diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index e478c6065..466b5cff5 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -55,7 +55,7 @@ void control_set_ematch(const struct shell *shell); void control_stop_ematch(const struct shell *shell); /** - * @brief Get status of test + * @brief Get status of test * @return Whether a test is running or not */ bool control_get_test_status(); diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index d60109548..8ac6e098b 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -3,19 +3,21 @@ CONFIG_SENSOR_SHELL=y CONFIG_SPEED_OPTIMIZATIONS=y CONFIG_SYS_CLOCK_TICKS_PER_SEC=100000 +CONFIG_EVENTS=y +CONFIG_BASE64=y + +# Logging CONFIG_LOG=y CONFIG_LOG_MODE_IMMEDIATE=n CONFIG_CRC=y -CONFIG_EVENTS=y -CONFIG_BASE64=y - -# GPIO and sensor enables +# GPIO and bus enables CONFIG_GPIO=y CONFIG_I2C=y CONFIG_SPI=y CONFIG_SPI_STM32_DMA=y +# ADC + sensor enables CONFIG_ADC=y CONFIG_ADC_LOG_LEVEL_ERR=y # Will spam annoying error at wrn level @@ -27,7 +29,7 @@ CONFIG_INA260=y CONFIG_SPI_NOR_SFDP_RUNTIME=y CONFIG_SPI_NOR_SLEEP_ERASE_MS=10 -# File system +# Flash + file system CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_FLASH_PAGE_LAYOUT=y diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index fdb0c81b9..a3880a925 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -1,4 +1,5 @@ #include "buzzer.h" +#include "control.h" #include #include @@ -83,6 +84,7 @@ void continuous_beep() { while (!test_running) { set_buzz(1); k_msleep(10); + test_running = control_get_test_status(); } set_buzz(0); } \ No newline at end of file diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 7df10fafb..47ec9d470 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -133,13 +133,14 @@ static void flash_storage_thread_entry() { flash_write(flash_dev, current_write_addr, calibration, 32); current_write_addr += 32; - // flash_write(flash_dev, current_write_addr, &curr_test_type, sizeof(curr_test_type)); - // current_write_addr += 4; + // if we include test type, implement this + // flash_write(flash_dev, current_write_addr, [test type], sizeof([test type])); + // current_write_addr += sizeof([test type]); static struct adc_sample page[SAMPLE_PER_PAGE] = {0}; size_t i = 0; size_t pages = 0; - // buffer here, write calibration name to flash block + while (1) { if (k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE) { LOG_INF("Test %d complete", current_test_number); From 9ae27cafd7028e9bd56a5bf6b62d710aaecd0943 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 1 Dec 2025 19:10:08 -0500 Subject: [PATCH 32/43] print whether test is terminal or meep, terminal formatting --- app/other/solids_test/include/flash_storage.h | 3 +- app/other/solids_test/src/adc_reading.c | 3 +- app/other/solids_test/src/buzzer.c | 2 +- app/other/solids_test/src/control.c | 2 +- app/other/solids_test/src/flash_storage.c | 29 +++++++++++-------- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index 8479a9a94..80998e48b 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -7,9 +7,10 @@ /** * @brief Begin flash storage event * @param calib_name Name of calibration to store. Defaults to "Test [#]" if empty or default string passed in + * @param terminal_test Whether test was triggered by terminal cmd or meep * @return 0 if successful */ -int start_flash_storage(char calib_name[]); +int start_flash_storage(char calib_name[], bool terminal_test); /** * @brief End flash storage event diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 0c1f71b02..0bbbf9102 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -36,14 +36,13 @@ K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, 15, 0, THR static uint32_t adc_buffer; static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = sizeof(adc_buffer), .resolution = 24}; +static bool terminal = true; #define DT_SPEC_AND_COMMA(node_id, prop, idx) ADC_DT_SPEC_GET_BY_IDX(node_id, idx), static const struct adc_dt_spec adc_channels[] = { DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, DT_SPEC_AND_COMMA)}; -bool terminal = true; - int adc_init() { if (!adc_is_ready_dt(&adc_channels[0])) { LOG_ERR("ADC controller device %s not ready\n", adc_channels[0].dev->name); diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index a3880a925..11a1eee26 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -14,7 +14,7 @@ const struct gpio_dt_spec ldo_enable = GPIO_DT_SPEC_GET(LDO_EN_NODE, gpios); static const struct gpio_dt_spec buzzer = GPIO_DT_SPEC_GET(DT_ALIAS(buzzer), gpios); static const struct gpio_dt_spec ematch = GPIO_DT_SPEC_GET(CAM_EN_NODE, gpios); -bool test_running = false; +static bool test_running = false; void set_buzz(int which) { gpio_pin_set_dt(&ldo_enable, which); diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index 9e115301f..341e268c2 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -30,7 +30,7 @@ void control_start_test(char calib_name[], bool terminal_test) { test_running = true; - start_flash_storage(curr_name); + start_flash_storage(curr_name, terminal_test); adc_start_reading(terminal_test); } diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 47ec9d470..bf8787373 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -33,12 +33,13 @@ K_MSGQ_DEFINE(adc_data_queue, sizeof(struct adc_sample), 1000, alignof(struct ad static const struct device *flash_dev = DEVICE_DT_GET(DT_ALIAS(storage)); static uint32_t current_test_number = 0; static off_t current_write_addr = 0; +static char test_type[] = "terminal"; static void flash_storage_thread_entry(void); K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, STORAGE_THREAD_PRIORITY, 0, 1000); -char calibration[32]; +static char calibration[32]; // Check if flash block is all 0xFF static bool flash_block_is_empty(off_t addr) { @@ -130,12 +131,12 @@ static void flash_storage_thread_entry() { current_write_addr = test_block_addr; + // Save calibration name and test type (terminal or meep) flash_write(flash_dev, current_write_addr, calibration, 32); current_write_addr += 32; - // if we include test type, implement this - // flash_write(flash_dev, current_write_addr, [test type], sizeof([test type])); - // current_write_addr += sizeof([test type]); + flash_write(flash_dev, current_write_addr, test_type, sizeof(test_type)); + current_write_addr += sizeof(test_type); static struct adc_sample page[SAMPLE_PER_PAGE] = {0}; size_t i = 0; @@ -179,7 +180,7 @@ static void flash_storage_thread_entry() { } } -int start_flash_storage(char calib_name[]) { +int start_flash_storage(char calib_name[], bool terminal_test) { // Set calibration name int res1 = strcmp(calib_name, "default"); int res2 = strcmp(calib_name, ""); @@ -189,6 +190,10 @@ int start_flash_storage(char calib_name[]) { snprintf(calibration, sizeof(calibration), "%s", calib_name); } + if (!terminal_test) { + snprintf(test_type, sizeof(test_type), "meep"); + } + enum storage_event event = BEGIN_STORAGE; return k_msgq_put(&storage_control_queue, &event, K_FOREVER); } @@ -216,17 +221,17 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index) { return 0; } - shell_print(shell, "==============Dumping Test #%d===============", test_index); - // print calibration name from flash block - // should we print if a test was a terminal or meep test? char calib_name[32]; - flash_read(flash_dev, block_addr, calib_name, sizeof(calib_name)); - calib_name[31] = '\0'; block_addr += 32; - shell_print(shell, "==============CALIBRATION: %s==============", calib_name); - shell_print(shell, "==============timestamp, value=============="); + flash_read(flash_dev, block_addr, test_type, sizeof(test_type)); + block_addr += sizeof(test_type); + + shell_print(shell, "================================\nDumping Test #%d", test_index); + shell_print(shell, "CALIBRATION: %s", calib_name); + shell_print(shell, "Test triggered by %s", test_type); + shell_print(shell, "timestamp, value\n================================"); for (int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++) { if (flash_read(flash_dev, block_addr, &sample, sizeof(sample)) < 0) { From afdbe5d8df0d3e9bce2b5a0739fcb42f1184cd59 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 1 Dec 2025 19:26:26 -0500 Subject: [PATCH 33/43] comments, rx/tx gpio interrupt conf flags --- app/other/solids_test/src/button.c | 4 ++-- app/other/solids_test/src/control.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index 1b84a3ac6..4ae02043e 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -43,13 +43,13 @@ int button_switch_init() { return -1; } - ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_BOTH); + ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, tx.port->name, tx.pin); return -1; } - ret = gpio_pin_interrupt_configure_dt(&rx, GPIO_INT_EDGE_BOTH); // GPIO_INT_EDGE_TO_ACTIVE? + ret = gpio_pin_interrupt_configure_dt(&rx, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, rx.port->name, rx.pin); return -1; diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index 341e268c2..adccb02a4 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -17,8 +17,8 @@ static bool test_running = false; static char curr_name[32]; void control_start_test(char calib_name[], bool terminal_test) { - // Set calibration name. If string is empty, get calibration name from previous test - // Assume name passed in from shell cmd before actual test, otherwise default name will be set in flash_storage + // If string is empty, get calibration name from last named test - name will stay until new one is passed in + // Default name will be set in flash_storage if no name specified in shell_cmd if (calib_name && calib_name[0] != '\0') { memcpy(curr_name, calib_name, sizeof(curr_name)); } From c65f31d3f87c16a74f21ca9aa4d00ded731af05f Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Mon, 8 Dec 2025 19:39:39 -0500 Subject: [PATCH 34/43] set buzzer back to buzzer instead of led :( --- .../solids_test/boards/grim_reefer.overlay | 3 +-- app/other/solids_test/src/adc_reading.c | 4 +-- app/other/solids_test/src/button.c | 1 - app/other/solids_test/src/buzzer.c | 26 +++++++++---------- app/other/solids_test/src/control.c | 4 +-- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 0bf38e709..3c9d60d95 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -7,8 +7,7 @@ aliases { adc0 = &adc; - // buzzer = &buzzer; - buzzer = &led1; // delete before pr + buzzer = &buzzer; }; leds: leds { diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index 0bbbf9102..feb55fe09 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -81,7 +81,7 @@ void adc_reading_task() { // Wait for start event k_event_wait(&adc_control_event, BEGIN_READING_EVENT, true, K_FOREVER); LOG_INF("ADC reading started"); - // set_ldo(1); + set_ldo(1); // Delay test 2 seconds, beep when test actually starts k_msleep(2000); test_start_beep(); @@ -140,7 +140,7 @@ void adc_reading_task() { } } - // set_ldo(0); + set_ldo(0); k_timer_stop(&adc_timer); LOG_INF( "number of samples: %d, %u missed, %u dropped, read time %llu, ms per = %.2f, loop time: %llu, loop time ticks: %llu", diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index 4ae02043e..f0fb65e33 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -79,7 +79,6 @@ void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32 if (!control_get_test_status()) { buzzing = true; // scream until test start } - } else { key_switched = false; buzzing = false; diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 11a1eee26..7c8e341e8 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -16,19 +16,6 @@ static const struct gpio_dt_spec ematch = GPIO_DT_SPEC_GET(CAM_EN_NODE, gpios); static bool test_running = false; -void set_buzz(int which) { - gpio_pin_set_dt(&ldo_enable, which); - gpio_pin_set_dt(&buzzer, which); -} - -void set_ldo(int level) { - gpio_pin_set_dt(&ldo_enable, level); -} - -void set_ematch(int level) { - gpio_pin_set_dt(&ematch, level); -} - int buzzer_init() { int ret = gpio_pin_configure_dt(&buzzer, GPIO_OUTPUT_INACTIVE); if (ret < 0) { @@ -44,6 +31,19 @@ int buzzer_init() { return 0; } +void set_buzz(int which) { + gpio_pin_set_dt(&ldo_enable, which); + gpio_pin_set_dt(&buzzer, which); +} + +void set_ldo(int level) { + gpio_pin_set_dt(&ldo_enable, level); +} + +void set_ematch(int level) { + gpio_pin_set_dt(&ematch, level); +} + void beep_full() { for (int i = 0; i < 10; i++) { printk("BEEP "); diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index adccb02a4..4ef84c938 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -47,7 +47,7 @@ void control_stop_test() { } void control_print_n(const struct shell *shell, int num) { - // set_ldo(1); + set_ldo(1); k_msleep(5000); uint32_t adc_val = 0; uint32_t start = k_uptime_ticks(); @@ -58,7 +58,7 @@ void control_print_n(const struct shell *shell, int num) { shell_print(shell, "%u, %d", k_ticks_to_us_near32(t), adc_val); k_msleep(1); } - // set_ldo(0); + set_ldo(0); } void control_dump_data(const struct shell *shell) { flash_dump_all(shell); } From be6d1599e93af331b03d499e644da4baf88a3398 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Fri, 19 Dec 2025 15:25:50 -0500 Subject: [PATCH 35/43] swap tx/rx and button/key_switch --- .../solids_test/boards/grim_reefer.overlay | 8 ++-- app/other/solids_test/src/button.c | 40 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 3c9d60d95..6bace055c 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -11,13 +11,13 @@ }; leds: leds { - tx: tx{ // button + button: button{ // TX gpios = <&gpioa 0 GPIO_ACTIVE_HIGH>; - label = "TX"; + label = "Button"; }; - rx: rx{ // key switch + key_switch: key_switch{ // RX gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; - label = "RX"; + label = "Key Switch"; }; }; }; diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index f0fb65e33..8d9ec1ae2 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -11,10 +11,11 @@ LOG_MODULE_REGISTER(button, LOG_LEVEL_INF); -#define TX_NODE DT_NODELABEL(tx) -#define RX_NODE DT_NODELABEL(rx) -static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(TX_NODE, gpios); -static const struct gpio_dt_spec rx = GPIO_DT_SPEC_GET(RX_NODE, gpios); +// button = tx, key switch = rx +#define BUTTON_NODE DT_NODELABEL(button) +#define SWITCH_NODE DT_NODELABEL(key_switch) +static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(BUTTON_NODE, gpios); +static const struct gpio_dt_spec key_switch = GPIO_DT_SPEC_GET(SWITCH_NODE, gpios); static struct gpio_callback button_cb_data; static struct gpio_callback switch_cb_data; @@ -24,42 +25,41 @@ static bool buzzing = false; K_THREAD_DEFINE(buzz_thread, 512, buzzer_task, NULL, NULL, NULL, 10, 0, 0); -// tx = button, rx = key switch int button_switch_init() { - int ret = gpio_pin_configure_dt(&rx, GPIO_INPUT); + int ret = gpio_pin_configure_dt(&key_switch, GPIO_INPUT); if (ret < 0) { - LOG_ERR("Failed to conf rx :("); + LOG_ERR("Failed to conf key switch (rx) :("); return -1; } - ret = gpio_pin_configure_dt(&tx, GPIO_INPUT); + ret = gpio_pin_configure_dt(&button, GPIO_INPUT); if (ret < 0) { - LOG_ERR("Failed to conf tx :("); + LOG_ERR("Failed to conf button (tx) :("); return -1; } - if (!gpio_is_ready_dt(&tx)) { - LOG_ERR("Error: button device %s is not ready\n", tx.port->name); + if (!gpio_is_ready_dt(&button)) { + LOG_ERR("Error: button device %s is not ready\n", button.port->name); return -1; } - ret = gpio_pin_interrupt_configure_dt(&tx, GPIO_INT_EDGE_TO_ACTIVE); + ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { - LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, tx.port->name, tx.pin); + LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, button.port->name, button.pin); return -1; } - ret = gpio_pin_interrupt_configure_dt(&rx, GPIO_INT_EDGE_TO_ACTIVE); + ret = gpio_pin_interrupt_configure_dt(&key_switch, GPIO_INT_EDGE_TO_ACTIVE); if (ret != 0) { - LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, rx.port->name, rx.pin); + LOG_ERR("Error %d: failed to configure interrupt on %s pin %d\n", ret, key_switch.port->name, key_switch.pin); return -1; } - gpio_init_callback(&button_cb_data, button_pressed, BIT(tx.pin)); - gpio_add_callback(tx.port, &button_cb_data); + gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin)); + gpio_add_callback(button.port, &button_cb_data); - gpio_init_callback(&switch_cb_data, key_switch_state, BIT(rx.pin)); - gpio_add_callback(rx.port, &switch_cb_data); + gpio_init_callback(&switch_cb_data, key_switch_state, BIT(key_switch.pin)); + gpio_add_callback(key_switch.port, &switch_cb_data); return 0; } @@ -70,7 +70,7 @@ void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t } void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { - int val = gpio_pin_get_dt(&rx); + int val = gpio_pin_get_dt(&key_switch); if (val > 0) { key_switched = true; set_ematch(1); From f71d87682526ec7b2789aa21c0d24cae5418da15 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Fri, 19 Dec 2025 15:58:07 -0500 Subject: [PATCH 36/43] clarifying comments --- app/other/solids_test/include/adc_reading.h | 5 +++-- app/other/solids_test/sample.yaml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index eefa19e28..ab68578f7 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -30,13 +30,14 @@ void adc_read_one(uint32_t *adc_val); void adc_reading_task(); /** - * @brief Sets ADC control event to start + * @brief Starts test * @param terminal_test Whether test was triggered by terminal cmd or meep + * If test was triggered by terminal, ematch will NOT light */ void adc_start_reading(bool terminal_test); /** - * @brief Sets ADC control event to end + * @brief Stops test */ void adc_stop_recording(); diff --git a/app/other/solids_test/sample.yaml b/app/other/solids_test/sample.yaml index 169ef60c1..a3ea542ea 100644 --- a/app/other/solids_test/sample.yaml +++ b/app/other/solids_test/sample.yaml @@ -4,6 +4,6 @@ sample: common: build_only: true platform_allow: - - grim_reefer + - solids_board tests: solids_test.default: {} From 038a463abd06d7732cd2f998d28509bff43f42f0 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Fri, 19 Dec 2025 16:23:01 -0500 Subject: [PATCH 37/43] sample name --- app/other/solids_test/sample.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/other/solids_test/sample.yaml b/app/other/solids_test/sample.yaml index a3ea542ea..f65d3b3ff 100644 --- a/app/other/solids_test/sample.yaml +++ b/app/other/solids_test/sample.yaml @@ -1,9 +1,9 @@ sample: description: - name: grim_reefer + name: solids_board common: build_only: true platform_allow: - - solids_board + - grim_reefer tests: solids_test.default: {} From 2ea3dde84af9d5a517110a3634a59e3bdef9c2a1 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sun, 28 Dec 2025 20:23:24 -0500 Subject: [PATCH 38/43] clarified comments, fixed values (spi max freq, added const calib name len) --- .../solids_test/boards/grim_reefer.overlay | 2 +- app/other/solids_test/include/adc_reading.h | 4 ++-- app/other/solids_test/include/button.h | 15 +++++++------- app/other/solids_test/include/buzzer.h | 6 +++--- app/other/solids_test/include/config.h | 1 + app/other/solids_test/include/control.h | 20 +++++++++---------- app/other/solids_test/include/flash_storage.h | 12 +++++------ app/other/solids_test/prj.conf | 2 +- app/other/solids_test/src/adc_reading.c | 6 +++--- app/other/solids_test/src/button.c | 2 +- app/other/solids_test/src/buzzer.c | 15 +++++++------- app/other/solids_test/src/control.c | 2 +- app/other/solids_test/src/flash_storage.c | 16 +++++++-------- app/other/solids_test/src/main.c | 2 ++ app/other/solids_test/src/shell_cmds.c | 4 ++-- 15 files changed, 56 insertions(+), 53 deletions(-) diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index 6bace055c..a397936d5 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -38,7 +38,7 @@ address = <1>; analog-clock-prescaler = <0>; boost-current-bias = <0>; - spi-max-frequency = ; + spi-max-frequency = ; irq-gpios = <&gpiob 14 GPIO_ACTIVE_LOW>; use-internal-clock; diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index ab68578f7..37837210a 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -20,7 +20,7 @@ int adc_init(); /** * @brief Read one ADC sample - * @param adc_val Pointer to value where sample will be written + * @param[out] adc_val Pointer to value where sample will be written */ void adc_read_one(uint32_t *adc_val); @@ -31,7 +31,7 @@ void adc_reading_task(); /** * @brief Starts test - * @param terminal_test Whether test was triggered by terminal cmd or meep + * @param[in] terminal_test Whether test was triggered by terminal cmd or meep * If test was triggered by terminal, ematch will NOT light */ void adc_start_reading(bool terminal_test); diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index 848cfba80..b2177a284 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -13,17 +13,18 @@ int button_switch_init(); /** * @brief Interrupt function that starts a test when TX pin is pulled high - * @param dev GPIO device - * @param cb Callback structure pointer - * @param pins Interrupt pins + * @param[in] dev GPIO device + * @param[in] cb Callback structure pointer + * @param[in] pins Interrupt pins */ void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); /** - * @brief Interrupt function that toggles ematch and does something else - * @param dev GPIO device - * @param cb Callback structure pointer - * @param pins Interrupt pins + * @brief Key switch interrupt function that sets ematch and checks test status + * If key switch is open and test is not started, buzzer will beep continuously + * @param[in] dev GPIO device + * @param[in] cb Callback structure pointer + * @param[in] pins Interrupt pins */ void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32_t pins); diff --git a/app/other/solids_test/include/buzzer.h b/app/other/solids_test/include/buzzer.h index 57c38b475..720f0f834 100644 --- a/app/other/solids_test/include/buzzer.h +++ b/app/other/solids_test/include/buzzer.h @@ -9,19 +9,19 @@ int buzzer_init(); /** * @brief Start or stop buzzer - * @param which Value to set buzzer + * @param[in] which Value to set buzzer */ void set_buzz(int which); /** * @brief Set ematch gpio pin - * @param level Value to assign to pin + * @param[in] level Value to assign to pin */ void set_ematch(int level); /** * @brief Set ldo gpio pin - * @param level Value to assign to pin + * @param[in] level Value to assign to pin */ void set_ldo(int level); diff --git a/app/other/solids_test/include/config.h b/app/other/solids_test/include/config.h index 6c4328294..bcc2682d4 100644 --- a/app/other/solids_test/include/config.h +++ b/app/other/solids_test/include/config.h @@ -4,6 +4,7 @@ #define SAMPLE_RATE 1000 #define TEST_DURATION 10000 // ms #define MAX_TESTS 30 +#define CALIB_NAME_MAX_LEN 32 // If solids members need more than 32 characters then they need better naming standards #define STORAGE_THREAD_PRIORITY 1 #define THREAD_START_DELAY 100 diff --git a/app/other/solids_test/include/control.h b/app/other/solids_test/include/control.h index 466b5cff5..ac649fa8d 100644 --- a/app/other/solids_test/include/control.h +++ b/app/other/solids_test/include/control.h @@ -6,8 +6,8 @@ /** * @brief Starts flash storage and ADC reading - * @param calib_name Name of calibration to store in flash storage - * @param terminal_test Whether test was triggered by terminal cmd or meep + * @param[in] calib_name Name of calibration to store in flash storage + * @param[in] terminal_test Whether test was triggered by terminal cmd or meep */ void control_start_test(char calib_name[], bool terminal_test); @@ -18,39 +18,39 @@ void control_stop_test(); /** * @brief Reads and prints n number of ADC samples - * @param shell Pointer to shell instance - * @param num Number of samples to read + * @param[in] shell Pointer to shell instance + * @param[in] num Number of samples to read */ void control_print_n(const struct shell *shell, int num); /** * @brief Dumps all ADC data from flash storage - * @param shell Pointer to shell instance + * @param[in] shell Pointer to shell instance */ void control_dump_data(const struct shell *shell); /** * @brief Dumps one ADC test from flash storage - * @param shell Pointer to shell instance - * @param test_index The test number to dump + * @param[in] shell Pointer to shell instance + * @param[in] test_index The test number to dump */ void control_dump_one(const struct shell *shell, uint32_t test_index); /** * @brief Clear all flash blocks - * @param shell Pointer to shell instance + * @param[in] shell Pointer to shell instance */ void control_erase_all(const struct shell *shell); /** * @brief Set ematch gpio high - * @param shell Pointer to shell instance + * @param[in] shell Pointer to shell instance */ void control_set_ematch(const struct shell *shell); /** * @brief Set ematch gpio low - * @param shell Pointer to shell instance + * @param[in] shell Pointer to shell instance */ void control_stop_ematch(const struct shell *shell); diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index 80998e48b..e1876209a 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -6,8 +6,8 @@ /** * @brief Begin flash storage event - * @param calib_name Name of calibration to store. Defaults to "Test [#]" if empty or default string passed in - * @param terminal_test Whether test was triggered by terminal cmd or meep + * @param[in] calib_name Name of calibration to store. Defaults to "Test [#]" if empty or default string passed in + * @param[in] terminal_test Whether test was triggered by terminal cmd or meep * @return 0 if successful */ int start_flash_storage(char calib_name[], bool terminal_test); @@ -19,22 +19,22 @@ void stop_flash_storage(); /** * @brief Dumps one ADC test from flash storage - * @param shell Pointer to shell instance - * @param test_index The test number to dump + * @param[in] shell Pointer to shell instance + * @param[in] test_index The test number to dump * @return 0 if successful */ int flash_dump_one(const struct shell *shell, uint32_t test_index); /** * @brief Dumps all ADC data from flash storage - * @param shell Pointer to shell instance + * @param[in] shell Pointer to shell instance * @return 0 if successful */ int flash_dump_all(const struct shell *shell); /** * @brief Clear all flash blocks - * @param shell Pointer to shell instance + * @param[in] shell Pointer to shell instance * @return 0 if successful */ int flash_erase_all(const struct shell *shell); diff --git a/app/other/solids_test/prj.conf b/app/other/solids_test/prj.conf index 8ac6e098b..e76dcffda 100644 --- a/app/other/solids_test/prj.conf +++ b/app/other/solids_test/prj.conf @@ -21,7 +21,7 @@ CONFIG_SPI_STM32_DMA=y CONFIG_ADC=y CONFIG_ADC_LOG_LEVEL_ERR=y # Will spam annoying error at wrn level -CONFIG_ADC_MCP356XR=y # new driver: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/adc/Kconfig.mcp356xr +CONFIG_ADC_MCP356XR=y CONFIG_ADC_MCP356XR_THREAD_STACK_SIZE=2048 CONFIG_ADC_MCP356XR_THREAD_PRIORITY=21 CONFIG_INA260=y diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index feb55fe09..c4c35897a 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -30,7 +30,7 @@ extern struct k_msgq adc_data_queue; static K_EVENT_DEFINE(adc_control_event); static K_TIMER_DEFINE(adc_timer, NULL, NULL); -void adc_reading_task(void); +void adc_reading_task(void*, void*, void*); K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, 15, 0, THREAD_START_DELAY); @@ -72,7 +72,7 @@ void adc_read_one(uint32_t *adc_val) { } } -void adc_reading_task() { +void adc_reading_task(void*, void*, void*) { uint32_t adc_val = 0; struct adc_sample sample = {0}; while (true) { @@ -107,7 +107,7 @@ void adc_reading_task() { if (num_expiries == 0) { // timer still running k_timer_status_sync(&adc_timer); } else { // timer expired - num_missed_expires += num_expiries -1; + num_missed_expires += num_expiries - 1; } // Only set off ematch if test is triggered by meep diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index 8d9ec1ae2..ad27d4a9a 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -89,7 +89,7 @@ void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32 } void buzzer_task() { - while (1) { + while (true) { if (buzzing && !control_get_test_status()) { continuous_beep(); } else { diff --git a/app/other/solids_test/src/buzzer.c b/app/other/solids_test/src/buzzer.c index 7c8e341e8..ad16a8627 100644 --- a/app/other/solids_test/src/buzzer.c +++ b/app/other/solids_test/src/buzzer.c @@ -22,11 +22,18 @@ int buzzer_init() { LOG_ERR("Failed to conf buzzer pin :("); return -1; } + ret = gpio_pin_configure_dt(&ldo_enable, GPIO_OUTPUT_INACTIVE); if (ret < 0) { LOG_ERR("Failed to conf ldo enable pin :("); return -1; } + + ret = gpio_pin_configure_dt(&ematch, GPIO_OUTPUT_INACTIVE); + if (ret < 0) { + LOG_ERR("Failed to conf ematch pin :("); + return -1; + } return 0; } @@ -46,30 +53,23 @@ void set_ematch(int level) { void beep_full() { for (int i = 0; i < 10; i++) { - printk("BEEP "); set_buzz(1); k_msleep(1000); set_buzz(0); k_msleep(1000); } - printk("\n"); } void test_start_beep() { - test_running = true; for (int i = 0; i < 3; i++) { - printk("BEEP "); set_buzz(1); k_msleep(100); set_buzz(0); k_msleep(100); } - printk("\n"); } void test_end_beep() { - test_running = false; - printk("BEEEEEP\n"); set_buzz(1); k_msleep(100); set_buzz(0); @@ -80,7 +80,6 @@ void test_end_beep() { } void continuous_beep() { - printk("BEEEEEEEEEEEEEEEEEEEEEEEP\n"); while (!test_running) { set_buzz(1); k_msleep(10); diff --git a/app/other/solids_test/src/control.c b/app/other/solids_test/src/control.c index 4ef84c938..12bcd1f99 100644 --- a/app/other/solids_test/src/control.c +++ b/app/other/solids_test/src/control.c @@ -14,7 +14,7 @@ LOG_MODULE_REGISTER(control, LOG_LEVEL_INF); static bool test_running = false; -static char curr_name[32]; +static char curr_name[CALIB_NAME_MAX_LEN]; void control_start_test(char calib_name[], bool terminal_test) { // If string is empty, get calibration name from last named test - name will stay until new one is passed in diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index bf8787373..4617d5e0c 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -39,7 +39,7 @@ static void flash_storage_thread_entry(void); K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, STORAGE_THREAD_PRIORITY, 0, 1000); -static char calibration[32]; +static char calibration[CALIB_NAME_MAX_LEN]; // Check if flash block is all 0xFF static bool flash_block_is_empty(off_t addr) { @@ -132,8 +132,8 @@ static void flash_storage_thread_entry() { current_write_addr = test_block_addr; // Save calibration name and test type (terminal or meep) - flash_write(flash_dev, current_write_addr, calibration, 32); - current_write_addr += 32; + flash_write(flash_dev, current_write_addr, calibration, CALIB_NAME_MAX_LEN); + current_write_addr += CALIB_NAME_MAX_LEN; flash_write(flash_dev, current_write_addr, test_type, sizeof(test_type)); current_write_addr += sizeof(test_type); @@ -205,7 +205,7 @@ void stop_flash_storage() { int flash_dump_one(const struct shell *shell, uint32_t test_index) { if (test_index >= MAX_TESTS ){ - shell_print(shell, "Pick a valid test [0-29]"); + shell_print(shell, "Pick a valid test [0-%u]", MAX_TESTS - 1); // %u or %d? return -1; } struct adc_sample sample; @@ -221,9 +221,9 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index) { return 0; } - char calib_name[32]; + char calib_name[CALIB_NAME_MAX_LEN]; flash_read(flash_dev, block_addr, calib_name, sizeof(calib_name)); - block_addr += 32; + block_addr += CALIB_NAME_MAX_LEN; flash_read(flash_dev, block_addr, test_type, sizeof(test_type)); block_addr += sizeof(test_type); @@ -269,10 +269,10 @@ int flash_erase_all(const struct shell *shell) { off_t curr_add = get_test_block_addr(i); int ret = flash_erase(flash_dev, curr_add, SPI_FLASH_BLOCK_SIZE); if (ret < 0) { - shell_print(shell, "flash_erase failed: %d", ret); + shell_error(shell, "flash_erase failed: %d", ret); continue; } else { - shell_error(shell, "Flash block %d erased", i); + shell_print(shell, "Flash block %d erased", i); } } diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 1573b1ff3..2cc3631b0 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -13,6 +13,8 @@ SYS_INIT(adc_init, APPLICATION, SYS_INIT_PRIORITY); SYS_INIT(buzzer_init, APPLICATION, SYS_INIT_PRIORITY); SYS_INIT(button_switch_init, APPLICATION, SYS_INIT_PRIORITY); +// TODO: move all K_THREAD_DEFINE here, expose necessary function threads in header files + int main(void) { LOG_INF("Solids Test Start"); LOG_INF("Use 'test start [calibration name]' to begin test"); diff --git a/app/other/solids_test/src/shell_cmds.c b/app/other/solids_test/src/shell_cmds.c index 6ab638c16..7bab7d850 100644 --- a/app/other/solids_test/src/shell_cmds.c +++ b/app/other/solids_test/src/shell_cmds.c @@ -1,5 +1,6 @@ #include "adc_reading.h" #include "control.h" +#include "config.h" #include "flash_storage.h" #include @@ -14,8 +15,7 @@ static int cmd_test_start(const struct shell *shell, size_t argc, char **argv) { ARG_UNUSED(argc); ARG_UNUSED(argv); - // If solids members need more than 32 characters then they need better naming standards - char calib_name[32] = "default"; // If no name arg, set to default. Name will be set to "Test [#]" in flash_storage + char calib_name[CALIB_NAME_MAX_LEN] = "default"; // If no name arg, set to default. Name will be set to "Test [#]" in flash_storage if (argc >= 2) { calib_name[0] = '\0'; // clear "default" for (int i = 1; i < argc; i++) { From ca4a05dff90238c61c752e0d93eaf0b072e34473 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sun, 28 Dec 2025 20:55:14 -0500 Subject: [PATCH 39/43] clarified more comments, fix print statements, protect against concurrent test type modification --- app/other/solids_test/include/adc_reading.h | 2 +- app/other/solids_test/include/button.h | 5 ++--- app/other/solids_test/src/flash_storage.c | 10 ++++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index 37837210a..39627f0ef 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -8,7 +8,7 @@ * @brief Struct to hold one ADC sample */ struct adc_sample { - uint32_t timestamp; /** Timestamp the sample was recorded in ms */ + uint32_t timestamp; /** Timestamp the sample was recorded in µs */ int32_t value; /** Value of the sample */ }; diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index b2177a284..cbf089946 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -12,7 +12,7 @@ int button_switch_init(); /** - * @brief Interrupt function that starts a test when TX pin is pulled high + * @brief Interrupt handler that starts a test when TX pin is pulled high * @param[in] dev GPIO device * @param[in] cb Callback structure pointer * @param[in] pins Interrupt pins @@ -20,8 +20,7 @@ int button_switch_init(); void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); /** - * @brief Key switch interrupt function that sets ematch and checks test status - * If key switch is open and test is not started, buzzer will beep continuously + * @brief Interrupt handler that sets ematch when key switch is open and manages warning buzzer state * @param[in] dev GPIO device * @param[in] cb Callback structure pointer * @param[in] pins Interrupt pins diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 4617d5e0c..b74a82000 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -205,7 +205,7 @@ void stop_flash_storage() { int flash_dump_one(const struct shell *shell, uint32_t test_index) { if (test_index >= MAX_TESTS ){ - shell_print(shell, "Pick a valid test [0-%u]", MAX_TESTS - 1); // %u or %d? + shell_print(shell, "Pick a valid test [0-%d]", MAX_TESTS - 1); return -1; } struct adc_sample sample; @@ -222,15 +222,17 @@ int flash_dump_one(const struct shell *shell, uint32_t test_index) { } char calib_name[CALIB_NAME_MAX_LEN]; + char local_test_type[sizeof(test_type)]; + flash_read(flash_dev, block_addr, calib_name, sizeof(calib_name)); block_addr += CALIB_NAME_MAX_LEN; - flash_read(flash_dev, block_addr, test_type, sizeof(test_type)); - block_addr += sizeof(test_type); + flash_read(flash_dev, block_addr, local_test_type, sizeof(local_test_type)); + block_addr += sizeof(local_test_type); shell_print(shell, "================================\nDumping Test #%d", test_index); shell_print(shell, "CALIBRATION: %s", calib_name); - shell_print(shell, "Test triggered by %s", test_type); + shell_print(shell, "Test triggered by %s", local_test_type); shell_print(shell, "timestamp, value\n================================"); for (int i = 0; i < (SPI_FLASH_BLOCK_SIZE / sizeof(sample)); i++) { From 40e4dd6b418f12aa079024434899ee4254672eb2 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sun, 28 Dec 2025 21:26:06 -0500 Subject: [PATCH 40/43] void*, void*, void* on tasks to avoid warnings, tasks exposed in headers, thread defines in main --- app/other/solids_test/include/adc_reading.h | 2 +- app/other/solids_test/include/button.h | 2 +- app/other/solids_test/include/flash_storage.h | 5 +++++ app/other/solids_test/src/adc_reading.c | 2 -- app/other/solids_test/src/button.c | 6 +++--- app/other/solids_test/src/flash_storage.c | 6 ++---- app/other/solids_test/src/main.c | 5 ++++- 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/other/solids_test/include/adc_reading.h b/app/other/solids_test/include/adc_reading.h index 39627f0ef..0c87aaea8 100644 --- a/app/other/solids_test/include/adc_reading.h +++ b/app/other/solids_test/include/adc_reading.h @@ -27,7 +27,7 @@ void adc_read_one(uint32_t *adc_val); /** * @brief Waits for ADC reading event to start, then reads ADC samples for 10 seconds */ -void adc_reading_task(); +void adc_reading_task(void*, void*, void*); /** * @brief Starts test diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index cbf089946..6634d927e 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -30,6 +30,6 @@ void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32 /** * @brief Thread to beep continuously if key switch is closed and test is not started */ -void buzzer_task(); +void buzzer_task(void*, void*, void*); #endif // BUTTON_H \ No newline at end of file diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index e1876209a..f737837c8 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -4,6 +4,11 @@ #include #include +/** + * @brief Thread to save test data to flash storage + */ +static void flash_storage_thread_entry(void*, void*, void*); + /** * @brief Begin flash storage event * @param[in] calib_name Name of calibration to store. Defaults to "Test [#]" if empty or default string passed in diff --git a/app/other/solids_test/src/adc_reading.c b/app/other/solids_test/src/adc_reading.c index c4c35897a..b60f0806c 100644 --- a/app/other/solids_test/src/adc_reading.c +++ b/app/other/solids_test/src/adc_reading.c @@ -32,8 +32,6 @@ static K_TIMER_DEFINE(adc_timer, NULL, NULL); void adc_reading_task(void*, void*, void*); -K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, 15, 0, THREAD_START_DELAY); - static uint32_t adc_buffer; static struct adc_sequence sequence = {.buffer = &adc_buffer, .buffer_size = sizeof(adc_buffer), .resolution = 24}; static bool terminal = true; diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index ad27d4a9a..bbe376184 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -17,14 +17,14 @@ LOG_MODULE_REGISTER(button, LOG_LEVEL_INF); static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(BUTTON_NODE, gpios); static const struct gpio_dt_spec key_switch = GPIO_DT_SPEC_GET(SWITCH_NODE, gpios); +void buzzer_task(void*, void*, void*); + static struct gpio_callback button_cb_data; static struct gpio_callback switch_cb_data; static bool key_switched = false; static bool buzzing = false; -K_THREAD_DEFINE(buzz_thread, 512, buzzer_task, NULL, NULL, NULL, 10, 0, 0); - int button_switch_init() { int ret = gpio_pin_configure_dt(&key_switch, GPIO_INPUT); if (ret < 0) { @@ -88,7 +88,7 @@ void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32 } } -void buzzer_task() { +void buzzer_task(void*, void*, void*) { while (true) { if (buzzing && !control_get_test_status()) { continuous_beep(); diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index b74a82000..7df5fe534 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -35,9 +35,7 @@ static uint32_t current_test_number = 0; static off_t current_write_addr = 0; static char test_type[] = "terminal"; -static void flash_storage_thread_entry(void); - -K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, STORAGE_THREAD_PRIORITY, 0, 1000); +static void flash_storage_thread_entry(void*, void*, void*); static char calibration[CALIB_NAME_MAX_LEN]; @@ -98,7 +96,7 @@ static off_t get_test_block_addr(uint32_t test_index) { return (off_t) (SPI_FLASH_START_ADDR + (test_index * SPI_FLASH_BLOCK_SIZE)); } -static void flash_storage_thread_entry() { +static void flash_storage_thread_entry(void*, void*, void*) { enum storage_event event; if (!device_is_ready(flash_dev)) { diff --git a/app/other/solids_test/src/main.c b/app/other/solids_test/src/main.c index 2cc3631b0..98eeb3da3 100644 --- a/app/other/solids_test/src/main.c +++ b/app/other/solids_test/src/main.c @@ -1,4 +1,5 @@ #include "adc_reading.h" +#include "flash_storage.h" #include "buzzer.h" #include "button.h" #include "config.h" @@ -13,7 +14,9 @@ SYS_INIT(adc_init, APPLICATION, SYS_INIT_PRIORITY); SYS_INIT(buzzer_init, APPLICATION, SYS_INIT_PRIORITY); SYS_INIT(button_switch_init, APPLICATION, SYS_INIT_PRIORITY); -// TODO: move all K_THREAD_DEFINE here, expose necessary function threads in header files +K_THREAD_DEFINE(adc_thread, 1024, adc_reading_task, NULL, NULL, NULL, 15, 0, THREAD_START_DELAY); +K_THREAD_DEFINE(buzz_thread, 512, buzzer_task, NULL, NULL, NULL, 10, 0, 0); +K_THREAD_DEFINE(storage_thread, 2048, flash_storage_thread_entry, NULL, NULL, NULL, STORAGE_THREAD_PRIORITY, 0, 1000); int main(void) { LOG_INF("Solids Test Start"); From 17b0b7ec96ceb8a525c0fe7732492b64036cf299 Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sat, 10 Jan 2026 09:14:21 -0500 Subject: [PATCH 41/43] while(true) instead of while(1), removed button and key switch internal functions from button.h --- app/other/solids_test/include/button.h | 16 ---------------- app/other/solids_test/src/flash_storage.c | 7 +++---- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/app/other/solids_test/include/button.h b/app/other/solids_test/include/button.h index 6634d927e..0ca6ee46b 100644 --- a/app/other/solids_test/include/button.h +++ b/app/other/solids_test/include/button.h @@ -11,22 +11,6 @@ */ int button_switch_init(); -/** - * @brief Interrupt handler that starts a test when TX pin is pulled high - * @param[in] dev GPIO device - * @param[in] cb Callback structure pointer - * @param[in] pins Interrupt pins - */ -void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); - -/** - * @brief Interrupt handler that sets ematch when key switch is open and manages warning buzzer state - * @param[in] dev GPIO device - * @param[in] cb Callback structure pointer - * @param[in] pins Interrupt pins - */ -void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32_t pins); - /** * @brief Thread to beep continuously if key switch is closed and test is not started */ diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index 7df5fe534..c26e8776b 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -75,8 +75,7 @@ static void load_metadata() { } static void save_metadata() { - int ret; - ret = flash_erase(flash_dev, FLASH_METADATA_ADDR, FLASH_METADATA_SIZE); + int ret = flash_erase(flash_dev, FLASH_METADATA_ADDR, FLASH_METADATA_SIZE); if (ret < 0) { LOG_ERR("flash_erase(metadata) failed: %d", ret); @@ -106,7 +105,7 @@ static void flash_storage_thread_entry(void*, void*, void*) { load_metadata(); - while (1) { + while (true) { // Wait for start command k_msgq_get(&storage_control_queue, &event, K_FOREVER); if (event != BEGIN_STORAGE) continue; @@ -140,7 +139,7 @@ static void flash_storage_thread_entry(void*, void*, void*) { size_t i = 0; size_t pages = 0; - while (1) { + while (true) { if (k_msgq_get(&storage_control_queue, &event, K_NO_WAIT) == 0 && event == END_STORAGE) { LOG_INF("Test %d complete", current_test_number); current_test_number++; From fe85e145184cc1c477ffe7a2bc7518ca53d1e25b Mon Sep 17 00:00:00 2001 From: zizz-0 Date: Sat, 24 Jan 2026 09:02:06 -0500 Subject: [PATCH 42/43] fwd declares --- app/other/solids_test/include/flash_storage.h | 2 +- app/other/solids_test/src/button.c | 2 ++ app/other/solids_test/src/flash_storage.c | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/other/solids_test/include/flash_storage.h b/app/other/solids_test/include/flash_storage.h index f737837c8..03bcd8574 100644 --- a/app/other/solids_test/include/flash_storage.h +++ b/app/other/solids_test/include/flash_storage.h @@ -7,7 +7,7 @@ /** * @brief Thread to save test data to flash storage */ -static void flash_storage_thread_entry(void*, void*, void*); +void flash_storage_thread_entry(void*, void*, void*); /** * @brief Begin flash storage event diff --git a/app/other/solids_test/src/button.c b/app/other/solids_test/src/button.c index bbe376184..180faabfd 100644 --- a/app/other/solids_test/src/button.c +++ b/app/other/solids_test/src/button.c @@ -18,6 +18,8 @@ static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(BUTTON_NODE, gpios); static const struct gpio_dt_spec key_switch = GPIO_DT_SPEC_GET(SWITCH_NODE, gpios); void buzzer_task(void*, void*, void*); +void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins); +void key_switch_state(const struct device *dev, struct gpio_callback *cb, uint32_t pins); static struct gpio_callback button_cb_data; static struct gpio_callback switch_cb_data; diff --git a/app/other/solids_test/src/flash_storage.c b/app/other/solids_test/src/flash_storage.c index c26e8776b..b399f3c25 100644 --- a/app/other/solids_test/src/flash_storage.c +++ b/app/other/solids_test/src/flash_storage.c @@ -35,7 +35,7 @@ static uint32_t current_test_number = 0; static off_t current_write_addr = 0; static char test_type[] = "terminal"; -static void flash_storage_thread_entry(void*, void*, void*); +void flash_storage_thread_entry(void*, void*, void*); static char calibration[CALIB_NAME_MAX_LEN]; @@ -95,7 +95,7 @@ static off_t get_test_block_addr(uint32_t test_index) { return (off_t) (SPI_FLASH_START_ADDR + (test_index * SPI_FLASH_BLOCK_SIZE)); } -static void flash_storage_thread_entry(void*, void*, void*) { +void flash_storage_thread_entry(void*, void*, void*) { enum storage_event event; if (!device_is_ready(flash_dev)) { From 1613455eb14368f616e3b43f83215724e8dc4b95 Mon Sep 17 00:00:00 2001 From: cowsed Date: Thu, 5 Feb 2026 18:24:14 -0500 Subject: [PATCH 43/43] key switch active low --- app/other/airbraker/quaternions | 1 + app/other/solids_test/boards/grim_reefer.overlay | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 160000 app/other/airbraker/quaternions diff --git a/app/other/airbraker/quaternions b/app/other/airbraker/quaternions new file mode 160000 index 000000000..a5ef2e58b --- /dev/null +++ b/app/other/airbraker/quaternions @@ -0,0 +1 @@ +Subproject commit a5ef2e58b05b9b314aa21ff5b2791cd86bf241a7 diff --git a/app/other/solids_test/boards/grim_reefer.overlay b/app/other/solids_test/boards/grim_reefer.overlay index a397936d5..773d4bc55 100644 --- a/app/other/solids_test/boards/grim_reefer.overlay +++ b/app/other/solids_test/boards/grim_reefer.overlay @@ -12,11 +12,11 @@ leds: leds { button: button{ // TX - gpios = <&gpioa 0 GPIO_ACTIVE_HIGH>; + gpios = <&gpioa 0 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>; label = "Button"; }; key_switch: key_switch{ // RX - gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; + gpios = <&gpioa 1 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; label = "Key Switch"; }; };