/
stm32l4xx_hal_msp.c
157 lines (113 loc) · 3.96 KB
/
stm32l4xx_hal_msp.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
// Copyright 2018 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
#include "main.h"
#include "event.h"
// Initialize global peripheral init
void HAL_MspInit(void) {
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
}
// Initialize all I2C ports
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) {
// Handle I2C1
if (hi2c->Instance==I2C1) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
// The Nucleo-L432KC multiplexes pins using SB16/SB18 in a way
// explained in 6.10 "Solder Bridges" that requires us to
// initialize PA6 and PA5 as input floating. See the manual
// and in particular the schematics for more info:
// https://www.st.com/resource/en/user_manual/dm00231744.pdf
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// I2C1 GPIO Configuration
// PB6 ------> I2C1_SCL
// PB7 ------> I2C1_SDA
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// Enable peripheral clock
__HAL_RCC_I2C1_CLK_ENABLE();
// I2C1 interrupt Init
HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
}
}
// Deinitialize all I2C ports
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c) {
// Handle I2C1
if (hi2c->Instance==I2C1) {
// Peripheral clock disable
__HAL_RCC_I2C1_CLK_DISABLE();
// I2C1 GPIO deconfigure
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
// I2C1 interrupt DeInit
HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
}
}
// Initialize all UART ports
void HAL_UART_MspInit(UART_HandleTypeDef* huart) {
// Handle USART1
if (huart->Instance==USART1) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Peripheral clock
__HAL_RCC_USART1_CLK_ENABLE();
// USART1 GPIO Configuration
// PA9 ------> USART1_TX
// PA10 ------> USART1_RX
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// USART1 interrupt Init
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
}
// Deinitialize all UARTs
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) {
// Handle USART1
if (huart->Instance==USART1) {
// Peripheral clock disable
__HAL_RCC_USART1_CLK_DISABLE();
// GPIO deconfigure
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
// Interrupt DeInit
HAL_NVIC_DisableIRQ(USART1_IRQn);
}
}
// Initialize all low power timers
#ifdef EVENT_TIMER
void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef* hlptim) {
// Handle LPTIM1
if (hlptim->Instance==LPTIM1) {
// Clock
__HAL_RCC_LPTIM1_CLK_ENABLE();
// LPTIM1 interrupt Init
HAL_NVIC_SetPriority(LPTIM1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(LPTIM1_IRQn);
}
}
#endif
// Deinitialize all low power timers
#ifdef EVENT_TIMER
void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef* hlptim) {
// Handle LPTIM1
if (hlptim->Instance==LPTIM1) {
// Peripheral clock
__HAL_RCC_LPTIM1_CLK_DISABLE();
// LPTIM1 interrupt
HAL_NVIC_DisableIRQ(LPTIM1_IRQn);
}
}
#endif