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
28
29
32
33
36
37
40
41
45
48
49
52
55
56
57
58
59
63
64
65
66
67
68
69
72
73
76
77
81
82
83
84
85
86
87
88
89
94
95
100
101
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
127
128
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
177
178
181
182
186
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
254
255
256
257
258
259
260
261
262
263
264
268
269
270
271
272
273
274
275
276
277
278
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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
412
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
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
511
512
514
515
516
517
520
521
522
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
614
615
616
617
618
623
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
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
700
701
702
703
704
705
706
707
708
709
710
711
715
716
721
722
723
724
725
726
727
728
729
730
734
735
736
741
746
747
748
749
750
751
752
753
754
755
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
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
851
857
858
862
868
869
873
877
878
881
882
883
884
887
891
892
896
901
902
907
916
917
921
926
927
931
935
936
940
944
945
946
947
950
954
955
958
962
963
969
970
971
972
973
974
975
976
977
978
979
980
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1005
1009
1010
1011
1012
1015
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1045
1046
1047
1048
1049
1050
1051
1052
1053
1057
1058
1059
1062
1066
1067
1073
1077
1078
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1119
1120
1124
1128
1129
1132
1133
1136
1137
1140
1141
/* ... */
#include "stm32412g_discovery.h"
/* ... */
/* ... */
/* ... */
/* ... */
typedef struct
{
__IO uint16_t REG;
__IO uint16_t RAM;
...}LCD_CONTROLLER_TypeDef;
/* ... */
/* ... */
/* ... */
#define __STM32412G_DISCOVERY_BSP_VERSION_MAIN (0x02)
#define __STM32412G_DISCOVERY_BSP_VERSION_SUB1 (0x00)
#define __STM32412G_DISCOVERY_BSP_VERSION_SUB2 (0x03)
#define __STM32412G_DISCOVERY_BSP_VERSION_RC (0x00)
#define __STM32412G_DISCOVERY_BSP_VERSION ((__STM32412G_DISCOVERY_BSP_VERSION_MAIN << 24)\
|(__STM32412G_DISCOVERY_BSP_VERSION_SUB1 << 16)\
|(__STM32412G_DISCOVERY_BSP_VERSION_SUB2 << 8 )\
|(__STM32412G_DISCOVERY_BSP_VERSION_RC))...
#define FMC_BANK1_BASE ((uint32_t)(0x60000000 | 0x00000000))
#define FMC_BANK3_BASE ((uint32_t)(0x60000000 | 0x08000000))
#define FMC_BANK1 ((LCD_CONTROLLER_TypeDef *) FMC_BANK1_BASE)
8 defines
/* ... */
/* ... */
const uint32_t GPIO_PIN[LEDn] = {LED1_PIN,
LED2_PIN,
LED3_PIN,
LED4_PIN...};
GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {WAKEUP_BUTTON_GPIO_PORT};
const uint16_t BUTTON_PIN[BUTTONn] = {WAKEUP_BUTTON_PIN};
const uint16_t BUTTON_IRQn[BUTTONn] = {WAKEUP_BUTTON_EXTI_IRQn};
GPIO_TypeDef* JOY_PORT[JOYn] = {SEL_JOY_GPIO_PORT,
DOWN_JOY_GPIO_PORT,
LEFT_JOY_GPIO_PORT,
RIGHT_JOY_GPIO_PORT,
UP_JOY_GPIO_PORT...};
const uint16_t JOY_PIN[JOYn] = {SEL_JOY_PIN,
DOWN_JOY_PIN,
LEFT_JOY_PIN,
RIGHT_JOY_PIN,
UP_JOY_PIN...};
const uint8_t JOY_IRQn[JOYn] = {SEL_JOY_EXTI_IRQn,
DOWN_JOY_EXTI_IRQn,
LEFT_JOY_EXTI_IRQn,
RIGHT_JOY_EXTI_IRQn,
UP_JOY_EXTI_IRQn...};
USART_TypeDef* COM_USART[COMn] = {DISCOVERY_COM1};
GPIO_TypeDef* COM_TX_PORT[COMn] = {DISCOVERY_COM1_TX_GPIO_PORT};
GPIO_TypeDef* COM_RX_PORT[COMn] = {DISCOVERY_COM1_RX_GPIO_PORT};
const uint16_t COM_TX_PIN[COMn] = {DISCOVERY_COM1_TX_PIN};
const uint16_t COM_RX_PIN[COMn] = {DISCOVERY_COM1_RX_PIN};
const uint16_t COM_TX_AF[COMn] = {DISCOVERY_COM1_TX_AF};
const uint16_t COM_RX_AF[COMn] = {DISCOVERY_COM1_RX_AF};
static I2C_HandleTypeDef hI2cAudioHandler;
static I2C_HandleTypeDef hI2cExtHandler;
/* ... */
/* ... */
static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler);
static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler);
static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials);
static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr);
static void FMC_BANK1_WriteData(uint16_t Data);
static void FMC_BANK1_WriteReg(uint8_t Reg);
static uint16_t FMC_BANK1_ReadData(void);
static void FMC_BANK1_Init(void);
static void FMC_BANK1_MspInit(void);
void LCD_IO_Init(void);
void LCD_IO_WriteData(uint16_t RegValue);
void LCD_IO_WriteReg(uint8_t Reg);
void LCD_IO_WriteMultipleData(uint16_t *pData, uint32_t Size);
uint16_t LCD_IO_ReadData(void);
void LCD_IO_Delay(uint32_t Delay);
void AUDIO_IO_Init(void);
void AUDIO_IO_DeInit(void);
void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value);
uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg);
void AUDIO_IO_Delay(uint32_t Delay);
void EEPROM_IO_Init(void);
HAL_StatusTypeDef EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
HAL_StatusTypeDef EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
void TS_IO_Init(void);
void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg);
uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
void TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
void TS_IO_Delay(uint32_t Delay);
/* ... */
/* ... */
/* ... */
uint32_t BSP_GetVersion(void)
{
return __STM32412G_DISCOVERY_BSP_VERSION;
}{ ... }
/* ... */
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef gpio_init_structure;
LEDx_GPIO_CLK_ENABLE();
gpio_init_structure.Pin = GPIO_PIN[Led];
gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LEDx_GPIO_PORT, &gpio_init_structure);
HAL_GPIO_WritePin(LEDx_GPIO_PORT, GPIO_PIN[Led], GPIO_PIN_SET);
}{ ... }
/* ... */
void BSP_LED_DeInit(Led_TypeDef Led)
{
GPIO_InitTypeDef gpio_init_structure;
HAL_GPIO_WritePin(LEDx_GPIO_PORT, GPIO_PIN[Led], GPIO_PIN_RESET);
gpio_init_structure.Pin = GPIO_PIN[Led];
HAL_GPIO_DeInit(LEDx_GPIO_PORT, gpio_init_structure.Pin);
}{ ... }
/* ... */
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(LEDx_GPIO_PORT, GPIO_PIN[Led], GPIO_PIN_RESET);
}{ ... }
/* ... */
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(LEDx_GPIO_PORT, GPIO_PIN[Led], GPIO_PIN_SET);
}{ ... }
/* ... */
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(LEDx_GPIO_PORT, GPIO_PIN[Led]);
}{ ... }
/* ... */
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
GPIO_InitTypeDef gpio_init_structure;
WAKEUP_BUTTON_GPIO_CLK_ENABLE();
if(ButtonMode == BUTTON_MODE_GPIO)
{
gpio_init_structure.Pin = BUTTON_PIN[Button];
gpio_init_structure.Mode = GPIO_MODE_INPUT;
gpio_init_structure.Pull = GPIO_PULLDOWN;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
}if (ButtonMode == BUTTON_MODE_GPIO) { ... }
if(ButtonMode == BUTTON_MODE_EXTI)
{
gpio_init_structure.Pin = BUTTON_PIN[Button];
gpio_init_structure.Pull = GPIO_PULLDOWN;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init_structure.Mode = GPIO_MODE_IT_RISING;
HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
}if (ButtonMode == BUTTON_MODE_EXTI) { ... }
}{ ... }
/* ... */
void BSP_PB_DeInit(Button_TypeDef Button)
{
GPIO_InitTypeDef gpio_init_structure;
gpio_init_structure.Pin = BUTTON_PIN[Button];
HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
}{ ... }
/* ... */
uint32_t BSP_PB_GetState(Button_TypeDef Button)
{
return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
}{ ... }
/* ... */
uint8_t BSP_JOY_Init(JOYMode_TypeDef Joy_Mode)
{
JOYState_TypeDef joykey;
GPIO_InitTypeDef GPIO_InitStruct;
for(joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++)
{
JOYx_GPIO_CLK_ENABLE(joykey);
GPIO_InitStruct.Pin = JOY_PIN[joykey];
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
if (Joy_Mode == JOY_MODE_GPIO)
{
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct);
}if (Joy_Mode == JOY_MODE_GPIO) { ... }
else if (Joy_Mode == JOY_MODE_EXTI)
{
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct);
HAL_NVIC_SetPriority((IRQn_Type)(JOY_IRQn[joykey]), 0x0F, 0x00);
HAL_NVIC_EnableIRQ((IRQn_Type)(JOY_IRQn[joykey]));
}else if (Joy_Mode == JOY_MODE_EXTI) { ... }
}for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) { ... }
return HAL_OK;
}{ ... }
/* ... */
void BSP_JOY_DeInit(void)
{
JOYState_TypeDef joykey;
for(joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++)
{
JOYx_GPIO_CLK_ENABLE(joykey);
HAL_GPIO_DeInit(JOY_PORT[joykey], JOY_PIN[joykey]);
}for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) { ... }
}{ ... }
/* ... */
JOYState_TypeDef BSP_JOY_GetState(void)
{
JOYState_TypeDef joykey;
for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++)
{
if (HAL_GPIO_ReadPin(JOY_PORT[joykey], JOY_PIN[joykey]) != GPIO_PIN_RESET)
{
return joykey;
}if (HAL_GPIO_ReadPin(JOY_PORT[joykey], JOY_PIN[joykey]) != GPIO_PIN_RESET) { ... }
}for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) { ... }
return JOY_NONE;
}{ ... }
/* ... */
void BSP_COM_Init(COM_TypeDef COM, UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef gpio_init_structure;
DISCOVERY_COMx_TX_GPIO_CLK_ENABLE(COM);
DISCOVERY_COMx_RX_GPIO_CLK_ENABLE(COM);
DISCOVERY_COMx_CLK_ENABLE(COM);
gpio_init_structure.Pin = COM_TX_PIN[COM];
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Alternate = COM_TX_AF[COM];
HAL_GPIO_Init(COM_TX_PORT[COM], &gpio_init_structure);
gpio_init_structure.Pin = COM_RX_PIN[COM];
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Alternate = COM_RX_AF[COM];
HAL_GPIO_Init(COM_RX_PORT[COM], &gpio_init_structure);
huart->Instance = COM_USART[COM];
HAL_UART_Init(huart);
}{ ... }
/* ... */
void BSP_COM_DeInit(COM_TypeDef COM, UART_HandleTypeDef *huart)
{
huart->Instance = COM_USART[COM];
HAL_UART_DeInit(huart);
DISCOVERY_COMx_CLK_DISABLE(COM);
/* ... */
/* ... */
}{ ... }
/* ... */
/* ... */
static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler)
{
GPIO_InitTypeDef gpio_init_structure;
if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
{
DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SCL_PIN;
gpio_init_structure.Mode = GPIO_MODE_AF_OD;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init_structure.Alternate = DISCOVERY_AUDIO_I2Cx_SCL_SDA_AF;
HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SDA_PIN;
HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
DISCOVERY_AUDIO_I2Cx_CLK_ENABLE();
DISCOVERY_AUDIO_I2Cx_FORCE_RESET();
DISCOVERY_AUDIO_I2Cx_RELEASE_RESET();
HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_EV_IRQn, 0x0F, 0x00);
HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_EV_IRQn);
HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_ER_IRQn, 0x0F, 0x00);
HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_ER_IRQn);
}if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler)) { ... }
else
{
DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SCL_PIN;
gpio_init_structure.Mode = GPIO_MODE_AF_OD;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init_structure.Alternate = DISCOVERY_EXT_I2Cx_SCL_AF;
HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SDA_PIN;
gpio_init_structure.Alternate = DISCOVERY_EXT_I2Cx_SDA_AF;
HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
DISCOVERY_EXT_I2Cx_CLK_ENABLE();
DISCOVERY_EXT_I2Cx_FORCE_RESET();
DISCOVERY_EXT_I2Cx_RELEASE_RESET();
HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_EV_IRQn, 0x0F, 0x00);
HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_EV_IRQn);
HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_ER_IRQn, 0x0F, 0x00);
HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_ER_IRQn);
}else { ... }
}{ ... }
/* ... */
static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler)
{
if(HAL_I2C_GetState(i2c_handler) == HAL_I2C_STATE_RESET)
{
if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
{
i2c_handler->Instance = DISCOVERY_AUDIO_I2Cx;
}if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler)) { ... }
else
{
i2c_handler->Instance = DISCOVERY_EXT_I2Cx;
}else { ... }
i2c_handler->Init.ClockSpeed = DISCOVERY_I2C_SPEED;
i2c_handler->Init.DutyCycle = I2C_DUTYCYCLE_2;
i2c_handler->Init.OwnAddress1 = 0;
i2c_handler->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c_handler->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
i2c_handler->Init.OwnAddress2 = 0;
i2c_handler->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
i2c_handler->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
I2Cx_MspInit(i2c_handler);
HAL_I2C_Init(i2c_handler);
}if (HAL_I2C_GetState(i2c_handler) == HAL_I2C_STATE_RESET) { ... }
}{ ... }
/* ... */
static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler,
uint8_t Addr,
uint16_t Reg,
uint16_t MemAddress,
uint8_t *Buffer,
uint16_t Length)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Read(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
if(status != HAL_OK)
{
I2Cx_Error(i2c_handler, Addr);
}if (status != HAL_OK) { ... }
return status;
}{ ... }
/* ... */
static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler,
uint8_t Addr,
uint16_t Reg,
uint16_t MemAddress,
uint8_t *Buffer,
uint16_t Length)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
if(status != HAL_OK)
{
I2Cx_Error(i2c_handler, Addr);
}if (status != HAL_OK) { ... }
return status;
}{ ... }
/* ... */
static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials)
{
return (HAL_I2C_IsDeviceReady(i2c_handler, DevAddress, Trials, 1000));
}{ ... }
/* ... */
static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr)
{
HAL_I2C_DeInit(i2c_handler);
I2Cx_Init(i2c_handler);
}{ ... }
/* ... */
static void I2Cx_DeInit(I2C_HandleTypeDef *i2c_handler)
{
if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))
{
i2c_handler->Instance = DISCOVERY_AUDIO_I2Cx;
}if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler)) { ... }
else
{
i2c_handler->Instance = DISCOVERY_EXT_I2Cx;
}else { ... }
__HAL_I2C_DISABLE(i2c_handler);
HAL_I2C_DeInit(i2c_handler);
}{ ... }
I2C Routines
/* ... */
static void FMC_BANK1_MspInit(void)
{
GPIO_InitTypeDef gpio_init_structure;
__HAL_RCC_FSMC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init_structure.Alternate = GPIO_AF12_FSMC;
gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 |\
GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOD, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 |\
GPIO_PIN_12 |GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
HAL_GPIO_Init(GPIOE, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_0 ;
HAL_GPIO_Init(GPIOF, &gpio_init_structure);
}{ ... }
/* ... */
static void FMC_BANK1_Init(void)
{
SRAM_HandleTypeDef hsram;
FMC_NORSRAM_TimingTypeDef sram_timing;
FMC_NORSRAM_TimingTypeDef sram_timing_write;
hsram.Instance = FSMC_NORSRAM_DEVICE;
hsram.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
sram_timing.AddressSetupTime = 9;
sram_timing.AddressHoldTime = 1;
sram_timing.DataSetupTime = 36;
sram_timing.BusTurnAroundDuration = 1;
sram_timing.CLKDivision = 2;
sram_timing.DataLatency = 2;
sram_timing.AccessMode = FSMC_ACCESS_MODE_A;
sram_timing_write.AddressSetupTime = 1;
sram_timing_write.AddressHoldTime = 1;
sram_timing_write.DataSetupTime = 7;
sram_timing_write.BusTurnAroundDuration = 0;
sram_timing_write.CLKDivision = 2;
sram_timing_write.DataLatency = 2;
sram_timing_write.AccessMode = FSMC_ACCESS_MODE_A;
hsram.Init.NSBank = FSMC_NORSRAM_BANK1;
hsram.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
hsram.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
hsram.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
hsram.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
hsram.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
hsram.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
hsram.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
hsram.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
hsram.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
hsram.Init.ExtendedMode = FSMC_EXTENDED_MODE_ENABLE;
hsram.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
hsram.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
hsram.Init.WriteFifo = FSMC_WRITE_FIFO_DISABLE;
hsram.Init.PageSize = FSMC_PAGE_SIZE_NONE;
hsram.Init.ContinuousClock = FSMC_CONTINUOUS_CLOCK_SYNC_ONLY;
FMC_BANK1_MspInit();
HAL_SRAM_Init(&hsram, &sram_timing, &sram_timing_write);
}{ ... }
/* ... */
static void FMC_BANK1_WriteData(uint16_t Data)
{
FMC_BANK1->RAM = Data;
__DSB();
}{ ... }
/* ... */
static void FMC_BANK1_WriteReg(uint8_t Reg)
{
FMC_BANK1->REG = Reg;
__DSB();
}{ ... }
/* ... */
static uint16_t FMC_BANK1_ReadData(void)
{
return FMC_BANK1->RAM;
}{ ... }
/* ... */
FMC Routines
/* ... */
void LCD_IO_Init(void)
{
FMC_BANK1_Init();
}{ ... }
/* ... */
void LCD_IO_WriteData(uint16_t RegValue)
{
FMC_BANK1_WriteData(RegValue);
}{ ... }
/* ... */
void LCD_IO_WriteMultipleData(uint16_t *pData, uint32_t Size)
{
uint32_t i;
for (i = 0; i < Size; i++)
{
FMC_BANK1_WriteData(pData[i]);
}for (i = 0; i < Size; i++) { ... }
}{ ... }
/* ... */
void LCD_IO_WriteReg(uint8_t Reg)
{
FMC_BANK1_WriteReg(Reg);
}{ ... }
/* ... */
uint16_t LCD_IO_ReadData(void)
{
return FMC_BANK1_ReadData();
}{ ... }
/* ... */
void LCD_IO_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}{ ... }
LINK LCD
/* ... */
void AUDIO_IO_Init(void)
{
I2Cx_Init(&hI2cAudioHandler);
}{ ... }
/* ... */
void AUDIO_IO_DeInit(void)
{
I2Cx_DeInit(&hI2cAudioHandler);
}{ ... }
/* ... */
void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value)
{
uint16_t tmp = Value;
Value = ((uint16_t)(tmp >> 8) & 0x00FF);
Value |= ((uint16_t)(tmp << 8)& 0xFF00);
I2Cx_WriteMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_16BIT,(uint8_t*)&Value, 2);
}{ ... }
/* ... */
uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg)
{
uint16_t read_value = 0, tmp = 0;
I2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_16BIT, (uint8_t*)&read_value, 2);
tmp = ((uint16_t)(read_value >> 8) & 0x00FF);
tmp |= ((uint16_t)(read_value << 8)& 0xFF00);
read_value = tmp;
return read_value;
}{ ... }
/* ... */
void AUDIO_IO_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}{ ... }
LINK AUDIO
/* ... */
void EEPROM_IO_Init(void)
{
I2Cx_Init(&hI2cExtHandler);
}{ ... }
/* ... */
HAL_StatusTypeDef EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
{
return (I2Cx_WriteMultiple(&hI2cExtHandler, DevAddress, MemAddress, I2C_MEMADD_SIZE_16BIT, pBuffer, BufferSize));
}{ ... }
/* ... */
HAL_StatusTypeDef EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
{
return (I2Cx_ReadMultiple(&hI2cExtHandler, DevAddress, MemAddress, I2C_MEMADD_SIZE_16BIT, pBuffer, BufferSize));
}{ ... }
/* ... */
HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
{
return (I2Cx_IsDeviceReady(&hI2cExtHandler, DevAddress, Trials));
}{ ... }
LINK I2C EEPROM
/* ... */
void TS_IO_Init(void)
{
I2Cx_Init(&hI2cAudioHandler);
}{ ... }
/* ... */
void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
{
I2Cx_WriteMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
}{ ... }
/* ... */
uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg)
{
uint8_t read_value = 0;
I2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&read_value, 1);
return read_value;
}{ ... }
/* ... */
uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
{
return I2Cx_ReadMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
}{ ... }
/* ... */
void TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
{
I2Cx_WriteMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
}{ ... }
/* ... */
void TS_IO_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */