Skip to content

Commit bb856c0

Browse files
authored
Merge pull request #10333 from bkleiner/feature_serial_pin_swap
at32: add uart pinswap
2 parents 4edba00 + b5da18f commit bb856c0

File tree

4 files changed

+80
-11
lines changed

4 files changed

+80
-11
lines changed

src/main/drivers/serial_uart.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ typedef enum {
5050
UARTDEV_5 = 4,
5151
UARTDEV_6 = 5,
5252
UARTDEV_7 = 6,
53-
UARTDEV_8 = 7
53+
UARTDEV_8 = 7,
54+
UARTDEV_MAX
5455
} UARTDevice_e;
5556

5657
typedef struct {
@@ -69,6 +70,7 @@ typedef struct {
6970

7071
void uartGetPortPins(UARTDevice_e device, serialPortPins_t * pins);
7172
void uartClearIdleFlag(uartPort_t *s);
73+
void uartConfigurePinSwap(uartPort_t *uartPort);
7274
#if defined(AT32F43x)
7375
serialPort_t *uartOpen(usart_type *USARTx, serialReceiveCallbackPtr rxCallback, void *rxCallbackData, uint32_t baudRate, portMode_t mode, portOptions_t options);
7476
#else

src/main/drivers/serial_uart_at32f43x.c

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef struct uartDevice_s {
4545
uint8_t af;
4646
uint8_t irq;
4747
uint32_t irqPriority;
48+
bool pinSwap;
4849
} uartDevice_t;
4950

5051
#ifdef USE_UART1
@@ -59,7 +60,12 @@ static uartDevice_t uart1 =
5960
#endif
6061
.rcc_apb2 = RCC_APB2(USART1),
6162
.irq = USART1_IRQn,
62-
.irqPriority = NVIC_PRIO_SERIALUART
63+
.irqPriority = NVIC_PRIO_SERIALUART,
64+
#ifdef USE_UART1_PIN_SWAP
65+
.pinSwap = true,
66+
#else
67+
.pinSwap = false,
68+
#endif
6369
};
6470
#endif
6571

@@ -75,7 +81,12 @@ static uartDevice_t uart2 =
7581
#endif
7682
.rcc_apb1 = RCC_APB1(USART2),
7783
.irq = USART2_IRQn,
78-
.irqPriority = NVIC_PRIO_SERIALUART
84+
.irqPriority = NVIC_PRIO_SERIALUART,
85+
#ifdef USE_UART2_PIN_SWAP
86+
.pinSwap = true,
87+
#else
88+
.pinSwap = false,
89+
#endif
7990
};
8091
#endif
8192

@@ -91,7 +102,12 @@ static uartDevice_t uart3 =
91102
#endif
92103
.rcc_apb1 = RCC_APB1(USART3),
93104
.irq = USART3_IRQn,
94-
.irqPriority = NVIC_PRIO_SERIALUART
105+
.irqPriority = NVIC_PRIO_SERIALUART,
106+
#ifdef USE_UART3_PIN_SWAP
107+
.pinSwap = true,
108+
#else
109+
.pinSwap = false,
110+
#endif
95111
};
96112
#endif
97113

@@ -107,7 +123,12 @@ static uartDevice_t uart4 =
107123
#endif
108124
.rcc_apb1 = RCC_APB1(UART4),
109125
.irq = UART4_IRQn,
110-
.irqPriority = NVIC_PRIO_SERIALUART
126+
.irqPriority = NVIC_PRIO_SERIALUART,
127+
#ifdef USE_UART4_PIN_SWAP
128+
.pinSwap = true,
129+
#else
130+
.pinSwap = false,
131+
#endif
111132
};
112133
#endif
113134

@@ -123,7 +144,12 @@ static uartDevice_t uart5 =
123144
#endif
124145
.rcc_apb1 = RCC_APB1(UART5),
125146
.irq = UART5_IRQn,
126-
.irqPriority = NVIC_PRIO_SERIALUART
147+
.irqPriority = NVIC_PRIO_SERIALUART,
148+
#ifdef USE_UART5_PIN_SWAP
149+
.pinSwap = true,
150+
#else
151+
.pinSwap = false,
152+
#endif
127153
};
128154
#endif
129155

@@ -139,7 +165,12 @@ static uartDevice_t uart6 =
139165
#endif
140166
.rcc_apb2 = RCC_APB2(USART6),
141167
.irq = USART6_IRQn,
142-
.irqPriority = NVIC_PRIO_SERIALUART
168+
.irqPriority = NVIC_PRIO_SERIALUART,
169+
#ifdef USE_UART6_PIN_SWAP
170+
.pinSwap = true,
171+
#else
172+
.pinSwap = false,
173+
#endif
143174
};
144175
#endif
145176

@@ -152,7 +183,12 @@ static uartDevice_t uart7 =
152183
.af = GPIO_MUX_8,
153184
.rcc_apb1 = RCC_APB1(UART7),
154185
.irq = UART7_IRQn,
155-
.irqPriority = NVIC_PRIO_SERIALUART
186+
.irqPriority = NVIC_PRIO_SERIALUART,
187+
#ifdef USE_UART7_PIN_SWAP
188+
.pinSwap = true,
189+
#else
190+
.pinSwap = false,
191+
#endif
156192
};
157193
#endif
158194

@@ -165,7 +201,12 @@ static uartDevice_t uart8 =
165201
.af = GPIO_MUX_8,
166202
.rcc_apb1 = RCC_APB1(UART8),
167203
.irq = UART8_IRQn,
168-
.irqPriority = NVIC_PRIO_SERIALUART
204+
.irqPriority = NVIC_PRIO_SERIALUART,
205+
#ifdef USE_UART8_PIN_SWAP
206+
.pinSwap = true,
207+
#else
208+
.pinSwap = false,
209+
#endif
169210
};
170211
#endif
171212

@@ -258,6 +299,30 @@ void uartClearIdleFlag(uartPort_t *s)
258299
(void) s->USARTx->dt;
259300
}
260301

302+
static uartDevice_t *uartFindDevice(uartPort_t *uartPort)
303+
{
304+
for (uint32_t i = 0; i < UARTDEV_MAX; i++) {
305+
uartDevice_t *pDevice = uartHardwareMap[i];
306+
307+
if (pDevice->dev == uartPort->USARTx) {
308+
return pDevice;
309+
}
310+
}
311+
return NULL;
312+
}
313+
314+
void uartConfigurePinSwap(uartPort_t *uartPort)
315+
{
316+
uartDevice_t *uartDevice = uartFindDevice(uartPort);
317+
if (!uartDevice) {
318+
return;
319+
}
320+
321+
if (uartDevice->pinSwap) {
322+
usart_transmit_receive_pin_swap(uartPort->USARTx, TRUE);
323+
}
324+
}
325+
261326
uartPort_t *serialUART(UARTDevice_e device, uint32_t baudRate, portMode_t mode, portOptions_t options)
262327
{
263328
uartPort_t *s;

src/main/drivers/serial_uart_hal_at32f43x.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static void uartReconfigure(uartPort_t *uartPort)
8585
usart_transmitter_enable(uartPort->USARTx, TRUE);
8686

8787
usartConfigurePinInversion(uartPort);
88+
uartConfigurePinSwap(uartPort);
8889

8990
if (uartPort->port.options & SERIAL_BIDIR)
9091
usart_single_line_halfduplex_select(uartPort->USARTx, TRUE);

src/main/target/NEUTRONRCF435MINI/target.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@
144144
#define UART2_TX_PIN PA2
145145

146146
#define USE_UART3
147-
#define UART3_RX_PIN PB11
148-
#define UART3_TX_PIN PB10
147+
#define USE_UART3_PIN_SWAP
148+
#define UART3_RX_PIN PB10
149+
#define UART3_TX_PIN PB11
149150

150151
#define USE_UART5
151152
#define UART5_RX_PIN PB8

0 commit comments

Comments
 (0)