From 01ee477b02e6aa58a3f9f5704c29dc3554f69bee Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Mon, 16 Nov 2020 11:24:52 +0100 Subject: [PATCH] Workaround for an STM32F405 bug causing a delay between an RCC peripheral clock enable and the effective peripheral enabling, as described in ST's silicon errata sheet --- platform/drivers/ADC/ADC1_MDxx380.c | 4 +++- platform/drivers/display/HX83XX_MDxx380.c | 5 +++++ platform/drivers/tones/toneGenerator_MDxx380.c | 3 +++ platform/mcu/STM32F4xx/boot/startup.c | 1 + platform/mcu/STM32F4xx/drivers/rtc.c | 2 ++ platform/mcu/STM32F4xx/drivers/usb/usb_bsp.c | 1 + platform/targets/MD-380/platform.c | 3 +++ platform/targets/MD-390/platform.c | 3 +++ platform/targets/MD-UV380/platform.c | 3 +++ 9 files changed, 24 insertions(+), 1 deletion(-) diff --git a/platform/drivers/ADC/ADC1_MDxx380.c b/platform/drivers/ADC/ADC1_MDxx380.c index 336c76d0..edf2f627 100644 --- a/platform/drivers/ADC/ADC1_MDxx380.c +++ b/platform/drivers/ADC/ADC1_MDxx380.c @@ -25,6 +25,7 @@ void adc1_init() { RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN; + __DSB(); /* * Configure GPIOs to analog input mode: @@ -100,8 +101,9 @@ void adc1_init() void adc1_terminate() { DMA2_Stream0->CR &= ~DMA_SxCR_EN; - ADC1->CR2 &= ADC_CR2_ADON; + ADC1->CR2 &= ~ADC_CR2_ADON; RCC->APB2ENR &= ~RCC_APB2ENR_ADC1EN; + __DSB(); } float adc1_getMeasurement(uint8_t ch) diff --git a/platform/drivers/display/HX83XX_MDxx380.c b/platform/drivers/display/HX83XX_MDxx380.c index 203aa8ce..e76b597f 100644 --- a/platform/drivers/display/HX83XX_MDxx380.c +++ b/platform/drivers/display/HX83XX_MDxx380.c @@ -132,6 +132,8 @@ void display_init() * framebuffer content to the screen without using CPU. */ RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN; + __DSB(); + NVIC_ClearPendingIRQ(DMA2_Stream7_IRQn); NVIC_SetPriority(DMA2_Stream7_IRQn, 14); NVIC_EnableIRQ(DMA2_Stream7_IRQn); @@ -141,6 +143,7 @@ void display_init() * lines. */ RCC->AHB3ENR |= RCC_AHB3ENR_FSMCEN; + __DSB(); /* Configure FSMC as LCD driver. * BCR1 config: @@ -331,6 +334,8 @@ void display_terminate() { /* Shut off FSMC and deallocate framebuffer */ RCC->AHB3ENR &= ~RCC_AHB3ENR_FSMCEN; + __DSB(); + if(frameBuffer != NULL) { free(frameBuffer); diff --git a/platform/drivers/tones/toneGenerator_MDxx380.c b/platform/drivers/tones/toneGenerator_MDxx380.c index e907ba6d..3be8ab5c 100644 --- a/platform/drivers/tones/toneGenerator_MDxx380.c +++ b/platform/drivers/tones/toneGenerator_MDxx380.c @@ -106,6 +106,7 @@ void toneGen_init() * - Nominal update rate is 16.384kHz -> error = +22.25Hz */ RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; + __DSB(); TIM3->ARR = 0xFF; TIM3->PSC = 9; @@ -125,6 +126,8 @@ void toneGen_init() void toneGen_shutdown() { RCC->APB1ENR &= ~RCC_APB1ENR_TIM3EN; + __DSB(); + gpio_setMode(CTCSS_OUT, INPUT); gpio_setMode(BEEP_OUT, INPUT); } diff --git a/platform/mcu/STM32F4xx/boot/startup.c b/platform/mcu/STM32F4xx/boot/startup.c index 801b4bd9..f21599bc 100644 --- a/platform/mcu/STM32F4xx/boot/startup.c +++ b/platform/mcu/STM32F4xx/boot/startup.c @@ -59,6 +59,7 @@ void Reset_Handler() | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOEEN; + __DSB(); // Configure all GPIO pins to fast speed mode (50MHz) GPIOA->OSPEEDR = 0xAAAAAAAA; diff --git a/platform/mcu/STM32F4xx/drivers/rtc.c b/platform/mcu/STM32F4xx/drivers/rtc.c index 7418a616..0f5234d4 100644 --- a/platform/mcu/STM32F4xx/drivers/rtc.c +++ b/platform/mcu/STM32F4xx/drivers/rtc.c @@ -25,6 +25,8 @@ void rtc_init() { /* Enable write protection for RTC registers */ RCC->APB1ENR |= RCC_APB1ENR_PWREN; + __DSB(); + PWR->CR |= PWR_CR_DBP; RTC->WPR = 0xCA; RTC->WPR = 0x53; diff --git a/platform/mcu/STM32F4xx/drivers/usb/usb_bsp.c b/platform/mcu/STM32F4xx/drivers/usb/usb_bsp.c index 5d26b850..82141835 100644 --- a/platform/mcu/STM32F4xx/drivers/usb/usb_bsp.c +++ b/platform/mcu/STM32F4xx/drivers/usb/usb_bsp.c @@ -58,6 +58,7 @@ void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; + __DSB(); } /** diff --git a/platform/targets/MD-380/platform.c b/platform/targets/MD-380/platform.c index aecc9bd7..4ebb41ea 100644 --- a/platform/targets/MD-380/platform.c +++ b/platform/targets/MD-380/platform.c @@ -52,6 +52,8 @@ void platform_init() * Backlight pin is connected to TIM8 CR1. */ RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; + __DSB(); + TIM8->ARR = 255; TIM8->PSC = 327; TIM8->CNT = 0; @@ -78,6 +80,7 @@ void platform_terminate() /* Shut down timer */ RCC->APB2ENR &= ~RCC_APB2ENR_TIM8EN; + __DSB(); /* Shut down ADC */ adc1_terminate(); diff --git a/platform/targets/MD-390/platform.c b/platform/targets/MD-390/platform.c index aecc9bd7..4ebb41ea 100644 --- a/platform/targets/MD-390/platform.c +++ b/platform/targets/MD-390/platform.c @@ -52,6 +52,8 @@ void platform_init() * Backlight pin is connected to TIM8 CR1. */ RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; + __DSB(); + TIM8->ARR = 255; TIM8->PSC = 327; TIM8->CNT = 0; @@ -78,6 +80,7 @@ void platform_terminate() /* Shut down timer */ RCC->APB2ENR &= ~RCC_APB2ENR_TIM8EN; + __DSB(); /* Shut down ADC */ adc1_terminate(); diff --git a/platform/targets/MD-UV380/platform.c b/platform/targets/MD-UV380/platform.c index 0f21249e..2b486b76 100644 --- a/platform/targets/MD-UV380/platform.c +++ b/platform/targets/MD-UV380/platform.c @@ -72,6 +72,8 @@ void platform_init() * With ARR = 256, Fpwm is 256Hz; */ RCC->APB2ENR |= RCC_APB2ENR_TIM11EN; + __DSB(); + TIM11->ARR = 255; TIM11->PSC = 1282; TIM11->CNT = 0; @@ -100,6 +102,7 @@ void platform_terminate() #ifdef ENABLE_BKLIGHT_DIMMING RCC->APB2ENR &= ~RCC_APB2ENR_TIM11EN; + __DSB(); #endif /* Shut down LEDs */