From 79fcfc428394ce1c4b17eb80db58d3cac8c3c199 Mon Sep 17 00:00:00 2001 From: saji Date: Tue, 2 Apr 2024 00:06:19 -0500 Subject: [PATCH] sdcard stuff, remove unused board --- app/CMakeLists.txt | 4 +- app/debug.conf | 1 - app/prj.conf | 33 +++++ app/src/canlog.c | 137 ++++++++++++++++++ app/src/main.c | 66 +++++---- boards/vendor/custom_plank/Kconfig | 8 - .../vendor/custom_plank/Kconfig.custom_plank | 5 - boards/vendor/custom_plank/README.md | 6 - boards/vendor/custom_plank/board.cmake | 12 -- boards/vendor/custom_plank/board.yml | 8 - .../custom_plank/custom_plank-pinctrl.dtsi | 26 ---- boards/vendor/custom_plank/custom_plank.dts | 46 ------ boards/vendor/custom_plank/custom_plank.yaml | 16 -- .../custom_plank/custom_plank_defconfig | 9 -- boards/vendor/telem3/telem3.dts | 40 ++++- boards/vendor/telem3/telem3_defconfig | 10 +- 16 files changed, 246 insertions(+), 181 deletions(-) create mode 100644 app/src/canlog.c delete mode 100644 boards/vendor/custom_plank/Kconfig delete mode 100644 boards/vendor/custom_plank/Kconfig.custom_plank delete mode 100644 boards/vendor/custom_plank/README.md delete mode 100644 boards/vendor/custom_plank/board.cmake delete mode 100644 boards/vendor/custom_plank/board.yml delete mode 100644 boards/vendor/custom_plank/custom_plank-pinctrl.dtsi delete mode 100644 boards/vendor/custom_plank/custom_plank.dts delete mode 100644 boards/vendor/custom_plank/custom_plank.yaml delete mode 100644 boards/vendor/custom_plank/custom_plank_defconfig diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index a59bf06..523e5b5 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -5,8 +5,10 @@ # SPDX-License-Identifier: Apache-2.0 cmake_minimum_required(VERSION 3.13.1) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(app LANGUAGES C) +target_include_directories(app PRIVATE inc) +target_sources(app PRIVATE src/main.c src/canlog.c) -target_sources(app PRIVATE src/main.c) diff --git a/app/debug.conf b/app/debug.conf index 885971f..cf6dd32 100644 --- a/app/debug.conf +++ b/app/debug.conf @@ -13,7 +13,6 @@ CONFIG_CONSOLE=y # UART console CONFIG_SERIAL=y CONFIG_UART_CONSOLE=y - # logging CONFIG_LOG=y CONFIG_APP_LOG_LEVEL_DBG=y diff --git a/app/prj.conf b/app/prj.conf index e7a3bfa..1d3ca4a 100644 --- a/app/prj.conf +++ b/app/prj.conf @@ -4,3 +4,36 @@ # This file contains selected Kconfig options for the application. CONFIG_SENSOR=y +CONFIG_POLL=y +CONFIG_CAN=y +CONFIG_CAN_INIT_PRIORITY=80 +CONFIG_CAN_MAX_FILTER=5 + +CONFIG_LOG=y +CONFIG_SHELL=y +CONFIG_SENSOR_SHELL=y +CONFIG_CAN_SHELL=y +CONFIG_DEVICE_SHELL=y +CONFIG_GPIO_SHELL=y + +CONFIG_STATS=y +CONFIG_STATS_NAMES=y +CONFIG_STATS_SHELL=y +CONFIG_CAN_STATS=y + +CONFIG_DISK_ACCESS=y +CONFIG_FILE_SYSTEM=y +CONFIG_FAT_FILESYSTEM_ELM=y +CONFIG_FILE_SYSTEM_MKFS=y +CONFIG_FILE_SYSTEM_SHELL=y +CONFIG_FS_LOG_LEVEL_DBG=y +CONFIG_SDMMC_LOG_LEVEL_DBG=y + +CONFIG_HEAP_MEM_POOL_SIZE=16384 +CONFIG_GNSS=y +CONFIG_UART_INTERRUPT_DRIVEN=y + +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_PRODUCT="Telem3.0" +CONFIG_USB_DEVICE_PID=0x0004 +CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=y \ No newline at end of file diff --git a/app/src/canlog.c b/app/src/canlog.c new file mode 100644 index 0000000..b053150 --- /dev/null +++ b/app/src/canlog.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2024 Saji Champlin + */ + +/* + * Listens to all can packets and logs them to a file. + * each power cycle generates a new file increment. + */ + + +#include +#include +#include +#include +#include +#include + + +#define CANLOG_STACKSIZE 1024 + + +#define DISK_DRIVE_NAME "SD" +#define DISK_MOUNT_PT "/"DISK_DRIVE_NAME":" +static const char *disk_mount_pt = DISK_MOUNT_PT; + +LOG_MODULE_REGISTER(canlog, CONFIG_APP_LOG_LEVEL); + +static FATFS fat_fs; +/* mounting info */ +static struct fs_mount_t mp = { + .type = FS_FATFS, + .fs_data = &fat_fs, +}; + +int mount_sd() { + mp.mnt_point = disk_mount_pt; + int res = fs_mount(&mp); + if (res != FR_OK) { + LOG_ERR("SD mount failed: %d", res); + return -1; + } + LOG_INF("init sd ok"); + return 0; +} +SYS_INIT(mount_sd, APPLICATION, 31); + +const struct device *const can_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_canbus)); + + +int init_can() { + + int res; + if (!device_is_ready(can_dev)) { + LOG_ERR("CAN %s not ready.\n", can_dev->name); + return -1; + } + res = can_set_mode(can_dev, CAN_MODE_LOOPBACK); + if (res != 0) { + printf("Error setting CAN mode [%d]", res); + return 0; + } + res = can_start(can_dev); + if (res) { + LOG_ERR("CAN %s failed to start: %d", can_dev->name, res); + return -1; + } + LOG_INF("can start ok"); + return 0; +} + +SYS_INIT(init_can, APPLICATION, 32); + + +/* + * This init is specifically to setup the filters before we start + * the routine. + * + */ + +int setup_can_filters() { +#ifndef CONFIG_LOOPBACK_MODE + int ret = can_set_mode(can_dev, CAN_MODE_LOOPBACK); + if (ret != 0) { + printf("Error setting CAN mode [%d]", ret); + return 0; + } +#endif + +} + +const struct can_filter all_packets_filter = { + .flags = 0U, + .id = 0, + .mask = 0, +}; +const struct can_filter high_prio_filter = { + .flags = 0U, + .id = 0U, + .mask = 0xE00U, +}; + + + +K_MSGQ_DEFINE(sd_canframes, sizeof(struct can_frame), 20, 1); +void can_task() { + struct fs_file_t logfile; + int res; + struct can_frame incoming_frame; + + fs_file_t_init(&logfile); + res = fs_open(&logfile, "/SD:/log3.txt", FS_O_CREATE |FS_O_APPEND | FS_O_WRITE); + if (res != FR_OK) { + LOG_ERR("Open file failed: %d", res); + return; + } + res = can_add_rx_filter_msgq(can_dev, &sd_canframes, &all_packets_filter); + if (res) { + LOG_ERR("Could not add filter: %d", res); + return; + } + while (1) { + // read the frame. + k_msgq_get(&sd_canframes, &incoming_frame, K_FOREVER); + fs_write(&logfile, &incoming_frame, sizeof(incoming_frame)); + fs_sync(&logfile); + LOG_INF("write!"); + } +} + + +/* +* work queue handler for SD card log. +*/ + + +K_THREAD_DEFINE(canlog_th, CANLOG_STACKSIZE, can_task, NULL, NULL, NULL, 5, 0, 0); +// create the task, mount the FS, listen to the can bus. diff --git a/app/src/main.c b/app/src/main.c index 241cc95..f1a6ea0 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -11,54 +11,60 @@ #include -static void gnss_data_cb(const struct device *dev, const struct gnss_data *data) -{ - if (data->info.fix_status != GNSS_FIX_STATUS_NO_FIX) { - printf("%s has fix!\r\n", dev->name); - } -} -GNSS_SATELLITES_CALLBACK_DEFINE(DEVICE_DT_GET(DT_ALIAS(gnss)), gnss_satellites_cb); +#include +#include +#include + +BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart), + "Console device is not ACM CDC UART device"); + LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL); -static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(gps_en, gpios); +static const struct gpio_dt_spec good_led = GPIO_DT_SPEC_GET(DT_NODELABEL(good_led), gpios); +static const struct gpio_dt_spec fault_led = GPIO_DT_SPEC_GET(DT_NODELABEL(fault_led), gpios); +static const struct gpio_dt_spec status_0_led = GPIO_DT_SPEC_GET(DT_NODELABEL(status_led0), gpios); +static const struct gpio_dt_spec status_1_led = GPIO_DT_SPEC_GET(DT_NODELABEL(status_led1), gpios); +static const struct gpio_dt_spec status_2_led = GPIO_DT_SPEC_GET(DT_NODELABEL(status_led2), gpios); +static const struct gpio_dt_spec status_3_led = GPIO_DT_SPEC_GET(DT_NODELABEL(status_led3), gpios); +static const struct gpio_dt_spec gps_en = GPIO_DT_SPEC_GET(DT_NODELABEL(gps_en), gpios); + + + int main(void) { int ret; - const struct device *sensor; - printk("Zephyr Example Application! hello\n"); + LOG_INF("Telem3 Bootup sequence..."); - if (!gpio_is_ready_dt(&led)) { + if (!gpio_is_ready_dt(&good_led)) { return 0; } - ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); + ret = gpio_pin_configure_dt(&good_led, GPIO_OUTPUT_ACTIVE); + ret |= gpio_pin_configure_dt(&fault_led, GPIO_OUTPUT_ACTIVE); + ret |= gpio_pin_configure_dt(&status_0_led, GPIO_OUTPUT_ACTIVE); + ret |= gpio_pin_configure_dt(&status_1_led, GPIO_OUTPUT_ACTIVE); + ret |= gpio_pin_configure_dt(&status_2_led, GPIO_OUTPUT_ACTIVE); + ret |= gpio_pin_configure_dt(&status_3_led, GPIO_OUTPUT_ACTIVE); + ret |= gpio_pin_configure_dt(&gps_en, GPIO_OUTPUT_ACTIVE); if (ret < 0) { return 0; } - gpio_pin_set_dt(&led, 1); + gpio_pin_set_dt(&good_led, 0); + gpio_pin_set_dt(&fault_led, 0); + gpio_pin_set_dt(&status_0_led, 0); + gpio_pin_set_dt(&status_1_led, 0); + gpio_pin_set_dt(&status_2_led, 0); + gpio_pin_set_dt(&status_3_led, 0); + gpio_pin_set_dt(&gps_en, 0); // enable gps + LOG_DBG("Init complete"); while (1) { - struct sensor_value val; - - ret = sensor_sample_fetch(sensor); - if (ret < 0) { - LOG_ERR("Could not fetch sample (%d)", ret); - return 0; - } - - ret = sensor_channel_get(sensor, SENSOR_CHAN_PROX, &val); - if (ret < 0) { - LOG_ERR("Could not get sample (%d)", ret); - return 0; - } - - printk("Sensor value: %d\n", val.val1); - - k_sleep(K_MSEC(1000)); + gpio_pin_toggle_dt(&fault_led); + k_sleep(K_MSEC(500)); } return 0; diff --git a/boards/vendor/custom_plank/Kconfig b/boards/vendor/custom_plank/Kconfig deleted file mode 100644 index 8cd0b55..0000000 --- a/boards/vendor/custom_plank/Kconfig +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor ASA -# SPDX-License-Identifier: Apache-2.0 - -config BOARD_ENABLE_DCDC - bool "Enable DCDC mode" - select SOC_DCDC_NRF52X - default y - depends on BOARD_CUSTOM_PLANK diff --git a/boards/vendor/custom_plank/Kconfig.custom_plank b/boards/vendor/custom_plank/Kconfig.custom_plank deleted file mode 100644 index 08c6e07..0000000 --- a/boards/vendor/custom_plank/Kconfig.custom_plank +++ /dev/null @@ -1,5 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor ASA -# SPDX-License-Identifier: Apache-2.0 - -config BOARD_CUSTOM_PLANK - select SOC_NRF52840_QIAA diff --git a/boards/vendor/custom_plank/README.md b/boards/vendor/custom_plank/README.md deleted file mode 100644 index f4b868b..0000000 --- a/boards/vendor/custom_plank/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Custom Plank Board - -`custom_plank` board is used to demonstrate how to create custom boards. It is -in fact a simplified version of the nRF52840-DK board, so the -`example-application` can be run on that development kit when using -`custom_plank`. diff --git a/boards/vendor/custom_plank/board.cmake b/boards/vendor/custom_plank/board.cmake deleted file mode 100644 index 5c6b531..0000000 --- a/boards/vendor/custom_plank/board.cmake +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor ASA -# SPDX-License-Identifier: Apache-2.0 - -board_runner_args(jlink "--device=nrf52" "--speed=4000") -board_runner_args(pyocd "--target=nrf52840" "--frequency=4000000") - -set(OPENOCD_NRF5_SUBFAMILY "nrf52") - -include(${ZEPHYR_BASE}/boards/common/nrfjprog.board.cmake) -include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake) -include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake) -include(${ZEPHYR_BASE}/boards/common/openocd-nrf5.board.cmake) diff --git a/boards/vendor/custom_plank/board.yml b/boards/vendor/custom_plank/board.yml deleted file mode 100644 index fde81e2..0000000 --- a/boards/vendor/custom_plank/board.yml +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) 2024 Nordic Semiconductor ASA -# SPDX-License-Identifier: Apache-2.0 - -board: - name: custom_plank - vendor: vendor - socs: - - name: nrf52840 diff --git a/boards/vendor/custom_plank/custom_plank-pinctrl.dtsi b/boards/vendor/custom_plank/custom_plank-pinctrl.dtsi deleted file mode 100644 index 4da31cb..0000000 --- a/boards/vendor/custom_plank/custom_plank-pinctrl.dtsi +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * SPDX-License-Identifier: Apache-2.0 - */ - -&pinctrl { - uart0_default: uart0_default { - group1 { - psels = , - , - , - ; - }; - }; - - uart0_sleep: uart0_sleep { - group1 { - psels = , - , - , - ; - low-power-enable; - }; - }; - -}; diff --git a/boards/vendor/custom_plank/custom_plank.dts b/boards/vendor/custom_plank/custom_plank.dts deleted file mode 100644 index 3cb284c..0000000 --- a/boards/vendor/custom_plank/custom_plank.dts +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2021 Nordic Semiconductor ASA - * SPDX-License-Identifier: Apache-2.0 - */ - -/dts-v1/; -#include -#include "custom_plank-pinctrl.dtsi" - -/ { - model = "Custom Plank Board"; - compatible = "vendor,custom-plank"; - - chosen { - zephyr,console = &uart0; - zephyr,sram = &sram0; - zephyr,flash = &flash0; - }; - - examplesensor0: examplesensor_0 { - compatible = "zephyr,examplesensor"; - input-gpios = <&gpio0 11 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; - }; -}; - -&uicr { - gpio-as-nreset; -}; - -&gpiote { - status = "okay"; -}; - -&gpio0 { - status = "okay"; -}; - -&uart0 { - compatible = "nordic,nrf-uarte"; - status = "okay"; - - current-speed = <115200>; - pinctrl-0 = <&uart0_default>; - pinctrl-1 = <&uart0_sleep>; - pinctrl-names = "default", "sleep"; -}; diff --git a/boards/vendor/custom_plank/custom_plank.yaml b/boards/vendor/custom_plank/custom_plank.yaml deleted file mode 100644 index 1a057e0..0000000 --- a/boards/vendor/custom_plank/custom_plank.yaml +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor ASA -# SPDX-License-Identifier: Apache-2.0 - -identifier: custom_plank -name: Custom-Plank -vendor: vendor -type: mcu -arch: arm -ram: 256 -flash: 1024 -toolchain: - - zephyr - - gnuarmemb - - xtools -supported: - - gpio diff --git a/boards/vendor/custom_plank/custom_plank_defconfig b/boards/vendor/custom_plank/custom_plank_defconfig deleted file mode 100644 index 5e5a7b7..0000000 --- a/boards/vendor/custom_plank/custom_plank_defconfig +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor ASA -# SPDX-License-Identifier: Apache-2.0 - -CONFIG_ARM_MPU=y -CONFIG_HW_STACK_PROTECTION=y - -CONFIG_GPIO=y - -CONFIG_PINCTRL=y diff --git a/boards/vendor/telem3/telem3.dts b/boards/vendor/telem3/telem3.dts index 8a88b2b..6c89bea 100644 --- a/boards/vendor/telem3/telem3.dts +++ b/boards/vendor/telem3/telem3.dts @@ -38,10 +38,10 @@ }; // TODO: should this really be here - // gps_en: gps_en { - // gpios = <&gpiod 9 GPIO_ACTIVE_HIGH>; - // label = "GPS power enable"; - // }; + gps_en: gps_en { + gpios = <&gpiod 9 GPIO_ACTIVE_HIGH>; + label = "GPS power enable"; + }; }; option_switches { @@ -60,11 +60,12 @@ }; }; chosen { - zephyr,console = &usart1; - zephyr,shell-uart = &usart1; + zephyr,console = &cdc_acm_uart; + zephyr,shell-uart = &cdc_acm_uart; zephyr,sram = &sram0; zephyr,flash = &flash0; zephyr,canbus = &fdcan1; + zephyr,sdmmc-disk = &sdmmc1; }; aliases { led0 = &good_led; @@ -72,6 +73,7 @@ die-temp0 = &die_temp; volt-sensor0 = &vref; volt-sensor1 = &vbat; + spi-flash0 = &spiflash0; }; soc { @@ -92,6 +94,9 @@ // status = "okay"; // }; +&clk_hsi48 { + status = "okay"; +}; &clk_hse { status = "okay"; clock-frequency = ; @@ -132,7 +137,7 @@ pinctrl-0 = <&usart2_tx_pd5 &usart2_rx_pd6>; pinctrl-names = "default"; status = "okay"; - gnss: gnss-nmea-generic { + gnss: l80_gps { compatible = "gnss-nmea-generic"; }; }; @@ -149,6 +154,9 @@ zephyr_udc0: &usb { pinctrl-0 = <&usb_dp_pa12 &usb_dm_pa11>; pinctrl-names = "default"; status = "okay"; + cdc_acm_uart: cdc_acm_uart { + compatible = "zephyr,cdc-acm-uart"; + }; }; &sdmmc1 { @@ -160,6 +168,7 @@ zephyr_udc0: &usb { &sdmmc1_d3_pc11 &sdmmc1_ck_pc12 &sdmmc1_cmd_pd2>; + // bus-width = <4>; pinctrl-names = "default"; }; @@ -203,3 +212,20 @@ zephyr_udc0: &usb { pinctrl-0 = <&fdcan1_tx_pd1 &fdcan1_rx_pd0>; pinctrl-names = "default"; }; + +&octospi1 { + pinctrl-0 = <&octospi1_clk_pf10 &octospi1_ncs_pa2 \ + &octospi1_io0_pf8 &octospi1_io1_pf9 &octospi1_io2_pf7 + &octospi1_io3_pf6 &octospi1_io4_pc1 &octospi1_io5_pc2 + &octospi1_io6_pc3 &octospi1_io7_pc0>; + pinctrl-names = "default"; + status = "okay"; + spiflash0: ospi-nor-flash@90000000 { + compatible = "st,stm32-ospi-nor"; + reg = <0x90000000 DT_SIZE_M(16)>; /* 128 Mbits */ + ospi-max-frequency = <80000000>; + spi-bus-width = ; + data-rate = ; + status = "okay"; + }; +}; \ No newline at end of file diff --git a/boards/vendor/telem3/telem3_defconfig b/boards/vendor/telem3/telem3_defconfig index 31c2add..15d295e 100644 --- a/boards/vendor/telem3/telem3_defconfig +++ b/boards/vendor/telem3/telem3_defconfig @@ -11,12 +11,6 @@ CONFIG_CLOCK_CONTROL=y CONFIG_CONSOLE=y CONFIG_UART_CONSOLE=y -CONFIG_GNSS=y -CONFIG_UART_INTERRUPT_DRIVEN=y -CONFIG_LOG=y -CONFIG_GNSS_DUMP_TO_LOG=y - - # Enable MPU CONFIG_ARM_MPU=y @@ -25,3 +19,7 @@ CONFIG_HW_STACK_PROTECTION=y # enable pin controller CONFIG_PINCTRL=y + +CONFIG_DISK_DRIVER_SDMMC=y +CONFIG_SDMMC_STM32=y +CONFIG_SDMMC_STM32_HWFC=y \ No newline at end of file