-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp_275.c
414 lines (354 loc) · 10.5 KB
/
tmp_275.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* tmp_275.c
*
* Created on: Jan 21, 2019
* Author: Danyal
*/
#include "tmp_275.h"
#include "driverlib.h"
#ifdef DEBUG
#define I2C_MASTER_TX_CONFIG (I2C_MASTER_SEND_MODE | I2C_MDR_STP | \
I2C_MDR_FREE | I2C_MDR_STT | I2C_MDR_IRS )
#define I2C_MASTER_RX_CONFIG (I2C_MASTER_RECEIVE_MODE | I2C_MDR_STP | \
I2C_MDR_FREE | I2C_MDR_STT | I2C_MDR_IRS )
#else
#define I2C_MASTER_TX_CONFIG (I2C_MASTER_SEND_MODE | I2C_MDR_STP | I2C_MDR_STT | I2C_MDR_IRS)
#define I2C_MASTER_RX_CONFIG (I2C_MASTER_RECEIVE_MODE | I2C_MDR_STP | I2C_MDR_STT | I2C_MDR_IRS )
#endif
/* basics */
/*
* Read contents of a register from tmp275
* master sends 2 frames:
* 1. slave address
* 2. register pointer
* slave sends 3 frames:
* 1. slave address
* 2. data byte 1 MSB
* 3. data byte 2
*/
uint16_t tmp_275_read_byte(TMP275_I2C_MSG* msg)
{
uint32_t i2c_base = msg->i2c_module;
uint16_t i2c_slave = msg->slave_addr;
/*
* Wait until the STP bit is cleared from any previous master
* communication. Clearing of this bit by the module is delayed until after
* the SCD bit is set. If this bit is not checked prior to initiating a
* new message, the I2C could get confused.
*/
if(I2C_getStopConditionStatus(i2c_base))
{
return(TMP275_MSG_ERROR_STOP_NOT_READY);
}
I2C_setSlaveAddress(i2c_base, i2c_slave);
/* Check if bus busy */
if(I2C_isBusBusy(i2c_base))
{
return(TMP275_MSG_ERROR_BUS_BUSY);
}
/* Setup number of bytes to send message_buffer and register address */
I2C_setDataCount(i2c_base, 1);
/* Setup data to send */
I2C_putData(i2c_base, msg->reg_ptr);
I2C_setConfig(i2c_base, I2C_MASTER_TX_CONFIG);
// I2C_clearStatus(i2c_base, I2C_STS_NO_ACK);
while (I2C_getStopConditionStatus (i2c_base) != 0 );
/* re send slave address and start condition to initiate register read back */
I2C_setDataCount(i2c_base, 1);
I2C_setConfig(i2c_base, I2C_MASTER_RX_CONFIG);
msg->reg_val = I2C_getData(i2c_base);
I2C_sendStopCondition(i2c_base);
// I2C_sendNACK(i2c_base);
// NOP;
I2C_clearStatus(i2c_base, I2C_STS_NO_ACK);
return TMP275_MSG_SUCCESS;
}
/*
* write a byte to the tmp275
* length of 3 frames:
* 1. slave address
* 2. register pointer
* 3. data byte 1
*/
uint16_t tmp_275_write_byte(TMP275_I2C_MSG* msg)
{
uint32_t i2c_base = msg->i2c_module;
uint16_t i2c_slave = msg->slave_addr;
/*
* Wait until the STP bit is cleared from any previous master
* communication. Clearing of this bit by the module is delayed until after
* the SCD bit is set. If this bit is not checked prior to initiating a
* new message, the I2C could get confused.
*/
if(I2C_getStopConditionStatus(i2c_base))
{
return(TMP275_MSG_ERROR_STOP_NOT_READY);
}
I2C_setSlaveAddress(i2c_base, i2c_slave);
/* Check if bus busy */
if(I2C_isBusBusy(i2c_base))
{
return(TMP275_MSG_ERROR_BUS_BUSY);
}
/* Setup number of bytes to send message_buffer and register address */
I2C_setDataCount(i2c_base, 2);
/* Setup data to send */
I2C_putData(i2c_base, msg->reg_ptr);
/* load in value, upper 8 bits then lower 8 bits */
I2C_putData(i2c_base, ((msg->reg_val)&0x00FF));
/* Send start as master transmitter */
I2C_setConfig(i2c_base, I2C_MASTER_TX_CONFIG);
// I2C_setConfig(i2c_base, I2C_MASTER_SEND_MODE);
// I2C_sendStartCondition(i2c_base);
// I2C_sendStopCondition(i2c_base);
// I2C_clearStatus(I2CA_BASE, I2C_STS_NO_ACK);
return TMP275_MSG_SUCCESS;
}
/*
* Read contents of a register from tmp275
* master sends 2 frames:
* 1. slave address
* 2. register pointer
* slave sends 3 frames:
* 1. slave address
* 2. data byte 1 MSB
* 3. data byte 2
*/
uint16_t tmp_275_read_word(TMP275_I2C_MSG* msg)
{
uint32_t i2c_base = msg->i2c_module;
uint16_t i2c_slave = msg->slave_addr;
/*
* Wait until the STP bit is cleared from any previous master
* communication. Clearing of this bit by the module is delayed until after
* the SCD bit is set. If this bit is not checked prior to initiating a
* new message, the I2C could get confused.
*/
if(I2C_getStopConditionStatus(i2c_base))
{
return(TMP275_MSG_ERROR_STOP_NOT_READY);
}
I2C_setSlaveAddress(i2c_base, i2c_slave);
/* Check if bus busy */
if(I2C_isBusBusy(i2c_base))
{
return(TMP275_MSG_ERROR_BUS_BUSY);
}
/* Setup number of bytes to send message_buffer and register address */
I2C_setDataCount(i2c_base, 1);
/* Setup data to send */
I2C_putData(i2c_base, msg->reg_ptr);
I2C_setConfig(i2c_base, I2C_MASTER_TX_CONFIG);
// I2C_clearStatus(i2c_base, I2C_STS_NO_ACK);
while (I2C_getStopConditionStatus (i2c_base) != 0 );
/* resend slave address and start condition to initiate register read back */
I2C_setDataCount(i2c_base, 2);
I2C_setConfig(i2c_base, I2C_MASTER_RX_CONFIG);
while (I2C_getRxFIFOStatus (i2c_base) == I2C_FIFO_RXEMPTY);
msg->reg_val = (I2C_getData(i2c_base))<<8;
while (I2C_getRxFIFOStatus (i2c_base) == I2C_FIFO_RXEMPTY);
msg->reg_val |= I2C_getData(i2c_base);
I2C_sendStopCondition(i2c_base);
// I2C_sendNACK(i2c_base);
// NOP;
I2C_clearStatus(i2c_base, I2C_STS_NO_ACK);
return TMP275_MSG_SUCCESS;
}
/*
* write a word to the tmp275
* length of 4 frames:
* 1. slave address
* 2. register pointer
* 3. data byte 1 MSB
* 4. data byte 2
*/
uint16_t tmp_275_write_word(TMP275_I2C_MSG* msg)
{
uint32_t i2c_base = msg->i2c_module;
uint16_t i2c_slave = msg->slave_addr;
/*
* Wait until the STP bit is cleared from any previous master
* communication. Clearing of this bit by the module is delayed until after
* the SCD bit is set. If this bit is not checked prior to initiating a
* new message, the I2C could get confused.
*/
if(I2C_getStopConditionStatus(i2c_base))
{
return(TMP275_MSG_ERROR_STOP_NOT_READY);
}
I2C_setSlaveAddress(i2c_base, i2c_slave);
/* Check if bus busy */
if(I2C_isBusBusy(i2c_base))
{
return(TMP275_MSG_ERROR_BUS_BUSY);
}
/* Setup number of bytes to send message_buffer and register address */
I2C_setDataCount(i2c_base, TMP275_WRITE_FRAMES);
/* Setup data to send */
I2C_putData(i2c_base, msg->reg_ptr);
/* load in value, upper 8 bits then lower 8 bits */
I2C_putData(i2c_base, (((msg->reg_val)&0xFF00)>>8));
I2C_putData(i2c_base, ((msg->reg_val)&0x00FF));
/* Send start as master transmitter */
I2C_setConfig(i2c_base, I2C_MASTER_TX_CONFIG);
// I2C_setConfig(i2c_base, I2C_MASTER_SEND_MODE);
// I2C_sendStartCondition(i2c_base);
// I2C_sendStopCondition(i2c_base);
// I2C_clearStatus(I2CA_BASE, I2C_STS_NO_ACK);
return TMP275_MSG_SUCCESS;
}
/* utility */
static inline numeric c2f(const numeric deg_c)
{
return (deg_c*9.0/5.0)+32.0;
}
static inline numeric f2c(const numeric deg_f)
{
return (deg_f-32.0)*5.0/9.0;
}
uint16_t tmp2775_c2bin(const numeric deg_c)
{
const int16_t res = (int16_t)(deg_c/TEMP_PRECISION_C);
return res;
}
uint16_t tmp275_f2bin(const numeric deg_f)
{
int16_t res = (int16_t)(f2c(deg_f)/TEMP_PRECISION_C);
return res;
}
numeric tmp275_bin2c(const uint16_t temp_code)
{
numeric res = (numeric)(temp_code*TEMP_PRECISION_C);
return res;
}
numeric tmp275_bin2f(const uint16_t temp_code)
{
numeric res = (numeric)(temp_code*TEMP_PRECISION_C);
return c2f(res);
}
/*
* determines conversion time based from device bits ratio
* from table 6 of TMP275 datasheet: http://www.ti.com/lit/ds/symlink/tmp275.pdf
*
*/
uint16_t tmp275_conversion_time(TMP275* dev)
{
uint16_t status = (dev->config && 0x60)>>5;
uint16_t conv_time = 0;
switch(status) // mask off bits of interest
{
case 0:
conv_time = 28; // 27.5ms -> 28ms
break;
case 1:
conv_time = 55;
break;
case 2:
conv_time = 110;
break;
case 3:
conv_time = 220;
break;
default:
conv_time = 0;
}
return conv_time;
}
/* convenience */
/*
* set configuration register settings
*/
uint16_t tmp275_set_config(TMP275* dev, uint_fast8_t config)
{
uint16_t status;
dev->config = config;
TMP275_I2C_MSG config_msg = {
.i2c_module=dev->i2c_module,
.slave_addr=dev->slave_addr,
.reg_ptr=TMP275_CONFIG_REG,
.reg_val=(uint16_t)(config),
.status=TMP275_MSG_STATUS_SEND_WITHSTOP
};
do
{
status = tmp_275_write_byte(&config_msg);
}
while(status != TMP275_MSG_SUCCESS);
return status;
}
/*
* set low temp threshold register value
*/
uint16_t tmp275_set_tlow(TMP275* dev, int16_t val)
{
uint16_t status;
TMP275_I2C_MSG tlow_msg = {
.i2c_module=dev->i2c_module,
.slave_addr=dev->slave_addr,
.reg_ptr=TMP275_TLOW_REG,
.reg_val=val,
.status=TMP275_MSG_STATUS_SEND_WITHSTOP
};
do
{
status = tmp_275_write_word(&tlow_msg);
}
while(status != TMP275_MSG_SUCCESS);
return status;
}
/*
* set high temp threshold register value
*/
uint16_t tmp275_set_thigh(TMP275* dev, int16_t val)
{
uint16_t status;
TMP275_I2C_MSG thigh_msg = {
.i2c_module=dev->i2c_module,
.slave_addr=dev->slave_addr,
.reg_ptr=TMP275_THIGH_REG,
.reg_val=val,
.status=TMP275_MSG_STATUS_SEND_WITHSTOP
};
do
{
status = tmp_275_write_word(&thigh_msg);
}
while(status != TMP275_MSG_SUCCESS);
return status;
}
numeric tmp275_get_temp_c(TMP275* dev)
{
uint16_t status;
TMP275_I2C_MSG read_temp_msg = {
.status = TMP275_MSG_STATUS_SEND_NOSTOP,
.slave_addr = dev->slave_addr,
.i2c_module=dev->i2c_module,
.reg_ptr = TMP275_TEMP_REG
};
do
{
status = (tmp_275_read_word(&read_temp_msg));
}
while(status != TMP275_MSG_SUCCESS);
numeric result = tmp275_bin2c(read_temp_msg.reg_val>>4); // only upper 12 bits, right shift preserves sign
return result;
}
numeric tmp275_get_temp_c_average(TMP275* dev, const uint_fast8_t num_averages)
{
uint_fast8_t i;
numeric result = 0.0f;
for (i=0;i<num_averages;i++)
{
result += tmp275_get_temp_c(dev);
}
result /= num_averages;
return result;
}
numeric tmp275_get_temp_f(TMP275* dev)
{
return c2f(tmp275_get_temp_c(dev));
}
numeric tmp275_get_temp_average_f(TMP275* dev, const uint_fast8_t num_averages)
{
return c2f(tmp275_get_temp_c_average(dev, num_averages));
}