board.c 4.0 KB
Newer Older
lymzzyh's avatar
lymzzyh 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * File      : board.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2009 RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      first implementation
 * 2017-10-20     ZYH          emmm...setup for HAL Libraries
14
 * 2017-11-15     ZYH            update to 3.0.0
lymzzyh's avatar
lymzzyh 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
 */

#include <rthw.h>
#include <rtthread.h>
#include "board.h"

/**
 * @addtogroup STM32
 */

/*@{*/

void HAL_MspInit(void)
{
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
    /* System interrupt init*/
    __HAL_RCC_AFIO_CLK_ENABLE();
    /* 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);
    /* SysTick_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
    /**DISABLE: JTAG-DP Disabled and SW-DP Disabled**/
    __HAL_AFIO_REMAP_SWJ_NOJTAG();
lymzzyh's avatar
lymzzyh 已提交
48
}
lymzzyh's avatar
lymzzyh 已提交
49

lymzzyh's avatar
lymzzyh 已提交
50 51
void SystemClock_Config(void)
{
52
    rt_err_t result;
53 54 55 56 57 58 59 60 61 62 63
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    /**Initializes the CPU, AHB and APB busses clocks
      */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
64 65
    result = HAL_RCC_OscConfig(&RCC_OscInitStruct);
    RT_ASSERT(result == HAL_OK);
66 67 68 69 70 71 72
    /**Initializes the CPU, AHB and APB busses clocks
      */
    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_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
73 74
    result = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
    RT_ASSERT(result == HAL_OK);
75 76 77 78 79 80 81 82
    /**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);
lymzzyh's avatar
lymzzyh 已提交
83 84 85 86 87 88 89 90
}

/**
 * This is the timer interrupt service routine.
 *
 */
void SysTick_Handler(void)
{
91 92 93 94 95 96
    /* enter interrupt */
    rt_interrupt_enter();
    HAL_IncTick();
    rt_tick_increase();
    /* leave interrupt */
    rt_interrupt_leave();
lymzzyh's avatar
lymzzyh 已提交
97 98 99 100 101 102 103
}

/**
 * This function will initial STM32 board.
 */
void rt_hw_board_init(void)
{
104 105 106 107
    HAL_Init();
    SystemClock_Config();
#ifdef RT_USING_HEAP
    rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
lymzzyh's avatar
lymzzyh 已提交
108
#endif
109 110
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
lymzzyh's avatar
lymzzyh 已提交
111 112
#endif
#ifdef RT_USING_CONSOLE
113
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
lymzzyh's avatar
lymzzyh 已提交
114 115
#endif
}
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

/**
 * This function will delay for some us.
 *
 * @param us the delay time of us
 */
void rt_hw_us_delay(rt_uint32_t us)
{
    rt_uint32_t delta;
    us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));
    delta = SysTick->VAL;
    if (delta < us)
    {
        /* wait current OSTick left time gone */
        while (SysTick->VAL < us);
        us -= delta;
        delta = SysTick->LOAD;
    }
    while (delta - SysTick->VAL < us);
}