Skip to content

Commit

Permalink
Added uart support for stm32l5 (LPUART1)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielinux committed Mar 16, 2023
1 parent a75b796 commit 994b1c0
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 54 deletions.
1 change: 1 addition & 0 deletions hal/stm32_tz.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void hal_tz_release_nonsecure_area(void)
#define SET_GTZC_MPCBBx_S_VCTR(bank,n,val) \
(*((volatile uint32_t *)(GTZC_MPCBB##bank##_S_VCTR_BASE ) + n ))= val


void hal_gtzc_init(void)
{
int i;
Expand Down
44 changes: 29 additions & 15 deletions hal/stm32l5.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,35 @@ static void clock_pll_on(int powersave)
DMB();
}

#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
static void periph_unsecure()
{
uint32_t pin;

/*Enable clock for User LED GPIOs */
RCC_AHB2_CLOCK_ER|= LED_AHB2_ENABLE;

/* Enable clock for LPUART1 */
RCC_APB1_CLOCK_ER |= UART1_APB1_CLOCK_ER_VAL;


PWR_CR2 |= PWR_CR2_IOSV;
/*Un-secure User LED GPIO pins */
#ifdef STM32_DISCOVERY
GPIO_SECCFGR(GPIOD_BASE) &= ~(1<<LED_USR_PIN);
GPIO_SECCFGR(GPIOG_BASE) &= ~(1<<LED_BOOT_PIN);
#else /* Nucleo board */
GPIO_SECCFGR(GPIOA_BASE) &= ~(1<<LED_BOOT_PIN);
GPIO_SECCFGR(GPIOB_BASE) &= ~(1<<LED_USR_PIN);
GPIO_SECCFGR(GPIOC_BASE) &= ~(1<<LED_EXTRA_PIN);
#endif

/* Unsecure LPUART1 */
TZSC_PRIVCFGR1 &= ~(TZSC_PRIVCFG1_LPUARTPRIV);
GPIO_SECCFGR(GPIOG_BASE) &= ~(1<<UART1_TX_PIN);
GPIO_SECCFGR(GPIOG_BASE) &= ~(1<<UART1_RX_PIN);
}
#endif


#define OPTR_SWAP_BANK (1 << 20)
Expand Down Expand Up @@ -336,20 +364,6 @@ void RAMFUNCTION hal_flash_dualbank_swap(void)
stm32l5_reboot();
}

static void led_unsecure()
{
uint32_t pin;

/*Enable clock for User LED GPIOs */
RCC_AHB2_CLOCK_ER|= GPIOD_AHB2_CLOCK_ER;
RCC_AHB2_CLOCK_ER|= GPIOG_AHB2_CLOCK_ER;
PWR_CR2 |= PWR_CR2_IOSV;

/*Un-secure User LED GPIO pins */
GPIOD_SECCFGR&=~(1<<LED_USR_PIN);
GPIOG_SECCFGR&=~(1<<LED_BOOT_PIN);

}

#if defined(DUALBANK_SWAP) && defined(__WOLFBOOT)
static uint8_t bootloader_copy_mem[BOOTLOADER_SIZE];
Expand Down Expand Up @@ -391,7 +405,7 @@ void hal_prepare_boot(void)
{
clock_pll_off();
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
led_unsecure();
periph_unsecure();
#endif
}

41 changes: 34 additions & 7 deletions hal/stm32l5.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
#define RCC_APB2ENR (*(volatile uint32_t *)(RCC_BASE + 0x60))
#define RCC_APB2ENR_SYSCFGEN (1 << 0)

#define RCC_CCIPR1 (*(volatile uint32_t *)(RCC_BASE + 0x88))
#define RCC_CCIPR1_LPUART1SEL_SHIFT (10)
#define RCC_CCIPR1_LPUART1SEL_MASK (0x3)


#define RCC_CRRCR (*(volatile uint32_t *)(RCC_BASE + 0x98))
#define RCC_CRRCR_HSI48ON (1 << 0)
#define RCC_CRRCR_HSI48RDY (1 << 1)
Expand Down Expand Up @@ -170,6 +175,8 @@
#define FLASH_NS_SR (*(volatile uint32_t *)(FLASH_NS_BASE + 0x20))
#define FLASH_NS_CR (*(volatile uint32_t *)(FLASH_NS_BASE + 0x28))

#define TZSC_PRIVCFGR1 *((uint32_t *)(0x500032020))
#define TZSC_PRIVCFG1_LPUARTPRIV (1 << 21)


#else
Expand Down Expand Up @@ -228,19 +235,39 @@
#define FLASH_OPTKEY2 (0x4C5D6E7FU)

/* GPIO*/
#define GPIOA_BASE 0x52020000
#define GPIOB_BASE 0x52020400
#define GPIOC_BASE 0x52020800
#define GPIOD_BASE 0x52020C00
#define GPIOG_BASE 0x52021800

#define GPIOD_SECCFGR (*(volatile uint32_t *)(GPIOD_BASE + 0x30))
#define GPIOG_SECCFGR (*(volatile uint32_t *)(GPIOG_BASE + 0x30))

#define LED_BOOT_PIN (12) //PG12 - Discovery - Green Led
#define LED_USR_PIN (3) //PD3 - Discovery - Red Led

#define RCC_AHB2_CLOCK_ER (*(volatile uint32_t *)(RCC_BASE + 0x4C ))
#define GPIOG_AHB2_CLOCK_ER (1 << 6)
#define GPIOA_AHB2_CLOCK_ER (1 << 0)
#define GPIOB_AHB2_CLOCK_ER (1 << 1)
#define GPIOC_AHB2_CLOCK_ER (1 << 2)
#define GPIOD_AHB2_CLOCK_ER (1 << 3)
#define GPIOG_AHB2_CLOCK_ER (1 << 6)
#define TRNG_AHB2_CLOCK_ER (1 << 18)

#define RCC_APB1_CLOCK_ER (*(volatile uint32_t *)(RCC_BASE + 0x5C ))
#define UART1_APB1_CLOCK_ER_VAL (1 << 0)

#define UART1_PIN_AF 8
#define UART1_RX_PIN 8
#define UART1_TX_PIN 7

#define GPIO_SECCFGR(base) (*(volatile uint32_t *)(base + 0x30))

#ifdef STM32_DISCOVERY
#define LED_AHB2_ENABLE (GPIOD_AHB2_CLOCK_ER | GPIOG_AHB2_CLOCK_ER)
#define LED_BOOT_PIN (12) /* PG12 - Discovery - Green Led */
#define LED_USR_PIN (3) /* PD3 - Discovery - Red Led */
#else
#define LED_AHB2_ENABLE (GPIOA_AHB2_CLOCK_ER | GPIOB_AHB2_CLOCK_ER | \
GPIOC_AHB2_CLOCK_ER)
#define LED_BOOT_PIN (9) /* PA9 - Nucleo board - Red Led */
#define LED_USR_PIN (7) /* PC7 - Nucleo board - Green Led */
#define LED_EXTRA_PIN (7) /* PB7 - Nucleo board - Blue Led */
#endif

#endif /* STM32L5_DEF_INCLUDED */
158 changes: 158 additions & 0 deletions hal/uart/uart_drv_stm32l5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* uart_drv_stm32l5.c
*
* Driver for the back-end of the UART_FLASH module.
*
* Example implementation for stm32L5 Nucleo
* using LPUART1 (VCS port through USB).
*
*
* Copyright (C) 2023 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

#include <stdint.h>
#include "hal/stm32l5.h"

#define UART1 (0x50008000) /* Using LPUART1 */
#define UART1_CR1 (*(volatile uint32_t *)(UART1 + 0x00))
#define UART1_CR2 (*(volatile uint32_t *)(UART1 + 0x04))
#define UART1_CR3 (*(volatile uint32_t *)(UART1 + 0x08))
#define UART1_BRR (*(volatile uint32_t *)(UART1 + 0x0c))
#define UART1_ISR (*(volatile uint32_t *)(UART1 + 0x1c))
#define UART1_ICR (*(volatile uint32_t *)(UART1 + 0x20))
#define UART1_RDR (*(volatile uint32_t *)(UART1 + 0x24))
#define UART1_TDR (*(volatile uint32_t *)(UART1 + 0x28))

#define UART_CR1_UART_ENABLE (1 << 0)
#define UART_CR1_SYMBOL_LEN (1 << 12)
#define UART_CR1_PARITY_ENABLED (1 << 10)
#define UART_CR1_OVER8 (1 << 15)
#define UART_CR1_PARITY_ODD (1 << 9)
#define UART_CR1_TX_ENABLE (1 << 3)
#define UART_CR1_RX_ENABLE (1 << 2)
#define UART_CR2_STOPBITS (3 << 12)
#define UART_CR2_LINEN (1 << 14)
#define UART_CR2_CLKEN (1 << 11)
#define UART_CR3_HDSEL (1 << 3)
#define UART_CR3_SCEN (1 << 5)
#define UART_CR3_IREN (1 << 1)
#define UART_ISR_TX_EMPTY (1 << 7)
#define UART_ISR_RX_NOTEMPTY (1 << 5)

#define GPIOG_MODE (*(volatile uint32_t *)(GPIOG_BASE + 0x00))
#define GPIOG_OTYPE (*(volatile uint32_t *)(GPIOG_BASE + 0x04))
#define GPIOG_OSPD (*(volatile uint32_t *)(GPIOG_BASE + 0x08))
#define GPIOG_PUPD (*(volatile uint32_t *)(GPIOG_BASE + 0x0c))
#define GPIOG_ODR (*(volatile uint32_t *)(GPIOG_BASE + 0x14))
#define GPIOG_BSRR (*(volatile uint32_t *)(GPIOG_BASE + 0x18))
#define GPIOG_AFL (*(volatile uint32_t *)(GPIOG_BASE + 0x20))
#define GPIOG_AFH (*(volatile uint32_t *)(GPIOG_BASE + 0x24))

#define GPIO_MODE_AF (2)

#define CPU_FREQ (110000000)

static void uart1_pins_setup(void)
{
uint32_t reg;
/* Set mode = AF */
reg = GPIOG_MODE & ~ (0x03 << (UART1_RX_PIN * 2));
GPIOG_MODE = reg | (2 << (UART1_RX_PIN * 2));
reg = GPIOG_MODE & ~ (0x03 << (UART1_TX_PIN * 2));
GPIOG_MODE = reg | (2 << (UART1_TX_PIN * 2));

/* Alternate function: use low pins (2 and 3) */
reg = GPIOG_AFL & ~(0xf << (UART1_TX_PIN * 4));
GPIOG_AFL = reg | (UART1_PIN_AF << (UART1_TX_PIN * 4));
reg = GPIOG_AFH & ~(0xf << ((UART1_RX_PIN - 8) * 4));
GPIOG_AFH = reg | (UART1_PIN_AF << ((UART1_RX_PIN - 8) * 4));

}

int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
{
uint32_t reg;
/* Enable pins and configure for AF */
uart1_pins_setup();

reg = RCC_CCIPR1 & (~ (RCC_CCIPR1_LPUART1SEL_MASK << RCC_CCIPR1_LPUART1SEL_SHIFT));
RCC_CCIPR1 = reg | (1 << RCC_CCIPR1_LPUART1SEL_SHIFT);

/* Enable 16-bit oversampling */
UART1_CR1 &= (~UART_CR1_OVER8);

/* Configure clock */
UART1_BRR |= (uint16_t)(CPU_FREQ / bitrate);

/* Configure data bits */
if (data == 8)
UART1_CR1 &= ~UART_CR1_SYMBOL_LEN;
else
UART1_CR1 |= UART_CR1_SYMBOL_LEN;

/* Configure parity */
switch (parity) {
case 'O':
UART1_CR1 |= UART_CR1_PARITY_ODD;
/* fall through to enable parity */
/* FALL THROUGH */
case 'E':
UART1_CR1 |= UART_CR1_PARITY_ENABLED;
break;
default:
UART1_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD);
}
/* Set stop bits */
reg = UART1_CR2 & ~UART_CR2_STOPBITS;
if (stop > 1)
UART1_CR2 = reg & (2 << 12);
else
UART1_CR2 = reg;

/* Clear flags for async mode */
UART1_CR2 &= ~(UART_CR2_LINEN | UART_CR2_CLKEN);
UART1_CR3 &= ~(UART_CR3_SCEN | UART_CR3_HDSEL | UART_CR3_IREN);

/* Configure for RX+TX, turn on. */
UART1_CR1 |= UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE | UART_CR1_UART_ENABLE;

return 0;
}

int uart_tx(const uint8_t c)
{
volatile uint32_t reg;
do {
reg = UART1_ISR;
} while ((reg & UART_ISR_TX_EMPTY) == 0);
UART1_TDR = c;
return 1;
}

int uart_rx(uint8_t *c, int len)
{
volatile uint32_t reg;
int i = 0;
reg = UART1_ISR;
if (reg & UART_ISR_RX_NOTEMPTY) {
*c = (uint8_t)UART1_RDR;
return 1;
}
return 0;
}

6 changes: 1 addition & 5 deletions hal/uart/uart_drv_stm32wb.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@

#define CLOCK_SPEED (64000000) /* 64 MHz (STM32WB55) */

#define APB2_CLOCK_ER (*(volatile uint32_t *)(0x58000060))
#define UART1_APB2_CLOCK_ER_VAL (1 << 14)

#define AHB2_CLOCK_ER (*(volatile uint32_t *)(0x5800004c))
#define GPIOB_AHB2_CLOCK_ER (1 << 1)
#define GPIOB_BASE 0x48000400
Expand Down Expand Up @@ -105,8 +102,7 @@ int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
uint32_t reg;
/* Enable pins and configure for AF7 */
uart_pins_setup();
/* Turn on the device */
APB2_CLOCK_ER |= UART1_APB2_CLOCK_ER_VAL;

UART1_CR1 &= ~(UART_CR1_UART_ENABLE);
UART1_CR1 &= ~(UART_CR1_FIFO_ENABLE);

Expand Down
2 changes: 2 additions & 0 deletions test-app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ ifeq ($(TARGET),stm32l5)
endif
CFLAGS+=-mcpu=cortex-m33
LDFLAGS+=-mcpu=cortex-m33
CFLAGS+=-I..
APP_OBJS+=../hal/uart/uart_drv_$(UART_TARGET).o
endif

ifeq ($(TARGET),stm32u5)
Expand Down
Loading

0 comments on commit 994b1c0

Please sign in to comment.