Data Logger BLE Cellular Board
Loading...
Searching...
No Matches
main.c File Reference

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
 

Detailed Description

Nordic UART Bridge Service (NUS) sample.

Note
SPDX-License-Identifier: LicenseRef-Nordic-5-Clause

Definition in file main.c.

Macro Definition Documentation

◆ ADC_1ST_CHANNEL_ID

#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)

Definition at line 229 of file main.c.

◆ ADC_1ST_CHANNEL_INPUT

#define ADC_1ST_CHANNEL_INPUT    NRF_SAADC_INPUT_AIN0

input selection for the first ADC channel, set to AIN0 for the first analog input on nRF52832 (e.g., connected to a sensor output or battery voltage)

Definition at line 230 of file main.c.

◆ ADC_2ND_CHANNEL_ID

#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)

Definition at line 233 of file main.c.

◆ ADC_2ND_CHANNEL_INPUT

#define ADC_2ND_CHANNEL_INPUT    NRF_SAADC_INPUT_AIN1

input selection for the second ADC channel, set to AIN1 for the second analog input on nRF52832 (e.g., connected to a different sensor output or reference voltage)

Definition at line 234 of file main.c.

◆ ADC_3RD_CHANNEL_ID

#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)

Definition at line 237 of file main.c.

◆ ADC_3RD_CHANNEL_INPUT

#define ADC_3RD_CHANNEL_INPUT    NRF_SAADC_INPUT_AIN2

input selection for the third ADC channel, set to AIN2 for the third analog input on nRF52832 (e.g., connected to a different sensor output or reference voltage)

Definition at line 238 of file main.c.

◆ ADC_4TH_CHANNEL_ID

#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)

Definition at line 241 of file main.c.

◆ ADC_4TH_CHANNEL_INPUT

#define ADC_4TH_CHANNEL_INPUT    NRF_SAADC_INPUT_AIN3

input selection for the fourth ADC channel, set to AIN3 for the fourth analog input on nRF52832 (e.g., connected to a different sensor output or reference voltage)

Definition at line 242 of file main.c.

◆ ADC_5TH_CHANNEL_ID

#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)

Definition at line 245 of file main.c.

◆ ADC_5TH_CHANNEL_INPUT

#define ADC_5TH_CHANNEL_INPUT    NRF_SAADC_INPUT_AIN4

input selection for the fifth ADC channel, set to AIN4 for the fifth analog input on nRF52832 (e.g., connected to a different sensor output or reference voltage)

Definition at line 246 of file main.c.

◆ ADC_6ST_CHANNEL_ID

#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)

Definition at line 249 of file main.c.

◆ ADC_6ST_CHANNEL_INPUT

#define ADC_6ST_CHANNEL_INPUT    NRF_SAADC_INPUT_AIN5

input selection for the sixth ADC channel, set to AIN5 for the sixth analog input on nRF52832 (e.g., connected to a different sensor output or reference voltage)

Definition at line 250 of file main.c.

◆ ADC_ACQUISITION_TIME

#define ADC_ACQUISITION_TIME    ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10)

acquisition time for ADC readings, set to 10 microseconds to allow for accurate sampling of the input signal while minimizing noise and power consumption

Definition at line 226 of file main.c.

◆ ADC_BUFFER_SIZE

#define ADC_BUFFER_SIZE   1

number of ADC channels to read

check if adc alias is okay in devicetree and set ADC_DEV_NODE to adc alias

Definition at line 211 of file main.c.

◆ ADC_DIV_MV

#define ADC_DIV_MV   1000U

Definition at line 315 of file main.c.

◆ ADC_GAIN

#define ADC_GAIN    ADC_GAIN_1_6

gain setting for ADC readings, set to 1/6 to allow for a wider input voltage range (e.g., up to 3.6V with a 3.3V reference)

Definition at line 221 of file main.c.

◆ ADC_MAX_VAL

#define ADC_MAX_VAL   ((1U << ADC_RESOLUTION) - 1U)

Definition at line 316 of file main.c.

◆ ADC_REF_MV

#define ADC_REF_MV   3600U

Definition at line 314 of file main.c.

◆ ADC_REFERENCE

#define ADC_REFERENCE    ADC_REF_INTERNAL

reference voltage for ADC readings, set to internal reference for stable and consistent measurements

Definition at line 224 of file main.c.

◆ ADC_RESOLUTION

#define ADC_RESOLUTION   10

resolution for ADC readings in bits (e.g., 10 bits for values from 0 to 1023)

Definition at line 220 of file main.c.

◆ BATEN_NODE

#define BATEN_NODE   DT_ALIAS(baten)

check if BATEN_NODE is okay in devicetree

check if baten alias is okay in devicetree and set BATEN_NODE to baten alias

Definition at line 171 of file main.c.

◆ CON_STATUS_LED

#define CON_STATUS_LED   DK_LED2

LED to indicate the BLE connection status.

Definition at line 69 of file main.c.

◆ DEVICE_NAME

#define DEVICE_NAME   CONFIG_BT_DEVICE_NAME

device name defined in prj.conf

Definition at line 63 of file main.c.

◆ DEVICE_NAME_LEN

#define DEVICE_NAME_LEN   (sizeof(DEVICE_NAME) - 1)

length of device name

Definition at line 64 of file main.c.

◆ HDC2080RDY_NODE

#define HDC2080RDY_NODE    DT_ALIAS(hdc2080rdy)

check if HDC2080RDY_NODE is okay in devicetree

check if hdc2080rdy alias is okay in devicetree and set HDC2080RDY_NODE to hdc2080rdy alias

Definition at line 180 of file main.c.

◆ KEY_PASSKEY_ACCEPT

#define KEY_PASSKEY_ACCEPT   DK_BTN1_MSK

Button used to accept a pairing passkey.

Definition at line 71 of file main.c.

◆ KEY_PASSKEY_REJECT

#define KEY_PASSKEY_REJECT   DK_BTN2_MSK

Button used to reject a pairing passkey.

Definition at line 72 of file main.c.

◆ PRIORITY

#define PRIORITY   7

priority for main thread

Definition at line 61 of file main.c.

◆ RUN_LED_BLINK_INTERVAL

#define RUN_LED_BLINK_INTERVAL   1000

interval for blinking the run status LED in milliseconds

Definition at line 67 of file main.c.

◆ RUN_STATUS_LED

#define RUN_STATUS_LED   DK_LED1

LED to indicate the application is running.

Definition at line 66 of file main.c.

◆ STACKSIZE

#define STACKSIZE   CONFIG_BT_NUS_THREAD_STACK_SIZE

stack size for main thread

Definition at line 60 of file main.c.

◆ SW2_NODE

#define SW2_NODE   DT_ALIAS(sw2input)

check if SW2_NODE is okay in devicetree

check if sw2input alias is okay in devicetree and set SW2_NODE to sw2input alias

Definition at line 191 of file main.c.

◆ UART_BUF_SIZE

#define UART_BUF_SIZE   CONFIG_BT_NUS_UART_BUFFER_SIZE

size of the buffer for UART data

Definition at line 74 of file main.c.

◆ UART_WAIT_FOR_BUF_DELAY

#define UART_WAIT_FOR_BUF_DELAY   K_MSEC(50)

delay to wait for UART buffer availability in milliseconds

Definition at line 75 of file main.c.

◆ UART_WAIT_FOR_RX

#define UART_WAIT_FOR_RX   CONFIG_BT_NUS_UART_RX_WAIT_TIME

Definition at line 76 of file main.c.

Enumeration Type Documentation

◆ COMMANDS

enum COMMANDS

Valid command indexes.

Enumerator
CR 
NONE 

Definition at line 378 of file main.c.

Function Documentation

◆ adc_raw_to_voltage()

static float adc_raw_to_voltage ( uint16_t raw)
inlinestatic

Convert raw ADC reading to voltage in volts.

Parameters
rawRaw ADC sample value (integer from 0..ADC_MAX_VAL)
Returns
Voltage corresponding to the raw ADC value in volts (float)

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.

Here is the caller graph for this function:

◆ adc_sample()

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

Returns
ADC_RESOLUTION value on ADC_1ST_CHANNEL_ID if pass or -1 if failed

Definition at line 343 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adv_work_handler()

static void adv_work_handler ( struct k_work * work)
static

Handler for the advertising work item.

Parameters
workpointer to the work item

Definition at line 748 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ advertising_start()

static void advertising_start ( void )
static

Start the advertising process.

Definition at line 764 of file main.c.

Here is the caller graph for this function:

◆ ble_write_thread()

void ble_write_thread ( void )

Definition at line 2052 of file main.c.

Here is the caller graph for this function:

◆ bleCommand()

uint8_t bleCommand ( uint8_t * data)

Executes a BLE command.

Parameters
dataPointer to the command data
Returns
TRUE if the command is executed successfully, FALSE otherwise This function takes a pointer to the command data received over BLE, processes it, and executes the corresponding command. It includes debug print statements to indicate the received command and its length. The function can be expanded to include specific command parsing and execution logic based on the content of the data. In the provided code, there is a placeholder for command execution, and the actual implementation can be added as needed. The function currently returns TRUE to indicate that the command has been processed, but this can be modified to return FALSE if the command is invalid or if there is an error during execution.

Definition at line 2239 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BT_CONN_CB_DEFINE()

BT_CONN_CB_DEFINE ( conn_callbacks )

Bluetooth connection callbacks.

Here is the call graph for this function:

◆ bt_receive_cb()

static void bt_receive_cb ( struct bt_conn * conn,
const uint8_t *const data,
uint16_t len )
static

Callback function for receiving data over Bluetooth.

Parameters
connpointer to the Bluetooth connection structure
datapointer to the received data
lenlength of the received data

Definition at line 982 of file main.c.

Here is the call graph for this function:

◆ configure_gpio()

static void configure_gpio ( void )
static

Definition at line 1166 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ connected()

static void connected ( struct bt_conn * conn,
uint8_t err )
static

Callback function for when a Bluetooth connection is established.

Parameters
connpointer to the Bluetooth connection structure
errerror 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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disconnected()

static void disconnected ( struct bt_conn * conn,
uint8_t reason )
static

Callback function for when a Bluetooth connection is lost.

Parameters
connpointer to the Bluetooth connection structure
reasonreason 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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ error()

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.

Here is the caller graph for this function:

◆ hdc2080_rdy_seen()

void hdc2080_rdy_seen ( const struct device * dev,
struct gpio_callback * cb,
uint32_t pins )

Callback function for HDC2080 RDY pin.

Parameters
devPointer to the device structure
cbPointer to the GPIO callback structure
pinsGPIO 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.

Here is the caller graph for this function:

◆ hdc2080RdyHandler()

void hdc2080RdyHandler ( void )

< check if i2c_0 alias is okay in devicetree

< check if i2c_1 alias is okay in devicetree < check if i2c_2 alias is okay in devicetree

Definition at line 1742 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_i2c()

int init_i2c ( )

Initializes the I2C interface.

Returns
TRUE if successful, FALSE otherwise This function initializes the I2C interface by configuring the I2C peripheral with the specified speed and mode. It checks if the I2C device is ready and then configures it. If any step fails, it returns FALSE; otherwise, it returns TRUE to indicate successful initialization.

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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ K_FIFO_DEFINE() [1/2]

static K_FIFO_DEFINE ( fifo_uart_rx_data )
static

FIFO queue for UART data that has been received, used to manage incoming UART data and ensure proper processing and flow control

◆ K_FIFO_DEFINE() [2/2]

static K_FIFO_DEFINE ( fifo_uart_tx_data )
static

FIFO queue for UART data to be transmitted, used to manage outgoing UART data and ensure proper sequencing and flow control

◆ K_SEM_DEFINE() [1/5]

static K_SEM_DEFINE ( ble_init_ok ,
0 ,
1  )
static

semaphore to indicate when BLE initialization is complete and the system is ready for BLE operations

◆ K_SEM_DEFINE() [2/5]

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() [3/5]

K_SEM_DEFINE ( hdc_rdy_seen ,
0 ,
1  )

semaphore to indicate when the HDC2080 sensor ready signal has been seen

◆ K_SEM_DEFINE() [4/5]

K_SEM_DEFINE ( sw2_rdy_seen ,
0 ,
1  )

semaphore to indicate when the SW2 button press has been seen

◆ K_SEM_DEFINE() [5/5]

K_SEM_DEFINE ( uart_rx_disabled_sem ,
0 ,
1  )

semaphore to indicate UART RX is disabled

◆ K_THREAD_DEFINE()

K_THREAD_DEFINE ( ble_write_thread_id ,
STACKSIZE ,
ble_write_thread ,
NULL ,
NULL ,
NULL ,
PRIORITY ,
0 ,
0  )
Here is the call graph for this function:

◆ LOG_MODULE_REGISTER()

LOG_MODULE_REGISTER ( main ,
LOG_LEVEL_DBG  )
Here is the call graph for this function:

◆ main()

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.

Returns
TRUE if passed, FALSE if failed

Definition at line 1199 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ myPrintk()

int myPrintk ( char *restrict fmt,
... )

Definition at line 2310 of file main.c.

Here is the caller graph for this function:

◆ myPrintkE()

int myPrintkE ( char *restrict fmt,
... )

prints an error message to the UART

Parameters
fmtformat string
Returns
number of characters printed This function prints an error message to the UART in red color. It takes a format string and additional arguments, similar to printf, and formats the message accordingly. The function uses ANSI escape codes to set the text color to red before printing the message and resets it back to default afterward. The number of characters printed is returned as an integer.

Definition at line 2452 of file main.c.

Here is the caller graph for this function:

◆ myPrintkI()

int myPrintkI ( char *restrict fmt,
... )

prints an information message to the UART

Parameters
fmtformat string
Returns
number of characters printed This function prints an information message to the UART in green color. It takes a format string and additional arguments, similar to printf, and formats the message accordingly. The function uses ANSI escape codes to set the text color to green before printing the message and resets it back to default afterward. The number of characters printed is returned as an integer.

Definition at line 2386 of file main.c.

Here is the caller graph for this function:

◆ myPrintkS()

int myPrintkS ( char *restrict fmt,
... )

prints a status message to the UART

Parameters
fmtformat string
Returns
number of characters printed This function prints a status message to the UART in cyan color. It takes a format string and additional arguments, similar to printf, and formats the message accordingly. The function uses ANSI escape codes to set the text color to cyan before printing the message and resets it back to default afterward. The number of characters printed is returned as an integer.

Definition at line 2419 of file main.c.

Here is the caller graph for this function:

◆ myPrintkW()

int myPrintkW ( char *restrict fmt,
... )

prints a warning message to the UART

Parameters
fmtformat string
Returns
number of characters printed This function prints a warning message to the UART in yellow color. It takes a format string and additional arguments, similar to printf, and formats the message accordingly. The function uses ANSI escape codes to set the text color to yellow before printing the message and resets it back to default afterward. The number of characters printed is returned as an integer.

Definition at line 2353 of file main.c.

Here is the caller graph for this function:

◆ recycled_cb()

static void recycled_cb ( void )
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rescan()

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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sw2_seen()

void sw2_seen ( const struct device * dev,
struct gpio_callback * cb,
uint32_t pins )

Callback function for SW2 button.

Parameters
devPointer to the device structure
cbPointer to the GPIO callback structure
pinsGPIO 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.

Here is the caller graph for this function:

Variable Documentation

◆ ad

struct bt_data ad[2]
static
Initial value:
= {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
}
#define DEVICE_NAME_LEN
length of device name
Definition main.c:64
#define DEVICE_NAME
device name defined in prj.conf
Definition main.c:63

Definition at line 416 of file main.c.

◆ adc_dev

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

Definition at line 254 of file main.c.

◆ adc_volts_per_step

const float adc_volts_per_step = (float)ADC_REF_MV / ((float)ADC_MAX_VAL * (float)ADC_DIV_MV)
static

Definition at line 318 of file main.c.

◆ adv_work

struct k_work adv_work
static

work item for handling BLE advertising operations, used to schedule advertising tasks in the system workqueue

Definition at line 121 of file main.c.

◆ auth_conn

struct bt_conn* auth_conn
static

pointer to the BLE connection structure that is currently undergoing authentication, used to manage pairing and security procedures separately from the active connection used for data transfer

Definition at line 118 of file main.c.

◆ baten

const struct gpio_dt_spec baten
Initial value:
=
GPIO_DT_SPEC_GET_OR(BATEN_NODE, gpios, {0})
#define BATEN_NODE
check if BATEN_NODE is okay in devicetree
Definition main.c:171

get the GPIO specification for the BATEN pin defined by BATEN_NODE

Definition at line 175 of file main.c.

◆ bleLoopCount

int bleLoopCount = 0

Definition at line 1182 of file main.c.

◆ conn_auth_callbacks

struct bt_conn_auth_cb conn_auth_callbacks
static

Definition at line 971 of file main.c.

◆ conn_auth_info_callbacks

struct bt_conn_auth_info_cb conn_auth_info_callbacks
static

Definition at line 972 of file main.c.

◆ current_conn

struct bt_conn* current_conn
static

pointer to the current BLE connection structure, used to keep track of the active BLE connection for sending data and managing connection state

Definition at line 116 of file main.c.

◆ device_name

char device_name[40]

buffer to hold the device name string, used for BLE advertising and identification

Definition at line 371 of file main.c.

◆ device_name_len

uint8_t device_name_len

length of the device name string, used to ensure proper formatting and transmission in BLE advertising packets

Definition at line 372 of file main.c.

◆ hdc2080

const struct gpio_dt_spec hdc2080
static
Initial value:
=
GPIO_DT_SPEC_GET_OR(HDC2080RDY_NODE, gpios, {0})
#define HDC2080RDY_NODE
check if HDC2080RDY_NODE is okay in devicetree
Definition main.c:180

get the GPIO specification for the HDC2080 ready pin defined by HDC2080RDY_NODE

Definition at line 185 of file main.c.

◆ hdc2080_cb_data

struct gpio_callback hdc2080_cb_data
static

GPIO callback structure for handling HDC2080 ready pin interrupts.

Definition at line 187 of file main.c.

◆ i2c_dev

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

Definition at line 106 of file main.c.

◆ m_1st_channel_cfg

const struct adc_channel_cfg m_1st_channel_cfg
static
Initial value:
= {
.gain = ADC_GAIN,
.reference = ADC_REFERENCE,
.acquisition_time = ADC_ACQUISITION_TIME,
.channel_id = ADC_1ST_CHANNEL_ID,
}
#define ADC_GAIN
Definition main.c:221
#define ADC_REFERENCE
reference voltage for ADC readings, set to internal reference for stable and consistent measurements
Definition main.c:224
#define ADC_1ST_CHANNEL_ID
channel ID for the first ADC channel to read, set to 0 for the first channel (e.g....
Definition main.c:229
#define ADC_ACQUISITION_TIME
Definition main.c:226

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.

Definition at line 301 of file main.c.

◆ m_sample_buffer

int16_t m_sample_buffer[ADC_BUFFER_SIZE]
static

buffer to receive ADC data from configured channels

Definition at line 312 of file main.c.

◆ nus_cb

struct bt_nus_cb nus_cb
static
Initial value:
= {
.received = bt_receive_cb,
}
static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data, uint16_t len)
Callback function for receiving data over Bluetooth.
Definition main.c:982

Bluetooth NUS callbacks This structure defines the callbacks for Bluetooth NUS events, including data reception.

Definition at line 1095 of file main.c.

◆ sd

const struct bt_data sd[]
static
Initial value:
= {
BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
}

Definition at line 421 of file main.c.

◆ sw2

const struct gpio_dt_spec sw2
static
Initial value:
=
GPIO_DT_SPEC_GET_OR(SW2_NODE, gpios, {0})
#define SW2_NODE
check if SW2_NODE is okay in devicetree
Definition main.c:191

get the GPIO specification for the SW2 button pin defined by SW2_NODE

Definition at line 195 of file main.c.

◆ sw2_cb_data

struct gpio_callback sw2_cb_data
static

GPIO callback structure for handling SW2 button pin interrupts.

Definition at line 197 of file main.c.

◆ validCmds

char const* validCmds[]
Initial value:
=
{
"cr",
"END",
}

array of valid commands - case insensitive

Definition at line 383 of file main.c.