10#include "uart_async_adapter.h"
11#include <zephyr/drivers/uart.h>
14#include <zephyr/kernel.h>
15#include <zephyr/types.h>
16#include <zephyr/usb/usb_device.h>
19#include <zephyr/device.h>
20#include <zephyr/devicetree.h>
22#include <zephyr/bluetooth/bluetooth.h>
23#include <zephyr/bluetooth/gatt.h>
24#include <zephyr/bluetooth/hci.h>
25#include <zephyr/bluetooth/uuid.h>
27#include <bluetooth/services/nus.h>
29#include <dk_buttons_and_leds.h>
31#include <zephyr/settings/settings.h>
34#include <zephyr/drivers/gpio.h>
39#include <zephyr/logging/log.h>
42#include "ncs_version.h"
46#include <zephyr/drivers/i2c.h>
50#include <zephyr/pm/device.h>
58#include <zephyr/drivers/gpio.h>
60#define STACKSIZE CONFIG_BT_NUS_THREAD_STACK_SIZE
63#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
64#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
66#define RUN_STATUS_LED DK_LED1
67#define RUN_LED_BLINK_INTERVAL 1000
69#define CON_STATUS_LED DK_LED2
71#define KEY_PASSKEY_ACCEPT DK_BTN1_MSK
72#define KEY_PASSKEY_REJECT DK_BTN2_MSK
74#define UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE
75#define UART_WAIT_FOR_BUF_DELAY K_MSEC(50)
76#define UART_WAIT_FOR_RX CONFIG_BT_NUS_UART_RX_WAIT_TIME
80#if DT_NODE_HAS_STATUS(DT_ALIAS(i2c_0), okay)
81#define I2C_DEV_NODE DT_ALIAS(i2c_0)
82#elif DT_NODE_HAS_STATUS(DT_ALIAS(i2c_1), okay)
83#define I2C_DEV_NODE DT_ALIAS(i2c_1)
84#elif DT_NODE_HAS_STATUS(DT_ALIAS(i2c_2), okay)
85#define I2C_DEV_NODE DT_ALIAS(i2c_2)
87#error "Please set the correct I2C device"
93#if DT_NODE_HAS_STATUS(DT_ALIAS(uart0), okay)
94#define UART0_DEV_NODE DT_ALIAS(uart0)
97#warning "UART0 not available"
99const struct device *
const uart0_dev = DEVICE_DT_GET(UART0_DEV_NODE);
106const struct device *
const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE);
126#ifdef SIMULATED_INTERRUPT
127struct k_timer simulated_interrupt_timer;
140void simulated_interrupt_handler(
struct k_work *work) {
142 static int count = 0;
144 sprintf(str,
"count: %d", count++);
146 if (bt_nus_send(NULL, str, strlen(str))) {
147 myPrintkE(
"Failed to send data over BLE connection\r\n");
151K_WORK_DEFINE(simulated_interrupt_work, simulated_interrupt_handler);
164void simulated_interrupt_timer_handler(
struct k_timer *dummy) { k_work_submit(&simulated_interrupt_work); }
171#define BATEN_NODE DT_ALIAS(baten)
172#if !DT_NODE_HAS_STATUS(BATEN_NODE, okay)
173#error "BATEN alias is not defined"
180#define HDC2080RDY_NODE \
182#if !DT_NODE_HAS_STATUS(HDC2080RDY_NODE, okay)
183#error "HDC2080 alias is not defined"
191#define SW2_NODE DT_ALIAS(sw2input)
192#if !DT_NODE_HAS_STATUS(SW2_NODE, okay)
193#error "SW2 alias is not defined"
195static const struct gpio_dt_spec
sw2 =
196 GPIO_DT_SPEC_GET_OR(
SW2_NODE, gpios, {0});
203#include <zephyr/drivers/spi.h>
209#include <nrfx_saadc.h>
210#include <zephyr/drivers/adc.h>
211#define ADC_BUFFER_SIZE 1
213#if DT_NODE_HAS_STATUS(DT_ALIAS(adc), okay)
214#define ADC_DEV_NODE DT_ALIAS(adc)
216#error "Please set the correct ADC device"
220#define ADC_RESOLUTION 10
224#define ADC_REFERENCE \
226#define ADC_ACQUISITION_TIME \
227 ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10)
229#define ADC_1ST_CHANNEL_ID 0
230#define ADC_1ST_CHANNEL_INPUT \
233#define ADC_2ND_CHANNEL_ID 1
234#define ADC_2ND_CHANNEL_INPUT \
237#define ADC_3RD_CHANNEL_ID 2
238#define ADC_3RD_CHANNEL_INPUT \
241#define ADC_4TH_CHANNEL_ID 3
242#define ADC_4TH_CHANNEL_INPUT \
245#define ADC_5TH_CHANNEL_ID 4
246#define ADC_5TH_CHANNEL_INPUT \
249#define ADC_6ST_CHANNEL_ID 5
250#define ADC_6ST_CHANNEL_INPUT \
254const struct device *
const adc_dev = DEVICE_DT_GET(ADC_DEV_NODE);
257static const struct adc_channel_cfg m_4th_channel_cfg =
263#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
270static const struct adc_channel_cfg m_3rd_channel_cfg =
276#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
283static const struct adc_channel_cfg m_2nd_channel_cfg =
289#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
306#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
314#define ADC_REF_MV 3600U
315#define ADC_DIV_MV 1000U
316#define ADC_MAX_VAL ((1U << ADC_RESOLUTION) - 1U)
345 const struct adc_sequence sequence = {
390static const struct device *uart =
391 DEVICE_DT_GET(DT_CHOSEN(nordic_nus_uart));
393static struct k_work_delayable
416static struct bt_data
ad[2] = {
417 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
421static const struct bt_data
sd[] = {
422 BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
426#if CONFIG_BT_NUS_UART_ASYNC_ADAPTER
427UART_ASYNC_ADAPTER_INST_DEFINE(async_adapter);
429static const struct device *
const async_adapter;
457static void uart_cb(
const struct device *dev,
struct uart_event *evt,
void *user_data) {
460 static size_t aborted_len;
462 static uint8_t *aborted_buf;
463 static bool disable_req;
468 if ((evt->data.tx.len == 0) || (!evt->data.tx.buf)) {
482 buf = k_fifo_get(&fifo_uart_tx_data, K_NO_WAIT);
487 if (uart_tx(uart, buf->
data, buf->
len, SYS_FOREVER_MS)) {
488 myPrintkW(
"Failed to send data over UART\r\n");
496 buf->
len += evt->data.rx.len;
502 if ((evt->data.rx.buf[buf->
len - 1] ==
'\n') || (evt->data.rx.buf[buf->
len - 1] ==
'\r')) {
504 uart_rx_disable(uart);
509 case UART_RX_DISABLED:
513 buf = k_malloc(
sizeof(*buf));
517 myPrintkW(
"Not able to allocate UART receive buffer\r\n");
534 case UART_RX_BUF_REQUEST:
536 buf = k_malloc(
sizeof(*buf));
539 uart_rx_buf_rsp(uart, buf->
data,
sizeof(buf->
data));
541 myPrintkW(
"Not able to allocate UART receive buffer\r\n");
546 case UART_RX_BUF_RELEASED:
551 k_fifo_put(&fifo_uart_rx_data, buf);
558 case UART_TX_ABORTED:
561 aborted_buf = (uint8_t *)evt->data.tx.buf;
564 aborted_len += evt->data.tx.len;
567 uart_tx(uart, &buf->
data[aborted_len], buf->
len - aborted_len, SYS_FOREVER_MS);
606static void uart_work_handler(
struct k_work *item) {
609 buf = k_malloc(
sizeof(*buf));
613 myPrintkW(
"Not able to allocate UART receive buffer\r\n");
629static bool uart_test_async_api(
const struct device *dev) {
630 const struct uart_driver_api *api = (
const struct uart_driver_api *)dev->api;
632 return (api->callback_set != NULL);
649 if (!device_is_ready(uart)) {
653 if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {
654 err = usb_enable(NULL);
655 if (err && (err != -EALREADY)) {
661 rx = k_malloc(
sizeof(*
rx));
668 k_work_init_delayable(&uart_work, uart_work_handler);
670 if (IS_ENABLED(CONFIG_BT_NUS_UART_ASYNC_ADAPTER) && !uart_test_async_api(uart)) {
672 uart_async_adapter_init(async_adapter, uart);
673 uart = async_adapter;
676 err = uart_callback_set(uart, uart_cb, NULL);
679 myPrintkE(
"Cannot initialize UART callback\r\n");
683 if (IS_ENABLED(CONFIG_UART_LINE_CTRL)) {
688 uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &dtr);
693 k_sleep(K_MSEC(100));
696 err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DCD, 1);
698 myPrintkW(
"Failed to set DCD, ret code %d\r\n", err);
700 err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DSR, 1);
702 myPrintkW(
"Failed to set DSR, ret code %d\r\n", err);
706 tx = k_malloc(
sizeof(*
tx));
709 pos = snprintf(
tx->data,
sizeof(
tx->data),
"Starting BLE Prototype (nRF52832)\r\n");
711 if ((pos < 0) || (pos >=
sizeof(
tx->data))) {
714 myPrintkE(
"snprintf returned %d\r\n", pos);
724 err = uart_tx(uart,
tx->data,
tx->len, SYS_FOREVER_MS);
728 myPrintkE(
"Cannot display welcome message (err: %d)\r\n", err);
732 err = uart_rx_enable(uart,
rx->data,
sizeof(
rx->data), 50);
734 myPrintkE(
"Cannot enable uart reception (err: %d)\r\n", err);
749 int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2,
ad, ARRAY_SIZE(
ad),
sd, ARRAY_SIZE(
sd));
752 myPrintkE(
"Advertising failed to start (err %d)\r\n", err);
756 myPrintkI(
"Advertising successfully started\r\n");
776static void connected(
struct bt_conn *conn, uint8_t err) {
777 char addr[BT_ADDR_LE_STR_LEN];
780 myPrintkE(
"Connection failed (err %u)\r\n", err);
784 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
802 char addr[BT_ADDR_LE_STR_LEN];
804 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
806 myPrintkI(
"Disconnected: %s (reason %u)\r\n", addr, reason);
830 myPrintkI(
"Connection object available from previous conn. Disconnect is complete!\r\n");
834#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
845static void security_changed(
struct bt_conn *conn, bt_security_t level,
enum bt_security_err err) {
846 char addr[BT_ADDR_LE_STR_LEN];
848 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
851 myPrintkI(
"Security changed: %s level %u\r\n", addr, level);
853 myPrintkW(
"Security failed: %s level %u err %d\r\n", addr, level, err);
865#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
866 .security_changed = security_changed,
870#if defined(CONFIG_BT_NUS_SECURITY_ENABLED)
879static void auth_passkey_display(
struct bt_conn *conn,
unsigned int passkey) {
880 char addr[BT_ADDR_LE_STR_LEN];
882 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
884 myPrintkI(
"Passkey for %s: %06u\r\n", addr, passkey);
895static void auth_passkey_confirm(
struct bt_conn *conn,
unsigned int passkey) {
896 char addr[BT_ADDR_LE_STR_LEN];
900 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
902 myPrintkI(
"Passkey for %s: %06u\r\n", addr, passkey);
903 myPrintkI(
"Press Button 1 to confirm, Button 2 to reject.\r\n");
912static void auth_cancel(
struct bt_conn *conn) {
913 char addr[BT_ADDR_LE_STR_LEN];
915 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
917 myPrintkI(
"Pairing cancelled: %s\r\n", addr);
927static void pairing_complete(
struct bt_conn *conn,
bool bonded) {
928 char addr[BT_ADDR_LE_STR_LEN];
930 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
932 myPrintkI(
"Pairing completed: %s, bonded: %d\r\n", addr, bonded);
942static void pairing_failed(
struct bt_conn *conn,
enum bt_security_err reason) {
943 char addr[BT_ADDR_LE_STR_LEN];
945 bt_addr_le_to_str(bt_conn_get_dst(conn), addr,
sizeof(addr));
947 myPrintkI(
"Pairing failed conn: %s, reason %d %s\r\n", addr, reason, bt_security_err_to_str(reason));
957 .passkey_display = auth_passkey_display,
958 .passkey_confirm = auth_passkey_confirm,
959 .cancel = auth_cancel,
969static struct bt_conn_auth_info_cb
conn_auth_info_callbacks = {.pairing_complete = pairing_complete, .pairing_failed = pairing_failed};
982static void bt_receive_cb(
struct bt_conn *conn,
const uint8_t *
const data, uint16_t len) {
984 char addr[BT_ADDR_LE_STR_LEN] = {0};
986 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, ARRAY_SIZE(addr));
993 k_sleep(K_SECONDS(2));
996 myPrintkI(
"Received data from: %s\r\n", addr);
999 for (uint16_t pos = 0; pos != len;) {
1007 k_sleep(K_SECONDS(2));
1009 myPrintkW(
"Not able to allocate UART send data buffer\r\n");
1014 size_t tx_data_size =
sizeof(
tx->data) - 1;
1016 if ((
len - pos) > tx_data_size) {
1017 tx->len = tx_data_size;
1019 tx->len = (
len - pos);
1022 memcpy(
tx->data, &
data[pos],
tx->len);
1029 if ((pos ==
len) && (
data[
len - 1] ==
'\r')) {
1030 tx->data[
tx->len] =
'\n';
1038 k_sleep(K_SECONDS(2));
1041 err = uart_tx(uart,
tx->data,
tx->len, SYS_FOREVER_MS);
1043 k_fifo_put(&fifo_uart_tx_data,
tx);
1047 tx->data[
tx->len] =
'\0';
1054 for (uint16_t pos = 0; pos !=
len;) {
1057 size_t tx_data_size =
sizeof(bleStr) - 1;
1059 if ((
len - pos) > tx_data_size) {
1060 lentemp = tx_data_size;
1062 lentemp = (
len - pos);
1065 memcpy(bleStr, &
data[pos], lentemp);
1072 if ((pos ==
len) && (
data[
len - 1] ==
'\r')) {
1073 bleStr[lentemp] =
'\n';
1078 bleStr[lentemp] =
'\0';
1083 k_sleep(K_SECONDS(2));
1108 dk_set_leds_state(DK_ALL_LEDS_MSK, DK_NO_LEDS_MSK);
1113 k_sleep(K_MSEC(1000));
1117#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
1126static void num_comp_reply(
bool accept) {
1128 bt_conn_auth_passkey_confirm(
auth_conn);
1151void button_changed(uint32_t button_state, uint32_t has_changed) {
1152 uint32_t buttons = button_state & has_changed;
1156 num_comp_reply(
true);
1160 num_comp_reply(
false);
1169#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
1170 err = dk_buttons_init(button_changed);
1172 myPrintkE(
"Cannot init buttons (err: %d)\r\n", err);
1176 err = dk_leds_init();
1178 myPrintkE(
"Cannot init LEDs (err: %d)\r\n", err);
1204 ret = NRF_POWER->RESETREAS;
1206 sprintf(
resetCause,
"Power applied reset, ");
1210 strcat(
resetCause,
"Global Watchdog reset, ");
1212 strcat(
resetCause,
"Wakeup from system off detect from GPIO reset, ");
1214 strcat(
resetCause,
"Wakeup from system off debug interface mode reset, ");
1216 strcat(
resetCause,
"AIRCR.SYSRESETREQ reset, ");
1224 NRF_POWER->RESETREAS =
ret;
1235 if (!gpio_is_ready_dt(&
hdc2080)) {
1242 err = gpio_pin_configure_dt(&
hdc2080, GPIO_INPUT);
1251 err = gpio_pin_interrupt_configure_dt(&
hdc2080, GPIO_INT_EDGE_FALLING);
1266 if (!gpio_is_ready_dt(&
sw2)) {
1267 myPrintkE(
"Error: SW2 GPIO %s is not ready\n",
sw2.port->name);
1273 err = gpio_pin_configure_dt(&
sw2, GPIO_INPUT);
1275 myPrintkE(
"Error %d: failed to configure %s pin %d\n", err,
sw2.port->name,
sw2.pin);
1278 myPrintkI(
"SW2 passed configure %s pin %d\n",
sw2.port->name,
sw2.pin);
1283 err = gpio_pin_interrupt_configure_dt(&
sw2, GPIO_INT_EDGE_FALLING);
1287 myPrintkE(
"Error %d: failed to configure interrupt on %s pin %d\n", err,
sw2.port->name,
sw2.pin);
1290 myPrintkI(
"SW2 passed configure interrupt on %s pin %d\n",
sw2.port->name,
sw2.pin);
1299 if (!gpio_is_ready_dt(&
spics1n)) {
1300 myPrintkE(
"SPI CS1N device is not ready\r\n");
1302 myPrintkI(
"SPI CS1N device is ready\r\n");
1303 gpio_pin_configure_dt(&
spics1n, GPIO_OUTPUT | GPIO_PULL_UP | GPIO_ACTIVE_LOW);
1307 if (!gpio_is_ready_dt(&
spics2n)) {
1308 myPrintkE(
"SPI CS2N device is not ready\r\n");
1310 myPrintkI(
"SPI CS2N device is ready\r\n");
1311 gpio_pin_configure_dt(&
spics2n, GPIO_OUTPUT | GPIO_PULL_UP | GPIO_ACTIVE_LOW);
1315 if (!gpio_is_ready_dt(&
baten)) {
1316 myPrintkE(
"BATEN device is not ready\r\n");
1319 gpio_pin_configure_dt(&
baten, GPIO_OUTPUT | GPIO_PULL_UP);
1326 test = gpio_pin_get(
sw2.port,
sw2.pin);
1328 k_sleep(K_SECONDS(1));
1336 if (k_sem_take(&sw2_rdy_seen, K_MSEC(100)) == 0 )
1356 k_sleep(K_MSEC(10));
1362 k_sleep(K_MSEC(20));
1386 k_sleep(K_MSEC(20));
1409 k_sleep(K_MSEC(20));
1433 unsigned char datas[6];
1438 i2c_write(
i2c_dev, datas, 1, 0x40);
1440 i2c_read(
i2c_dev, datas, 2, 0x40);
1441 printf(
"I2C 0x%02X 0x%02X\r\n",datas[0],datas[1]);
1442 k_sleep(K_MSEC(1000));
1451 if (!device_is_ready(
spi_dev)) {
1452 myPrintkE(
"SPI device is not ready\r\n");
1458 printf(
"SPI Test Running\r\n");
1462 for(
int i=0;i<4;i++)
1470 k_sleep(K_MSEC(10));
1474 k_sleep(K_MSEC(10));
1482 uint8_t readBuffer[10];
1486 printf(
"Flash Test Running\r\n");
1504 k_sleep(K_MSEC(100));
1514 if (!device_is_ready(
adc_dev))
1523 myPrintkE(
"Error in adc AIN0 setup: %d\n", err);
1529 printf(
"ADC Test Running\r\n");
1534 k_sleep(K_MSEC(5000));
1542 sprintf(
devIDStr,
"%08X", NRF_FICR->DEVICEID[0]);
1552#ifndef CONFIG_SERIAL
1557 myPrintkS(
"Built:%s %s\r\n", __DATE__, __TIME__);
1565 myPrintkW(
"Flash key not found, try again\r\n");
1573 k_sleep(K_SECONDS(1));
1582#ifdef SIMULATED_INTERRUPT
1584 k_timer_init(&simulated_interrupt_timer, simulated_interrupt_timer_handler, NULL);
1585 k_timer_start(&simulated_interrupt_timer, K_SECONDS(5), K_SECONDS(5));
1586 k_sleep(K_SECONDS(2));
1593 myPrintkE(
"watchdog_init_and_start, error: %d\n", err);
1595 myPrintkI(
"watchdog_init_and_start Passed\n");
1600 if (IS_ENABLED(CONFIG_BT_NUS_SECURITY_ENABLED)) {
1603 myPrintkE(
"Failed to register BLE authorization callbacks.\n");
1606 myPrintkI(
"Passed register BLE authorization callbacks.\n");
1610 myPrintkE(
"Failed to register BLE authorization info callbacks.\n");
1613 myPrintkI(
"passed register BLE authorization info callbacks.\n");
1616 err = bt_enable(NULL);
1618 myPrintkE(
"Failed to enable BLE. %d\n", err);
1623 k_sem_give(&ble_init_ok);
1625 if (IS_ENABLED(CONFIG_SETTINGS)) {
1626 err = settings_load();
1629 err = bt_nus_init(&
nus_cb);
1642 ad[1].type = BT_DATA_NAME_COMPLETE;
1646#ifndef SW2STARTSADVERTISING
1683 k_sleep(K_SECONDS(3));
1690 myPrintkE(
"UART0 RX Disable Failed: %d\r\n", err);
1692 myPrintkW(
"UART0 SUSPEND: %d\r\n", err);
1698 err = pm_device_action_run(
uart0_dev, PM_DEVICE_ACTION_SUSPEND);
1707 if (k_sem_take(&cli_cmd_seen, K_NO_WAIT) == 0) {
1714 if (k_sem_take(&sw2_rdy_seen, K_NO_WAIT) == 0) {
1737 k_sleep(K_MSEC(100));
1747 uint8_t humidityStatus;
1750 myPrintkW(
"hdc2080RdyHandler called\r\n");
1795 myPrintkW(
"In the middle of sending device info, skip sending data\r\n");
1834 if ((
byte[0] & 0x6) == 0x0)
1847 }
else if ((
byte[0] & 0x6) == 0x2)
1851 }
else if ((
byte[0] & 0x6) == 0x4)
1860 k_sleep(K_SECONDS(2));
1874 k_sleep(K_SECONDS(2));
1884 k_sleep(K_SECONDS(2));
1894 k_sleep(K_SECONDS(2));
1906 k_sleep(K_SECONDS(2));
2055 k_sem_take(&ble_init_ok, K_FOREVER);
2063 struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data,
2066 int plen = MIN(
sizeof(nus_data.
data) - nus_data.
len, buf->
len);
2071 memcpy(&nus_data.
data[nus_data.
len], &buf->
data[loc], plen);
2072 nus_data.
len += plen;
2075 if (nus_data.
len >=
sizeof(nus_data.
data) ||
2076 (nus_data.
data[nus_data.
len - 1] ==
'\n') ||
2077 (nus_data.
data[nus_data.
len - 1] ==
'\r'))
2079 if (bt_nus_send(NULL, nus_data.
data, nus_data.
len))
2081 LOG_WRN(
"Failed to send data over BLE connection");
2086 plen = MIN(
sizeof(nus_data.
data), buf->
len - loc);
2092 uint8_t myBuff[128];
2098 k_sem_take(&ble_init_ok, K_FOREVER);
2105 struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data, K_FOREVER);
2107 int plen = MIN(
sizeof(nus_data.
data) - nus_data.
len, buf->
len);
2110 memset(myBuff, 0,
sizeof(myBuff));
2112 memcpy(&nus_data.
data[nus_data.
len], &buf->
data[loc], plen);
2113 nus_data.
len += plen;
2117 if (nus_data.
len >=
sizeof(nus_data.
data) || (nus_data.
data[nus_data.
len - 1] ==
'\n') || (nus_data.
data[nus_data.
len - 1] ==
'\r')) {
2118 nus_data.
data[nus_data.
len - 1] =
'\0';
2121 strncpy(myBuff, nus_data.
data, nus_data.
len);
2122 for (i = 0; i < strlen(myBuff); i++) {
2123 if ((myBuff[i] ==
'\r') || (myBuff[i] ==
'\n'))
2130 k_sem_give(&cli_cmd_seen);
2138 plen = MIN(
sizeof(nus_data.
data), buf->
len - loc);
2161 uint32_t i2c_cfg = I2C_SPEED_SET(I2C_SPEED_FAST) | I2C_MODE_CONTROLLER;
2164 myPrintkS(
"Initializing I2C device\r\n");
2166 if (!device_is_ready(
i2c_dev))
2174 if (i2c_configure(
i2c_dev, i2c_cfg))
2179 myPrintkI(
"I2C config passed %08X\n", i2c_cfg);
2199 if (BIT(
hdc2080.pin) & pins) {
2219void sw2_seen(
const struct device *dev,
struct gpio_callback *cb, uint32_t pins) {
2222 if (BIT(
sw2.pin) & pins) {
2223 k_sem_give(&sw2_rdy_seen);
2316 n = vprintf(fmt, ap);
2358 printk(
"\33[1;33m");
2391 printk(
"\33[1;32m");
2424 printk(
"\33[1;36m");
2457 printk(
"\33[1;31m");
uint8_t readReg(uint8_t reg, uint8_t *data, uint8_t count)
Reads a specified number of bytes from a sensor register over I2C.
void setRate(int rate)
Sets the automatic measurement frequency.
float readHumidity(void)
Reads the relative humidity from the HDC2080 sensor.
int initHDC2080(void)
Initializes the HDC2080 sensor with default parameters.
float readTemp(void)
Reads the temperature data from the HDC2080 sensor.
uint8_t readInterruptStatus(void)
Reads the interrupt status register.
#define MEASUREMENT_CONFIG
int initBME688(void)
Initializes the BME688 sensor over SPI. Performs interface initialization, device reset,...
int getBME688Data(int count, uint8_t quiet)
Triggers and retrieves measurement data from the BME688.
struct spi_config spi_cfg1
SPI configuration.
struct spi_buf tx_bufs[]
SPI TX buffer structure.
const struct spi_buf_set rx
SPI RX buffer structure.
const struct device *const spi_dev
flashParametersStruct flashParameters
working valuses of flash parameters in RAM
float adc_voltage[2]
last ADC voltage read [0] battery
const struct gpio_dt_spec spics2n
struct spi_config spi_cfg2
SPI configuration.
uint8_t buffer_tx[SPI_BUF_SIZE+8]
SPI transmit buffer.
uint8_t sendingDeviceInfo
flag to indicate sending device info in BLE packet instead of sensor data
long long gatewayDateTime
char * cmdLineParameter[MAX_PARAMETERS]
array of pointers to command line parameter strings
struct bme68x_data BME688Data
struct spi_buf rx_bufs[]
SPI RX buffer structure.
const struct spi_buf_set tx
SPI TX buffer structure.
const struct gpio_dt_spec spics1n
COMMANDS
< vaild command indexes
void executeCmd(char *line)
executes the command in line array.
void int64_to_string(int64_t num, char *str)
Converts a 64-bit signed integer to a null-terminated string.
common struct, enum, externs, prototypes
void sw2_seen(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
Callback function for SW2 button.
#define HIGHHUMIDITYACTIVE
void displayFlashxParameters(void)
displays the NV flash parameters.
void flashxClearParameters(void)
clears/initializes the flash parameters in RAM
#define VERSION_DATE_EXT2
int initW25Q64JV(void)
Initiailizes the W25Q64JW.
#define LOWHUMIDITYACTIVE
#define VERSION_DATE_EXT3
struct k_sem uart_rx_disabled_sem
struct k_sem hdc_rdy_seen
#define EXPAND_AND_STR(x)
#define FLASHPARAMETERKEY
void hdc2080_rdy_seen(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
Callback function for HDC2080 RDY pin.
const struct device *const i2c_dev
get the device structure for the I2C device defined by I2C_DEV_NODE
#define VERSION_DATE_EXT1
const struct gpio_dt_spec baten
get the GPIO specification for the BATEN pin defined by BATEN_NODE
const struct device *const uart0_dev
void setFlashxParameters(uint8_t display)
loads the NV Flash parameters from teh RAM flash parameters
#define HIGHHUMIDITYENABLE
void readNVFlashxStatus(uint8_t *flashStatus)
reads the 3 NV Flash status registers over the SPI bus.
#define LOWHUMIDITYENABLE
void getFlashparameters(void)
Read the stored flash parameter block from serial flash.
int getCmd(uint8_t *line)
#define ADC_RESOLUTION
resolution for ADC readings in bits (e.g., 10 bits for values from 0 to 1023)
int myPrintk(char *restrict fmt,...)
void sw2_seen(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
Callback function for SW2 button.
char const * validCmds[]
array of valid commands - case insensitive
static struct bt_conn_auth_cb conn_auth_callbacks
static void disconnected(struct bt_conn *conn, uint8_t reason)
Callback function for when a Bluetooth connection is lost.
#define ADC_4TH_CHANNEL_ID
channel ID for the fourth ADC channel to read, set to 3 for the fourth channel (e....
K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL, NULL, PRIORITY, 0, 0)
#define STACKSIZE
stack size for main thread
#define PRIORITY
priority for main thread
static void adv_work_handler(struct k_work *work)
Handler for the advertising work item.
#define ADC_2ND_CHANNEL_INPUT
static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data, uint16_t len)
Callback function for receiving data over Bluetooth.
#define ADC_REFERENCE
reference voltage for ADC readings, set to internal reference for stable and consistent measurements
static void configure_gpio(void)
const struct device *const adc_dev
get the device structure for the ADC device defined by ADC_DEV_NODE
static struct bt_data ad[2]
int myPrintkS(char *restrict fmt,...)
prints a status message to the UART
static const float adc_volts_per_step
#define CON_STATUS_LED
LED to indicate the BLE connection status.
#define KEY_PASSKEY_ACCEPT
Button used to accept a pairing passkey.
#define UART_WAIT_FOR_BUF_DELAY
delay to wait for UART buffer availability in milliseconds
static void recycled_cb(void)
Callback function for when a Bluetooth connection object is recycled.
int adc_sample(void)
get a single reading from ADC channel
void hdc2080RdyHandler(void)
< check if i2c_0 alias is okay in devicetree
void error(void)
Function to handle fatal errors This function is called when a fatal error occurs in the system....
static struct gpio_callback hdc2080_cb_data
GPIO callback structure for handling HDC2080 ready pin interrupts.
int myPrintkI(char *restrict fmt,...)
prints an information message to the UART
#define DEVICE_NAME_LEN
length of device name
#define DEVICE_NAME
device name defined in prj.conf
#define ADC_BUFFER_SIZE
number of ADC channels to read
static struct bt_nus_cb nus_cb
Bluetooth NUS callbacks This structure defines the callbacks for Bluetooth NUS events,...
#define BATEN_NODE
check if BATEN_NODE is okay in devicetree
#define RUN_STATUS_LED
LED to indicate the application is running.
static struct k_work adv_work
work item for handling BLE advertising operations, used to schedule advertising tasks in the system w...
static struct bt_conn_auth_info_cb conn_auth_info_callbacks
void hdc2080_rdy_seen(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
Callback function for HDC2080 RDY pin.
int main(void)
Nordic UART Bridge Service (NUS) sample (BLE_Prototype) starting point.
#define ADC_3RD_CHANNEL_ID
channel ID for the third ADC channel to read, set to 2 for the third channel (e.g....
int myPrintkE(char *restrict fmt,...)
prints an error message to the UART
#define SW2_NODE
check if SW2_NODE is okay in devicetree
char device_name[40]
buffer to hold the device name string, used for BLE advertising and identification
BT_CONN_CB_DEFINE(conn_callbacks)
Bluetooth connection callbacks.
uint8_t bleCommand(uint8_t *data)
Executes a BLE command.
static int16_t m_sample_buffer[ADC_BUFFER_SIZE]
buffer to receive ADC data from configured channels
void ble_write_thread(void)
static const struct gpio_dt_spec hdc2080
get the GPIO specification for the HDC2080 ready pin defined by HDC2080RDY_NODE
void rescan(void)
rescan all I2C and SPI devices
static K_FIFO_DEFINE(fifo_uart_tx_data)
int init_i2c()
Initializes the I2C interface.
#define ADC_3RD_CHANNEL_INPUT
#define ADC_1ST_CHANNEL_ID
channel ID for the first ADC channel to read, set to 0 for the first channel (e.g....
#define UART_BUF_SIZE
size of the buffer for UART data
static void connected(struct bt_conn *conn, uint8_t err)
Callback function for when a Bluetooth connection is established.
#define KEY_PASSKEY_REJECT
Button used to reject a pairing passkey.
static float adc_raw_to_voltage(uint16_t raw)
Convert raw ADC reading to voltage in volts.
#define HDC2080RDY_NODE
check if HDC2080RDY_NODE is okay in devicetree
static const struct bt_data sd[]
#define ADC_4TH_CHANNEL_INPUT
static const struct gpio_dt_spec sw2
get the GPIO specification for the SW2 button pin defined by SW2_NODE
#define ADC_2ND_CHANNEL_ID
channel ID for the second ADC channel to read, set to 1 for the second channel (e....
static void advertising_start(void)
Start the advertising process.
uint8_t device_name_len
length of the device name string, used to ensure proper formatting and transmission in BLE advertisin...
#define ADC_ACQUISITION_TIME
#define ADC_1ST_CHANNEL_INPUT
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG)
static struct bt_conn * current_conn
int myPrintkW(char *restrict fmt,...)
prints a warning message to the UART
static const struct adc_channel_cfg m_1st_channel_cfg
ADC configuration for the first channel This structure defines the configuration for the first ADC ch...
static K_SEM_DEFINE(ble_init_ok, 0, 1)
semaphore to indicate when BLE initialization is complete and the system is ready for BLE operations
static struct bt_conn * auth_conn
static struct gpio_callback sw2_cb_data
GPIO callback structure for handling SW2 button pin interrupts.
#define RUN_LED_BLINK_INTERVAL
interval for blinking the run status LED in milliseconds
Flash parameters structure.
uint8_t data[UART_BUF_SIZE]
int watchdog_init_and_start(void)
Initialize and start the watchdog timer.
Watchdog module for Asset Tracker v2.