From 5a6910a45b89bf3681d0a6230f9f23fced52ee1c Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Thu, 11 Apr 2019 11:08:17 +0900 Subject: [PATCH] Adding STM32L432 support for USART (not yet USB, ADC, Flash, etc.). --- board/board-st-nucleo-l432.h | 62 +++++++++++++++++------------------- mcu/clk_gpio_init-stm32l.c | 43 ++++++++++++++++++++++++- mcu/stm32l.h | 8 +++++ 3 files changed, 80 insertions(+), 33 deletions(-) diff --git a/board/board-st-nucleo-l432.h b/board/board-st-nucleo-l432.h index dad5947..0504a4f 100644 --- a/board/board-st-nucleo-l432.h +++ b/board/board-st-nucleo-l432.h @@ -2,7 +2,7 @@ #define BOARD_ID 0x3a8d5116 /* - * Please add USB cable to ST Nucleo L432. + * When using USB, please add USB cable to ST Nucleo L432. * * At CN4, connect USB cable, only when ST Link is not connected * Vbus RED --> 4 @@ -15,47 +15,45 @@ #define MCU_STM32L4 1 -#define RCC_HSICLK 48000000 - #define GPIO_LED_BASE GPIOB_BASE #define GPIO_LED_SET_TO_EMIT 3 #undef GPIO_USB_BASE /* No external DISCONNECT/RENUM circuit. */ -#define GPIO_OTHER_BASE GPIOB_BASE +#define GPIO_OTHER_BASE GPIOA_BASE /* * Port A setup. - * PA0 - Input with pull-up USART2-CTS - * PA1 - Alternate function push pull output 2MHz USART2-RTS - * PA2 - Alternate function push pull output 2MHz USART2-TX - * PA3 - Input with pull-up USART2-RX - * PA4 - Alternate function push pull output 2MHz USART2-CK - * PA5 - Push pull output 2MHz (LED 1:ON 0:OFF) - * PA11 - Push Pull output 10MHz 0 default (until USB enabled) (USBDM) - * PA12 - Push Pull output 10MHz 0 default (until USB enabled) (USBDP) + * + * MODER: 10 11 - 11 01 - 01 11 - 10 10 11 11 - 11 11 - 11 10 - 11 11 + * + * PA2 - USART2-TX: AF7 + * PA8 - USART1-CK: AF7 + * PA9 - USART1-TX: AF7 Open-drain pull-up + * PA11 - Push Pull output medium-speed 0 (until USB enabled) (USBDM: AF10) + * PA12 - Push Pull output medium-speed 0 (until USB enabled) (USBDP: AF10) + * PA15 - USART2-RX: AF3 * ------------------------ Default - * PAx - input with pull-up + * PAx - analog input */ -#define VAL_GPIO_LED_ODR 0xFFFFE7FF +#define VAL_GPIO_LED_MODER 0xBD7AFFEF +#define VAL_GPIO_LED_OTYPER 0x00000200 +#define VAL_GPIO_LED_OSPEEDR 0xFB7FFFFF +#define VAL_GPIO_LED_PUPDR 0x00040000 + +#define VAL_GPIO_LED_AFRL 0x00000700 +#define VAL_GPIO_LED_AFRH 0x30000077 /* * Port B setup. - * PB0 - input with pull-up: AN8 for NeuG - * PB1 - input with pull-up: AN9 for NeuG - * --- - * --- - * PB4 - Input with pull-up: Card insertion detect: 0 when detected - * --- - * PB6 - Output push pull 2MHz: Vcc for card: default 0 - * --- - * PB8 - Output push pull 2MHz: Vpp for card: default 0 - * PB9 - Output push pull 2MHz: RST for card: default 0 - * PB10 - Alternate function open-drain output 50MHz USART3-TX - * PB11 - Input with pull-up USART3-RX - * PB12 - Alternate function push pull output 50MHz USART3-CK - * PB13 - Input with pull-up USART3-CTS - * PB14 - Alternate function push pull output 50MHz USART3-RTS - * --- + * + * MODER: 11 11 - 11 11 - 11 11 - 11 11 11 11 - 11 11 - 01 11 - 11 11 + * + * PB3 - ON (LED 1:ON 0:OFF) * ------------------------ Default - * PBx - input with pull-up. + * PAx - analog input */ -#define VAL_GPIO_OTHER_ODR 0xFFFFFCBF +#define VAL_GPIO_OTHER_MODER 0xFFFFFF7F +#define VAL_GPIO_OTHER_OTYPER 0x00000000 +#define VAL_GPIO_OTHER_OSPEEDR 0x00000000 +#define VAL_GPIO_OTHER_PUPDR 0x00000000 + +#define RCC_PHR_GPIO (RCC_PHR_GPIOA | RCC_PHR_GPIOB) diff --git a/mcu/clk_gpio_init-stm32l.c b/mcu/clk_gpio_init-stm32l.c index 600149e..850687e 100644 --- a/mcu/clk_gpio_init-stm32l.c +++ b/mcu/clk_gpio_init-stm32l.c @@ -28,9 +28,33 @@ #include +#define STM32_FLASHBITS 0x00000704 + static void __attribute__((used)) clock_init (void) { + /* MSI: 4MHz (keep the default value) */ + + /* PLL input: MSI at 4MHz, VCO: 160 MHz, output 80MHz */ + RCC->PLLCFGR = ((1 << 24) | (40 << 8)) | 0x01; + + /* Enable PLL */ + RCC->CR |= (1 << 24); + while (!(RCC->CR & (1 << 25))) + ; + + /* Flash setup: four wait states at 80MHz */ + FLASH->ACR = STM32_FLASHBITS; + while ((FLASH->ACR & 0x07) != (STM32_FLASHBITS & 0x07)) + ; + + /* Configure bus clocks: AHB: 80MHz, APB1: 40MHz, APB2: 80MHz */ + RCC->CFGR = (0x04 << 8); + + /* Switch SYSCLOCK using PLL */ + RCC->CFGR |= 0x03; + while ((RCC->CFGR & 0x0C) != 0x0C) + ; } static struct GPIO *const GPIO_LED = (struct GPIO *)GPIO_LED_BASE; @@ -45,7 +69,24 @@ static void __attribute__((used)) gpio_init (void) { /* Enable GPIO clock. */ + RCC->AHB2ENR |= RCC_IOP; + RCC->AHB2RSTR = RCC_IOP; + RCC->AHB2RSTR = 0; + + /* Delay (more than two clocks) is needed. */ + while (RCC->AHB2RSTR != 0) + ; /* LED is mandatory. We configure it always. */ - GPIO_LED->ODR = VAL_GPIO_LED_ODR; + GPIO_LED->OSPEEDR = VAL_GPIO_LED_OSPEEDR; + GPIO_LED->OTYPER = VAL_GPIO_LED_OTYPER; + GPIO_LED->MODER = VAL_GPIO_LED_MODER; + GPIO_LED->PUPDR = VAL_GPIO_LED_PUPDR; + +#ifdef GPIO_OTHER_BASE + GPIO_OTHER->OSPEEDR = VAL_GPIO_OTHER_OSPEEDR; + GPIO_OTHER->OTYPER = VAL_GPIO_OTHER_OTYPER; + GPIO_OTHER->MODER = VAL_GPIO_OTHER_MODER; + GPIO_OTHER->PUPDR = VAL_GPIO_OTHER_PUPDR; +#endif } diff --git a/mcu/stm32l.h b/mcu/stm32l.h index ece30d9..b5b8f85 100644 --- a/mcu/stm32l.h +++ b/mcu/stm32l.h @@ -59,6 +59,14 @@ struct RCC { #define RCC_BASE (AHB1PERIPH_BASE + 0x1000) static struct RCC *const RCC = (struct RCC *)RCC_BASE; +#define RCC_PHR_GPIOA 0x00000001 +#define RCC_PHR_GPIOB 0x00000002 +#define RCC_PHR_GPIOC 0x00000004 +#define RCC_PHR_GPIOD 0x00000008 +#define RCC_PHR_GPIOE 0x00000010 +#define RCC_PHR_GPIOH 0x00000080 + + struct PWR { volatile uint32_t CR1;