diff --git a/Kconfig b/Kconfig index 9255721..82ea4a2 100644 --- a/Kconfig +++ b/Kconfig @@ -3,8 +3,8 @@ source "Kconfig.zephyr" config PW_SIGN_KEY string "Signing key" -config PW_ROTATE_PERIOD_SEC - int "How ofen message will be rotated" +config PW_UPDATE_PERIOD_SEC + int "How ofen update adv message data" default 15 config ADV_BTN_TIMES @@ -15,14 +15,22 @@ config ADV_USE_PUB_ADDRESS bool "Advertise using identity address." default y -config ADV_INTERVAL_MIN_MS - int "Minimum periodic advertising interval" - default 100 +config ADV_INTERVAL_MIN + int "Minimum periodic advertising interval in 0.625 ms units" + default 160 -config ADV_INTERVAL_MAX_MS - int "Maximum periodic advertising interval" - default 150 +config ADV_INTERVAL_MAX + int "Maximum periodic advertising interval in 0.625 ms units" + default 240 -config PW_BATT_VOLTAGE +config PW_BATT + bool "Have battery support" + default y + +config PW_BATT_MIN_VOLTAGE + int "Max battery mVoltage" + default 1700 + +config PW_BATT_MAX_VOLTAGE int "Max battery mVoltage" default 3000 diff --git a/boards/holyiot_21014.overlay b/boards/holyiot_21014.overlay index 815a26c..afe2ea8 100644 --- a/boards/holyiot_21014.overlay +++ b/boards/holyiot_21014.overlay @@ -23,4 +23,4 @@ zephyr,resolution = <14>; zephyr,oversampling = <8>; }; -}; \ No newline at end of file +}; diff --git a/prj.conf b/prj.conf index f58327d..7dd511d 100644 --- a/prj.conf +++ b/prj.conf @@ -1,20 +1,18 @@ CONFIG_BT_DEVICE_NAME="PinkyWinky" CONFIG_PW_SIGN_KEY="lol-kek-cheburek" -CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_MAIN_STACK_SIZE=4096 CONFIG_LOG=n CONFIG_SERIAL=n CONFIG_CONSOLE=n +CONFIG_UART_CONSOLE=n +CONFIG_STDOUT_CONSOLE=n +CONFIG_PRINTK=n +CONFIG_EARLY_CONSOLE=n CONFIG_ADC=y -CONFIG_BT=y -CONFIG_BT_EXT_ADV=y -CONFIG_BT_PER_ADV=n -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=64 - CONFIG_MBEDTLS=y CONFIG_MBEDTLS_BUILTIN=y CONFIG_MBEDTLS_SHA1=y @@ -28,4 +26,22 @@ CONFIG_I2C=n CONFIG_SPI=n CONFIG_BOOT_BANNER=n -CONFIG_BOOT_DELAY=0 + +CONFIG_BT=y +CONFIG_BT_EXT_ADV=y +CONFIG_BT_PER_ADV=n +CONFIG_BT_PERIPHERAL=y +CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=128 + +# Disable Bluetooth features not needed +CONFIG_BT_DEBUG_NONE=y +CONFIG_BT_SMP=n +CONFIG_BT_DIS=n +CONFIG_BT_BAS=n +CONFIG_BT_HRS=n +CONFIG_BT_PHY_UPDATE=n +CONFIG_BT_GATT_CACHING=n +CONFIG_BT_GATT_SERVICE_CHANGED=n + +# Disable Bluetooth controller features not needed +CONFIG_BT_CTLR_PHY_2M=n diff --git a/pw-linux.code-workspace b/pw-linux.code-workspace index 998dfea..5cf2522 100644 --- a/pw-linux.code-workspace +++ b/pw-linux.code-workspace @@ -9,27 +9,28 @@ "C_Cpp.default.compilerPath": "/usr/bin/arm-none-eabi-gcc", "C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json", "files.associations": { - "printk.h": "c", - "gatt.h": "c", - "*.c_": "c", - "gpio.h": "c", - "typeinfo": "c", - "chrono": "c", - "text_encoding": "c", - "optional": "c", - "ratio": "c", - "system_error": "c", - "array": "c", - "functional": "c", - "tuple": "c", - "type_traits": "c", - "utility": "c", - "variant": "c", - "charconv": "c", - "format": "c", - "cstdint": "c", - "stdbool.h": "c" - }, + "printk.h": "c", + "gatt.h": "c", + "*.c_": "c", + "gpio.h": "c", + "typeinfo": "c", + "chrono": "c", + "text_encoding": "c", + "optional": "c", + "ratio": "c", + "system_error": "c", + "array": "c", + "functional": "c", + "tuple": "c", + "type_traits": "c", + "utility": "c", + "variant": "c", + "charconv": "c", + "format": "c", + "cstdint": "c", + "stdbool.h": "c", + "compare": "c" + }, "zephyr-ide.projects": {}, "python.defaultInterpreterPath": "~/zephyr/.venv/bin/python", "python.terminal.activateEnvironment": true, diff --git a/src/batt.c b/src/batt.c index 264a793..89747a1 100644 --- a/src/batt.c +++ b/src/batt.c @@ -6,17 +6,12 @@ LOG_MODULE_REGISTER(pw_batt); -#if DT_NODE_EXISTS(DT_PATH(zephyr_user)) && \ +#if CONFIG_PW_BATT && DT_NODE_EXISTS(DT_PATH(zephyr_user)) && \ DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channels) && \ DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channel_names) static const struct adc_dt_spec adc_channel = ADC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), batt); -static uint16_t pw_adc_buf; -static struct adc_sequence pw_adc_sequence = { - .buffer = &pw_adc_buf, - /* buffer size in bytes, not number of samples */ - .buffer_size = sizeof(pw_adc_buf), -}; +static uint8_t pw_batt_cur_percent = 0; int pw_batt_enable() { @@ -30,48 +25,68 @@ int pw_batt_enable() err = adc_channel_setup_dt(&adc_channel); if (err < 0) { LOG_ERR("could not setup ADC channel (err %d)", err); - return -1; - } - - err = adc_sequence_init_dt(&adc_channel, &pw_adc_sequence); - if (err) { - LOG_ERR("could not initialize dt (err %d)", err); - return -1; + return -2; } LOG_INF("Battery initialized at %s", adc_channel.dev->name); - return 0; + return pw_batt_refresh_data(); } uint8_t pw_batt_percent() +{ + return pw_batt_cur_percent; +} + +int pw_batt_refresh_data() { int err; + uint16_t sample_buff = 0; + struct adc_sequence sequence = { + .buffer = &sample_buff, + .buffer_size = sizeof(sample_buff), + }; + + err = adc_sequence_init_dt(&adc_channel, &sequence); + if (err) { + LOG_ERR("could not initialize dt (err %d)", err); + return -1; + } - err = adc_read_dt(&adc_channel, &pw_adc_sequence); + err = adc_read_dt(&adc_channel, &sequence); if (err) { LOG_ERR("could not read dt (err %d)", err); - return 1; + return -2; } - int32_t val_mv = (int32_t)pw_adc_buf; + int32_t val_mv = (int32_t)sample_buff; err = adc_raw_to_millivolts_dt(&adc_channel, &val_mv); if (err) { LOG_ERR("could not convert dt voltage (err %d)", err); - return 2; + return -3; + } + + if (val_mv <= CONFIG_PW_BATT_MIN_VOLTAGE) { + pw_batt_cur_percent = 0; + return 0; } - uint8_t percent = val_mv * 100 / CONFIG_PW_BATT_VOLTAGE; - if (percent > 100) { - return 100; + if (val_mv >= CONFIG_PW_BATT_MAX_VOLTAGE) { + pw_batt_cur_percent = 100; + return 0; } - return percent; + val_mv -= CONFIG_PW_BATT_MIN_VOLTAGE; + val_mv *= 100; + val_mv /= CONFIG_PW_BATT_MAX_VOLTAGE - CONFIG_PW_BATT_MIN_VOLTAGE; + pw_batt_cur_percent = (uint8_t) val_mv; + return 0; } #else int pw_batt_enable() { + LOG_WRN("Battery is disabled or not available"); return 0; } @@ -80,4 +95,9 @@ uint8_t pw_batt_percent() return 0; } -#endif \ No newline at end of file +int pw_batt_refresh_data() +{ + return 0; +} + +#endif diff --git a/src/batt.h b/src/batt.h index d5a431d..5e5c2bf 100644 --- a/src/batt.h +++ b/src/batt.h @@ -5,6 +5,7 @@ int pw_batt_enable(); +int pw_batt_refresh_data(); uint8_t pw_batt_percent(); #endif diff --git a/src/ble.c b/src/ble.c index 78a0956..0e8f93e 100644 --- a/src/ble.c +++ b/src/ble.c @@ -125,14 +125,6 @@ int pw_ble_init_adv_data() return 0; } -void pw_ble_adv_timer_cb(struct k_timer *dummy) -{ - LOG_INF("rotate data"); - pw_ble_refresh_data(); -} - -K_TIMER_DEFINE(pw_ble_adv_timer, pw_ble_adv_timer_cb, NULL); - int pw_ble_enable() { int err; @@ -149,7 +141,7 @@ int pw_ble_enable() err = bt_le_ext_adv_create( BT_LE_ADV_PARAM( BT_LE_ADV_OPT_EXT_ADV | PW_BLE_EXT_ADV_OPTS, - CONFIG_ADV_INTERVAL_MIN_MS, CONFIG_ADV_INTERVAL_MAX_MS, + CONFIG_ADV_INTERVAL_MIN, CONFIG_ADV_INTERVAL_MAX, NULL ), NULL, @@ -172,9 +164,6 @@ int pw_ble_enable() LOG_ERR("failed to start extended advertising (err %d)", err); return -1; } - - LOG_INF("start adv msg rotation..."); - k_timer_start(&pw_ble_adv_timer, K_SECONDS(CONFIG_PW_ROTATE_PERIOD_SEC), K_SECONDS(CONFIG_PW_ROTATE_PERIOD_SEC)); LOG_INF("BLE initialized"); return 0; diff --git a/src/ble.h b/src/ble.h index 2f1678f..09be5f9 100644 --- a/src/ble.h +++ b/src/ble.h @@ -1,6 +1,8 @@ #ifndef H_PW_BLE_ #define H_PW_BLE_ +#define BLE_ADV_INT_UNIT 0.625 + int pw_ble_enable(); int pw_ble_refresh_data(); diff --git a/src/btn.c b/src/btn.c index f921f54..432a33e 100644 --- a/src/btn.c +++ b/src/btn.c @@ -10,7 +10,8 @@ LOG_MODULE_REGISTER(pw_btn); -#define BUTTON_DEBOUNCE_DELAY_MS 100 +#define BUTTON_DEBOUNCE_DELAY_MS (100) +#define BUTTON_PRESS_TIMEOUT (CONFIG_ADV_BTN_TIMES * CONFIG_ADV_INTERVAL_MAX * BLE_ADV_INT_UNIT) #define SW_NODE DT_ALIAS(button_sw) #if !DT_NODE_HAS_STATUS(SW_NODE, okay) @@ -79,5 +80,5 @@ void pw_btn_cb(const struct device *dev, struct gpio_callback *cb, uint32_t pins pw_btn_pressed = true; pw_led_on(); pw_ble_refresh_data(); - k_timer_start(&pw_btn_release_timer, K_MSEC(CONFIG_ADV_BTN_TIMES * CONFIG_ADV_INTERVAL_MAX_MS), K_NO_WAIT); + k_timer_start(&pw_btn_release_timer, K_MSEC(BUTTON_PRESS_TIMEOUT), K_NO_WAIT); } diff --git a/src/main.c b/src/main.c index c25fe65..f33bc54 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include "led.h" @@ -42,5 +43,19 @@ int main(void) } LOG_INF("PinkyWinky was started"); + for (;;) { + k_sleep(K_SECONDS(CONFIG_PW_UPDATE_PERIOD_SEC)); + + err = pw_batt_refresh_data(); + if (err) { + LOG_ERR("batt refresh failed (err %d)", err); + } + + err = pw_ble_refresh_data(); + if (err) { + LOG_ERR("ble refresh failed (err %d)", err); + } + } + return 0; }