2 ******************************************************************************
4 * @author MCD Application Team
5 * @brief Initialization of HAL tick
6 ******************************************************************************
9 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
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.
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.
33 ******************************************************************************
37 TIM_HandleTypeDef TimMasterHandle;
38 uint32_t PreviousVal = 0;
40 void us_ticker_irq_handler(void);
41 void set_compare(uint16_t count);
43 extern volatile uint32_t SlaveCounter;
44 extern volatile uint32_t oc_int_part;
45 extern volatile uint16_t oc_rem_part;
47 void timer_irq_handler(void) {
48 uint16_t cval = TIM_MST->CNT;
50 TimMasterHandle.Instance = TIM_MST;
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);
58 // Channel 1 for mbed timeout
59 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
60 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
61 if (oc_rem_part > 0) {
62 set_compare(oc_rem_part); // Finish the remaining time left
65 if (oc_int_part > 0) {
67 oc_rem_part = cval; // To finish the counter loop the next time
70 us_ticker_irq_handler();
75 // Channel 2 for HAL tick
76 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
77 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC2);
78 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
79 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
80 // Increment HAL variable
82 // Prepare next interrupt
83 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
89 // Reconfigure the HAL tick using a standard timer instead of systick.
90 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
98 // Update the SystemCoreClock variable
99 SystemCoreClockUpdate();
101 // Configure time base
102 TimMasterHandle.Instance = TIM_MST;
103 TimMasterHandle.Init.Period = 0xFFFFFFFF;
104 TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
105 TimMasterHandle.Init.ClockDivision = 0;
106 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
107 HAL_TIM_Base_Init(&TimMasterHandle);
109 // Configure output compare channel 1 for mbed timeout (enabled later when used)
110 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
112 // Configure output compare channel 2 for HAL tick
113 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
114 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
115 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
117 // Configure interrupts
118 // Update interrupt used for 32-bit counter
119 // Output compare channel 1 interrupt for mbed timeout
120 // Output compare channel 2 interrupt for HAL tick
121 NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
122 NVIC_EnableIRQ(TIM_MST_IRQ);
125 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter
126 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick
129 HAL_TIM_Base_Start(&TimMasterHandle);
145 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/