IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
SHT30.c File Reference

SHT30 functionality. More...

#include <stdint.h>
#include <stdbool.h>
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_i2c.h"
#include "SHT30.h"
#include "debug_dbprint.h"
#include "pin_mapping.h"
#include "i2c.h"
#include "delay.h"

Go to the source code of this file.

Macros

#define SHT30_ADDRESS   0x44 /* (7 bit notation) */
 
#define SOFT_RESET_CMD   0x30A2
 
#define HIGH_REP_MEAS_CMD   0x2400 /* High repeatablility, no clock stretching */
 
#define SEND_MEASUREMENT_COMMAND   0x2C
 
#define HIGH_REPEATABILITY_COMMAND   0x06
 

Functions

int8_t softResetSHT30 (void)
 Soft reset the SHT30. More...
 
int32_t SHT30_readTemp (void)
 Do I2C transfers to get the temperature from the SHT30. More...
 

Variables

bool SHT30_reset = false
 

Detailed Description

SHT30 functionality.

Version
1.0
Author
Brecht Van Eeckhoudt

Versions

  • v1.0: Initial version.

License

Copyright (C) 2020 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file SHT30.c.

Macro Definition Documentation

◆ HIGH_REP_MEAS_CMD

#define HIGH_REP_MEAS_CMD   0x2400 /* High repeatablility, no clock stretching */

Definition at line 61 of file SHT30.c.

◆ HIGH_REPEATABILITY_COMMAND

#define HIGH_REPEATABILITY_COMMAND   0x06

Definition at line 63 of file SHT30.c.

◆ SEND_MEASUREMENT_COMMAND

#define SEND_MEASUREMENT_COMMAND   0x2C

Definition at line 62 of file SHT30.c.

◆ SHT30_ADDRESS

#define SHT30_ADDRESS   0x44 /* (7 bit notation) */

Definition at line 57 of file SHT30.c.

◆ SOFT_RESET_CMD

#define SOFT_RESET_CMD   0x30A2

Definition at line 60 of file SHT30.c.

Function Documentation

◆ SHT30_readTemp()

int32_t SHT30_readTemp ( void  )

Do I2C transfers to get the temperature from the SHT30.

This method also initializes i2C if necessary.

Returns
The temperature (*1000).

Definition at line 134 of file SHT30.c.

135 {
136  /* Initialize I2C if not already the case */
137  if (initI2C() != 0) return (-6);
138 
139  /* Separate command in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
140  uint8_t cmd_buf[2] = {(HIGH_REP_MEAS_CMD >> 8), (HIGH_REP_MEAS_CMD & 0x00FF)};
141 
142  /* Transfer structure */
143  I2C_TransferSeq_TypeDef i2cTransfer;
144  I2C_TransferReturn_TypeDef result;
145 
146  /* Create buffer for received bytes */
147  uint8_t data_buf[6] = {0, 0, 0, 0, 0, 0};
148 
149  // data_buf[6] = cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
150  // float cTemp = ((((data_buf[0] * 256.0) + data_buf[1]) * 175) / 65535.0) - 45;
151  // float humidity = ((((data_buf[3] * 256.0) + data_buf[4]) * 100) / 65535.0);
152 
153  /* Initializing I2C transfer */
154  i2cTransfer.addr = SHT30_ADDRESS << 1; /* Left shift so we go to 8 bit notation instead of 7 */
155  i2cTransfer.flags = I2C_FLAG_WRITE; /* Data written from buf[0].data */
156  i2cTransfer.buf[0].data = cmd_buf;
157  i2cTransfer.buf[0].len = 2; /* Command is two bytes */
158  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
159 
160  /* Transmitting I2C data (busy-wait until transfer is complete) */
161  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
162 
163  delay(10);
164 
165  /* Initializing I2C transfer */
166  i2cTransfer.flags = I2C_FLAG_READ; /* Data read into buf[0].data */
167  i2cTransfer.buf[0].data = data_buf;
168  i2cTransfer.buf[0].len = 6; /* Data itself is 6 bytes */
169  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
170 
171  /* Transmitting I2C data (busy-wait until transfer is complete) */
172  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
173 
174  return ((int32_t)((((((data_buf[0] * 256.0) + data_buf[1]) * 175) / 65535.0) - 45) * 1000));
175 }
#define BUS_I2C
Definition: pin_mapping.h:53
#define SHT30_ADDRESS
Definition: SHT30.c:57
int int32_t
Typedef defining 32 bit int type. The developer should modify this to suit the platform being deploye...
Definition: vl53l1_types.h:83
void delay(uint32_t msDelay)
Wait for a certain amount of milliseconds in EM2/3.
Definition: delay.c:124
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98
#define HIGH_REP_MEAS_CMD
Definition: SHT30.c:61

◆ softResetSHT30()

int8_t softResetSHT30 ( void  )

Soft reset the SHT30.

This method also initializes i2C if necessary.

Note
This is a static method because it's only internally used in this file and called by other methods if necessary.
Returns
  • 0 - Byte written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 91 of file SHT30.c.

92 {
93  /* Initialize I2C if not already the case */
94  if (initI2C() != 0) return (-6);
95 
96  /* Separate command in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
97  uint8_t cmd_buf[2] = {(SOFT_RESET_CMD >> 8), (SOFT_RESET_CMD & 0x00FF)};
98 
99  /* Transfer structure */
100  I2C_TransferSeq_TypeDef i2cTransfer;
101  I2C_TransferReturn_TypeDef result;
102 
103  /* Initializing I2C transfer */
104  i2cTransfer.addr = SHT30_ADDRESS << 1; /* Left shift so we go to 8 bit notation instead of 7 */
105  i2cTransfer.flags = I2C_FLAG_WRITE; /* Data written from buf[0].data */
106  i2cTransfer.buf[0].data = cmd_buf;
107  i2cTransfer.buf[0].len = 2; /* Command is two bytes */
108  result = I2C_TransferInit(BUS_I2C, &i2cTransfer);
109 
110  /* Transmitting I2C data (busy-wait until transfer is complete) */
111  while (result == i2cTransferInProgress) result = I2C_Transfer(BUS_I2C);
112 
113 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
114  dbinfo("Soft reset SHT30");
115 #endif /* DEBUG_DBPRINT */
116 
117  /* Indicate that SHT30 has been soft reset */
118  SHT30_reset = true;
119 
120  return (result);
121 }
bool SHT30_reset
Definition: SHT30.c:67
#define BUS_I2C
Definition: pin_mapping.h:53
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:505
#define SHT30_ADDRESS
Definition: SHT30.c:57
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
#define SOFT_RESET_CMD
Definition: SHT30.c:60
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

Variable Documentation

◆ SHT30_reset

bool SHT30_reset = false

Definition at line 67 of file SHT30.c.