1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
32
35
48
49
50
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
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
172
173
174
175
179
180
181
182
183
184
185
190
191
192
193
194
195
196
197
198
199
200
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
232
233
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
287
288
289
290
294
295
296
297
298
299
304
305
306
310
311
312
313
314
315
316
317
318
319
320
321
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
353
354
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
398
399
400
401
402
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
427
432
433
438
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
464
465
466
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
497
498
508
509
516
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
568
569
570
571
572
573
574
575
578
579
580
581
582
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
604
609
610
616
617
618
619
620
621
626
627
628
629
630
631
637
638
639
640
641
643
644
645
646
647
648
653
654
655
656
657
658
659
660
661
663
664
665
666
667
668
669
670
671
672
673
679
688
689
696
697
698
699
700
701
702
703
705
706
707
708
709
710
711
712
713
714
715
716
718
719
720
721
723
724
725
726
727
728
729
730
732
733
734
735
736
737
738
739
740
741
745
746
747
748
750
758
763
764
765
766
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
805
806
809
810
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
LL_I2C_InitTypeDef i2c_initstruct;
Private typedef
/* ... */
#define I2C_SPEEDCLOCK 400000
#define I2C_DUTYCYCLE LL_I2C_DUTYCYCLE_2
/* ... */
#define SLAVE_BYTE_TO_SEND (uint8_t)0xA5
Private define
__IO uint8_t ubNbDataToTransmit = sizeof(SLAVE_BYTE_TO_SEND);
__IO uint8_t ubNbDataToReceive = sizeof(SLAVE_BYTE_TO_SEND);
__IO uint8_t ubButtonPress = 0;
/* ... */
uint8_t aReceiveBuffer[0xF] = {0};
__IO uint8_t ubReceiveIndex = 0;
Private variables
void SystemClock_Config(void);
void Configure_I2C_Slave(void);
void Configure_I2C_Master(void);
void Activate_I2C_Slave(void);
void Activate_I2C_Master(void);
void LED_Init(void);
void LED_On(void);
void LED_Off(void);
void LED_Blinking(uint32_t Period);
void WaitForUserButtonPress(void);
void Handle_I2C_Slave(void);
void Handle_I2C_Master(void);
void UserButton_Init(void);
Private function prototypes
/* ... */
int main(void)
{
SystemClock_Config();
LED_Init();
LED_Off();
UserButton_Init();
Configure_I2C_Slave();
Configure_I2C_Master();
Activate_I2C_Slave();
Activate_I2C_Master();
WaitForUserButtonPress();
Handle_I2C_Slave();
Handle_I2C_Master();
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
void Configure_I2C_Slave(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_6, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_6, LL_GPIO_AF_4);
LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_6, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_6, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_6, LL_GPIO_PULL_UP);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_7, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_7, LL_GPIO_AF_4);
LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_7, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_7, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_7, LL_GPIO_PULL_UP);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
/* ... */
NVIC_SetPriority(I2C1_EV_IRQn, 0);
NVIC_EnableIRQ(I2C1_EV_IRQn);
/* ... */
NVIC_SetPriority(I2C1_ER_IRQn, 0);
NVIC_EnableIRQ(I2C1_ER_IRQn);
if (LL_I2C_DeInit(I2C1) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_I2C_DeInit(I2C1) != SUCCESS) { ... }
/* ... */
/* ... */
i2c_initstruct.PeripheralMode = LL_I2C_MODE_I2C;
i2c_initstruct.ClockSpeed = I2C_SPEEDCLOCK;
i2c_initstruct.DutyCycle = I2C_DUTYCYCLE;
i2c_initstruct.OwnAddress1 = SLAVE_OWN_ADDRESS;
i2c_initstruct.TypeAcknowledge = LL_I2C_ACK;
i2c_initstruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
if (LL_I2C_Init(I2C1, &i2c_initstruct) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_I2C_Init(I2C1, &i2c_initstruct) != SUCCESS) { ... }
/* ... */
LL_I2C_Disable(I2C1);
}{ ... }
/* ... */
void Configure_I2C_Master(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_10, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(GPIOB, LL_GPIO_PIN_10, LL_GPIO_AF_4);
LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_10, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_10, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_10, LL_GPIO_PULL_UP);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_3, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_3, LL_GPIO_AF_9);
LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_3, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_3, LL_GPIO_OUTPUT_OPENDRAIN);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_3, LL_GPIO_PULL_UP);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
/* ... */
NVIC_SetPriority(I2C2_EV_IRQn, 0);
NVIC_EnableIRQ(I2C2_EV_IRQn);
/* ... */
NVIC_SetPriority(I2C2_ER_IRQn, 0);
NVIC_EnableIRQ(I2C2_ER_IRQn);
if (LL_I2C_DeInit(I2C2) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_I2C_DeInit(I2C2) != SUCCESS) { ... }
/* ... */
/* ... */
/* ... */
i2c_initstruct.PeripheralMode = LL_I2C_MODE_I2C;
i2c_initstruct.ClockSpeed = I2C_SPEEDCLOCK;
i2c_initstruct.DutyCycle = I2C_DUTYCYCLE;
i2c_initstruct.OwnAddress1 = 0x00;
i2c_initstruct.TypeAcknowledge = LL_I2C_ACK;
i2c_initstruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
if (LL_I2C_Init(I2C2, &i2c_initstruct) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_I2C_Init(I2C2, &i2c_initstruct) != SUCCESS) { ... }
/* ... */
LL_I2C_Disable(I2C2);
}{ ... }
/* ... */
void Activate_I2C_Slave(void)
{
LL_I2C_Enable(I2C1);
/* ... */
LL_I2C_EnableIT_EVT(I2C1);
LL_I2C_EnableIT_ERR(I2C1);
}{ ... }
/* ... */
void Activate_I2C_Master(void)
{
LL_I2C_Enable(I2C2);
/* ... */
LL_I2C_EnableIT_EVT(I2C2);
LL_I2C_EnableIT_ERR(I2C2);
}{ ... }
/* ... */
void LED_Init(void)
{
LED2_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_MODE_OUTPUT);
}{ ... }
/* ... */
void LED_On(void)
{
LL_GPIO_SetOutputPin(LED2_GPIO_PORT, LED2_PIN);
}{ ... }
/* ... */
void LED_Off(void)
{
LL_GPIO_ResetOutputPin(LED2_GPIO_PORT, LED2_PIN);
}{ ... }
/* ... */
void LED_Blinking(uint32_t Period)
{
LL_GPIO_SetOutputPin(LED2_GPIO_PORT, LED2_PIN);
while (1)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(Period);
}while (1) { ... }
}{ ... }
/* ... */
void UserButton_Init(void)
{
USER_BUTTON_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_PULL_NO);
USER_BUTTON_SYSCFG_SET_EXTI();
USER_BUTTON_EXTI_LINE_ENABLE();
USER_BUTTON_EXTI_FALLING_TRIG_ENABLE();
NVIC_EnableIRQ(USER_BUTTON_EXTI_IRQn);
NVIC_SetPriority(USER_BUTTON_EXTI_IRQn,0x03);
}{ ... }
/* ... */
void WaitForUserButtonPress(void)
{
while (ubButtonPress == 0)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(LED_BLINK_FAST);
}while (ubButtonPress == 0) { ... }
LL_GPIO_ResetOutputPin(LED2_GPIO_PORT, LED2_PIN);
}{ ... }
/* ... */
void Handle_I2C_Slave(void)
{
LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK);
}{ ... }
/* ... */
void Handle_I2C_Master(void)
{
LL_I2C_AcknowledgeNextData(I2C2, LL_I2C_ACK);
LL_I2C_GenerateStartCondition(I2C2);
}{ ... }
/* ... */
void SystemClock_Config(void)
{
LL_RCC_HSE_EnableBypass();
LL_RCC_HSE_Enable();
while(LL_RCC_HSE_IsReady() != 1)
{
}while (LL_RCC_HSE_IsReady() != 1) { ... };
LL_FLASH_SetLatency(LL_FLASH_LATENCY_3);
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_8, 400, LL_RCC_PLLP_DIV_4);
LL_RCC_PLL_Enable();
while(LL_RCC_PLL_IsReady() != 1)
{
}while (LL_RCC_PLL_IsReady() != 1) { ... };
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
{
}while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) { ... };
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
SysTick_Config(100000000 / 1000);
SystemCoreClock = 100000000;
}{ ... }
/* ... */
void UserButton_Callback(void)
{
ubButtonPress = 1;
}{ ... }
/* ... */
void Slave_Ready_To_Transmit_Callback(void)
{
ubNbDataToTransmit--;
if(ubNbDataToTransmit == 0)
{
LL_I2C_DisableIT_BUF(I2C1);
}if (ubNbDataToTransmit == 0) { ... }
LL_I2C_TransmitData8(I2C1, SLAVE_BYTE_TO_SEND);
}{ ... }
/* ... */
void Master_Reception_Callback(void)
{
if(ubNbDataToReceive > 3)
{
/* ... */
aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C2);
ubNbDataToReceive--;
}if (ubNbDataToReceive > 3) { ... }
else if((ubNbDataToReceive == 2) || (ubNbDataToReceive == 3))
{
LL_I2C_DisableIT_BUF(I2C2);
}else if ((ubNbDataToReceive == 2) || (ubNbDataToReceive == 3)) { ... }
else if(ubNbDataToReceive == 1)
{
LL_I2C_DisableIT_BUF(I2C2);
LL_I2C_GenerateStopCondition(I2C2);
/* ... */
aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C2);
ubNbDataToReceive--;
Master_Complete_Callback();
}else if (ubNbDataToReceive == 1) { ... }
}{ ... }
/* ... */
void Slave_Complete_Callback(void)
{
/* ... */
LL_I2C_DisableIT_EVT(I2C1);
LL_I2C_DisableIT_ERR(I2C1);
}{ ... }
/* ... */
void Master_Complete_Callback(void)
{
if(ubNbDataToReceive == 3)
{
LL_I2C_AcknowledgeNextData(I2C2, LL_I2C_NACK);
/* ... */
aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C2);
ubNbDataToReceive--;
LL_I2C_DisableIT_BUF(I2C2);
}if (ubNbDataToReceive == 3) { ... }
else if(ubNbDataToReceive == 2)
{
LL_I2C_GenerateStopCondition(I2C2);
/* ... */
aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C2);
ubNbDataToReceive--;
/* ... */
aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C2);
ubNbDataToReceive--;
}else if (ubNbDataToReceive == 2) { ... }
else
{
if(ubNbDataToReceive > 0)
{
/* ... */
aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C2);
ubNbDataToReceive--;
}if (ubNbDataToReceive > 0) { ... }
}else { ... }
if(ubNbDataToReceive == 0)
{
/* ... */
LL_I2C_DisableIT_EVT(I2C2);
LL_I2C_DisableIT_ERR(I2C2);
/* ... */
if(aReceiveBuffer[ubReceiveIndex-1] == SLAVE_BYTE_TO_SEND)
{
/* ... */
LED_On();
}if (aReceiveBuffer[ubReceiveIndex-1] == SLAVE_BYTE_TO_SEND) { ... }
else
{
Error_Callback();
}else { ... }
}if (ubNbDataToReceive == 0) { ... }
}{ ... }
/* ... */
void Error_Callback(void)
{
NVIC_DisableIRQ(I2C1_EV_IRQn);
NVIC_DisableIRQ(I2C2_EV_IRQn);
NVIC_DisableIRQ(I2C1_ER_IRQn);
NVIC_DisableIRQ(I2C2_ER_IRQn);
LED_Blinking(LED_BLINK_ERROR);
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t *file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t *file, uint32_t line) { ... }
/* ... */#endif
/* ... */
/* ... */