2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 * UART pin configuration common to all MCUs.
27 * jflyper - Created as a part of configurable UART/refactoring.
37 #include "build/build_config.h"
39 #include "drivers/rcc.h"
40 #include "drivers/serial.h"
41 #include "drivers/serial_uart.h"
42 #include "drivers/serial_uart_impl.h"
44 void uartPinConfigure(const serialPinConfig_t
*pSerialPinConfig
)
46 for (const uartHardware_t
* hardware
= uartHardware
; hardware
< ARRAYEND(uartHardware
); hardware
++) {
47 const serialPortIdentifier_e identifier
= hardware
->identifier
;
48 uartDevice_t
* uartdev
= uartDeviceFromIdentifier(identifier
);
49 const int resourceIndex
= serialResourceIndex(identifier
);
50 if (uartdev
== NULL
|| resourceIndex
< 0) {
51 // malformed uartHardware
54 const ioTag_t cfgRx
= pSerialPinConfig
->ioTagRx
[resourceIndex
];
55 const ioTag_t cfgTx
= pSerialPinConfig
->ioTagTx
[resourceIndex
];
56 #if UART_TRAIT_PINSWAP
59 for (unsigned pindex
= 0; pindex
< UARTHARDWARE_MAX_PINS
; pindex
++) {
60 if (cfgRx
&& cfgRx
== hardware
->rxPins
[pindex
].pin
) {
61 uartdev
->rx
= hardware
->rxPins
[pindex
];
64 if (cfgTx
&& cfgTx
== hardware
->txPins
[pindex
].pin
) {
65 uartdev
->tx
= hardware
->txPins
[pindex
];
67 #if UART_TRAIT_PINSWAP
68 // Check for swapped pins
69 if (cfgTx
&& cfgTx
== hardware
->rxPins
[pindex
].pin
) {
70 uartdev
->tx
= hardware
->rxPins
[pindex
];
74 if (cfgRx
&& cfgRx
== hardware
->txPins
[pindex
].pin
) {
75 uartdev
->rx
= hardware
->txPins
[pindex
];
80 if (uartdev
->rx
.pin
|| uartdev
->tx
.pin
) {
81 uartdev
->hardware
= hardware
;
82 #if UART_TRAIT_PINSWAP
83 uartdev
->pinSwap
= swap
;