-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_temp.c
329 lines (254 loc) · 9.3 KB
/
i2c_temp.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
/*
* i2c_tmp.c
*
* Created on: 14/12/2023
* Author: carlos Almeida and benny
*/
#include "i2c_temp.h"
#include "lcd_task.h"
// Queue that stores most recent temperature
xQueueHandle g_pI2cTempQueue;
float temperature=0;
float temperature_conv=0;
// Command that initializes sensor, defines the resolution, etc... (Temperature sensor TMP101)
uint32_t command_tmp101=0b01100000;
uint8_t i;
//initialize I2C module 0
void InitI2C0(void)
{
//enable I2C module 0
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
//reset module
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);
//enable GPIO peripheral that contains I2C 0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Configure the pin muxing for I2C0 functions on port B2 and B3.
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// Select the I2C function for these pins.
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
// Enable and initialize the I2C0 master module. Use the system clock for
// the I2C0 module.
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), true);
}
/*
//sends an I2C command to the specified slave
void I2CSend(uint8_t slave_addr, uint8_t num_of_args, ...)
{
// Tell the master module what address it will place on the bus when
// communicating with the slave.
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);
//stores list of variable number of arguments
va_list vargs;
//specifies the va_list to "open" and the last fixed argument
//so vargs knows where to start looking
va_start(vargs, num_of_args);
//put data to be sent into FIFO
I2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint32_t));
//if there is only one argument, we only need to use the
//single send I2C function
if(num_of_args == 1)
{
//Initiate send of data from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
//"close" variable argument list
va_end(vargs);
}
//otherwise, we start transmission of multiple bytes on the
//I2C bus
else
{
//Initiate send of data from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
//send num_of_args-2 pieces of data, using the
//BURST_SEND_CONT command of the I2C module
for(i = 1; i < (num_of_args - 1); i++)
{
//put next piece of data into I2C FIFO
I2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint32_t));
//send next data that was just placed into FIFO
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
}
//put last piece of data into I2C FIFO
I2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint32_t));
//send next data that was just placed into FIFO
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
//"close" variable args list
va_end(vargs);
}
}
//sends an array of data via I2C to the specified slave
void I2CSendString(uint32_t slave_addr, char array[])
{
// Tell the master module what address it will place on the bus when
// communicating with the slave.
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);
//put data to be sent into FIFO
I2CMasterDataPut(I2C0_BASE, array[0]);
//if there is only one argument, we only need to use the
//single send I2C function
if(array[1] == '\0')
{
//Initiate send of data from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
}
//otherwise, we start transmission of multiple bytes on the
//I2C bus
else
{
//Initiate send of data from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
//initialize index into array
uint8_t i = 1;
//send num_of_args-2 pieces of data, using the
//BURST_SEND_CONT command of the I2C module
while(array[i + 1] != '\0')
{
//put next piece of data into I2C FIFO
I2CMasterDataPut(I2C0_BASE, array[i++]);
//send next data that was just placed into FIFO
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
}
//put last piece of data into I2C FIFO
I2CMasterDataPut(I2C0_BASE, array[i]);
//send next data that was just placed into FIFO
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
}
}
//read specified register on slave device
uint32_t I2CReceive(uint32_t slave_addr, uint8_t reg)
{
//specify that we are writing (a register address) to the
//slave device
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);
//specify register to be read
I2CMasterDataPut(I2C0_BASE, reg);
//send control byte and register address byte to slave device
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
//wait for MCU to finish transaction
while(I2CMasterBusy(I2C0_BASE));
//specify that we are going to read from slave device
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, true);
//send control byte and read from the register we
//specified
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
//wait for MCU to finish transaction
while(I2CMasterBusy(I2C0_BASE));
//return data pulled from the specified register
return I2CMasterDataGet(I2C0_BASE);
}
//sends an I2C command to the specified slave
void I2CSendByte(uint8_t slave_addr, uint32_t byte)
{
// Tell the master module what address it will place on the bus when
// communicating with the slave.
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);
//put data to be sent into FIFO
I2CMasterDataPut(I2C0_BASE, byte);
//Initiate send of data from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
}
*/
// Writes 8 bits to SDA of I2C
uint32_t I2C_write8BitRegister(uint8_t slaveAddress, uint32_t addressPointer, uint8_t firstByte)
{
I2CMasterSlaveAddrSet(I2C0_BASE, slaveAddress, false);
I2CMasterDataPut(I2C0_BASE, addressPointer);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_BASE))
{
}
I2CMasterDataPut(I2C0_BASE, firstByte);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_BASE))
{
}
return 0;
}
// Reads 8 bits from a register twice
uint32_t I2C_read16BitRegister(uint8_t slaveAddress, uint8_t registerAddress)
{
I2CMasterSlaveAddrSet(I2C0_BASE, slaveAddress, false); // set slave address
I2CMasterDataPut(I2C0_BASE, registerAddress); // set register to be read
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C0_BASE))
{
}
/*---------------------------------READ-Section----------------------------------------*/
I2CMasterSlaveAddrSet(I2C0_BASE, slaveAddress, true); // set slave address
//read first Byte
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C0_BASE))
{
}
uint32_t msb=I2CMasterDataGet(I2C0_BASE);
//read second Byte
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
while(I2CMasterBusy(I2C0_BASE))
{
}
uint32_t lsb=I2CMasterDataGet(I2C0_BASE);
return ((msb << 8) | lsb);
}
static void I2cTempTask(void *pvParameters)
{
portTickType ui32WakeTime;
//
// Get the current tick count.
//
ui32WakeTime = xTaskGetTickCount();
//
// Loop forever.
//
while(1)
{
if (initiated>0){
// reads temperature from temperature register of tmp101
temperature=I2C_read16BitRegister(TMP101_ADDRESS,TMP101_TEMP_REG);
temperature_conv=temperature/256; // conversion binary * 0.0625 (binary/16) /16 again
// Sends converted float to a queue, it overwrites the previous value on the queue, as this is a one slot queue
// And the value is only accessed with peeks
xQueueOverwrite(g_pI2cTempQueue, &temperature_conv);
}
vTaskDelayUntil(&ui32WakeTime, 1000 / portTICK_RATE_MS);
}
}
uint32_t
I2cTempTaskInit(void)
{
// I2C init
InitI2C0();
// Needs hard delay, as this isnt a task yet
SysCtlDelay(2000);
I2C_write8BitRegister(TMP101_ADDRESS,TMP101_config_REG,command_tmp101);
//
// Create the Temperature task.
//
if(xTaskCreate(I2cTempTask, (const portCHAR *)"I2c Temperature", TEMPSTACKSIZE, NULL,tskIDLE_PRIORITY + PRIORITY_TEMP_TASK, NULL) != pdTRUE)
{
return(1);
}
//
// Success.
//
return(0);
}