IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
SHT30.c
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file SHT30.c
3  * @brief SHT30 functionality.
4  * @version 1.0
5  * @author Brecht Van Eeckhoudt
6  *
7  * ******************************************************************************
8  *
9  * @section Versions
10  *
11  * @li v1.0: Initial version.
12  *
13  * ******************************************************************************
14  *
15  * @section License
16  *
17  * **Copyright (C) 2020 - Brecht Van Eeckhoudt**
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the **GNU General Public License** as published by
21  * the Free Software Foundation, either **version 3** of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * *A copy of the GNU General Public License can be found in the `LICENSE`
30  * file along with this source code.*
31  *
32  * @n
33  *
34  * Some methods use code obtained from examples from [Silicon Labs' GitHub](https://github.com/SiliconLabs/peripheral_examples).
35  * These sections are licensed under the Silabs License Agreement. See the file
36  * "Silabs_License_Agreement.txt" for details. Before using this software for
37  * any purpose, you must agree to the terms of that agreement.
38  *
39  ******************************************************************************/
40 
41 
42 #include <stdint.h> /* (u)intXX_t */
43 #include <stdbool.h> /* "bool", "true", "false" */
44 #include "em_device.h" /* Include necessary MCU-specific header file */
45 #include "em_cmu.h" /* Clock management unit */
46 #include "em_gpio.h" /* General Purpose IO */
47 #include "em_i2c.h"
48 
49 #include "SHT30.h" /* Corresponding header file */
50 #include "debug_dbprint.h" /* Enable or disable printing to UART */
51 #include "pin_mapping.h" /* PORT and PIN definitions */
52 #include "i2c.h" /* I2C initialization */
53 #include "delay.h" /* Delay functionality */
54 
55 
56 /* Local definitions */
57 #define SHT30_ADDRESS 0x44 /* (7 bit notation) */
58 
59 /* Sensor commands */
60 #define SOFT_RESET_CMD 0x30A2
61 #define HIGH_REP_MEAS_CMD 0x2400 /* High repeatablility, no clock stretching */
62 #define SEND_MEASUREMENT_COMMAND 0x2C
63 #define HIGH_REPEATABILITY_COMMAND 0x06
64 
65 
66 /* Local variables */
67 bool SHT30_reset = false;
68 
69 
70 /**************************************************************************//**
71  * @brief
72  * Soft reset the SHT30.
73  *
74  * @details
75  * This method also initializes i2C if necessary.
76  *
77  * @note
78  * This is a static method because it's only internally used in this file
79  * and called by other methods if necessary.
80  *
81  * @return
82  * @return
83  * @li `0` - Byte written correctly.
84  * @li `-1` - `i2cTransferNack`, NACK received during transfer.
85  * @li `-2` - `i2cTransferBusErr`, Bus error during transfer (misplaced START/STOP).
86  * @li `-3` - `i2cTransferArbLost`, Arbitration lost during transfer.
87  * @li `-4` - `i2cTransferUsageFault`, Usage fault.
88  * @li `-5` - `i2cTransferSwFault`, SW fault.
89  * @li `-6` - Coudldn't configure I2C.
90  *****************************************************************************/
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 }
122 
123 
124 /**************************************************************************//**
125  * @brief
126  * Do I2C transfers to get the temperature from the SHT30.
127  *
128  * @details
129  * This method also initializes i2C if necessary.
130  *
131  * @return
132  * The temperature (*1000).
133  *****************************************************************************/
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 }
signed char int8_t
Typedef defining 8 bit char type. The developer should modify this to suit the platform being deploye...
Definition: vl53l1_types.h:103
int32_t SHT30_readTemp(void)
Do I2C transfers to get the temperature from the SHT30.
Definition: SHT30.c:134
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
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
The pin definitions for the custom board.
Delay functionality.
Enable or disable printing to UART with dbprint.
int8_t softResetSHT30(void)
Soft reset the SHT30.
Definition: SHT30.c:91
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
SHT30 functionality.
#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
#define HIGH_REP_MEAS_CMD
Definition: SHT30.c:61
I2C functionality.