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
56
60
61
62
67
68
69
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
108
109
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
131
138
139
140
141
142
143
160
161
167
168
174
175
176
177
178
183
184
185
186
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
219
224
225
226
231
235
236
241
245
246
247
248
249
254
263
264
265
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
390
391
392
393
397
398
399
400
401
402
403
404
405
406
410
411
412
413
414
415
416
417
418
419
420
421
431
432
433
436
437
/* ... */
#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 DestinationAddress = 0;
uint8_t InitiatorAddress = 0;
__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)
DestinationAddress = 0x3;
InitiatorAddress = 0x1;/* ... */
#elif defined (DEVICE_2)
DestinationAddress = 0x1;
InitiatorAddress = 0x3;/* ... */
#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,InitiatorAddress ,DestinationAddress, (uint8_t *)&Tab_Tx, TxSize);
while (TxStatus != 1);
StartSending = 0;
}while ((StartSending == 1) && (ReceivedFrame == 0)) { ... }
if (ReceivedFrame == 1)
{
if (Tab_Rx[1] == 0x44)
{
if (Tab_Rx[2] == 0x41)
{
BSP_LED_On(LED1);
BSP_LED_On(LED2);
BSP_LED_On(LED3);
BSP_LED_On(LED4);
...}
else if (Tab_Rx[2] == 0x42)
{
BSP_LED_Off(LED1);
BSP_LED_Off(LED2);
BSP_LED_Off(LED3);
BSP_LED_Off(LED4);
...}
...}
else if (Tab_Rx[1] == 0x46)
{
BSP_LED_On(LED1);
BSP_LED_On(LED2);
BSP_LED_Off(LED3);
BSP_LED_Off(LED4);
...}
else if (Tab_Rx[1] == 0x9F)
{
BSP_LED_Off(LED1);
BSP_LED_Off(LED2);
BSP_LED_On(LED3);
BSP_LED_On(LED4);
...}
ReceivedFrame = 0;
CEC_FlushRxBuffer();
}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;
#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);
Tab_Tx[0] = 0x44;
Tab_Tx[1] = 0x42;
TxSize = 0x02;
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_DOWN)
{
BSP_LED_Toggle(LED4);
Tab_Tx[0] = 0x9F;
TxSize = 0x01;
StartSending = 1;
}if (JoyState == JOY_DOWN) { ... }
if(JoyState == JOY_SEL)
{
BSP_LED_Toggle(LED2);
Tab_Tx[0] = 0x46;
TxSize = 0x01;
StartSending = 1;
}if (JoyState == JOY_SEL) { ... }
if(JoyState == JOY_UP)
{
BSP_LED_Toggle(LED3);
Tab_Tx[0] = 0x44;
Tab_Tx[1] = 0x41;
TxSize = 0x02;
StartSending = 1;
}if (JoyState == JOY_UP) { ... }
...}
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
/* ... */
/* ... */