Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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