2021-08-20 16:37:50 +08:00
|
|
|
/**************************************************************************//**
|
|
|
|
* @file system_fm33lc0xx.c
|
|
|
|
* @brief CMSIS Cortex-M0 Device Peripheral Access Layer Source File for
|
|
|
|
* Device FM33LC0XX
|
2022-07-26 10:11:12 +08:00
|
|
|
* @version V2.0.0
|
|
|
|
* @date 15. Mar 2021
|
2021-08-20 16:37:50 +08:00
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
/* Copyright (c) 2012 ARM LIMITED
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
- Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
- Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
- Neither the name of ARM nor the names of its contributors may be used
|
|
|
|
to endorse or promote products derived from this software without
|
|
|
|
specific prior written permission.
|
|
|
|
*
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "system_fm33lc0xx.h"
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* Clock Variable definitions ------------------------------------------------*/
|
|
|
|
uint32_t XTLFClock = XTLF_DEFAULT_VALUE; /*!< External Low-freq Osc Clock Frequency (XTLF) */
|
|
|
|
uint32_t XTHFClock = XTHF_DEFAULT_VALUE; /*!< External High-freq Osc Clock Frequency (XTHF) */
|
|
|
|
uint32_t SystemCoreClock = HCLK_DEFAULT_VALUE; /*!< System Clock Frequency (Core Clock) */
|
|
|
|
|
|
|
|
/* Clock functions -----------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* @brief Retrieve the PLL clock frequency
|
|
|
|
*
|
|
|
|
* @retval PLL clock frequency
|
|
|
|
*/
|
2021-08-20 16:37:50 +08:00
|
|
|
static uint32_t SystemPLLClockUpdate(void)
|
|
|
|
{
|
|
|
|
uint32_t clock = 0;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* Acquire PLL clock source */
|
2021-08-20 16:37:50 +08:00
|
|
|
switch ((RCC->PLLCR >> 1) & 0x1)
|
|
|
|
{
|
|
|
|
case 0:
|
2022-07-26 10:11:12 +08:00
|
|
|
switch ((RCC->RCHFCR >> 16) & 0xFU)
|
2021-08-20 16:37:50 +08:00
|
|
|
{
|
2022-07-26 10:11:12 +08:00
|
|
|
case 1: /* 16MHz */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock = 16000000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 2: /* 24MHz */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock = 24000000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 0: /* 8MHz */
|
2021-08-20 16:37:50 +08:00
|
|
|
default:
|
|
|
|
clock = 8000000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
2021-08-20 16:37:50 +08:00
|
|
|
case 1:
|
2022-07-26 10:11:12 +08:00
|
|
|
clock = XTHFClock;
|
2021-08-20 16:37:50 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* Acquire PLL prescaler */
|
2021-08-20 16:37:50 +08:00
|
|
|
switch ((RCC->PLLCR >> 0x4) & 0x7)
|
|
|
|
{
|
2022-07-26 10:11:12 +08:00
|
|
|
case 0: /* input divided by 1 */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock /= 1;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 1: /* input divided by 2 */
|
|
|
|
clock /= 2;
|
2021-08-20 16:37:50 +08:00
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 2: /* input divided by 4 */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock /= 4;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 3: /* input divided by 8 */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock /= 8;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 4: /* input divided by 12 */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock /= 12;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 5: /* input divided by 16 */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock /= 16;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 6: /* input divided by 24 */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock /= 24;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 7: /* input divided by 32 */
|
2021-08-20 16:37:50 +08:00
|
|
|
clock /= 32;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* Acquire PLL multiplier and calculate PLL frequency */
|
|
|
|
clock = clock * (((RCC->PLLCR >> 16) & 0x7F) + 1);
|
|
|
|
|
|
|
|
/* Acquire PLL output channel(PLLx1 or PLLx2) */
|
2021-08-20 16:37:50 +08:00
|
|
|
if ((RCC->PLLCR >> 3) & 0x1)
|
|
|
|
{
|
|
|
|
clock *= 2;
|
|
|
|
}
|
2022-07-26 10:11:12 +08:00
|
|
|
|
2021-08-20 16:37:50 +08:00
|
|
|
return clock;
|
|
|
|
}
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Update the core clock frequency variable: SystemCoreClock
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void SystemCoreClockUpdate(void)
|
2021-08-20 16:37:50 +08:00
|
|
|
{
|
|
|
|
switch ((RCC->SYSCLKCR >> 0) & 0x7)
|
2022-07-26 10:11:12 +08:00
|
|
|
{
|
|
|
|
case 1: /* XTHF */
|
|
|
|
SystemCoreClock = XTHFClock;
|
2021-08-20 16:37:50 +08:00
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 2: /* PLL */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = SystemPLLClockUpdate();
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 4: /* RCMF */
|
2021-08-20 16:37:50 +08:00
|
|
|
switch ((RCC->RCMFCR >> 16) & 0x3)
|
|
|
|
{
|
2022-07-26 10:11:12 +08:00
|
|
|
case 0: /* output divided by 1 */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 4000000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 1: /* output divided by 4 */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 1000000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 2: /* output divided by 8 */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 500000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 3: /* output divided by 16 */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 250000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 5: /* LSCLK */
|
|
|
|
#ifdef USE_LSCLK_CLOCK_SRC_LPOSC
|
|
|
|
SystemCoreClock = 32000;
|
|
|
|
#else
|
|
|
|
SystemCoreClock = XTLFClock;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6: /* LPOSC */
|
|
|
|
SystemCoreClock = 32000;
|
2021-08-20 16:37:50 +08:00
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 7: /* USBBCK */
|
2021-08-20 16:37:50 +08:00
|
|
|
switch ((RCC->SYSCLKCR >> 3) & 0x1)
|
|
|
|
{
|
2022-07-26 10:11:12 +08:00
|
|
|
case 0: /* USBBCK 48MHz */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 48000000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 1: /* USBBCK 120MHz/2 */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 60000000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
2021-08-20 16:37:50 +08:00
|
|
|
default:
|
|
|
|
switch ((RCC->RCHFCR >> 16) & 0xf)
|
|
|
|
{
|
2022-07-26 10:11:12 +08:00
|
|
|
case 1: /* 16MHz */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 16000000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 2: /* 24MHz */
|
2021-08-20 16:37:50 +08:00
|
|
|
SystemCoreClock = 24000000;
|
|
|
|
break;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
case 0: /* 8MHz */
|
2021-08-20 16:37:50 +08:00
|
|
|
default:
|
|
|
|
SystemCoreClock = 8000000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Setup the microcontroller system.
|
|
|
|
* Initialize the System.
|
|
|
|
*/
|
2022-07-26 10:11:12 +08:00
|
|
|
void SystemInit(void)
|
2021-08-20 16:37:50 +08:00
|
|
|
{
|
2022-07-26 10:11:12 +08:00
|
|
|
#if !defined(MFANG) && defined(USE_LSCLK_CLOCK_SRC_XTLF)
|
2021-08-20 16:37:50 +08:00
|
|
|
uint32_t temp;
|
2022-07-26 10:11:12 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_IWDT_ON_STARTUP)
|
|
|
|
RCC->PCLKCR1 |= 0x20U; /* Enable IWDT Operation Clock */
|
|
|
|
IWDT->CR = IWDT_OVERFLOW_PERIOD; /* Configure IWDT overflow period */
|
|
|
|
IWDT->SERV = 0x12345A5AU; /* Enable IWDT */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Reset PLL & SYSCLK selection */
|
|
|
|
RCC->PLLCR = 0x00000000U;
|
|
|
|
RCC->SYSCLKCR = 0x0A000000U;
|
|
|
|
|
|
|
|
/* Enable PAD Operation Clock */
|
|
|
|
RCC->PCLKCR1 |= (0x1U << 7);
|
|
|
|
|
|
|
|
#ifndef MFANG /* MFANG handles clock configurations by itself */
|
|
|
|
#ifdef USE_LSCLK_CLOCK_SRC_XTLF
|
|
|
|
|
|
|
|
/* XTLF IO configuration */
|
|
|
|
GPIOD->FCR |= 0x003C0000U;
|
|
|
|
|
|
|
|
/* Enable XTLF */
|
|
|
|
RCC->XTLFCR = 0x00000000U;
|
|
|
|
RCC->XTLFCR |= (uint32_t)(0x5U << 8);
|
|
|
|
for(temp = 2000U; temp > 0U; temp--);
|
|
|
|
|
|
|
|
#ifdef USE_LSCLK_AUTO_SWITCH
|
|
|
|
|
|
|
|
/* Enable LSCLK auto switch */
|
2021-08-20 16:37:50 +08:00
|
|
|
RCC->SYSCLKCR |= 0x8000000U;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* LSCLK from XTLF */
|
|
|
|
RCC->LSCLKSEL = 0xAAU;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* Disable LSCLK auto switch */
|
|
|
|
CMU->SYSCLKCR &= 0x7FFFFFFU;
|
|
|
|
|
|
|
|
/* LSCLK from XTLF */
|
|
|
|
CMU->LSCLKSEL = 0xAAU;
|
|
|
|
|
|
|
|
#endif /* USE_LSCLK_AUTO_SWITCH */
|
2021-08-20 16:37:50 +08:00
|
|
|
#else
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* Disable LSCLK auto switch */
|
2021-08-20 16:37:50 +08:00
|
|
|
RCC->SYSCLKCR &= 0x7FFFFFFU;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* LSCLK from LPOSC */
|
|
|
|
RCC->LSCLKSEL = 0x55U;
|
|
|
|
|
|
|
|
#endif /* USE_LSCLK_CLOCK_SRC_XTLF */
|
|
|
|
#endif /* MFANG */
|
|
|
|
|
|
|
|
/* PDR & BOR Configuration */
|
|
|
|
RMU->PDRCR = 0x1U;
|
|
|
|
RMU->BORCR = 0xEU;
|
|
|
|
|
|
|
|
/* Disable IWDT & WWDT, enable other peripherals(e.g. timers) under Debug Mode */
|
|
|
|
DBG->CR = 0x3U;
|
|
|
|
|
|
|
|
/* Load clock trim value */
|
|
|
|
RCC->RCHFTR = RCHF8M_TRIM;
|
2021-08-20 16:37:50 +08:00
|
|
|
RCC->RCMFTR = RCMF4M_TRIM;
|
|
|
|
RCC->LPOSCTR = LPOSC_TRIM;
|
2022-07-26 10:11:12 +08:00
|
|
|
|
|
|
|
/* Enable SWD port pull up */
|
|
|
|
GPIOD->PUEN |= (0x3U << 7U);
|
|
|
|
|
2021-08-20 16:37:50 +08:00
|
|
|
/* DMA Flash Channel: Flash->RAM */
|
2022-07-26 10:11:12 +08:00
|
|
|
RCC->PCLKCR2 |= (0x1U << 4U);
|
|
|
|
DMA->CH7CR |= (0x1U << 10U);
|
|
|
|
RCC->PCLKCR2 &= ~(0x1U << 4U);
|
|
|
|
|
|
|
|
/* Update System Core Clock */
|
|
|
|
SystemCoreClockUpdate();
|
|
|
|
|
|
|
|
#if defined(USE_IWDT_ON_STARTUP)
|
|
|
|
IWDT->SERV = 0x12345A5AU; /* Feed IWDT */
|
|
|
|
#endif
|
|
|
|
}
|
2021-08-20 16:37:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|