TMP42x HAL Based Library 1.0.2
This library is a software library that works with the TMP42x remote and local temperature sensor with N-Factor and series-R correction. This library provides a convenient and efficient way to access the I2C interfaces of the chip, allowing developers to easily integrate this power meter into their systems.
Loading...
Searching...
No Matches
TMP42x.c
Go to the documentation of this file.
1
33
34#include "TMP42x.h"
35
36// Privates
37
48TMP42x_Status __TMP42x_readByte(TMP42x* self, uint8_t MemAddress){
49 uint8_t data;
50 if(HAL_I2C_Mem_Read(self->hi2c, self->I2C_ADDR << 1, MemAddress, I2C_MEMADD_SIZE_8BIT, &data, 1, 1000)) return TMP42x_TimeOut;
51 self->reg8.raw_data = data;
52 return TMP42x_OK;
53}
54
65TMP42x_Status __TMP42x_writeByte(TMP42x* self, uint8_t MemAddress){
66 uint8_t data[1];
67 data[0] = self->reg8.raw_data;
68 if(HAL_I2C_Mem_Write(self->hi2c, self->I2C_ADDR << 1, MemAddress, I2C_MEMADD_SIZE_8BIT, data, 1, 1000)) return TMP42x_TimeOut;
69 return TMP42x_OK;
70}
71
82TMP42x_Status __TMP42x_readTwoBytes(TMP42x* self, uint8_t MemAddress){
83 if(HAL_OK == HAL_I2C_Mem_Read(self->hi2c, self->I2C_ADDR << 1, MemAddress, I2C_MEMADD_SIZE_8BIT, self->reg16.raw_data, 2, 100)){
84
85 self->reg16.raw_data[0] ^= self->reg16.raw_data[1];
86 self->reg16.raw_data[1] ^= self->reg16.raw_data[0];
87 self->reg16.raw_data[0] ^= self->reg16.raw_data[1];
88
89 return TMP42x_OK;
90 }
91 else{
92 return TMP42x_TimeOut;
93 }
94}
95
96//Public
138TMP42x_Status TMP42x_init(TMP42x* self, TMP42x_Type type, I2C_HandleTypeDef* hi2c, uint8_t I2C_ADDR, TMP42x_Shutdown shutdown, TMP42x_Range range, TMP42x_ConversionRate conversion_rate, float n_eff1, float n_eff2, float n_eff3){
139
140 self->hi2c = hi2c;
141 self->I2C_ADDR = I2C_ADDR;
142 self->type = type;
143 self->shutdown = shutdown;
144 self->range = range;
145 self->conversion_rate = conversion_rate;
146 self->n_eff1 = n_eff1;
147 self->n_eff2 = n_eff2;
148 self->n_eff3 = n_eff3;
149
150 // Config 1
152 self->reg8.config1_register.SD = self->shutdown;
155
156 // Config 2
158 self->reg8._config2_register.LEN = 1;
159 self->reg8._config2_register.RC = 1;
160 self->reg8._config2_register.REN = 1;
161 if(self->type != TMP421) self->reg8._config2_register.REN2 = 1;
162 if(self->type == TMP423) self->reg8._config2_register.REN3 = 1;
164
165 // Conversion Rate
167 self->reg8.conversion_rate_register.RATE = conversion_rate;
169
170 // eta 1
172 int8_t n_adjust = (int8_t)(300 - (302.4/self->n_eff1));
173 self->reg8.n_correction_register.N = n_adjust;
175
176
177 // eta 2
178 if(self->type != TMP421){
180 n_adjust = (int8_t)(300 - (302.4/self->n_eff2));
181 self->reg8.n_correction_register.N = n_adjust;
183 }
184
185 // eta 3
186 if(self->type == TMP423){
188 n_adjust = (int8_t)(300 - (302.4/self->n_eff3));
189 self->reg8.n_correction_register.N = n_adjust;
191 }
192
193 return TMP42x_OK;
194}
195
217double TMP42x_getRemoteTemp(TMP42x* self, uint8_t channel_number){
218 if(channel_number > (self->type + 1)) return -999;
219 __TMP42x_readTwoBytes(self, REMOTE1_TEMP_MSB + channel_number - 1);
220 return self->reg16.temp_register.TEMP * 0.0625 - self->range * 0x40;
221}
222
244int8_t TMP42x_getRemoteTemp_Int(TMP42x* self, uint8_t channel_number){
245 if(channel_number > (self->type + 1)) return -128;
246 __TMP42x_readByte(self, REMOTE1_TEMP_MSB + channel_number - 1);
247 return self->reg8.raw_data - self->range * 0x40;
248}
249
264 return self->reg16.temp_register.TEMP * 0.0625 - self->range * 0x40;
265}
266
281 return self->reg8.raw_data - self->range * 0x40;
282}
283
298
313
324
331uint8_t TMP42x_getDevID(TMP42x* self){
333 return self->reg8.dev_id_register.DEVID;
334}
TMP42x_Status __TMP42x_readByte(TMP42x *self, uint8_t MemAddress)
Read a byte of data from TMP42x and stores in the tmp42x::_reg8::raw_data.
Definition TMP42x.c:48
uint8_t TMP42x_getManID(TMP42x *self)
Get the manufacturer ID.
Definition TMP42x.c:320
uint8_t TMP42x_getDevID(TMP42x *self)
Get the device ID.
Definition TMP42x.c:331
double TMP42x_getLocalTemp(TMP42x *self)
Reads the full (floating-point) local temperature from a specified channel.
Definition TMP42x.c:262
TMP42x_Status __TMP42x_readTwoBytes(TMP42x *self, uint8_t MemAddress)
Read two bytes of data from TMP42x and stores in the tmp42x::_reg16::raw_data.
Definition TMP42x.c:82
TMP42x_Status TMP42x_oneShotStart(TMP42x *self)
Starts a single conversion if the device is in shutdown mode.
Definition TMP42x.c:294
int8_t TMP42x_getRemoteTemp_Int(TMP42x *self, uint8_t channel_number)
Reads the integer part of the remote temperature from a specified channel.
Definition TMP42x.c:244
TMP42x_Status TMP42x_softwareReset(TMP42x *self)
Resets the device.
Definition TMP42x.c:309
TMP42x_Status TMP42x_init(TMP42x *self, TMP42x_Type type, I2C_HandleTypeDef *hi2c, uint8_t I2C_ADDR, TMP42x_Shutdown shutdown, TMP42x_Range range, TMP42x_ConversionRate conversion_rate, float n_eff1, float n_eff2, float n_eff3)
Initialize the TMP42x with the given config.
Definition TMP42x.c:138
double TMP42x_getRemoteTemp(TMP42x *self, uint8_t channel_number)
Reads the full (floating-point) remote temperature from a specified channel.
Definition TMP42x.c:217
int8_t TMP42x_getLocalTemp_Int(TMP42x *self)
Reads the integer part of the local temperature.
Definition TMP42x.c:279
TMP42x_Status __TMP42x_writeByte(TMP42x *self, uint8_t MemAddress)
Write a byte of data to TMP42x from the tmp42x::_reg8::raw_data.
Definition TMP42x.c:65
TMP42x_Type
Definition TMP42x.h:59
@ TMP421
Definition TMP42x.h:59
@ TMP423
Definition TMP42x.h:59
#define nCORRECTION2_REGISTER
Definition TMP42x.h:53
#define ONE_SHOT_START_REGISTER
Definition TMP42x.h:47
#define CONFIG2_REGISTER
Definition TMP42x.h:45
TMP42x_ConversionRate
Definition TMP42x.h:63
#define CONVERSION_RATE_REGISTER
Definition TMP42x.h:46
#define LOCAL_TEMP_MSB
Definition TMP42x.h:39
#define DEVICE_ID_REGISTER
Definition TMP42x.h:57
TMP42x_Status
Definition TMP42x.h:60
@ TMP42x_TimeOut
Definition TMP42x.h:60
@ TMP42x_OK
Definition TMP42x.h:60
TMP42x_Range
Definition TMP42x.h:61
TMP42x_Shutdown
Definition TMP42x.h:62
struct tmp42x TMP42x
Class (struct) that stores variables for interacting with TMP42x.
#define CONFIG1_REGISTER
Definition TMP42x.h:44
#define nCORRECTION3_REGISTER
Definition TMP42x.h:54
#define MAN_ID_REGISTER
Definition TMP42x.h:56
#define REMOTE1_TEMP_MSB
Definition TMP42x.h:40
#define SOFTWARE_RESET_REGISTER
Definition TMP42x.h:55
#define nCORRECTION1_REGISTER
Definition TMP42x.h:52
union tmp42x::_reg8 reg8
TMP42x_Shutdown shutdown
Definition TMP42x.h:74
TMP42x_ConversionRate conversion_rate
Definition TMP42x.h:76
union tmp42x::_reg16 reg16
float n_eff1
Definition TMP42x.h:77
I2C_HandleTypeDef * hi2c
Definition TMP42x.h:70
uint8_t I2C_ADDR
Definition TMP42x.h:71
TMP42x_Range range
Definition TMP42x.h:75
TMP42x_Type type
Definition TMP42x.h:73
float n_eff3
Definition TMP42x.h:79
float n_eff2
Definition TMP42x.h:78
uint8_t raw_data[2]
Definition TMP42x.h:82
struct tmp42x::_reg16::_temp_register temp_register
struct tmp42x::_reg8::_n_correction_register n_correction_register
uint8_t raw_data
Definition TMP42x.h:91
struct tmp42x::_reg8::_man_id_register man_id_register
struct tmp42x::_reg8::_dev_id_register dev_id_register
struct tmp42x::_reg8::_conversion_rate_register conversion_rate_register
struct tmp42x::_reg8::_config2_register _config2_register
struct tmp42x::_reg8::_config1_register config1_register