EFR32 Mighty Gecko 13 Software Documentation  efr32mg13-doc-5.1.2
tempsens.c
Go to the documentation of this file.
1 /***************************************************************************/
17 #include "i2cspm.h"
18 #include "tempsens.h"
19 
20 /*******************************************************************************
21  ************************** GLOBAL FUNCTIONS *******************************
22  ******************************************************************************/
23 
24 /***************************************************************************/
33 {
34  int32_t convert;
35 
36  /* Use integer arithmetic. Values are sufficiently low, so int32 will */
37  /* not overflow. */
38  convert = (int32_t)(temp->i) * 10000;
39  convert += (int32_t)(temp->f);
40  convert = ((convert * 9) / 5) + (32 * 10000);
41 
42  temp->i = (int16_t)(convert / 10000);
43  temp->f = (int16_t)(convert % 10000);
44 }
45 
46 
47 /***************************************************************************/
75  uint8_t addr,
77  uint16_t *val)
78 {
81  uint8_t regid[1];
82  uint8_t data[2];
83 
84  seq.addr = addr;
86  /* Select register to be read */
87  regid[0] = ((uint8_t) reg) & 0x3;
88  seq.buf[0].data = regid;
89  seq.buf[0].len = 1;
90  /* Select location/length to place register */
91  if (reg == tempsensRegConfig)
92  {
93  /* Only 1 byte reg, clear upper 8 bits */
94  data[0] = 0;
95  seq.buf[1].data = data + 1;
96  seq.buf[1].len = 1;
97  }
98  else
99  {
100  seq.buf[1].data = data;
101  seq.buf[1].len = 2;
102  }
103 
104  ret = I2CSPM_Transfer(i2c, &seq);
105  if (ret != i2cTransferDone)
106  {
107  return((int) ret);
108  }
109 
110  *val = (((uint16_t)(data[0])) << 8) | data[1];
111 
112  return(0);
113 }
114 
115 
116 /***************************************************************************/
137  uint8_t addr,
139  uint16_t val)
140 {
143  uint8_t data[3];
144 
145  if (reg == tempsensRegTemp)
146  {
147  return(-1);
148  }
149 
150  seq.addr = addr;
151  seq.flags = I2C_FLAG_WRITE;
152  /* Select register to be written */
153  data[0] = ((uint8_t) reg) & 0x3;
154  seq.buf[0].data = data;
155  if (reg == tempsensRegConfig)
156  {
157  /* Only 1 byte reg */
158  data[1] = (uint8_t) val;
159  seq.buf[0].len = 2;
160  }
161  else
162  {
163  data[1] = (uint8_t)(val >> 8);
164  data[2] = (uint8_t) val;
165  seq.buf[0].len = 3;
166  }
167 
168  ret = I2CSPM_Transfer(i2c, &seq);
169  if (ret != i2cTransferDone)
170  {
171  return((int) ret);
172  }
173 
174  return(ret);
175 }
176 
177 
178 /***************************************************************************/
210  uint8_t addr,
211  TEMPSENS_Temp_TypeDef *temp)
212 {
213  int ret;
214  uint32_t tmp;
215  uint16_t val = 0;
216 
217  ret = TEMPSENS_RegisterGet(i2c, addr, tempsensRegTemp, &val);
218  if (ret < 0)
219  {
220  return(ret);
221  }
222 
223  /* Get all 12 bits potentially used */
224  tmp = (uint32_t)(val >> 4);
225 
226  /* If negative number, convert using 2s complement */
227  if (tmp & 0x800)
228  {
229  tmp = (~tmp + 1) & 0xfff;
230  temp->i = -(int16_t)(tmp >> 4);
231  temp->f = -(int16_t)((tmp & 0xf) * 625);
232  }
233  else
234  {
235  temp->i = (int16_t)(tmp >> 4);
236  temp->f = (int16_t)((tmp & 0xf) * 625);
237  }
238 
239  return(0);
240 }
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
Definition: i2cspm.c:124
#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
int TEMPSENS_RegisterGet(I2C_TypeDef *i2c, uint8_t addr, TEMPSENS_Register_TypeDef reg, uint16_t *val)
Read sensor register content.
Definition: tempsens.c:74
void TEMPSENS_Celsius2Fahrenheit(TEMPSENS_Temp_TypeDef *temp)
Convert temperature from Celsius to Fahrenheit.
Definition: tempsens.c:32
int TEMPSENS_RegisterSet(I2C_TypeDef *i2c, uint8_t addr, TEMPSENS_Register_TypeDef reg, uint16_t val)
Write to sensor register.
Definition: tempsens.c:136
I2C simple poll-based master mode driver for the DK/STK.
struct I2C_TransferSeq_TypeDef::@0 buf[2]
Temperature sensor driver for DS75 temperature sensor compatible device on the DK.
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
Definition: em_i2c.h:252
TEMPSENS_Register_TypeDef
Definition: tempsens.h:50
#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
int TEMPSENS_TemperatureGet(I2C_TypeDef *i2c, uint8_t addr, TEMPSENS_Temp_TypeDef *temp)
Fetch current temperature from temperature sensor (in Celsius).
Definition: tempsens.c:209