![]() |
Data Logger BLE Cellular Board
|
Nordic UART Bridge Service (NUS) sample. More...
#include <zephyr/kernel.h>#include <zephyr/types.h>#include <zephyr/usb/usb_device.h>#include <soc.h>#include <zephyr/device.h>#include <zephyr/devicetree.h>#include <zephyr/bluetooth/bluetooth.h>#include <zephyr/bluetooth/gatt.h>#include <zephyr/bluetooth/hci.h>#include <zephyr/bluetooth/uuid.h>#include <bluetooth/services/nus.h>#include <dk_buttons_and_leds.h>#include <zephyr/settings/settings.h>#include <stdio.h>#include <zephyr/drivers/gpio.h>#include "jfet_files/jfet_common.h"#include "mainpage.h"#include "watchdog/watchdog_app.h"#include <zephyr/logging/log.h>#include "jfet_files/HDC2080.h"#include "ncs_version.h"#include <stdlib.h>#include <time.h>#include <zephyr/drivers/i2c.h>#include <zephyr/pm/device.h>#include <zephyr/drivers/spi.h>#include <math.h>#include <nrfx_saadc.h>#include <zephyr/drivers/adc.h>Go to the source code of this file.
Classes | |
| struct | uart_data_t |
Macros | |
| #define | STACKSIZE CONFIG_BT_NUS_THREAD_STACK_SIZE |
| stack size for main thread | |
| #define | PRIORITY 7 |
| priority for main thread | |
| #define | DEVICE_NAME CONFIG_BT_DEVICE_NAME |
| device name defined in prj.conf | |
| #define | DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) |
| length of device name | |
| #define | RUN_STATUS_LED DK_LED1 |
| LED to indicate the application is running. | |
| #define | RUN_LED_BLINK_INTERVAL 1000 |
| interval for blinking the run status LED in milliseconds | |
| #define | CON_STATUS_LED DK_LED2 |
| LED to indicate the BLE connection status. | |
| #define | KEY_PASSKEY_ACCEPT DK_BTN1_MSK |
| Button used to accept a pairing passkey. | |
| #define | KEY_PASSKEY_REJECT DK_BTN2_MSK |
| Button used to reject a pairing passkey. | |
| #define | UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE |
| size of the buffer for UART data | |
| #define | UART_WAIT_FOR_BUF_DELAY K_MSEC(50) |
| delay to wait for UART buffer availability in milliseconds | |
| #define | UART_WAIT_FOR_RX CONFIG_BT_NUS_UART_RX_WAIT_TIME |
| #define | BATEN_NODE DT_ALIAS(baten) |
| check if BATEN_NODE is okay in devicetree | |
| #define | HDC2080RDY_NODE DT_ALIAS(hdc2080rdy) |
| check if HDC2080RDY_NODE is okay in devicetree | |
| #define | SW2_NODE DT_ALIAS(sw2input) |
| check if SW2_NODE is okay in devicetree | |
| #define | ADC_BUFFER_SIZE 1 |
| number of ADC channels to read | |
| #define | ADC_RESOLUTION 10 |
| resolution for ADC readings in bits (e.g., 10 bits for values from 0 to 1023) | |
| #define | ADC_GAIN ADC_GAIN_1_6 |
| #define | ADC_REFERENCE ADC_REF_INTERNAL |
| reference voltage for ADC readings, set to internal reference for stable and consistent measurements | |
| #define | ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10) |
| #define | ADC_1ST_CHANNEL_ID 0 |
| channel ID for the first ADC channel to read, set to 0 for the first channel (e.g., AIN0 on nRF52832) | |
| #define | ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0 |
| #define | ADC_2ND_CHANNEL_ID 1 |
| channel ID for the second ADC channel to read, set to 1 for the second channel (e.g., AIN1 on nRF52832) | |
| #define | ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN1 |
| #define | ADC_3RD_CHANNEL_ID 2 |
| channel ID for the third ADC channel to read, set to 2 for the third channel (e.g., AIN2 on nRF52832) | |
| #define | ADC_3RD_CHANNEL_INPUT NRF_SAADC_INPUT_AIN2 |
| #define | ADC_4TH_CHANNEL_ID 3 |
| channel ID for the fourth ADC channel to read, set to 3 for the fourth channel (e.g., AIN3 on nRF52832) | |
| #define | ADC_4TH_CHANNEL_INPUT NRF_SAADC_INPUT_AIN3 |
| #define | ADC_5TH_CHANNEL_ID 4 |
| channel ID for the fifth ADC channel to read, set to 4 for the fifth channel (e.g., AIN4 on nRF52832) | |
| #define | ADC_5TH_CHANNEL_INPUT NRF_SAADC_INPUT_AIN4 |
| #define | ADC_6ST_CHANNEL_ID 5 |
| channel ID for the sixth ADC channel to read, set to 5 for the sixth channel (e.g., AIN5 on nRF52832) | |
| #define | ADC_6ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN5 |
| #define | ADC_REF_MV 3600U |
| #define | ADC_DIV_MV 1000U |
| #define | ADC_MAX_VAL ((1U << ADC_RESOLUTION) - 1U) |
Enumerations | |
| enum | COMMANDS { CR , NONE } |
| Valid command indexes. More... | |
Functions | |
| LOG_MODULE_REGISTER (main, LOG_LEVEL_DBG) | |
| void | hdc2080RdyHandler (void) |
| < check if i2c_0 alias is okay in devicetree | |
| 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 | |
| K_SEM_DEFINE (hdc_rdy_seen, 0, 1) | |
| semaphore to indicate when the HDC2080 sensor ready signal has been seen | |
| K_SEM_DEFINE (sw2_rdy_seen, 0, 1) | |
| semaphore to indicate when the SW2 button press has been seen | |
| K_SEM_DEFINE (cli_cmd_seen, 0, 1) | |
| semaphore to indicate when a CLI command has been received and is ready for processing | |
| K_SEM_DEFINE (uart_rx_disabled_sem, 0, 1) | |
| semaphore to indicate UART RX is disabled | |
| static float | adc_raw_to_voltage (uint16_t raw) |
| Convert raw ADC reading to voltage in volts. | |
| int | adc_sample (void) |
| get a single reading from ADC channel | |
| static | K_FIFO_DEFINE (fifo_uart_tx_data) |
| static | K_FIFO_DEFINE (fifo_uart_rx_data) |
| static void | adv_work_handler (struct k_work *work) |
| Handler for the advertising work item. | |
| static void | advertising_start (void) |
| Start the advertising process. | |
| static void | connected (struct bt_conn *conn, uint8_t err) |
| Callback function for when a Bluetooth connection is established. | |
| static void | disconnected (struct bt_conn *conn, uint8_t reason) |
| Callback function for when a Bluetooth connection is lost. | |
| static void | recycled_cb (void) |
| Callback function for when a Bluetooth connection object is recycled. | |
| BT_CONN_CB_DEFINE (conn_callbacks) | |
| Bluetooth connection callbacks. | |
| static void | bt_receive_cb (struct bt_conn *conn, const uint8_t *const data, uint16_t len) |
| Callback function for receiving data over Bluetooth. | |
| void | error (void) |
| Function to handle fatal errors This function is called when a fatal error occurs in the system. It turns off all LEDs to indicate an error state and enters an infinite loop to halt the system. This is a common error handling approach in embedded systems to prevent further damage or undefined behavior after a critical failure. The function also includes a commented-out print statement that could be used to log the error message before halting the system, but it is currently disabled to avoid potential issues with logging in an error state. | |
| static void | configure_gpio (void) |
| int | main (void) |
| Nordic UART Bridge Service (NUS) sample (BLE_Prototype) starting point. | |
| void | ble_write_thread (void) |
| int | init_i2c () |
| Initializes the I2C interface. | |
| void | hdc2080_rdy_seen (const struct device *dev, struct gpio_callback *cb, uint32_t pins) |
| Callback function for HDC2080 RDY pin. | |
| void | sw2_seen (const struct device *dev, struct gpio_callback *cb, uint32_t pins) |
| Callback function for SW2 button. | |
| uint8_t | bleCommand (uint8_t *data) |
| Executes a BLE command. | |
| void | rescan (void) |
| rescan all I2C and SPI devices | |
| int | myPrintk (char *restrict fmt,...) |
| int | myPrintkW (char *restrict fmt,...) |
| prints a warning message to the UART | |
| int | myPrintkI (char *restrict fmt,...) |
| prints an information message to the UART | |
| int | myPrintkS (char *restrict fmt,...) |
| prints a status message to the UART | |
| int | myPrintkE (char *restrict fmt,...) |
| prints an error message to the UART | |
| K_THREAD_DEFINE (ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL, NULL, PRIORITY, 0, 0) | |
Variables | |
| const struct device *const | i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE) |
| get the device structure for the I2C device defined by I2C_DEV_NODE | |
| static struct bt_conn * | current_conn |
| static struct bt_conn * | auth_conn |
| static struct k_work | adv_work |
| work item for handling BLE advertising operations, used to schedule advertising tasks in the system workqueue | |
| const struct gpio_dt_spec | baten |
| get the GPIO specification for the BATEN pin defined by BATEN_NODE | |
| static const struct gpio_dt_spec | hdc2080 |
| get the GPIO specification for the HDC2080 ready pin defined by HDC2080RDY_NODE | |
| static struct gpio_callback | hdc2080_cb_data |
| GPIO callback structure for handling HDC2080 ready pin interrupts. | |
| static const struct gpio_dt_spec | sw2 |
| get the GPIO specification for the SW2 button pin defined by SW2_NODE | |
| static struct gpio_callback | sw2_cb_data |
| GPIO callback structure for handling SW2 button pin interrupts. | |
| const struct device *const | adc_dev = DEVICE_DT_GET(ADC_DEV_NODE) |
| get the device structure for the ADC device defined by ADC_DEV_NODE | |
| 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 channel, including gain, reference voltage, acquisition time, channel ID, and input selection. It is used to set up the ADC for reading from the specified channel with the desired settings. | |
| static int16_t | m_sample_buffer [ADC_BUFFER_SIZE] |
| buffer to receive ADC data from configured channels | |
| static const float | adc_volts_per_step = (float)ADC_REF_MV / ((float)ADC_MAX_VAL * (float)ADC_DIV_MV) |
| char | device_name [40] |
| buffer to hold the device name string, used for BLE advertising and identification | |
| uint8_t | device_name_len |
| length of the device name string, used to ensure proper formatting and transmission in BLE advertising packets | |
| char const * | validCmds [] |
| array of valid commands - case insensitive | |
| static struct bt_data | ad [2] |
| static const struct bt_data | sd [] |
| static struct bt_conn_auth_cb | conn_auth_callbacks |
| static struct bt_conn_auth_info_cb | conn_auth_info_callbacks |
| static struct bt_nus_cb | nus_cb |
| Bluetooth NUS callbacks This structure defines the callbacks for Bluetooth NUS events, including data reception. | |
| int | bleLoopCount = 0 |
Nordic UART Bridge Service (NUS) sample.
Definition in file main.c.
| #define ADC_1ST_CHANNEL_ID 0 |
| #define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0 |
| #define ADC_2ND_CHANNEL_ID 1 |
| #define ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN1 |
| #define ADC_3RD_CHANNEL_ID 2 |
| #define ADC_3RD_CHANNEL_INPUT NRF_SAADC_INPUT_AIN2 |
| #define ADC_4TH_CHANNEL_ID 3 |
| #define ADC_4TH_CHANNEL_INPUT NRF_SAADC_INPUT_AIN3 |
| #define ADC_5TH_CHANNEL_ID 4 |
| #define ADC_5TH_CHANNEL_INPUT NRF_SAADC_INPUT_AIN4 |
| #define ADC_6ST_CHANNEL_ID 5 |
| #define ADC_6ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN5 |
| #define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10) |
| #define ADC_BUFFER_SIZE 1 |
| #define ADC_GAIN ADC_GAIN_1_6 |
| #define ADC_MAX_VAL ((1U << ADC_RESOLUTION) - 1U) |
| #define ADC_REFERENCE ADC_REF_INTERNAL |
| #define ADC_RESOLUTION 10 |
| #define BATEN_NODE DT_ALIAS(baten) |
| #define CON_STATUS_LED DK_LED2 |
| #define DEVICE_NAME CONFIG_BT_DEVICE_NAME |
| #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) |
| #define HDC2080RDY_NODE DT_ALIAS(hdc2080rdy) |
| #define KEY_PASSKEY_ACCEPT DK_BTN1_MSK |
| #define KEY_PASSKEY_REJECT DK_BTN2_MSK |
| #define RUN_LED_BLINK_INTERVAL 1000 |
| #define RUN_STATUS_LED DK_LED1 |
| #define STACKSIZE CONFIG_BT_NUS_THREAD_STACK_SIZE |
| #define SW2_NODE DT_ALIAS(sw2input) |
| #define UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE |
| #define UART_WAIT_FOR_BUF_DELAY K_MSEC(50) |
|
inlinestatic |
Convert raw ADC reading to voltage in volts.
| raw | Raw ADC sample value (integer from 0..ADC_MAX_VAL) |
Uses the precomputed adc_volts_per_step factor which incorporates the reference voltage (ADC_REF_MV), ADC resolution and any divider (ADC_DIV_MV).
Definition at line 330 of file main.c.

| int adc_sample | ( | void | ) |
get a single reading from ADC channel
will read ADC_1ST_CHANNEL_ID
resoultion set to ADC_RESOLUTION in millivolts
will printout floating point value based on 3.6V max
Definition at line 343 of file main.c.


|
static |
|
static |
| void ble_write_thread | ( | void | ) |
| uint8_t bleCommand | ( | uint8_t * | data | ) |
Executes a BLE command.
| data | Pointer to the command data |
Definition at line 2239 of file main.c.


| BT_CONN_CB_DEFINE | ( | conn_callbacks | ) |
Bluetooth connection callbacks.

|
static |
|
static |
|
static |
Callback function for when a Bluetooth connection is established.
| conn | pointer to the Bluetooth connection structure |
| err | error code indicating the result of the connection attempt This function is called when a Bluetooth connection is established. It checks for any errors during the connection process and logs the result. If the connection is successful, it stores a reference to the current connection and updates the connection status LED to indicate that a device is connected. |
Definition at line 776 of file main.c.


|
static |
Callback function for when a Bluetooth connection is lost.
| conn | pointer to the Bluetooth connection structure |
| reason | reason for the disconnection This function is called when a Bluetooth connection is lost. It logs the address of the disconnected device and the reason for the disconnection. It also checks if there are any active connections or authentication processes and cleans up the connection references accordingly. Finally, it updates the connection status LED to indicate that no device is currently connected. |
Definition at line 801 of file main.c.


| void error | ( | void | ) |
Function to handle fatal errors This function is called when a fatal error occurs in the system. It turns off all LEDs to indicate an error state and enters an infinite loop to halt the system. This is a common error handling approach in embedded systems to prevent further damage or undefined behavior after a critical failure. The function also includes a commented-out print statement that could be used to log the error message before halting the system, but it is currently disabled to avoid potential issues with logging in an error state.
Definition at line 1107 of file main.c.

| void hdc2080_rdy_seen | ( | const struct device * | dev, |
| struct gpio_callback * | cb, | ||
| uint32_t | pins ) |
Callback function for HDC2080 RDY pin.
| dev | Pointer to the device structure |
| cb | Pointer to the GPIO callback structure |
| pins | GPIO pin mask This function is a callback that is triggered when the HDC2080 RDY pin is activated. It checks if the specific pin associated with the HDC2080 is part of the pins that triggered the callback. If it is, it gives a semaphore to indicate that the HDC2080 RDY signal has been seen, allowing other parts of the program to proceed with reading data from the sensor or performing other actions that depend on the RDY signal. The function includes commented-out code for debugging purposes, which can be used to print the state of the pin and the pins that triggered the callback. |
Definition at line 2196 of file main.c.

| void hdc2080RdyHandler | ( | void | ) |
| int init_i2c | ( | ) |
Initializes the I2C interface.
Note: The I2C speed is set to fast mode, and the device is configured as a controller. The function also includes debug print statements to indicate the status of the initialization process.
Definition at line 2160 of file main.c.


|
static |
FIFO queue for UART data that has been received, used to manage incoming UART data and ensure proper processing and flow control
|
static |
FIFO queue for UART data to be transmitted, used to manage outgoing UART data and ensure proper sequencing and flow control
|
static |
semaphore to indicate when BLE initialization is complete and the system is ready for BLE operations
| K_SEM_DEFINE | ( | cli_cmd_seen | , |
| 0 | , | ||
| 1 | ) |
semaphore to indicate when a CLI command has been received and is ready for processing
| K_SEM_DEFINE | ( | hdc_rdy_seen | , |
| 0 | , | ||
| 1 | ) |
semaphore to indicate when the HDC2080 sensor ready signal has been seen
| K_SEM_DEFINE | ( | sw2_rdy_seen | , |
| 0 | , | ||
| 1 | ) |
semaphore to indicate when the SW2 button press has been seen
| K_SEM_DEFINE | ( | uart_rx_disabled_sem | , |
| 0 | , | ||
| 1 | ) |
semaphore to indicate UART RX is disabled
| K_THREAD_DEFINE | ( | ble_write_thread_id | , |
| STACKSIZE | , | ||
| ble_write_thread | , | ||
| NULL | , | ||
| NULL | , | ||
| NULL | , | ||
| PRIORITY | , | ||
| 0 | , | ||
| 0 | ) |

| LOG_MODULE_REGISTER | ( | main | , |
| LOG_LEVEL_DBG | ) |

| int main | ( | void | ) |
Nordic UART Bridge Service (NUS) sample (BLE_Prototype) starting point.
This is the main function of the program, which serves as the entry point for execution. It performs initial setup and configuration, including determining the cause of the last reset, configuring GPIOs for buttons and LEDs, and setting up interrupts for the HDC2080 sensor and SW2 button. It also initializes the Bluetooth functionality and starts advertising for connections. The function includes error handling to ensure that any issues during initialization are logged and that the system enters a safe state if critical errors occur. Overall, this function orchestrates the initialization and main loop of the application, ensuring that all components are properly set up and ready for operation.
Definition at line 1199 of file main.c.


| int myPrintk | ( | char *restrict | fmt, |
| ... ) |
| int myPrintkE | ( | char *restrict | fmt, |
| ... ) |
prints an error message to the UART
| fmt | format string |
Definition at line 2452 of file main.c.

| int myPrintkI | ( | char *restrict | fmt, |
| ... ) |
prints an information message to the UART
| fmt | format string |
Definition at line 2386 of file main.c.

| int myPrintkS | ( | char *restrict | fmt, |
| ... ) |
prints a status message to the UART
| fmt | format string |
Definition at line 2419 of file main.c.

| int myPrintkW | ( | char *restrict | fmt, |
| ... ) |
prints a warning message to the UART
| fmt | format string |
Definition at line 2353 of file main.c.

|
static |
Callback function for when a Bluetooth connection object is recycled.
This function is called when a Bluetooth connection object is recycled, which typically occurs after a disconnection has been fully processed and the connection resources have been released. It logs a message indicating that the connection object is now available for reuse and starts the advertising process again to allow new devices to connect.
Definition at line 829 of file main.c.


| void rescan | ( | void | ) |
rescan all I2C and SPI devices
This function rescans all I2C and SPI devices by calling the initialization functions for each device. It checks if the HDC2080, W25Q64JV, and BME688 devices are available and properly initialized. If any of the initialization functions fail, it can be modified to handle the error accordingly. The function includes calls to the initialization functions for each device and can be expanded to include additional devices as needed. The current implementation assumes that the initialization functions will handle any necessary error reporting and that the rescan function will simply call them in sequence.
Definition at line 2281 of file main.c.


| void sw2_seen | ( | const struct device * | dev, |
| struct gpio_callback * | cb, | ||
| uint32_t | pins ) |
Callback function for SW2 button.
| dev | Pointer to the device structure |
| cb | Pointer to the GPIO callback structure |
| pins | GPIO pin mask This function is a callback that is triggered when the SW2 button is pressed. It checks if the specific pin associated with the SW2 button is part of the pins that triggered the callback. If it is, it gives a semaphore to indicate that the SW2 button has been seen, allowing other parts of the program to proceed with actions that depend on the SW2 button press. The function includes commented-out code for debugging purposes, which can be used to print the state of the pin and the pins that triggered the callback. |
Definition at line 2219 of file main.c.

|
static |
| const struct device* const adc_dev = DEVICE_DT_GET(ADC_DEV_NODE) |
|
static |
|
static |
|
static |
| const struct gpio_dt_spec baten |
get the GPIO specification for the BATEN pin defined by BATEN_NODE
|
static |
| char device_name[40] |
| uint8_t device_name_len |
|
static |
get the GPIO specification for the HDC2080 ready pin defined by HDC2080RDY_NODE
|
static |
| const struct device* const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE) |
|
static |
ADC configuration for the first channel This structure defines the configuration for the first ADC channel, including gain, reference voltage, acquisition time, channel ID, and input selection. It is used to set up the ADC for reading from the specified channel with the desired settings.
|
static |
|
static |
Bluetooth NUS callbacks This structure defines the callbacks for Bluetooth NUS events, including data reception.
|
static |
|
static |
get the GPIO specification for the SW2 button pin defined by SW2_NODE
|
static |