L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
mysensors_base.h File Reference
#include "mysensors_def.h"
#include "tmphm_sht45.h"
#include "amb_tsl2591.h"
#include "bar_ils22qs.h"
#include "bar_bmp585.h"
#include "scd41.h"
#include "sps30.h"
#include <stdint.h>
Include dependency graph for mysensors_base.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  systemParams_t
 System configuration parameters – persisted in NFC EEPROM at address 0. The structure contains all modifiable variables/settings of system, can be read/writa via NFC tool or LoRa special read/write process The structure is 90 bytes (SZ_SYSTEMPARAMS) with a CRC8 byte at the end. Fields marked RO are written by the firmware; fields marked RW can be updated by a phone app via the NFC EEPROM or via LoRa special process. More...

Macros

#define SYSTEMPARAMS_SIGN   "MTTV"
 4-byte ASCII signature stored at offset 0 of systemParams_t to validate data integrity.
#define SZ_SYSTEMPARAMS   90
 Total size in bytes of the systemParams_t structure stored on the NFC tag and send via LoRa - the size is used for check of struct systemParams_t and it doens't match, the Error_Handler() is called.
#define SYSTEMPARAMS_STRUCTVER   1
 Version of the systemParams_t layout; increment when fields are added or reordered.
#define SYSTEMPARAMS_APPVER00   0x01
 Application firmware minor version number (plain integer).
#define SYSTEMPARAMS_APPVER01   0x01
 Application firmware major version number (plain integer) - check of validity struct together with SYSTEMPARAMS_SIGN.
#define SYSTEMPARAMS_DEFAULTALTITUDE   300
 The default sensor altitue.
#define SYSTEMPARAMS_MINSENSORTIME   30000
 Minimum allowed sensor measurement interval in milliseconds (30 seconds).
#define SYSTEMPARAMS_DEFAULTSENSORTIME   60*1000*10
 Default sensor measurement interval in milliseconds (10 minutes).
#define SYSTEMPARAMS_MAXSENSORTIME   60*1000*60*18
 Maximum allowed sensor measurement interval in milliseconds (18 hour).
#define SYSTEMPARAMS_MINTIME2OFF   10000
 Minimum idle time in milliseconds before the device enters OFF (standby) mode. Values below ~10 s are too short because the LoRaWAN stack may still be finishing a transmission when the timer fires.
#define SYSTEMPARAMS_MINLORAWANPORT   1
 Minimum valid LoRaWAN application port number (port 0 is reserved by the stack).
#define SYSTEMPARAMS_DEFAULTLORAWANPORT   2
 Default LoRaWAN application port used for sensor uplinks.
#define SYSTEMPARAMS_MAXLORAWANPORT   223
 Maximum valid LoRaWAN application port number (ports 224–255 are reserved).
#define SYSTEM_APP_EUI   00,00,00,00,00,00,00,00
 Default LoRaWAN JoinEUI / AppEUI (8 bytes, all zeros – overridden via NFC).
#define SYSTEM_APP_KEY   AB,CD,EF,01,02,03,10,11,12,05,06,07,EF,EF,EF,EF
 Default LoRaWAN AppKey (16 bytes – overridden via NFC before deployment).
#define SYSTEM_APP_DevEUI0   0x45
 First byte of the 3-byte OUI prefix used in the auto-generated DevEUI.
#define SYSTEM_APP_DevEUI1   0x87
 Second byte of the 3-byte OUI prefix used in the auto-generated DevEUI.
#define SYSTEM_APP_DevEUI2   0xF2
 Third byte of the 3-byte OUI prefix used in the auto-generated DevEUI.
#define SIZE_SHT45   sizeof(tmphm_sht45_t)
 Byte size of tmphm_sht45_t in the packed sensor record.
#define SIZE_TSL2591   sizeof(amb_tsl2591_t)
 Byte size of amb_tsl2591_t in the packed sensor record.
#define SIZE_ILS22QS   sizeof(bar_ils22qs_t)
 Byte size of bar_ils22qs_t in the packed sensor record.
#define SIZE_BMP585   sizeof(bar_bmp585_t)
 Byte size of bar_bmp585_t in the packed sensor record.
#define SIZE_SCD41   sizeof(scd41_t)
 Byte size of scd41_t in the packed sensor record.
#define SIZE_SPS30   sizeof(sps30_t)
 Byte size of sps30_t in the packed sensor record.
#define SENSORS_DATASIZE
 Total size in bytes of one packed sensor data record. Computed as the sum of all enabled sensor struct sizes. Stored as Data_ItemSize in mems_MainBlock_t; changing the set of active sensors changes this value and requires a memory reset. The struct size contains 2bytes - represent the timeout of sensor measuring, in 0.1minutes (10min -> value 100, 0.5min value 50).

Enumerations

enum  systemParams_Sensors_t {
  SYSPARAM_TEMP_HUM = 0x01 , SYSPARAM_BAR = 0x02 , SYSPARAM_AMBIENT = 0x04 , SYSPARAM_CO2 = 0x08 ,
  SYSPARAM_PARTICULAR = 0x10
}
 Bitmask of sensor modules that are enabled / available for data collection. Used in systemParams_t.AvailableSensors; individual bits can be ORed together. More...

Functions

uint8_t * systemParams_getAppDevEUI ()
 Return a pointer to the DevEUI stored in _systemParams. The DevEUI is auto-generated from the chip UID during initialisation and is read-only from the phone app's perspective.
uint8_t * systemParams_getAppEUI ()
 Return a pointer to the AppEUI (JoinEUI) stored in _systemParams. Used by the LoRaWAN stack during the OTAA join procedure.
uint8_t * systemParams_getAppKey ()
 Return a pointer to the AppKey stored in _systemParams. Used by the LoRaWAN stack to derive session keys during OTAA join.
uint8_t systemParams_CalculateCrc (uint8_t *data, uint8_t len)
 Calculate a CRC-8 checksum (Sensirion polynomial) over a byte buffer. Used to validate sensor I2C response packets as well as the CRC field of systemParams_t.
uint8_t systemParams_CheckCRC (const systemParams_t *par)
 Verify the CRC field of a systemParams_t structure.
void systemParams_SetCRCSystemParams ()
 Recalculate and store the CRC in _systemParams.Crc. Call this after modifying any field of _systemParams before writing the structure to the NFC EEPROM.
void systemParams_Correction ()
 Clamp all RW fields of _systemParams to their valid ranges. Called after loading parameters from NFC to ensure no out-of-range values are used (e.g. port numbers, timeouts, data rates).
uint8_t systemParams_IsSensorAvaiable (systemParams_Sensors_t sensorType)
 Check whether a particular sensor is enabled in _systemParams.AvailableSensors.
void systemParams_Log (const char *info)
 Log the current _systemParams fields to the UART debug output.
void sensorsBase_Init ()
 Initialization of base sensor data structure.
void sensorsBase_StartTimerToOff ()
 start timer to off the function is called from PWR_EnterStopMode - timer is started
void sensorsBase_StopTimerToOff ()
 stop timer to off the function is called from PWR_ExitStopMode - the timer value is checked and the PWR_ExitStopMode was invoked from this times, system goes to OFF mode setModeDevice(MODEDEVICE_OFF)

Variables

int8_t _tryInit
systemParams_t _systemParams

Macro Definition Documentation

◆ SENSORS_DATASIZE

#define SENSORS_DATASIZE
Value:
(\
sizeof(uint16_t) \
)
#define SIZE_ILS22QS
Byte size of bar_ils22qs_t in the packed sensor record.
#define SIZE_SCD41
Byte size of scd41_t in the packed sensor record.
#define SIZE_SHT45
Byte size of tmphm_sht45_t in the packed sensor record.
#define SIZE_SPS30
Byte size of sps30_t in the packed sensor record.
#define SIZE_BMP585
Byte size of bar_bmp585_t in the packed sensor record.
#define SIZE_TSL2591
Byte size of amb_tsl2591_t in the packed sensor record.

Total size in bytes of one packed sensor data record. Computed as the sum of all enabled sensor struct sizes. Stored as Data_ItemSize in mems_MainBlock_t; changing the set of active sensors changes this value and requires a memory reset. The struct size contains 2bytes - represent the timeout of sensor measuring, in 0.1minutes (10min -> value 100, 0.5min value 50).

Definition at line 329 of file mysensors_base.h.

Referenced by sensors_WriteFromBckToDataBlock().

◆ SYSTEM_APP_DevEUI0

#define SYSTEM_APP_DevEUI0   0x45

First byte of the 3-byte OUI prefix used in the auto-generated DevEUI.

Definition at line 79 of file mysensors_base.h.

Referenced by getDevEuiFromHardware().

◆ SYSTEM_APP_DevEUI1

#define SYSTEM_APP_DevEUI1   0x87

Second byte of the 3-byte OUI prefix used in the auto-generated DevEUI.

Definition at line 81 of file mysensors_base.h.

Referenced by getDevEuiFromHardware().

◆ SYSTEM_APP_DevEUI2

#define SYSTEM_APP_DevEUI2   0xF2

Third byte of the 3-byte OUI prefix used in the auto-generated DevEUI.

Definition at line 83 of file mysensors_base.h.

Referenced by getDevEuiFromHardware().

◆ SYSTEM_APP_EUI

#define SYSTEM_APP_EUI   00,00,00,00,00,00,00,00

Default LoRaWAN JoinEUI / AppEUI (8 bytes, all zeros – overridden via NFC).

Definition at line 73 of file mysensors_base.h.

◆ SYSTEM_APP_KEY

#define SYSTEM_APP_KEY   AB,CD,EF,01,02,03,10,11,12,05,06,07,EF,EF,EF,EF

Default LoRaWAN AppKey (16 bytes – overridden via NFC before deployment).

Definition at line 76 of file mysensors_base.h.

◆ SYSTEMPARAMS_APPVER00

#define SYSTEMPARAMS_APPVER00   0x01

Application firmware minor version number (plain integer).

Definition at line 37 of file mysensors_base.h.

◆ SYSTEMPARAMS_APPVER01

#define SYSTEMPARAMS_APPVER01   0x01

Application firmware major version number (plain integer) - check of validity struct together with SYSTEMPARAMS_SIGN.

Definition at line 40 of file mysensors_base.h.

Referenced by nfc_ReadSystemParamsToBck().

◆ SYSTEMPARAMS_DEFAULTALTITUDE

#define SYSTEMPARAMS_DEFAULTALTITUDE   300

The default sensor altitue.

Definition at line 43 of file mysensors_base.h.

◆ SYSTEMPARAMS_DEFAULTLORAWANPORT

#define SYSTEMPARAMS_DEFAULTLORAWANPORT   2

Default LoRaWAN application port used for sensor uplinks.

Definition at line 66 of file mysensors_base.h.

◆ SYSTEMPARAMS_DEFAULTSENSORTIME

#define SYSTEMPARAMS_DEFAULTSENSORTIME   60*1000*10

Default sensor measurement interval in milliseconds (10 minutes).

Definition at line 49 of file mysensors_base.h.

◆ SYSTEMPARAMS_MAXLORAWANPORT

#define SYSTEMPARAMS_MAXLORAWANPORT   223

Maximum valid LoRaWAN application port number (ports 224–255 are reserved).

Definition at line 69 of file mysensors_base.h.

Referenced by systemParams_Correction().

◆ SYSTEMPARAMS_MAXSENSORTIME

#define SYSTEMPARAMS_MAXSENSORTIME   60*1000*60*18

Maximum allowed sensor measurement interval in milliseconds (18 hour).

Definition at line 52 of file mysensors_base.h.

Referenced by systemParams_Correction().

◆ SYSTEMPARAMS_MINLORAWANPORT

#define SYSTEMPARAMS_MINLORAWANPORT   1

Minimum valid LoRaWAN application port number (port 0 is reserved by the stack).

Definition at line 63 of file mysensors_base.h.

Referenced by systemParams_Correction().

◆ SYSTEMPARAMS_MINSENSORTIME

#define SYSTEMPARAMS_MINSENSORTIME   30000

Minimum allowed sensor measurement interval in milliseconds (30 seconds).

Definition at line 46 of file mysensors_base.h.

Referenced by systemParams_Correction().

◆ SYSTEMPARAMS_MINTIME2OFF

#define SYSTEMPARAMS_MINTIME2OFF   10000

Minimum idle time in milliseconds before the device enters OFF (standby) mode. Values below ~10 s are too short because the LoRaWAN stack may still be finishing a transmission when the timer fires.

Definition at line 59 of file mysensors_base.h.

Referenced by sensorsBase_StartTimerToOff(), and sensorsBase_StopTimerToOff().

◆ SYSTEMPARAMS_SIGN

#define SYSTEMPARAMS_SIGN   "MTTV"

4-byte ASCII signature stored at offset 0 of systemParams_t to validate data integrity.

Definition at line 26 of file mysensors_base.h.

Referenced by nfc_ReadSystemParamsToBck().

◆ SYSTEMPARAMS_STRUCTVER

#define SYSTEMPARAMS_STRUCTVER   1

Version of the systemParams_t layout; increment when fields are added or reordered.

Definition at line 34 of file mysensors_base.h.

◆ SZ_SYSTEMPARAMS

#define SZ_SYSTEMPARAMS   90

Total size in bytes of the systemParams_t structure stored on the NFC tag and send via LoRa - the size is used for check of struct systemParams_t and it doens't match, the Error_Handler() is called.

Definition at line 31 of file mysensors_base.h.

Referenced by sensorsBase_Init().

Enumeration Type Documentation

◆ systemParams_Sensors_t

Bitmask of sensor modules that are enabled / available for data collection. Used in systemParams_t.AvailableSensors; individual bits can be ORed together.

Enumerator
SYSPARAM_TEMP_HUM 

Temperature and humidity sensor (SHT45)

SYSPARAM_BAR 

Barometric pressure sensor (ILPS22QS or BMP585)

SYSPARAM_AMBIENT 

Ambient light sensor (TSL2591)

SYSPARAM_CO2 

CO2 / temperature / humidity sensor (SCD41)

SYSPARAM_PARTICULAR 

Particular matter sensor (SPS30)

Definition at line 109 of file mysensors_base.h.

Function Documentation

◆ sensorsBase_Init()

void sensorsBase_Init ( )

Initialization of base sensor data structure.

Definition at line 195 of file mysensors_base.c.

References Error_Handler(), systemParams_getAppDevEUI(), systemParams_SetCRCSystemParams(), SZ_SYSTEMPARAMS, and writeLog().

Referenced by main().

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

◆ sensorsBase_StartTimerToOff()

void sensorsBase_StartTimerToOff ( )

start timer to off the function is called from PWR_EnterStopMode - timer is started

Definition at line 206 of file mysensors_base.c.

References _timerTick, _timerToOff, HAL_GetTick(), OnTimerToOff(), and SYSTEMPARAMS_MINTIME2OFF.

Referenced by PWR_EnterStopMode().

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

◆ sensorsBase_StopTimerToOff()

void sensorsBase_StopTimerToOff ( )

stop timer to off the function is called from PWR_ExitStopMode - the timer value is checked and the PWR_ExitStopMode was invoked from this times, system goes to OFF mode setModeDevice(MODEDEVICE_OFF)

Definition at line 214 of file mysensors_base.c.

References _timerTick, _timerToOff, HAL_GetTick(), MODEDEVICE_OFF, sensors_Retiming(), setModeDevice(), and SYSTEMPARAMS_MINTIME2OFF.

Referenced by PWR_ExitStopMode().

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

◆ systemParams_CalculateCrc()

uint8_t systemParams_CalculateCrc ( uint8_t * data,
uint8_t len )

Calculate a CRC-8 checksum (Sensirion polynomial) over a byte buffer. Used to validate sensor I2C response packets as well as the CRC field of systemParams_t.

Parameters
dataPointer to the data buffer.
lenNumber of bytes to include in the calculation.
Returns
Computed CRC-8 byte.

Definition at line 167 of file mysensors_base.c.

Referenced by getDevEuiFromHardware(), scd41_IsDataReady(), scd41_Read(), scd41_WriteWithCRC(), sps30_GetAutoCleanInterval(), sps30_On(), sps30_Read(), sps30_SetAutoCleanInterval(), systemParams_CheckCRC(), and systemParams_SetCRCSystemParams().

Here is the caller graph for this function:

◆ systemParams_CheckCRC()

uint8_t systemParams_CheckCRC ( const systemParams_t * par)

Verify the CRC field of a systemParams_t structure.

Parameters
parPointer to the systemParams_t to check.
Return values
1– CRC is valid (structure is intact).
0– CRC mismatch (structure is corrupt or uninitialised).

Definition at line 189 of file mysensors_base.c.

References systemParams_t::Crc, and systemParams_CalculateCrc().

Referenced by nfc_ReadSystemParamsToBck().

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

◆ systemParams_Correction()

void systemParams_Correction ( )

Clamp all RW fields of _systemParams to their valid ranges. Called after loading parameters from NFC to ensure no out-of-range values are used (e.g. port numbers, timeouts, data rates).

Definition at line 226 of file mysensors_base.c.

References _systemParams, CLAMP, SYSTEMPARAMS_MAXLORAWANPORT, SYSTEMPARAMS_MAXSENSORTIME, SYSTEMPARAMS_MINLORAWANPORT, and SYSTEMPARAMS_MINSENSORTIME.

Referenced by nfc_Init().

Here is the caller graph for this function:

◆ systemParams_getAppDevEUI()

uint8_t * systemParams_getAppDevEUI ( )

Return a pointer to the DevEUI stored in _systemParams. The DevEUI is auto-generated from the chip UID during initialisation and is read-only from the phone app's perspective.

Returns
Pointer to the 8-byte DevEUI array inside _systemParams.

Definition at line 140 of file mysensors_base.c.

References _systemParams, GetDevAddr(), and getDevEuiFromHardware().

Referenced by LoRaWAN_Init(), and sensorsBase_Init().

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

◆ systemParams_getAppEUI()

uint8_t * systemParams_getAppEUI ( )

Return a pointer to the AppEUI (JoinEUI) stored in _systemParams. Used by the LoRaWAN stack during the OTAA join procedure.

Returns
Pointer to the 8-byte AppEUI array inside _systemParams.

Definition at line 154 of file mysensors_base.c.

References _systemParams.

Referenced by LoRaWAN_Init().

Here is the caller graph for this function:

◆ systemParams_getAppKey()

uint8_t * systemParams_getAppKey ( )

Return a pointer to the AppKey stored in _systemParams. Used by the LoRaWAN stack to derive session keys during OTAA join.

Returns
Pointer to the 16-byte AppKey array inside _systemParams.

Definition at line 159 of file mysensors_base.c.

References _systemParams.

Referenced by LoRaWAN_Init().

Here is the caller graph for this function:

◆ systemParams_IsSensorAvaiable()

uint8_t systemParams_IsSensorAvaiable ( systemParams_Sensors_t sensorType)

Check whether a particular sensor is enabled in _systemParams.AvailableSensors.

Parameters
sensorTypeOne of the systemParams_Sensors_t bitmask values.
Return values
1– sensor is marked as available and should be polled.
0– sensor is disabled in the configuration.

Definition at line 238 of file mysensors_base.c.

References _systemParams.

Referenced by sensors_Init(), sensors_OnOff(), sensors_Read(), and sensors_Service().

Here is the caller graph for this function:

◆ systemParams_Log()

void systemParams_Log ( const char * info)

Log the current _systemParams fields to the UART debug output.

Parameters
infoOptional prefix string printed before the dump (e.g. "LOAD" or "SAVE").

Definition at line 271 of file mysensors_base.c.

References _systemParams, SYS_WR, SYS_WR_ARR, SYS_WR_STR, and writeLog().

Referenced by main().

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

◆ systemParams_SetCRCSystemParams()

void systemParams_SetCRCSystemParams ( )

Recalculate and store the CRC in _systemParams.Crc. Call this after modifying any field of _systemParams before writing the structure to the NFC EEPROM.

Definition at line 184 of file mysensors_base.c.

References _systemParams, and systemParams_CalculateCrc().

Referenced by nfc_WriteSystemParams(), and sensorsBase_Init().

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

Variable Documentation

◆ _systemParams

◆ _tryInit

int8_t _tryInit
extern

Flag used by xxx_Is() calls; when set to 1, the Is function attempts to (re-)initialise the sensor

Definition at line 60 of file mysensors_base.c.

Referenced by nfc_FactoryReset(), nfc_ReadSystemParams(), nfc_ReadSystemParamsToBck(), nfc_WriteSystemParams(), and sensors_Read().