From 01dc3a1690e8c55cebb2e5fe570f180b9199ae93 Mon Sep 17 00:00:00 2001 From: Kim Mansfield Date: Mon, 13 Mar 2023 05:38:38 -0600 Subject: [PATCH] Add gecko_adc driver usage to tmo_shell These changes add the gecko_adc driver usage into tmo_shell. Signed-off-by: Kim Mansfield --- samples/tmo_shell/CMakeLists.txt | 1 - samples/tmo_shell/boards/tmo_dev_edge.overlay | 39 ++ samples/tmo_shell/prj.conf | 1 + samples/tmo_shell/src/tmo_adc.c | 200 ---------- samples/tmo_shell/src/tmo_adc.h | 15 - samples/tmo_shell/src/tmo_battery_ctrl.c | 26 ++ samples/tmo_shell/src/tmo_battery_ctrl.h | 2 + samples/tmo_shell/src/tmo_ble_demo.c | 113 ++++-- samples/tmo_shell/src/tmo_shell.c | 346 +++++++++++++----- samples/tmo_shell/src/tmo_web_demo.c | 53 ++- 10 files changed, 445 insertions(+), 351 deletions(-) delete mode 100644 samples/tmo_shell/src/tmo_adc.c delete mode 100644 samples/tmo_shell/src/tmo_adc.h diff --git a/samples/tmo_shell/CMakeLists.txt b/samples/tmo_shell/CMakeLists.txt index e2ee9d5..9031ebe 100644 --- a/samples/tmo_shell/CMakeLists.txt +++ b/samples/tmo_shell/CMakeLists.txt @@ -27,7 +27,6 @@ target_sources(app PRIVATE src/buzzer_test.c) target_sources(app PRIVATE src/led_test.c) target_sources(app PRIVATE src/misc_test.c) target_sources(app PRIVATE src/tmo_file.c) -target_sources(app PRIVATE src/tmo_adc.c) target_sources(app PRIVATE src/tmo_bq24250.c) target_sources(app PRIVATE src/tmo_battery_ctrl.c) target_sources(app PRIVATE src/tmo_sntp.c) diff --git a/samples/tmo_shell/boards/tmo_dev_edge.overlay b/samples/tmo_shell/boards/tmo_dev_edge.overlay index fb28cb9..3ff91bc 100644 --- a/samples/tmo_shell/boards/tmo_dev_edge.overlay +++ b/samples/tmo_shell/boards/tmo_dev_edge.overlay @@ -3,3 +3,42 @@ * * SPDX-License-Identifier: Apache-2.0 */ + #include + #include + +/ { + zephyr,user { + /* adjust channel number according to pinmux in board.dts */ + io-channels = <&adc 0>, <&adc 1>; + battsense-gpios = <&gpiok 0 GPIO_ACTIVE_HIGH>; + }; +}; + +&gpiok { + hog1 { + gpio-hog; + gpios = <0 GPIO_ACTIVE_HIGH>; + output-high; + }; +}; + +&adc { + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + zephyr,gain = "ADC_GAIN_1_3"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <12>; + }; + + channel@1 { + reg = <1>; + zephyr,gain = "ADC_GAIN_1_3"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <12>; + }; +}; \ No newline at end of file diff --git a/samples/tmo_shell/prj.conf b/samples/tmo_shell/prj.conf index 691a5d9..4ba2a73 100644 --- a/samples/tmo_shell/prj.conf +++ b/samples/tmo_shell/prj.conf @@ -160,3 +160,4 @@ CONFIG_LOG_PRINTK=n CONFIG_TMO_TEST_MFG_CHECK_GOLDEN=y CONFIG_TMO_TEST_MFG_CHECK_ACCESS_CODE=y CONFIG_DFU_GECKO_LIB=y +CONFIG_ADC_GECKO=y diff --git a/samples/tmo_shell/src/tmo_adc.c b/samples/tmo_shell/src/tmo_adc.c deleted file mode 100644 index 16f49a3..0000000 --- a/samples/tmo_shell/src/tmo_adc.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * # License - * Copyright 2020 Silicon Laboratories Inc. www.silabs.com - * Copyright (c) 2022 T-Mobile USA, Inc. - * - * SPDX-License-Identifier: Apache-2.0 - * - */ -#include -#include -#include -#include "em_device.h" -#include "em_chip.h" -#include "em_cmu.h" -#include "em_adc.h" -#include "tmo_battery_ctrl.h" -#include "tmo_adc.h" -#include "board.h" -#include -LOG_MODULE_REGISTER(tmo_adc, LOG_LEVEL_INF); - -#define adcFreq 16000000 -K_SEM_DEFINE(adc_sem, 0, 1); - -static ADC_InitSingle_TypeDef initSingle_bv = ADC_INITSINGLE_DEFAULT; -static ADC_InitSingle_TypeDef initSingle_hwid = ADC_INITSINGLE_DEFAULT; - -#ifdef HWID_APORT -/** - * @brief Set the VBAT_SNS_EN Pin High to enable ADC readings - * - */ -static void set_vbat_sens_en(bool enable) -{ - // pin = 0 - // mode = gpioModeEnabled; - // out is 1 otherwise it will be input - // Set PK0/PinE2 as output so it can be -#ifdef VBAT_EN_PORT - GPIO_PinModeSet(VBAT_EN_PORT, VBAT_EN_PIN, gpioModePushPull, enable); -#endif /* VBAT_EN_PORT */ -} -#endif /* HWID_APORT */ - -/** - * @brief Initialize the Gecko ADC - * - */ -void initADC(void) -{ - - // Enable ADC0 clock - CMU_ClockEnable(cmuClock_ADC0, true); - - // Declare init structs - ADC_Init_TypeDef init = ADC_INIT_DEFAULT; - - // Modify init structs and initialize - init.prescale = ADC_PrescaleCalc(adcFreq, 0); // Init to max ADC clock for Series 1 - - initSingle_bv.diff = false; // single ended - initSingle_bv.reference = adcRef2V5; // internal 2.5V reference - initSingle_bv.resolution = adcRes12Bit; // 12-bit resolution - initSingle_bv.acqTime = adcAcqTime32; // set acquisition time to meet minimum requirement - - memcpy(&initSingle_hwid, &initSingle_bv, sizeof(initSingle_hwid)); - - // Select ADC input. See README for corresponding EXP header pin. - // initSingle.posSel = adcPosSelAPORT4XCH10; -#ifdef HWID_APORT - initSingle_hwid.posSel = HWID_APORT; -#endif /* HWID_APORT */ -#ifdef VBAT_APORT - initSingle_bv.posSel = VBAT_APORT; -#endif /* VBAT_APORT */ - - init.timebase = ADC_TimebaseCalc(0); - - ADC_Init(ADC0, &init); - - int hwid = read_hwid(); - - LOG_INF("HWID = %d\n", hwid); -} - -/** - * @brief Exponential filter for battery level - */ -static void apply_filter(float *bv) -{ - static float s_filtered_capacity = -1; - static bool s_battery_is_charging = false; - bool battery_is_charging; - - // If there has been a switch between charger and battery, reset the filter - battery_is_charging = is_battery_charging(); - if (s_battery_is_charging != battery_is_charging) { - s_battery_is_charging = battery_is_charging; - s_filtered_capacity = -1; - } - - if (s_filtered_capacity < 0) { - s_filtered_capacity = *bv; - } - *bv = s_filtered_capacity = s_filtered_capacity * 0.95 + (*bv) * 0.05; -} - -/* - * @brief This function writes the amount of battery charge remaining - * (to the nearest 1%) in bv. - * It returns true if successful, or false if there is an issue - */ -bool millivolts_to_percent(uint32_t millivolts, uint8_t *percent) { - float curBv = get_remaining_capacity((float) millivolts / 1000); - apply_filter(&curBv); - *percent = (uint8_t) (curBv + 0.5); - return true; -} - -/** - * @brief Main function - */ -int read_battery_voltage(void) -{ -#ifdef VBAT_APORT - uint32_t sample; - uint32_t millivolts; - float millivolts_f; - // Start ADC conversion - k_sem_take(&adc_sem, K_MSEC(500)); - - set_vbat_sens_en(true); - k_msleep(100); - - ADC_InitSingle(ADC0, &initSingle_bv); - ADC_Start(ADC0, adcStartSingle); - - // Wait for conversion to be complete - while(!(ADC0->STATUS & _ADC_STATUS_SINGLEDV_MASK)); - - // Get ADC result - sample = ADC_DataSingleGet(ADC0); - - set_vbat_sens_en(false); - - k_sem_give(&adc_sem); - - // Calculate input voltage in mV - millivolts_f = (sample * 2500.0) / 4096.0; - - // On the 2nd generation dev edge, voltage on PA2 is - // one third the actual battery voltage - millivolts = (uint32_t) (3.0 * millivolts_f + 0.5); - - return (millivolts); -#else - return 0; -#endif /* VBAT_APORT */ -} - -/** - * @brief Read HWID divider voltage - * - * @return int Millivolts - */ -int read_hwid(void) -{ -#ifdef HWID_APORT - uint32_t sample; - uint32_t millivolts; - float millivolts_f; - // Start ADC conversion - k_sem_take(&adc_sem, K_MSEC(500)); - - set_vbat_sens_en(true); - k_msleep(100); - - ADC_InitSingle(ADC0, &initSingle_hwid); - ADC_Start(ADC0, adcStartSingle); - - // Wait for conversion to be complete - while(!(ADC0->STATUS & _ADC_STATUS_SINGLEDV_MASK)); - - // Get ADC result - sample = ADC_DataSingleGet(ADC0); - - set_vbat_sens_en(false); - - k_sem_give(&adc_sem); - - // Calculate input voltage in mV - millivolts_f = (sample * 2500.0) / 4096.0; - - millivolts = (uint32_t) millivolts_f; - - return (millivolts); -#else - return 0; -#endif /* HWID_APORT */ -} diff --git a/samples/tmo_shell/src/tmo_adc.h b/samples/tmo_shell/src/tmo_adc.h deleted file mode 100644 index cc9b83a..0000000 --- a/samples/tmo_shell/src/tmo_adc.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2022 T-Mobile USA, Inc. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef TMO_ADC_H -#define TMO_ADC_H - -int read_battery_voltage(void); -int read_hwid(void); -bool millivolts_to_percent(uint32_t millivolts, uint8_t *bv); -void initADC(); - -#endif diff --git a/samples/tmo_shell/src/tmo_battery_ctrl.c b/samples/tmo_shell/src/tmo_battery_ctrl.c index b23de08..b50169a 100755 --- a/samples/tmo_shell/src/tmo_battery_ctrl.c +++ b/samples/tmo_shell/src/tmo_battery_ctrl.c @@ -92,3 +92,29 @@ int get_battery_charging_status(uint8_t *charging, uint8_t *vbus, uint8_t *attac return status; } + + +void battery_apply_filter(float *bv) +{ + static float s_filtered_capacity = -1; + static bool s_battery_is_charging = false; + bool battery_is_charging; + + // If there has been a switch between charger and battery, reset the filter + battery_is_charging = is_battery_charging(); + if (s_battery_is_charging != battery_is_charging) { + s_battery_is_charging = battery_is_charging; + s_filtered_capacity = -1; + } + + if (s_filtered_capacity < 0) { + s_filtered_capacity = *bv; + } + *bv = s_filtered_capacity = s_filtered_capacity * 0.95 + (*bv) * 0.05; +} + +uint8_t battery_millivolts_to_percent(uint32_t millivolts) { + float curBv = get_remaining_capacity((float) millivolts / 1000); + battery_apply_filter(&curBv); + return (uint8_t) (curBv + 0.5); +} // Calculate input voltage in mV diff --git a/samples/tmo_shell/src/tmo_battery_ctrl.h b/samples/tmo_shell/src/tmo_battery_ctrl.h index ef8a60a..a000b7d 100644 --- a/samples/tmo_shell/src/tmo_battery_ctrl.h +++ b/samples/tmo_shell/src/tmo_battery_ctrl.h @@ -36,4 +36,6 @@ float get_remaining_capacity(float battery_voltage); bool is_battery_charging(void); int get_battery_charging_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault); +void battery_apply_filter(float *bv); +uint8_t battery_millivolts_to_percent(uint32_t millivolts); #endif diff --git a/samples/tmo_shell/src/tmo_ble_demo.c b/samples/tmo_shell/src/tmo_ble_demo.c index 70005d2..348a0b0 100644 --- a/samples/tmo_shell/src/tmo_ble_demo.c +++ b/samples/tmo_shell/src/tmo_ble_demo.c @@ -29,11 +29,11 @@ #include #include #include +#include #include "tmo_buzzer.h" #include "tmo_web_demo.h" #include "tmo_ble_demo.h" -#include "tmo_adc.h" #include "tmo_gnss.h" #include "tmo_smp.h" #include "tmo_shell.h" @@ -46,8 +46,10 @@ static inline void strupper(char *p) { while (*p) *p++ &= 0xdf;} #define uuid128(...) BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(__VA_ARGS__)) +extern const struct adc_dt_spec adc_channels[]; extern struct bt_conn *get_acl_conn(int i); extern int get_active_le_conns(); +extern const struct device *battery_dev; K_SEM_DEFINE(update_sem, 0, 1); @@ -191,27 +193,64 @@ static ssize_t battery_voltage_get(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { - uint8_t percent = 0; - uint32_t millivolts = 0; uint8_t battery_attached = 0; - uint8_t charging = 0; uint8_t vbus = 0; + uint8_t charging = 0; uint8_t fault = 0; + uint8_t err; + uint8_t percent = 0; + int32_t val_mv; + + int16_t buffer; + struct adc_sequence sequence = { + .buffer = &buffer, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buffer), + }; get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); - /* flush the fault status out by reading again */ - if (fault) { - get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); + if (battery_attached != 0) { + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + err = adc_read(adc_channels[0].dev, &sequence); + if (err < 0) { + shell_error(shell,"Could not read (%d)\n", err); + return err; + } } - /* there can be 2 of these to flush */ - if (fault) { - get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); + + /* flush the fault status out by reading again */ + if (fault) { + get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); + if (battery_attached != 0) { + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + err = adc_read(adc_channels[0].dev, &sequence); + if (err < 0) { + shell_error(shell,"Could not read (%d)\n", err); + return err; + } + } + + } + /* there can be 2 of these to flush */ + if (fault) { + get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); + if (battery_attached != 0) { + err = adc_read(adc_channels[0].dev, &sequence); + if (err < 0) { + shell_error(shell,"Could not read (%d)\n", err); + return err; + } + } + val_mv = (int32_t)buf; + err = adc_raw_to_millivolts_dt(&adc_channels[0], &val_mv); + /* conversion to mV may not be supported, skip if not */ + if (err < 0) + shell_print(shell," (value in mV not available)\n"); + else + percent = battery_millivolts_to_percent(val_mv); } - millivolts = read_battery_voltage(); - millivolts_to_percent(millivolts, &percent); - return bt_gatt_attr_read(conn, attr, buf, len, offset, (uint8_t*) &percent, 1); } @@ -220,10 +259,13 @@ static ssize_t battery_power_source_get(struct bt_conn *conn, uint16_t len, uint16_t offset) { uint8_t power_source; - uint8_t charging, vbus, battery_attached, fault; + uint8_t battery_attached = 0; + uint8_t fault = 0; + uint8_t vbus = 0; + uint8_t charging = 0; get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); - if (vbus) { + if (vbus || !battery_attached) { power_source = ON_CHARGER_POWER; } else { power_source = ON_BATTERY_POWER; @@ -797,9 +839,24 @@ void ble_notif_thread(void *a, void *b, void *c) ARG_UNUSED(a); ARG_UNUSED(b); ARG_UNUSED(c); - uint8_t button_last_state = 0; + uint8_t battery_last_percent = 0; + uint8_t battery_attached = 0; + uint8_t vbus = 0; + uint8_t charging = 0; + uint8_t fault = 0; + uint8_t percent = 0; + int err; + int32_t val_mv=0; + int16_t buf; + struct adc_sequence sequence = { + .buffer = &buf, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buf), + }; + + uint8_t button_last_state = 0; while (1) { if (!get_active_le_conns()) { k_sem_take(&ble_thd_sem, K_FOREVER); @@ -843,15 +900,25 @@ void ble_notif_thread(void *a, void *b, void *c) ln_buf_gen(); bt_gatt_notify(NULL, &ln_svc.attrs[2], ln_las_buf, sizeof(ln_las_buf)); } - uint8_t charging, vbus, battery_attached, fault, percent; - + get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); - uint32_t millivolts = read_battery_voltage(); - millivolts_to_percent(millivolts, &percent); - if (battery_last_percent != percent) { - bt_gatt_notify(NULL, &bas.attrs[1], &percent, sizeof(percent)); + + if (battery_attached != 0) { + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + err = adc_read(adc_channels[0].dev, &sequence); + if (err < 0) + shell_error(shell,"Could not read (%d)\n", err); + + val_mv = (int32_t)buf; + err = adc_raw_to_millivolts_dt(&adc_channels[0], + &val_mv); + + percent = battery_millivolts_to_percent(val_mv); + + if (battery_last_percent != percent) + bt_gatt_notify(NULL, &bas.attrs[1], &percent, sizeof(percent)); + battery_last_percent = percent; } - battery_last_percent = percent; } } diff --git a/samples/tmo_shell/src/tmo_shell.c b/samples/tmo_shell/src/tmo_shell.c index 6ed9014..46e9a97 100644 --- a/samples/tmo_shell/src/tmo_shell.c +++ b/samples/tmo_shell/src/tmo_shell.c @@ -25,6 +25,7 @@ LOG_MODULE_REGISTER(tmo_shell, LOG_LEVEL_INF); #include #include #include +#include #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) #include "tls_internal.h" @@ -49,14 +50,13 @@ typedef int sec_tag_t; #include "tmo_dfu_download.h" #include "tmo_file.h" #include "tmo_certs.h" -#include "tmo_adc.h" + #include "tmo_battery_ctrl.h" #include "tmo_shell.h" #include "tmo_sntp.h" #include "tmo_modem.h" #include "board.h" #include "dfu_gecko_lib.h" - #if CONFIG_TMO_SHELL_BUILD_EK #include "ek18/src/kermit_cmd.h" #endif @@ -76,9 +76,24 @@ typedef int sec_tag_t; #include "tmo_tone_player.h" +const struct device *gecko_adc_dev = NULL; const struct device *ext_flash_dev = NULL; const struct device *gecko_flash_dev = NULL; +#if !DT_NODE_EXISTS(DT_PATH(zephyr_user)) || \ + !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channels) +#error "No suitable devicetree overlay specified" +#endif + +#define DT_SPEC_AND_COMMA(node_id, prop, idx) \ + ADC_DT_SPEC_GET_BY_IDX(node_id, idx), + +/* Data of ADC io-channels specified in devicetree. */ +const struct adc_dt_spec adc_channels[] = { + DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, + DT_SPEC_AND_COMMA) +}; + #if (CONFIG_SPI_NOR - 0) || \ DT_NODE_HAS_STATUS(DT_INST(0, jedec_spi_nor), okay) #define FLASH_DEVICE DT_NODE_FULL_NAME(DT_INST(0, jedec_spi_nor)) @@ -130,6 +145,32 @@ int udp_cred_dtls(const struct shell *shell, size_t argc, char **argv); int udp_profile_dtls(const struct shell *shell, size_t argc, char **argv); #endif +int read_hwid() +{ + int32_t val_mv; + int status; + int16_t buf; + struct adc_sequence sequence = { + .buffer = &buf, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buf), + }; + + (void)adc_sequence_init_dt(&adc_channels[1], &sequence); + status = adc_read(adc_channels[1].dev, &sequence); + if (status < 0) { + shell_error(shell,"Could not read (%d)\n", status); + return status; + } + val_mv = (int32_t)buf; + status = adc_raw_to_millivolts_dt(&adc_channels[1], &val_mv); + /* conversion to mV may not be supported, skip if not */ + if (status < 0) + shell_error(shell," (value in mV not available)\n"); + + return val_mv; +} + int tmo_set_modem(enum murata_1sc_io_ctl cmd, union params_cmd* params, int sd) { int res = -1; @@ -365,7 +406,7 @@ int udp_create_dtls_core(const struct shell *shell, size_t argc, char **argv, in dev_private_key, sizeof(dev_private_key)); if (ret < 0) { - shell_error(shell, "<<< Failed to register dev private key %d >>>\n", ret); + shell_error(shell, "<<< Failed to register dev private key %d >>>\n", ret); } else { shell_info(shell, "===== Added DTLS dev private key credential! =========\n"); @@ -427,7 +468,7 @@ int tmo_dtls_cred(const struct shell *shell, char* cred_type, char* operation, i int ret = 0; struct tls_credential cred_s; size_t credlen; - union params_cmd params_cmd_u; + union params_cmd params_cmd_u; union params_cmd* u = ¶ms_cmd_u; const char *cred; enum tls_credential_type ct_val; @@ -452,12 +493,12 @@ int tmo_dtls_cred(const struct shell *shell, char* cred_type, char* operation, i return 1; } strcat(filename, ".pem"); - if (((strcmp(operation,"w") != 0) && (strcmp(operation,"d") != 0)) || sd < 0 || sd >= MAX_SOCK_REC ) { + if (((strcmp(operation,"w") != 0) && (strcmp(operation,"d") != 0)) || sd < 0 || sd >= MAX_SOCK_REC ) { shell_error(shell, "\n Invalid Inputs, operation %s sd %d ", operation, sd); } - /* Here do AT%CERTCMD to write key cert to modem NVRAM */ + /* Here do AT%CERTCMD to write key cert to modem NVRAM */ memset(u, 0, sizeof(params_cmd_u)); memset(&cred_s, 0, sizeof(struct tls_credential)); cred_s.tag = 0; @@ -476,7 +517,7 @@ int tmo_dtls_cred(const struct shell *shell, char* cred_type, char* operation, i if ((ret = tmo_set_modem(DEL_CERT, (union params_cmd*)filename, sd)) == 0) { shell_info(shell, "Deleted %s using sd: %d ", cred_type, sd); } else { - shell_error(shell, "Failed to delete %s using sd:%d error:%d", cred_type, sd, errno); + shell_error(shell, "Failed to delete %s using sd:%d error:%d", cred_type, sd, errno); } } return ret; @@ -486,10 +527,10 @@ int tmo_dtls_cred(const struct shell *shell, char* cred_type, char* operation, i int tmo_profile_dtls(const struct shell *shell, char* operation, int sd) { int ret = 0; - union params_cmd params_cmd_u; + union params_cmd params_cmd_u; union params_cmd* u = ¶ms_cmd_u; - if (((strcmp(operation,"a") != 0) && (strcmp(operation,"d") != 0)) || sd < 0 || sd >= MAX_SOCK_REC) { + if (((strcmp(operation,"a") != 0) && (strcmp(operation,"d") != 0)) || sd < 0 || sd >= MAX_SOCK_REC) { shell_error(shell, "\n Invalid Inputs, operation %s sd %d ", operation, sd); } @@ -498,14 +539,14 @@ int tmo_profile_dtls(const struct shell *shell, char* operation, int sd) u->profile.ca_file = NULL; u->profile.ca_path = NULL; u->profile.dev_cert = "dtls_cred_devcert.pem"; - u->profile.dev_key = "dtls_cred_devkey.pem"; + u->profile.dev_key = "dtls_cred_devkey.pem"; u->profile.psk_id = NULL; u->profile.psk_key = NULL; if (strcmp(operation, "a") == 0) { if (tmo_set_modem(CREATE_CERT_PROFILE, (union params_cmd*)u, sd) == 0) { create_profile_done = true; - shell_info(shell, "Created cert profile: errno %d sd %d", errno, sd); + shell_info(shell, "Created cert profile: errno %d sd %d", errno, sd); } else { shell_error(shell, "Failed to create cert profile, errno: %d sd %d", errno, sd); ret = -errno; @@ -532,7 +573,7 @@ int tmo_profile_dtls(const struct shell *shell, char* operation, int sd) } else if (strcmp(operation, "d") == 0) { if (tmo_set_modem(DELETE_CERT_PROFILE, (union params_cmd*)u, sd) == 0) { create_profile_done = false; - shell_info(shell, "Deleted cert profile: errno %d sd %d", errno, sd); + shell_info(shell, "Deleted cert profile: errno %d sd %d", errno, sd); } else { shell_error(shell, "Failed to delete cert profile: errno %d sd %d", errno, sd); ret = -errno; @@ -592,7 +633,7 @@ int sock_connect(const struct shell *shell, size_t argc, char **argv) } ret = net_ipaddr_parse(host, strlen(host), &target); if (!ret) { //try dns - /* dns stuff */ + /* dns stuff */ static struct addrinfo hints; struct zsock_addrinfo *res; @@ -639,7 +680,7 @@ int sock_connect(const struct shell *shell, size_t argc, char **argv) } zsock_inet_ntop(ai_family, src, tmp1, sizeof(tmp1)); - shell_print(shell, "DNS to conn, addr: %s\n", tmp1); + shell_print(shell, "DNS to conn, addr: %s", tmp1); } if (socks[sock_idx].flags & (BIT(sock_tls) | BIT(sock_dtls))){ ret = setsockopt(sd, SOL_TLS, TLS_HOSTNAME, @@ -868,7 +909,7 @@ int sock_sendb(const struct shell *shell, size_t argc, char **argv) stat = zsock_send(sd, mxfer_buf + total, MIN(sendsize - total, max_fragment), 0); if (stat == -1) { if (errno == EMSGSIZE) { - shell_warn(shell, "Note: EMSGSIZE (errno=%d) may be cause by a fragment being larger than network MTU.", EMSGSIZE); + shell_warn(shell, "Possible fragment larger than MTU"); } shell_error(shell, "send failed, errno = %d", errno); break; @@ -1002,9 +1043,9 @@ int sock_rcv(const struct shell *shell, size_t argc, char **argv) memset(mxfer_buf, 0, XFER_SIZE); stat = zsock_recv(sd, mxfer_buf, XFER_SIZE, ZSOCK_MSG_DONTWAIT); if (stat > 0){ - shell_print(shell, "RECEIVED:\n%s ", (char*)mxfer_buf); + shell_print(shell, "RECEIVED:\n%s", (char*)mxfer_buf); } else if (stat == -1 && errno == EWOULDBLOCK) { - shell_print(shell, "No data available!"); + shell_error(shell, "No data available!"); return stat; } while (stat == XFER_SIZE) { @@ -1055,9 +1096,9 @@ int sock_rcvfrom(const struct shell *shell, size_t argc, char **argv) #endif net_addr_ntop(ai_family, addr, addrbuf, sizeof(addrbuf)); if (stat > 0){ - shell_print(shell, "RECEIVED from %s:%d:\n%s ", addrbuf, port, (char*)mxfer_buf); - } else if (stat == -1 && errno == EWOULDBLOCK) { - shell_print(shell, "No data available!"); + shell_print(shell, "RECEIVED from %s:%d:\n%s", addrbuf, port, (char*)mxfer_buf); + } else if (stat == -1 && errno == EWOULDBLOCK) { + shell_error(shell, "No data available!"); return stat; } while (stat == XFER_SIZE) { @@ -1164,7 +1205,7 @@ int sock_recvsms(const struct shell *shell, size_t argc, char **argv) sms.timeout = K_SECONDS(wait); ret = fcntl_ptr(sock_idx, SMS_RECV, &sms); if (ret > 0) - shell_print(shell, "Received SMS from %s at %s: %s\n", sms.phone, sms.time, sms.msg); + shell_print(shell, "Received SMS from %s at %s: %s", sms.phone, sms.time, sms.msg); else shell_print(shell, "No SMS received!"); return ret; @@ -1181,7 +1222,7 @@ int cmd_list_socks(const struct shell *shell, size_t argc, char **argv) }; char *protos[5] = {"TCP", "UDP", "TLS", "DTLS", "?"}; int proto_x = 0; - shell_print(shell, "Open sockets: "); + shell_print(shell, "Open sockets:"); for (int i = 0; i < MAX_SOCK_REC; i++) { // SD: iface=%d proto= if (socks[i].flags & BIT(sock_open)) { @@ -1279,11 +1320,11 @@ int cmd_modem(const struct shell *shell, size_t argc, char **argv) strupper(cmd_buf); int res = fcntl_ptr(sd, GET_ATCMD_RESP, cmd_buf); if (res < 0) { - shell_error(shell, "request: %s failed, error: %d\n", argv[2], res); + shell_error(shell, "request: %s failed, error: %d", argv[2], res); } else if (cmd_buf[0] == 0) { - shell_error(shell, "request: %s, response: \n", argv[2]); + shell_error(shell, "request: %s, response: ", argv[2]); } else { - shell_print(shell, "request: %s, response: %s\n", argv[2], cmd_buf); + shell_print(shell, "request: %s, response: %s", argv[2], cmd_buf); } } @@ -1341,7 +1382,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(tmo_tcp_sub, SHELL_CMD(createv6, NULL, "", tcp_createv6), #endif SHELL_CMD(recv, NULL, "", tcp_rcv), - SHELL_CMD(recvb, NULL, " ", tcp_recvb), + SHELL_CMD(recvb, NULL, " ", tcp_recvb), #if CONFIG_MODEM SHELL_CMD(recvsms, NULL, " ", sock_recvsms), #endif /* CONFIG_MODEM */ @@ -1352,11 +1393,11 @@ SHELL_STATIC_SUBCMD_SET_CREATE(tmo_tcp_sub, #endif #endif SHELL_CMD(send, NULL, " ", tcp_send), - SHELL_CMD(sendb, NULL, " ", tcp_sendb), + SHELL_CMD(sendb, NULL, " ", tcp_sendb), #if CONFIG_MODEM SHELL_CMD(sendsms, NULL, " ", sock_sendsms), #endif /* CONFIG_MODEM */ - SHELL_CMD(xfersz, NULL, "[size]", sock_mxfragment), + SHELL_CMD(xfersz, NULL, "[size]", sock_mxfragment), SHELL_SUBCMD_SET_END ); @@ -1399,8 +1440,8 @@ int udp_close(const struct shell *shell, size_t argc, char **argv) SHELL_STATIC_SUBCMD_SET_CREATE(tmo_udp_sub, #if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS) - SHELL_CMD(cert, NULL, " ", udp_cred_dtls), - SHELL_CMD(ca, NULL, " ", udp_cred_dtls), + SHELL_CMD(cert, NULL, " ", udp_cred_dtls), + SHELL_CMD(ca, NULL, " ", udp_cred_dtls), #endif SHELL_CMD(close, NULL, "", udp_close), SHELL_CMD(connect, NULL, " ", udp_connect), @@ -1410,26 +1451,26 @@ SHELL_STATIC_SUBCMD_SET_CREATE(tmo_udp_sub, #endif // SHELL_CMD(bind, NULL, " ", sock_bind), #if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS) - SHELL_CMD(key, NULL, " ", udp_cred_dtls), - SHELL_CMD(profile, NULL, " ", udp_profile_dtls), + SHELL_CMD(key, NULL, " ", udp_cred_dtls), + SHELL_CMD(profile, NULL, " ", udp_profile_dtls), SHELL_CMD(secure_create, NULL, "", udp_create_dtls), #if IS_ENABLED(CONFIG_NET_IPV6) SHELL_CMD(secure_createv6, NULL, "", udp_create_dtlsv6), #endif #endif SHELL_CMD(recv, NULL, "", udp_rcv), - SHELL_CMD(recvb, NULL, " ", udp_recvb), + SHELL_CMD(recvb, NULL, " ", udp_recvb), SHELL_CMD(recvfrom, NULL, " ", sock_rcvfrom), #if CONFIG_MODEM SHELL_CMD(recvsms, NULL, " ", sock_recvsms), #endif /* CONFIG_MODEM */ SHELL_CMD(send, NULL, " ", udp_send), - SHELL_CMD(sendb, NULL, " ", udp_sendb), + SHELL_CMD(sendb, NULL, " ", udp_sendb), #if CONFIG_MODEM SHELL_CMD(sendsms, NULL, " ", sock_sendsms), #endif /* CONFIG_MODEM */ - SHELL_CMD(sendto, NULL, " ", sock_sendto), - SHELL_CMD(xfersz, NULL, "", sock_mxfragment), + SHELL_CMD(sendto, NULL, " ", sock_sendto), + SHELL_CMD(xfersz, NULL, "", sock_mxfragment), SHELL_SUBCMD_SET_END ); @@ -1598,7 +1639,7 @@ void print_set_modem_edrx_usage(const struct shell *shell) { shell_print(shell, "tmo modem edrx "); shell_print(shell, "mode: 0 - off, 1 - on, 2 - unsolicited messages enabled"); - shell_print(shell, "Act-Type: 4- LTE,LTE-M, 5 - NB-IoT"); + shell_print(shell, "Act-Type: 4 - LTE/LTE-M, 5 - NB-IoT"); shell_print(shell, "edrx values: 1 to 15"); } @@ -1621,8 +1662,8 @@ int process_cli_cmd_modem_psm(const struct shell *shell, size_t argc, char **arg int t3412 = strtol(argv[5], NULL, 10); int t3324_mul = strtol(argv[6], NULL, 10); int t3324 = strtol(argv[7], NULL, 10); - if ( (mode >= 0 && mode <=1) && (t3412_mul >= 0 && t3412_mul <= 7) && - ((t3324_mul >= 0 && t3324_mul <= 2) || t3324_mul == 7) && + if ( (mode >= 0 && mode <=1) && (t3412_mul >= 0 && t3412_mul <= 7) && + ((t3324_mul >= 0 && t3324_mul <= 2) || t3324_mul == 7) && (t3412 >=0 && t3412 <= 31) && (t3324 >=0 && t3324 <= 31) ) { u->psm.mode = mode; u->psm.t3412 = t3412 + (t3412_mul << 5); @@ -1633,11 +1674,11 @@ int process_cli_cmd_modem_psm(const struct shell *shell, size_t argc, char **arg mode, t3412_mul, t3412, t3324_mul, t3324); tmo_set_modem(AT_MODEM_PSM_SET,(union params_cmd*) u, sd); } else { - shell_print(shell, "Invalid inputs for PSM timer"); + shell_error(shell, "Invalid inputs for PSM timer"); print_set_modem_psm_usage(shell); } } else if (argc == 3) { - tmo_set_modem(AT_MODEM_PSM_GET, (union params_cmd*) u, sd); + tmo_set_modem(AT_MODEM_PSM_GET, (union params_cmd*) u, sd); shell_print(shell, "%s", (char *)u); } else { // Invalid PSM command input @@ -1648,7 +1689,7 @@ int process_cli_cmd_modem_psm(const struct shell *shell, size_t argc, char **arg int process_cli_cmd_modem_edrx(const struct shell *shell, size_t argc, char **argv, int sd) { - union params_cmd params_cmd_u; + union params_cmd params_cmd_u; union params_cmd* u = ¶ms_cmd_u; if (argc == 6) { // This is setting the edrx timer @@ -1661,7 +1702,7 @@ int process_cli_cmd_modem_edrx(const struct shell *shell, size_t argc, char **ar u->edrx.mode, u->edrx.act_type, u->edrx.time_mask); tmo_set_modem(AT_MODEM_EDRX_SET, (union params_cmd*) u, sd); } else { - shell_print(shell, "Invalid inputs for edrx timer"); + shell_error(shell, "Invalid inputs for edrx timer"); print_set_modem_edrx_usage(shell); } } else if (argc == 3) { @@ -1683,37 +1724,38 @@ int process_cli_cmd_modem_edrx_ptw(const struct shell *shell, size_t argc, char shell_print(shell, "Set eDRX PTW: %d", ptw); fcntl_ptr(sd, AT_MODEM_EDRX_PTW_SET, (const void*)&ptw); } else { - shell_print(shell, "Invalid eDRX PTW value"); - shell_print(shell, "tmo modem ptw [ptw_value]"); + shell_error(shell, "Invalid eDRX PTW value"); + shell_print(shell, "Usage: tmo modem ptw [ptw_value (0-15)]"); } } else if (argc == 3) { fcntl_ptr(sd, AT_MODEM_EDRX_PTW_GET, (const void*)&ptw); shell_print(shell, "PTW: %d", ptw); } else { - shell_print(shell, "tmo modem ptw [ptw_value]"); + shell_error(shell, "Invalid eDRX PTW value"); + shell_print(shell, "Usage: tmo modem ptw [ptw_value (0-15)]"); } return 0; } #if IS_ENABLED(CONFIG_BT_SMP) SHELL_STATIC_SUBCMD_SET_CREATE(ble_smp_9116_toggles, - SHELL_CMD(keyboard, NULL, "Toggle Keyboard.", toggle_keyboard), - SHELL_CMD(confirm, NULL, "Toggle Confirm.", toggle_confirm), - SHELL_CMD(display, NULL, "Toggle Display.", toggle_display), + SHELL_CMD(keyboard, NULL, "Toggle Keyboard", toggle_keyboard), + SHELL_CMD(confirm, NULL, "Toggle Confirm", toggle_confirm), + SHELL_CMD(display, NULL, "Toggle Display", toggle_display), SHELL_SUBCMD_SET_END ); SHELL_STATIC_SUBCMD_SET_CREATE(ble_smp_9116_respond, - SHELL_CMD(key, NULL, "Send Passkey.", send_passkey), - SHELL_CMD(confirm, NULL, "Send Confirm.", send_confirm), - SHELL_CMD(cancel, NULL, "Send cancel.", send_cancel), + SHELL_CMD(key, NULL, "Send Passkey", send_passkey), + SHELL_CMD(confirm, NULL, "Send Confirm", send_confirm), + SHELL_CMD(cancel, NULL, "Send cancel", send_cancel), SHELL_SUBCMD_SET_END ); SHELL_STATIC_SUBCMD_SET_CREATE(ble_smp_9116_sub, SHELL_CMD(enable, NULL, "Enable Security Manager Protocol (SMP)", smp_enable), SHELL_CMD(disable, NULL, "Disable Security Manager Protocol (SMP)", smp_disable), - SHELL_CMD(callbacks, NULL, "Show enabled callbacks.", show_enabled), + SHELL_CMD(callbacks, NULL, "Show enabled callbacks.", show_enabled), SHELL_CMD(toggle, &ble_smp_9116_toggles, "Toggle callbacks.", NULL), SHELL_CMD(respond, &ble_smp_9116_respond, "Send response.", NULL), SHELL_SUBCMD_SET_END @@ -1835,7 +1877,7 @@ static int cmd_get_unused_slot(const struct shell *shell, size_t argc, char **ar int slot = get_unused_slot(); if (slot >= 0) { - shell_print(shell, "Unused/inactive slot is Slot %d", slot); + shell_print(shell, "Unused/inactive slot is Slot %d", slot); } else { shell_error(shell, "Unused/inactive slot is undefined"); } @@ -1860,7 +1902,7 @@ static int cmd_erase_slot(const struct shell *shell, size_t argc, char **argv) } if (force || slot_is_safe_to_erase(slot)) { - int ret = erase_image_slot(slot); + int ret = erase_image_slot(slot); if (ret == 0) { shell_print(shell, "Slot %d was erased", slot); } @@ -2022,7 +2064,7 @@ int cmd_dfu_update(const struct shell *shell, size_t argc, char **argv) case DFU_MODEM: { - shell_print(shell,"\nStarting the FW update for Murata 1SC"); + shell_print(shell,"Starting the FW update for Murata 1SC"); int status; sprintf(dfu_modem_file.desc, "Murata 1SC Firmware Update"); sprintf(dfu_modem_file.lfile, "/tmo/%s.ua", dfu_modem_filename); @@ -2040,7 +2082,7 @@ int cmd_dfu_update(const struct shell *shell, size_t argc, char **argv) case DFU_9116W: { - shell_print(shell,"\nStarting the FW update for SiLabs RS9116W"); + shell_print(shell,"Starting the FW update for SiLabs RS9116W"); int status; status = dfu_wifi_firmware_upgrade(); if (status != 0) { @@ -2067,17 +2109,16 @@ int cmd_dfu_update(const struct shell *shell, size_t argc, char **argv) int cmd_charging_status(const struct shell *shell, size_t argc, char **argv) { - int status; uint8_t charging = 0; uint8_t vbus = 0; - uint8_t attached = 0; + uint8_t attached; uint8_t fault = 0; + uint8_t status; status = get_battery_charging_status(&charging, &vbus, &attached, &fault); if (status != 0) { shell_error(shell, "Charger status command failed"); - } - else { + } else { if (!attached) { shell_print(shell, "No battery attached"); } else if (!vbus) { @@ -2100,21 +2141,42 @@ int cmd_charging_status(const struct shell *shell, size_t argc, char **argv) int cmd_battery_voltage(const struct shell *shell, size_t argc, char **argv) { int status; - uint32_t millivolts = 0; uint8_t attached = 0; uint8_t charging = 0; - uint8_t vbus = 0; + uint8_t vbus = 0; uint8_t fault = 0; + int32_t val_mv; + + int16_t buf; + struct adc_sequence sequence = { + .buffer = &buf, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buf), + }; status = get_battery_charging_status(&charging, &vbus, &attached, &fault); if (status != 0) { shell_error(shell, "Charger status command failed"); + return status; + } + + if (!attached) { + shell_print(shell, "No battery attached"); } else { - if (!attached) { - shell_print(shell, "No battery attached"); + adc_sequence_init_dt(&adc_channels[0], &sequence); + status = adc_read(adc_channels[0].dev, &sequence); + if (status < 0) { + shell_error(shell,"Could not read (%d)\n", status); + return status; + } + + val_mv = (int32_t)buf; + status = adc_raw_to_millivolts_dt(&adc_channels[0], &val_mv); + /* conversion to mV may not be supported, skip if not */ + if (status < 0) { + shell_error(shell,"Value in mV not available"); } else { - millivolts = read_battery_voltage(); - shell_print(shell, "Battery voltage %d.%03dV", millivolts/1000, millivolts%1000); + shell_print(shell,"Battery voltage %d.%03dV\n", val_mv/1000, val_mv%1000); } } return status; @@ -2123,18 +2185,20 @@ int cmd_battery_voltage(const struct shell *shell, size_t argc, char **argv) extern uint8_t aio_btn_pushed; int cmd_battery_discharge(const struct shell *shell, size_t argc, char **argv) { + int status; uint8_t set_point = 60; uint8_t percent = 100; uint8_t old_percent = 0; - uint32_t millivolts = 0; uint8_t attached = 0; uint8_t charging = 0; - uint8_t vbus = 0; - uint8_t fault= 0; + uint8_t vbus = 0; + uint8_t fault; + int16_t buf; + int32_t val_mv; if (argc > 2) { shell_error(shell, "Incorrect parameters"); - shell_print(shell, "usage: tmo battery discharge [set point (optional, default: 60)]"); + shell_print(shell, "Usage: tmo battery discharge [set point (optional, default: 60)]"); return -1; } if (argc == 2) { int val = strtol(argv[1], NULL, 10); @@ -2147,17 +2211,40 @@ int cmd_battery_discharge(const struct shell *shell, size_t argc, char **argv) } } shell_print(shell, "Discharge setpoint: %d", set_point); + + struct adc_sequence sequence = { + .buffer = &buf, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buf), + }; - get_battery_charging_status(&charging, &vbus, &attached, &fault); - if (attached != 0) { - millivolts = read_battery_voltage(); - millivolts_to_percent(millivolts, &percent); - old_percent = percent; - } else { + status = get_battery_charging_status(&charging, &vbus, &attached, &fault); + if (status != 0) { + shell_error(shell, "Charger status command failed"); + return status; + } + + if (!attached) { shell_error(shell, "Battery not attached, aborting..."); return -ENOEXEC; } + adc_sequence_init_dt(&adc_channels[0], &sequence); + status = adc_read(adc_channels[0].dev, &sequence); + if (status < 0) { + shell_error(shell,"Could not read (%d)\n", status); + return status; + } + val_mv = (int32_t)buf; + status = adc_raw_to_millivolts_dt(&adc_channels[0], &val_mv); + /* conversion to mV may not be supported, skip if not */ + if (status < 0) { + shell_error(shell,"Value in mV not available"); + return status; + } + + old_percent = percent; + shell_print(shell, "Battery level is currently %d%%", percent); if (charging) { @@ -2179,15 +2266,31 @@ int cmd_battery_discharge(const struct shell *shell, size_t argc, char **argv) } while (percent > set_point) { - get_battery_charging_status(&charging, &vbus, &attached, &fault); - if (attached != 0) { - millivolts = read_battery_voltage(); - millivolts_to_percent(millivolts, &percent); + status = get_battery_charging_status(&charging, &vbus, &attached, &fault); + if (status != 0) { + shell_error(shell, "Charger status command failed"); + return status; + } + if (attached != 0) { + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + status = adc_read(adc_channels[0].dev, &sequence); + if (status < 0) { + shell_error(shell,"Could not read (%d)\n", status); + return status; + } + val_mv = (int32_t)buf; + status = adc_raw_to_millivolts_dt(&adc_channels[0], &val_mv); + if (status < 0) { + shell_error(shell,"Value in mV not available"); + return status; + } + percent = battery_millivolts_to_percent(val_mv); } else { shell_error(shell, "Battery not attached, aborting..."); return -ENOEXEC; } - if ((abs(old_percent - percent) > 5) && ((percent % 5) == 0)) { + + if ((abs(old_percent - percent) > 5) && (( percent % 5) == 0)) { shell_print(shell, "Battery level is now (%d%%)...", percent); old_percent = percent; } @@ -2205,7 +2308,6 @@ int cmd_battery_discharge(const struct shell *shell, size_t argc, char **argv) shell_print(shell, "Battery is discharged (%d%%), shutting down...", percent); cmd_pmsysfulloff(shell, 0, NULL); - return 0; } @@ -2213,23 +2315,43 @@ int cmd_battery_percentage(const struct shell *shell, size_t argc, char **argv) { int status; uint8_t percent = 0; - uint32_t millivolts = 0; uint8_t attached = 0; uint8_t charging = 0; - uint8_t vbus = 0; + uint8_t vbus = 0; uint8_t fault= 0; + int32_t val_mv; + int16_t buf; + struct adc_sequence sequence = { + .buffer = &buf, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buf), + }; status = get_battery_charging_status(&charging, &vbus, &attached, &fault); if (status != 0) { shell_error(shell, "Charger status command failed"); + return status; + } + if (!attached) { + shell_print(shell, "No battery attached"); + return -ENOEXEC;; + } + + adc_sequence_init_dt(&adc_channels[0], &sequence); + status = adc_read(adc_channels[0].dev, &sequence); + if (status < 0) { + shell_error(shell,"Could not read (%d)\n", status); + return status; + } + + val_mv = (int32_t)buf; + + status = adc_raw_to_millivolts_dt(&adc_channels[0], &val_mv); + if (status < 0) { + shell_error(shell," (value in mV not available)"); } else { - if (!attached) { - shell_print(shell, "No battery attached"); - } else { - millivolts = read_battery_voltage(); - millivolts_to_percent(millivolts, &percent); - shell_print(shell, "Battery level %d percent", percent); - } + percent = battery_millivolts_to_percent(val_mv); + shell_print(shell, "\tBattery level %d percent", percent); } return status; } @@ -2239,7 +2361,7 @@ int udp_cred_dtls(const struct shell *shell, size_t argc, char **argv) if (argc < 3) { shell_error(shell, "Missing required arguments"); shell_print(shell, "Usage: tmo udp %s >\n" - " operation:w to write, d to delete dtls credential from modem NVRAM\n" + " operation: w to write, d to delete dtls credential from modem NVRAM\n" " socket: socket descriptor", argv[0]); return -EINVAL; } @@ -2251,8 +2373,8 @@ int udp_profile_dtls(const struct shell *shell, size_t argc, char **argv) if (argc < 3) { shell_error(shell, "Missing required arguments"); shell_print(shell, "Usage: tmo udp profile >\n" - " operation:a to add, d to delete cert profile from modem NVRAM\n" - " socket: socket descriptor from - tmo udp secure_create 1 "); + " operation: a to add, d to delete cert profile from modem NVRAM\n" + " socket: socket descriptor from - tmo udp secure_create 1"); return -EINVAL; } return tmo_profile_dtls(shell, argv[1], (int) strtol(argv[2], NULL, 10)); @@ -2371,7 +2493,7 @@ int cmd_json_set_iface(const struct shell *shell, size_t argc, char **argv) return -EINVAL; } int idx = strtol(argv[1], NULL, 10); - return set_json_iface_type(idx); + return set_json_iface_type(idx); } int cmd_json_transmit_enable(const struct shell *shell, size_t argc, char **argv) @@ -2559,7 +2681,7 @@ int cmd_tmo_buzzer_vol(const struct shell *shell, int argc, char**argv) { if (argc < 2) { shell_error(shell, "Missing required arguments"); - shell_print(shell, " tmo buzzer vol "); + shell_print(shell, "Usage: tmo buzzer vol "); return 1; } int vol = strtol(argv[1], NULL, 10); @@ -2583,7 +2705,7 @@ int cmd_tmo_cert_modem_load(const struct shell* shell, int argc, char **argv) { bool force = false; if (argc > 2) { - shell_print(shell, " usage: tmo certs modem_load [force]"); + shell_print(shell, "Usage: tmo certs modem_load [force]"); } if (argc == 2) { force = strtol(argv[1], NULL, 10); @@ -2742,15 +2864,34 @@ static void count_ifaces(struct net_if *iface, void *user_data) num_ifaces++; } +void adc_gecko_setup() { + int status; + + /* Configure channels individually prior to sampling. */ + for (size_t i = 0U; i < ARRAY_SIZE(adc_channels); i++) { + if (!device_is_ready(adc_channels[i].dev)) { + printf("ADC controller device not ready\n"); + return; + } + + status = adc_channel_setup_dt(&adc_channels[i]); + if (status < 0) { + printf("Could not setup channel #%d (%d)\n", i, status); + return; + } + } +} + void tmo_shell_main(void) { shell = shell_backend_uart_get_ptr(); net_if_foreach(count_ifaces, NULL); ext_flash_dev = device_get_binding(FLASH_DEVICE); + gecko_adc_dev = DEVICE_DT_GET(DT_INST(0, silabs_adc_gecko)); if (!ext_flash_dev) { - shell_print(shell, "External flash driver %s was not found!", FLASH_DEVICE); + shell_error(shell, "External flash driver %s was not found!", FLASH_DEVICE); exit(-1); } else { @@ -2759,7 +2900,7 @@ void tmo_shell_main(void) gecko_flash_dev = device_get_binding(GECKO_FLASH_DEVICE); if (!gecko_flash_dev) { - shell_print(shell, "Gecko flash driver not found"); + shell_error(shell, "Gecko flash driver not found"); } else { shell_print(shell, "Gecko flash driver %s ready!", GECKO_FLASH_DEVICE); @@ -2769,7 +2910,10 @@ void tmo_shell_main(void) mountfs(); cxd5605_init(); - initADC(); + + adc_gecko_setup(); + + shell = shell_backend_uart_get_ptr(); #ifdef CONFIG_WIFI tmo_wifi_connect(); #endif diff --git a/samples/tmo_shell/src/tmo_web_demo.c b/samples/tmo_shell/src/tmo_web_demo.c index 7909460..be5282f 100644 --- a/samples/tmo_shell/src/tmo_web_demo.c +++ b/samples/tmo_shell/src/tmo_web_demo.c @@ -12,12 +12,11 @@ #include #include #include +#include #if CONFIG_MODEM -#include "modem_sms.h" #include #endif -#include "tmo_adc.h" #include "tmo_gnss.h" #include "tmo_ble_demo.h" #include "tmo_web_demo.h" @@ -25,6 +24,8 @@ #include "tmo_shell.h" #include "tmo_battery_ctrl.h" +extern const struct adc_dt_spec adc_channels[]; + static struct web_demo_settings_t web_demo_settings = {false, 0, 2, TRANSMIT_INTERVAL_SECS_WEB}; #define MAX_BASE_URL_SIZE 100 #define MAX_PATH_SIZE 100 @@ -40,6 +41,8 @@ static const char *battery_state_string[] = { "charging", "not-charging", "not-attached", "attached", }; +extern const struct device *battery_dev; + bool get_transmit_flag() { return web_demo_settings.transmit_flag; @@ -169,6 +172,18 @@ int create_json() int buffer_size = MAX_PAYLOAD_BUFFER_SIZE; memset(json_payload, 0, MAX_PAYLOAD_BUFFER_SIZE); + uint8_t vbus; + uint8_t charging; + uint8_t percent = 0; + + int16_t buf; + int32_t val_mv = 0; + struct adc_sequence sequence = { + .buffer = &buf, + /* buffer size in bytes, not number of samples */ + .buffer_size = sizeof(buf), + }; + // test blank payload // json_payload[0] = '\0'; // return strlen(json_payload); @@ -197,13 +212,29 @@ int create_json() total_bytes_written += ret_val; if (ret_val >= 0 && total_bytes_written < buffer_size) { - uint8_t percent = 0; - uint32_t millivolts = 0; enum battery_state e_bat_state = battery_state_not_attached; + + get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); + if (battery_attached !=0) { - millivolts = read_battery_voltage(); - millivolts_to_percent(millivolts, &percent); - if (is_battery_charging()) { + (void)adc_sequence_init_dt(&adc_channels[0], &sequence); + ret_val = adc_read(adc_channels[0].dev, &sequence); + if (ret_val < 0) { + shell_error(shell,"Could not read (%d)\n", ret_val); + return ret_val; + } + + val_mv = (int32_t)buf; + ret_val = adc_raw_to_millivolts_dt(&adc_channels[0], + &val_mv); + /* conversion to mV may not be supported, skip if not */ + if (ret_val < 0) { + shell_print(shell," (value in mV not available)\n"); + return ret_val; + } + + percent = battery_millivolts_to_percent(val_mv); + if (vbus && charging) { e_bat_state = battery_state_charging; } else { e_bat_state = battery_state_not_charging; @@ -211,9 +242,9 @@ int create_json() } else { e_bat_state = battery_state_not_attached; } - ret_val = snprintf(json_payload+total_bytes_written, num_bytes_avail_buffer, + ret_val =snprintf(json_payload+total_bytes_written, num_bytes_avail_buffer, "\"battery\":{\n\"voltage\":%d.%03d,\n\"percent\":%d,\n\"state\":\"%s\"\n},\n", - millivolts/1000, millivolts%1000, percent, + val_mv/1000, val_mv%1000, percent, battery_state_string[e_bat_state]); } else { return ret_val; @@ -312,11 +343,11 @@ static void tmo_web_demo_notif_thread(void *a, void *b, void *c) ARG_UNUSED(b); ARG_UNUSED(c); k_sleep(K_SECONDS(TRANSMIT_INTERVAL_SECS_WEB)); + uint8_t vbus; + uint8_t charging; while (1) { k_sleep(K_SECONDS(web_demo_settings.transmit_interval)); - uint8_t charging = 0; - uint8_t vbus = 0; if (get_transmit_flag()) { get_battery_charging_status(&charging, &vbus, &battery_attached, &fault); create_json();