lol
Fork of Application by
mbed-dev/targets/TARGET_STM/hal_tick_16b.c@12:3a30cdffa27c, 2018-01-21 (annotated)
- Committer:
- danix
- Date:
- Sun Jan 21 22:28:30 2018 +0000
- Revision:
- 12:3a30cdffa27c
- Parent:
- 10:41552d038a69
Working acelerometer and mouse
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zaitsev | 10:41552d038a69 | 1 | /* mbed Microcontroller Library |
Zaitsev | 10:41552d038a69 | 2 | * Copyright (c) 2006-2016 ARM Limited |
Zaitsev | 10:41552d038a69 | 3 | * |
Zaitsev | 10:41552d038a69 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Zaitsev | 10:41552d038a69 | 5 | * you may not use this file except in compliance with the License. |
Zaitsev | 10:41552d038a69 | 6 | * You may obtain a copy of the License at |
Zaitsev | 10:41552d038a69 | 7 | * |
Zaitsev | 10:41552d038a69 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Zaitsev | 10:41552d038a69 | 9 | * |
Zaitsev | 10:41552d038a69 | 10 | * Unless required by applicable law or agreed to in writing, software |
Zaitsev | 10:41552d038a69 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Zaitsev | 10:41552d038a69 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Zaitsev | 10:41552d038a69 | 13 | * See the License for the specific language governing permissions and |
Zaitsev | 10:41552d038a69 | 14 | * limitations under the License. |
Zaitsev | 10:41552d038a69 | 15 | */ |
Zaitsev | 10:41552d038a69 | 16 | #include "hal_tick.h" |
Zaitsev | 10:41552d038a69 | 17 | |
Zaitsev | 10:41552d038a69 | 18 | // A 16-bit timer is used |
Zaitsev | 10:41552d038a69 | 19 | #if TIM_MST_16BIT |
Zaitsev | 10:41552d038a69 | 20 | |
Zaitsev | 10:41552d038a69 | 21 | #define DEBUG_TICK 0 // Set to 1 to toggle a pin (see below which pin) at each tick |
Zaitsev | 10:41552d038a69 | 22 | |
Zaitsev | 10:41552d038a69 | 23 | extern TIM_HandleTypeDef TimMasterHandle; |
Zaitsev | 10:41552d038a69 | 24 | |
Zaitsev | 10:41552d038a69 | 25 | extern volatile uint32_t SlaveCounter; |
Zaitsev | 10:41552d038a69 | 26 | extern volatile uint32_t oc_int_part; |
Zaitsev | 10:41552d038a69 | 27 | extern volatile uint16_t oc_rem_part; |
Zaitsev | 10:41552d038a69 | 28 | extern volatile uint8_t tim_it_update; |
Zaitsev | 10:41552d038a69 | 29 | extern volatile uint32_t tim_it_counter; |
Zaitsev | 10:41552d038a69 | 30 | |
Zaitsev | 10:41552d038a69 | 31 | volatile uint32_t PreviousVal = 0; |
Zaitsev | 10:41552d038a69 | 32 | |
Zaitsev | 10:41552d038a69 | 33 | void us_ticker_irq_handler(void); |
Zaitsev | 10:41552d038a69 | 34 | void set_compare(uint16_t count); |
Zaitsev | 10:41552d038a69 | 35 | |
Zaitsev | 10:41552d038a69 | 36 | #if defined(TARGET_STM32F0) |
Zaitsev | 10:41552d038a69 | 37 | void timer_update_irq_handler(void) { |
Zaitsev | 10:41552d038a69 | 38 | #else |
Zaitsev | 10:41552d038a69 | 39 | void timer_irq_handler(void) |
Zaitsev | 10:41552d038a69 | 40 | { |
Zaitsev | 10:41552d038a69 | 41 | #endif |
Zaitsev | 10:41552d038a69 | 42 | uint16_t cnt_val = TIM_MST->CNT; |
Zaitsev | 10:41552d038a69 | 43 | TimMasterHandle.Instance = TIM_MST; |
Zaitsev | 10:41552d038a69 | 44 | |
Zaitsev | 10:41552d038a69 | 45 | // Clear Update interrupt flag |
Zaitsev | 10:41552d038a69 | 46 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) { |
Zaitsev | 10:41552d038a69 | 47 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_UPDATE) == SET) { |
Zaitsev | 10:41552d038a69 | 48 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_UPDATE); |
Zaitsev | 10:41552d038a69 | 49 | SlaveCounter++; |
Zaitsev | 10:41552d038a69 | 50 | tim_it_counter = cnt_val + (uint32_t)(SlaveCounter << 16); |
Zaitsev | 10:41552d038a69 | 51 | tim_it_update = 1; |
Zaitsev | 10:41552d038a69 | 52 | } |
Zaitsev | 10:41552d038a69 | 53 | } |
Zaitsev | 10:41552d038a69 | 54 | |
Zaitsev | 10:41552d038a69 | 55 | #if defined(TARGET_STM32F0) |
Zaitsev | 10:41552d038a69 | 56 | } // end timer_update_irq_handler function |
Zaitsev | 10:41552d038a69 | 57 | // Used for mbed timeout (channel 1) and HAL tick (channel 2) |
Zaitsev | 10:41552d038a69 | 58 | void timer_oc_irq_handler(void) |
Zaitsev | 10:41552d038a69 | 59 | { |
Zaitsev | 10:41552d038a69 | 60 | uint16_t cnt_val = TIM_MST->CNT; |
Zaitsev | 10:41552d038a69 | 61 | TimMasterHandle.Instance = TIM_MST; |
Zaitsev | 10:41552d038a69 | 62 | #endif |
Zaitsev | 10:41552d038a69 | 63 | |
Zaitsev | 10:41552d038a69 | 64 | // Channel 1 for mbed timeout |
Zaitsev | 10:41552d038a69 | 65 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { |
Zaitsev | 10:41552d038a69 | 66 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { |
Zaitsev | 10:41552d038a69 | 67 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); |
Zaitsev | 10:41552d038a69 | 68 | if (oc_rem_part > 0) { |
Zaitsev | 10:41552d038a69 | 69 | set_compare(oc_rem_part); // Finish the remaining time left |
Zaitsev | 10:41552d038a69 | 70 | oc_rem_part = 0; |
Zaitsev | 10:41552d038a69 | 71 | } else { |
Zaitsev | 10:41552d038a69 | 72 | if (oc_int_part > 0) { |
Zaitsev | 10:41552d038a69 | 73 | set_compare(0xFFFF); |
Zaitsev | 10:41552d038a69 | 74 | oc_rem_part = cnt_val; // To finish the counter loop the next time |
Zaitsev | 10:41552d038a69 | 75 | oc_int_part--; |
Zaitsev | 10:41552d038a69 | 76 | } else { |
Zaitsev | 10:41552d038a69 | 77 | us_ticker_irq_handler(); |
Zaitsev | 10:41552d038a69 | 78 | } |
Zaitsev | 10:41552d038a69 | 79 | } |
Zaitsev | 10:41552d038a69 | 80 | } |
Zaitsev | 10:41552d038a69 | 81 | } |
Zaitsev | 10:41552d038a69 | 82 | |
Zaitsev | 10:41552d038a69 | 83 | // Channel 2 for HAL tick |
Zaitsev | 10:41552d038a69 | 84 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) { |
Zaitsev | 10:41552d038a69 | 85 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) { |
Zaitsev | 10:41552d038a69 | 86 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2); |
Zaitsev | 10:41552d038a69 | 87 | uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle); |
Zaitsev | 10:41552d038a69 | 88 | if ((val - PreviousVal) >= HAL_TICK_DELAY) { |
Zaitsev | 10:41552d038a69 | 89 | // Increment HAL variable |
Zaitsev | 10:41552d038a69 | 90 | HAL_IncTick(); |
Zaitsev | 10:41552d038a69 | 91 | // Prepare next interrupt |
Zaitsev | 10:41552d038a69 | 92 | __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); |
Zaitsev | 10:41552d038a69 | 93 | PreviousVal = val; |
Zaitsev | 10:41552d038a69 | 94 | #if DEBUG_TICK > 0 |
Zaitsev | 10:41552d038a69 | 95 | HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); |
Zaitsev | 10:41552d038a69 | 96 | #endif |
Zaitsev | 10:41552d038a69 | 97 | } |
Zaitsev | 10:41552d038a69 | 98 | } |
Zaitsev | 10:41552d038a69 | 99 | } |
Zaitsev | 10:41552d038a69 | 100 | } |
Zaitsev | 10:41552d038a69 | 101 | |
Zaitsev | 10:41552d038a69 | 102 | // Reconfigure the HAL tick using a standard timer instead of systick. |
Zaitsev | 10:41552d038a69 | 103 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) |
Zaitsev | 10:41552d038a69 | 104 | { |
Zaitsev | 10:41552d038a69 | 105 | // Enable timer clock |
Zaitsev | 10:41552d038a69 | 106 | TIM_MST_RCC; |
Zaitsev | 10:41552d038a69 | 107 | |
Zaitsev | 10:41552d038a69 | 108 | // Reset timer |
Zaitsev | 10:41552d038a69 | 109 | TIM_MST_RESET_ON; |
Zaitsev | 10:41552d038a69 | 110 | TIM_MST_RESET_OFF; |
Zaitsev | 10:41552d038a69 | 111 | |
Zaitsev | 10:41552d038a69 | 112 | // Update the SystemCoreClock variable |
Zaitsev | 10:41552d038a69 | 113 | SystemCoreClockUpdate(); |
Zaitsev | 10:41552d038a69 | 114 | |
Zaitsev | 10:41552d038a69 | 115 | // Configure time base |
Zaitsev | 10:41552d038a69 | 116 | TimMasterHandle.Instance = TIM_MST; |
Zaitsev | 10:41552d038a69 | 117 | TimMasterHandle.Init.Period = 0xFFFF; |
Zaitsev | 10:41552d038a69 | 118 | TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick |
Zaitsev | 10:41552d038a69 | 119 | TimMasterHandle.Init.ClockDivision = 0; |
Zaitsev | 10:41552d038a69 | 120 | TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
Zaitsev | 10:41552d038a69 | 121 | HAL_TIM_Base_Init(&TimMasterHandle); |
Zaitsev | 10:41552d038a69 | 122 | |
Zaitsev | 10:41552d038a69 | 123 | // Configure output compare channel 1 for mbed timeout (enabled later when used) |
Zaitsev | 10:41552d038a69 | 124 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); |
Zaitsev | 10:41552d038a69 | 125 | |
Zaitsev | 10:41552d038a69 | 126 | // Configure output compare channel 2 for HAL tick |
Zaitsev | 10:41552d038a69 | 127 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); |
Zaitsev | 10:41552d038a69 | 128 | PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); |
Zaitsev | 10:41552d038a69 | 129 | __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); |
Zaitsev | 10:41552d038a69 | 130 | |
Zaitsev | 10:41552d038a69 | 131 | // Configure interrupts |
Zaitsev | 10:41552d038a69 | 132 | // Update interrupt used for 32-bit counter |
Zaitsev | 10:41552d038a69 | 133 | // Output compare channel 1 interrupt for mbed timeout |
Zaitsev | 10:41552d038a69 | 134 | // Output compare channel 2 interrupt for HAL tick |
Zaitsev | 10:41552d038a69 | 135 | #if defined(TARGET_STM32F0) |
Zaitsev | 10:41552d038a69 | 136 | NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler); |
Zaitsev | 10:41552d038a69 | 137 | NVIC_EnableIRQ(TIM_MST_UP_IRQ); |
Zaitsev | 10:41552d038a69 | 138 | NVIC_SetPriority(TIM_MST_UP_IRQ, 0); |
Zaitsev | 10:41552d038a69 | 139 | NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler); |
Zaitsev | 10:41552d038a69 | 140 | NVIC_EnableIRQ(TIM_MST_OC_IRQ); |
Zaitsev | 10:41552d038a69 | 141 | NVIC_SetPriority(TIM_MST_OC_IRQ, 1); |
Zaitsev | 10:41552d038a69 | 142 | #else |
Zaitsev | 10:41552d038a69 | 143 | NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); |
Zaitsev | 10:41552d038a69 | 144 | NVIC_EnableIRQ(TIM_MST_IRQ); |
Zaitsev | 10:41552d038a69 | 145 | #endif |
Zaitsev | 10:41552d038a69 | 146 | |
Zaitsev | 10:41552d038a69 | 147 | // Enable interrupts |
Zaitsev | 10:41552d038a69 | 148 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter |
Zaitsev | 10:41552d038a69 | 149 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick |
Zaitsev | 10:41552d038a69 | 150 | |
Zaitsev | 10:41552d038a69 | 151 | // Enable timer |
Zaitsev | 10:41552d038a69 | 152 | HAL_TIM_Base_Start(&TimMasterHandle); |
Zaitsev | 10:41552d038a69 | 153 | |
Zaitsev | 10:41552d038a69 | 154 | #if DEBUG_TICK > 0 |
Zaitsev | 10:41552d038a69 | 155 | __GPIOB_CLK_ENABLE(); |
Zaitsev | 10:41552d038a69 | 156 | GPIO_InitTypeDef GPIO_InitStruct; |
Zaitsev | 10:41552d038a69 | 157 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
Zaitsev | 10:41552d038a69 | 158 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
Zaitsev | 10:41552d038a69 | 159 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
Zaitsev | 10:41552d038a69 | 160 | GPIO_InitStruct.Speed = GPIO_SPEED_FAST; |
Zaitsev | 10:41552d038a69 | 161 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
Zaitsev | 10:41552d038a69 | 162 | #endif |
Zaitsev | 10:41552d038a69 | 163 | |
Zaitsev | 10:41552d038a69 | 164 | return HAL_OK; |
Zaitsev | 10:41552d038a69 | 165 | } |
Zaitsev | 10:41552d038a69 | 166 | |
Zaitsev | 10:41552d038a69 | 167 | void HAL_SuspendTick(void) |
Zaitsev | 10:41552d038a69 | 168 | { |
Zaitsev | 10:41552d038a69 | 169 | TimMasterHandle.Instance = TIM_MST; |
Zaitsev | 10:41552d038a69 | 170 | __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
Zaitsev | 10:41552d038a69 | 171 | } |
Zaitsev | 10:41552d038a69 | 172 | |
Zaitsev | 10:41552d038a69 | 173 | void HAL_ResumeTick(void) |
Zaitsev | 10:41552d038a69 | 174 | { |
Zaitsev | 10:41552d038a69 | 175 | TimMasterHandle.Instance = TIM_MST; |
Zaitsev | 10:41552d038a69 | 176 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
Zaitsev | 10:41552d038a69 | 177 | } |
Zaitsev | 10:41552d038a69 | 178 | |
Zaitsev | 10:41552d038a69 | 179 | #endif // TIM_MST_16BIT |