Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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