2017-07-05 18:17:16 +08:00
|
|
|
/*
|
|
|
|
* File : board.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
2018-05-16 23:58:59 +08:00
|
|
|
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
2017-07-05 18:17:16 +08:00
|
|
|
*
|
2017-11-20 19:37:05 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2017-07-05 18:17:16 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2018-05-16 23:58:59 +08:00
|
|
|
* 2018-05-14 ZYH first implementation
|
2017-07-05 18:17:16 +08:00
|
|
|
*/
|
2018-08-06 14:07:48 +08:00
|
|
|
|
2017-07-05 18:17:16 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include "board.h"
|
2018-08-06 14:07:48 +08:00
|
|
|
|
2018-05-16 23:58:59 +08:00
|
|
|
#ifdef BSP_USING_HSI
|
|
|
|
#error Can not using HSI on this bsp
|
|
|
|
#endif
|
2017-07-05 18:17:16 +08:00
|
|
|
|
2018-05-16 23:58:59 +08:00
|
|
|
static void SystemClock_Config(void)
|
2017-07-05 18:17:16 +08:00
|
|
|
{
|
2018-08-06 14:07:48 +08:00
|
|
|
RCC_OscInitTypeDef RCC_OscInitStruct;
|
|
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
|
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInit;
|
2017-07-05 18:17:16 +08:00
|
|
|
|
2018-08-06 14:07:48 +08:00
|
|
|
/**Initializes the CPU, AHB and APB busses clocks
|
2018-05-16 23:58:59 +08:00
|
|
|
*/
|
2018-08-06 14:07:48 +08:00
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_MSI;
|
|
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
|
|
|
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
|
|
|
|
RCC_OscInitStruct.MSICalibrationValue = 0;
|
|
|
|
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
|
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
|
|
|
|
RCC_OscInitStruct.PLL.PLLM = 1;
|
|
|
|
RCC_OscInitStruct.PLL.PLLN = 40;
|
|
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
|
|
|
|
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
|
|
|
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
|
|
|
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
|
|
|
|
|
|
|
/**Initializes the CPU, AHB and APB busses clocks
|
2018-05-16 23:58:59 +08:00
|
|
|
*/
|
2018-08-06 14:07:48 +08:00
|
|
|
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
|
|
|
|
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
|
|
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
|
|
|
|
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
|
|
|
|
|
|
|
|
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2
|
|
|
|
| RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_UART4
|
|
|
|
| RCC_PERIPHCLK_UART5 | RCC_PERIPHCLK_LPUART1
|
|
|
|
| RCC_PERIPHCLK_USB;
|
|
|
|
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
|
|
|
|
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
|
|
|
|
PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
|
|
|
|
PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
|
|
|
|
PeriphClkInit.Uart5ClockSelection = RCC_UART5CLKSOURCE_PCLK1;
|
|
|
|
PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
|
|
|
|
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
|
|
|
|
PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
|
|
|
|
PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
|
|
|
|
PeriphClkInit.PLLSAI1.PLLSAI1N = 24;
|
|
|
|
PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
|
|
|
|
PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
|
|
|
|
PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
|
|
|
|
PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK;
|
|
|
|
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
|
|
|
|
|
|
|
|
/**Configure the main internal regulator output voltage
|
2018-05-16 23:58:59 +08:00
|
|
|
*/
|
2018-08-06 14:07:48 +08:00
|
|
|
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
|
2017-07-05 18:17:16 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysTick_Handler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
rt_tick_increase();
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
|
|
|
|
{
|
2018-05-16 23:58:59 +08:00
|
|
|
/**Configure the Systick interrupt time
|
|
|
|
*/
|
|
|
|
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
|
|
|
|
|
|
|
|
/**Configure the Systick
|
|
|
|
*/
|
|
|
|
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
|
|
|
|
|
|
|
|
/* SysTick_IRQn interrupt configuration */
|
|
|
|
HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
|
2017-07-05 18:17:16 +08:00
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t HAL_GetTick(void)
|
|
|
|
{
|
2018-05-16 23:58:59 +08:00
|
|
|
return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_Delay(__IO uint32_t Delay)
|
|
|
|
{
|
|
|
|
rt_thread_delay(Delay * 1000 / RT_TICK_PER_SECOND);
|
2017-07-05 18:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_SuspendTick(void)
|
|
|
|
{
|
2018-05-16 23:58:59 +08:00
|
|
|
/* we should not suspend tick */
|
2017-07-05 18:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_ResumeTick(void)
|
|
|
|
{
|
2018-05-16 23:58:59 +08:00
|
|
|
/* we should not resume tick */
|
2017-07-05 18:17:16 +08:00
|
|
|
}
|
|
|
|
|
2018-05-16 23:58:59 +08:00
|
|
|
void HAL_MspInit(void)
|
2017-07-05 18:17:16 +08:00
|
|
|
{
|
2018-05-16 23:58:59 +08:00
|
|
|
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
|
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
|
|
|
|
|
|
|
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
|
|
|
|
|
|
|
|
/* System interrupt init*/
|
|
|
|
/* MemoryManagement_IRQn interrupt configuration */
|
|
|
|
HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
|
|
|
|
/* BusFault_IRQn interrupt configuration */
|
|
|
|
HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
|
|
|
|
/* UsageFault_IRQn interrupt configuration */
|
|
|
|
HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
|
|
|
|
/* SVCall_IRQn interrupt configuration */
|
|
|
|
HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
|
|
|
|
/* DebugMonitor_IRQn interrupt configuration */
|
|
|
|
HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
|
|
|
|
/* PendSV_IRQn interrupt configuration */
|
|
|
|
HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
|
2017-07-05 18:17:16 +08:00
|
|
|
}
|
2018-08-06 14:07:48 +08:00
|
|
|
|
2017-07-05 18:17:16 +08:00
|
|
|
/**
|
|
|
|
* This function will initial STM32 board.
|
|
|
|
*/
|
2018-05-16 23:58:59 +08:00
|
|
|
void rt_hw_board_init(void)
|
2017-07-05 18:17:16 +08:00
|
|
|
{
|
2018-05-16 23:58:59 +08:00
|
|
|
/* Configure the system clock @ 80 Mhz */
|
2017-07-05 18:17:16 +08:00
|
|
|
SystemClock_Config();
|
2018-05-16 23:58:59 +08:00
|
|
|
HAL_Init();
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
|
|
|
|
#endif
|
2017-07-05 18:17:16 +08:00
|
|
|
#ifdef RT_USING_COMPONENTS_INIT
|
|
|
|
rt_components_board_init();
|
|
|
|
#endif
|
2017-11-20 19:37:05 +08:00
|
|
|
#ifdef RT_USING_CONSOLE
|
2018-05-16 23:58:59 +08:00
|
|
|
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
|
2017-11-20 19:37:05 +08:00
|
|
|
#endif
|
2017-07-05 18:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*@}*/
|