EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
si7021.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include "i2cspm.h"
17 
18 #include "thunderboard/util.h"
19 #include "thunderboard/board.h"
20 #include "thunderboard/si7021.h"
21 
22 /***************************************************************************/
28  /**************************************************************************/
34 /***************************************************************************/
41 uint32_t SI7021_init( void )
42 {
43 
44  uint32_t status;
45  uint8_t cmdReadId2[2] = SI7021_CMD_READ_ID_BYTE2;
46  uint8_t deviceId[8];
47 
48  /* Enable the sensor and wait for it to become ready */
49  BOARD_envSensEnable( true );
50  UTIL_delay( 80 );
51 
52  /* Read and compare device ID */
53  status = SI7021_cmdRead( cmdReadId2, 2, deviceId, 8 );
54  if( status != SI7021_OK ) {
55  return status;
56  }
57 
58  if( deviceId[0] != SI7021_DEVICE_ID ) {
60  }
61 
62  return SI7021_OK;
63 
64 }
65 
66 /***************************************************************************/
74 void SI7021_deInit( void )
75 {
76 
77  return;
78 
79 }
80 
81 /***************************************************************************/
94 uint32_t SI7021_measure( uint32_t *rhData, int32_t *tData )
95 {
96 
97  uint32_t status;
98  uint8_t cmd;
99  uint8_t readData[2];
100  uint32_t timeout;
101 
102  /* Start no-hold measurement */
104  status = SI7021_cmdWrite( &cmd, 1, NULL, 0 );
105  if( status != SI7021_OK ) {
106  return status;
107  }
108 
109  /* Wait for data to become ready */
110  timeout = 10;
111  while( timeout-- ) {
112 
113  status = SI7021_cmdRead( NULL, 0, readData, 2 );
114 
115  if( status == SI7021_OK ) {
116  /* Data is ready */
117  *rhData = ( (uint32_t) readData[0] << 8 ) + ( readData[1] & 0xfc );
118  *rhData = ( ( ( *rhData ) * 15625L ) >> 13 ) - 6000;
119  break;
120  }
121  else if( status == SI7021_ERROR_I2C_TRANSFER_NACK ) {
122  /* Data not ready, sleep and try again */
123  UTIL_delay( 10 );
124  }
125  else {
126  /* Something else went wrong! */
127  return status;
128  }
129  }
130 
131  if( timeout == 0 ) {
132  return SI7021_ERROR_TIMEOUT;
133  }
134 
135  cmd = SI7021_CMD_READ_TEMP;
136  status = SI7021_cmdRead( &cmd, 1, readData, 2 );
137  if( status != SI7021_OK )
138  return status;
139 
140  *tData = ( (uint32_t) readData[0] << 8 ) + ( readData[1] & 0xfc );
141  *tData = ( ( ( *tData ) * 21965L ) >> 13 ) - 46850;
142 
143  return SI7021_OK;
144 
145 }
146 
147 /***************************************************************************/
157 uint32_t SI7021_getFwRev( uint8_t *fwRev )
158 {
159 
160  uint32_t status;
161  uint8_t cmdReadFwRev[2] = SI7021_CMD_READ_FW_REV;
162 
163  status = SI7021_cmdRead( cmdReadFwRev, 2, fwRev, 1 );
164 
165  return status;
166 
167 }
168 
169 /***************************************************************************/
188 uint32_t SI7021_cmdRead( uint8_t *cmd, size_t cmdLen, uint8_t *result, size_t resultLen )
189 {
190 
193 
194  seq.addr = SI7021_I2C_BUS_ADDRESS << 1;
195 
196  if( cmdLen > 0 ) {
197 
199 
200  seq.buf[0].data = cmd;
201  seq.buf[0].len = cmdLen;
202  seq.buf[1].data = result;
203  seq.buf[1].len = resultLen;
204  }
205  else {
206 
207  seq.flags = I2C_FLAG_READ;
208 
209  seq.buf[0].data = result;
210  seq.buf[0].len = resultLen;
211  }
212 
213  ret = I2CSPM_Transfer( SI7021_I2C_DEVICE, &seq );
214 
215  if( ret == i2cTransferNack ) {
216 
218  }
219  else if( ret != i2cTransferDone ) {
220 
222  }
223 
224  return SI7021_OK;
225 
226 }
227 
228 /***************************************************************************/
247 uint32_t SI7021_cmdWrite( uint8_t *cmd, size_t cmdLen, uint8_t *data, size_t dataLen )
248 {
249 
252 
253  seq.addr = SI7021_I2C_BUS_ADDRESS << 1;
254  seq.buf[0].data = cmd;
255  seq.buf[0].len = cmdLen;
256 
257  if( dataLen > 0 ) {
258 
260  seq.buf[1].data = data;
261  seq.buf[1].len = dataLen;
262  }
263  else {
264 
265  seq.flags = I2C_FLAG_WRITE;
266  }
267 
268  ret = I2CSPM_Transfer( SI7021_I2C_DEVICE, &seq );
269 
270  if( ret == i2cTransferNack ) {
271 
273  }
274  else if( ret != i2cTransferDone ) {
275 
277  }
278 
279  return SI7021_OK;
280 
281 }
Driver for the Si7021 I2C Humidity and Temperature Sensor.
#define SI7021_CMD_MEASURE_RH_NO_HOLD
Definition: si7021.h:73
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
Definition: i2cspm.c:124
#define I2C_FLAG_READ
Indicate plain read sequence: S+ADDR(R)+DATA0+P.
Definition: em_i2c.h:135
void UTIL_delay(uint32_t ms)
Delays number of msTick Systicks (1 ms)
Definition: util.c:97
#define SI7021_ERROR_TIMEOUT
Definition: si7021.h:62
#define SI7021_ERROR_INVALID_DEVICE_ID
Definition: si7021.h:61
#define I2C_FLAG_WRITE
Indicate plain write sequence: S+ADDR(W)+DATA0+P.
Definition: em_i2c.h:124
I2C_TransferReturn_TypeDef
Definition: em_i2c.h:179
uint32_t BOARD_envSensEnable(bool enable)
Enables or disables the environmental sensor group (Pressure, RH/Temp, UV/Ambient light and Hall sens...
Definition: board.c:492
uint32_t SI7021_getFwRev(uint8_t *fwRev)
Reads the firmware revision of the Si7021 chip.
Definition: si7021.c:157
I2C simple poll-based master mode driver for the DK/STK.
#define SI7021_CMD_READ_ID_BYTE2
Definition: si7021.h:83
#define SI7021_ERROR_I2C_TRANSFER_NACK
Definition: si7021.h:59
uint32_t SI7021_cmdWrite(uint8_t *cmd, size_t cmdLen, uint8_t *data, size_t dataLen)
Sends a command and data to the chip over the I2C bus.
Definition: si7021.c:247
Utility Functions for the Thunderboard Sense.
struct I2C_TransferSeq_TypeDef::@0 buf[2]
#define SI7021_OK
Definition: si7021.h:58
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
Definition: em_i2c.h:252
#define SI7021_I2C_DEVICE
Definition: si7021.h:46
#define SI7021_I2C_BUS_ADDRESS
Definition: si7021.h:47
uint32_t SI7021_measure(uint32_t *rhData, int32_t *tData)
Performs relative humidity and temperature measurements.
Definition: si7021.c:94
uint32_t SI7021_init(void)
Initializes the Si7021 chip.
Definition: si7021.c:41
#define SI7021_DEVICE_ID
Definition: si7021.h:48
#define I2C_FLAG_WRITE_WRITE
Indicate write sequence using two buffers: S+ADDR(W)+DATA0+DATA1+P.
Definition: em_i2c.h:159
#define SI7021_CMD_READ_FW_REV
Definition: si7021.h:84
#define I2C_FLAG_WRITE_READ
Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
Definition: em_i2c.h:148
uint16_t addr
Address to use after (repeated) start.
Definition: em_i2c.h:262
BOARD module header file.
uint32_t SI7021_cmdRead(uint8_t *cmd, size_t cmdLen, uint8_t *result, size_t resultLen)
Sends a command and reads the result byte(s) over the I2C bus.
Definition: si7021.c:188
#define SI7021_CMD_READ_TEMP
Definition: si7021.h:76
void SI7021_deInit(void)
De-initializes the Si7021 chip. Disables the sensor power domain, this also disables other sensors...
Definition: si7021.c:74
#define SI7021_ERROR_I2C_TRANSFER_FAILED
Definition: si7021.h:60