L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
mymems.c File Reference
#include "mymems.h"
#include "main.h"
#include "flash_at25.h"
#include "spi.h"
#include "rtc.h"
#include "mysensors_base.h"
#include "mysensors.h"
#include "nfc.h"
#include "sys_app.h"
#include "adc_if.h"
#include <string.h>
Include dependency graph for mymems.c:

Go to the source code of this file.

Macros

#define MEMS_SIGN   "MTK0001"
#define MEMS_SIGN_BLOCK   "TM"
#define MEMS_MAINBLOCK_ADDR   0
#define MEMS_DATA_BITS_BOUNDARY   5
#define INX_GET(nInx, nMax)

Functions

static void mens_MainBlockInit ()
void mems_Init ()
 Initialise NFC and external flash; load the main block and system parameters. Verifies the flash signature and resets the queue if the block size has changed.
HAL_StatusTypeDef mems_ReadMainBlock ()
 
  • reading of main block from flash. If there is not valid MEMS_SIGN, the structure is initialize to 0 The main block it stored on address 0

HAL_StatusTypeDef mems_WriteMainBlock ()
 writing the main block on flash. The main block is stored on address 0
HAL_StatusTypeDef mems_Reset ()
 memory reset - remove sign from chip
static uint32_t mems_SetOffset (uint32_t offset, int16_t count)
 move the offset in circular queue
static uint32_t mems_GetAddr (uint32_t offset)
mems_DataBlock_tmems_InicBuffer (mems_DataType_t type)
 Initialize the work buffer - _memsDataBlock.
HAL_StatusTypeDef mems_AddData (const void *data, mems_DataType_t type, uint8_t size)
 write new data of sensor at the end of queue, if is no space, the most older data start to rewrites
HAL_StatusTypeDef mems_GetLastData (mems_DataBlock_t **memBlock, uint8_t inxFromEnd)
 Get the last sensor data if exists. The data and mem block is stored in work buffer, which can be modify by other mems_xxxx function, temporary valid only.
HAL_StatusTypeDef mems_Check (int8_t correct)
 Check the memory consist.
void mems_RemoveLastData (uint8_t fromEndTo)
 remove last data
uint8_t mems_WriteToSystemParams ()
 write data from memory to systemParams
void nfc_st25r3_OnMailboxData (uint8_t *data, uint16_t len)
uint32_t getBackUpRegister (uint32_t backupRegister)
 Read a value from an RTC backup register.
void setBackUpRegister (uint32_t backupRegister, uint32_t value)
 Write a value to an RTC backup register.

Variables

mems_MainBlock_t _memsMainBlock = {}
 Global instance of the main flash control block; loaded by mems_ReadMainBlock().
static flash_at25CS_t _flash
static mems_DataBlock_t _memsDataBlock = {}

Macro Definition Documentation

◆ INX_GET

#define INX_GET ( nInx,
nMax )
Value:
(((nInx)>=0)?((nInx)%(nMax)):((nMax)-((-(nInx))%(nMax))))

Definition at line 25 of file mymems.c.

Referenced by mems_SetOffset().

◆ MEMS_DATA_BITS_BOUNDARY

#define MEMS_DATA_BITS_BOUNDARY   5

Definition at line 23 of file mymems.c.

Referenced by mens_MainBlockInit().

◆ MEMS_MAINBLOCK_ADDR

#define MEMS_MAINBLOCK_ADDR   0

Definition at line 22 of file mymems.c.

Referenced by mems_ReadMainBlock(), mems_Reset(), mems_WriteMainBlock(), and mens_MainBlockInit().

◆ MEMS_SIGN

#define MEMS_SIGN   "MTK0001"

Definition at line 20 of file mymems.c.

Referenced by mems_ReadMainBlock(), and mens_MainBlockInit().

◆ MEMS_SIGN_BLOCK

#define MEMS_SIGN_BLOCK   "TM"

Definition at line 21 of file mymems.c.

Referenced by mems_Check(), and mems_InicBuffer().

Function Documentation

◆ getBackUpRegister()

uint32_t getBackUpRegister ( uint32_t backupRegister)

Read a value from an RTC backup register.

This function reads a 32-bit value from one of the RTC backup registers. These registers are retained during system standby mode and after system reset (as long as VDD is maintained or VBAT is supplied).

Parameters
backupRegisterBackup register index (0-19 for STM32WL). Should use RTC_BKP_DRx constants defined in stm32wlxx_hal_rtc_ex.h (e.g., RTC_BKP_DR0=0, RTC_BKP_DR1=1, ..., RTC_BKP_DR19=19)
Returns
uint32_t The 32-bit value stored in the backup register

Definition at line 337 of file mymems.c.

References hrtc.

Referenced by main().

Here is the caller graph for this function:

◆ mems_AddData()

HAL_StatusTypeDef mems_AddData ( const void * data,
mems_DataType_t type,
uint8_t size )

write new data of sensor at the end of queue, if is no space, the most older data start to rewrites

Parameters
data- the buffer, NULL(other parameter are ignored, if used the main default work buffer - _memsDataBlock)
type- type of data, optional
size- buffer, must be <= to SENSORS_DATASIZE (_memsMainBlock.Sens_ItemSize)
Return values
HAL_OKor error

Definition at line 168 of file mymems.c.

References _flash, _memsDataBlock, _memsMainBlock, flash_at25_Write(), mems_GetAddr(), mems_InicBuffer(), mems_SetOffset(), and mems_WriteMainBlock().

Referenced by myloraWan_MeasureFinish().

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

◆ mems_Check()

HAL_StatusTypeDef mems_Check ( int8_t correct)

Check the memory consist.

Parameters
correct- correct - means to reset all sensor data
Return values
HAL_OK- memory is OK, HAL_ERROR - error

Definition at line 213 of file mymems.c.

References _flash, _memsDataBlock, _memsMainBlock, Error_Handler(), flash_at25_Read(), mems_GetAddr(), mems_SetOffset(), MEMS_SIGN_BLOCK, mems_WriteMainBlock(), mens_MainBlockInit(), and writeLog().

Referenced by mems_ReadMainBlock().

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

◆ mems_GetAddr()

uint32_t mems_GetAddr ( uint32_t offset)
static

Definition at line 154 of file mymems.c.

References _memsMainBlock.

Referenced by mems_AddData(), mems_Check(), and mems_GetLastData().

Here is the caller graph for this function:

◆ mems_GetLastData()

HAL_StatusTypeDef mems_GetLastData ( mems_DataBlock_t ** memBlock,
uint8_t inxFromEnd )

Get the last sensor data if exists. The data and mem block is stored in work buffer, which can be modify by other mems_xxxx function, temporary valid only.

Parameters
memBlock- pointer to mem block with data
inxFromEnd- the index from end of memory, which is reading to buffer (0 - last, 1 - before last, etc)
Return values
HAL_OKor error, HAL_BUSY no more data

Definition at line 194 of file mymems.c.

References _flash, _memsDataBlock, _memsMainBlock, flash_at25_Read(), mems_GetAddr(), and mems_SetOffset().

Referenced by myLoraWan_LastDataSend().

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

◆ mems_InicBuffer()

mems_DataBlock_t * mems_InicBuffer ( mems_DataType_t type)

Initialize the work buffer - _memsDataBlock.

Return values
returnpointer to buffer

Definition at line 160 of file mymems.c.

References _memsDataBlock, and MEMS_SIGN_BLOCK.

Referenced by mems_AddData(), and myloraWan_MeasureFinish().

Here is the caller graph for this function:

◆ mems_Init()

void mems_Init ( )

Initialise NFC and external flash; load the main block and system parameters. Verifies the flash signature and resets the queue if the block size has changed.

Definition at line 52 of file mymems.c.

References _flash, _memsMainBlock, Error_Handler(), flash_at25_Init(), mems_ReadMainBlock(), mens_MainBlockInit(), SZ_DATABLOCK, SZ_MAINBLOCK, and writeLog().

Referenced by main().

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

◆ mems_ReadMainBlock()

HAL_StatusTypeDef mems_ReadMainBlock ( )

  • reading of main block from flash. If there is not valid MEMS_SIGN, the structure is initialize to 0 The main block it stored on address 0

Return values
HAL_OKor error

Definition at line 93 of file mymems.c.

References _flash, _memsMainBlock, flash_at25_Read(), mems_Check(), MEMS_MAINBLOCK_ADDR, MEMS_SIGN, mems_WriteMainBlock(), mens_MainBlockInit(), and writeLog().

Referenced by mems_Init().

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

◆ mems_RemoveLastData()

void mems_RemoveLastData ( uint8_t fromEndTo)

remove last data

Parameters
fromEndToremove from last to index, 0 - last, 1 - last&before last, etc

Definition at line 242 of file mymems.c.

References _memsMainBlock, mems_SetOffset(), and mems_WriteMainBlock().

Referenced by OnLoRaWANTxData().

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

◆ mems_Reset()

HAL_StatusTypeDef mems_Reset ( )

memory reset - remove sign from chip

  • Return values
    status

Definition at line 125 of file mymems.c.

References _flash, flash_at25_Write(), and MEMS_MAINBLOCK_ADDR.

Referenced by checkResetRestart().

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

◆ mems_SetOffset()

uint32_t mems_SetOffset ( uint32_t offset,
int16_t count )
static

move the offset in circular queue

Definition at line 137 of file mymems.c.

References _memsMainBlock, Error_Handler(), INX_GET, and writeLog().

Referenced by mems_AddData(), mems_Check(), mems_GetLastData(), and mems_RemoveLastData().

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

◆ mems_WriteMainBlock()

HAL_StatusTypeDef mems_WriteMainBlock ( )

writing the main block on flash. The main block is stored on address 0

Return values
HAL_OKor error

Definition at line 117 of file mymems.c.

References _flash, _memsMainBlock, flash_at25_Write(), and MEMS_MAINBLOCK_ADDR.

Referenced by amb_tsl2591_On(), bar_bmp585_On(), bar_ils22qs_On(), mems_AddData(), mems_Check(), mems_ReadMainBlock(), mems_RemoveLastData(), scd41_On(), sps30_On(), and tmphm_sht45_On().

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

◆ mems_WriteToSystemParams()

uint8_t mems_WriteToSystemParams ( )

write data from memory to systemParams

.MaxSensorData = 0, // will be updated later, according to memory chip .CurrentCountSensorData = 0, // will be updated later, according to memory chip .BatteryLevelStatus = 0, // will be updated later .BatteryMV = 0, // will be updated later, according to memory chip

Definition at line 268 of file mymems.c.

References _memsMainBlock, _systemParams, GetBatteryLevel(), and SYS_GetBatteryLevel().

Referenced by main().

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

◆ mens_MainBlockInit()

void mens_MainBlockInit ( )
static

Definition at line 34 of file mymems.c.

References _memsMainBlock, AT25_MEMSIZE, MEMS_DATA_BITS_BOUNDARY, MEMS_MAINBLOCK_ADDR, and MEMS_SIGN.

Referenced by mems_Check(), mems_Init(), and mems_ReadMainBlock().

Here is the caller graph for this function:

◆ nfc_st25r3_OnMailboxData()

void nfc_st25r3_OnMailboxData ( uint8_t * data,
uint16_t len )

Definition at line 303 of file mymems.c.

◆ setBackUpRegister()

void setBackUpRegister ( uint32_t backupRegister,
uint32_t value )

Write a value to an RTC backup register.

This function writes a 32-bit value to one of the RTC backup registers. These registers are retained during system standby mode and after system reset (as long as VDD is maintained or VBAT is supplied).

Use this function to save critical data before entering standby mode. On restart, read the values using getBackUpRegister() to check system state and restore necessary information.

Parameters
backupRegisterBackup register index (0-19 for STM32WL). Should use RTC_BKP_DRx constants defined in stm32wlxx_hal_rtc_ex.h (e.g., RTC_BKP_DR0=0, RTC_BKP_DR1=1, ..., RTC_BKP_DR19=19)
valueThe 32-bit value to store in the backup register

Definition at line 358 of file mymems.c.

References hrtc.

Referenced by main(), and PWR_EnterOffMode().

Here is the caller graph for this function:

Variable Documentation

◆ _flash

flash_at25CS_t _flash
static
Initial value:
=
{ .CSPort = GPIOA , .CSPin = GPIO_PIN_4 , .Spi = &hspi1, .Is = 0, .Size = 0 }
SPI_HandleTypeDef hspi1
Definition spi.c:27

Definition at line 29 of file mymems.c.

Referenced by mems_AddData(), mems_Check(), mems_GetLastData(), mems_Init(), mems_ReadMainBlock(), mems_Reset(), and mems_WriteMainBlock().

◆ _memsDataBlock

mems_DataBlock_t _memsDataBlock = {}
static

Definition at line 32 of file mymems.c.

Referenced by mems_AddData(), mems_Check(), mems_GetLastData(), and mems_InicBuffer().

◆ _memsMainBlock