-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment2.c
636 lines (478 loc) · 20.2 KB
/
Assignment2.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#include "main.h"
#include "Time_Delays.h"
#include "Clk_Config.h"
#include "LCD_Display.h"
#include <stdio.h>
#include <string.h>
#include "Small_7.h"
#include "Arial_9.h"
#include "Arial_12.h"
#include "Arial_24.h"
#include "stm32f4xx_ll_crc.h"
/*
This program flashes an LED to test the nucleo board, while including the header files and '#defines' that
you will need for the later parts of the lab. Use this project as a template for the remaining tasks of the lab.
*/
//Temperature Sensor I2C Address
#define TEMPADR 0x90
//EEPROM I2C Address
#define EEPROMADR 0xA0
uint32_t joystick_centre(void);
uint32_t joystick_right(void);
uint32_t joystick_left(void);
uint32_t joystick_up(void);
uint32_t joystick_down(void);
uint16_t Read_Temperature_Sensor(void);
uint16_t Read_Temp_EEPROM_TO_Memory(void);
void Write_To_EEPROM(uint16_t temperature);
void Write_to_EEPROM_Array(uint8_t * Packet_Array, void(*Ack_Polling)(void));
void Read_Packet_From_EEPROM_DisplayLCD(uint8_t * Packet_Array);
void Write_Packet_to_EEPROM_ACK_Polling(void);
uint32_t CRC_Check(uint8_t * Packet_Array);
int main(void){
//Initialization
SystemClock_Config();/* Configure the system clock to 84.0 MHz */
SysTick_Config_MCE2(us);
char outputString [18];
uint16_t Temp_Read = 0;
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
uint32_t FCS = 0;
uint32_t FCS_Check=0;
uint8_t Packet_Array[60];
for (int i = 0; i<6; i++){
Packet_Array[i] = 0xaa; //MAC Destinatiomn
}
for (int t = 6; t<12; t++){
Packet_Array[t] = 0xbb; //MAC Source
}
Packet_Array[12] = 0x00; //Length Lower Byte
Packet_Array[13] = 0x2e; //Length Higher Byte
for (int u = 14; u<60; u++){ //Payload
Packet_Array[u] = 0;
}
//Configure LED: GPIO Port B, pin 4
LL_GPIO_SetPinMode (GPIOB, LL_GPIO_PIN_4, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType (GPIOB, LL_GPIO_PIN_4, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull (GPIOB, LL_GPIO_PIN_4, LL_GPIO_PULL_NO);
LL_GPIO_SetPinSpeed (GPIOB, LL_GPIO_PIN_4, LL_GPIO_SPEED_FREQ_HIGH);
//JOYSTICK CONFIGURATION
//Configuring Center Joystick (PORTB,PIN5)
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB); //Enables peripheral clock for PORT B
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_5, LL_GPIO_MODE_INPUT); //Sets B5 as an Input
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_5, LL_GPIO_PULL_NO); //Sets B5 as NO pull
//Configuring Left Joystick (PORTC, PIN1)
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC); //Enables peripheral clock for Port C
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_1, LL_GPIO_MODE_INPUT); //Configures C1 as Input (subject to user press)
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_1, LL_GPIO_PULL_NO); //Sets C1 as NO pull
//Configuring Right Joystick (PORTC, PIN0)
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC); //Enables peripheral clock for Port C
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_0, LL_GPIO_MODE_INPUT); //Configures C0 as Input (subject to user press)
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_0, LL_GPIO_PULL_NO); //Sets C0 as NO pull
//Configuring Down Joystick (PORTB, PIN0)
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB); //Enables peripheral clock for Port B
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_0, LL_GPIO_MODE_INPUT); //Configures B0 as Input (subject to user press)
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_0, LL_GPIO_PULL_NO); //Sets B0 as NO pull
//Configuring Up Joystick (PORTA, PIN4)
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA); //Enables peripheral clock for Port A
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_4, LL_GPIO_MODE_INPUT); //Configures A4 as Input (subject to user press)
LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_4, LL_GPIO_PULL_NO); //Sets A4 as NO pull
//CONFIGURING LCD
Configure_LCD_Pins();
Configure_SPI1();
Activate_SPI1();
Clear_Screen();
Initialise_LCD_Controller();
set_font((unsigned char*)Small_7);
//Configuring SDA as : Alternate, Open Drain, Pull-Up; SDA = Data Line
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_9, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(GPIOB, LL_GPIO_PIN_9, LL_GPIO_AF_4);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_9, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_9, LL_GPIO_PULL_UP);
//Configuring SCL as Alternate function, Open Drainm, Pull Up; SCL = Clock
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_8, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(GPIOB, LL_GPIO_PIN_8, LL_GPIO_AF_4);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_8, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_8, LL_GPIO_PULL_UP);
//Enable the I2C1 Peripheral for us; Sets up Mode and Clock
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
LL_I2C_Disable(I2C1); //disable I2C1 prior to configuration
LL_I2C_SetMode(I2C1, LL_I2C_MODE_I2C);
LL_I2C_ConfigSpeed(I2C1, 84000000, 100000, LL_I2C_DUTYCYCLE_2); //set speed at 100kHz
LL_I2C_Enable(I2C1); // re-enable I2C1
//Enable CRC Checking
LL_AHB1_GRP1_EnableClock (LL_AHB1_GRP1_PERIPH_CRC);
int Down_Pos = 0; //Will drive Up/Down Joystick switch-statement
int x = 0, y = 10;
sprintf (outputString, "MAC Dest:");
put_string(0,0,outputString);
for (int i =0; i<6 ;i++){
sprintf(outputString, "%x", Packet_Array[i]);
put_string(x, y, outputString);
x += 20;
}
while(1){
//Display MAC Destination
//Centre Pressed -> Read Temperature from sensor over I2C, store in memory and display in on LCD screen
if(joystick_centre() == 1){ //Display value of Temperature Sensor on LCD.
Clear_Screen();
Temp_Read = Read_Temperature_Sensor();
//Split Temperature in 2 Bytes.
uint8_t Temp_Read_LSB = Temp_Read & 0x00ff;
uint8_t Temp_Read_MSB = (Temp_Read & 0xff00)>>8;
//Print temperature sensor value
sprintf (outputString, "Temp: %f", Temp_Read*0.125);
put_string(10,10, outputString);
LL_mDelay(500000);
//Store Temperature bytes in 2 first Payload elements
Packet_Array[14] = Temp_Read_MSB;
Packet_Array[15] = Temp_Read_LSB;
Clear_Screen();
sprintf (outputString, "Stored in Pld!");
put_string(0,10, outputString);
LL_mDelay(1000000);
Clear_Screen();
FCS = CRC_Check(Packet_Array);
sprintf (outputString, "CRC CHECK:");
put_string(0,0, outputString);
sprintf (outputString, "%x", FCS);
put_string(0,10, outputString);
LL_mDelay(500000);
}
else if (joystick_right()) { //Write packet to EEPROM
Clear_Screen();
sprintf (outputString, "Storing...");
put_string(10,10, outputString);
Write_to_EEPROM_Array(Packet_Array, Write_Packet_to_EEPROM_ACK_Polling);
LL_mDelay(500000);
Clear_Screen();
sprintf (outputString, "Stored in EEPROM.");
put_string(0,10, outputString);
LL_mDelay(500000);
}
else if (joystick_left()) { //Read value from EEPROM and display it on the LCD screen.
Clear_Screen();
Read_Packet_From_EEPROM_DisplayLCD(Packet_Array);
//Payload_Temp = (Packet_Array[14] + Packet_Array[15]);
//sprintf (outputString, "%f", Payload_Temp*0.125);
//put_string(10,10, outputString);
LL_mDelay(500000);
}
else if (joystick_down()) {
Clear_Screen();
uint16_t Payload_Temp;
if (Down_Pos<5)
Down_Pos+=1;
switch(Down_Pos){
case 1: //MAC Dest
//Down_Pos++;
Clear_Screen();
x = 0, y = 10;
//sprintf (outputString, "DP: 1");
//put_string(0,10,outputString);
sprintf (outputString, "MAC Dest:");
put_string(0,0,outputString);
for (int i =0; i<6 ;i++){
sprintf(outputString, "%x", Packet_Array[i]);
put_string(x, y, outputString);
x += 20;
}
LL_mDelay(500000);
break;
case 2: //MAC Dest-->MAC Src
//Down_Pos++;
Clear_Screen();
x = 0, y = 10;
//sprintf (outputString, "DP: 1");
//put_string(0,10,outputString);
sprintf (outputString, "MAC SRC:");
put_string(0,0,outputString);
for (int i =6; i<12 ;i++){
sprintf(outputString, "%x", Packet_Array[i]);
put_string(x, y, outputString);
x += 20;
}
LL_mDelay(500000);
break;
case 3: //MAX Src --> Length
Clear_Screen();
x = 0, y = 10;
sprintf (outputString, "Length");
put_string(0,0,outputString);
for (int i =12; i<14 ;i++){
sprintf(outputString, "%x", Packet_Array[i]);
put_string(x, y, outputString);
x += 30;
}
LL_mDelay(500000);
break;
case 4: //Length --> Payload --> Payload
Clear_Screen();
Payload_Temp = (Packet_Array[14] + Packet_Array[15]);
sprintf (outputString, "Temp C°:%f", Payload_Temp*0.125);
put_string(10,10, outputString);
LL_mDelay(500000);
break;
case 5:
Clear_Screen();
FCS_Check = CRC_Check(Packet_Array);
LL_mDelay(500000);
if(FCS == FCS_Check){
sprintf (outputString, "CRC Check good!");
put_string(0,0, outputString);
LL_mDelay(500000);
Clear_Screen();
sprintf (outputString, "%x", FCS_Check);
put_string(0,10, outputString);
}
else
sprintf (outputString, "CRC Check bad!");
put_string(0,0, outputString);
}
// }
}
else if(joystick_up()){
uint16_t Payload_Temp;
if (Down_Pos > 1 )
Down_Pos-=1;
switch(Down_Pos) {
case 1: //Mac Src --> Mac Dest ---> Mac Dest
Clear_Screen();
x = 0, y = 10;
sprintf (outputString, "MAC Dest:");
put_string(0,0,outputString);
for (int t =0; t<6 ;t++){
sprintf(outputString, "%x", Packet_Array[t]);
put_string(x, y, outputString);
x += 20;
}
LL_mDelay(500000);
break;
case 2: //Length --> Mac Src
Clear_Screen();
x = 0, y = 10;
sprintf (outputString, "MAC SRC:");
put_string(0,0,outputString);
for (int i =6; i<12 ;i++){
sprintf(outputString, "%x", Packet_Array[i]);
put_string(x, y, outputString);
x += 20;
}
LL_mDelay(500000);
break;
case 3: //Payload --> Length
Clear_Screen();
x = 0, y = 10;
sprintf (outputString, "Length");
put_string(0,0,outputString);
for (int i =12; i<14 ;i++){
sprintf(outputString, "%x", Packet_Array[i]);
put_string(x, y, outputString);
x += 20;
}
LL_mDelay(500000);
break;
case 4: //CRC Check --> Payload
Clear_Screen();
LL_mDelay(500000);
Payload_Temp = (Packet_Array[14] + Packet_Array[15]);
sprintf (outputString, "Temp C°:%f", Payload_Temp*0.125);
put_string(10,10, outputString);
LL_mDelay(500000);
break;
}
}
/** else {
LL_mDelay(500000);
Clear_Screen();
int x = 0, y = 10;
sprintf (outputString, "MAC Dest:");
put_string(0,0,outputString);
for (int i =0; i<6 ;i++){
sprintf(outputString, "%x", Packet_Array[i]);
put_string(x, y, outputString);
x += 20;
}
}**/
}
}
uint32_t joystick_centre(void) {
return (LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_5)); //Returns 1 if the joystick is pressed in the centre, 0 otherwise
}
uint32_t joystick_left(void){
return (LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_1));
}
uint32_t joystick_right(void){
return (LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_0));
}
uint32_t joystick_up(void){
return (LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_4));
}
uint32_t joystick_down(void){
return (LL_GPIO_IsInputPinSet(GPIOB, LL_GPIO_PIN_0));
}
//void Write_Temp_To_EEPROM(uint16_t temperature) {
//}
uint16_t Read_Temperature_Sensor(void) {
uint16_t temperature = 0; //to store the temperature value
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1)); //ACK for Start Condition
LL_I2C_TransmitData8(I2C1, TEMPADR); //Read 1 byte From Temperature Sensor Address
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_TransmitData8(I2C1, 0x00); //Set pointer register to the temperature register
while(!LL_I2C_IsActiveFlag_TXE(I2C1)); //TXE flag confirms ACK
LL_I2C_GenerateStartCondition(I2C1); //Re-Start if you want to read from an address; i.e. re-generate a Start Condition
while(!LL_I2C_IsActiveFlag_SB(I2C1));//Ack of Restart condition
LL_I2C_TransmitData8(I2C1, TEMPADR+1); //Read byte from the next pointer address
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Ack needed
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK); //Ack the incoming data
while(!LL_I2C_IsActiveFlag_RXNE(I2C1)); //Wait until complete
temperature = LL_I2C_ReceiveData8(I2C1);
temperature = temperature << 8; // shift to upermost bits
LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK); //ack next byte received
while(!LL_I2C_IsActiveFlag_RXNE(I2C1)); //wait until complete
temperature += LL_I2C_ReceiveData8(I2C1); //combine temperature values
LL_I2C_GenerateStopCondition(I2C1); //end transmissions sequence
temperature = temperature >>5; //Shift temperature back to the lower 5 bits
return temperature;
}
void Write_Packet_to_EEPROM_ACK_Polling(void){
LL_I2C_ClearFlag_AF (I2C1);
while(1){
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1)); //ACK for Start Condition
LL_I2C_TransmitData8(I2C1, EEPROMADR); //Focus on EEPROM
//while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_mDelay(100);
if (LL_I2C_IsActiveFlag_AF (I2C1)==0)
break;
LL_I2C_ClearFlag_AF (I2C1);
}
}
void Write_to_EEPROM_Array(uint8_t * Packet_Array, void (*Ack_Polling)(void)){
//Write the Packet Array to EEPROM
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1)); //ACK for Start Condition
LL_I2C_TransmitData8(I2C1, EEPROMADR); //Focus on EEPROM
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_TransmitData8(I2C1, 0x00); //Set pointer register to the EEPROM internal register
while(!LL_I2C_IsActiveFlag_TXE(I2C1)); //TXE flag confirms ACK
LL_I2C_TransmitData8(I2C1, 0x00);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
for (int i=0; i<32; i++){
LL_I2C_TransmitData8(I2C1, Packet_Array[i]);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
}
LL_I2C_GenerateStopCondition(I2C1);
Ack_Polling();
//LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
//while(!LL_I2C_IsActiveFlag_SB(I2C1)); //ACK for Start Condition
//LL_I2C_TransmitData8(I2C1, EEPROMADR); //Focus on EEPROM
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_TransmitData8(I2C1, 0x00); //Set pointer register to the EEPROM internal register
while(!LL_I2C_IsActiveFlag_TXE(I2C1)); //TXE flag confirms ACK
LL_I2C_TransmitData8(I2C1, 0x20);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
for (int i=32; i<60; i++){
LL_I2C_TransmitData8(I2C1, Packet_Array[i]);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
}
LL_I2C_GenerateStopCondition(I2C1);
}
void Write_To_EEPROM(uint16_t temperature){
//WRITE FROM MEMORY TO EEPROM ADDRESS.
uint8_t temperatureLSB = (temperature & 0x00FF);
uint16_t temperatureHighByte = (temperature & 0xFF00);
uint8_t temperatureMSB = temperatureHighByte >> 8;
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1)); //ACK for Start Condition
LL_I2C_TransmitData8(I2C1, EEPROMADR); //Focus on EEPROM
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_TransmitData8(I2C1, 0x00); //Set pointer register to the EEPROM internal register
while(!LL_I2C_IsActiveFlag_TXE(I2C1)); //TXE flag confirms ACK
LL_I2C_TransmitData8(I2C1, 0x00);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
LL_I2C_TransmitData8(I2C1, temperatureMSB);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
LL_I2C_TransmitData8(I2C1, temperatureLSB);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
LL_I2C_GenerateStopCondition(I2C1);
}
void Read_Packet_From_EEPROM_DisplayLCD(uint8_t * Packet_Array){
char outputString[18];
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1)); //ACK for Start Condition
LL_I2C_TransmitData8(I2C1, EEPROMADR); //Focus on EEPROM
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_TransmitData8(I2C1, 0x00); //Set pointer register to the EEPROM internal register
while(!LL_I2C_IsActiveFlag_TXE(I2C1)); //TXE flag confirms ACK
LL_I2C_TransmitData8(I2C1, 0x00);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1));
LL_I2C_TransmitData8(I2C1, EEPROMADR+1); //Focus on EEPROM
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
for (int i=0; i<60; i++){
LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK); //Ack the incoming data
while(!LL_I2C_IsActiveFlag_RXNE(I2C1)); //Wait until complete
Packet_Array[i] = LL_I2C_ReceiveData8(I2C1);
}
LL_I2C_GenerateStopCondition(I2C1); //end transmission
uint16_t Payload_Temp;
Payload_Temp = (Packet_Array[14] + Packet_Array[15]);
sprintf (outputString, "EEPROM VAL:");
put_string(0,0,outputString);
sprintf (outputString, "%f", Payload_Temp*0.125);
put_string(10,10, outputString);
}
uint32_t CRC_Check(uint8_t * Packet_Array) {
LL_CRC_ResetCRCCalculationUnit (CRC);
uint32_t FCS = 0;
for (int i = 0; i<60; i++){
FCS+=Packet_Array[i];
if (i%4 == 0)
LL_CRC_FeedData32(CRC, FCS);
LL_mDelay(50);
}
uint32_t CRC_Result = LL_CRC_ReadData32(CRC);
return CRC_Result;
//sprintf (outputString, "CRC CHECK:");
//put_string(0,0,outputString);
//sprintf (outputString, "%x", CRC_Result);
//put_string(10,10, outputString);
}
uint16_t Read_Temp_EEPROM_TO_Memory(void){
uint16_t temperature = 0;
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1)); //ACK for Start Condition
LL_I2C_TransmitData8(I2C1, EEPROMADR); //Focus on EEPROM
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_TransmitData8(I2C1, 0x00); //Set pointer register to the EEPROM internal register
while(!LL_I2C_IsActiveFlag_TXE(I2C1)); //TXE flag confirms ACK
LL_I2C_TransmitData8(I2C1, 0x00);
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
LL_I2C_GenerateStartCondition(I2C1); //Generate Start condition for start sequence
while(!LL_I2C_IsActiveFlag_SB(I2C1));
LL_I2C_TransmitData8(I2C1, EEPROMADR+1); //Focus on EEPROM
while(!LL_I2C_IsActiveFlag_ADDR(I2C1)); //Wait for ADDR flag to confirm ACK
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK); //Ack the incoming data
while(!LL_I2C_IsActiveFlag_RXNE(I2C1)); //Wait until complete
uint16_t temperatureLSB = LL_I2C_ReceiveData8(I2C1) << 8;
//temperature = temperature << 8; // shift to upermost bits
LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK); //ack next byte received
while(!LL_I2C_IsActiveFlag_RXNE(I2C1)); //wait until complete
uint16_t temperatureMSB = LL_I2C_ReceiveData8(I2C1); //combine temperature values
LL_I2C_GenerateStopCondition(I2C1); //end transmissions sequence
//temperature = temperature >>5; //Shift temperature back to the lower 5 bits
temperature = temperatureLSB + temperatureMSB;
return temperature;
}