Skip to content

Commit

Permalink
🚧 wip(th sensor) start implementing the interface functions
Browse files Browse the repository at this point in the history
  • Loading branch information
polesskiy-dev committed Aug 16, 2024
1 parent 8c30b37 commit 5612498
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef enum {
// GLOBAL Events
GLOBAL_CMD_INITIALIZE,
GLOBAL_INITIALIZE_SUCCESS,
GLOBAL_RTC_WAKE_UP, ///> RTC wakes up event
GLOBAL_WAKE_N_READ, ///> RTC wakes up event, mostly leads to the sensor measurements read
GLOBAL_TEMPERATURE_HUMIDITY_MEASUREMENTS_READY, ///< Temperature and humidity measurements are ready
GLOBAL_LIGHT_MEASUREMENTS_READY, ///< Light measurements are ready
GLOBAL_CMD_INFO_LED_ON,
Expand All @@ -40,21 +40,17 @@ typedef enum {
// INFO_LED
INFO_LED_FLASH,
// NFC
NFC_INITIALIZE,
NFC_GPO_INTERRUPT,
NFC_MAILBOX_HAS_NEW_MESSAGE,
// ACCELEROMETER_SENSOR
// TODO: Add accelerometer events
// TEMPERATURE_HUMIDITY_SENSOR
TH_SENS_INITIALIZE,
TH_SENS_START_SINGLE_SHOT_READ,
TH_SENS_CRON_READ,
TH_SENS_TURN_OFF,
TH_SENS_ERROR,
// LIGHT_SENSOR
LIGHT_SENS_INITIALIZE_SUCCESS,
LIGHT_SENS_SINGLE_SHOT_READ,
LIGHT_SENS_MEASURE_CONTINUOUSLY,
LIGHT_SENS_CRON_READ,
LIGHT_SENS_SET_LIMIT,
LIGHT_SENS_TURN_OFF,
LIGHT_SENS_LIMIT_INT,
Expand Down
2 changes: 1 addition & 1 deletion firmware/iot-risk-logger-stm32l4/app/core/cron/cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ actor_t* CRON_ActorInit(void) {
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) {
int32_t currentTimestamp = getCurrentUnixTimestamp();

osMessageQueuePut(EV_MANAGER_Actor.super.osMessageQueueId, &(message_t){GLOBAL_RTC_WAKE_UP, .payload.value = currentTimestamp}, 0, 0);
osMessageQueuePut(EV_MANAGER_Actor.super.osMessageQueueId, &(message_t){GLOBAL_WAKE_N_READ, .payload.value = currentTimestamp}, 0, 0);
}

static osStatus_t handleCronMessage(CRON_Actor_t *this, message_t *message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ extern "C" {

#include "main.h"

#define OPT3001_I2C_ADDR_45 (0x45)

/**
* @brief Config Conversion time
*/
Expand Down
65 changes: 64 additions & 1 deletion firmware/iot-risk-logger-stm32l4/app/drivers/sht3x/sht3x.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,67 @@
* @author artempolisskyi
*/

#include "sht3x.h"
#include "sht3x.h"

SHT3x_IO_t SHT3x_IO = {
.i2cAddress = 0x00,
.writeCommand = NULL,
.readCommand = NULL,
.crc8 = NULL
};

SHT3x_RESULT SHT3x_InitIO(uint8_t i2cAddress, SHT3x_WriteCommand_Func writeCommand, SHT3x_ReadCommand_Func readCommand, SHT3x_CRC8_Func crc8) {
SHT3x_IO.i2cAddress = i2cAddress;
SHT3x_IO.writeCommand = writeCommand;
SHT3x_IO.readCommand = readCommand;
SHT3x_IO.crc8 = crc8;

return SHT3x_OK;
}

/**
* @brief Read the serial number of the SHT3x sensor.
*
* Serial number structure:
* - 2 bytes: Serial number word 1
* - 1 byte: CRC for serial number word 1
* - 2 bytes: Serial number word 2
* - 1 byte: CRC for serial number word 2
*
* @see https://sensirion.com/media/documents/E5762713/63D103C2/Sensirion_electronic_identification_code_SHT3x.pdf
* @param id
* @return
*/
SHT3x_RESULT SHT3x_ReadDeviceID(uint32_t *id) {
uint8_t serialNumberData[SHT3x_SERIAL_NUMBER_SIZE];
SHT3x_RESULT result = SHT3x_IO.readCommand(SHT3x_IO.i2cAddress, SHT3x_SERIAL_NUMBER_CMD_ID, serialNumberData, SHT3x_SERIAL_NUMBER_SIZE);

if (result != SHT3x_OK) {
return result;
}

// TODO remove this check after the CRC8 function is implemented
if (SHT3x_IO.crc8 != NULL) {
// Check CRC
uint8_t word1ReceivedCRC = serialNumberData[2];
uint8_t word2ReceivedCRC = serialNumberData[5];
bool word1CRCValid = SHT3x_IO.crc8(serialNumberData, 2) == word1ReceivedCRC;
bool word2CRCValid = SHT3x_IO.crc8(serialNumberData + 3, 2) == word2ReceivedCRC;

if (word1CRCValid == false || word2CRCValid == false) {
return SHT3x_CRC_ERROR;
}
}

*id = serialNumberData[0] << 24 | serialNumberData[1] << 16 | serialNumberData[3] << 8 | serialNumberData[4];

return SHT3x_OK;
}

float SHT3x_RawToTemperatureC(uint16_t rawTemperature) {
return -45.0f + 175.0f * (rawTemperature / 65535.0f);
}

float SHT3x_RawToHumidityRH(uint16_t rawHumidity) {
return 100.0f * (rawHumidity / 65535.0f);
}
18 changes: 11 additions & 7 deletions firmware/iot-risk-logger-stm32l4/app/drivers/sht3x/sht3x.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ extern "C" {
#endif

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#include "main.h"

#define SHT3x_COMMAND_SIZE 2
#define SHT3x_I2C_ADDR_44 (0x44)
#define SHT3x_I2C_ADDR_45 (0x45)

#define SHT3x_MEASURE_SINGLE_SHOT_HIGH_REPEATABILITY_CMD_ID (0x2400)
#define SHT3x_MEASURE_SINGLE_SHOT_HIGH_REPEATABILITY_CLOCK_STRETCHING_CMD_ID (0x2c06)
Expand Down Expand Up @@ -50,6 +51,11 @@ extern "C" {
#define SHT3x_READ_STATUS_REGISTER_CMD_ID (0xf32d)
#define SHT3x_CLEAR_STATUS_REGISTER_CMD_ID (0x3041)
#define SHT3x_SOFT_RESET_CMD_ID (0x30a2)
#define SHT3x_SERIAL_NUMBER_CMD_ID (0x3682)
#define SHT3x_SERIAL_NUMBER_CLOCK_STRETCHING_CMD_ID (0x3780)

#define SHT3x_COMMAND_SIZE 2
#define SHT3x_SERIAL_NUMBER_SIZE 6

/**
* @brief SHT3x Temperature & Humidity Sensor status enumerator definition.
Expand All @@ -65,19 +71,17 @@ extern "C" {

typedef SHT3x_RESULT (*SHT3x_WriteCommand_Func)(uint16_t, uint16_t, uint8_t*, uint16_t);
typedef SHT3x_RESULT (*SHT3x_ReadCommand_Func) (uint16_t, uint16_t, uint8_t*, uint16_t);
typedef SHT3x_RESULT (*SHT3x_HardResetPulse_Func) (void);
typedef uint8_t (*SHT3x_CRC8_Func) (uint8_t*, uint8_t);

typedef struct {
uint8_t i2cAddress;
SHT3x_WriteCommand_Func writeCommand;
SHT3x_ReadCommand_Func readCommand;
SHT3x_HardResetPulse_Func hardResetPulse;
SHT3x_CRC8_Func crc8;
} SHT3x_IO_t;

SHT3x_RESULT SHT3x_InitIO(uint8_t i2cAddress, SHT3x_WriteCommand_Func writeCommand, SHT3x_ReadCommand_Func readCommand, SHT3x_HardResetPulse_Func hardResetPulse, SHT3x_CRC8_Func crc8);
SHT3x_RESULT SHT3x_ReadDeviceID(uint16_t *id);
SHT3x_RESULT SHT3x_InitIO(uint8_t i2cAddress, SHT3x_WriteCommand_Func writeCommand, SHT3x_ReadCommand_Func readCommand, SHT3x_CRC8_Func crc8);
SHT3x_RESULT SHT3x_ReadDeviceID(uint32_t *id);
SHT3x_RESULT SHT3x_ReadStatus(uint16_t *status);
SHT3x_RESULT SHT3x_ClearStatus(void);
SHT3x_RESULT SHT3x_SingleShotAcquisitionMode(uint16_t modeCondition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern actor_t* ACTORS_LIST_SystemRegistry[MAX_ACTORS];
const ACTOR_ID EV_MANAGER_SubscribersIdsMatrix[GLOBAL_EVENTS_MAX][MAX_ACTORS] = {
[GLOBAL_CMD_INITIALIZE] = {CRON_ACTOR_ID, PWRM_MANAGER_ACTOR_ID, NFC_ACTOR_ID, ACCELEROMETER_ACTOR_ID, TEMPERATURE_HUMIDITY_SENSOR_ACTOR_ID, LIGHT_SENSOR_ACTOR_ID, MEMORY_ACTOR_ID},
[GLOBAL_INITIALIZE_SUCCESS] = {},
[GLOBAL_RTC_WAKE_UP] = {TEMPERATURE_HUMIDITY_SENSOR_ACTOR_ID, LIGHT_SENSOR_ACTOR_ID},
[GLOBAL_WAKE_N_READ] = {TEMPERATURE_HUMIDITY_SENSOR_ACTOR_ID, LIGHT_SENSOR_ACTOR_ID},
[GLOBAL_TEMPERATURE_HUMIDITY_MEASUREMENTS_READY] = {MEMORY_ACTOR_ID},
[GLOBAL_LIGHT_MEASUREMENTS_READY] = {MEMORY_ACTOR_ID},
[GLOBAL_CMD_START_CONTINUOUS_SENSING] = {TEMPERATURE_HUMIDITY_SENSOR_ACTOR_ID, LIGHT_SENSOR_ACTOR_ID},
Expand Down Expand Up @@ -82,7 +82,7 @@ static osStatus_t handleEvManagerMessage(EV_MANAGER_Actor_t *this, message_t *me
return osOK;
case GLOBAL_CMD_SET_TIME_DATE:
case GLOBAL_CMD_SET_WAKE_UP_PERIOD:
case GLOBAL_RTC_WAKE_UP:
case GLOBAL_WAKE_N_READ:
case GLOBAL_CMD_TURN_OFF:
case GLOBAL_CMD_START_CONTINUOUS_SENSING:
publishEventToSubscribers(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
title Light Sensor FSM
hide empty description
TURNED_OFF: Initialized, turned off\nready for commands, low power mode
CONTINUOUS_MEASURE: Device is measuring continuously\ngenerates interrupt on threshold exceed
CONTINUOUS_MEASURE: Device is measuring continuously\ngenerates interrupt on threshold exceed\n\nGLOBAL_WAKE_N_READ: Cron Read Measurements
OUT_OF_RANGE: Lux is out of range, limits are swapped\nreturn to measurements after lux returns in limits
[*] --> TURNED_OFF : GLOBAL_CMD_INITIALIZE
note "Publishes: \nGLOBAL_INITIALIZE_SUCCESS\nGLOBAL_ERROR" as N1
TURNED_OFF --> TURNED_OFF : SINGLE_SHOT_READ
TURNED_OFF --> TURNED_OFF : SET_LIMIT
TURNED_OFF --> CONTINUOUS_MEASURE : GLOBAL_CMD_START_CONTINUOUS_SENSING
CONTINUOUS_MEASURE --> CONTINUOUS_MEASURE : CRON_READ
CONTINUOUS_MEASURE --> CONTINUOUS_MEASURE : GLOBAL_WAKE_N_READ
CONTINUOUS_MEASURE --> OUT_OF_RANGE : LIMIT_INT
OUT_OF_RANGE --> CONTINUOUS_MEASURE : LIMIT_INT
OUT_OF_RANGE --> OUT_OF_RANGE : CRON_READ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static osStatus_t handleInit(LIGHT_SENS_Actor_t *this, message_t *message) {
BSP_I2C1_Init(); // TODO think about proper place to init I2C

// init the driver (io)
osStatus_t ioStatus = OPT3001_InitIO(OPT3001_I2C_ADDRESS, BSP_I2C1_WriteReg, BSP_I2C1_ReadReg);
osStatus_t ioStatus = OPT3001_InitIO(LIGHT_SENS_I2C_ADDRESS, BSP_I2C1_WriteReg, BSP_I2C1_ReadReg);

if (ioStatus != osOK) return osError;

Expand Down Expand Up @@ -138,6 +138,7 @@ static osStatus_t handleInit(LIGHT_SENS_Actor_t *this, message_t *message) {
osMessageQueueId_t evManagerQueue = ACTORS_LIST_SystemRegistry[EV_MANAGER_ACTOR_ID]->osMessageQueueId;
osMessageQueuePut(evManagerQueue, &(message_t){GLOBAL_INITIALIZE_SUCCESS, .payload.value = LIGHT_SENSOR_ACTOR_ID}, 0, 0);

fprintf(stdout, "Light sensor %ul initialized\n", LIGHT_SENSOR_ACTOR_ID);
TO_STATE(this, LIGHT_SENS_TURNED_OFF_STATE);
}

Expand Down Expand Up @@ -207,7 +208,7 @@ static osStatus_t handleContinuousMeasure(LIGHT_SENS_Actor_t *this, message_t *m
osStatus_t ioStatus;

switch (message->event) {
case LIGHT_SENS_CRON_READ:
case GLOBAL_WAKE_N_READ:
// read the measured rawLux on RTC cron
ioStatus = OPT3001_ReadResultRawLux(&this->rawLux);
if (ioStatus != osOK) return osError;
Expand Down Expand Up @@ -248,7 +249,7 @@ static osStatus_t handleOutOfRange(LIGHT_SENS_Actor_t *this, message_t *message)
osStatus_t ioStatus;

switch (message->event) {
case LIGHT_SENS_CRON_READ:
case GLOBAL_WAKE_N_READ:
// still read the measured rawLux on RTC cron
ioStatus = OPT3001_ReadResultRawLux(&this->rawLux);
if (ioStatus != osOK) return osError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {
#include "actor.h"
#include "opt3001.h"

#define OPT3001_I2C_ADDRESS (0x45 << 1) // ADDR connected to VDD due to SHT3x 0x44 address conflict
#define LIGHT_SENS_I2C_ADDRESS (OPT3001_I2C_ADDR_45 << 1) // ADDR connected to VDD due to SHT3x address conflict

typedef enum {
LIGHT_SENS_NO_STATE = 0,
Expand Down
2 changes: 1 addition & 1 deletion firmware/iot-risk-logger-stm32l4/app/tasks/nfc/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static osStatus_t handleInit(NFC_Actor_t *this, message_t *message) {
osStatus_t status;

switch (message->event) {
case NFC_INITIALIZE:
case GLOBAL_CMD_INITIALIZE:
status = NFC_ST25DVInit(&this->st25dv);
if (status != NFCTAG_OK)
return osError;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Temperature & Humidity Sensor Task

### Overview

### State Diagram

<details>
<summary>Diagram as a code</summary>

```plantuml
@startuml
title Temperature & Humidity Sensor FSM
hide empty description
IDLE: Initialized\nready for commands, low power mode
CONTINUOUS_MEASURE: Device is measuring continuously\ngenerates interrupt on threshold exceed\n\nGLOBAL_WAKE_N_READ: Cron Read Measurements
[*] --> IDLE : GLOBAL_CMD_INITIALIZE
note "Publishes: \nGLOBAL_INITIALIZE_SUCCESS\nGLOBAL_ERROR" as N1
IDLE --> IDLE : SINGLE_SHOT_READ
IDLE --> IDLE : SET_LIMIT
IDLE --> CONTINUOUS_MEASURE : GLOBAL_CMD_START_CONTINUOUS_SENSING
CONTINUOUS_MEASURE --> CONTINUOUS_MEASURE : GLOBAL_WAKE_N_READ
CONTINUOUS_MEASURE --> CONTINUOUS_MEASURE : LIMIT_INT
ERROR --> IDLE : RECOVER (TODO)
IDLE --> ERROR : ERROR
CONTINUOUS_MEASURE --> ERROR : ERROR
@enduml
```
</details>
Loading

0 comments on commit 5612498

Please sign in to comment.