Skip to content

Commit

Permalink
Adds support for STM32L43x MCUs (#486)
Browse files Browse the repository at this point in the history
Adds support for the STM32L432 and STM32L431 MCUs.
Adds blink_raw for the nucleo-L432KC board.

- Adds stm32 cube L4 makefiles and path variables.
- Adds generic board definition for L4 MCUs including interrupt vector table.
- Adds compilation target for CubeL4 HAL library for the L432 and L431.
- Adds HwInit and board directory for nucleo-L432KC. This HwInit uses the internal RC clock  as using an external clock would need soldering on the nucleo board.
- Renames FLASH_SIZE constant to EEPROMEMU_FLASH_SIZE to resolve a symbol conflict with the L4 HAL library.
- Updates STM32 device drivers to support the L432 and L431 chips
- Reduces complexity in some of the ifdefs in the STM32 device drivers.

===

* Adds stm32F4 cube to path.mk

* Update F3 to F4 in cubef4.mk

* Adds cubeL4 into path and .mk.

* Adds interrupt vector table for STM32L4xx

* Adds hardware pin mapping for stm32L432KC
Adds clock setup routine.
Adds blinker timer routine.

* Adds template for HAL module configuration.

* Fixes a too common symbol in eepromemu driver.
This symbol conflicts with an STM32L4 HAL definition.

* Adds L4 HAL includes.

* Moves over definitions to L4 hal.h

* Adds a missing but required definition for default_handlers.

* updates forward declarationsfor handlers.

* Updates HwInit to fix compilation errors.

* Adjusts makefile for L4 includes.

* Adds memory map for L432KC.

* Adds missing symbols to cover for link errors.

* Updates openocd target cfg file.

* Adds l432 driver lib build.

* Adds necessary build symlinks.

* Adds L4 to stm32can.

* Fixes stm32gpio to not need to be range specific.

* Fixes stm32I2C to not be range specific.

* Fixes stm32SPI for L4.

* Fixes stm32uart for L4.

* Adds cube432 drivers to the target.

* Adds blink_raw target for nucleo 432.

* fix compile errors.

* fix compile error on master.

* line up comments.

* more alignment work.

* Adds l431 directory for HAL / freertos drivers.

* cleanup commented code

* Add 431

* Fix whitespace.
  • Loading branch information
balazsracz authored Dec 17, 2020
1 parent 07549df commit e89db63
Show file tree
Hide file tree
Showing 37 changed files with 877 additions and 527 deletions.
2 changes: 2 additions & 0 deletions boards/armv7m/default_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ extern unsigned long __data_section_table_end;
extern unsigned long __bss_section_table;
extern unsigned long __bss_section_table_end;

#define NVIC_INT_CTRL_R (*((volatile uint32_t *)0xE000ED04))

/** This hardware initialization code will be called before C++ global objects
* are initialized. */
extern void hw_preinit(void);
Expand Down
540 changes: 162 additions & 378 deletions boards/st-stm32l4-generic/startup.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions boards/st-stm32l4-generic/target.ld
113 changes: 63 additions & 50 deletions boards/st-stm32l432kc-nucleo/HwInit.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#include <new>
#include <cstdint>

#include "stm32f3xx_hal_conf.h"
#include "stm32f3xx_hal.h"
#include "stm32f_hal_conf.hxx"
#include "stm32l4xx_hal.h"

#include "os/OS.hxx"
#include "Stm32Uart.hxx"
Expand All @@ -61,7 +61,7 @@ static Stm32Uart uart0("/dev/ser0", USART2, USART2_IRQn);
static Stm32Can can0("/dev/can0");

/** EEPROM emulation driver. The file size might be made bigger. */
static Stm32EEPROMEmulation eeprom0("/dev/eeprom", 512);
//static Stm32EEPROMEmulation eeprom0("/dev/eeprom", 512);

const size_t EEPROMEmulation::SECTOR_SIZE = 2048;

Expand Down Expand Up @@ -94,13 +94,12 @@ void setblink(uint32_t pattern)
}


/// TIM17 shares this interrupt with certain features of timer1
void tim1_trg_com_interrupt_handler(void)
void tim7_interrupt_handler(void)
{
//
// Clear the timer interrupt.
//
TIM17->SR = ~TIM_IT_UPDATE;
TIM7->SR = ~TIM_IT_UPDATE;

// Set output LED.
BLINKER_RAW_Pin::set(rest_pattern & 1);
Expand All @@ -124,114 +123,128 @@ void diewith(uint32_t pattern)
}

/** CPU clock speed. */
const unsigned long cm3_cpu_clock_hz = 72000000;
const unsigned long cm3_cpu_clock_hz = 80000000;
uint32_t SystemCoreClock;
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
const uint32_t MSIRangeTable[12] = {100000U, 200000U, 400000U, 800000U,
1000000U, 2000000U, 4000000U, 8000000U, 16000000U, 24000000U, 32000000U,
48000000U};
const uint32_t HSEValue = 8000000;

/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 72000000
* HCLK(Hz) = 72000000
* System Clock source = PLL (MSI)
* SYSCLK(Hz) = 80000000
* HCLK(Hz) = 80000000
* AHB Prescaler = 1
* APB1 Prescaler = 2
* APB1 Prescaler = 1
* APB2 Prescaler = 1
* HSE Frequency(Hz) = 8000000
* HSE PREDIV = 1
* PLLMUL = 9
* Flash Latency(WS) = 2
* MSI Frequency(Hz) = 4000000
* PLL_M = 1
* PLL_N = 40
* PLL_R = 2
* PLL_P = 7
* PLL_Q = 4
* Flash Latency(WS) = 4
* @param None
* @retval None
*/
static void clock_setup(void)
{
HAL_RCC_DeInit();

RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;

/* Enable HSE Oscillator and activate PLL with HSE as source on bypass
* mode. This allows using the MCO clock output from the ST_Link part of
* the nucleo board and freeing up the other clock pin for GPIO. */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
/* MSI is enabled after System reset, activate PLL with MSI as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;

HAL_RCC_OscConfig(&RCC_OscInitStruct);

/* Select PLL as system clock source and configure the HCLK, PCLK1 and
* PCLK2 clocks dividers
*/
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLP = 7;
RCC_OscInitStruct.PLL.PLLQ = 4;
HASSERT(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK);

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

HASSERT(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) == HAL_OK);
HASSERT(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) == HAL_OK);

// This will fail if the clocks are somehow misconfigured.
HASSERT(SystemCoreClock == cm3_cpu_clock_hz);
}

/// We don't need the HAL tick configuration code to run. FreeRTOS will take
/// care of that.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
return HAL_OK;
}

/** Initialize the processor hardware.
*/
void hw_preinit(void)
{
/* Globally disables interrupts until the FreeRTOS scheduler is up. */
asm("cpsid i\n");

/* these FLASH settings enable opertion at 72 MHz */
/* these FLASH settings enable opertion at 80 MHz */
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_2);
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_4);

/* setup the system clock */
clock_setup();

/* enable peripheral clocks */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_CAN1_CLK_ENABLE();
__HAL_RCC_TIM17_CLK_ENABLE();
__HAL_RCC_TIM7_CLK_ENABLE();

/* setup pinmux */
GPIO_InitTypeDef gpio_init;
memset(&gpio_init, 0, sizeof(gpio_init));

/* USART2 pinmux on PA2 and PA3 */
/* USART2 pinmux on PA2 and PA15 */
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Pull = GPIO_PULLUP;
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init.Alternate = GPIO_AF7_USART2;
gpio_init.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOA, &gpio_init);
gpio_init.Pin = GPIO_PIN_3;
gpio_init.Pin = GPIO_PIN_15;
gpio_init.Alternate = GPIO_AF3_USART2;
HAL_GPIO_Init(GPIOA, &gpio_init);

/* CAN pinmux on PB8 and PB9 */
/* CAN pinmux on PA11 and PA12 */
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Pull = GPIO_PULLUP;
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init.Alternate = GPIO_AF9_CAN;
gpio_init.Pin = GPIO_PIN_8;
HAL_GPIO_Init(GPIOB, &gpio_init);
gpio_init.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOB, &gpio_init);
gpio_init.Alternate = GPIO_AF9_CAN1;
gpio_init.Pin = GPIO_PIN_11;
HAL_GPIO_Init(GPIOA, &gpio_init);
gpio_init.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOA, &gpio_init);

GpioInit::hw_init();

/* Initializes the blinker timer. */
TIM_HandleTypeDef TimHandle;
memset(&TimHandle, 0, sizeof(TimHandle));
TimHandle.Instance = TIM17;
TimHandle.Instance = TIM7;
TimHandle.Init.Period = configCPU_CLOCK_HZ / 10000 / 8;
TimHandle.Init.Prescaler = 10000;
TimHandle.Init.ClockDivision = 0;
Expand All @@ -247,9 +260,9 @@ void hw_preinit(void)
/* Starting Error */
HASSERT(0);
}
__HAL_DBGMCU_FREEZE_TIM17();
SetInterruptPriority(TIM17_IRQn, 0);
NVIC_EnableIRQ(TIM17_IRQn);
__HAL_DBGMCU_FREEZE_TIM7();
SetInterruptPriority(TIM7_IRQn, 0);
NVIC_EnableIRQ(TIM7_IRQn);
}

void usart2_interrupt_handler(void)
Expand Down
12 changes: 6 additions & 6 deletions boards/st-stm32l432kc-nucleo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ sh -c "if [ \"X`printenv OPENMRNPATH`\" != \"X\" ]; then printenv OPENMRNPATH; \
else echo OPENMRNPATH not found; fi" \
)

# Find STM32CubeF3 libraries
include $(OPENMRNPATH)/etc/stm32cubef3.mk
# Find STM32CubeL4 libraries
include $(OPENMRNPATH)/etc/stm32cubel4.mk

LDFLAGSEXTRA +=
SYSLIBRARIESEXTRA += -lfreertos_drivers_stm32cubef303xe
SYSLIBRARIESEXTRA += -lfreertos_drivers_stm32cubel432xx
OBJEXTRA +=

CFLAGS += -DSTM32F303xE
CXXFLAGS += -DSTM32F303xE
OPENOCDARGS = -f board/st_nucleo_f3.cfg
CFLAGS += -DSTM32L432xx
CXXFLAGS += -DSTM32L432xx
OPENOCDARGS = -f board/st_nucleo_l476rg.cfg

ifndef TARGET
export TARGET := freertos.armv7m
Expand Down
6 changes: 2 additions & 4 deletions boards/st-stm32l432kc-nucleo/hardware.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
#include "utils/GpioInitializer.hxx"
#include "BlinkerGPIO.hxx"

GPIO_PIN(LED_GREEN_RAW, LedPin, A, 5);
GPIO_PIN(LED_GREEN_RAW, LedPin, B, 3);

GPIO_PIN(SW_USER, GpioInputPU, C, 13);

typedef GpioInitializer<LED_GREEN_RAW_Pin,SW_USER_Pin> GpioInit;
typedef GpioInitializer<LED_GREEN_RAW_Pin> GpioInit;

typedef LED_GREEN_RAW_Pin BLINKER_RAW_Pin;
typedef BLINKER_Pin LED_GREEN_Pin;
6 changes: 3 additions & 3 deletions boards/st-stm32l432kc-nucleo/memory_map.ld
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 488K
EEPROMEMU (r) : ORIGIN = 0x0807A000, LENGTH = 16K
BOOTLOADER (rx) : ORIGIN = 0x0807E000, LENGTH = 8K
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 232K
EEPROMEMU (r) : ORIGIN = 0x0803A000, LENGTH = 16K
BOOTLOADER (rx) : ORIGIN = 0x0803E000, LENGTH = 8K
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64K
}

Expand Down
1 change: 1 addition & 0 deletions boards/st-stm32l432kc-nucleo/startup.c
1 change: 1 addition & 0 deletions boards/st-stm32l432kc-nucleo/target.ld
2 changes: 2 additions & 0 deletions boards/ti-ek-tm4c123gxl-launchpad/HwInit.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ struct DccHwDefs {
* '1' bit */
static int dcc_preamble_count() { return 16; }

static bool generate_railcom_halfzero() { return false; }

static void flip_led() {}

/** the time (in nanoseconds) to wait between turning off the low driver and
Expand Down
22 changes: 22 additions & 0 deletions etc/path.mk
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ STM32CUBEF3PATH:=$(TRYPATH)
endif
endif #STM32CUBEF3PATH

################ STM32Cube_F4 ##################
ifndef STM32CUBEF4PATH
SEARCHPATH := \
/opt/st/STM32Cube_FW_F4/default

TRYPATH:=$(call findfirst,Drivers,$(SEARCHPATH))
ifneq ($(TRYPATH),)
STM32CUBEF4PATH:=$(TRYPATH)
endif
endif #STM32CUBEF4PATH

################ STM32Cube_L4 ##################
ifndef STM32CUBEL4PATH
SEARCHPATH := \
/opt/st/STM32Cube_FW_L4/default

TRYPATH:=$(call findfirst,Drivers,$(SEARCHPATH))
ifneq ($(TRYPATH),)
STM32CUBEL4PATH:=$(TRYPATH)
endif
endif #STM32CUBEL4PATH

################ STM32Cube_F7 ##################
ifndef STM32CUBEF7PATH
SEARCHPATH := \
Expand Down
10 changes: 5 additions & 5 deletions etc/stm32cubef4.mk
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
include $(OPENMRNPATH)/etc/path.mk

ifdef STM32CUBEF3PATH
ifdef STM32CUBEF4PATH
INCLUDES += -I$(OPENMRNPATH)/src/freertos_drivers/st \
-I$(STM32CUBEF3PATH)/Drivers/STM32F3xx_HAL_Driver/Inc \
-I$(STM32CUBEF3PATH)/Drivers/CMSIS/Device/ST/STM32F3xx/Include \
-I$(STM32CUBEF3PATH)/Drivers/CMSIS/Include
-I$(STM32CUBEF4PATH)/Drivers/STM32F4xx_HAL_Driver/Inc \
-I$(STM32CUBEF4PATH)/Drivers/CMSIS/Device/ST/STM32F4xx/Include \
-I$(STM32CUBEF4PATH)/Drivers/CMSIS/Include
endif

CFLAGS +=
CXXFLAGS +=

DEPS += STM32CUBEF3PATH
DEPS += STM32CUBEF4PATH
10 changes: 5 additions & 5 deletions etc/stm32cubel4.mk
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
include $(OPENMRNPATH)/etc/path.mk

ifdef STM32CUBEF3PATH
ifdef STM32CUBEL4PATH
INCLUDES += -I$(OPENMRNPATH)/src/freertos_drivers/st \
-I$(STM32CUBEF3PATH)/Drivers/STM32F3xx_HAL_Driver/Inc \
-I$(STM32CUBEF3PATH)/Drivers/CMSIS/Device/ST/STM32F3xx/Include \
-I$(STM32CUBEF3PATH)/Drivers/CMSIS/Include
-I$(STM32CUBEL4PATH)/Drivers/STM32L4xx_HAL_Driver/Inc \
-I$(STM32CUBEL4PATH)/Drivers/CMSIS/Device/ST/STM32L4xx/Include \
-I$(STM32CUBEL4PATH)/Drivers/CMSIS/Include
endif

CFLAGS +=
CXXFLAGS +=

DEPS += STM32CUBEF3PATH
DEPS += STM32CUBEL4PATH
4 changes: 2 additions & 2 deletions src/freertos_drivers/common/EEPROMEmulation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ EEPROMEmulation::EEPROMEmulation(const char *name, size_t file_size)
: EEPROM(name, file_size)
{
/* make sure we have an appropriate sized region of memory for our device */
HASSERT(FLASH_SIZE >= (2 * SECTOR_SIZE)); // at least two of them
HASSERT((FLASH_SIZE % SECTOR_SIZE) == 0); // and nothing remaining
HASSERT(EEPROMEMU_FLASH_SIZE >= (2 * SECTOR_SIZE)); // at least two of them
HASSERT((EEPROMEMU_FLASH_SIZE % SECTOR_SIZE) == 0); // and nothing remaining
HASSERT(file_size <= (SECTOR_SIZE >> 1)); // single block fit all the data
HASSERT(file_size <= (1024 * 64 - 2)); // uint16 indexes, 0xffff reserved
HASSERT(BLOCK_SIZE >= 4); // we don't support block sizes less than 4 bytes
Expand Down
Loading

0 comments on commit e89db63

Please sign in to comment.