-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32f407xx_i2c_driver.c
1100 lines (963 loc) · 30.8 KB
/
stm32f407xx_i2c_driver.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* File : stm32f407xx_i2c_driver.c
* Brief : STM32F407xx MCU specific I2C driver source file
* Author ; Kyungjae Lee
* Date : Jun 09, 2023
*
* Note : This code includes only the features that are necessary for my
* personal projects.
* ****************************************************************************/
#include "stm32f407xx.h"
/* Declaration of I2C peripheral driver private functions */
static void I2C_ExecuteAddressPhaseRead(I2C_TypeDef *pI2Cx, uint8_t slaveAddr);
static void I2C_ExecuteAddressPhaseWrite(I2C_TypeDef *pI2Cx, uint8_t slaveAddr);
static void I2C_ClearADDRFlag(I2C_Handle_TypeDef *pI2CHandle);
/*******************************************************************************
* APIs supported by the I2C driver
* (See function definitions for more information)
******************************************************************************/
/**
* I2C_PeriControl()
* Brief : Enables or disables I2C peripheral
* Param : @pI2Cx - base address of I2Cx peripheral
* @state - ENABLE or DISABLE macro
* Retval : None
* Note : N/A
*/
void I2C_PeriControl(I2C_TypeDef *pI2Cx, uint8_t state)
{
if (state == ENABLE)
{
pI2Cx->CR1 |= (1 << I2C_CR1_PE);
}
else
{
pI2Cx->CR1 &= ~(1 << I2C_CR1_PE);
}
} /* End of I2C_PeriControl */
/**
* I2C_PeriClockControl()
* Brief : Enable or disable clock for I2C peripheral
* Param : @pI2Cx - base address of I2Cx peripheral
* Retval : None
* Note : N/A
*/
void I2C_PeriClockControl(I2C_TypeDef *pI2Cx, uint8_t state)
{
if (state == ENABLE)
{
if (pI2Cx == I2C1)
I2C1_PCLK_EN();
else if (pI2Cx == I2C2)
I2C2_PCLK_EN();
else if (pI2Cx == I2C3)
I2C3_PCLK_EN();
}
else
{
if (pI2Cx == I2C1)
I2C1_PCLK_DI();
else if (pI2Cx == I2C2)
I2C2_PCLK_DI();
else if (pI2Cx == I2C3)
I2C3_PCLK_DI();
}
} /* End of I2C_PeriClockControl */
/**
* I2C_Init()
* Brief : Initializes passed I2C peripheral
* Param : @pI2CHandle - pointer to I2C peripheral handle
* Retval : None
* Note : N/A
*/
void I2C_Init(I2C_Handle_TypeDef *pI2CHandle)
{
uint32_t temp = 0;
/* Enable I2C1 peripheral clock */
I2C_PeriClockControl(pI2CHandle->pI2Cx, ENABLE);
/* Configure I2C_CR1 (Enable ACK) */
//temp |= pI2CHandle->I2C_Config.I2C_ACKEnable << I2C_CR1_ACK;
//pI2CHandle->pI2Cx->CR1 = temp;
// Commented out since ACK bit cannot be set when PE=0. This will be done
// in the application.
// (Consider moving this logic into 'I2C_PeriControl()' function. )
/* Configure I2C_CR2 (FREQ[5:0])
* The FREQ bits must be configured with the APB clock frequency value (I2C
* peripheral connected to APB). The FREQ field is used by the peripheral
* to generate data setup and hold times compliant with the I2C
* specifications. The minimum allowed frequency is 2 MHz, the maximum
* frequency is limited by the maximum APB frequency and cannot exceed
* 50 MHz (peripheral intrinsic maximum limit).
*
* 0b000000: Not allowed
* 0b000001: Not allowed
* 0b000010: 2 MHz
* ...
* 0b110010: 50 MHz
* Higher than 0b101010: Not allowed
*/
temp = 0;
temp |= RCC_GetPCLK1Value() / 1000000; /* e.g., 16 MHz -> 16 */
pI2CHandle->pI2Cx->CR2 |= (temp & 0x3F); /* Masking for safety purpose */
/* Configure OAR1 (Device own address) */
temp = 0;
temp |= pI2CHandle->I2C_Config.I2C_DeviceAddress << I2C_OAR1_ADD71;
/* bit[0] of OAR1 is used only when the own address is of 10-bit long
* instead of 7-bit. big[15] of OAR1 determines the length of the
* address (7 or 10). 7-bit length by default.
*/
temp |= (0x1 << 14);
/* bit[14] should always be kept at 1 by software (Reference manual) */
pI2CHandle->pI2Cx->OAR1 |= temp;
/* Configure CCR (Calaulate CCR) */
uint16_t ccrVal = 0;
temp = 0;
if (pI2CHandle->I2C_Config.I2C_SCLSpeed <= I2C_SCL_SPEED_SM)
{
/* Standard mode (I2C_CCR bit[15] == 0) */
ccrVal = RCC_GetPCLK1Value() / (2 * pI2CHandle->I2C_Config.I2C_SCLSpeed);
temp |= (ccrVal & 0xFFF); /* 12-bit value */
}
else
{
/* Fast mode (I2C_CCR bit[15] == 1) */
temp |= (0x1 << I2C_CCR_FS);
temp |= (pI2CHandle->I2C_Config.I2C_FMDutyCycle << I2C_CCR_DUTY);
if (pI2CHandle->I2C_Config.I2C_FMDutyCycle == I2C_FM_DUTY_2)
{
ccrVal = RCC_GetPCLK1Value() / (3 * pI2CHandle->I2C_Config.I2C_SCLSpeed);
}
else
{
ccrVal = RCC_GetPCLK1Value() / (25 * pI2CHandle->I2C_Config.I2C_SCLSpeed);
}
temp |= (ccrVal & 0xFFF); /* 12-bit value */
}
pI2CHandle->pI2Cx->CCR |= temp;
/* TRISE configuration */
if (pI2CHandle->I2C_Config.I2C_SCLSpeed <= I2C_SCL_SPEED_SM)
{
/* Standard mode */
temp = (RCC_GetPCLK1Value() / 1000000U) + 1;
}
else
{
/* Fast mode */
temp = ((RCC_GetPCLK1Value() * 300U) / 1000000000U) + 1;
}
pI2CHandle->pI2Cx->TRISE = (temp & 0x3F);
} /* End of I2C_Init */
/**
* I2C_DeInit()
* Brief : Deinitializes passed I2C peripheral
* Param : @pI2Cx - base address of I2Cx peripheral
* Retval : None
* Note : N/A
*/
void I2C_DeInit(I2C_TypeDef *pI2Cx)
{
/* Do nothing */
} /* End of I2C_DeInit */
/**
* I2C_MasterTxBlocking()
* Brief : Handles blocking-based I2C transmission
* Param : @pI2CHandle - pointer to I2C peripheral handle
* @pTxBuffer - address of the Tx buffer
* @len - length of the data to transmit
* @slaveAddr - slave address
* @repeatedStartState - I2C_REPEATED_START_EN or DI
* Retval : None
* Note : N/A
*/
void I2C_MasterTxBlocking(I2C_Handle_TypeDef *pI2CHandle, uint8_t *pTxBuffer, uint8_t len, uint8_t slaveAddr, uint8_t repeatedStartState)
{
/* 1. Generate the START condition */
I2C_GenerateSTARTCondition(pI2CHandle->pI2Cx); /* Helper function private to driver */
/* 2. Confirm that START generation is completed by checking the SB flag in
* the SR1
* Note: Until SB (Start Bit) gets cleared, SCL will be stretched
* (pulled to LOW)
* According to the reference manual, SB bit can be cleared by
* reading the SR1 register followed by writing the DR register,
* or by hardware when PE = 0.
*/
while ( !(pI2CHandle->pI2Cx->SR1 & (0x01 << I2C_SR1_SB)) );
/* Reading SR1 register is done here. Writing the DR register will
* be done in Step 3.
*/
/* 3. Send the address of the slave with r/w bit set to w(0) (total 8 bits)
*/
I2C_ExecuteAddressPhaseWrite(pI2CHandle->pI2Cx, slaveAddr);
/* 4. Wait until the address phase is completed by checking the ADDR flag
* of SR1
*/
while ( !(pI2CHandle->pI2Cx->SR1 & (0x01 << I2C_SR1_ADDR)) );
/* 5. Clear the ADDR flag according to its software sequence (ADDR bit is
* cleared by software reading SR1 register followed by reading SR2, or
* by hardware when PE = 0.)
* Note: Until ADDR is cleared SCL will be stretched (pulled to LOW)
*/
I2C_ClearADDRFlag(pI2CHandle);
/* 6. Send the data until @len becomes 0 */
while (len > 0)
{
/* Wait until the TxE is set */
while ( !(pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_TxE)) );
/* Copy @pTxBuffer contents to DR */
pI2CHandle->pI2Cx->DR = *pTxBuffer;
pTxBuffer++;
len--;
}
/* 7. When @len becomes zero wait for TXE=1 and BTF=1 before generating the
* STOP condition.
* Note: TXE=1, BTF=1, means that both SR and DR are empty and next
* transmission should begin when BTF=1 SCL will be stretched
* (pulled to LOW)
*/
while ( !(pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_TxE)) );
while ( !(pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_BTF)) );
/* 8. Generate STOP condition and master does not need to wait for the
* completion of stop condition.
* Note: generating STOP, automatically clears the BTF.
*/
if (repeatedStartState == I2C_REPEATED_START_DI)
{
I2C_GenerateSTOPCondition(pI2CHandle->pI2Cx);
}
} /* End of I2C_MasterTxBlocking */
/**
* I2C_MasterRxBlocking()
* Brief : Handles blocking-based I2C reception
* Param : @pI2CHandle - pointer to I2C peripheral handle
* @pTxBuffer - address of the Tx buffer
* @len - length of the data to transmit
* @slaveAddr - slave address
* @repeatedStartState - I2C_REPEATED_START_EN or DI
* Retval : None
* Note : N/A
*/
void I2C_MasterRxBlocking(I2C_Handle_TypeDef *pI2CHandle, uint8_t *pRxBuffer, uint8_t len, uint8_t slaveAddr, uint8_t repeatedStartState)
{
/* 1. Generate the START condition */
I2C_GenerateSTARTCondition(pI2CHandle->pI2Cx); /* Helper function private to driver */
/* 2. Confirm that START generation is completed by checking the SB flag in
* the SR1
* Note: Until SB (Start Bit) gets cleared, SCL will be stretched
* (pulled to LOW)
* According to the reference manual, SB bit can be cleared by
* reading the SR1 register followed by writing the DR register,
* or by hardware when PE = 0.
*/
while ( !(pI2CHandle->pI2Cx->SR1 & (0x01 << I2C_SR1_SB)) );
/* Reading SR1 register is done here. Writing the DR register will
* be done in Step 3.
*/
/* 3. Send the address of the slave with r/w bit set to R(1) (total 8 bits)
*/
I2C_ExecuteAddressPhaseRead(pI2CHandle->pI2Cx, slaveAddr);
/* 4. Wait until the address phase is completed by checking the ADDR flag
* of SR1
*/
while ( !(pI2CHandle->pI2Cx->SR1 & (0x01 << I2C_SR1_ADDR)) );
/**
* 5. Read data from the slave
*/
/* 5.1. Read the last byte */
if (len == 1)
{
/* Disable ACK */
I2C_ManageACK(pI2CHandle->pI2Cx, I2C_ACK_DISABLE);
/* Clear the ADDR flag */
I2C_ClearADDRFlag(pI2CHandle);
/* Wait until the RxNE flag is set */
while ( !(pI2CHandle->pI2Cx->SR1 & (0x01 << I2C_SR1_RxNE)) );
/* Generate STOP condition */
if (repeatedStartState == I2C_REPEATED_START_DI)
{
I2C_GenerateSTOPCondition(pI2CHandle->pI2Cx);
}
/* Read data into the Rx buffer */
*pRxBuffer = pI2CHandle->pI2Cx->DR;
}
/* 5.2. Read non-last byte */
if (len > 1)
{
/* Clear the ADDR flag */
I2C_ClearADDRFlag(pI2CHandle);
/* Now, the data reception begins! */
/* Read the data until @len becomes zero */
for (uint32_t i = len; i > 0; i--)
{
/* Wait until RxNE flag is set */
while ( !(pI2CHandle->pI2Cx->SR1 & (0x01 << I2C_SR1_RxNE)) );
/* Read the second to last byte */
if (i == 2)
{
/* Disable ACK */
I2C_ManageACK(pI2CHandle->pI2Cx, I2C_ACK_DISABLE);
/* Generate STOP condition */
if (repeatedStartState == I2C_REPEATED_START_DI)
{
I2C_GenerateSTOPCondition(pI2CHandle->pI2Cx);
}
}
/* Read the data from the DR into Rx buffer */
*pRxBuffer = pI2CHandle->pI2Cx->DR;
/* Increment the buffer address */
pRxBuffer++;
}
}
/* Enable ACK (Before entering this function, ACK was enabled.
* Make sure to re-enable ACK to restore the previous condition.) */
if (pI2CHandle->I2C_Config.I2C_ACKEnable == I2C_ACK_ENABLE)
I2C_ManageACK(pI2CHandle->pI2Cx, I2C_ACK_ENABLE);
} /* End of I2C_MasterRxBlocking */
/**
* I2C_MasterTxInterrupt()
* Brief : Handles interrupt-based I2C transmission
* Param : @pI2CHandle - pointer to I2C peripheral handle
* @pTxBuffer - address of the Tx buffer
* @len - length of the data to transmit
* @slaveAddr - slave address
* @repeatedStartState - I2C_REPEATED_START_EN or DI
* Retval : None
* Note : N/A
*/
uint8_t I2C_MasterTxInterrupt(I2C_Handle_TypeDef *pI2CHandle, uint8_t *pTxBuffer, uint8_t len, uint8_t slaveAddr, uint8_t repeatedStartState)
{
uint8_t busyState = pI2CHandle->TxRxState;
if ((busyState != I2C_BUSY_IN_TX) && (busyState != I2C_BUSY_IN_RX))
{
pI2CHandle->pTxBuffer = pTxBuffer;
pI2CHandle->TxLen = len;
pI2CHandle->TxRxState= I2C_BUSY_IN_TX;
pI2CHandle->DevAddr = slaveAddr;
pI2CHandle->RepeatedStart = repeatedStartState;
/* Generate START condition */
I2C_GenerateSTARTCondition(pI2CHandle->pI2Cx);
/* If successful, SB bit will bet set here */
/**
* Enable all available interrupts by setting the control bits
*/
/* Enable ITBUFEN control bit */
pI2CHandle->pI2Cx->CR2 |= (0x1 << I2C_CR2_ITBUFEN);
/* Enable ITEVTEN control bit */
pI2CHandle->pI2Cx->CR2 |= (0x1 << I2C_CR2_ITEVTEN);
/* Enable ITERREN control bit */
pI2CHandle->pI2Cx->CR2 |= (0x1 << I2C_CR2_ITERREN);
}
return busyState;
} /* End of I2C_MasterTxInterrupt */
/**
* I2C_MasterRxInterrupt()
* Brief : Handles interrupt-based I2C reception
* Param : @pI2CHandle - pointer to I2C peripheral handle
* @pTxBuffer - address of the Tx buffer
* @len - length of the data to transmit
* @slaveAddr - slave address
* @repeatedStartState - I2C_REPEATED_START_EN or DI
* Retval : None
* Note : N/A
*/
uint8_t I2C_MasterRxInterrupt(I2C_Handle_TypeDef *pI2CHandle, uint8_t *pRxBuffer, uint8_t len, uint8_t slaveAddr, uint8_t repeatedStartState)
{
uint8_t busyState = pI2CHandle->TxRxState;
if ((busyState != I2C_BUSY_IN_TX) && (busyState != I2C_BUSY_IN_RX))
{
pI2CHandle->pRxBuffer = pRxBuffer;
pI2CHandle->RxLen = len;
pI2CHandle->TxRxState= I2C_BUSY_IN_RX;
pI2CHandle->RxSize = len;
pI2CHandle->DevAddr = slaveAddr;
pI2CHandle->RepeatedStart = repeatedStartState;
/* Generate START condition */
I2C_GenerateSTARTCondition(pI2CHandle->pI2Cx);
/* If successful, SB bit will bet set here */
/**
* Enable all available interrupts by setting the control bits
*/
/* Enable ITBUFEN control bit */
pI2CHandle->pI2Cx->CR2 |= (1 << I2C_CR2_ITBUFEN);
/* Enable ITEVTEN control bit */
pI2CHandle->pI2Cx->CR2 |= (1 << I2C_CR2_ITEVTEN);
/* Enable ITERREN control bit */
pI2CHandle->pI2Cx->CR2 |= (1 << I2C_CR2_ITERREN);
}
return busyState;
} /* End of I2C_MasterRxInterrupt */
/**
* I2C_SlaveTx()
* Brief : Handles transmission of a byte of data upon master's read event
* Param : @pI2Cx - base address of I2Cx peripheral
* @data - a byte of data to send
* Retval : None
* Note : N/A
*/
void I2C_SlaveTx(I2C_TypeDef *pI2Cx, uint8_t data)
{
pI2Cx->DR = data;
} /* End of I2C_SlaveTx */
/**
* I2C_SlaveRx()
* Brief : Handles reception of a byte of data upon master's write event
* Param : @pI2Cx - base address of I2Cx peripheral
* @state - ENABLE or DISABLE macro
* Retval : Received byte of data
* Note : N/A
*/
uint8_t I2C_SlaveRx(I2C_TypeDef *pI2Cx)
{
return (uint8_t)pI2Cx->DR;
} /* End of I2C_SlaveRx */
/**
* I2C_ManageACK()
* Brief : Enables or disables @I2Cx peripheral's ACKing
* Param : @pI2Cx - base address of I2Cx peripheral
* @state - ENABLE or DISABLE macro
* Retval : None
* Note : N/A
*/
void I2C_ManageACK(I2C_TypeDef *pI2Cx, uint8_t state)
{
if (state == I2C_ACK_ENABLE)
{
/* Enable ACK */
pI2Cx->CR1 |= (0x1 << I2C_CR1_ACK);
}
else
{
/* Disable ACK */
pI2Cx->CR1 &= ~(0x1 << I2C_CR1_ACK);
}
} /* End of I2C_ManageACK */
/**
* I2C_IRQInterruptConfig()
* Brief : Enables or disables I2C IRQ interrupts
* Param : @irqNumber - IRQ number
* @state - ENABLE or DISABLE macro
* Retval : None
* Note : N/A
*/
void I2C_IRQInterruptConfig(uint8_t irqNumber, uint8_t state)
{
if (state == ENABLE)
{
/* Configure NVIC_ISERx register */
if (irqNumber <= 31)
*NVIC_ISER0 |= (0x1 << irqNumber);
else if (32 <= irqNumber && irqNumber <= 64)
*NVIC_ISER1 |= (0x1 << irqNumber % 32);
else if (65 <= irqNumber && irqNumber <= 96)
*NVIC_ISER2 |= (0x1 << irqNumber % 32);
}
else
{
/* Configure NVIC_ICERx register */
if (irqNumber <= 31)
*NVIC_ICER0 |= (0x1 << irqNumber);
else if (32 <= irqNumber && irqNumber <= 64)
*NVIC_ICER1 |= (0x1 << irqNumber % 32);
else if (65 <= irqNumber && irqNumber <= 96)
*NVIC_ICER2 |= (0x1 << irqNumber % 32);
}
} /* End of I2C_IRQInterruptConfig */
/**
* I2C_IRQPriorityConfig()
* Brief : Configures I2C IRQ interrupt priorities
* Param : @irqNumber - IRQ number
* @irqPriotity - IRQ priority (Make sure this parameter is of
* type uint32_t. Due to the number of bits it
* needs to be shifted during the calculation,
* declaring it as uint8_t did not do its job.
* Retval : None
* Note : N/A
*/
void I2C_IRQPriorityConfig(uint8_t irqNumber, uint32_t irqPriority)
{
/* Find out the IPR register */
uint8_t iprNumber = irqNumber / 4;
uint8_t iprSection = irqNumber % 4;
uint8_t bitOffset = (iprSection * 8) + (8 - NUM_PRI_BITS_USED);
*(NVIC_IPR_BASE + iprNumber) |= (irqPriority << bitOffset);
} /* End of I2C_IRQPriorityConfig */
/**
* I2C_EV_IRQHandling()
* Brief : Handles I2C event IRQ (common for both master and slave modes)
* Param : @pI2CHandle - pointer to I2C peripheral handle
* Retval : None
* Note : This function will first decode the event that occurred, and
* handle the event accordingly.
*/
void I2C_EV_IRQHandling(I2C_Handle_TypeDef *pI2CHandle)
{
uint32_t temp1, temp2, temp3;
temp1 = pI2CHandle->pI2Cx->CR2 & (0x1 << I2C_CR2_ITEVTEN);
temp2 = pI2CHandle->pI2Cx->CR2 & (0x1 << I2C_CR2_ITBUFEN);
temp3 = pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_SB);
/* 1. Handle the interrupt generated by SB event
* Note: SB event is available only in master mode. Therefore, this block
* will note be executed in slave mode. (For slave, SB bit is always
* zero. Meaning that in I2C, slaves cannot start communication.)
*/
if (temp1 && temp3)
{
/* SB flag is set */
/* Execute the address phase */
if (pI2CHandle->TxRxState == I2C_BUSY_IN_TX)
{
I2C_ExecuteAddressPhaseWrite(pI2CHandle->pI2Cx, pI2CHandle->DevAddr);
}
else if (pI2CHandle->TxRxState == I2C_BUSY_IN_RX)
{
I2C_ExecuteAddressPhaseRead(pI2CHandle->pI2Cx, pI2CHandle->DevAddr);
}
}
temp3 = pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_ADDR);
/* 2. Handle the interrupt generated by ADDR event
* Note: When in master mode - Address is sent
* When in slave mode - Address matched with own address
*/
if (temp1 && temp3)
{
/* ADDR flag is set
*
* When ADDR flag is set, clock will be stretched and both the master
* and slave will be in wait state. So it is important to clear it
* according to the logic.
*/
I2C_ClearADDRFlag(pI2CHandle);
}
temp3 = pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_BTF);
/* 3. Handle the interrupt generated by BTF event */
if (temp1 && temp3)
{
/* BTF (Byte Transfer Finished) flag is set
*
* When BTF flag is set:
*
* 1. If TxE=1 during transmission
* - Both SR and DR are empty
* - Clock will be stretched
* 2. If RxNe=1 during reception
* - Both SR and DR are full
* - Clock will be stretched
*/
if (pI2CHandle->TxRxState == I2C_BUSY_IN_TX)
{
/* If TxE=1 */
if (pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_TxE))
{
/**
* BTF=1 & TxE=1; Close I2C data transmission
*/
/* Check if the Tx length is 0 */
if (pI2CHandle->TxLen == 0)
{
/* 1. Generate STOP condition */
if (pI2CHandle->RepeatedStart == I2C_REPEATED_START_DI)
{
I2C_GenerateSTOPCondition(pI2CHandle->pI2Cx);
}
/* 2. Reset all the members of the handle structure */
I2C_CloseTx(pI2CHandle);
/* 3. Notify the application about transmission complete */
I2C_ApplicationEventCallback(pI2CHandle, I2C_EV_TX_CMPLT);
}
}
}
else if (pI2CHandle->TxRxState == I2C_BUSY_IN_RX)
{
/* Do nothing */
}
}
temp3 = pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_STOPF);
/* 4. Handle the interrupt generated by STOPF event
* Note: STOP event detection (STOPF) is available only in slave mode.
* (In master mode, STOPF flag will not be set.)
* STOPF flag is set by hardware when a STOP condition is detected
* on the bus by the slave after an Acknowledgement (ACK=1).
* STOPF flag is cleared by software reading the SR1 register
* followed by a write in the CR1 register,
* or by hardware when PE=0.
*
*/
if (temp1 && temp3)
{
/* STOPF flag is set */
/* Clear STOPF (i.e., Read SR1, write to CR1)
* Note: Reading SR1 is already done in the previous line of code
*/
/* Write to CR1 */
pI2CHandle->pI2Cx->CR1 |= 0x0000; /* Write without affecting CR1 */
/* Notify the application of the STOP detection */
I2C_ApplicationEventCallback(pI2CHandle, I2C_EV_STOP);
}
temp3 = pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_TxE);
/* 5. Handle the interrupt generated by TxE event */
if (temp1 && temp2 && temp3)
{
/* TxE flag is set, perform the data transmission
* Note: This is an indication that DR is empty, and software can
* put data into DR in order to send the data byte to the
* external world.
*/
/* Check for device mode */
if (pI2CHandle->pI2Cx->SR2 & (0x1 << I2C_SR2_MSL))
{
/* Device is master */
if (pI2CHandle->TxRxState == I2C_BUSY_IN_TX)
{
if (pI2CHandle->TxLen > 0)
{
/* 1. Load the data into DR */
pI2CHandle->pI2Cx->DR = *(pI2CHandle->pTxBuffer);
/* 2. Decrement TxLen */
pI2CHandle->TxLen--;
/* 3. Increment the Tx buffer address */
pI2CHandle->pTxBuffer++;
}
}
}
else
{
/* Device is slave */
if (pI2CHandle->pI2Cx->SR2 & (0x1 << I2C_SR2_TRA))
{
/* Slave in transmitter mode */
I2C_ApplicationEventCallback(pI2CHandle, I2C_EV_TX);
}
}
}
temp3 = pI2CHandle->pI2Cx->SR1 & (0x1 << I2C_SR1_RxNE);
/* 6. Handle the interrupt generated by RxNE event */
if (temp1 && temp2 && temp3)
{
/* RxNE flag is set */
/* Check for device mode */
if (pI2CHandle->pI2Cx->SR2 & (0x1 << I2C_SR2_MSL))
{
/* Device is master */
if (pI2CHandle->TxRxState == I2C_BUSY_IN_RX)
{
/* Perform the data reception */
if (pI2CHandle->RxSize == 1)
{
*pI2CHandle->pRxBuffer = pI2CHandle->pI2Cx->DR;
pI2CHandle->RxLen--;
}
if (pI2CHandle->RxSize > 1)
{
if (pI2CHandle->RxLen == 2)
{
/* Clear the ACK bit */
I2C_ManageACK(pI2CHandle->pI2Cx, DISABLE);
}
/* Read DR */
*pI2CHandle->pRxBuffer = pI2CHandle->pI2Cx->DR;
pI2CHandle->pRxBuffer++;
pI2CHandle->RxLen--;
}
if (pI2CHandle->RxLen == 0)
{
/**
* No more data to receive. Close the I2C data reception
* and notify the application of the communication termination.
*/
/* 1. Generate STOP condition */
if (pI2CHandle->RepeatedStart == I2C_REPEATED_START_DI)
{
I2C_GenerateSTOPCondition(pI2CHandle->pI2Cx);
}
/* 2. Close I2C Rx */
I2C_CloseRx(pI2CHandle);
/* 3. Notify the application */
I2C_ApplicationEventCallback(pI2CHandle, I2C_EV_RX_CMPLT);
}
}
}
else
{
/* Device is slave */
if (!(pI2CHandle->pI2Cx->SR2 & (0x1 << I2C_SR2_TRA)))
{
/* Slave in transmitter mode */
I2C_ApplicationEventCallback(pI2CHandle, I2C_EV_RX);
}
}
}
} /* End of I2C_EV_IRQHandling */
/**
* I2C_ER_IRQHandling()
* Brief : Handles I2C error IRQ (common for both master and slave modes)
* Param : @pI2CHandle - pointer to I2C peripheral handle
* Retval : None
* Note : N/A
*/
void I2C_ER_IRQHandling(I2C_Handle_TypeDef *pI2CHandle)
{
uint32_t temp1, temp2;
/* Check the status of SR2 ITERREN control bit */
temp2 = (pI2CHandle->pI2Cx->CR2) & (0x1 << I2C_CR2_ITERREN);
/* Check for bus error (BERR) *********************************************/
temp1 = (pI2CHandle->pI2Cx->SR1) & (0x1 << I2C_SR1_BERR);
if (temp1 && temp2)
{
/**
* Handle bus error
*/
/* Clear the bus error flag */
pI2CHandle->pI2Cx->SR1 &= ~(0x1 << I2C_SR1_BERR);
/* Notify the application of this error */
I2C_ApplicationEventCallback(pI2CHandle, I2C_ERROR_BERR);
}
/* Check for arbitration lost (ARLO) **************************************/
temp1 = (pI2CHandle->pI2Cx->SR1) & (0x1 << I2C_SR1_ARLO);
if (temp1 && temp2)
{
/**
* Handle arbitration lost error
*/
/* Clear the arbitration lost error flag */
pI2CHandle->pI2Cx->SR1 &= ~(0x1 << I2C_SR1_ARLO);
/* Notify the application of this error */
I2C_ApplicationEventCallback(pI2CHandle, I2C_ERROR_ARLO);
}
/* Check for ACK failure (AF) *********************************************/
temp1 = (pI2CHandle->pI2Cx->SR1) & (0x1 << I2C_SR1_AF);
if (temp1 && temp2)
{
/**
* Handle ACK failure error
*/
/* Clear the ACK failure error flag */
pI2CHandle->pI2Cx->SR1 &= ~(0x1 << I2C_SR1_AF);
/* Notify the application of this error */
I2C_ApplicationEventCallback(pI2CHandle, I2C_ERROR_AF);
}
/* Check for overrun/underrun (OVR) ***************************************/
temp1 = (pI2CHandle->pI2Cx->SR1) & (0x1 << I2C_SR1_OVR);
if (temp1 && temp2)
{
/**
* Handle overrun/underrun error
*/
/* Clear the overrun/underrun error flag */
pI2CHandle->pI2Cx->SR1 &= ~(0x1 << I2C_SR1_OVR);
/* Notify the application of this error */
I2C_ApplicationEventCallback(pI2CHandle, I2C_ERROR_OVR);
}
/* Check for timeout error (TIMEOUT) **************************************/
temp1 = (pI2CHandle->pI2Cx->SR1) & (0x1 << I2C_SR1_TIMEOUT);
if (temp1 && temp2)
{
/**
* Handle timeout error
*/
/* Clear the timeout error flag */
pI2CHandle->pI2Cx->SR1 &= ~(0x1 << I2C_SR1_TIMEOUT);
/* Notify the application of this error */
I2C_ApplicationEventCallback(pI2CHandle, I2C_ERROR_TIMEOUT);
}
} /* End of I2C_ER_IRQHandling */
/**
* I2C_CloseTx()
* Brief : Handles closing I2C Tx operation
* Param : @pI2CHandle - pointer to I2C peripheral handle
* Retval : None
* Note : N/A
*/
void I2C_CloseTx(I2C_Handle_TypeDef *pI2CHandle)
{
/* Disable ITBUFEN control bit */
pI2CHandle->pI2Cx->CR2 &= ~(0x1 << I2C_CR2_ITBUFEN);
/* Disable ITEVTEN control bit */
pI2CHandle->pI2Cx->CR2 &= ~(0x1 << I2C_CR2_ITEVTEN);
/* Reset I2C handle members */
pI2CHandle->TxRxState = I2C_READY;
pI2CHandle->pTxBuffer = NULL;
pI2CHandle->TxLen = 0;
} /* End of I2C_CloseTx */
/**
* I2C_CloseRx()
* Brief : Handles closing I2C Rx operation
* Param : @pI2CHandle - pointer to I2C peripheral handle
* Retval : None
* Note : N/A
*/
void I2C_CloseRx(I2C_Handle_TypeDef *pI2CHandle)
{
/* Disable ITBUFEN control bit */
pI2CHandle->pI2Cx->CR2 &= ~(0x1 << I2C_CR2_ITBUFEN);
/* Disable ITEVTEN control bit */
pI2CHandle->pI2Cx->CR2 &= ~(0x1 << I2C_CR2_ITEVTEN);
/* Reset I2C handle members */
pI2CHandle->TxRxState = I2C_READY;
pI2CHandle->pRxBuffer = NULL;
pI2CHandle->RxLen = 0;
pI2CHandle->RxSize = 0;
/* Enable ACK */
if (pI2CHandle->I2C_Config.I2C_ACKEnable == I2C_ACK_ENABLE)
{
I2C_ManageACK(pI2CHandle->pI2Cx, ENABLE);
}
} /* End of I2C_CloseRx */
/**
* I2C_GenerateSTARTCondition()
* Brief : Generates I2C START condition
* Param : @pI2Cx - base address of I2Cx peripheral
* Retval : None
* Note : N/A
*/
void I2C_GenerateSTARTCondition(I2C_TypeDef *pI2Cx)
{
pI2Cx->CR1 |= (0x1 << I2C_CR1_START);
} /* End of I2C_GenerateSTARTCondition */
/**
* I2C_GenerateSTOPCondition()
* Brief : Generates I2C START condition
* Param : @pI2Cx - base address of I2Cx peripheral
* Retval : None
* Note : N/A
*/
void I2C_GenerateSTOPCondition(I2C_TypeDef *pI2Cx)
{
pI2Cx->CR1 |= (0x1 << I2C_CR1_STOP);
} /* End of I2C_GenerateSTOPCondition */
/**
* I2C_SlaveEnableDisableCallbackEvents()
* Brief : Enables or disables @pI2Cx's (as a slave) callback events
* Param : @pI2Cx - base address of I2Cx peripheral
* @state - ENABLE or DISABLE macro
* Retval : None
* Note : N/A
*/
void I2C_SlaveEnableDisableCallbackEvents(I2C_TypeDef *pI2Cx, uint8_t state)
{
if (state == ENABLE)
{
/**
* Enable all available interrupts by setting the control bits
*/
/* Enable ITBUFEN control bit */
pI2Cx->CR2 |= (0x1 << I2C_CR2_ITBUFEN);
/* Enable ITEVTEN control bit */
pI2Cx->CR2 |= (0x1 << I2C_CR2_ITEVTEN);
/* Enable ITERREN control bit */
pI2Cx->CR2 |= (0x1 << I2C_CR2_ITERREN);
}
else
{
/**
* Disable all available interrupts by setting the control bits
*/
/* Disable ITBUFEN control bit */
pI2Cx->CR2 &= ~(0x1 << I2C_CR2_ITBUFEN);
/* Disable ITEVTEN control bit */
pI2Cx->CR2 &= ~(0x1 << I2C_CR2_ITEVTEN);
/* Disable ITERREN control bit */
pI2Cx->CR2 &= ~(0x1 << I2C_CR2_ITERREN);
}
} /* End of I2C_SlaveEnableDisableCallbackEvents */
/**
* I2C_ApplicationEventCallback()
* Brief : Notifies the application of the event occurred
* Param : @pI2CHandle - pointer to I2C handle structure
* @appEvent - I2C event occurred
* Retval : None
* Note : This function must be implemented by the application. Since the driver
* does not know in which application this function will be implemented,
* the driver defines it as a weak function. The application may override
* this function.
* If the application does not implement this function, the following
* definition will be executed.