@@ -44,7 +44,6 @@ HAL_StatusTypeDef init_uart(serial_t *obj);
44
44
void serial_init (serial_t * obj , PinName tx , PinName rx )
45
45
{
46
46
struct serial_s * obj_s = SERIAL_S (obj );
47
- int IndexNumber = 0 ;
48
47
uint8_t stdio_config = 0 ;
49
48
50
49
// Determine the UART to use (UART_1, UART_2, ...)
@@ -64,157 +63,131 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
64
63
}
65
64
}
66
65
67
- // Enable USART clock
66
+ // Reset and enable clock
68
67
#if defined(USART1_BASE )
69
68
if (obj_s -> uart == UART_1 ) {
70
69
__HAL_RCC_USART1_FORCE_RESET ();
71
70
__HAL_RCC_USART1_RELEASE_RESET ();
72
71
__HAL_RCC_USART1_CLK_ENABLE ();
73
- obj_s -> index = IndexNumber ;
74
72
}
75
- IndexNumber ++ ;
76
73
#endif
77
74
78
75
#if defined (USART2_BASE )
79
76
if (obj_s -> uart == UART_2 ) {
80
77
__HAL_RCC_USART2_FORCE_RESET ();
81
78
__HAL_RCC_USART2_RELEASE_RESET ();
82
79
__HAL_RCC_USART2_CLK_ENABLE ();
83
- obj_s -> index = IndexNumber ;
84
80
}
85
- IndexNumber ++ ;
86
81
#endif
87
82
88
83
#if defined(USART3_BASE )
89
84
if (obj_s -> uart == UART_3 ) {
90
85
__HAL_RCC_USART3_FORCE_RESET ();
91
86
__HAL_RCC_USART3_RELEASE_RESET ();
92
87
__HAL_RCC_USART3_CLK_ENABLE ();
93
- obj_s -> index = IndexNumber ;
94
88
}
95
- IndexNumber ++ ;
96
89
#endif
97
90
98
91
#if defined(UART4_BASE )
99
92
if (obj_s -> uart == UART_4 ) {
100
93
__HAL_RCC_UART4_FORCE_RESET ();
101
94
__HAL_RCC_UART4_RELEASE_RESET ();
102
95
__HAL_RCC_UART4_CLK_ENABLE ();
103
- obj_s -> index = IndexNumber ;
104
96
}
105
- IndexNumber ++ ;
106
97
#endif
107
98
108
99
#if defined(USART4_BASE )
109
100
if (obj_s -> uart == UART_4 ) {
110
101
__HAL_RCC_USART4_FORCE_RESET ();
111
102
__HAL_RCC_USART4_RELEASE_RESET ();
112
103
__HAL_RCC_USART4_CLK_ENABLE ();
113
- obj_s -> index = IndexNumber ;
114
104
}
115
- IndexNumber ++ ;
116
105
#endif
117
106
118
107
#if defined(UART5_BASE )
119
108
if (obj_s -> uart == UART_5 ) {
120
109
__HAL_RCC_UART5_FORCE_RESET ();
121
110
__HAL_RCC_UART5_RELEASE_RESET ();
122
111
__HAL_RCC_UART5_CLK_ENABLE ();
123
- obj_s -> index = IndexNumber ;
124
112
}
125
- IndexNumber ++ ;
126
113
#endif
127
114
128
115
#if defined(USART5_BASE )
129
116
if (obj_s -> uart == UART_5 ) {
130
117
__HAL_RCC_USART5_FORCE_RESET ();
131
118
__HAL_RCC_USART5_RELEASE_RESET ();
132
119
__HAL_RCC_USART5_CLK_ENABLE ();
133
- obj_s -> index = IndexNumber ;
134
120
}
135
- IndexNumber ++ ;
136
121
#endif
137
122
138
123
#if defined(USART6_BASE )
139
124
if (obj_s -> uart == UART_6 ) {
140
125
__HAL_RCC_USART6_FORCE_RESET ();
141
126
__HAL_RCC_USART6_RELEASE_RESET ();
142
127
__HAL_RCC_USART6_CLK_ENABLE ();
143
- obj_s -> index = IndexNumber ;
144
128
}
145
- IndexNumber ++ ;
146
129
#endif
147
130
148
131
#if defined(UART7_BASE )
149
132
if (obj_s -> uart == UART_7 ) {
150
133
__HAL_RCC_UART7_FORCE_RESET ();
151
134
__HAL_RCC_UART7_RELEASE_RESET ();
152
135
__HAL_RCC_UART7_CLK_ENABLE ();
153
- obj_s -> index = IndexNumber ;
154
136
}
155
- IndexNumber ++ ;
156
137
#endif
157
138
158
139
#if defined(USART7_BASE )
159
140
if (obj_s -> uart == UART_7 ) {
160
141
__HAL_RCC_USART7_FORCE_RESET ();
161
142
__HAL_RCC_USART7_RELEASE_RESET ();
162
143
__HAL_RCC_USART7_CLK_ENABLE ();
163
- obj_s -> index = IndexNumber ;
164
144
}
165
- IndexNumber ++ ;
166
145
#endif
167
146
168
147
#if defined(UART8_BASE )
169
148
if (obj_s -> uart == UART_8 ) {
170
149
__HAL_RCC_UART8_FORCE_RESET ();
171
150
__HAL_RCC_UART8_RELEASE_RESET ();
172
151
__HAL_RCC_UART8_CLK_ENABLE ();
173
- obj_s -> index = IndexNumber ;
174
152
}
175
- IndexNumber ++ ;
176
153
#endif
177
154
178
155
#if defined(USART8_BASE )
179
156
if (obj_s -> uart == UART_8 ) {
180
157
__HAL_RCC_USART8_FORCE_RESET ();
181
158
__HAL_RCC_USART8_RELEASE_RESET ();
182
159
__HAL_RCC_USART8_CLK_ENABLE ();
183
- obj_s -> index = IndexNumber ;
184
160
}
185
- IndexNumber ++ ;
186
161
#endif
187
162
188
163
#if defined(UART9_BASE )
189
164
if (obj_s -> uart == UART_9 ) {
190
165
__HAL_RCC_UART9_FORCE_RESET ();
191
166
__HAL_RCC_UART9_RELEASE_RESET ();
192
167
__HAL_RCC_UART9_CLK_ENABLE ();
193
- obj_s -> index = IndexNumber ;
194
168
}
195
- IndexNumber ++ ;
196
169
#endif
197
170
198
171
#if defined(UART10_BASE )
199
172
if (obj_s -> uart == UART_10 ) {
200
173
__HAL_RCC_UART10_FORCE_RESET ();
201
174
__HAL_RCC_UART10_RELEASE_RESET ();
202
175
__HAL_RCC_UART10_CLK_ENABLE ();
203
- obj_s -> index = IndexNumber ;
204
176
}
205
- IndexNumber ++ ;
206
177
#endif
207
178
208
179
#if defined(LPUART1_BASE )
209
180
if (obj_s -> uart == LPUART_1 ) {
210
181
__HAL_RCC_LPUART1_FORCE_RESET ();
211
182
__HAL_RCC_LPUART1_RELEASE_RESET ();
212
183
__HAL_RCC_LPUART1_CLK_ENABLE ();
213
- obj_s -> index = IndexNumber ;
214
184
}
215
- IndexNumber ++ ;
216
185
#endif
217
186
187
+ // Assign serial object index
188
+ obj_s -> index = get_uart_index (obj_s -> uart );
189
+ MBED_ASSERT (obj_s -> index >= 0 );
190
+
218
191
// Configure UART pins
219
192
pinmap_pinout (tx , PinMap_UART_TX );
220
193
pinmap_pinout (rx , PinMap_UART_RX );
@@ -556,8 +529,6 @@ HAL_StatusTypeDef init_uart(serial_t *obj)
556
529
return HAL_UART_Init (huart );
557
530
}
558
531
559
- // Warning: the list of UART/USART in this function must be aligned with the list
560
- // written in serial_init function.
561
532
int8_t get_uart_index (UARTName uart_name )
562
533
{
563
534
uint8_t index = 0 ;
0 commit comments