Skip to content

Commit b6efdd5

Browse files
committed
STM32 serial: improve index assignment in serial_init
1 parent e8454ff commit b6efdd5

File tree

1 file changed

+5
-34
lines changed

1 file changed

+5
-34
lines changed

targets/TARGET_STM/serial_api.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ HAL_StatusTypeDef init_uart(serial_t *obj);
4444
void serial_init(serial_t *obj, PinName tx, PinName rx)
4545
{
4646
struct serial_s *obj_s = SERIAL_S(obj);
47-
int IndexNumber = 0;
4847
uint8_t stdio_config = 0;
4948

5049
// Determine the UART to use (UART_1, UART_2, ...)
@@ -64,157 +63,131 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
6463
}
6564
}
6665

67-
// Enable USART clock
66+
// Reset and enable clock
6867
#if defined(USART1_BASE)
6968
if (obj_s->uart == UART_1) {
7069
__HAL_RCC_USART1_FORCE_RESET();
7170
__HAL_RCC_USART1_RELEASE_RESET();
7271
__HAL_RCC_USART1_CLK_ENABLE();
73-
obj_s->index = IndexNumber;
7472
}
75-
IndexNumber++;
7673
#endif
7774

7875
#if defined (USART2_BASE)
7976
if (obj_s->uart == UART_2) {
8077
__HAL_RCC_USART2_FORCE_RESET();
8178
__HAL_RCC_USART2_RELEASE_RESET();
8279
__HAL_RCC_USART2_CLK_ENABLE();
83-
obj_s->index = IndexNumber;
8480
}
85-
IndexNumber++;
8681
#endif
8782

8883
#if defined(USART3_BASE)
8984
if (obj_s->uart == UART_3) {
9085
__HAL_RCC_USART3_FORCE_RESET();
9186
__HAL_RCC_USART3_RELEASE_RESET();
9287
__HAL_RCC_USART3_CLK_ENABLE();
93-
obj_s->index = IndexNumber;
9488
}
95-
IndexNumber++;
9689
#endif
9790

9891
#if defined(UART4_BASE)
9992
if (obj_s->uart == UART_4) {
10093
__HAL_RCC_UART4_FORCE_RESET();
10194
__HAL_RCC_UART4_RELEASE_RESET();
10295
__HAL_RCC_UART4_CLK_ENABLE();
103-
obj_s->index = IndexNumber;
10496
}
105-
IndexNumber++;
10697
#endif
10798

10899
#if defined(USART4_BASE)
109100
if (obj_s->uart == UART_4) {
110101
__HAL_RCC_USART4_FORCE_RESET();
111102
__HAL_RCC_USART4_RELEASE_RESET();
112103
__HAL_RCC_USART4_CLK_ENABLE();
113-
obj_s->index = IndexNumber;
114104
}
115-
IndexNumber++;
116105
#endif
117106

118107
#if defined(UART5_BASE)
119108
if (obj_s->uart == UART_5) {
120109
__HAL_RCC_UART5_FORCE_RESET();
121110
__HAL_RCC_UART5_RELEASE_RESET();
122111
__HAL_RCC_UART5_CLK_ENABLE();
123-
obj_s->index = IndexNumber;
124112
}
125-
IndexNumber++;
126113
#endif
127114

128115
#if defined(USART5_BASE)
129116
if (obj_s->uart == UART_5) {
130117
__HAL_RCC_USART5_FORCE_RESET();
131118
__HAL_RCC_USART5_RELEASE_RESET();
132119
__HAL_RCC_USART5_CLK_ENABLE();
133-
obj_s->index = IndexNumber;
134120
}
135-
IndexNumber++;
136121
#endif
137122

138123
#if defined(USART6_BASE)
139124
if (obj_s->uart == UART_6) {
140125
__HAL_RCC_USART6_FORCE_RESET();
141126
__HAL_RCC_USART6_RELEASE_RESET();
142127
__HAL_RCC_USART6_CLK_ENABLE();
143-
obj_s->index = IndexNumber;
144128
}
145-
IndexNumber++;
146129
#endif
147130

148131
#if defined(UART7_BASE)
149132
if (obj_s->uart == UART_7) {
150133
__HAL_RCC_UART7_FORCE_RESET();
151134
__HAL_RCC_UART7_RELEASE_RESET();
152135
__HAL_RCC_UART7_CLK_ENABLE();
153-
obj_s->index = IndexNumber;
154136
}
155-
IndexNumber++;
156137
#endif
157138

158139
#if defined(USART7_BASE)
159140
if (obj_s->uart == UART_7) {
160141
__HAL_RCC_USART7_FORCE_RESET();
161142
__HAL_RCC_USART7_RELEASE_RESET();
162143
__HAL_RCC_USART7_CLK_ENABLE();
163-
obj_s->index = IndexNumber;
164144
}
165-
IndexNumber++;
166145
#endif
167146

168147
#if defined(UART8_BASE)
169148
if (obj_s->uart == UART_8) {
170149
__HAL_RCC_UART8_FORCE_RESET();
171150
__HAL_RCC_UART8_RELEASE_RESET();
172151
__HAL_RCC_UART8_CLK_ENABLE();
173-
obj_s->index = IndexNumber;
174152
}
175-
IndexNumber++;
176153
#endif
177154

178155
#if defined(USART8_BASE)
179156
if (obj_s->uart == UART_8) {
180157
__HAL_RCC_USART8_FORCE_RESET();
181158
__HAL_RCC_USART8_RELEASE_RESET();
182159
__HAL_RCC_USART8_CLK_ENABLE();
183-
obj_s->index = IndexNumber;
184160
}
185-
IndexNumber++;
186161
#endif
187162

188163
#if defined(UART9_BASE)
189164
if (obj_s->uart == UART_9) {
190165
__HAL_RCC_UART9_FORCE_RESET();
191166
__HAL_RCC_UART9_RELEASE_RESET();
192167
__HAL_RCC_UART9_CLK_ENABLE();
193-
obj_s->index = IndexNumber;
194168
}
195-
IndexNumber++;
196169
#endif
197170

198171
#if defined(UART10_BASE)
199172
if (obj_s->uart == UART_10) {
200173
__HAL_RCC_UART10_FORCE_RESET();
201174
__HAL_RCC_UART10_RELEASE_RESET();
202175
__HAL_RCC_UART10_CLK_ENABLE();
203-
obj_s->index = IndexNumber;
204176
}
205-
IndexNumber++;
206177
#endif
207178

208179
#if defined(LPUART1_BASE)
209180
if (obj_s->uart == LPUART_1) {
210181
__HAL_RCC_LPUART1_FORCE_RESET();
211182
__HAL_RCC_LPUART1_RELEASE_RESET();
212183
__HAL_RCC_LPUART1_CLK_ENABLE();
213-
obj_s->index = IndexNumber;
214184
}
215-
IndexNumber++;
216185
#endif
217186

187+
// Assign serial object index
188+
obj_s->index = get_uart_index(obj_s->uart);
189+
MBED_ASSERT(obj_s->index >= 0);
190+
218191
// Configure UART pins
219192
pinmap_pinout(tx, PinMap_UART_TX);
220193
pinmap_pinout(rx, PinMap_UART_RX);
@@ -556,8 +529,6 @@ HAL_StatusTypeDef init_uart(serial_t *obj)
556529
return HAL_UART_Init(huart);
557530
}
558531

559-
// Warning: the list of UART/USART in this function must be aligned with the list
560-
// written in serial_init function.
561532
int8_t get_uart_index(UARTName uart_name)
562533
{
563534
uint8_t index = 0;

0 commit comments

Comments
 (0)