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
29
30
33
34
39
40
43
46
47
48
51
52
55
56
57
58
59
63
64
67
68
69
70
73
74
77
80
81
84
85
86
87
88
89
90
91
92
95
96
97
107
108
109
112
113
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
144
145
146
147
148
151
152
155
156
160
164
165
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
196
197
198
199
200
201
202
203
204
205
206
207
213
217
218
224
228
229
235
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
287
288
289
290
291
292
293
294
295
296
302
306
307
310
311
312
313
314
315
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
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
390
391
392
393
394
395
396
397
402
403
404
408
409
410
411
412
413
414
415
416
421
422
423
426
427
428
429
430
431
432
433
434
435
438
439
440
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
473
474
475
479
490
491
497
502
503
507
514
515
516
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
589
590
591
592
593
594
595
596
597
598
599
600
606
607
608
609
610
611
612
615
616
617
618
621
622
623
624
625
626
627
630
631
632
633
634
635
636
637
638
639
640
644
648
649
650
651
652
653
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
722
729
730
731
732
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
757
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
791
792
796
800
804
808
812
816
817
818
822
823
824
825
826
827
828
829
830
833
834
837
838
841
842
/* ... */
#include "stm32f4xx_nucleo.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
#define __STM32F4xx_NUCLEO_BSP_VERSION_MAIN (0x01)
#define __STM32F4xx_NUCLEO_BSP_VERSION_SUB1 (0x02)
#define __STM32F4xx_NUCLEO_BSP_VERSION_SUB2 (0x09)
#define __STM32F4xx_NUCLEO_BSP_VERSION_RC (0x00)
#define __STM32F4xx_NUCLEO_BSP_VERSION ((__STM32F4xx_NUCLEO_BSP_VERSION_MAIN << 24)\
|(__STM32F4xx_NUCLEO_BSP_VERSION_SUB1 << 16)\
|(__STM32F4xx_NUCLEO_BSP_VERSION_SUB2 << 8 )\
|(__STM32F4xx_NUCLEO_BSP_VERSION_RC)) ...
/* ... */
#define SD_DUMMY_BYTE 0xFF
#define SD_NO_RESPONSE_EXPECTED 0x80
7 defines
/* ... */
/* ... */
/* ... */
/* ... */
GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
/* ... */
#ifdef ADAFRUIT_TFT_JOY_SD_ID802
#ifdef HAL_SPI_MODULE_ENABLED
uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX;
static SPI_HandleTypeDef hnucleo_Spi;/* ... */
#endif
#ifdef HAL_ADC_MODULE_ENABLED
static ADC_HandleTypeDef hnucleo_Adc;
static ADC_ChannelConfTypeDef sConfig;/* ... */
#endif /* ... */
#endif
/* ... */
/* ... */
#ifdef ADAFRUIT_TFT_JOY_SD_ID802
#ifdef HAL_SPI_MODULE_ENABLED
static void SPIx_Init(void);
static void SPIx_Write(uint8_t Value);
static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLegnth);
static void SPIx_Error(void);
static void SPIx_MspInit(SPI_HandleTypeDef *hspi);
void SD_IO_Init(void);
void SD_IO_CSState(uint8_t state);
void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
uint8_t SD_IO_WriteByte(uint8_t Data);
void LCD_IO_Init(void);
void LCD_IO_WriteData(uint8_t Data);
void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
void LCD_IO_WriteReg(uint8_t LCDReg);
void LCD_Delay(uint32_t delay);/* ... */
#endif
#ifdef HAL_ADC_MODULE_ENABLED
static void ADCx_Init(void);
static void ADCx_DeInit(void);
static void ADCx_MspInit(ADC_HandleTypeDef *hadc);
static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc);/* ... */
#endif
/* ... */
#endif
/* ... */
/* ... */
/* ... */
uint32_t BSP_GetVersion(void)
{
return __STM32F4xx_NUCLEO_BSP_VERSION;
}{ ... }
/* ... */
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_InitStruct;
LEDx_GPIO_CLK_ENABLE(Led);
GPIO_InitStruct.Pin = GPIO_PIN[Led];
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
}{ ... }
/* ... */
void BSP_LED_DeInit(Led_TypeDef Led)
{
GPIO_InitTypeDef gpio_init_structure;
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
gpio_init_structure.Pin = GPIO_PIN[Led];
HAL_GPIO_DeInit(GPIO_PORT[Led], gpio_init_structure.Pin);
}{ ... }
/* ... */
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
}{ ... }
/* ... */
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
}{ ... }
/* ... */
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
}{ ... }
/* ... */
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
GPIO_InitTypeDef GPIO_InitStruct;
BUTTONx_GPIO_CLK_ENABLE(Button);
if(ButtonMode == BUTTON_MODE_GPIO)
{
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
}if (ButtonMode == BUTTON_MODE_GPIO) { ... }
if(ButtonMode == BUTTON_MODE_EXTI)
{
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
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]);
}{ ... }
/* ... */
#ifdef ADAFRUIT_TFT_JOY_SD_ID802
#ifdef HAL_SPI_MODULE_ENABLED
/* ... */
static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_InitStruct;
NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF;
HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = NUCLEO_SPIx_MOSI_PIN;
GPIO_InitStruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = NUCLEO_SPIx_MISO_PIN;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
NUCLEO_SPIx_CLK_ENABLE();
}{ ... }
/* ... */
static void SPIx_Init(void)
{
if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
{
hnucleo_Spi.Instance = NUCLEO_SPIx;
/* ... */
hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
hnucleo_Spi.Init.CLKPhase = SPI_PHASE_2EDGE;
hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_HIGH;
hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
hnucleo_Spi.Init.CRCPolynomial = 7;
hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLED;
hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
SPIx_MspInit(&hnucleo_Spi);
HAL_SPI_Init(&hnucleo_Spi);
}if (HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET) { ... }
}{ ... }
/* ... */
static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLegnth)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) DataIn, DataOut, DataLegnth, SpixTimeout);
if(status != HAL_OK)
{
SPIx_Error();
}if (status != HAL_OK) { ... }
}{ ... }
/* ... */
static void SPIx_Write(uint8_t Value)
{
HAL_StatusTypeDef status = HAL_OK;
uint8_t data;
status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &Value, &data, 1, SpixTimeout);
if(status != HAL_OK)
{
SPIx_Error();
}if (status != HAL_OK) { ... }
}{ ... }
/* ... */
static void SPIx_Error (void)
{
HAL_SPI_DeInit(&hnucleo_Spi);
SPIx_Init();
}{ ... }
/* ... */
/* ... */
void SD_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
uint8_t counter;
SD_CS_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = SD_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
SPIx_Init();
SD_CS_HIGH();
for (counter = 0; counter <= 9; counter++)
{
SD_IO_WriteByte(SD_DUMMY_BYTE);
}for (counter = 0; counter <= 9; counter++) { ... }
}{ ... }
/* ... */
void SD_IO_CSState(uint8_t val)
{
if(val == 1)
{
SD_CS_HIGH();
}if (val == 1) { ... }
else
{
SD_CS_LOW();
}else { ... }
}{ ... }
/* ... */
void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
{
SPIx_WriteReadData(DataIn, DataOut, DataLength);
}{ ... }
/* ... */
uint8_t SD_IO_WriteByte(uint8_t Data)
{
uint8_t tmp;
SPIx_WriteReadData(&Data,&tmp,1);
return tmp;
}{ ... }
LINK SD
/* ... */
void LCD_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
LCD_CS_GPIO_CLK_ENABLE();
LCD_DC_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = LCD_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(LCD_CS_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LCD_DC_PIN;
HAL_GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStruct);
LCD_CS_HIGH();
SPIx_Init();
}{ ... }
/* ... */
void LCD_IO_WriteReg(uint8_t LCDReg)
{
LCD_CS_LOW();
LCD_DC_LOW();
SPIx_Write(LCDReg);
LCD_CS_HIGH();
}{ ... }
/* ... */
void LCD_IO_WriteData(uint8_t Data)
{
LCD_CS_LOW();
LCD_DC_HIGH();
SPIx_Write(Data);
LCD_CS_HIGH();
}{ ... }
/* ... */
void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
{
uint32_t counter = 0;
__IO uint32_t data = 0;
LCD_CS_LOW();
LCD_DC_HIGH();
if (Size == 1)
{
SPIx_Write(*pData);
}if (Size == 1) { ... }
else
{
for (counter = Size; counter != 0; counter--)
{
while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
{
}while (((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE) { ... }
*((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *(pData+1);
while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
{
}while (((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE) { ... }
*((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *pData;
counter--;
pData += 2;
}for (counter = Size; counter != 0; counter--) { ... }
while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET)
{
}while (((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET) { ... }
}else { ... }
data = *(&hnucleo_Spi.Instance->DR);
UNUSED(data);
LCD_CS_HIGH();
}{ ... }
/* ... */
void LCD_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}{ ... }
#endif/* ... */
#ifdef HAL_ADC_MODULE_ENABLED
/* ... */
static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
{
GPIO_InitTypeDef GPIO_InitStruct;
NUCLEO_ADCx_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &GPIO_InitStruct);
NUCLEO_ADCx_CLK_ENABLE();
}{ ... }
/* ... */
static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc)
{
GPIO_InitTypeDef GPIO_InitStruct;
NUCLEO_ADCx_CLK_DISABLE();
GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
HAL_GPIO_DeInit(NUCLEO_ADCx_GPIO_PORT, GPIO_InitStruct.Pin);
}{ ... }
/* ... */
static void ADCx_Init(void)
{
if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
{
hnucleo_Adc.Instance = NUCLEO_ADCx;
hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
hnucleo_Adc.Init.Resolution = ADC_RESOLUTION12b;
hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hnucleo_Adc.Init.ContinuousConvMode = DISABLE;
hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hnucleo_Adc.Init.EOCSelection = EOC_SINGLE_CONV;
hnucleo_Adc.Init.NbrOfConversion = 1;
hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;
ADCx_MspInit(&hnucleo_Adc);
HAL_ADC_Init(&hnucleo_Adc);
}if (HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET) { ... }
}{ ... }
/* ... */
static void ADCx_DeInit(void)
{
hnucleo_Adc.Instance = NUCLEO_ADCx;
HAL_ADC_DeInit(&hnucleo_Adc);
ADCx_MspDeInit(&hnucleo_Adc);
}{ ... }
/* ... */
uint8_t BSP_JOY_Init(void)
{
uint8_t status = HAL_ERROR;
ADCx_Init();
sConfig.Channel = NUCLEO_ADCx_CHANNEL;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
sConfig.Rank = 1;
status = HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig);
return status;
}{ ... }
/* ... */
void BSP_JOY_DeInit(void)
{
ADCx_DeInit();
}{ ... }
/* ... */
JOYState_TypeDef BSP_JOY_GetState(void)
{
JOYState_TypeDef state;
uint16_t keyconvertedvalue = 0;
HAL_ADC_Start(&hnucleo_Adc);
HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
if(((HAL_ADC_GetState(&hnucleo_Adc) & HAL_ADC_STATE_EOC_REG) == HAL_ADC_STATE_EOC_REG))
{
keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
}if (((HAL_ADC_GetState(&hnucleo_Adc) & HAL_ADC_STATE_EOC_REG) == HAL_ADC_STATE_EOC_REG)) { ... }
if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090))
{
state = JOY_UP;
}if ((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090)) { ... }
else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780))
{
state = JOY_RIGHT;
}else if ((keyconvertedvalue > 680) && (keyconvertedvalue < 780)) { ... }
else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350))
{
state = JOY_SEL;
}else if ((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350)) { ... }
else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130))
{
state = JOY_DOWN;
}else if ((keyconvertedvalue > 50) && (keyconvertedvalue < 130)) { ... }
else if((keyconvertedvalue > 3680) && (keyconvertedvalue < 3760))
{
state = JOY_LEFT;
}else if ((keyconvertedvalue > 3680) && (keyconvertedvalue < 3760)) { ... }
else
{
state = JOY_NONE;
}else { ... }
if(state != JOY_NONE)
{
keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
}if (state != JOY_NONE) { ... }
return state;
}{ ... }
#endif/* ... */
/* ... */
#endif
/* ... */
/* ... */
/* ... */
/* ... */