Fix UARTS and LPUART
[betaflight.git] / src / main / drivers / serial_uart_pinconfig.c
blob70fe92571dfd48364ce6dba86399302cd11e4395
1 /*
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)
8 * any later version.
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.
26 * Authors:
27 * jflyper - Created as a part of configurable UART/refactoring.
30 #include <stdbool.h>
31 #include <stdint.h>
33 #include "platform.h"
35 #ifdef USE_UART
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 static FAST_DATA_ZERO_INIT uartDevice_t uartDevice[UARTDEV_COUNT]; // Only those configured in target.h
45 FAST_DATA_ZERO_INIT uartDevice_t *uartDevmap[UARTDEV_COUNT_MAX]; // Full array
47 void uartPinConfigure(const serialPinConfig_t *pSerialPinConfig)
49 uartDevice_t *uartdev = uartDevice;
51 for (size_t hindex = 0; hindex < UARTDEV_COUNT; hindex++) {
53 const uartHardware_t *hardware = &uartHardware[hindex];
54 const UARTDevice_e device = hardware->device;
56 #if !(defined(STM32F1) || defined(STM32F4)) // Older CPUs don't support pin swap.
57 uartdev->pinSwap = false;
58 #endif
59 for (int pindex = 0 ; pindex < UARTHARDWARE_MAX_PINS ; pindex++) {
60 if (pSerialPinConfig->ioTagRx[device] && (pSerialPinConfig->ioTagRx[device] == hardware->rxPins[pindex].pin)) {
61 uartdev->rx = hardware->rxPins[pindex];
64 if (pSerialPinConfig->ioTagTx[device] && (pSerialPinConfig->ioTagTx[device] == hardware->txPins[pindex].pin)) {
65 uartdev->tx = hardware->txPins[pindex];
69 #if !(defined(STM32F1) || defined(STM32F4))
70 // Check for swapped pins
71 if (pSerialPinConfig->ioTagTx[device] && (pSerialPinConfig->ioTagTx[device] == hardware->rxPins[pindex].pin)) {
72 uartdev->tx = hardware->rxPins[pindex];
73 uartdev->pinSwap = true;
76 if (pSerialPinConfig->ioTagRx[device] && (pSerialPinConfig->ioTagRx[device] == hardware->txPins[pindex].pin)) {
77 uartdev->rx = hardware->txPins[pindex];
78 uartdev->pinSwap = true;
80 #endif
83 if (uartdev->rx.pin || uartdev->tx.pin) {
84 uartdev->hardware = hardware;
85 uartDevmap[device] = uartdev++;
89 #endif