-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32l552e_eval.c
1542 lines (1397 loc) · 39.3 KB
/
stm32l552e_eval.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 stm32l552e_eval.c
* @author MCD Application Team
* @brief This file provides set of firmware functions to manage:
* - LEDs, push-buttons, potentiometer, joystick and COM ports available
* on STM32L552E-EV board (MB1372) from STMicroelectronics
******************************************************************************
* @attention
*
* Copyright (c) 2019 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 "stm32l552e_eval.h"
#if (USE_BSP_COM_FEATURE == 1)
#if (USE_COM_LOG == 1)
#include <stdio.h>
#endif
#endif
/** @addtogroup BSP
* @{
*/
/** @defgroup STM32L552E-EV STM32L552E-EV
* @{
*/
/** @defgroup STM32L552E-EV_COMMON STM32L552E-EV COMMON
* @brief This file provides set of firmware functions to manage Leds and push-buttons
* potentiometer and COM ports available on STM32L552E-EV board from STMicroelectronics.
* @{
*/
/** @defgroup STM32L552E-EV_COMMON_Private_Defines STM32L552E-EV COMMON Private Defines
* @{
*/
#if (USE_BSP_COM_FEATURE == 1)
#if (USE_COM_LOG == 1)
#define COM_POLL_TIMEOUT 1000
#endif
#endif
#if (USE_BSP_POT_FEATURE == 1)
#define POT_ADC_POLL_TIMEOUT 1000
#endif
/**
* @}
*/
/** @defgroup STM32L552E-EV_COMMON_Private_Macros STM32L552E-EV COMMON Private Macros
* @{
*/
#if (USE_BSP_POT_FEATURE == 1)
#define POT_CONVERT2PERC(x) (((int32_t)(x) * 100) / 0xFFF)
#endif
/**
* @}
*/
/** @defgroup STM32L552E-EV_COMMON_Private_TypesDefinitions STM32L552E-EV COMMON Private Types Definitions
* @{
*/
typedef void (* BSP_EXTI_LineCallback)(void);
/**
* @}
*/
/** @defgroup STM32L552E-EV_COMMON_Private_FunctionPrototypes STM32L552E-EV COMMON Private Function Prototypes
* @{
*/
static void BUTTON_WAKEUP_EXTI_Callback(void);
static void BUTTON_TAMPER_EXTI_Callback(void);
#if (USE_BSP_IO_CLASS == 1)
static void JOY1_EXTI_Callback(void);
#endif
#if (USE_BSP_COM_FEATURE == 1)
static void UART_MspInit(UART_HandleTypeDef *huart);
static void UART_MspDeInit(UART_HandleTypeDef *huart);
#endif
#if (USE_BSP_POT_FEATURE == 1)
static void ADC_MspInit(ADC_HandleTypeDef *hadc);
static void ADC_MspDeInit(ADC_HandleTypeDef *hadc);
#endif
/**
* @}
*/
/** @addtogroup STM32L552E-EV_COMMON_Exported_Variables
* @{
*/
#if (USE_BSP_COM_FEATURE == 1)
UART_HandleTypeDef hcom_uart[COMn];
#endif
#if (USE_BSP_POT_FEATURE == 1)
ADC_HandleTypeDef hpot_adc[POTn];
#endif
/**
* @}
*/
/** @defgroup STM32L552E-EV_COMMON_Private_Variables STM32L552E-EV COMMON Private Variables
* @{
*/
#if (USE_BSP_IO_CLASS == 1)
static uint16_t LED_PIN[LEDn] = {LED4_PIN, LED5_PIN, LED6_PIN, LED7_PIN};
static GPIO_TypeDef *LED_PORT[LEDn] = {LED4_GPIO_PORT, LED5_GPIO_PORT, 0, 0};
#else
static uint16_t LED_PIN[LEDn] = {LED4_PIN, LED5_PIN};
static GPIO_TypeDef *LED_PORT[LEDn] = {LED4_GPIO_PORT, LED5_GPIO_PORT};
#endif
static uint16_t BUTTON_PIN[BUTTONn] = {BUTTON_WAKEUP_PIN, BUTTON_TAMPER_PIN};
static GPIO_TypeDef *BUTTON_PORT[BUTTONn] = {BUTTON_WAKEUP_GPIO_PORT, BUTTON_TAMPER_GPIO_PORT};
static IRQn_Type BUTTON_IRQn[BUTTONn] = {BUTTON_WAKEUP_EXTI_IRQn, BUTTON_TAMPER_EXTI_IRQn};
static EXTI_HandleTypeDef hpb_exti[BUTTONn];
#if (USE_BSP_COM_FEATURE == 1)
static USART_TypeDef *COM_UART[COMn] = {COM1_UART, COM2_UART};
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
static uint32_t IsComMspCbValid[COMn] = {0U, 0U};
#endif
#if (USE_COM_LOG == 1)
static COM_TypeDef COM_ActiveLogPort = COM1;
#endif
#endif /* (USE_BSP_COM_FEATURE == 1) */
#if (USE_BSP_POT_FEATURE == 1)
static ADC_TypeDef *POT_ADC[POTn] = {POT1_ADC};
#if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
static uint32_t IsPotMspCbValid[POTn] = {0U};
#endif
#endif /* (USE_BSP_POT_FEATURE == 1) */
#if (USE_BSP_IO_CLASS == 1)
static uint32_t CurrentJoyPins[JOYn] = {0};
static uint32_t JOY_SEL_PIN[JOYn] = {JOY1_SEL_PIN};
static uint32_t JOY_DOWN_PIN[JOYn] = {JOY1_DOWN_PIN};
static uint32_t JOY_LEFT_PIN[JOYn] = {JOY1_LEFT_PIN};
static uint32_t JOY_RIGHT_PIN[JOYn] = {JOY1_RIGHT_PIN};
static uint32_t JOY_UP_PIN[JOYn] = {JOY1_UP_PIN};
static uint32_t JOY_ALL_PIN[JOYn] = {JOY1_ALL_PIN};
static EXTI_HandleTypeDef hjoy_exti[JOYn];
#endif
/**
* @}
*/
/** @addtogroup STM32L552E-EV_COMMON_Exported_Functions
* @{
*/
/**
* @brief This method returns the STM32L552E EVAL BSP Driver revision
* @retval version : 0xXYZR (8bits for each decimal, R for RC)
*/
int32_t BSP_GetVersion(void)
{
return ((int32_t)STM32L552E_EVAL_BSP_VERSION);
}
/**
* @brief This method returns the board name
* @retval pointer to the board name string
*/
const uint8_t* BSP_GetBoardName(void)
{
return (const uint8_t*)STM32L552E_EVAL_BSP_BOARD_NAME;
}
/**
* @brief This method returns the board ID
* @retval pointer to the board ID string
*/
const uint8_t* BSP_GetBoardID(void)
{
return (const uint8_t*)STM32L552E_EVAL_BSP_BOARD_ID;
}
/** @addtogroup STM32L552E-EV_COMMON_LED_Functions
* @{
*/
/**
* @brief Configure LED GPIO.
* @param Led Specifies the Led to be configured.
* This parameter can be one of following parameters:
* @arg LED4
* @arg LED5
* @arg LED6 only if IO expander is used
* @arg LED7 only if IO expander is used
* @retval BSP error code
*/
int32_t BSP_LED_Init(Led_TypeDef Led)
{
int32_t status = BSP_ERROR_NONE;
GPIO_InitTypeDef GPIO_Init;
#if (USE_BSP_IO_CLASS == 1)
BSP_IO_Init_t IO_Init;
#endif
if ((Led == LED4) || (Led == LED5))
{
/* Enable the GPIO_LED Clock */
if (Led == LED4)
{
LED4_GPIO_CLK_ENABLE();
}
else /* Led = LED5 */
{
LED5_GPIO_CLK_ENABLE();
}
/* configure the GPIO_LED pin */
GPIO_Init.Pin = LED_PIN[Led];
GPIO_Init.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Init.Pull = GPIO_PULLUP;
GPIO_Init.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(LED_PORT[Led], &GPIO_Init);
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
}
#if (USE_BSP_IO_CLASS == 1)
else /* Led = LED6 or Led = LED7 */
{
/* Initialize the BSP IO driver and configure the Led pin */
IO_Init.Pin = LED_PIN[Led];
IO_Init.Mode = IO_MODE_OUTPUT_PP;
IO_Init.Pull = IO_PULLUP;
status = BSP_IO_Init(0, &IO_Init);
if (status == BSP_ERROR_NONE)
{
status = BSP_IO_WritePin(0, LED_PIN[Led], IO_PIN_SET);
}
}
#endif /* USE_BSP_IO_CLASS */
return status;
}
/**
* @brief DeInitialize LEDs.
* @param Led LED to be de-init.
* This parameter can be one of the following values:
* @arg LED4
* @arg LED5
* @arg LED6 only if IO expander is used
* @arg LED7 only if IO expander is used
* @note BSP_LED_DeInit() does not disable the GPIO clock
* @retval BSP error code
*/
int32_t BSP_LED_DeInit(Led_TypeDef Led)
{
int32_t status = BSP_ERROR_NONE;
GPIO_InitTypeDef GPIO_Init;
#if (USE_BSP_IO_CLASS == 1)
BSP_IO_Init_t IO_Init;
#endif
if ((Led == LED4) || (Led == LED5))
{
/* DeInit the GPIO_LED pin */
GPIO_Init.Pin = LED_PIN[Led];
/* Turn off LED */
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
HAL_GPIO_DeInit(LED_PORT[Led], GPIO_Init.Pin);
}
#if (USE_BSP_IO_CLASS == 1)
else /* Led = LED6 or Led = LED7 */
{
/* Just turn off the LED and reset IO pin */
status = BSP_IO_WritePin(0, LED_PIN[Led], IO_PIN_SET);
if (status == BSP_ERROR_NONE)
{
IO_Init.Pin = LED_PIN[Led];
IO_Init.Mode = IO_MODE_OFF;
IO_Init.Pull = IO_NOPULL;
status = BSP_IO_Init(0, &IO_Init);
}
}
#endif /* USE_BSP_IO_CLASS */
return status;
}
/**
* @brief Turn selected LED On.
* @param Led Specifies the Led to be set on.
* This parameter can be one of following parameters:
* @arg LED4
* @arg LED5
* @arg LED6 only if IO expander is used
* @arg LED7 only if IO expander is used
* @retval BSP error code
*/
int32_t BSP_LED_On(Led_TypeDef Led)
{
int32_t status = BSP_ERROR_NONE;
if ((Led == LED4) || (Led == LED5))
{
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
}
#if (USE_BSP_IO_CLASS == 1)
else
{
status = BSP_IO_WritePin(0, LED_PIN[Led], IO_PIN_RESET);
}
#endif /* USE_BSP_IO_CLASS */
return status;
}
/**
* @brief Turn selected LED Off.
* @param Led Specifies the Led to be set off.
* This parameter can be one of following parameters:
* @arg LED4
* @arg LED5
* @arg LED6 only if IO expander is used
* @arg LED7 only if IO expander is used
* @retval BSP error code
*/
int32_t BSP_LED_Off(Led_TypeDef Led)
{
int32_t status = BSP_ERROR_NONE;
if ((Led == LED4) || (Led == LED5))
{
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
}
#if (USE_BSP_IO_CLASS == 1)
else
{
status = BSP_IO_WritePin(0, LED_PIN[Led], IO_PIN_SET);
}
#endif /* USE_BSP_IO_CLASS */
return status;
}
/**
* @brief Toggle the selected LED.
* @param Led Specifies the Led to be toggled.
* This parameter can be one of following parameters:
* @arg LED4
* @arg LED5
* @arg LED6 only if IO expander is used
* @arg LED7 only if IO expander is used
* @retval BSP error code
*/
int32_t BSP_LED_Toggle(Led_TypeDef Led)
{
int32_t status = BSP_ERROR_NONE;
if ((Led == LED4) || (Led == LED5))
{
HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]);
}
#if (USE_BSP_IO_CLASS == 1)
else
{
status = BSP_IO_TogglePin(0, LED_PIN[Led]);
}
#endif /* USE_BSP_IO_CLASS */
return status;
}
/**
* @brief Get the state of selected LED.
* @param Led Specifies the Led.
* This parameter can be one of following parameters:
* @arg LED4
* @arg LED5
* @arg LED6 only if IO expander is used
* @arg LED7 only if IO expander is used
* @retval 0 means off, 1 means on, negative value means error.
*/
int32_t BSP_LED_GetState(Led_TypeDef Led)
{
int32_t status;
#if (USE_BSP_IO_CLASS == 1)
int32_t tmp;
#endif
#if (USE_BSP_IO_CLASS == 1)
if ((Led == LED4) || (Led == LED5))
{
#endif
status = (HAL_GPIO_ReadPin(LED_PORT[Led], LED_PIN[Led]) == GPIO_PIN_RESET) ? 1 : 0;
#if (USE_BSP_IO_CLASS == 1)
}
else
{
tmp = BSP_IO_ReadPin(0, LED_PIN[Led]);
if (tmp < 0)
{
/* Error ocuured */
status = tmp;
}
else
{
status = ((uint32_t) tmp == IO_PIN_RESET) ? 1 : 0;
}
}
#endif /* USE_BSP_IO_CLASS */
return status;
}
/**
* @}
*/
/** @addtogroup STM32L552E-EV_COMMON_BUTTON_Functions
* @{
*/
/**
* @brief Configure Button GPIO and EXTI Line.
* @param Button Specifies the Button to be configured.
* This parameter should be:
* @arg BUTTON_WAKEUP
* @arg BUTTON_TAMPER
* @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 BSP error code
*/
int32_t BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
int32_t status = BSP_ERROR_NONE;
GPIO_InitTypeDef GPIO_Init;
uint32_t BSP_BUTTON_IT_PRIO[BUTTONn] = {BSP_BUTTON_WAKEUP_IT_PRIORITY, BSP_BUTTON_TAMPER_IT_PRIORITY};
uint32_t BUTTON_EXTI_LINE[BUTTONn] = {BUTTON_WAKEUP_EXTI_LINE, BUTTON_TAMPER_EXTI_LINE};
BSP_EXTI_LineCallback ButtonCallback[BUTTONn] = {BUTTON_WAKEUP_EXTI_Callback, BUTTON_TAMPER_EXTI_Callback};
/* Enable the BUTTON clock */
if (Button == BUTTON_WAKEUP)
{
BUTTON_WAKEUP_GPIO_CLK_ENABLE();
}
else /* BUTTON_TAMPER */
{
BUTTON_TAMPER_GPIO_CLK_ENABLE();
}
GPIO_Init.Pin = BUTTON_PIN[Button];
GPIO_Init.Pull = GPIO_NOPULL;
GPIO_Init.Speed = GPIO_SPEED_FREQ_HIGH;
if (ButtonMode == BUTTON_MODE_GPIO)
{
/* Configure Button pin as input */
GPIO_Init.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_Init);
}
if (ButtonMode == BUTTON_MODE_EXTI)
{
/* Configure Button pin as input with External interrupt */
GPIO_Init.Mode = GPIO_MODE_IT_RISING;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_Init);
if (HAL_EXTI_GetHandle(&hpb_exti[Button], BUTTON_EXTI_LINE[Button]) == HAL_OK)
{
if (HAL_EXTI_RegisterCallback(&hpb_exti[Button], HAL_EXTI_RISING_CB_ID, ButtonCallback[Button]) == HAL_OK)
{
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority(BUTTON_IRQn[Button], BSP_BUTTON_IT_PRIO[Button], 0x00);
HAL_NVIC_EnableIRQ(BUTTON_IRQn[Button]);
}
else
{
status = BSP_ERROR_PERIPH_FAILURE;
}
}
else
{
status = BSP_ERROR_PERIPH_FAILURE;
}
}
return status;
}
/**
* @brief DeInitialize Push Button.
* @param Button Button to be configured
* This parameter should be:
* @arg BUTTON_WAKEUP
* @arg BUTTON_TAMPER
* @note BSP_PB_DeInit() does not disable the GPIO clock
* @retval BSP error code
*/
int32_t BSP_PB_DeInit(Button_TypeDef Button)
{
GPIO_InitTypeDef GPIO_Init;
GPIO_Init.Pin = BUTTON_PIN[Button];
HAL_NVIC_DisableIRQ(BUTTON_IRQn[Button]);
HAL_GPIO_DeInit(BUTTON_PORT[Button], GPIO_Init.Pin);
return BSP_ERROR_NONE;
}
/**
* @brief Return the selected Button state.
* @param Button Specifies the Button to be checked.
* This parameter should be:
* @arg BUTTON_WAKEUP
* @arg BUTTON_TAMPER
* @retval 0 means released, 1 means pressed.
*/
int32_t BSP_PB_GetState(Button_TypeDef Button)
{
int32_t status;
status = (HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]) == GPIO_PIN_SET) ? 1 : 0;
return status;
}
/**
* @brief BSP Push Button callback
* @param Button Specifies the pin connected EXTI line
* @retval None.
*/
__weak void BSP_PB_Callback(Button_TypeDef Button)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(Button);
/* This function should be implemented by the user application.
It is called into this driver when an event on Button is triggered. */
}
/**
* @brief BSP PB interrupt handler.
* @param Button Button where interrupt occurs.
* This parameter should be:
* @arg BUTTON_WAKEUP
* @arg BUTTON_TAMPER
* @retval None.
*/
void BSP_PB_IRQHandler(Button_TypeDef Button)
{
HAL_EXTI_IRQHandler(&hpb_exti[Button]);
}
/**
* @}
*/
#if (USE_BSP_COM_FEATURE == 1)
/** @addtogroup STM32L552E-EV_COMMON_COM_Functions
* @{
*/
/**
* @brief Configure COM port.
* @param COM COM port to be configured.
* This parameter can be COM1 or COM2
* @param COM_Init Pointer to a COM_InitTypeDef structure that contains the
* configuration information for the specified COM peripheral.
* @retval BSP error code
*/
int32_t BSP_COM_Init(COM_TypeDef COM, COM_InitTypeDef *COM_Init)
{
int32_t status = BSP_ERROR_NONE;
if (COM_Init == NULL)
{
status = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Initialize COM instance */
hcom_uart[COM].Instance = COM_UART[COM];
#if (USE_HAL_UART_REGISTER_CALLBACKS == 0)
UART_MspInit(&hcom_uart[COM]);
#else
if (IsComMspCbValid[COM] == 0U)
{
/* Register the MSP callbacks */
if (BSP_COM_RegisterDefaultMspCallbacks(COM) != BSP_ERROR_NONE)
{
status = BSP_ERROR_MSP_FAILURE;
}
}
#endif
if (status == BSP_ERROR_NONE)
{
if (COM == COM1)
{
if (MX_USART3_Init(&hcom_uart[COM], COM_Init) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
}
else /* COM = COM2 */
{
if (MX_LPUART1_Init(&hcom_uart[COM], COM_Init) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
}
}
}
return status;
}
/**
* @brief DeInitialize COM port.
* @param COM COM port to be de_init.
* This parameter can be COM1 or COM2
* @retval BSP error code
*/
int32_t BSP_COM_DeInit(COM_TypeDef COM)
{
int32_t status = BSP_ERROR_NONE;
/* COM de-init */
hcom_uart[COM].Instance = COM_UART[COM];
if (HAL_UART_DeInit(&hcom_uart[COM]) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_UART_REGISTER_CALLBACKS == 0)
else
{
UART_MspDeInit(&hcom_uart[COM]);
}
#endif
return status;
}
#if (USE_COM_LOG == 1)
/**
* @brief Select the active COM port.
* @param COM COM port to be activated.
* This parameter can be COM1 or COM2
* @retval BSP error code
*/
int32_t BSP_COM_SelectLogPort(COM_TypeDef COM)
{
if (COM_ActiveLogPort != COM)
{
COM_ActiveLogPort = COM;
}
return BSP_ERROR_NONE;
}
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
int fputc(int ch, __attribute__((unused))FILE *f)
#elif __GNUC__
int __io_putchar(int ch)
#endif
{
(void) HAL_UART_Transmit(&hcom_uart[COM_ActiveLogPort], (uint8_t *) &ch, 1, COM_POLL_TIMEOUT);
return ch;
}
#endif /* (USE_COM_LOG == 1) */
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
/**
* @brief Register default COM msp callbacks.
* @param COM COM port.
* This parameter can be COM1 or COM2
* @retval BSP status.
*/
int32_t BSP_COM_RegisterDefaultMspCallbacks(COM_TypeDef COM)
{
int32_t status = BSP_ERROR_NONE;
/* Register MspInit/MspDeInit callbacks */
if (HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPINIT_CB_ID, UART_MspInit) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPDEINIT_CB_ID, UART_MspDeInit) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsComMspCbValid[COM] = 1U;
}
/* Return BSP status */
return status;
}
/**
* @brief Register BSP COM msp callbacks.
* @param COM COM port.
* This parameter can be COM1 or COM2
* @param CallBacks Pointer to MspInit/MspDeInit callback functions.
* @retval BSP status
*/
int32_t BSP_COM_RegisterMspCallbacks(COM_TypeDef COM, BSP_COM_Cb_t *CallBacks)
{
int32_t status = BSP_ERROR_NONE;
if (CallBacks == NULL)
{
status = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit callbacks */
if (HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPINIT_CB_ID, CallBacks->pMspInitCb) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_UART_RegisterCallback(&hcom_uart[COM], HAL_UART_MSPDEINIT_CB_ID, CallBacks->pMspDeInitCb) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsComMspCbValid[COM] = 1U;
}
}
/* Return BSP status */
return status;
}
#endif /* (USE_HAL_UART_REGISTER_CALLBACKS == 1) */
/**
* @brief Initialize LPUART1.
* @param huart UART handle.
* @param MXInit UART initialization structure.
* @retval HAL status.
*/
__weak HAL_StatusTypeDef MX_LPUART1_Init(UART_HandleTypeDef* huart, MX_UART_InitTypeDef *MXInit)
{
/* UART configuration */
huart->Init.BaudRate = MXInit->BaudRate;
huart->Init.WordLength = (uint32_t) MXInit->WordLength;
huart->Init.StopBits = (uint32_t) MXInit->StopBits;
huart->Init.Parity = (uint32_t) MXInit->Parity;
huart->Init.Mode = UART_MODE_TX_RX;
huart->Init.HwFlowCtl = (uint32_t) MXInit->HwFlowCtl;
huart->Init.OverSampling = UART_OVERSAMPLING_8;
huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart->Init.ClockPrescaler = UART_PRESCALER_DIV1;
return HAL_UART_Init(huart);
}
/**
* @brief Initialize USART3.
* @param huart UART handle.
* @param MXInit UART initialization structure.
* @retval HAL status.
*/
__weak HAL_StatusTypeDef MX_USART3_Init(UART_HandleTypeDef* huart, MX_UART_InitTypeDef *MXInit)
{
/* UART configuration */
huart->Init.BaudRate = MXInit->BaudRate;
huart->Init.WordLength = (uint32_t) MXInit->WordLength;
huart->Init.StopBits = (uint32_t) MXInit->StopBits;
huart->Init.Parity = (uint32_t) MXInit->Parity;
huart->Init.Mode = UART_MODE_TX_RX;
huart->Init.HwFlowCtl = (uint32_t) MXInit->HwFlowCtl;
huart->Init.OverSampling = UART_OVERSAMPLING_8;
huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart->Init.ClockPrescaler = UART_PRESCALER_DIV1;
return HAL_UART_Init(huart);
}
/**
* @}
*/
#endif /* (USE_BSP_COM_FEATURE == 1) */
#if (USE_BSP_POT_FEATURE == 1)
/** @addtogroup STM32L552E-EV_COMMON_POT_Functions
* @{
*/
/**
* @brief Configure Potentiometer.
* @param POT Potentiometer to be configured.
* This parameter can be POT1
* @retval BSP error code
*/
int32_t BSP_POT_Init(POT_TypeDef POT)
{
int32_t status = BSP_ERROR_NONE;
/* Initialize POT instance */
hpot_adc[POT].Instance = POT_ADC[POT];
#if (USE_HAL_ADC_REGISTER_CALLBACKS == 0)
ADC_MspInit(&hpot_adc[POT]);
#else
if (IsPotMspCbValid[POT] == 0U)
{
/* Register the MSP callbacks */
if (BSP_POT_RegisterDefaultMspCallbacks(POT) != BSP_ERROR_NONE)
{
status = BSP_ERROR_MSP_FAILURE;
}
}
#endif
if (status == BSP_ERROR_NONE)
{
if (MX_ADC1_Init(&hpot_adc[POT]) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
}
return status;
}
/**
* @brief DeInitialize Potentiometer.
* @param POT Potentiometer to be de_init.
* This parameter can be POT1
* @retval BSP error code
*/
int32_t BSP_POT_DeInit(POT_TypeDef POT)
{
int32_t status = BSP_ERROR_NONE;
/* POT de-init */
hpot_adc[POT].Instance = POT_ADC[POT];
if (HAL_ADC_DeInit(&hpot_adc[POT]) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_ADC_REGISTER_CALLBACKS == 0)
else
{
ADC_MspDeInit(&hpot_adc[POT]);
}
#endif
return status;
}
/**
* @brief Get Potentiometer level.
* @param POT Potentiometer.
* This parameter can be POT1
* @retval Potentiometer level(0..100%), negative value means error.
*/
int32_t BSP_POT_GetLevel(POT_TypeDef POT)
{
int32_t retval = BSP_ERROR_PERIPH_FAILURE;
uint32_t AdcValue;
if (HAL_ADC_Start(&hpot_adc[POT]) == HAL_OK)
{
if (HAL_ADC_PollForConversion(&hpot_adc[POT], POT_ADC_POLL_TIMEOUT) == HAL_OK)
{
AdcValue = HAL_ADC_GetValue(&hpot_adc[POT]);
retval = POT_CONVERT2PERC(AdcValue);
}
}
return retval;
}
#if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
/**
* @brief Register default POT msp callbacks.
* @param POT Potentiometer.
* This parameter can be POT1
* @retval BSP status.
*/
int32_t BSP_POT_RegisterDefaultMspCallbacks(POT_TypeDef POT)
{
int32_t status = BSP_ERROR_NONE;
/* Register MspInit/MspDeInit callbacks */
if (HAL_ADC_RegisterCallback(&hpot_adc[POT], HAL_ADC_MSPINIT_CB_ID, ADC_MspInit) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_ADC_RegisterCallback(&hpot_adc[POT], HAL_ADC_MSPDEINIT_CB_ID, ADC_MspDeInit) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsPotMspCbValid[POT] = 1U;
}
/* Return BSP status */
return status;
}
/**
* @brief Register BSP POT msp callbacks.
* @param POT Potentiometer.
* This parameter can be POT1
* @param CallBacks Pointer to MspInit/MspDeInit callback functions.
* @retval BSP status
*/
int32_t BSP_POT_RegisterMspCallbacks(POT_TypeDef POT, BSP_POT_Cb_t *CallBacks)
{
int32_t status = BSP_ERROR_NONE;
if (CallBacks == NULL)
{
status = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit callbacks */
if (HAL_ADC_RegisterCallback(&hpot_adc[POT], HAL_ADC_MSPINIT_CB_ID, CallBacks->pMspInitCb) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_ADC_RegisterCallback(&hpot_adc[POT], HAL_ADC_MSPDEINIT_CB_ID, CallBacks->pMspDeInitCb) != HAL_OK)
{
status = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsPotMspCbValid[POT] = 1U;
}
}
/* Return BSP status */
return status;
}
#endif /* (USE_HAL_ADC_REGISTER_CALLBACKS == 1) */
/**
* @brief Initialize ADC1.
* @param hadc ADC handle.
* @retval HAL status.
*/
__weak HAL_StatusTypeDef MX_ADC1_Init(ADC_HandleTypeDef* hadc)
{
HAL_StatusTypeDef status = HAL_ERROR;
ADC_ChannelConfTypeDef CHANNEL_Config;
uint32_t POT_ADC_CHANNEL[POTn] = {POT1_ADC_CHANNEL};
/* ADC configuration */
hadc->Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc->Init.Resolution = ADC_RESOLUTION_12B;
hadc->Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc->Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc->Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc->Init.LowPowerAutoWait = DISABLE;
hadc->Init.ContinuousConvMode = DISABLE;
hadc->Init.NbrOfConversion = 1;
hadc->Init.DiscontinuousConvMode = DISABLE;
hadc->Init.NbrOfDiscConversion = 1;
hadc->Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc->Init.DMAContinuousRequests = DISABLE;
hadc->Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
hadc->Init.OversamplingMode = DISABLE;
hadc->Init.DFSDMConfig = ADC_DFSDM_MODE_DISABLE;
if (HAL_ADC_Init(hadc) == HAL_OK)
{
/* ADC channel configuration */
CHANNEL_Config.Channel = POT_ADC_CHANNEL[POT1];
CHANNEL_Config.Rank = ADC_REGULAR_RANK_1;
CHANNEL_Config.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;
CHANNEL_Config.SingleDiff = ADC_SINGLE_ENDED;
CHANNEL_Config.OffsetNumber = ADC_OFFSET_NONE;
CHANNEL_Config.Offset = 0;
if (HAL_ADC_ConfigChannel(hadc, &CHANNEL_Config) == HAL_OK)
{
if (HAL_ADCEx_Calibration_Start(hadc, ADC_SINGLE_ENDED) == HAL_OK)
{
status = HAL_OK;
}
}
}
return status;
}