test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elijahsj 1:8a094db1347f 1 /* mbed Microcontroller Library
elijahsj 1:8a094db1347f 2 * Copyright (c) 2006-2016 ARM Limited
elijahsj 1:8a094db1347f 3 *
elijahsj 1:8a094db1347f 4 * Licensed under the Apache License, Version 2.0 (the "License");
elijahsj 1:8a094db1347f 5 * you may not use this file except in compliance with the License.
elijahsj 1:8a094db1347f 6 * You may obtain a copy of the License at
elijahsj 1:8a094db1347f 7 *
elijahsj 1:8a094db1347f 8 * http://www.apache.org/licenses/LICENSE-2.0
elijahsj 1:8a094db1347f 9 *
elijahsj 1:8a094db1347f 10 * Unless required by applicable law or agreed to in writing, software
elijahsj 1:8a094db1347f 11 * distributed under the License is distributed on an "AS IS" BASIS,
elijahsj 1:8a094db1347f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elijahsj 1:8a094db1347f 13 * See the License for the specific language governing permissions and
elijahsj 1:8a094db1347f 14 * limitations under the License.
elijahsj 1:8a094db1347f 15 */
elijahsj 1:8a094db1347f 16 #include "hal_tick.h"
elijahsj 1:8a094db1347f 17
elijahsj 1:8a094db1347f 18 // A 32-bit timer is used
elijahsj 1:8a094db1347f 19 #if !TIM_MST_16BIT
elijahsj 1:8a094db1347f 20
elijahsj 1:8a094db1347f 21 #define DEBUG_TICK 0 // Set to 1 to toggle a pin (see below which pin) at each tick
elijahsj 1:8a094db1347f 22
elijahsj 1:8a094db1347f 23 extern TIM_HandleTypeDef TimMasterHandle;
elijahsj 1:8a094db1347f 24
elijahsj 1:8a094db1347f 25 extern void HAL_IncTick(void);
elijahsj 1:8a094db1347f 26
elijahsj 1:8a094db1347f 27 volatile uint32_t PreviousVal = 0;
elijahsj 1:8a094db1347f 28
elijahsj 1:8a094db1347f 29 void us_ticker_irq_handler(void);
elijahsj 1:8a094db1347f 30
elijahsj 1:8a094db1347f 31 void timer_irq_handler(void)
elijahsj 1:8a094db1347f 32 {
elijahsj 1:8a094db1347f 33 // Channel 1 for mbed timeout
elijahsj 1:8a094db1347f 34 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
elijahsj 1:8a094db1347f 35 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
elijahsj 1:8a094db1347f 36 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
elijahsj 1:8a094db1347f 37 us_ticker_irq_handler();
elijahsj 1:8a094db1347f 38 }
elijahsj 1:8a094db1347f 39 }
elijahsj 1:8a094db1347f 40
elijahsj 1:8a094db1347f 41 // Channel 2 for HAL tick
elijahsj 1:8a094db1347f 42 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
elijahsj 1:8a094db1347f 43 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
elijahsj 1:8a094db1347f 44 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
elijahsj 1:8a094db1347f 45 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
elijahsj 1:8a094db1347f 46 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
elijahsj 1:8a094db1347f 47 // Increment HAL variable
elijahsj 1:8a094db1347f 48 HAL_IncTick();
elijahsj 1:8a094db1347f 49 // Prepare next interrupt
elijahsj 1:8a094db1347f 50 __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
elijahsj 1:8a094db1347f 51 PreviousVal = val;
elijahsj 1:8a094db1347f 52 #if DEBUG_TICK > 0
elijahsj 1:8a094db1347f 53 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
elijahsj 1:8a094db1347f 54 #endif
elijahsj 1:8a094db1347f 55 }
elijahsj 1:8a094db1347f 56 }
elijahsj 1:8a094db1347f 57 }
elijahsj 1:8a094db1347f 58 }
elijahsj 1:8a094db1347f 59
elijahsj 1:8a094db1347f 60 // Reconfigure the HAL tick using a standard timer instead of systick.
elijahsj 1:8a094db1347f 61 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
elijahsj 1:8a094db1347f 62 {
elijahsj 1:8a094db1347f 63 RCC_ClkInitTypeDef RCC_ClkInitStruct;
elijahsj 1:8a094db1347f 64 uint32_t PclkFreq;
elijahsj 1:8a094db1347f 65
elijahsj 1:8a094db1347f 66 // Get clock configuration
elijahsj 1:8a094db1347f 67 // Note: PclkFreq contains here the Latency (not used after)
elijahsj 1:8a094db1347f 68 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
elijahsj 1:8a094db1347f 69
elijahsj 1:8a094db1347f 70 // Get timer clock value
elijahsj 1:8a094db1347f 71 #if TIM_MST_PCLK == 1
elijahsj 1:8a094db1347f 72 PclkFreq = HAL_RCC_GetPCLK1Freq();
elijahsj 1:8a094db1347f 73 #else
elijahsj 1:8a094db1347f 74 PclkFreq = HAL_RCC_GetPCLK2Freq();
elijahsj 1:8a094db1347f 75 #endif
elijahsj 1:8a094db1347f 76
elijahsj 1:8a094db1347f 77 // Enable timer clock
elijahsj 1:8a094db1347f 78 TIM_MST_RCC;
elijahsj 1:8a094db1347f 79
elijahsj 1:8a094db1347f 80 // Reset timer
elijahsj 1:8a094db1347f 81 TIM_MST_RESET_ON;
elijahsj 1:8a094db1347f 82 TIM_MST_RESET_OFF;
elijahsj 1:8a094db1347f 83
elijahsj 1:8a094db1347f 84 // Configure time base
elijahsj 1:8a094db1347f 85 TimMasterHandle.Instance = TIM_MST;
elijahsj 1:8a094db1347f 86 TimMasterHandle.Init.Period = 0xFFFFFFFF;
elijahsj 1:8a094db1347f 87
elijahsj 1:8a094db1347f 88 // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
elijahsj 1:8a094db1347f 89 #if TIM_MST_PCLK == 1
elijahsj 1:8a094db1347f 90 if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) {
elijahsj 1:8a094db1347f 91 #else
elijahsj 1:8a094db1347f 92 if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) {
elijahsj 1:8a094db1347f 93 #endif
elijahsj 1:8a094db1347f 94 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
elijahsj 1:8a094db1347f 95 } else {
elijahsj 1:8a094db1347f 96 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
elijahsj 1:8a094db1347f 97 }
elijahsj 1:8a094db1347f 98
elijahsj 1:8a094db1347f 99 TimMasterHandle.Init.ClockDivision = 0;
elijahsj 1:8a094db1347f 100 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
elijahsj 1:8a094db1347f 101 #if !TARGET_STM32L1
elijahsj 1:8a094db1347f 102 TimMasterHandle.Init.RepetitionCounter = 0;
elijahsj 1:8a094db1347f 103 #endif
elijahsj 1:8a094db1347f 104 #ifdef TIM_AUTORELOAD_PRELOAD_DISABLE
elijahsj 1:8a094db1347f 105 TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
elijahsj 1:8a094db1347f 106 #endif
elijahsj 1:8a094db1347f 107 HAL_TIM_OC_Init(&TimMasterHandle);
elijahsj 1:8a094db1347f 108
elijahsj 1:8a094db1347f 109 NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
elijahsj 1:8a094db1347f 110 NVIC_EnableIRQ(TIM_MST_IRQ);
elijahsj 1:8a094db1347f 111
elijahsj 1:8a094db1347f 112 // Channel 1 for mbed timeout
elijahsj 1:8a094db1347f 113 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
elijahsj 1:8a094db1347f 114
elijahsj 1:8a094db1347f 115 // Channel 2 for HAL tick
elijahsj 1:8a094db1347f 116 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
elijahsj 1:8a094db1347f 117 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
elijahsj 1:8a094db1347f 118 __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
elijahsj 1:8a094db1347f 119 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
elijahsj 1:8a094db1347f 120
elijahsj 1:8a094db1347f 121 // Freeze timer on stop/breakpoint
elijahsj 1:8a094db1347f 122 // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example
elijahsj 1:8a094db1347f 123 #if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE)
elijahsj 1:8a094db1347f 124 TIM_MST_DBGMCU_FREEZE;
elijahsj 1:8a094db1347f 125 #endif
elijahsj 1:8a094db1347f 126
elijahsj 1:8a094db1347f 127 #if DEBUG_TICK > 0
elijahsj 1:8a094db1347f 128 __HAL_RCC_GPIOB_CLK_ENABLE();
elijahsj 1:8a094db1347f 129 GPIO_InitTypeDef GPIO_InitStruct;
elijahsj 1:8a094db1347f 130 GPIO_InitStruct.Pin = GPIO_PIN_6;
elijahsj 1:8a094db1347f 131 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
elijahsj 1:8a094db1347f 132 GPIO_InitStruct.Pull = GPIO_PULLUP;
elijahsj 1:8a094db1347f 133 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
elijahsj 1:8a094db1347f 134 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
elijahsj 1:8a094db1347f 135 #endif
elijahsj 1:8a094db1347f 136
elijahsj 1:8a094db1347f 137 return HAL_OK;
elijahsj 1:8a094db1347f 138 }
elijahsj 1:8a094db1347f 139
elijahsj 1:8a094db1347f 140 /* NOTE: must be called with interrupts disabled! */
elijahsj 1:8a094db1347f 141 void HAL_SuspendTick(void)
elijahsj 1:8a094db1347f 142 {
elijahsj 1:8a094db1347f 143 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
elijahsj 1:8a094db1347f 144 }
elijahsj 1:8a094db1347f 145
elijahsj 1:8a094db1347f 146 /* NOTE: must be called with interrupts disabled! */
elijahsj 1:8a094db1347f 147 void HAL_ResumeTick(void)
elijahsj 1:8a094db1347f 148 {
elijahsj 1:8a094db1347f 149 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
elijahsj 1:8a094db1347f 150 }
elijahsj 1:8a094db1347f 151
elijahsj 1:8a094db1347f 152 #endif // !TIM_MST_16BIT