]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/hal_tick.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F0 / TARGET_NUCLEO_F070RB / 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 void set_compare(uint16_t count);
42
43 extern volatile uint32_t SlaveCounter;
44 extern volatile uint32_t oc_int_part;
45 extern volatile uint16_t oc_rem_part;
46
47 // Used to increment the slave counter
48 void timer_update_irq_handler(void)
49 {
50 TimMasterHandle.Instance = TIM_MST;
51
52 // Clear Update interrupt flag
53 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
54 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE);
55 SlaveCounter++;
56 }
57 }
58
59 // Used for mbed timeout (channel 1) and HAL tick (channel 2)
60 void timer_oc_irq_handler(void)
61 {
62 uint16_t cval = TIM_MST->CNT;
63 TimMasterHandle.Instance = TIM_MST;
64
65 // Channel 1 for mbed timeout
66 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
67 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
68 if (oc_rem_part > 0) {
69 set_compare(oc_rem_part); // Finish the remaining time left
70 oc_rem_part = 0;
71 } else {
72 if (oc_int_part > 0) {
73 set_compare(0xFFFF);
74 oc_rem_part = cval; // To finish the counter loop the next time
75 oc_int_part--;
76 } else {
77 us_ticker_irq_handler();
78 }
79 }
80 }
81
82 // Channel 2 for HAL tick
83 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
84 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC2);
85 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
86 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
87 // Increment HAL variable
88 HAL_IncTick();
89 // Prepare next interrupt
90 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
91 PreviousVal = val;
92 #if 1 // For DEBUG only
93 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
94 #endif
95 }
96 }
97 }
98
99 // Reconfigure the HAL tick using a standard timer instead of systick.
100 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
101 // Enable timer clock
102 TIM_MST_RCC;
103
104 // Reset timer
105 TIM_MST_RESET_ON;
106 TIM_MST_RESET_OFF;
107
108 // Update the SystemCoreClock variable
109 SystemCoreClockUpdate();
110
111 // Configure time base
112 TimMasterHandle.Instance = TIM_MST;
113 TimMasterHandle.Init.Period = 0xFFFF;
114 TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
115 TimMasterHandle.Init.ClockDivision = 0;
116 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
117 HAL_TIM_Base_Init(&TimMasterHandle);
118
119 // Configure output compare channel 1 for mbed timeout (enabled later when used)
120 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
121
122 // Configure output compare channel 2 for HAL tick
123 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
124 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
125 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
126
127 // Configure interrupts
128 // Update interrupt used for 32-bit counter
129 // Output compare channel 1 interrupt for mbed timeout
130 // Output compare channel 2 interrupt for HAL tick
131 NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler);
132 NVIC_EnableIRQ(TIM_MST_UP_IRQ);
133 NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler);
134 NVIC_EnableIRQ(TIM_MST_OC_IRQ);
135
136 // Enable interrupts
137 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter
138 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick
139
140 // Enable timer
141 HAL_TIM_Base_Start(&TimMasterHandle);
142
143 #if 1 // For DEBUG only
144 __GPIOB_CLK_ENABLE();
145 GPIO_InitTypeDef GPIO_InitStruct;
146 GPIO_InitStruct.Pin = GPIO_PIN_6;
147 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
148 GPIO_InitStruct.Pull = GPIO_PULLUP;
149 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
150 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
151 #endif
152
153 return HAL_OK;
154 }
155
156 /**
157 * @}
158 */
159
160 /**
161 * @}
162 */
163
164 /**
165 * @}
166 */
167 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum