2021-03-19 10:05:24 -07:00
|
|
|
/*
|
|
|
|
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file STM32F1xx/hal_lld.c
|
|
|
|
* @brief STM32F1xx HAL subsystem low level driver source.
|
|
|
|
*
|
|
|
|
* @addtogroup HAL
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CMSIS system core clock variable.
|
|
|
|
* @note It is declared in system_stm32f10x.h.
|
|
|
|
*/
|
2021-03-20 11:47:59 -07:00
|
|
|
uint32_t SystemCoreClock = GD32_HCLK;
|
2021-03-19 10:05:24 -07:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes the backup domain.
|
|
|
|
* @note WARNING! Changing clock source impossible without resetting
|
|
|
|
* of the whole BKP domain.
|
|
|
|
*/
|
|
|
|
static void hal_lld_backup_domain_init(void) {
|
|
|
|
|
|
|
|
/* Backup domain access enabled and left open.*/
|
|
|
|
PWR->CR |= PWR_CR_DBP;
|
|
|
|
|
|
|
|
#if HAL_USE_RTC
|
|
|
|
/* Reset BKP domain if different clock source selected.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
if ((RCC->BDCR & GD32_RTCSEL_MASK) != GD32_RTCSEL) {
|
2021-03-19 10:05:24 -07:00
|
|
|
/* Backup domain reset.*/
|
|
|
|
RCC->BDCR = RCC_BDCR_BDRST;
|
|
|
|
RCC->BDCR = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If enabled then the LSE is started.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_LSE_ENABLED
|
|
|
|
#if defined(GD32_LSE_BYPASS)
|
2021-03-19 10:05:24 -07:00
|
|
|
/* LSE Bypass.*/
|
|
|
|
RCC->BDCR |= RCC_BDCR_LSEON | RCC_BDCR_LSEBYP;
|
|
|
|
#else
|
|
|
|
/* No LSE Bypass.*/
|
|
|
|
RCC->BDCR |= RCC_BDCR_LSEON;
|
|
|
|
#endif
|
|
|
|
while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0)
|
|
|
|
; /* Waits until LSE is stable. */
|
2021-03-20 11:47:59 -07:00
|
|
|
#endif /* GD32_LSE_ENABLED */
|
2021-03-19 10:05:24 -07:00
|
|
|
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_RTCSEL != GD32_RTCSEL_NOCLOCK
|
2021-03-19 10:05:24 -07:00
|
|
|
/* If the backup domain hasn't been initialized yet then proceed with
|
|
|
|
initialization.*/
|
|
|
|
if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) {
|
|
|
|
/* Selects clock source.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
RCC->BDCR |= GD32_RTCSEL;
|
2021-03-19 10:05:24 -07:00
|
|
|
|
|
|
|
/* Prescaler value loaded in registers.*/
|
|
|
|
rtc_lld_set_prescaler();
|
|
|
|
|
|
|
|
/* RTC clock enabled.*/
|
|
|
|
RCC->BDCR |= RCC_BDCR_RTCEN;
|
|
|
|
}
|
2021-03-20 11:47:59 -07:00
|
|
|
#endif /* GD32_RTCSEL != GD32_RTCSEL_NOCLOCK */
|
2021-03-19 10:05:24 -07:00
|
|
|
#endif /* HAL_USE_RTC */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2021-03-20 11:47:59 -07:00
|
|
|
#if defined(GD32_DMA_REQUIRED) || defined(__DOXYGEN__)
|
2021-03-20 15:47:28 -07:00
|
|
|
#if defined(GD32_DMA1_CH35_HANDLER) || defined(__DOXYGEN__)
|
2021-03-19 10:05:24 -07:00
|
|
|
/**
|
2021-03-20 15:47:28 -07:00
|
|
|
* @brief DMA1 streams 4 and 5 shared ISR.
|
2021-03-19 10:05:24 -07:00
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
2021-03-20 15:47:28 -07:00
|
|
|
OSAL_IRQ_HANDLER(GD32_DMA1_CH35_HANDLER) {
|
2021-03-19 10:05:24 -07:00
|
|
|
|
|
|
|
OSAL_IRQ_PROLOGUE();
|
|
|
|
|
2021-03-20 15:47:28 -07:00
|
|
|
/* Check on channel 4 of DMA1.*/
|
|
|
|
dmaServeInterrupt(GD32_DMA1_STREAM3);
|
2021-03-19 10:05:24 -07:00
|
|
|
|
2021-03-20 15:47:28 -07:00
|
|
|
/* Check on channel 5 of DMA1.*/
|
|
|
|
dmaServeInterrupt(GD32_DMA1_STREAM4);
|
2021-03-19 10:05:24 -07:00
|
|
|
|
|
|
|
OSAL_IRQ_EPILOGUE();
|
|
|
|
}
|
2021-03-20 15:47:28 -07:00
|
|
|
#endif /* defined(GD32_DMA1_CH35_HANDLER) */
|
2021-03-20 11:47:59 -07:00
|
|
|
#endif /* defined(GD32_DMA_REQUIRED) */
|
2021-03-19 10:05:24 -07:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Low level HAL driver initialization.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void hal_lld_init(void) {
|
|
|
|
|
|
|
|
/* Reset of all peripherals.*/
|
|
|
|
rccResetAPB1(0xFFFFFFFF);
|
|
|
|
rccResetAPB2(0xFFFFFFFF);
|
|
|
|
|
|
|
|
/* PWR and BD clocks enabled.*/
|
|
|
|
rccEnablePWRInterface(true);
|
|
|
|
rccEnableBKPInterface(true);
|
|
|
|
|
|
|
|
/* Initializes the backup domain.*/
|
|
|
|
hal_lld_backup_domain_init();
|
|
|
|
|
|
|
|
/* DMA subsystems initialization.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
#if defined(GD32_DMA_REQUIRED)
|
2021-03-19 10:05:24 -07:00
|
|
|
dmaInit();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* IRQ subsystem initialization.*/
|
|
|
|
irqInit();
|
|
|
|
|
|
|
|
/* Programmable voltage detector enable.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_PVD_ENABLE
|
|
|
|
PWR->CR |= PWR_CR_PVDE | (GD32_PLS & GD32_PLS_MASK);
|
|
|
|
#endif /* GD32_PVD_ENABLE */
|
2021-03-19 10:05:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief STM32 clocks and PLL initialization.
|
|
|
|
* @note All the involved constants come from the file @p board.h.
|
|
|
|
* @note This function should be invoked just after the system reset.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(GD32VF103) //TODO whole family
|
|
|
|
/*
|
|
|
|
* Clocks initialization for the CL sub-family.
|
|
|
|
*/
|
|
|
|
void gd32_clock_init(void) {
|
|
|
|
|
2021-03-20 11:47:59 -07:00
|
|
|
#if !GD32_NO_INIT
|
2021-03-19 10:05:24 -07:00
|
|
|
/* HSI setup, it enforces the reset situation in order to handle possible
|
|
|
|
problems with JTAG probes and re-initializations.*/
|
|
|
|
RCC->CR |= RCC_CR_HSION; /* Make sure HSI is ON. */
|
|
|
|
while (!(RCC->CR & RCC_CR_HSIRDY))
|
|
|
|
; /* Wait until HSI is stable. */
|
|
|
|
|
|
|
|
/* HSI is selected as new source without touching the other fields in
|
|
|
|
CFGR. Clearing the register has to be postponed after HSI is the
|
|
|
|
new source.*/
|
|
|
|
RCC->CFGR &= ~RCC_CFGR_SW; /* Reset SW, selecting HSI. */
|
|
|
|
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
|
|
|
|
; /* Wait until HSI is selected. */
|
|
|
|
|
|
|
|
/* Registers finally cleared to reset values.*/
|
|
|
|
RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value. */
|
|
|
|
RCC->CFGR = 0; /* CFGR reset value. */
|
|
|
|
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_HSE_ENABLED
|
|
|
|
#if defined(GD32_HSE_BYPASS)
|
2021-03-19 10:05:24 -07:00
|
|
|
/* HSE Bypass.*/
|
|
|
|
RCC->CR |= RCC_CR_HSEBYP;
|
|
|
|
#endif
|
|
|
|
/* HSE activation.*/
|
|
|
|
RCC->CR |= RCC_CR_HSEON;
|
|
|
|
while (!(RCC->CR & RCC_CR_HSERDY))
|
|
|
|
; /* Waits until HSE is stable. */
|
|
|
|
#endif
|
|
|
|
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_LSI_ENABLED
|
2021-03-19 10:05:24 -07:00
|
|
|
/* LSI activation.*/
|
|
|
|
RCC->CSR |= RCC_CSR_LSION;
|
|
|
|
while ((RCC->CSR & RCC_CSR_LSIRDY) == 0)
|
|
|
|
; /* Waits until LSI is stable. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Settings of various dividers and multipliers in CFGR2.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
/*RCC->CFGR2 = GD32_PLL3MUL | GD32_PLL2MUL | GD32_PREDIV2 |
|
|
|
|
GD32_PREDIV1 | GD32_PREDIV1SRC;*/
|
2021-03-19 10:05:24 -07:00
|
|
|
|
|
|
|
/* PLL2 setup, if activated.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_ACTIVATE_PLL2
|
2021-03-19 10:05:24 -07:00
|
|
|
RCC->CR |= RCC_CR_PLL2ON;
|
|
|
|
while (!(RCC->CR & RCC_CR_PLL2RDY))
|
|
|
|
; /* Waits until PLL2 is stable. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* PLL3 setup, if activated.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_ACTIVATE_PLL3
|
2021-03-19 10:05:24 -07:00
|
|
|
RCC->CR |= RCC_CR_PLL3ON;
|
|
|
|
while (!(RCC->CR & RCC_CR_PLL3RDY))
|
|
|
|
; /* Waits until PLL3 is stable. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* PLL1 setup, if activated.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
//#if GD32_ACTIVATE_PLL1
|
|
|
|
#if GD32_ACTIVATE_PLL
|
|
|
|
RCC->CFGR |= GD32_PLLMUL | GD32_PLLSRC;
|
2021-03-19 10:05:24 -07:00
|
|
|
RCC->CR |= RCC_CR_PLLON;
|
|
|
|
while (!(RCC->CR & RCC_CR_PLLRDY))
|
|
|
|
; /* Waits until PLL1 is stable. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Clock settings.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
#if GD32_HAS_OTG1
|
|
|
|
RCC->CFGR = GD32_MCOSEL | GD32_USBPRE | GD32_PLLMUL | GD32_PLLSRC |
|
|
|
|
GD32_ADCPRE | GD32_PPRE2 | GD32_PPRE1 | GD32_HPRE;
|
2021-03-19 10:05:24 -07:00
|
|
|
#else
|
2021-03-20 11:47:59 -07:00
|
|
|
RCC->CFGR = GD32_MCO | GD32_PLLMUL | GD32_PLLSRC |
|
|
|
|
GD32_ADCPRE | GD32_PPRE2 | GD32_PPRE1 | GD32_HPRE;
|
2021-03-19 10:05:24 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Flash setup and final clock selection. */
|
2021-03-20 11:47:59 -07:00
|
|
|
FLASH->ACR = GD32_FLASHBITS; /* Flash wait states depending on clock. */
|
2021-03-19 10:05:24 -07:00
|
|
|
while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) !=
|
2021-03-20 11:47:59 -07:00
|
|
|
(GD32_FLASHBITS & FLASH_ACR_LATENCY_Msk)) {
|
2021-03-19 10:05:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Switching to the configured clock source if it is different from HSI.*/
|
2021-03-20 11:47:59 -07:00
|
|
|
#if (GD32_SW != GD32_SW_HSI)
|
|
|
|
RCC->CFGR |= GD32_SW; /* Switches on the selected clock source. */
|
|
|
|
while ((RCC->CFGR & RCC_CFGR_SWS) != (GD32_SW << 2))
|
2021-03-19 10:05:24 -07:00
|
|
|
;
|
|
|
|
#endif
|
|
|
|
|
2021-03-20 11:47:59 -07:00
|
|
|
#if !GD32_HSI_ENABLED
|
2021-03-19 10:05:24 -07:00
|
|
|
RCC->CR &= ~RCC_CR_HSION;
|
|
|
|
#endif
|
2021-03-20 11:47:59 -07:00
|
|
|
#endif /* !GD32_NO_INIT */
|
2021-03-19 10:05:24 -07:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void gd32_clock_init(void) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** @} */
|