Skip to content

Commit 62a7ecd

Browse files
Merge pull request #5570 from jeromecoutant/PR_STDIO
STM32 UART init update
2 parents 18e7276 + 1cf47f6 commit 62a7ecd

File tree

11 files changed

+482
-1638
lines changed

11 files changed

+482
-1638
lines changed

targets/TARGET_STM/TARGET_STM32F0/serial_device.c

Lines changed: 11 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* mbed Microcontroller Library
22
*******************************************************************************
3-
* Copyright (c) 2015, STMicroelectronics
3+
* Copyright (c) 2017, STMicroelectronics
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -27,234 +27,26 @@
2727
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*******************************************************************************
2929
*/
30-
#include "mbed_assert.h"
31-
#include "serial_api.h"
32-
#include "serial_api_hal.h"
3330

3431
#if DEVICE_SERIAL
3532

36-
#include "cmsis.h"
37-
#include "pinmap.h"
38-
#include <string.h>
39-
#include "PeripheralPins.h"
40-
#include "mbed_error.h"
33+
#include "serial_api_hal.h"
4134

42-
#if defined (TARGET_STM32F091RC)
43-
#define UART_NUM (8)
35+
#if defined (TARGET_STM32F031K6)
36+
#define UART_NUM (1)
4437
#elif defined (TARGET_STM32F030R8) || defined (TARGET_STM32F051R8) || defined (TARGET_STM32F042K6)
4538
#define UART_NUM (2)
46-
#elif defined (TARGET_STM32F031K6)
47-
#define UART_NUM (1)
48-
#else
39+
#elif defined (TARGET_STM32F070RB) || defined (TARGET_STM32F072RB)
4940
#define UART_NUM (4)
41+
#else
42+
#define UART_NUM (8) // max value (TARGET_STM32F091RC)
5043
#endif
5144

52-
static uint32_t serial_irq_ids[UART_NUM] = {0};
45+
uint32_t serial_irq_ids[UART_NUM] = {0};
5346
UART_HandleTypeDef uart_handlers[UART_NUM];
5447

5548
static uart_irq_handler irq_handler;
5649

57-
int stdio_uart_inited = 0;
58-
serial_t stdio_uart;
59-
60-
void serial_init(serial_t *obj, PinName tx, PinName rx)
61-
{
62-
struct serial_s *obj_s = SERIAL_S(obj);
63-
64-
// Determine the UART to use (UART_1, UART_2, ...)
65-
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
66-
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
67-
68-
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
69-
obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
70-
MBED_ASSERT(obj_s->uart != (UARTName)NC);
71-
72-
// Enable USART clock
73-
if (obj_s->uart == UART_1) {
74-
__HAL_RCC_USART1_FORCE_RESET();
75-
__HAL_RCC_USART1_RELEASE_RESET();
76-
__HAL_RCC_USART1_CLK_ENABLE();
77-
obj_s->index = 0;
78-
}
79-
80-
#if defined USART2_BASE
81-
if (obj_s->uart == UART_2) {
82-
__HAL_RCC_USART2_FORCE_RESET();
83-
__HAL_RCC_USART2_RELEASE_RESET();
84-
__HAL_RCC_USART2_CLK_ENABLE();
85-
obj_s->index = 1;
86-
}
87-
#endif
88-
89-
#if defined USART3_BASE
90-
if (obj_s->uart == UART_3) {
91-
__HAL_RCC_USART3_FORCE_RESET();
92-
__HAL_RCC_USART3_RELEASE_RESET();
93-
__HAL_RCC_USART3_CLK_ENABLE();
94-
obj_s->index = 2;
95-
}
96-
#endif
97-
98-
#if defined USART4_BASE
99-
if (obj_s->uart == UART_4) {
100-
__HAL_RCC_USART4_FORCE_RESET();
101-
__HAL_RCC_USART4_RELEASE_RESET();
102-
__HAL_RCC_USART4_CLK_ENABLE();
103-
obj_s->index = 3;
104-
}
105-
#endif
106-
107-
#if defined USART5_BASE
108-
if (obj_s->uart == UART_5) {
109-
__HAL_RCC_USART5_FORCE_RESET();
110-
__HAL_RCC_USART5_RELEASE_RESET();
111-
__HAL_RCC_USART5_CLK_ENABLE();
112-
obj_s->index = 4;
113-
}
114-
#endif
115-
116-
#if defined USART6_BASE
117-
if (obj_s->uart == UART_6) {
118-
__HAL_RCC_USART6_FORCE_RESET();
119-
__HAL_RCC_USART6_RELEASE_RESET();
120-
__HAL_RCC_USART6_CLK_ENABLE();
121-
obj_s->index = 5;
122-
}
123-
#endif
124-
125-
#if defined USART7_BASE
126-
if (obj_s->uart == UART_7) {
127-
__HAL_RCC_USART7_FORCE_RESET();
128-
__HAL_RCC_USART7_RELEASE_RESET();
129-
__HAL_RCC_USART7_CLK_ENABLE();
130-
obj_s->index = 6;
131-
}
132-
#endif
133-
134-
#if defined USART8_BASE
135-
if (obj_s->uart == UART_8) {
136-
__HAL_RCC_USART8_FORCE_RESET();
137-
__HAL_RCC_USART8_RELEASE_RESET();
138-
__HAL_RCC_USART8_CLK_ENABLE();
139-
obj_s->index = 7;
140-
}
141-
#endif
142-
143-
// Configure the UART pins
144-
pinmap_pinout(tx, PinMap_UART_TX);
145-
pinmap_pinout(rx, PinMap_UART_RX);
146-
147-
if (tx != NC) {
148-
pin_mode(tx, PullUp);
149-
}
150-
if (rx != NC) {
151-
pin_mode(rx, PullUp);
152-
}
153-
154-
// Configure UART
155-
obj_s->baudrate = 9600;
156-
obj_s->databits = UART_WORDLENGTH_8B;
157-
obj_s->stopbits = UART_STOPBITS_1;
158-
obj_s->parity = UART_PARITY_NONE;
159-
160-
#if DEVICE_SERIAL_FC
161-
obj_s->hw_flow_ctl = UART_HWCONTROL_NONE;
162-
#endif
163-
164-
obj_s->pin_tx = tx;
165-
obj_s->pin_rx = rx;
166-
167-
init_uart(obj);
168-
169-
// For stdio management
170-
if (obj_s->uart == STDIO_UART) {
171-
stdio_uart_inited = 1;
172-
memcpy(&stdio_uart, obj, sizeof(serial_t));
173-
}
174-
}
175-
176-
void serial_free(serial_t *obj)
177-
{
178-
struct serial_s *obj_s = SERIAL_S(obj);
179-
180-
// Reset UART and disable clock
181-
if (obj_s->uart == UART_1) {
182-
__HAL_RCC_USART1_FORCE_RESET();
183-
__HAL_RCC_USART1_RELEASE_RESET();
184-
__HAL_RCC_USART1_CLK_DISABLE();
185-
}
186-
187-
#if defined(USART2_BASE)
188-
if (obj_s->uart == UART_2) {
189-
__HAL_RCC_USART2_FORCE_RESET();
190-
__HAL_RCC_USART2_RELEASE_RESET();
191-
__HAL_RCC_USART2_CLK_DISABLE();
192-
}
193-
#endif
194-
195-
#if defined USART3_BASE
196-
if (obj_s->uart == UART_3) {
197-
__HAL_RCC_USART3_FORCE_RESET();
198-
__HAL_RCC_USART3_RELEASE_RESET();
199-
__HAL_RCC_USART3_CLK_DISABLE();
200-
}
201-
#endif
202-
203-
#if defined USART4_BASE
204-
if (obj_s->uart == UART_4) {
205-
__HAL_RCC_USART4_FORCE_RESET();
206-
__HAL_RCC_USART4_RELEASE_RESET();
207-
__HAL_RCC_USART4_CLK_DISABLE();
208-
}
209-
#endif
210-
211-
#if defined USART5_BASE
212-
if (obj_s->uart == UART_5) {
213-
__HAL_RCC_USART5_FORCE_RESET();
214-
__HAL_RCC_USART5_RELEASE_RESET();
215-
__HAL_RCC_USART5_CLK_DISABLE();
216-
}
217-
#endif
218-
219-
#if defined USART6_BASE
220-
if (obj_s->uart == UART_6) {
221-
__HAL_RCC_USART6_FORCE_RESET();
222-
__HAL_RCC_USART6_RELEASE_RESET();
223-
__HAL_RCC_USART6_CLK_DISABLE();
224-
}
225-
#endif
226-
227-
#if defined USART7_BASE
228-
if (obj_s->uart == UART_7) {
229-
__HAL_RCC_USART7_FORCE_RESET();
230-
__HAL_RCC_USART7_RELEASE_RESET();
231-
__HAL_RCC_USART7_CLK_DISABLE();
232-
}
233-
#endif
234-
235-
#if defined USART8_BASE
236-
if (obj_s->uart == UART_8) {
237-
__HAL_RCC_USART8_FORCE_RESET();
238-
__HAL_RCC_USART8_RELEASE_RESET();
239-
__HAL_RCC_USART8_CLK_DISABLE();
240-
}
241-
#endif
242-
243-
// Configure GPIOs
244-
pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
245-
pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
246-
247-
serial_irq_ids[obj_s->index] = 0;
248-
}
249-
250-
void serial_baud(serial_t *obj, int baudrate)
251-
{
252-
struct serial_s *obj_s = SERIAL_S(obj);
253-
254-
obj_s->baudrate = baudrate;
255-
init_uart(obj);
256-
}
257-
25850
/******************************************************************************
25951
* INTERRUPTS HANDLING
26052
******************************************************************************/
@@ -884,7 +676,7 @@ void serial_rx_abort_asynch(serial_t *obj)
884676
}
885677
}
886678

887-
#endif
679+
#endif /* DEVICE_SERIAL_ASYNCH */
888680

889681
#if DEVICE_SERIAL_FC
890682

@@ -943,6 +735,6 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
943735
init_uart(obj);
944736
}
945737

946-
#endif
738+
#endif /* DEVICE_SERIAL_FC */
947739

948-
#endif
740+
#endif /* DEVICE_SERIAL */

0 commit comments

Comments
 (0)