The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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