L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
sps30.c File Reference
#include "sps30.h"
#include "i2c.h"
#include "utils/mydefs.h"
#include "utils/utils.h"
#include "mymems.h"
#include <stdio.h>
Include dependency graph for sps30.c:

Go to the source code of this file.

Macros

#define SPS30_I2C_ADDR   (0x69 << 1)
#define SPS30_CMD_START_MEAS   0x0010
#define SPS30_CMD_STOP_MEAS   0x0104
#define SPS30_CMD_READ_DRDY   0x0202
#define SPS30_CMD_READ_MEAS   0x0300
#define SPS30_CMD_SOFT_RESET   0xD304
#define SPS30_CMD_START_CLEAN   0x5607
#define SPS30_CMD_SLEEP   0x1001
#define SPS30_CMD_WAKEUP   0x1103
#define SPS30_CMD_CLEAN_INTERVAL   0x8004

Enumerations

enum  clearing_t { CLR_NONE , CLR_START , CLR_PROCESS , CLR_STOP }

Functions

AQI_Level_t sps30_ClassifyPM25 (char **label)
 Classify the current PM2.5 mass concentration according to the AQI scale.
int8_t sps30_Is (I2C_HandleTypeDef *hi2c, int8_t tryInit)
 
  • check if SPS30 sensor is present

HAL_StatusTypeDef sps30_Init (I2C_HandleTypeDef *hi2c)
 initialization of sensor sps30
HAL_StatusTypeDef sps30_On (I2C_HandleTypeDef *hi2c)
 Turn on laser and fan to allow measurements.
HAL_StatusTypeDef sps30_Off (I2C_HandleTypeDef *hi2c)
 Turn off laser and fan to stop measurements.
HAL_StatusTypeDef sps30_IsDataReady (I2C_HandleTypeDef *hi2c)
 Check if data is available.
HAL_StatusTypeDef sps30_Read (I2C_HandleTypeDef *hi2c)
 Read data if data is available.
HAL_StatusTypeDef sps30_IsOnOff (I2C_HandleTypeDef *hi2c, uint8_t *onOff)
 
  • helper to check if sensor is on or not

HAL_StatusTypeDef sps30_StartCleaning (I2C_HandleTypeDef *hi2c)
 manually start fan for cleaning, runs for about 10s Note: The sensor will be busy cleaning for 10 seconds. Data read during this time might be inconsistent due to high airflow.
HAL_StatusTypeDef sps30_GetAutoCleanInterval (I2C_HandleTypeDef *hi2c, uint32_t *interval_sec)
 interval for auto cleaning
HAL_StatusTypeDef sps30_SetAutoCleanInterval (I2C_HandleTypeDef *hi2c, uint32_t interval_sec)
 set interval for auto cleaning
void sps30_LogData (char *buf)
 log data to buffer
int8_t sps30_Service (I2C_HandleTypeDef *hi2c)
 the sensor need to be cleaning

Variables

sps30_t _sps30Data = {}
 Live measurement data from the SPS30 sensor; updated by sps30_Read().
sps30_t _bck_sps30Data = {}
 Snapshot copy of the last completed SPS30 measurement; used for LoRaWAN transmission.
static uint8_t _isSps30 = 0
static const uint8_t _cleaningInterval = 100
static clearing_t _isClearing = CLR_NONE
static sleeper_t _clearingTime = {}

Macro Definition Documentation

◆ SPS30_CMD_CLEAN_INTERVAL

#define SPS30_CMD_CLEAN_INTERVAL   0x8004

Definition at line 28 of file sps30.c.

Referenced by sps30_GetAutoCleanInterval(), and sps30_SetAutoCleanInterval().

◆ SPS30_CMD_READ_DRDY

#define SPS30_CMD_READ_DRDY   0x0202

Definition at line 22 of file sps30.c.

Referenced by sps30_IsDataReady().

◆ SPS30_CMD_READ_MEAS

#define SPS30_CMD_READ_MEAS   0x0300

Definition at line 23 of file sps30.c.

Referenced by sps30_Read().

◆ SPS30_CMD_SLEEP

#define SPS30_CMD_SLEEP   0x1001

Definition at line 26 of file sps30.c.

Referenced by sps30_Init(), and sps30_Off().

◆ SPS30_CMD_SOFT_RESET

#define SPS30_CMD_SOFT_RESET   0xD304

Definition at line 24 of file sps30.c.

Referenced by sps30_Init().

◆ SPS30_CMD_START_CLEAN

#define SPS30_CMD_START_CLEAN   0x5607

Definition at line 25 of file sps30.c.

Referenced by sps30_StartCleaning().

◆ SPS30_CMD_START_MEAS

#define SPS30_CMD_START_MEAS   0x0010

Definition at line 20 of file sps30.c.

Referenced by sps30_On().

◆ SPS30_CMD_STOP_MEAS

#define SPS30_CMD_STOP_MEAS   0x0104

Definition at line 21 of file sps30.c.

Referenced by sps30_Off().

◆ SPS30_CMD_WAKEUP

#define SPS30_CMD_WAKEUP   0x1103

Definition at line 27 of file sps30.c.

Referenced by sps30_Init(), and sps30_On().

◆ SPS30_I2C_ADDR

#define SPS30_I2C_ADDR   (0x69 << 1)

Enumeration Type Documentation

◆ clearing_t

enum clearing_t
Enumerator
CLR_NONE 
CLR_START 
CLR_PROCESS 
CLR_STOP 

Definition at line 30 of file sps30.c.

Function Documentation

◆ sps30_ClassifyPM25()

AQI_Level_t sps30_ClassifyPM25 ( char ** label)

Classify the current PM2.5 mass concentration according to the AQI scale.

Parameters
labelOptional output pointer; if non-NULL, *label is set to a static human-readable string (e.g. "Good", "Moderate", "Hazardous").
Returns
The AQI_Level_t value corresponding to the PM2.5 concentration in _sps30Data.Mass_pm2_5.

Definition at line 47 of file sps30.c.

References _sps30Data, AQI_GOOD, AQI_HAZARDOUS, AQI_MODERATE, AQI_UNHEALTHY, AQI_UNHEALTHY_SENSITIVE, and AQI_VERY_UNHEALTHY.

Referenced by sps30_LogData().

Here is the caller graph for this function:

◆ sps30_GetAutoCleanInterval()

HAL_StatusTypeDef sps30_GetAutoCleanInterval ( I2C_HandleTypeDef * hi2c,
uint32_t * interval_sec )

interval for auto cleaning

Definition at line 300 of file sps30.c.

References _isSps30, SPS30_CMD_CLEAN_INTERVAL, SPS30_I2C_ADDR, and systemParams_CalculateCrc().

Here is the call graph for this function:

◆ sps30_Init()

HAL_StatusTypeDef sps30_Init ( I2C_HandleTypeDef * hi2c)

initialization of sensor sps30

Return values
HAL_OK- sensor is present, HAL_ERROR - error

Definition at line 90 of file sps30.c.

References _isClearing, _isSps30, CLR_NONE, HAL_Delay(), I2C_IsDeviceReadyMT(), SPS30_CMD_SLEEP, SPS30_CMD_SOFT_RESET, SPS30_CMD_WAKEUP, and SPS30_I2C_ADDR.

Referenced by sps30_Is().

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

◆ sps30_Is()

int8_t sps30_Is ( I2C_HandleTypeDef * hi2c,
int8_t tryInit )

  • check if SPS30 sensor is present

Parameters
tryInit- in case sensor is not yet initialized, 1 - attempt to initialize again, 0 - no
Return values
1- is present, 0 - is not

Definition at line 83 of file sps30.c.

References _isSps30, and sps30_Init().

Here is the call graph for this function:

◆ sps30_IsDataReady()

HAL_StatusTypeDef sps30_IsDataReady ( I2C_HandleTypeDef * hi2c)

Check if data is available.

Return values
HAL_OK,HAL_ERROR,HAL_BUSY

Definition at line 201 of file sps30.c.

References _isSps30, SPS30_CMD_READ_DRDY, and SPS30_I2C_ADDR.

Referenced by sps30_IsOnOff(), and sps30_Read().

Here is the caller graph for this function:

◆ sps30_IsOnOff()

HAL_StatusTypeDef sps30_IsOnOff ( I2C_HandleTypeDef * hi2c,
uint8_t * onOff )

  • helper to check if sensor is on or not

Definition at line 271 of file sps30.c.

References sps30_IsDataReady().

Here is the call graph for this function:

◆ sps30_LogData()

void sps30_LogData ( char * buf)

log data to buffer

Definition at line 356 of file sps30.c.

References _sps30Data, PRIf_02, PRIf_02D, and sps30_ClassifyPM25().

Here is the call graph for this function:

◆ sps30_Off()

HAL_StatusTypeDef sps30_Off ( I2C_HandleTypeDef * hi2c)

Turn off laser and fan to stop measurements.

Return values
HAL_OK,HAL_ERROR

Definition at line 175 of file sps30.c.

References _isSps30, HAL_Delay(), SPS30_CMD_SLEEP, SPS30_CMD_STOP_MEAS, and SPS30_I2C_ADDR.

Referenced by sps30_Service().

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

◆ sps30_On()

HAL_StatusTypeDef sps30_On ( I2C_HandleTypeDef * hi2c)

Turn on laser and fan to allow measurements.

Return values
HAL_OK,HAL_ERROR

Definition at line 128 of file sps30.c.

References _cleaningInterval, _isClearing, _isSps30, _memsMainBlock, _sps30Data, CLR_NONE, CLR_START, HAL_Delay(), mems_WriteMainBlock(), SPS30_CMD_START_MEAS, SPS30_CMD_WAKEUP, SPS30_I2C_ADDR, and systemParams_CalculateCrc().

Referenced by sps30_Service().

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

◆ sps30_Read()

HAL_StatusTypeDef sps30_Read ( I2C_HandleTypeDef * hi2c)

Read data if data is available.

Definition at line 224 of file sps30.c.

References _sps30Data, SPS30_CMD_READ_MEAS, SPS30_I2C_ADDR, sps30_IsDataReady(), and systemParams_CalculateCrc().

Here is the call graph for this function:

◆ sps30_Service()

int8_t sps30_Service ( I2C_HandleTypeDef * hi2c)

the sensor need to be cleaning

Return values
0- no need, 1 - sensor is clearing

Definition at line 367 of file sps30.c.

References _clearingTime, _isClearing, CLR_NONE, CLR_PROCESS, CLR_START, CLR_STOP, sleeper_Init(), sleeper_IsElapsed(), sps30_Off(), sps30_On(), sps30_StartCleaning(), and writeLog().

Here is the call graph for this function:

◆ sps30_SetAutoCleanInterval()

HAL_StatusTypeDef sps30_SetAutoCleanInterval ( I2C_HandleTypeDef * hi2c,
uint32_t interval_sec )

set interval for auto cleaning

Definition at line 329 of file sps30.c.

References _isSps30, SPS30_CMD_CLEAN_INTERVAL, SPS30_I2C_ADDR, and systemParams_CalculateCrc().

Here is the call graph for this function:

◆ sps30_StartCleaning()

HAL_StatusTypeDef sps30_StartCleaning ( I2C_HandleTypeDef * hi2c)

manually start fan for cleaning, runs for about 10s Note: The sensor will be busy cleaning for 10 seconds. Data read during this time might be inconsistent due to high airflow.

Definition at line 288 of file sps30.c.

References _isSps30, SPS30_CMD_START_CLEAN, and SPS30_I2C_ADDR.

Referenced by sps30_Service().

Here is the caller graph for this function:

Variable Documentation

◆ _bck_sps30Data

sps30_t _bck_sps30Data = {}

Snapshot copy of the last completed SPS30 measurement; used for LoRaWAN transmission.

Definition at line 39 of file sps30.c.

Referenced by sensors_CayennelFromBckData(), sensors_CopyToBck(), sensors_ReadToBckFromDataBlock(), and sensors_WriteFromBckToDataBlock().

◆ _cleaningInterval

const uint8_t _cleaningInterval = 100
static

Definition at line 42 of file sps30.c.

Referenced by sps30_On().

◆ _clearingTime

sleeper_t _clearingTime = {}
static

Definition at line 45 of file sps30.c.

Referenced by sps30_Service().

◆ _isClearing

clearing_t _isClearing = CLR_NONE
static

Definition at line 44 of file sps30.c.

Referenced by sps30_Init(), sps30_On(), and sps30_Service().

◆ _isSps30

◆ _sps30Data

sps30_t _sps30Data = {}

Live measurement data from the SPS30 sensor; updated by sps30_Read().

Definition at line 38 of file sps30.c.

Referenced by sensors_CopyToBck(), sps30_ClassifyPM25(), sps30_LogData(), sps30_On(), and sps30_Read().