|
| 1 | +/* uart_drv_stm32.c |
| 2 | + * |
| 3 | + * Driver for the back-end of the UART_FLASH module. |
| 4 | + * |
| 5 | + * Example implementation for stm32F4, using UART3. |
| 6 | + * |
| 7 | + * Pinout: RX=PD9, TX=PD8 |
| 8 | + * |
| 9 | + * Copyright (C) 2020 wolfSSL Inc. |
| 10 | + * |
| 11 | + * This file is part of wolfBoot. |
| 12 | + * |
| 13 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 14 | + * it under the terms of the GNU General Public License as published by |
| 15 | + * the Free Software Foundation; either version 2 of the License, or |
| 16 | + * (at your option) any later version. |
| 17 | + * |
| 18 | + * wolfBoot is distributed in the hope that it will be useful, |
| 19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + * GNU General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU General Public License |
| 24 | + * along with this program; if not, write to the Free Software |
| 25 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 26 | + */ |
| 27 | + |
| 28 | +#include <stdint.h> |
| 29 | + |
| 30 | +/* Driver hardcoded to work on UART3 (PD8/PD9) */ |
| 31 | +#define UART3 (0x40004800) |
| 32 | +#define UART3_PIN_AF 7 |
| 33 | +#define UART3_RX_PIN 9 |
| 34 | +#define UART3_TX_PIN 8 |
| 35 | + |
| 36 | +#define UART3_SR (*(volatile uint32_t *)(UART3)) |
| 37 | +#define UART3_DR (*(volatile uint32_t *)(UART3 + 0x04)) |
| 38 | +#define UART3_BRR (*(volatile uint32_t *)(UART3 + 0x08)) |
| 39 | +#define UART3_CR1 (*(volatile uint32_t *)(UART3 + 0x0c)) |
| 40 | +#define UART3_CR2 (*(volatile uint32_t *)(UART3 + 0x10)) |
| 41 | + |
| 42 | +#define UART_CR1_UART_ENABLE (1 << 13) |
| 43 | +#define UART_CR1_SYMBOL_LEN (1 << 12) |
| 44 | +#define UART_CR1_PARITY_ENABLED (1 << 10) |
| 45 | +#define UART_CR1_PARITY_ODD (1 << 9) |
| 46 | +#define UART_CR1_TX_ENABLE (1 << 3) |
| 47 | +#define UART_CR1_RX_ENABLE (1 << 2) |
| 48 | +#define UART_CR2_STOPBITS (3 << 12) |
| 49 | +#define UART_SR_TX_EMPTY (1 << 7) |
| 50 | +#define UART_SR_RX_NOTEMPTY (1 << 5) |
| 51 | + |
| 52 | + |
| 53 | +#define CLOCK_SPEED (168000000) |
| 54 | + |
| 55 | +#define APB1_CLOCK_ER (*(volatile uint32_t *)(0x40023840)) |
| 56 | +#define UART3_APB1_CLOCK_ER_VAL (1 << 18) |
| 57 | + |
| 58 | +#define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830)) |
| 59 | +#define GPIOD_AHB1_CLOCK_ER (1 << 3) |
| 60 | +#define GPIOD_BASE 0x40020c00 |
| 61 | +#define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00)) |
| 62 | +#define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20)) |
| 63 | +#define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24)) |
| 64 | +#define GPIO_MODE_AF (2) |
| 65 | + |
| 66 | +static void uart_pins_setup(void) |
| 67 | +{ |
| 68 | + uint32_t reg; |
| 69 | + AHB1_CLOCK_ER |= GPIOD_AHB1_CLOCK_ER; |
| 70 | + /* Set mode = AF */ |
| 71 | + reg = GPIOD_MODE & ~ (0x03 << (UART3_RX_PIN * 2)); |
| 72 | + GPIOD_MODE = reg | (2 << (UART3_RX_PIN * 2)); |
| 73 | + reg = GPIOD_MODE & ~ (0x03 << (UART3_TX_PIN * 2)); |
| 74 | + GPIOD_MODE = reg | (2 << (UART3_TX_PIN * 2)); |
| 75 | + |
| 76 | + /* Alternate function: use high pins (8 and 9) */ |
| 77 | + reg = GPIOD_AFH & ~(0xf << ((UART3_TX_PIN - 8) * 4)); |
| 78 | + GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_TX_PIN - 8) * 4)); |
| 79 | + reg = GPIOD_AFH & ~(0xf << ((UART3_RX_PIN - 8) * 4)); |
| 80 | + GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_RX_PIN - 8) * 4)); |
| 81 | +} |
| 82 | + |
| 83 | +int uart_tx(const uint8_t c) |
| 84 | +{ |
| 85 | + uint32_t reg; |
| 86 | + do { |
| 87 | + reg = UART3_SR; |
| 88 | + } while ((reg & UART_SR_TX_EMPTY) == 0); |
| 89 | + UART3_DR = c; |
| 90 | + return 1; |
| 91 | +} |
| 92 | + |
| 93 | +int uart_rx(uint8_t *c) |
| 94 | +{ |
| 95 | + volatile uint32_t reg = UART3_SR; |
| 96 | + if ((reg & UART_SR_RX_NOTEMPTY) != 0) { |
| 97 | + reg = UART3_DR; |
| 98 | + *c = (uint8_t)(reg & 0xff); |
| 99 | + return 1; |
| 100 | + } |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop) |
| 105 | +{ |
| 106 | + uint32_t reg; |
| 107 | + /* Enable pins and configure for AF7 */ |
| 108 | + uart_pins_setup(); |
| 109 | + /* Turn on the device */ |
| 110 | + APB1_CLOCK_ER |= UART3_APB1_CLOCK_ER_VAL; |
| 111 | + UART3_CR1 &= ~(UART_CR1_UART_ENABLE); |
| 112 | + |
| 113 | + /* Configure for TX + RX */ |
| 114 | + UART3_CR1 |= (UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE); |
| 115 | + |
| 116 | + /* Configure clock */ |
| 117 | + UART3_BRR = CLOCK_SPEED / bitrate; |
| 118 | + |
| 119 | + /* Configure data bits */ |
| 120 | + if (data == 8) |
| 121 | + UART3_CR1 &= ~UART_CR1_SYMBOL_LEN; |
| 122 | + else |
| 123 | + UART3_CR1 |= UART_CR1_SYMBOL_LEN; |
| 124 | + |
| 125 | + /* Configure parity */ |
| 126 | + switch (parity) { |
| 127 | + case 'O': |
| 128 | + UART3_CR1 |= UART_CR1_PARITY_ODD; |
| 129 | + /* fall through to enable parity */ |
| 130 | + /* FALL THROUGH */ |
| 131 | + case 'E': |
| 132 | + UART3_CR1 |= UART_CR1_PARITY_ENABLED; |
| 133 | + break; |
| 134 | + default: |
| 135 | + UART3_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD); |
| 136 | + } |
| 137 | + /* Set stop bits */ |
| 138 | + reg = UART3_CR2 & ~UART_CR2_STOPBITS; |
| 139 | + if (stop > 1) |
| 140 | + UART3_CR2 = reg & (2 << 12); |
| 141 | + else |
| 142 | + UART3_CR2 = reg; |
| 143 | + |
| 144 | + /* Turn on uart */ |
| 145 | + UART3_CR1 |= UART_CR1_UART_ENABLE; |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
0 commit comments