]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F411RE/hal_tick.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F4 / TARGET_MTS_MDOT_F411RE / hal_tick.c
1 /**
2   ******************************************************************************
3   * @file    hal_tick.c
4   * @author  MCD Application Team
5   * @brief   Initialization of HAL tick
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
10   *
11   * Redistribution and use in source and binary forms, with or without modification,
12   * are permitted provided that the following conditions are met:
13   *   1. Redistributions of source code must retain the above copyright notice,
14   *      this list of conditions and the following disclaimer.
15   *   2. Redistributions in binary form must reproduce the above copyright notice,
16   *      this list of conditions and the following disclaimer in the documentation
17   *      and/or other materials provided with the distribution.
18   *   3. Neither the name of STMicroelectronics nor the names of its contributors
19   *      may be used to endorse or promote products derived from this software
20   *      without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   *
33   ******************************************************************************
34   */
35 #include "hal_tick.h"
36
37 TIM_HandleTypeDef TimMasterHandle;
38 uint32_t PreviousVal = 0;
39
40 void us_ticker_irq_handler(void);
41
42 void timer_irq_handler(void) {
43     // Channel 1 for mbed timeout
44     if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
45         us_ticker_irq_handler();
46     }
47
48     // Channel 2 for HAL tick
49     if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC2) == SET) {
50         __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
51         uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
52         if ((val - PreviousVal) >= HAL_TICK_DELAY) {
53             // Increment HAL variable
54             HAL_IncTick();
55             // Prepare next interrupt
56             __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
57             PreviousVal = val;
58 #if 0 // For DEBUG only
59             HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
60 #endif
61         }
62     }
63 }
64
65 // Reconfigure the HAL tick using a standard timer instead of systick.
66 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
67     // Enable timer clock
68     TIM_MST_RCC;
69
70     // Reset timer
71     TIM_MST_RESET_ON;
72     TIM_MST_RESET_OFF;
73
74     // Update the SystemCoreClock variable
75     SystemCoreClockUpdate();
76
77     // Configure time base
78     TimMasterHandle.Instance = TIM_MST;
79     TimMasterHandle.Init.Period            = 0xFFFFFFFF;
80     TimMasterHandle.Init.Prescaler         = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
81     TimMasterHandle.Init.ClockDivision     = 0;
82     TimMasterHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
83     TimMasterHandle.Init.RepetitionCounter = 0;
84     HAL_TIM_OC_Init(&TimMasterHandle);
85
86     NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
87     NVIC_EnableIRQ(TIM_MST_IRQ);
88
89     // Channel 1 for mbed timeout
90     HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
91
92     // Channel 2 for HAL tick
93     HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
94     PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
95     __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
96     __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
97
98 #if 0 // For DEBUG only
99     __GPIOB_CLK_ENABLE();
100     GPIO_InitTypeDef GPIO_InitStruct;
101     GPIO_InitStruct.Pin = GPIO_PIN_6;
102     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
103     GPIO_InitStruct.Pull = GPIO_PULLUP;
104     GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
105     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
106 #endif
107
108     return HAL_OK;
109 }
110
111 /**
112   * @}
113   */
114
115 /**
116   * @}
117   */
118   
119 /**
120   * @}
121   */    
122 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/