1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
31
32
33
34
35
37
43
44
47
48
51
52
53
54
55
57
58
59
60
64
65
66
71
72
73
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
117
118
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
143
144
145
146
147
148
149
150
151
152
153
154
155
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
204
205
206
207
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
241
245
246
247
252
256
257
262
266
267
272
281
282
283
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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
404
405
406
407
411
412
413
414
415
416
417
418
419
420
424
425
426
427
428
429
430
431
432
433
434
435
445
446
447
450
451
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
uint8_t Tab_Rx[CEC_MAX_PAYLOAD];
/* ... */
uint8_t Tab_Tx[CEC_MAX_PAYLOAD-1];
/* ... */
uint8_t ReceivedFrame = 0x0;
uint16_t NbOfReceivedBytes = 0x0;
/* ... */
uint8_t StartSending = 0x0;
uint32_t TxSize = 0x0;
/* ... */
uint8_t MyLogicalAddress1 = 0x0;
uint8_t MyLogicalAddress2 = 0x0;
uint8_t MyFollowerAddress1 = 0x0;
uint8_t MyFollowerAddress2 = 0x0;
uint8_t DestinationAddress = 0x0;
/* ... */
__IO uint8_t TxStatus = 0;
CEC_HandleTypeDef hcec;Private variables
static void SystemClock_Config(void);
static void CEC_Config(CEC_HandleTypeDef *hcec);
static void CEC_FlushRxBuffer(void);Private function prototypes
/* ... */
int main(void)
{
/* ... */
/* ... */
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
BSP_PB_Init(BUTTON_TAMPER,BUTTON_MODE_EXTI);
BSP_JOY_Init(JOY_MODE_EXTI);
#if defined (DEVICE_1)
/* ... */
MyLogicalAddress1 = DEVICE_ADDRESS_1;
MyFollowerAddress1 = DEVICE_ADDRESS_2;
MyFollowerAddress2 = DEVICE_ADDRESS_3;/* ... */
#elif defined (DEVICE_2)
/* ... */
MyLogicalAddress1 = DEVICE_ADDRESS_2;
MyLogicalAddress2 = DEVICE_ADDRESS_3;
MyFollowerAddress1 = DEVICE_ADDRESS_1;/* ... */
#endif
hcec.Instance = CEC;
HAL_CEC_DeInit(&hcec);
CEC_Config(&hcec);
ReceivedFrame = 0;
StartSending = 0;
NbOfReceivedBytes = 0;
CEC_FlushRxBuffer();
while (1)
{
/* ... */
while( (StartSending == 1) && (ReceivedFrame == 0))
{
HAL_CEC_Transmit_IT(&hcec, MyLogicalAddress1 ,DestinationAddress, (uint8_t *)&Tab_Tx, TxSize);
while (TxStatus != 1);
StartSending = 0;
TxStatus = 0;
}while ((StartSending == 1) && (ReceivedFrame == 0)) { ... }
if (ReceivedFrame == 1)
{
/* ... */
if ((Tab_Rx[0]&0x0F) == 0x01)
{
BSP_LED_On(LED1);
BSP_LED_On(LED4);
BSP_LED_Off(LED3);
}if ((Tab_Rx[0]&0x0F) == 0x01) { ... }
else if ((Tab_Rx[0]&0x0F) == 0x03)
{
BSP_LED_On(LED1);
BSP_LED_Off(LED4);
BSP_LED_Off(LED3);
}else if ((Tab_Rx[0]&0x0F) == 0x03) { ... }
else if ((Tab_Rx[0]&0x0F) == 0x05)
{
BSP_LED_On(LED4);
BSP_LED_Off(LED1);
BSP_LED_Off(LED3);
}else if ((Tab_Rx[0]&0x0F) == 0x05) { ... }
else if ((Tab_Rx[0]&0x0F) == 0x0F)
{
BSP_LED_Off(LED1);
BSP_LED_Off(LED2);
BSP_LED_Off(LED3);
BSP_LED_Off(LED4);
}else if ((Tab_Rx[0]&0x0F) == 0x0F) { ... }
ReceivedFrame = 0;
}if (ReceivedFrame == 1) { ... }
else if (ReceivedFrame == 2)
{
BSP_LED_On(LED3);
ReceivedFrame = 0;
...}
}while (1) { ... }
}{ ... }
/* ... */
static void CEC_Config(CEC_HandleTypeDef *hcec)
{
#if defined (DEVICE_1)
hcec->Init.OwnAddress = CEC_OWN_ADDRESS_1;
#elif defined (DEVICE_2)
hcec->Init.OwnAddress = CEC_OWN_ADDRESS_3|CEC_OWN_ADDRESS_5;
#endif
hcec->Init.SignalFreeTime = CEC_DEFAULT_SFT;
hcec->Init.Tolerance = CEC_STANDARD_TOLERANCE;
hcec->Init.BRERxStop = CEC_NO_RX_STOP_ON_BRE;
hcec->Init.BREErrorBitGen = CEC_BRE_ERRORBIT_NO_GENERATION;
hcec->Init.LBPEErrorBitGen = CEC_LBPE_ERRORBIT_NO_GENERATION;
hcec->Init.BroadcastMsgNoErrorBitGen = CEC_BROADCASTERROR_NO_ERRORBIT_GENERATION;
hcec->Init.SignalFreeTimeOption = CEC_SFT_START_ON_TXSOM;
hcec->Init.ListenMode = CEC_REDUCED_LISTENING_MODE;
hcec->Init.RxBuffer = Tab_Rx;
HAL_CEC_Init(hcec);
}{ ... }
/* ... */
void HAL_CEC_TxCpltCallback(CEC_HandleTypeDef *hcec)
{
TxStatus = 1;
}{ ... }
/* ... */
void HAL_CEC_RxCpltCallback(CEC_HandleTypeDef *hcec, uint32_t RxFrameSize)
{
ReceivedFrame = 1;
}{ ... }
/* ... */
void HAL_CEC_ErrorCallback(CEC_HandleTypeDef *hcec)
{
ReceivedFrame = 2;
}{ ... }
/* ... */
static void CEC_FlushRxBuffer(void)
{
uint32_t cpt;
for (cpt = CEC_MAX_PAYLOAD; cpt > 0; cpt--)
{
Tab_Rx[cpt-1] = 0;
}for (cpt = CEC_MAX_PAYLOAD; cpt > 0; cpt--) { ... }
}{ ... }
/* ... */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
uint32_t ITstatus = 0;
JOYState_TypeDef JoyState = JOY_NONE;
if(GPIO_Pin == TAMPER_BUTTON_PIN)
{
BSP_LED_Toggle(LED1);
DestinationAddress = MyFollowerAddress1;
TxSize = 0x0;
StartSending = 1;
}if (GPIO_Pin == TAMPER_BUTTON_PIN) { ... }
if(GPIO_Pin == MFX_IRQOUT_PIN)
{
/* ... */
ITstatus = BSP_IO_ITGetStatus(JOY_ALL_PINS);
if (ITstatus)
{
JoyState = BSP_JOY_GetState();
if(JoyState == JOY_UP)
{
BSP_LED_Toggle(LED3);
}if (JoyState == JOY_UP) { ... }
if(JoyState == JOY_DOWN)
{
BSP_LED_Toggle(LED4);
#if defined (DEVICE_1)
DestinationAddress = MyFollowerAddress2;
#elif defined (DEVICE_2)
DestinationAddress = MyFollowerAddress1;
#endif
TxSize = 0x0;
StartSending = 1;
}if (JoyState == JOY_DOWN) { ... }
if(JoyState == JOY_SEL)
{
BSP_LED_Toggle(LED2);
DestinationAddress = 0xF;
TxSize = 0x0;
StartSending = 1;
}if (JoyState == JOY_SEL) { ... }
...}
BSP_IO_ITClear();
...}
}{ ... }
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
HAL_StatusTypeDef ret = HAL_OK;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
RCC_OscInitStruct.PLL.PLLR = 2;
ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
ret = HAL_PWREx_EnableOverDrive();
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
}{ ... }
#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
/* ... */
/* ... */