-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32l4xx_nucleo.c
1139 lines (965 loc) · 32.4 KB
/
stm32l4xx_nucleo.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
107
108
109
110
111
112
113
114
115
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
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
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
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
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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
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
510
511
512
513
514
515
516
517
518
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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
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
666
667
668
669
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
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
794
795
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file stm32l4xx_nucleo.c
* @author MCD Application Team
* @brief This file provides set of firmware functions to manage:
* - LEDs and push-button available on STM32L4XX-Nucleo Kit
* from STMicroelectronics
* - LCD, joystick and microSD available on Adafruit 1.8" TFT LCD
* shield (reference ID 802), external SMPS
******************************************************************************
* @attention
*
* Copyright (c) 2015 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32l4xx_nucleo.h"
/** @addtogroup BSP
* @{
*/
/** @defgroup STM32L4XX_NUCLEO NUCLEO 64
* @brief This file provides set of firmware functions to manage Leds and push-button
* available on STM32L4XX-Nucleo Kit from STMicroelectronics.
* It provides also LCD, joystick and uSD functions to communicate with
* Adafruit 1.8" TFT LCD shield (reference ID 802)
* @{
*/
/** @defgroup STM32L4XX_NUCLEO_Private_Constants Private Constants
* @{
*/
/**
* @brief STM32L4XX NUCLEO BSP Driver version number
*/
#define __STM32L4XX_NUCLEO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
#define __STM32L4XX_NUCLEO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
#define __STM32L4XX_NUCLEO_BSP_VERSION_SUB2 (0x08) /*!< [15:8] sub2 version */
#define __STM32L4XX_NUCLEO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
#define __STM32L4XX_NUCLEO_BSP_VERSION ((__STM32L4XX_NUCLEO_BSP_VERSION_MAIN << 24)\
|(__STM32L4XX_NUCLEO_BSP_VERSION_SUB1 << 16)\
|(__STM32L4XX_NUCLEO_BSP_VERSION_SUB2 << 8 )\
|(__STM32L4XX_NUCLEO_BSP_VERSION_RC))
/**
* @brief LINK SD Card
*/
#define SD_DUMMY_BYTE 0xFF
#define SD_NO_RESPONSE_EXPECTED 0x80
#ifdef USE_STM32L4XX_NUCLEO_64_SMPS
/**
* @brief SMPS
*/
#ifdef USE_ADP5301ACBZ /* ADP5301ACBZ */
/* ######################################################################## */
/* #define PORT_SMPS GPIOA */
/* #define PIN_SMPS_ENABLE GPIO_PIN_4 */
/* #define PIN_SMPS_POWERGOOD GPIO_PIN_6 */
/* #define PIN_SMPS_SWITCH_ENABLE GPIO_PIN_7 */
/* IN CASE OF SMPS VOLTAGE RANGE SELECTION */
/* #define PIN_SMPS_V1 GPIO_PIN_5 */
/* ######################################################################## */
#endif /* ADP5301ACBZ */
/**
* @}
*/
#endif /* USE_STM32L4XX_NUCLEO_64_SMPS */
/** @defgroup STM32L4XX_NUCLEO_Private_Variables Private Variables
* @{
*/
#ifdef USE_STM32L4XX_NUCLEO_64_SMPS
GPIO_TypeDef *GPIO_PORT[LEDn] = {LED4_GPIO_PORT};
const uint16_t GPIO_PIN[LEDn] = {LED4_PIN};
#else
GPIO_TypeDef *GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
#endif
GPIO_TypeDef *BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT};
const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN};
const uint16_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
/**
* @brief BUS variables
*/
#ifdef HAL_SPI_MODULE_ENABLED
uint32_t hnucleo_SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
static SPI_HandleTypeDef hnucleo_Spi;
#endif /* HAL_SPI_MODULE_ENABLED */
#ifdef HAL_ADC_MODULE_ENABLED
static ADC_HandleTypeDef hnucleo_Adc;
/* ADC channel configuration structure declaration */
static ADC_ChannelConfTypeDef hnucleo_AdcChannelConfig;
#endif /* HAL_ADC_MODULE_ENABLED */
/**
* @}
*/
/* Private Function Prototypes */
#ifdef HAL_SPI_MODULE_ENABLED
static void SPIx_Init(void);
static void SPIx_MspInit(void);
static void SPIx_Write(uint8_t Value);
static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
static void SPIx_Error(void);
#endif /* HAL_SPI_MODULE_ENABLED */
#ifdef HAL_ADC_MODULE_ENABLED
static HAL_StatusTypeDef ADCx_Init(void);
static void ADCx_DeInit(void);
static void ADCx_MspInit(ADC_HandleTypeDef *hadc);
static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc);
#endif /* HAL_ADC_MODULE_ENABLED */
#ifdef HAL_SPI_MODULE_ENABLED
/* SD IO functions */
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);
/* LCD IO functions */
void LCD_IO_Init(void);
void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
void LCD_IO_WriteReg(uint8_t LCDReg);
void LCD_Delay(uint32_t delay);
#endif /* HAL_SPI_MODULE_ENABLED */
/** @defgroup STM32L4XX_NUCLEO_Exported_Functions Exported Functions
* @{
*/
/**
* @brief This method returns the STM32L4XX NUCLEO BSP Driver revision
* @retval version : 0xXYZR (8bits for each decimal, R for RC)
*/
uint32_t BSP_GetVersion(void)
{
return __STM32L4XX_NUCLEO_BSP_VERSION;
}
/** @defgroup STM32L4XX_NUCLEO_LED_Functions LED Functions
* @{
*/
/**
* @brief Configure LED GPIO.
* @param Led: LED to be configured.
* This parameter can be one of the following values:
* @arg LED2 or LED4 on Nucleo-64 with external SMPS
* @retval None
*/
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Enable the GPIO_LED Clock */
LEDx_GPIO_CLK_ENABLE(Led);
/* Configure the GPIO_LED pin */
GPIO_InitStruct.Pin = GPIO_PIN[Led];
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
}
/**
* @brief DeInitialize LED GPIO.
* @param Led: LED to be deinitialized.
* This parameter can be one of the following values:
* @arg LED2 or LED4 on Nucleo-64 with external SMPS
* @note BSP_LED_DeInit() does not disable the GPIO clock
* @retval None
*/
void BSP_LED_DeInit(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Turn off LED */
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
/* DeInit the GPIO_LED pin */
GPIO_InitStruct.Pin = GPIO_PIN[Led];
HAL_GPIO_DeInit(GPIO_PORT[Led], GPIO_InitStruct.Pin);
}
/**
* @brief Turn selected LED On.
* @param Led: Specifies the Led to be set on.
* This parameter can be one of following parameters:
* @arg LED2 or LED4 on Nucleo-64 with external SMPS
* @retval None
*/
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
}
/**
* @brief Turn selected LED Off.
* @param Led: Specifies the Led to be set off.
* This parameter can be one of following parameters:
* @arg LED2 or LED4 on Nucleo-64 with external SMPS
* @retval None
*/
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
}
/**
* @brief Toggle the selected LED.
* @param Led: Specifies the Led to be toggled.
* This parameter can be one of following parameters:
* @arg LED2 or LED4 on Nucleo-64 with external SMPS
* @retval None
*/
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
}
/**
* @}
*/
/** @defgroup STM32L4XX_NUCLEO_BUTTON_Functions BUTTON Functions
* @{
*/
/**
* @brief Configure Button GPIO and EXTI Line.
* @param Button: Specifies the Button to be configured.
* This parameter should be: BUTTON_USER
* @param ButtonMode: Specifies Button mode.
* This parameter can be one of following parameters:
* @arg BUTTON_MODE_GPIO: Button will be used as simple IO
* @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
* generation capability
* @retval None
*/
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Enable the BUTTON Clock */
BUTTONx_GPIO_CLK_ENABLE(Button);
if (ButtonMode == BUTTON_MODE_GPIO)
{
/* Configure Button pin as input */
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
}
else if (ButtonMode == BUTTON_MODE_EXTI)
{
/* Configure Button pin as input with External interrupt */
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
}
}
/**
* @brief DeInitialize Push Button.
* @param Button: Button to be configured
* This parameter should be: BUTTON_USER
* @note BSP_PB_DeInit() does not disable the GPIO clock
* @retval None
*/
void BSP_PB_DeInit(Button_TypeDef Button)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
HAL_GPIO_DeInit(BUTTON_PORT[Button], GPIO_InitStruct.Pin);
}
/**
* @brief Return the selected Button state.
* @param Button: Specifies the Button to be checked.
* This parameter should be: BUTTON_USER
* @retval Button state.
*/
uint32_t BSP_PB_GetState(Button_TypeDef Button)
{
return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
}
#ifdef HAL_ADC_MODULE_ENABLED
/**
* @brief Configure joystick available on adafruit 1.8" TFT shield
* managed through ADC to detect motion.
* @retval Joystickstatus (0=> success, 1=> fail)
*/
uint8_t BSP_JOY_Init(void)
{
if (ADCx_Init() != HAL_OK)
{
return (uint8_t) HAL_ERROR;
}
/* Select ADC Channel to be converted */
hnucleo_AdcChannelConfig.Channel = NUCLEO_ADCx_CHANNEL;
hnucleo_AdcChannelConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES_5;
hnucleo_AdcChannelConfig.Rank = ADC_REGULAR_RANK_1;
hnucleo_AdcChannelConfig.SingleDiff = ADC_SINGLE_ENDED;
hnucleo_AdcChannelConfig.OffsetNumber = ADC_OFFSET_NONE;
/* Return Joystick initialization status */
return (uint8_t) HAL_ADC_ConfigChannel(&hnucleo_Adc, &hnucleo_AdcChannelConfig);
}
/**
* @brief DeInitialize joystick GPIOs.
* @note JOY DeInit does not disable the MFX IO Expander, just set the MFX IO Expander pins in Off mode
* @retval None.
*/
void BSP_JOY_DeInit(void)
{
ADCx_DeInit();
}
/**
* @brief Return the Joystick key pressed.
* @note To know which Joystick key is pressed we need to detect the voltage
* level on each key output
* - None : 3.3 V / 4095
* - SEL : 1.055 V / 1308
* - DOWN : 0.71 V / 88
* - LEFT : 3.0 V / 3720
* - RIGHT : 0.595 V / 737
* - UP : 1.65 V / 2046
* @retval JOYState_TypeDef: Code of the Joystick key pressed.
*/
JOYState_TypeDef BSP_JOY_GetState(void)
{
JOYState_TypeDef state = JOY_NONE;
uint16_t keyconvertedvalue = 0;
/* Start the conversion process */
HAL_ADC_Start(&hnucleo_Adc);
/* Wait for the end of conversion */
HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
/* Check if the continuous conversion of regular channel is finished */
if (HAL_ADC_GetState(&hnucleo_Adc) & HAL_ADC_STATE_REG_EOC)
{
/* Get the converted value of regular channel */
keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
}
if ((keyconvertedvalue > 1980) && (keyconvertedvalue < 2120))
{
state = JOY_UP;
}
else if ((keyconvertedvalue > 630) && (keyconvertedvalue < 830))
{
state = JOY_RIGHT;
}
else if ((keyconvertedvalue > 1210) && (keyconvertedvalue < 1410))
{
state = JOY_SEL;
}
else if ((keyconvertedvalue > 20) && (keyconvertedvalue < 160))
{
state = JOY_DOWN;
}
else if ((keyconvertedvalue > 3620) && (keyconvertedvalue < 3820))
{
state = JOY_LEFT;
}
else
{
state = JOY_NONE;
}
/* Return the code of the Joystick key pressed*/
return state;
}
#endif /* HAL_ADC_MODULE_ENABLED */
/**
* @}
*/
#ifdef USE_STM32L4XX_NUCLEO_64_SMPS
#ifdef USE_ADP5301ACBZ /* ADP5301ACBZ */
/** @defgroup STM32L4XX_NUCLEO_SMPS_Functions SMPS Functions
* @{
*/
/******************************************************************************
SMPS OPERATIONS
*******************************************************************************/
/**
* @brief DeInitialize the external SMPS component
* @note Low power consumption GPIO settings
* @retval SMPS status
*/
uint32_t BSP_SMPS_DeInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
PWR_AND_CLK_SMPS();
/* -------------------------------------------------------------------------------------- */
/* Added for Deinit if No PIN_SMPS_ENABLE & PIN_SMPS_SWITCH_ENABLE are not disabled before */
/* Disable SMPS SWITCH */
HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE, GPIO_PIN_RESET);
HAL_Delay(1);
/* Disable SMPS */
HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_ENABLE, GPIO_PIN_RESET);
/* -------------------------------------------------------------------------------------- */
/* Set all GPIO in output push/pull pulldown state to reduce power consumption */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
/* Consider all SMPS pins but V1, not used on ADP5301ACBZ */
GPIO_InitStruct.Pin = PIN_SMPS_ENABLE | PIN_SMPS_SWITCH_ENABLE | PIN_SMPS_POWERGOOD;
HAL_GPIO_Init(PORT_SMPS, &GPIO_InitStruct);
return SMPS_OK;
}
/**
* @brief Initialize the external SMPS component
* @param VoltageRange: Select operating SMPS supply
* @arg DCDC_AND_BOARD_DEPENDENT
* @note VoltageRange is not used with all boards. When not
* used, resort to PWR_REGULATOR_VOLTAGE_SCALE2 by default.
* @retval SMPS status
*/
uint32_t BSP_SMPS_Init(uint32_t VoltageRange)
{
PWR_AND_CLK_SMPS();
GPIO_InitTypeDef GPIO_InitStruct;
/* Reconfigure PWR_PUCRx/PDCRx registers only when not coming */
/* back from Standby or Shutdown states. */
/* Consider as well non-SMPS related pins. */
if (!(READ_BIT(PWR->PWR_PU_REG, PWR_GPIO_ENABLE)))
{
HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_SMPS, PWR_GPIO_SWITCH_ENABLE);
HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_SMPS, PWR_GPIO_ENABLE);
/* HW limitation: Level shifter consumes because of dangling, so pull PA2 up
(LPUART1_TX), PA13 (SWD/TMS) and PB3 (SWO) */
HAL_PWREx_EnableGPIOPullUp(PWR_GPIO_A, GPIO_PIN_2); /* LPUART1_TX */
HAL_PWREx_EnableGPIOPullUp(PWR_GPIO_A, GPIO_PIN_13); /* SWD/TMS */
HAL_PWREx_EnableGPIOPullUp(PWR_GPIO_B, GPIO_PIN_3); /* SWO */
/* Don't set PWR_CR3 APC bit at this time as it increases power
consumption in non-Standby/Shutdown modes. It will have to be
set with HAL_PWREx_EnablePullUpPullDownConfig() API upon
Standby or Shutdown modes entering */
}
/* ------------------------------------------------------------------------ */
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Pin = PIN_SMPS_POWERGOOD;
HAL_GPIO_Init(PORT_SMPS, &GPIO_InitStruct);
/* ------------------------------------------------------------------------ */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pin = PIN_SMPS_ENABLE | PIN_SMPS_SWITCH_ENABLE;
HAL_GPIO_Init(PORT_SMPS, &GPIO_InitStruct);
/* --------- SMPS VOLTAGE RANGE SELECTION ----------------------------------*/
/* ######################################################################## */
/* - > Not applicable to ADP5301ACBZ on MB1319 */
/* ######################################################################## */
/* - > Applicable to ST1PS02D1QTR */
/* Control to be added */
/* ST1PS02D1QTR on MB1312 */
/* if (VoltageRange == ST1PS02D1QTR_VOUT_1_25) */
/* HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_V1, GPIO_PIN_SET); */
/* 1.25V */
/* D0/D1/D2 = H/L/L */
/* else */
/* */
/* ST1PS02D1QTR on MB1312 */
/* ST1PS02D1QTR_VOUT_1_05 */
/* 1.05V */
/* D0/D1/D2 = L/L/L */
/* HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_V1, GPIO_PIN_RESET); */
/* ######################################################################## */
return SMPS_OK;
}
/**
* @brief Enable the external SMPS component
* @param Delay: delay in ms after enable
* @param Power_Good_Check: Enable Power good check
* @note Power_Good_Check is not used with all external
* SMPS components
* @retval SMPS status
* @arg SMPS_OK: SMPS ENABLE OK
* @arg SMPS_KO: POWER GOOD CHECK FAILS
*/
uint32_t BSP_SMPS_Enable(uint32_t Delay, uint32_t Power_Good_Check)
{
PWR_AND_CLK_SMPS();
HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_ENABLE, GPIO_PIN_SET);
/* Delay upon request */
if (Delay != 0)
{
HAL_Delay(Delay);
}
/* CHECK POWER GOOD or NOT */
if (Power_Good_Check != 0)
{
if (GPIO_PIN_RESET == (HAL_GPIO_ReadPin(PORT_SMPS, PIN_SMPS_POWERGOOD)))
{
/* POWER GOOD KO */
return SMPS_KO;
}
}
/* SMPS ENABLED */
return SMPS_OK;
}
/**
* @brief Disable the external SMPS component
* @note SMPS SWITCH should be disabled first !
* @retval SMPS status
* @arg SMPS_OK: SMPS DISABLE OK - DONE
* @arg SMPS_KO: POWER GOOD CHECK FAILS
*
*/
uint32_t BSP_SMPS_Disable(void)
{
PWR_AND_CLK_SMPS();
/* Check if SMPS SWITCH is disabled */
if (HAL_GPIO_ReadPin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE) != GPIO_PIN_RESET)
{
/* ERROR AS SWITCH SHOULD BE DISABLED */
return SMPS_KO;
}
/* Disable SMPS */
HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_ENABLE, GPIO_PIN_RESET);
/* SMPS DISABLED */
return SMPS_OK;
}
/**
* @brief Enable the external SMPS SWITCH component
* @param Delay: delay in ms before SMPS SWITCH ENABLE
* @param Power_Good_Check: Enable Power good check
* @note Power_Good_Check is not used with all boards
* @retval SMPS status
* @arg SMPS_OK: SMPS ENABLE OK
* @arg SMPS_KO: POWER GOOD CHECK FAILS
*/
uint32_t BSP_SMPS_Supply_Enable(uint32_t Delay, uint32_t Power_Good_Check)
{
PWR_AND_CLK_SMPS();
if (Delay != 0)
{
HAL_Delay(Delay);
}
/* CHECK POWER GOOD or NOT */
if (Power_Good_Check != 0)
{
if (GPIO_PIN_RESET == (HAL_GPIO_ReadPin(PORT_SMPS, PIN_SMPS_POWERGOOD)))
{
/* POWER GOOD KO */
return SMPS_KO;
}
}
/* SMPS SWITCH ENABLE */
HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE, GPIO_PIN_SET);
return SMPS_OK;
}
/**
* @brief Disable the external SMPS SWITCH component
* @retval SMPS status
* @arg SMPS_OK: SMPS SWITCH DISABLE OK
*/
uint32_t BSP_SMPS_Supply_Disable(void)
{
PWR_AND_CLK_SMPS();
/* SMPS SWITCH DISABLE */
HAL_GPIO_WritePin(PORT_SMPS, PIN_SMPS_SWITCH_ENABLE, GPIO_PIN_RESET);
return SMPS_OK;
}
#endif /* ADP5301ACBZ */
#endif /* USE_STM32L4XX_NUCLEO_64_SMPS */
/**
* @}
*/
/**
* @}
*/
/** @defgroup STM32L4XX_NUCLEO_Private_Functions Private Functions
* @{
*/
#ifdef HAL_SPI_MODULE_ENABLED
/******************************************************************************
BUS OPERATIONS
*******************************************************************************/
/**
* @brief Initialize SPI MSP.
* @retval None
*/
static void SPIx_MspInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*** Configure the GPIOs ***/
/* Enable GPIO clock */
NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
/* Configure SPI SCK */
GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF;
HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
/* Configure SPI MISO and MOSI */
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;
HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
/*** Configure the SPI peripheral ***/
/* Enable SPI clock */
NUCLEO_SPIx_CLK_ENABLE();
}
/**
* @brief Initialize SPI HAL.
* @retval None
*/
static void SPIx_Init(void)
{
if (HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
{
/* SPI Config */
hnucleo_Spi.Instance = NUCLEO_SPIx;
/* SPI baudrate is set to 8 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 32/4 = 8 MHz)
to verify these constraints:
- ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
Since the provided driver doesn't use read capability from LCD, only constraint
on write baudrate is considered.
- SD card SPI interface max baudrate is 25MHz for write/read
- PCLK2 max frequency is 32 MHz
*/
hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
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_DISABLE;
hnucleo_Spi.Init.CRCPolynomial = 7;
hnucleo_Spi.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
hnucleo_Spi.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLE;
hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
SPIx_MspInit();
HAL_SPI_Init(&hnucleo_Spi);
}
}
/**
* @brief SPI Write byte(s) to device
* @param DataIn: Pointer to data buffer to write
* @param DataOut: Pointer to data buffer for read data
* @param DataLength: number of bytes to write
* @retval None
*/
static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t *) DataIn, DataOut, DataLength, hnucleo_SpixTimeout);
/* Check the communication status */
if (status != HAL_OK)
{
/* Execute user timeout callback */
SPIx_Error();
}
}
/**
* @brief SPI Write a byte to device
* @param Value: value to be written
* @retval None
*/
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, hnucleo_SpixTimeout);
/* Check the communication status */
if (status != HAL_OK)
{
/* Execute user timeout callback */
SPIx_Error();
}
}
/**
* @brief SPI error treatment function
* @retval None
*/
static void SPIx_Error(void)
{
/* De-initialize the SPI communication BUS */
HAL_SPI_DeInit(&hnucleo_Spi);
/* Re-Initiaize the SPI communication BUS */
SPIx_Init();
}
/******************************************************************************
LINK OPERATIONS
*******************************************************************************/
/********************************* LINK SD ************************************/
/**
* @brief Initialize the SD Card and put it into StandBy State (Ready for
* data transfer).
* @retval None
*/
void SD_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
uint8_t counter = 0;
/* SD_CS_GPIO Periph clock enable */
SD_CS_GPIO_CLK_ENABLE();
/* Configure SD_CS_PIN pin: SD Card CS pin */
GPIO_InitStruct.Pin = SD_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
/* Configure LCD_CS_PIN pin: LCD Card CS pin */
GPIO_InitStruct.Pin = LCD_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(LCD_CS_GPIO_PORT, &GPIO_InitStruct);
LCD_CS_HIGH();
/*------------Put SD in SPI mode--------------*/
/* SD SPI Config */
SPIx_Init();
/* SD chip select high */
SD_CS_HIGH();
/* Send dummy byte 0xFF, 10 times with CS high */
/* Rise CS and MOSI for 80 clocks cycles */
for (counter = 0; counter <= 9; counter++)
{
/* Send dummy byte 0xFF */
SD_IO_WriteByte(SD_DUMMY_BYTE);
}
}
/**
* @brief Set SD interface Chip Select state
* @param val: 0 (low) or 1 (high) state
* @retval None
*/
void SD_IO_CSState(uint8_t val)
{
if (val == 1)
{
SD_CS_HIGH();
}
else
{
SD_CS_LOW();
}
}
/**
* @brief Write byte(s) on the SD
* @param DataIn: Pointer to data buffer to write
* @param DataOut: Pointer to data buffer for read data
* @param DataLength: number of bytes to write
* @retval None
*/
void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
{
/* Send the byte */
SPIx_WriteReadData(DataIn, DataOut, DataLength);
}
/**
* @brief Write a byte on the SD.
* @param Data: byte to send.
* @retval Data written
*/
uint8_t SD_IO_WriteByte(uint8_t Data)
{
uint8_t tmp;
/* Send the byte */
SPIx_WriteReadData(&Data, &tmp, 1);
return tmp;
}
/********************************* LINK LCD ***********************************/
/**
* @brief Initialize the LCD
* @retval None
*/
void LCD_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
LCD_CS_GPIO_CLK_ENABLE();
LCD_DC_GPIO_CLK_ENABLE();
/* Configure LCD_CS_PIN pin: LCD Card CS pin */
GPIO_InitStruct.Pin = LCD_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(LCD_CS_GPIO_PORT, &GPIO_InitStruct);
/* Configure LCD_DC_PIN pin: LCD Card DC pin */
GPIO_InitStruct.Pin = LCD_DC_PIN;
HAL_GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStruct);
/* LCD chip select high */
LCD_CS_HIGH();
/* LCD SPI Config */
SPIx_Init();
}
/**
* @brief Write command to select the LCD register.
* @param LCDReg: Address of the selected register.
* @retval None
*/
void LCD_IO_WriteReg(uint8_t LCDReg)
{
/* Reset LCD control line CS */
LCD_CS_LOW();
/* Set LCD data/command line DC to Low */
LCD_DC_LOW();
/* Send Command */
SPIx_Write(LCDReg);
/* Deselect : Chip Select high */
LCD_CS_HIGH();
}
/**
* @brief Write register value.
* @param pData Pointer on the register value
* @param Size Size of byte to transmit to the register
* @retval None
*/
void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
{
uint32_t counter = 0;
__IO uint32_t data = 0;
/* Reset LCD control line CS */
LCD_CS_LOW();
/* Set LCD data/command line DC to High */
LCD_DC_HIGH();
if (Size == 1)
{
/* Only 1 byte to be sent to LCD - general interface can be used */
/* Send Data */
SPIx_Write(*pData);
}
else
{
/* Several data should be sent in a raw */
/* Direct SPI accesses for optimization */
for (counter = Size; counter != 0; counter--)
{
while (((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
{
}
/* Need to invert bytes for LCD*/
*((__IO uint8_t *)&hnucleo_Spi.Instance->DR) = *(pData + 1);
while (((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
{
}
*((__IO uint8_t *)&hnucleo_Spi.Instance->DR) = *pData;
counter--;
pData += 2;
}
/* Wait until the bus is ready before releasing Chip select */
while (((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET)
{
}
}
/* Empty the Rx fifo */
data = *(&hnucleo_Spi.Instance->DR);
UNUSED(data); /* Remove GNU warning */
/* Deselect : Chip Select high */
LCD_CS_HIGH();
}