|
| 1 | +/* uart_drv_stm32.c |
| 2 | + * |
| 3 | + * Driver for the back-end of the UART_FLASH module. |
| 4 | + * |
| 5 | + * Example implementation for stm32WB, using UART1. |
| 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 UART1 (PB6/PB7) */ |
| 31 | +#define UART1 (0x40013800) |
| 32 | +#define UART1_PIN_AF 7 |
| 33 | +#define UART1_RX_PIN 7 |
| 34 | +#define UART1_TX_PIN 6 |
| 35 | + |
| 36 | +#define UART1_CR1 (*(volatile uint32_t *)(UART1 + 0x00)) |
| 37 | +#define UART1_CR2 (*(volatile uint32_t *)(UART1 + 0x04)) |
| 38 | +#define UART1_BRR (*(volatile uint32_t *)(UART1 + 0x0C)) |
| 39 | +#define UART1_ISR (*(volatile uint32_t *)(UART1 + 0x1C)) |
| 40 | +#define UART1_RDR (*(volatile uint32_t *)(UART1 + 0x24)) |
| 41 | +#define UART1_TDR (*(volatile uint32_t *)(UART1 + 0x28)) |
| 42 | +#define UART_CR1_UART_ENABLE (1 << 0) |
| 43 | +#define UART_CR1_TX_ENABLE (1 << 3) |
| 44 | +#define UART_CR1_RX_ENABLE (1 << 2) |
| 45 | +#define UART_CR1_SYMBOL_LEN (1 << 28) |
| 46 | +#define UART_CR1_FIFO_ENABLE (1 << 29) |
| 47 | +#define UART_CR1_PARITY_ENABLED (1 << 10) |
| 48 | +#define UART_CR1_PARITY_ODD (1 << 9) |
| 49 | +#define UART_ISR_TX_EMPTY (1 << 7) |
| 50 | +#define UART_ISR_RX_NOTEMPTY (1 << 5) |
| 51 | + |
| 52 | +#define CLOCK_SPEED (64000000) /* 64 MHz (STM32WB55) */ |
| 53 | + |
| 54 | +#define APB2_CLOCK_ER (*(volatile uint32_t *)(0x58000060)) |
| 55 | +#define UART1_APB2_CLOCK_ER_VAL (1 << 14) |
| 56 | + |
| 57 | +#define AHB2_CLOCK_ER (*(volatile uint32_t *)(0x5800004c)) |
| 58 | +#define GPIOB_AHB2_CLOCK_ER (1 << 1) |
| 59 | +#define GPIOB_BASE 0x48000400 |
| 60 | +#define GPIOB_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00)) |
| 61 | +#define GPIOB_AFL (*(volatile uint32_t *)(GPIOB_BASE + 0x20)) |
| 62 | +#define GPIOB_AFH (*(volatile uint32_t *)(GPIOB_BASE + 0x24)) |
| 63 | +#define GPIO_MODE_AF (2) |
| 64 | + |
| 65 | +static void uart_pins_setup(void) |
| 66 | +{ |
| 67 | + uint32_t reg; |
| 68 | + AHB2_CLOCK_ER |= GPIOB_AHB2_CLOCK_ER; |
| 69 | + /* Set mode = AF */ |
| 70 | + reg = GPIOB_MODE & ~ (0x03 << (UART1_RX_PIN * 2)); |
| 71 | + GPIOB_MODE = reg | (2 << (UART1_RX_PIN * 2)); |
| 72 | + reg = GPIOB_MODE & ~ (0x03 << (UART1_TX_PIN * 2)); |
| 73 | + GPIOB_MODE = reg | (2 << (UART1_TX_PIN * 2)); |
| 74 | + |
| 75 | + /* Alternate function: use low pins (6 and 7) */ |
| 76 | + reg = GPIOB_AFL & ~(0xf << ((UART1_TX_PIN) * 4)); |
| 77 | + GPIOB_AFL = reg | (UART1_PIN_AF << ((UART1_TX_PIN) * 4)); |
| 78 | + reg = GPIOB_AFL & ~(0xf << ((UART1_RX_PIN) * 4)); |
| 79 | + GPIOB_AFL = reg | (UART1_PIN_AF << ((UART1_RX_PIN) * 4)); |
| 80 | +} |
| 81 | + |
| 82 | +int uart_tx(const uint8_t c) |
| 83 | +{ |
| 84 | + uint32_t reg; |
| 85 | + do { |
| 86 | + reg = UART1_ISR; |
| 87 | + } while ((reg & UART_ISR_TX_EMPTY) == 0); |
| 88 | + UART1_TDR = c; |
| 89 | + return 1; |
| 90 | +} |
| 91 | + |
| 92 | +int uart_rx(uint8_t *c) |
| 93 | +{ |
| 94 | + volatile uint32_t reg = UART1_ISR; |
| 95 | + if ((reg & UART_ISR_RX_NOTEMPTY) != 0) { |
| 96 | + reg = UART1_RDR; |
| 97 | + *c = (uint8_t)(reg & 0xff); |
| 98 | + return 1; |
| 99 | + } |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop) |
| 104 | +{ |
| 105 | + uint32_t reg; |
| 106 | + /* Enable pins and configure for AF7 */ |
| 107 | + uart_pins_setup(); |
| 108 | + /* Turn on the device */ |
| 109 | + APB2_CLOCK_ER |= UART1_APB2_CLOCK_ER_VAL; |
| 110 | + UART1_CR1 &= ~(UART_CR1_UART_ENABLE); |
| 111 | + UART1_CR1 &= ~(UART_CR1_FIFO_ENABLE); |
| 112 | + |
| 113 | + /* Configure for TX + RX */ |
| 114 | + UART1_CR1 |= (UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE); |
| 115 | + |
| 116 | + /* Configure clock */ |
| 117 | + UART1_BRR = CLOCK_SPEED / (2 * bitrate); |
| 118 | + |
| 119 | + /* Configure data bits */ |
| 120 | + if (data == 8) |
| 121 | + UART1_CR1 &= ~UART_CR1_SYMBOL_LEN; |
| 122 | + else |
| 123 | + UART1_CR1 |= UART_CR1_SYMBOL_LEN; |
| 124 | + |
| 125 | + /* Configure parity */ |
| 126 | + switch (parity) { |
| 127 | + case 'O': |
| 128 | + UART1_CR1 |= UART_CR1_PARITY_ODD; |
| 129 | + /* fall through to enable parity */ |
| 130 | + /* FALL THROUGH */ |
| 131 | + case 'E': |
| 132 | + UART1_CR1 |= UART_CR1_PARITY_ENABLED; |
| 133 | + break; |
| 134 | + default: |
| 135 | + UART1_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD); |
| 136 | + } |
| 137 | + /* Set stop bits (not supported) */ |
| 138 | + (void)stop; |
| 139 | + |
| 140 | + /* Turn on uart */ |
| 141 | + UART1_CR1 |= UART_CR1_UART_ENABLE; |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
0 commit comments