From 5547d3961839286a2a7dad03cd08173e13231f24 Mon Sep 17 00:00:00 2001 From: Andrew Pullin Date: Mon, 28 Apr 2025 09:11:28 -0700 Subject: [PATCH 1/3] Updates DK2 for nordic SDK 2.9.0 --- .../prj_xiao_ble_sense_devkitv2-adafruit.conf | 5 +++-- omi/firmware/devkit/src/mic.c | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/omi/firmware/devkit/prj_xiao_ble_sense_devkitv2-adafruit.conf b/omi/firmware/devkit/prj_xiao_ble_sense_devkitv2-adafruit.conf index ec7b9c92ba..2cf796a0e0 100644 --- a/omi/firmware/devkit/prj_xiao_ble_sense_devkitv2-adafruit.conf +++ b/omi/firmware/devkit/prj_xiao_ble_sense_devkitv2-adafruit.conf @@ -3,7 +3,7 @@ # CONFIG_GPIO=y -CONFIG_NRFX_PDM=y +CONFIG_NRFX_PDM0=y CONFIG_ADC=y # To flash via dev board @@ -45,7 +45,7 @@ CONFIG_BT_DIS_FW_REV_STR="2.0.10" CONFIG_BT_DIS_HW_REV_STR="Seeed Xiao BLE Sense" # -# Large BLE packets / BLE Buffers +# Large BLE packets / BLE Buffers # # CONFIG_BT_GATT_CLIENT=y # CONFIG_BT_THROUGHPUT=y @@ -191,6 +191,7 @@ CONFIG_RING_BUFFER=y CONFIG_PM_DEVICE=y # CONFIG_NRFX_USBD=y +CONFIG_USB_DEVICE_STACK=y # CONFIG_FLASH=y diff --git a/omi/firmware/devkit/src/mic.c b/omi/firmware/devkit/src/mic.c index 73ef80ccfa..267c0f6df7 100644 --- a/omi/firmware/devkit/src/mic.c +++ b/omi/firmware/devkit/src/mic.c @@ -21,6 +21,8 @@ static int16_t _buffer_1[MIC_BUFFER_SAMPLES]; static volatile uint8_t _next_buffer_index = 0; static volatile mix_handler _callback = NULL; +static const nrfx_pdm_t pdm0_instance = NRFX_PDM_INSTANCE(0); + static void pdm_irq_handler(nrfx_pdm_evt_t const *event) { // Ignore error (how to handle?) @@ -36,12 +38,12 @@ static void pdm_irq_handler(nrfx_pdm_evt_t const *event) LOG_DBG("Audio buffer requested"); if (_next_buffer_index == 0) { - nrfx_pdm_buffer_set(_buffer_0, MIC_BUFFER_SAMPLES); + nrfx_pdm_buffer_set(&pdm0_instance, _buffer_0, MIC_BUFFER_SAMPLES); _next_buffer_index = 1; } else { - nrfx_pdm_buffer_set(_buffer_1, MIC_BUFFER_SAMPLES); + nrfx_pdm_buffer_set(&pdm0_instance, _buffer_1, MIC_BUFFER_SAMPLES); _next_buffer_index = 0; } } @@ -75,8 +77,9 @@ int mic_start() pdm_config.mode = NRF_PDM_MODE_MONO; pdm_config.edge = NRF_PDM_EDGE_LEFTFALLING; pdm_config.ratio = NRF_PDM_RATIO_80X; - IRQ_DIRECT_CONNECT(PDM_IRQn, 5, nrfx_pdm_irq_handler, 0); // IMPORTANT! - if (nrfx_pdm_init(&pdm_config, pdm_irq_handler) != NRFX_SUCCESS) + + IRQ_DIRECT_CONNECT(PDM_IRQn, 5, nrfx_pdm_0_irq_handler, 0); // IMPORTANT! + if (nrfx_pdm_init(&pdm0_instance, &pdm_config, pdm_irq_handler) != NRFX_SUCCESS) { LOG_ERR("Audio unable to initialize PDM"); return -1; @@ -87,7 +90,7 @@ int mic_start() nrfy_gpio_pin_set(PDM_PWR_PIN); // Start PDM - if (nrfx_pdm_start() != NRFX_SUCCESS) + if (nrfx_pdm_start(&pdm0_instance) != NRFX_SUCCESS) { LOG_ERR("Audio unable to start PDM"); return -1; @@ -97,7 +100,7 @@ int mic_start() return 0; } -void set_mic_callback(mix_handler callback) +void set_mic_callback(mix_handler callback) { _callback = callback; } From ad7e4dbfa805b7da600fe2631568ac26e9618c04 Mon Sep 17 00:00:00 2001 From: Andrew Pullin Date: Mon, 28 Apr 2025 09:21:50 -0700 Subject: [PATCH 2/3] Resolves several build warnings throughout the project --- omi/firmware/devkit/src/button.c | 127 ++++++++++++----------- omi/firmware/devkit/src/codec.c | 18 ++-- omi/firmware/devkit/src/config.h | 1 + omi/firmware/devkit/src/main.c | 3 + omi/firmware/devkit/src/sdcard.c | 152 ++++++++++++++-------------- omi/firmware/devkit/src/speaker.c | 104 +++++++++---------- omi/firmware/devkit/src/storage.c | 110 ++++++++++---------- omi/firmware/devkit/src/transport.c | 30 +++++- omi/firmware/devkit/src/transport.h | 2 +- omi/firmware/devkit/src/usb.c | 6 +- 10 files changed, 290 insertions(+), 263 deletions(-) diff --git a/omi/firmware/devkit/src/button.c b/omi/firmware/devkit/src/button.c index 7609d22760..e0bb0484e5 100644 --- a/omi/firmware/devkit/src/button.c +++ b/omi/firmware/devkit/src/button.c @@ -31,7 +31,7 @@ static struct bt_gatt_attr button_service_attr[] = { static struct bt_gatt_service button_service = BT_GATT_SERVICE(button_service_attr); -static void button_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) +static void button_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) { if (value == BT_GATT_CCC_NOTIFY) { @@ -47,8 +47,8 @@ static void button_ccc_config_changed_handler(const struct bt_gatt_attr *attr, u } } -struct gpio_dt_spec d4_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=4, .dt_flags = GPIO_OUTPUT_ACTIVE}; //3.3 -struct gpio_dt_spec d5_pin_input = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=5, .dt_flags = GPIO_INT_EDGE_RISING}; +struct gpio_dt_spec d4_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=4, .dt_flags = 0}; //3.3 +struct gpio_dt_spec d5_pin_input = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=5, .dt_flags = 0}; static bool was_pressed = false; @@ -60,11 +60,11 @@ void button_pressed_callback(const struct device *dev, struct gpio_callback *cb, { int temp = gpio_pin_get_raw(dev,d5_pin_input.pin); LOG_PRINTK("button_pressed_callback %d\n", temp); - if (temp) + if (temp) { was_pressed = false; } - else + else { was_pressed = true; } @@ -88,64 +88,64 @@ static uint32_t inc_count_1 = 0; static uint32_t inc_count_0 = 0; static int final_button_state[2] = {0,0}; -const static int threshold = 10; +__attribute__((unused)) const static int threshold = 10; -static void reset_count() +__attribute__((unused)) static void reset_count() { inc_count_0 = 0; inc_count_1 = 0; } -static inline void notify_press() +static inline void notify_press() { final_button_state[0] = BUTTON_PRESS; LOG_INF("Button pressed"); struct bt_conn *conn = get_current_connection(); if (conn != NULL) - { + { bt_gatt_notify(conn, &button_service.attrs[1], &final_button_state, sizeof(final_button_state)); } } -static inline void notify_unpress() +static inline void notify_unpress() { - final_button_state[0] = BUTTON_RELEASE; + final_button_state[0] = BUTTON_RELEASE; LOG_INF("Button released"); struct bt_conn *conn = get_current_connection(); if (conn != NULL) - { + { bt_gatt_notify(conn, &button_service.attrs[1], &final_button_state, sizeof(final_button_state)); } } -static inline void notify_tap() +static inline void notify_tap() { final_button_state[0] = SINGLE_TAP; LOG_INF("Button single tap"); struct bt_conn *conn = get_current_connection(); if (conn != NULL) - { + { bt_gatt_notify(conn, &button_service.attrs[1], &final_button_state, sizeof(final_button_state)); } } -static inline void notify_double_tap() +static inline void notify_double_tap() { final_button_state[0] = DOUBLE_TAP; //button press LOG_INF("Button double tap"); struct bt_conn *conn = get_current_connection(); if (conn != NULL) - { + { bt_gatt_notify(conn, &button_service.attrs[1], &final_button_state, sizeof(final_button_state)); } } -static inline void notify_long_tap() +static inline void notify_long_tap() { final_button_state[0] = LONG_TAP; //button press LOG_INF("Button long tap"); struct bt_conn *conn = get_current_connection(); if (conn != NULL) - { + { bt_gatt_notify(conn, &button_service.attrs[1], &final_button_state, sizeof(final_button_state)); } } @@ -173,7 +173,7 @@ static bool btn_is_pressed; static u_int8_t btn_last_event = BUTTON_EVENT_NONE; -void check_button_level(struct k_work *work_item) +void check_button_level(struct k_work *work_item) { current_time = current_time + 1; @@ -199,7 +199,7 @@ void check_button_level(struct k_work *work_item) btn_last_tap_time = current_time; } } - } + } // Check for single tap if (btn_state == BUTTON_RELEASED && !btn_is_pressed) { @@ -265,23 +265,22 @@ void check_button_level(struct k_work *work_item) } k_work_reschedule(&button_work, K_MSEC(BUTTON_CHECK_INTERVAL)); - return 0; } // @deprecated //#define LONG_PRESS_INTERVAL 25 //#define SINGLE_PRESS_INTERVAL 2 -//void check_button_level_2(struct k_work *work_item) +//void check_button_level_2(struct k_work *work_item) //{ // //insert the current button state here // int state_ = was_pressed ? 1 : 0; -// if (current_button_state == IDLE) +// if (current_button_state == IDLE) // { -// if (state_ == 0) +// if (state_ == 0) // { // //Do nothing! // } -// else if (state_ == 1) +// else if (state_ == 1) // { // //Also do nothing, but transition to the next state // notify_press(); @@ -295,29 +294,29 @@ void check_button_level(struct k_work *work_item) // } // } // -// else if (current_button_state == ONE_PRESS) +// else if (current_button_state == ONE_PRESS) // { -// if (state_ == 0) +// if (state_ == 0) // { -// -// if(inc_count_0 == 0) +// +// if(inc_count_0 == 0) // { // notify_unpress(); // } // inc_count_0++; //button is unpressed -// if (inc_count_0 > SINGLE_PRESS_INTERVAL) +// if (inc_count_0 > SINGLE_PRESS_INTERVAL) // { -// //If button is not pressed for a little while....... +// //If button is not pressed for a little while....... // //transition to Two_press. button could be a single or double tap // current_button_state = TWO_PRESS; -// reset_count(); +// reset_count(); // } // } -// if (state_ == 1) +// if (state_ == 1) // { // inc_count_1++; //button is pressed // -// if (inc_count_1 > LONG_PRESS_INTERVAL) +// if (inc_count_1 > LONG_PRESS_INTERVAL) // { // //If button is pressed for a long time....... // notify_long_tap(); @@ -346,15 +345,15 @@ void check_button_level(struct k_work *work_item) // // } // -// else if (current_button_state == TWO_PRESS) +// else if (current_button_state == TWO_PRESS) // { -// if (state_ == 0) +// if (state_ == 0) // { -// if (inc_count_1 > 0) +// if (inc_count_1 > 0) // { // if button has been pressed...... // notify_unpress(); // notify_double_tap(); -// +// // //Fire the notify and enter a grace period // current_button_state = GRACE; // reset_count(); @@ -380,19 +379,19 @@ void check_button_level(struct k_work *work_item) // current_button_state = GRACE; // reset_count(); // } -// else +// else // { // inc_count_0++; //not pressed // } // } -// else if (state_ == 1 ) +// else if (state_ == 1 ) // { -// if (inc_count_1 == 0) +// if (inc_count_1 == 0) // { // notify_press(); // inc_count_1++; // } -// if (inc_count_1 > threshold) +// if (inc_count_1 > threshold) // { // notify_long_tap(); // //play_haptic_milli(10); @@ -417,22 +416,22 @@ void check_button_level(struct k_work *work_item) // } // } // -// else if (current_button_state == GRACE) +// else if (current_button_state == GRACE) // { -// if (state_ == 0) +// if (state_ == 0) // { -// if (inc_count_0 == 0 && (inc_count_1 > 0)) +// if (inc_count_0 == 0 && (inc_count_1 > 0)) // { // notify_unpress(); // } // inc_count_0++; -// if (inc_count_0 > 1) +// if (inc_count_0 > 1) // { // current_button_state = IDLE; // reset_count(); // } // } -// else if (state_ == 1) +// else if (state_ == 1) // { // inc_count_1++; // } @@ -441,38 +440,38 @@ void check_button_level(struct k_work *work_item) //} -static ssize_t button_data_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) +static ssize_t button_data_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { LOG_INF("button_data_read_characteristic"); LOG_PRINTK("was_pressed: %d\n", final_button_state[0]); return bt_gatt_attr_read(conn, attr, buf, len, offset, &final_button_state, sizeof(final_button_state)); } -int button_init() +int button_init() { - if (gpio_is_ready_dt(&d4_pin)) + if (gpio_is_ready_dt(&d4_pin)) { LOG_INF("D4 Pin ready"); } - else + else { LOG_ERR("Error setting up D4 Pin"); return -1; } - if (gpio_pin_configure_dt(&d4_pin, GPIO_OUTPUT_ACTIVE) < 0) + if (gpio_pin_configure_dt(&d4_pin, GPIO_OUTPUT_ACTIVE) < 0) { LOG_ERR("Error setting up D4 Pin Voltage"); return -1; } - else + else { LOG_INF("D4 ready to transmit voltage"); } - if (gpio_is_ready_dt(&d5_pin_input)) + if (gpio_is_ready_dt(&d5_pin_input)) { LOG_INF("D5 Pin ready"); } - else + else { LOG_ERR("D5 Pin not ready"); return -1; @@ -480,28 +479,28 @@ int button_init() int err2 = gpio_pin_configure_dt(&d5_pin_input,GPIO_INPUT); - if (err2 != 0) + if (err2 != 0) { LOG_ERR("Error setting up D5 Pin"); return -1; } - else + else { LOG_INF("D5 ready"); } // GPIO_INT_LEVEL_INACTIVE err2 = gpio_pin_interrupt_configure_dt(&d5_pin_input,GPIO_INT_EDGE_BOTH); - if (err2 != 0) + if (err2 != 0) { LOG_ERR("D5 unable to detect button presses"); return -1; } - else + else { LOG_INF("D5 ready to detect button presses"); } - + gpio_init_callback(&button_cb_data, button_pressed_callback, BIT(d5_pin_input.pin)); gpio_add_callback(d5_pin_input.port, &button_cb_data); @@ -509,17 +508,17 @@ int button_init() return 0; } -void activate_button_work() +void activate_button_work() { k_work_schedule(&button_work, K_MSEC(BUTTON_CHECK_INTERVAL)); } -void register_button_service() +void register_button_service() { bt_gatt_service_register(&button_service); } -FSM_STATE_T get_current_button_state() +FSM_STATE_T get_current_button_state() { return current_button_state; } @@ -539,7 +538,7 @@ void turnoff_all() gpio_remove_callback(d5_pin_input.port, &button_cb_data); gpio_pin_interrupt_configure_dt(&d5_pin_input,GPIO_INT_LEVEL_INACTIVE); //maybe save something here to indicate success. next time the button is pressed we should know about it - NRF_USBD->INTENCLR= 0xFFFFFFFF; + NRF_USBD->INTENCLR= 0xFFFFFFFF; NRF_POWER->SYSTEMOFF=1; } diff --git a/omi/firmware/devkit/src/codec.c b/omi/firmware/devkit/src/codec.c index ae8ed363eb..78f5737770 100644 --- a/omi/firmware/devkit/src/codec.c +++ b/omi/firmware/devkit/src/codec.c @@ -26,16 +26,16 @@ void set_codec_callback(codec_callback callback) uint8_t codec_ring_buffer_data[AUDIO_BUFFER_SAMPLES * 2]; // 2 bytes per sample struct ring_buf codec_ring_buf; -int codec_receive_pcm(int16_t *data, size_t len) //this gets called after mic data is finished -{ - +int codec_receive_pcm(int16_t *data, size_t len) //this gets called after mic data is finished +{ + int written = ring_buf_put(&codec_ring_buf, (uint8_t *)data, len * 2); if (written != len * 2) { LOG_ERR("Failed to write %d bytes to codec ring buffer", len * 2); return -1; } - + return 0; } @@ -51,17 +51,19 @@ static struct k_thread codec_thread; uint16_t execute_codec(); #if CODEC_OPUS -#if (CONFIG_OPUS_MODE == CONFIG_OPUS_MODE_CELT) +#if CONFIG_OPUS_MODE == CONFIG_OPUS_MODE_CELT #define OPUS_ENCODER_SIZE 7180 -#endif -#if (CONFIG_OPUS_MODE == CONFIG_OPUS_MODE_HYBRID) +#elif CONFIG_OPUS_MODE == CONFIG_OPUS_MODE_HYBRID #define OPUS_ENCODER_SIZE 10916 +#else +#error "Unsupported CONFIG_OPUS_MODE value" #endif __ALIGN(4) static uint8_t m_opus_encoder[OPUS_ENCODER_SIZE]; static OpusEncoder *const m_opus_state = (OpusEncoder *)m_opus_encoder; #endif + void codec_entry() { @@ -98,7 +100,7 @@ int codec_start() // OPUS #if CODEC_OPUS - ASSERT_TRUE(opus_encoder_get_size(1) == sizeof(m_opus_encoder)); + ASSERT_TRUE((opus_encoder_get_size(1) == sizeof(m_opus_encoder))); ASSERT_TRUE(opus_encoder_init(m_opus_state, 16000, 1, CODEC_OPUS_APPLICATION) == OPUS_OK); ASSERT_TRUE(opus_encoder_ctl(m_opus_state, OPUS_SET_BITRATE(CODEC_OPUS_BITRATE)) == OPUS_OK); ASSERT_TRUE(opus_encoder_ctl(m_opus_state, OPUS_SET_VBR(CODEC_OPUS_VBR)) == OPUS_OK); diff --git a/omi/firmware/devkit/src/config.h b/omi/firmware/devkit/src/config.h index dddfc7683b..58ceaf6957 100644 --- a/omi/firmware/devkit/src/config.h +++ b/omi/firmware/devkit/src/config.h @@ -29,6 +29,7 @@ #define CODEC_OPUS_VBR 1 // Or 1 #define CODEC_OPUS_COMPLEXITY 3 #endif +// TODO: If this is a CONFIG_ , it should be driven by a Kconfig setting. #define CONFIG_OPUS_MODE CONFIG_OPUS_MODE_CELT // Codec IDs diff --git a/omi/firmware/devkit/src/main.c b/omi/firmware/devkit/src/main.c index 2acf2eb6b4..72a7ac8376 100644 --- a/omi/firmware/devkit/src/main.c +++ b/omi/firmware/devkit/src/main.c @@ -11,6 +11,9 @@ #include "storage.h" #include "speaker.h" #include "usb.h" +#if defined(CONFIG_OMI_ENABLE_BATTERY) +#include "lib/battery/battery.h" +#endif #define BOOT_BLINK_DURATION_MS 600 #define BOOT_PAUSE_DURATION_MS 200 #define VBUS_DETECT (1U << 20) diff --git a/omi/firmware/devkit/src/sdcard.c b/omi/firmware/devkit/src/sdcard.c index e76a272b66..3880a01c98 100644 --- a/omi/firmware/devkit/src/sdcard.c +++ b/omi/firmware/devkit/src/sdcard.c @@ -18,7 +18,7 @@ static struct fs_mount_t mount_point = { .fs_data = &fat_fs, }; -struct gpio_dt_spec sd_en_gpio_pin = { .port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=19, .dt_flags = GPIO_INT_DISABLE }; +struct gpio_dt_spec sd_en_gpio_pin = { .port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=19, .dt_flags = 0 }; uint8_t file_count = 0; @@ -27,26 +27,28 @@ static char current_full_path[MAX_PATH_LENGTH]; static char read_buffer[MAX_PATH_LENGTH]; static char write_buffer[MAX_PATH_LENGTH]; -uint32_t file_num_array[2]; +uint32_t file_num_array[2]; static const char *disk_mount_pt = "/SD:/"; bool sd_enabled = false; +int get_file_contents(struct fs_dir_t *zdp, struct fs_dirent *entry); + int mount_sd_card(void) { //initialize the sd card enable pin (v2) - if (gpio_is_ready_dt(&sd_en_gpio_pin)) + if (gpio_is_ready_dt(&sd_en_gpio_pin)) { LOG_INF("SD Enable Pin ready"); } - else + else { LOG_ERR("Error setting up SD Enable Pin"); return -1; } - if (gpio_pin_configure_dt(&sd_en_gpio_pin, GPIO_OUTPUT_ACTIVE) < 0) + if (gpio_pin_configure_dt(&sd_en_gpio_pin, GPIO_OUTPUT_ACTIVE) < 0) { LOG_ERR("Error setting up SD Pin"); return -1; @@ -54,14 +56,14 @@ int mount_sd_card(void) sd_enabled = true; //initialize the sd card - const char *disk_pdrv = "SD"; - int err = disk_access_init(disk_pdrv); + const char *disk_pdrv = "SD"; + int err = disk_access_init(disk_pdrv); LOG_INF("disk_access_init: %d\n", err); - if (err) + if (err) { //reattempt k_msleep(1000); - err = disk_access_init(disk_pdrv); - if (err) + err = disk_access_init(disk_pdrv); + if (err) { LOG_ERR("disk_access_init failed"); return -1; @@ -70,28 +72,28 @@ int mount_sd_card(void) mount_point.mnt_point = "/SD:"; int res = fs_mount(&mount_point); - if (res == FR_OK) + if (res == FR_OK) { LOG_INF("SD card mounted successfully"); - } - else + } + else { LOG_ERR("f_mount failed: %d", res); return -1; } - + res = fs_mkdir("/SD:/audio"); - if (res == FR_OK) + if (res == FR_OK) { LOG_INF("audio directory created successfully"); initialize_audio_file(1); } - else if (res == FR_EXIST) + else if (res == FR_EXIST) { LOG_INF("audio directory already exists"); } - else + else { LOG_INF("audio directory creation failed: %d", res); } @@ -99,9 +101,9 @@ int mount_sd_card(void) struct fs_dir_t audio_dir_entry; fs_dir_t_init(&audio_dir_entry); err = fs_opendir(&audio_dir_entry,"/SD:/audio"); - if (err) + if (err) { - LOG_ERR("error while opening directory ",err); + LOG_ERR("error while opening directory: %d",err); return -1; } LOG_INF("result of opendir: %d",err); @@ -109,7 +111,7 @@ int mount_sd_card(void) struct fs_dirent file_count_entry; file_count = get_file_contents(&audio_dir_entry, &file_count_entry); file_count = 1; - if (file_count < 0) + if (file_count < 0) { LOG_ERR(" error getting file count"); return -1; @@ -119,8 +121,8 @@ int mount_sd_card(void) // file_count++; LOG_INF("new num files: %d",file_count); - res = move_write_pointer(file_count); - if (res) + res = move_write_pointer(file_count); + if (res) { LOG_ERR("erro while moving the write pointer"); return -1; @@ -128,23 +130,23 @@ int mount_sd_card(void) move_read_pointer(file_count); - if (res) + if (res) { LOG_ERR("error while moving the reader pointer\n"); return -1; } LOG_INF("file count: %d",file_count); - + struct fs_dirent info_file_entry; //check if the info file exists. if not, generate new info file const char *info_path = "/SD:/info.txt"; res = fs_stat(info_path,&info_file_entry); //for later - if (res) + if (res) { res = create_file("info.txt"); save_offset(0); LOG_INF("result of info.txt creation: %d ",res); } - + LOG_INF("result of check: %d",res); return 0; @@ -156,43 +158,43 @@ uint32_t get_file_size(uint8_t num) snprintf(current_full_path, sizeof(current_full_path), "%s%s", disk_mount_pt, ptr); k_free(ptr); struct fs_dirent entry; - int res = fs_stat(¤t_full_path,&entry); + int res = fs_stat(current_full_path,&entry); if (res) { LOG_ERR("invalid file in get file size\n"); - return 0; + return 0; } return (uint32_t)entry.size; } -int move_read_pointer(uint8_t num) +int move_read_pointer(uint8_t num) { char *read_ptr = generate_new_audio_header(num); snprintf(read_buffer, sizeof(read_buffer), "%s%s", disk_mount_pt, read_ptr); k_free(read_ptr); - struct fs_dirent entry; - int res = fs_stat(&read_buffer,&entry); - if (res) + struct fs_dirent entry; + int res = fs_stat(read_buffer,&entry); + if (res) { LOG_ERR("invalid file in move read ptr\n"); - return -1; + return -1; } return 0; } -int move_write_pointer(uint8_t num) +int move_write_pointer(uint8_t num) { char *write_ptr = generate_new_audio_header(num); snprintf(write_buffer, sizeof(write_buffer), "%s%s", disk_mount_pt, write_ptr); k_free(write_ptr); struct fs_dirent entry; - int res = fs_stat(&write_buffer,&entry); - if (res) + int res = fs_stat(write_buffer,&entry); + if (res) { - LOG_ERR("invalid file in move write pointer\n"); - return -1; + LOG_ERR("invalid file in move write pointer\n"); + return -1; } - return 0; + return 0; } int create_file(const char *file_path) @@ -202,21 +204,21 @@ int create_file(const char *file_path) struct fs_file_t data_file; fs_file_t_init(&data_file); ret = fs_open(&data_file, current_full_path, FS_O_WRITE | FS_O_CREATE); - if (ret) + if (ret) { LOG_ERR("File creation failed %d", ret); return -2; - } + } fs_close(&data_file); return 0; } -int read_audio_data(uint8_t *buf, int amount,int offset) +int read_audio_data(uint8_t *buf, int amount,int offset) { struct fs_file_t read_file; - fs_file_t_init(&read_file); + fs_file_t_init(&read_file); uint8_t *temp_ptr = buf; - struct fs_dirent entry; + __attribute__((unused)) struct fs_dirent entry; int rc = fs_open(&read_file, read_buffer, FS_O_READ | FS_O_RDWR); rc = fs_seek(&read_file,offset,FS_SEEK_SET); @@ -241,11 +243,11 @@ int write_to_file(uint8_t *data,uint32_t length) fs_close(&write_file); return 0; } - -int initialize_audio_file(uint8_t num) + +int initialize_audio_file(uint8_t num) { char *header = generate_new_audio_header(num); - if (header == NULL) + if (header == NULL) { return -1; } @@ -254,7 +256,7 @@ int initialize_audio_file(uint8_t num) return 0; } -char* generate_new_audio_header(uint8_t num) +char* generate_new_audio_header(uint8_t num) { if (num > 99 ) return NULL; char *ptr_ = k_malloc(14); @@ -276,22 +278,22 @@ char* generate_new_audio_header(uint8_t num) return ptr_; } -int get_file_contents(struct fs_dir_t *zdp, struct fs_dirent *entry) +int get_file_contents(struct fs_dir_t *zdp, struct fs_dirent *entry) { - if (zdp->mp->fs->readdir(zdp, entry) ) + if (zdp->mp->fs->readdir(zdp, entry) ) { return -1; } - if (entry->name[0] == 0) + if (entry->name[0] == 0) { return 0; } - int count = 0; + int count = 0; file_num_array[count] = entry->size; LOG_INF("file numarray %d %d ",count,file_num_array[count]); LOG_INF("file name is %s ", entry->name); count++; - while (zdp->mp->fs->readdir(zdp, entry) == 0 ) + while (zdp->mp->fs->readdir(zdp, entry) == 0 ) { if (entry->name[0] == 0 ) { @@ -304,14 +306,14 @@ int get_file_contents(struct fs_dir_t *zdp, struct fs_dirent *entry) } return count; } -//we should clear instead of delete since we lose fifo structure -int clear_audio_file(uint8_t num) +//we should clear instead of delete since we lose fifo structure +int clear_audio_file(uint8_t num) { char *clear_header = generate_new_audio_header(num); snprintf(current_full_path, sizeof(current_full_path), "%s%s", disk_mount_pt, clear_header); k_free(clear_header); int res = fs_unlink(current_full_path); - if (res) + if (res) { LOG_ERR("error deleting file"); return -1; @@ -321,7 +323,7 @@ int clear_audio_file(uint8_t num) k_msleep(10); res = create_file(create_file_header); k_free(create_file_header); - if (res) + if (res) { LOG_ERR("error creating file"); return -1; @@ -330,13 +332,13 @@ int clear_audio_file(uint8_t num) return 0; } -int delete_audio_file(uint8_t num) +int delete_audio_file(uint8_t num) { char *ptr = generate_new_audio_header(num); snprintf(current_full_path, sizeof(current_full_path), "%s%s", disk_mount_pt, ptr); k_free(ptr); int res = fs_unlink(current_full_path); - if (res) + if (res) { LOG_PRINTK("error deleting file in delete\n"); return -1; @@ -345,9 +347,9 @@ int delete_audio_file(uint8_t num) return 0; } //the nuclear option. -int clear_audio_directory() +int clear_audio_directory() { - if (file_count == 1) + if (file_count == 1) { return 0; } @@ -355,37 +357,37 @@ int clear_audio_directory() // char* path_ = "/SD:/audio"; // clear_audio_file(file_count); int res=0; - for (uint8_t i = file_count ; i > 0; i-- ) + for (uint8_t i = file_count ; i > 0; i-- ) { res = delete_audio_file(i); k_msleep(10); - if (res) + if (res) { LOG_PRINTK("error on %d\n",i); return -1; - } + } } res = fs_unlink("/SD:/audio"); - if (res) + if (res) { LOG_ERR("error deleting file"); return -1; } res = fs_mkdir("/SD:/audio"); - if (res) + if (res) { LOG_ERR("failed to make directory"); return -1; } res = create_file("audio/a01.txt"); - if (res) + if (res) { LOG_ERR("failed to make new file in directory files"); return -1; } LOG_ERR("done with clearing"); - file_count = 1; + file_count = 1; move_write_pointer(1); return 0; //if files are cleared, then directory is oked for destrcution. @@ -396,14 +398,14 @@ int save_offset(uint32_t offset) uint8_t buf[4] = { offset & 0xFF, (offset >> 8) & 0xFF, - (offset >> 16) & 0xFF, - (offset >> 24) & 0xFF + (offset >> 16) & 0xFF, + (offset >> 24) & 0xFF }; struct fs_file_t write_file; fs_file_t_init(&write_file); int res = fs_open(&write_file, "/SD:/info.txt" , FS_O_WRITE | FS_O_CREATE); - if (res) + if (res) { LOG_ERR("error opening file %d",res); return -1; @@ -451,15 +453,15 @@ int get_offset() void sd_off() { -// gpio_pin_set_dt(&sd_en_gpio_pin, 0); - sd_enabled = false; +// gpio_pin_set_dt(&sd_en_gpio_pin, 0); + sd_enabled = false; } void sd_on() { -// gpio_pin_set_dt(&sd_en_gpio_pin, 1); - sd_enabled = true; +// gpio_pin_set_dt(&sd_en_gpio_pin, 1); + sd_enabled = true; } bool is_sd_on() diff --git a/omi/firmware/devkit/src/speaker.c b/omi/firmware/devkit/src/speaker.c index fe82799378..22cac3ac4d 100644 --- a/omi/firmware/devkit/src/speaker.c +++ b/omi/firmware/devkit/src/speaker.c @@ -15,19 +15,19 @@ LOG_MODULE_REGISTER(speaker, CONFIG_LOG_DEFAULT_LEVEL); #define MAX_BLOCK_SIZE 10000 //24000 * 2 -#define BLOCK_COUNT 2 +#define BLOCK_COUNT 2 #define SAMPLE_FREQUENCY 8000 #define NUMBER_OF_CHANNELS 2 #define PACKET_SIZE 400 #define WORD_SIZE 16 #define NUM_CHANNELS 2 -#define PI 3.14159265358979323846 +#define PI 3.14159265358979323846f #define MAX_HAPTIC_DURATION 5000 K_MEM_SLAB_DEFINE_STATIC(mem_slab, MAX_BLOCK_SIZE, BLOCK_COUNT, 2); -struct device *audio_speaker; +const struct device *audio_speaker; static void* rx_buffer; static void* buzz_buffer; @@ -37,10 +37,10 @@ static int16_t *clear_ptr; static uint16_t current_length; static uint16_t offset; -struct gpio_dt_spec haptic_gpio_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio1)), .pin=11, .dt_flags = GPIO_INT_DISABLE}; +struct gpio_dt_spec haptic_gpio_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio1)), .pin=11, .dt_flags = 0}; -struct gpio_dt_spec speaker_gpio_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=4, .dt_flags = GPIO_INT_DISABLE}; +struct gpio_dt_spec speaker_gpio_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin=4, .dt_flags = 0}; // ble service // @@ -58,12 +58,12 @@ static struct bt_gatt_attr speaker_service_attr[] = { }; static struct bt_gatt_service speaker_service = BT_GATT_SERVICE(speaker_service_attr); -void register_speaker_service() +void register_speaker_service() { bt_gatt_service_register(&speaker_service); } -static void speaker_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) +static void speaker_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) { if (value == BT_GATT_CCC_NOTIFY) { @@ -80,7 +80,7 @@ static void speaker_ccc_config_changed_handler(const struct bt_gatt_attr *attr, } -static ssize_t speaker_haptic_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags) +static ssize_t speaker_haptic_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags) { LOG_INF("play the haptic"); @@ -108,47 +108,47 @@ static ssize_t speaker_haptic_handler(struct bt_conn *conn, const struct bt_gatt return 1; } -int speaker_init() +int speaker_init() { LOG_INF("Speaker init"); audio_speaker = device_get_binding("I2S_0"); - - if (!device_is_ready(audio_speaker)) + + if (!device_is_ready(audio_speaker)) { LOG_ERR("Speaker device is not supported : %s", audio_speaker->name); return -1; } - if (gpio_is_ready_dt(&speaker_gpio_pin)) + if (gpio_is_ready_dt(&speaker_gpio_pin)) { LOG_PRINTK("Speaker Pin ready\n"); } - else + else { LOG_PRINTK("Error setting up speaker Pin\n"); return -1; } - if (gpio_pin_configure_dt(&speaker_gpio_pin, GPIO_OUTPUT_INACTIVE) < 0) + if (gpio_pin_configure_dt(&speaker_gpio_pin, GPIO_OUTPUT_INACTIVE) < 0) { LOG_PRINTK("Error setting up Haptic Pin\n"); return -1; } gpio_pin_set_dt(&speaker_gpio_pin, 1); - + struct i2s_config config = { .word_size= WORD_SIZE, //how long is one left/right word. - .channels = NUMBER_OF_CHANNELS, //how many words in a frame 2 + .channels = NUMBER_OF_CHANNELS, //how many words in a frame 2 .format = I2S_FMT_DATA_FORMAT_LEFT_JUSTIFIED, //format // .format = I2S_FMT_DATA_FORMAT_I2S, .options = I2S_OPT_FRAME_CLK_MASTER | I2S_OPT_BIT_CLK_MASTER | I2S_OPT_BIT_CLK_GATED, //how to configure the mclock - .frame_clk_freq = SAMPLE_FREQUENCY, /* Sampling rate */ + .frame_clk_freq = SAMPLE_FREQUENCY, /* Sampling rate */ .mem_slab = &mem_slab,/* Memory slab to store rx/tx data */ .block_size = MAX_BLOCK_SIZE,/* size of ONE memory block in bytes */ .timeout = -1, /* Number of milliseconds to wait in case Tx queue is full or RX queue is empty, or 0, or SYS_FOREVER_MS */ }; int err = i2s_configure(audio_speaker, I2S_DIR_TX, &config); - if (err) + if (err) { LOG_ERR("Failed to configure Speaker (%d)", err); return -1; @@ -161,16 +161,16 @@ int speaker_init() } err = k_mem_slab_alloc(&mem_slab, &buzz_buffer, K_MSEC(200)); - if (err) + if (err) { LOG_INF("Failed to allocate for chime (%d)", err); return -1; } - + memset(rx_buffer, 0, MAX_BLOCK_SIZE); memset(buzz_buffer, 0, MAX_BLOCK_SIZE); - + return 0; } @@ -178,39 +178,39 @@ uint16_t speak(uint16_t len, const void *buf) //direct from bt { uint16_t amount = 0; amount = len; - if (len == 4) //if stage 1 + if (len == 4) //if stage 1 { current_length = ((uint32_t *)buf)[0]; LOG_INF("About to write %u bytes", current_length); ptr2 = (int16_t *)rx_buffer; clear_ptr = (int16_t *)rx_buffer; } - else + else { //if not stage 1 - if (current_length > PACKET_SIZE) + if (current_length > PACKET_SIZE) { LOG_INF("Data length: %u", len); current_length = current_length - PACKET_SIZE; LOG_INF("remaining data: %u", current_length); - for (int i = 0; i < (int)(len/2); i++) + for (int i = 0; i < (int)(len/2); i++) { - *ptr2++ = ((int16_t *)buf)[i]; - *ptr2++ = ((int16_t *)buf)[i]; + *ptr2++ = ((int16_t *)buf)[i]; + *ptr2++ = ((int16_t *)buf)[i]; } offset = offset + len; } - else if (current_length < PACKET_SIZE) + else if (current_length < PACKET_SIZE) { LOG_INF("entered the final stretch"); LOG_INF("Data length: %u", len); current_length = current_length - len; LOG_INF("remaining data: %u", current_length); // memcpy(rx_buffer+offset, buf, len); - for (int i = 0; i < len/2; i++) + for (int i = 0; i < len/2; i++) { - *ptr2++ = ((int16_t *)buf)[i]; - *ptr2++ = ((int16_t *)buf)[i]; + *ptr2++ = ((int16_t *)buf)[i]; + *ptr2++ = ((int16_t *)buf)[i]; } offset = offset + len; LOG_INF("offset: %u", offset); @@ -220,13 +220,13 @@ uint16_t speak(uint16_t len, const void *buf) //direct from bt { LOG_PRINTK("Failed to write I2S data: %d\n", res); } - i2s_trigger(audio_speaker, I2S_DIR_TX, I2S_TRIGGER_START);// calls are probably non blocking - if (res != 0) + i2s_trigger(audio_speaker, I2S_DIR_TX, I2S_TRIGGER_START);// calls are probably non blocking + if (res != 0) { LOG_PRINTK("Failed to drain I2S transmission: %d\n", res); - } + } res = i2s_trigger(audio_speaker, I2S_DIR_TX, I2S_TRIGGER_DRAIN); - if (res != 0) + if (res != 0) { LOG_PRINTK("Failed to drain I2S transmission: %d\n", res); } @@ -245,18 +245,18 @@ uint16_t speak(uint16_t len, const void *buf) //direct from bt void generate_gentle_chime(int16_t *buffer, int num_samples) { LOG_INF("Generating gentle chime");//2500 - const float frequencies[] = {523.25, 659.25, 783.99, 1046.50}; // C5, E5, G5, C6 + const float frequencies[] = {523.25f, 659.25f, 783.99f, 1046.50f}; // C5, E5, G5, C6 const int num_freqs = sizeof(frequencies) / sizeof(frequencies[0]);//4 - for (int i = 0; i < num_samples; i++) - { + for (int i = 0; i < num_samples; i++) + { float t = (float)i / SAMPLE_FREQUENCY;//0.000125 float sample = 0; - for (int j = 0; j < num_freqs; j++) + for (int j = 0; j < num_freqs; j++) { - sample += sinf(2 * PI * frequencies[j] * t) * (1.0 - t); + sample += sinf(2 * (float)PI * frequencies[j] * t) * (1.0f - t); } - int16_t int_sample = (int16_t)(sample / num_freqs * 32767 * 0.5); + int16_t int_sample = (int16_t)(sample / num_freqs * 32767 * 0.5f); buffer[i * NUM_CHANNELS] = int_sample; buffer[i * NUM_CHANNELS + 1] = int_sample; } @@ -273,43 +273,43 @@ int play_boot_sound(void) LOG_INF("Writing to speaker"); k_sleep(K_MSEC(100)); ret = i2s_write(audio_speaker, buffer, MAX_BLOCK_SIZE); - if (ret) + if (ret) { LOG_ERR("Failed to write initial I2S data: %d", ret); return ret; } ret = i2s_trigger(audio_speaker, I2S_DIR_TX, I2S_TRIGGER_START); - if (ret) + if (ret) { LOG_ERR("Failed to start I2S transmission: %d", ret); return ret; - } + } ret = i2s_trigger(audio_speaker, I2S_DIR_TX, I2S_TRIGGER_DRAIN); - if (ret != 0) + if (ret != 0) { LOG_ERR("Failed to drain I2S transmission: %d", ret); return ret; } - k_sleep(K_MSEC(3000)); + k_sleep(K_MSEC(3000)); return 0; } -int init_haptic_pin() +int init_haptic_pin() { - if (gpio_is_ready_dt(&haptic_gpio_pin)) + if (gpio_is_ready_dt(&haptic_gpio_pin)) { LOG_INF("Haptic Pin ready"); } - else + else { LOG_ERR("Error setting up Haptic Pin"); return -1; } - if (gpio_pin_configure_dt(&haptic_gpio_pin, GPIO_OUTPUT_INACTIVE) < 0) + if (gpio_pin_configure_dt(&haptic_gpio_pin, GPIO_OUTPUT_INACTIVE) < 0) { LOG_ERR("Error setting up Haptic Pin"); return -1; @@ -337,10 +337,10 @@ void play_haptic_milli(uint32_t duration) return; } gpio_pin_set_dt(&haptic_gpio_pin, 1); - // if (k_sem_take(&haptic_sem, K_MSEC(50)) != 0) + // if (k_sem_take(&haptic_sem, K_MSEC(50)) != 0) // { - // } + // } // else // { k_timer_start(&my_status_timer, K_MSEC(duration), K_NO_WAIT); diff --git a/omi/firmware/devkit/src/storage.c b/omi/firmware/devkit/src/storage.c index 5dde6a0be1..02eb623e5c 100644 --- a/omi/firmware/devkit/src/storage.c +++ b/omi/firmware/devkit/src/storage.c @@ -58,7 +58,7 @@ struct bt_gatt_service storage_service = BT_GATT_SERVICE(storage_service_attr); bool storage_is_on = false; -static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) +static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) { storage_is_on = true; @@ -77,7 +77,7 @@ static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint } -static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) +static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { k_msleep(10); uint32_t amount[2] = {0}; @@ -91,27 +91,27 @@ static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt uint8_t transport_started = 0; -static uint16_t packet_next_index = 0; +__attribute__((unused)) static uint16_t packet_next_index = 0; #define SD_BLE_SIZE 440 static uint8_t storage_write_buffer[SD_BLE_SIZE]; static uint32_t offset = 0; -static uint8_t index = 0; -static uint8_t current_packet_size = 0; -static uint8_t tx_buffer_size = 0; +__attribute__((unused)) static uint8_t index = 0; +__attribute__((unused)) static uint8_t current_packet_size = 0; +__attribute__((unused)) static uint8_t tx_buffer_size = 0; static uint8_t stop_started = 0; static uint8_t delete_started = 0; static uint8_t current_read_num = 1; uint32_t remaining_length = 0; -static int setup_storage_tx() +static int setup_storage_tx() { - transport_started = (uint8_t)0; + transport_started = (uint8_t)0; // offset = 0; LOG_INF("about to transmit storage\n"); k_msleep(1000); int res = move_read_pointer(current_read_num); - if (res) + if (res) { LOG_INF("bad pointer"); transport_started = 0; @@ -121,30 +121,30 @@ static int setup_storage_tx() } LOG_INF("current read ptr %d",current_read_num); - + remaining_length = file_num_array[current_read_num-1]; - if(current_read_num == file_count) + if(current_read_num == file_count) { remaining_length = get_file_size(file_count); } remaining_length = remaining_length - offset; - + // offset=offset_; LOG_INF("remaining length: %d",remaining_length); LOG_INF("offset: %d",offset); LOG_INF("file: %d",current_read_num); - + return 0; } uint8_t delete_num = 0; uint8_t nuke_started = 0; static uint8_t heartbeat_count = 0; -static uint8_t parse_storage_command(void *buf,uint16_t len) +static uint8_t parse_storage_command(const void *buf,uint16_t len) { - if (len != 6 && len != 2) + if (len != 6 && len != 2) { LOG_INF("invalid command"); return INVALID_COMMAND; @@ -152,44 +152,44 @@ static uint8_t parse_storage_command(void *buf,uint16_t len) const uint8_t command = ((uint8_t*)buf)[0]; const uint8_t file_num = ((uint8_t*)buf)[1]; uint32_t size = 0; - if ( len == 6 ) + if ( len == 6 ) { size = ((uint8_t*)buf)[2] <<24 |((uint8_t*)buf)[3] << 16 | ((uint8_t*)buf)[4] << 8 | ((uint8_t*)buf)[5]; } LOG_PRINTK("command successful: command: %d file: %d size: %d \n",command,file_num,size); - if (file_num == 0) + if (file_num == 0) { LOG_INF("invalid file count 0"); return INVALID_FILE_SIZE; } - if (file_num > file_count) //invalid file count + if (file_num > file_count) //invalid file count { LOG_INF("invalid file count"); return INVALID_FILE_SIZE; //add audio all? } - if (command == READ_COMMAND) //read - { + if (command == READ_COMMAND) //read + { uint32_t temp = file_num_array[file_num-1]; - if ( file_num == ( file_count ) ) + if ( file_num == ( file_count ) ) { LOG_INF("file_count == final file"); offset = size - (size % SD_BLE_SIZE); //round down to nearest SD_BLE_SIZE current_read_num = file_num; - transport_started = 1; + transport_started = 1; } - else if (temp == 0) + else if (temp == 0) { LOG_INF("file size is 0"); return ZERO_FILE_SIZE; } - else if (size > temp) + else if (size > temp) { LOG_INF("requested size is too large"); return 5; } - else + else { LOG_INF("valid command, setting up "); offset = size - (size % SD_BLE_SIZE); @@ -197,12 +197,12 @@ static uint8_t parse_storage_command(void *buf,uint16_t len) transport_started = 1; } } - else if (command == DELETE_COMMAND) + else if (command == DELETE_COMMAND) { delete_num = file_num; delete_started = 1; } - else if (command == NUKE) + else if (command == NUKE) { nuke_started = 1; } @@ -215,7 +215,7 @@ static uint8_t parse_storage_command(void *buf,uint16_t len) { heartbeat_count = 0; } - else + else { LOG_INF("invalid command \n"); return 6; @@ -224,14 +224,14 @@ static uint8_t parse_storage_command(void *buf,uint16_t len) } -static ssize_t storage_write_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags) +static ssize_t storage_write_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags) { LOG_INF("about to schedule the storage"); LOG_INF("was sent %d ", ((uint8_t*)buf)[0] ); uint8_t result_buffer[1] = {0}; uint8_t result = parse_storage_command(buf,len); - result_buffer[0] = result; + result_buffer[0] = result; LOG_INF("length of storage write: %d",len); LOG_INF("result: %d ", result); bt_gatt_notify(conn, &storage_service.attrs[1], &result_buffer,1); @@ -239,7 +239,7 @@ static ssize_t storage_write_handler(struct bt_conn *conn, const struct bt_gatt_ return len; } -// static void write_to_gatt(struct bt_conn *conn) +// static void write_to_gatt(struct bt_conn *conn) // { // uint32_t id = packet_next_index++; // index = 0; @@ -255,11 +255,11 @@ static ssize_t storage_write_handler(struct bt_conn *conn, const struct bt_gatt_ // index++; // int err = bt_gatt_notify(conn, &storage_service.attrs[1], &storage_write_buffer,packet_size+FRAME_PREFIX_LENGTH); -// if (err) +// if (err) // { // LOG_PRINTK("error writing to gatt: %d\n",err); // } -// else +// else // { // remaining_length = remaining_length - OPUS_ENTRY_LENGTH; // } @@ -270,46 +270,47 @@ static void write_to_gatt(struct bt_conn *conn) { //unsafe. designed for max spe uint32_t packet_size = MIN(remaining_length,SD_BLE_SIZE); int r = read_audio_data(storage_write_buffer,packet_size,offset); + (void)r; offset = offset + packet_size; int err = bt_gatt_notify(conn, &storage_service.attrs[1], &storage_write_buffer,packet_size); - if (err) + if (err) { LOG_PRINTK("error writing to gatt: %d\n",err); } - else + else { remaining_length = remaining_length - SD_BLE_SIZE; } // LOG_PRINTK("wrote to gatt %d\n",err); } -void storage_write(void) +void storage_write(void) { - while (1) + while (1) { struct bt_conn *conn = get_current_connection(); - - if ( transport_started ) + + if ( transport_started ) { LOG_INF("transpor started in side : %d",transport_started); setup_storage_tx(); } //probably prefer to implement using work orders for delete,nuke,etc... - if (delete_started) - { + if (delete_started) + { LOG_INF("delete:%d\n",delete_started); int err = clear_audio_file(1); offset = 0; save_offset(offset); - - if (err) + + if (err) { LOG_PRINTK("error clearing\n"); } - else + else { uint8_t result_buffer[1] = {200}; - if (conn) + if (conn) { bt_gatt_notify(get_current_connection(), &storage_service.attrs[1], &result_buffer,1); } @@ -317,14 +318,14 @@ void storage_write(void) delete_started = 0; k_msleep(10); } - if (nuke_started) + if (nuke_started) { clear_audio_directory(); save_offset(0); nuke_started = 0; } - if (stop_started) - { + if (stop_started) + { remaining_length = 0; stop_started = 0; save_offset(offset); @@ -337,9 +338,9 @@ void storage_write(void) // continue; } - if(remaining_length > 0 ) + if(remaining_length > 0 ) { - if (conn == NULL) + if (conn == NULL) { LOG_ERR("invalid connection"); remaining_length = 0; @@ -352,9 +353,9 @@ void storage_write(void) write_to_gatt(conn); heartbeat_count = (heartbeat_count + 1) % (MAX_HEARTBEAT_FRAMES + 1); - + transport_started = 0; - if (remaining_length == 0 ) + if (remaining_length == 0 ) { if(stop_started) { @@ -365,10 +366,11 @@ void storage_write(void) LOG_PRINTK("done. attempting to download more files\n"); uint8_t stop_result[1] = {100}; int err = bt_gatt_notify(get_current_connection(), &storage_service.attrs[1], &stop_result,1); + (void)err; k_sleep(K_MSEC(10)); } - } + } } k_yield(); @@ -376,7 +378,7 @@ void storage_write(void) } -int storage_init() +int storage_init() { k_thread_create(&storage_thread, storage_stack, K_THREAD_STACK_SIZEOF(storage_stack), (k_thread_entry_t)storage_write, NULL, NULL, NULL, K_PRIO_PREEMPT(7), 0, K_NO_WAIT); return 0; diff --git a/omi/firmware/devkit/src/transport.c b/omi/firmware/devkit/src/transport.c index 32926cbbf0..7551bc6e80 100644 --- a/omi/firmware/devkit/src/transport.c +++ b/omi/firmware/devkit/src/transport.c @@ -48,6 +48,8 @@ static ssize_t audio_codec_read_characteristic(struct bt_conn *conn, const struc static void dfu_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value); static ssize_t dfu_control_point_write_handler(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, uint16_t offset, uint8_t flags); +static const char *phy2str(uint8_t phy); + // // Service and Characteristic // @@ -93,7 +95,7 @@ static struct bt_gatt_service dfu_service = BT_GATT_SERVICE(dfu_service_attr); //this code activates the onboard accelerometer. some cute ideas may include shaking the necklace to color strobe // static struct sensors mega_sensor; -static struct device *lsm6dsl_dev; +static const struct device *lsm6dsl_dev; //Arbritrary uuid, feel free to change static struct bt_uuid_128 accel_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x32403790,0x0000,0x1000,0x7450,0xBF445E5829A2)); static struct bt_uuid_128 accel_uuid_x = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x32403791,0x0000,0x1000,0x7450,0xBF445E5829A2)); @@ -142,7 +144,7 @@ void broadcast_accel(struct k_work *work_item) { k_work_reschedule(&accel_work, K_MSEC(ACCEL_REFRESH_INTERVAL)); } -struct gpio_dt_spec accel_gpio_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio1)), .pin=8, .dt_flags = GPIO_INT_DISABLE}; +struct gpio_dt_spec accel_gpio_pin = {.port = DEVICE_DT_GET(DT_NODELABEL(gpio1)), .pin=8, .dt_flags = 0}; //use d4,d5 static void accel_ccc_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) @@ -267,7 +269,9 @@ static ssize_t audio_data_write_handler(struct bt_conn *conn, const struct bt_ga { uint16_t amount = 400; int16_t *int16_buf = (int16_t *)buf; + (void)int16_buf; uint8_t *data = (uint8_t *)buf; + (void)data; bt_gatt_notify(conn, attr, &amount, sizeof(amount)); amount = speak(len, buf); return len; @@ -568,7 +572,7 @@ static bool push_to_gatt(struct bt_conn *conn) #define OPUS_PADDED_LENGTH 80 #define MAX_WRITE_SIZE 440 static uint8_t storage_temp_data[MAX_WRITE_SIZE]; -static uint32_t offset = 0; +__attribute__((unused)) static uint32_t offset = 0; static uint16_t buffer_offset = 0; // bool write_to_storage(void) // { @@ -636,10 +640,10 @@ bool write_to_storage(void) {//max possible packing return true; } -static bool use_storage = true; +__attribute__((unused)) static bool use_storage = true; #define MAX_FILES 10 #define MAX_AUDIO_FILE_SIZE 300000 -static int recent_file_size_updated = 0; +__attribute__((unused)) static int recent_file_size_updated = 0; static uint8_t heartbeat_count = 0; void update_file_size() { @@ -782,6 +786,9 @@ int bt_off() int bt_on() { int err = bt_enable(NULL); + if( err < 0 ) { + return err; + } bt_le_adv_start(BT_LE_ADV_CONN, bt_ad, ARRAY_SIZE(bt_ad), bt_sd, ARRAY_SIZE(bt_sd)); bt_gatt_service_register(&storage_service); sd_on(); @@ -880,3 +887,16 @@ void accel_off() { gpio_pin_set_dt(&accel_gpio_pin, 0); } + + +// This helper function appears to not be exported anywhere in the SDK +static const char *phy2str(uint8_t phy) +{ + switch (phy) { + case 0: return "No packets"; + case BT_GAP_LE_PHY_1M: return "LE 1M"; + case BT_GAP_LE_PHY_2M: return "LE 2M"; + case BT_GAP_LE_PHY_CODED: return "LE Coded"; + default: return "Unknown"; + } +} \ No newline at end of file diff --git a/omi/firmware/devkit/src/transport.h b/omi/firmware/devkit/src/transport.h index f5e40fff4a..c9b859c2bd 100644 --- a/omi/firmware/devkit/src/transport.h +++ b/omi/firmware/devkit/src/transport.h @@ -11,7 +11,7 @@ typedef struct sensors { struct sensor_value g_y; struct sensor_value g_z; -}; +} sensors_t; /** * @brief Initialize the BLE transport logic * diff --git a/omi/firmware/devkit/src/usb.c b/omi/firmware/devkit/src/usb.c index cbbbd15b4d..69341009d5 100644 --- a/omi/firmware/devkit/src/usb.c +++ b/omi/firmware/devkit/src/usb.c @@ -12,7 +12,7 @@ LOG_MODULE_REGISTER(usb, CONFIG_LOG_DEFAULT_LEVEL); //add all device drivers here? bool usb_charge = false; -usb_dc_status_callback udc_status_cb(enum usb_dc_status_code status, +void udc_status_cb(enum usb_dc_status_code status, const uint8_t *param) { switch (status) @@ -25,9 +25,7 @@ usb_dc_status_callback udc_status_cb(enum usb_dc_status_code status, break; default: usb_charge = true; - } - - return; + }; } int init_usb() From 09ed678b1d045c5bdb4fbb424d9ad42598fbca53 Mon Sep 17 00:00:00 2001 From: Andrew Pullin Date: Mon, 28 Apr 2025 10:12:48 -0700 Subject: [PATCH 3/3] Moves Opus codec to library build, with appropriate flags Also resolves many warnings --- omi/firmware/devkit/CMakeLists.txt | 153 +---------------------------- 1 file changed, 4 insertions(+), 149 deletions(-) diff --git a/omi/firmware/devkit/CMakeLists.txt b/omi/firmware/devkit/CMakeLists.txt index e12ff8ba38..ae54e714a4 100644 --- a/omi/firmware/devkit/CMakeLists.txt +++ b/omi/firmware/devkit/CMakeLists.txt @@ -20,152 +20,7 @@ target_sources(app PRIVATE # src/nfc.c future release ) -target_sources_ifdef(CONFIG_OMI_CODEC_OPUS app PRIVATE - src/lib/opus-1.2.1/A2NLSF.c - src/lib/opus-1.2.1/CNG.c - src/lib/opus-1.2.1/HP_variable_cutoff.c - src/lib/opus-1.2.1/LPC_analysis_filter.c - src/lib/opus-1.2.1/LPC_fit.c - src/lib/opus-1.2.1/LPC_inv_pred_gain.c - src/lib/opus-1.2.1/LP_variable_cutoff.c - src/lib/opus-1.2.1/LTP_analysis_filter_FIX.c - src/lib/opus-1.2.1/LTP_scale_ctrl_FIX.c - src/lib/opus-1.2.1/NLSF2A.c - src/lib/opus-1.2.1/NLSF_VQ.c - src/lib/opus-1.2.1/NLSF_VQ_weights_laroia.c - src/lib/opus-1.2.1/NLSF_decode.c - src/lib/opus-1.2.1/NLSF_del_dec_quant.c - src/lib/opus-1.2.1/NLSF_encode.c - src/lib/opus-1.2.1/NLSF_stabilize.c - src/lib/opus-1.2.1/NLSF_unpack.c - src/lib/opus-1.2.1/NSQ.c - src/lib/opus-1.2.1/NSQ_del_dec.c - src/lib/opus-1.2.1/PLC.c - src/lib/opus-1.2.1/VAD.c - src/lib/opus-1.2.1/VQ_WMat_EC.c - src/lib/opus-1.2.1/ana_filt_bank_1.c - src/lib/opus-1.2.1/analysis.c - src/lib/opus-1.2.1/apply_sine_window_FIX.c - src/lib/opus-1.2.1/autocorr_FIX.c - src/lib/opus-1.2.1/bands.c - src/lib/opus-1.2.1/biquad_alt.c - src/lib/opus-1.2.1/burg_modified_FIX.c - src/lib/opus-1.2.1/bwexpander.c - src/lib/opus-1.2.1/bwexpander_32.c - src/lib/opus-1.2.1/celt.c - src/lib/opus-1.2.1/celt_decoder.c - src/lib/opus-1.2.1/celt_encoder.c - src/lib/opus-1.2.1/celt_lpc.c - src/lib/opus-1.2.1/arm/celt_pitch_xcorr_arm_gcc.s - src/lib/opus-1.2.1/check_control_input.c - src/lib/opus-1.2.1/code_signs.c - src/lib/opus-1.2.1/control_SNR.c - src/lib/opus-1.2.1/control_audio_bandwidth.c - src/lib/opus-1.2.1/control_codec.c - src/lib/opus-1.2.1/corrMatrix_FIX.c - src/lib/opus-1.2.1/cwrs.c - src/lib/opus-1.2.1/debug.c - src/lib/opus-1.2.1/dec_API.c - src/lib/opus-1.2.1/decode_core.c - src/lib/opus-1.2.1/decode_frame.c - src/lib/opus-1.2.1/decode_indices.c - src/lib/opus-1.2.1/decode_parameters.c - src/lib/opus-1.2.1/decode_pitch.c - src/lib/opus-1.2.1/decode_pulses.c - src/lib/opus-1.2.1/decoder_set_fs.c - src/lib/opus-1.2.1/enc_API.c - src/lib/opus-1.2.1/encode_frame_FIX.c - src/lib/opus-1.2.1/encode_indices.c - src/lib/opus-1.2.1/encode_pulses.c - src/lib/opus-1.2.1/entcode.c - src/lib/opus-1.2.1/entdec.c - src/lib/opus-1.2.1/entenc.c - src/lib/opus-1.2.1/find_LPC_FIX.c - src/lib/opus-1.2.1/find_LTP_FIX.c - src/lib/opus-1.2.1/find_pitch_lags_FIX.c - src/lib/opus-1.2.1/find_pred_coefs_FIX.c - src/lib/opus-1.2.1/gain_quant.c - src/lib/opus-1.2.1/init_decoder.c - src/lib/opus-1.2.1/init_encoder.c - src/lib/opus-1.2.1/inner_prod_aligned.c - src/lib/opus-1.2.1/interpolate.c - src/lib/opus-1.2.1/k2a_FIX.c - src/lib/opus-1.2.1/k2a_Q16_FIX.c - src/lib/opus-1.2.1/kiss_fft.c - src/lib/opus-1.2.1/laplace.c - src/lib/opus-1.2.1/lin2log.c - src/lib/opus-1.2.1/log2lin.c - src/lib/opus-1.2.1/mathops.c - src/lib/opus-1.2.1/mdct.c - src/lib/opus-1.2.1/mlp.c - src/lib/opus-1.2.1/mlp_data.c - src/lib/opus-1.2.1/modes.c - src/lib/opus-1.2.1/noise_shape_analysis_FIX.c - src/lib/opus-1.2.1/opus.c - src/lib/opus-1.2.1/opus_decoder.c - src/lib/opus-1.2.1/opus_encoder.c - src/lib/opus-1.2.1/opus_multistream.c - src/lib/opus-1.2.1/opus_multistream_decoder.c - src/lib/opus-1.2.1/opus_multistream_encoder.c - src/lib/opus-1.2.1/pitch.c - src/lib/opus-1.2.1/pitch_analysis_core_FIX.c - src/lib/opus-1.2.1/pitch_est_tables.c - src/lib/opus-1.2.1/process_NLSFs.c - src/lib/opus-1.2.1/process_gains_FIX.c - src/lib/opus-1.2.1/quant_LTP_gains.c - src/lib/opus-1.2.1/quant_bands.c - src/lib/opus-1.2.1/rate.c - src/lib/opus-1.2.1/regularize_correlations_FIX.c - src/lib/opus-1.2.1/repacketizer.c - src/lib/opus-1.2.1/resampler.c - src/lib/opus-1.2.1/resampler_down2.c - src/lib/opus-1.2.1/resampler_down2_3.c - src/lib/opus-1.2.1/resampler_private_AR2.c - src/lib/opus-1.2.1/resampler_private_IIR_FIR.c - src/lib/opus-1.2.1/resampler_private_down_FIR.c - src/lib/opus-1.2.1/resampler_private_up2_HQ.c - src/lib/opus-1.2.1/resampler_rom.c - src/lib/opus-1.2.1/residual_energy16_FIX.c - src/lib/opus-1.2.1/residual_energy_FIX.c - src/lib/opus-1.2.1/schur64_FIX.c - src/lib/opus-1.2.1/schur_FIX.c - src/lib/opus-1.2.1/shell_coder.c - src/lib/opus-1.2.1/sigm_Q15.c - src/lib/opus-1.2.1/sort.c - src/lib/opus-1.2.1/stereo_LR_to_MS.c - src/lib/opus-1.2.1/stereo_MS_to_LR.c - src/lib/opus-1.2.1/stereo_decode_pred.c - src/lib/opus-1.2.1/stereo_encode_pred.c - src/lib/opus-1.2.1/stereo_find_predictor.c - src/lib/opus-1.2.1/stereo_quant_pred.c - src/lib/opus-1.2.1/sum_sqr_shift.c - src/lib/opus-1.2.1/table_LSF_cos.c - src/lib/opus-1.2.1/tables_LTP.c - src/lib/opus-1.2.1/tables_NLSF_CB_NB_MB.c - src/lib/opus-1.2.1/tables_NLSF_CB_WB.c - src/lib/opus-1.2.1/tables_gain.c - src/lib/opus-1.2.1/tables_other.c - src/lib/opus-1.2.1/tables_pitch_lag.c - src/lib/opus-1.2.1/tables_pulses_per_block.c - src/lib/opus-1.2.1/vector_ops_FIX.c - src/lib/opus-1.2.1/vq.c - src/lib/opus-1.2.1/warped_autocorrelation_FIX.c - src/lib/opus-1.2.1/arm/celt_pitch_xcorr_arm_gcc.s -) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DARM_MATH_CM4") -# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DVAR_ARRAYS") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPUS_ARM_ASM") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPUS_ARM_INLINE_ASM") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPUS_ARM_INLINE_EDSP") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPUS_ARM_INLINE_MEDIA") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPUS_ARM_MAY_HAVE_EDSP") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPUS_ARM_PRESUME_EDSP") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPUS_BUILD") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUSE_ALLOCA") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFIXED_POINT -DDISABLE_FLOAT_API") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_CONFIG_H") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_ALLOCA_H") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsingle-precision-constant") # A lot of constants are written as doubles -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LRINT -DHAVE_LRINTF") - +if(CONFIG_OMI_CODEC_OPUS) + add_subdirectory(src/lib/opus-1.2.1/) + target_link_libraries(app PRIVATE opus_codec) +endif()