lol

Dependencies:   MMA8451Q

Fork of Application by Mateusz Kowalik

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?

UserRevisionLine numberNew 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 32-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 volatile uint32_t PreviousVal = 0;
Zaitsev 10:41552d038a69 26
Zaitsev 10:41552d038a69 27 void us_ticker_irq_handler(void);
Zaitsev 10:41552d038a69 28
Zaitsev 10:41552d038a69 29 void timer_irq_handler(void)
Zaitsev 10:41552d038a69 30 {
Zaitsev 10:41552d038a69 31 // Channel 1 for mbed timeout
Zaitsev 10:41552d038a69 32 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
Zaitsev 10:41552d038a69 33 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
Zaitsev 10:41552d038a69 34 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
Zaitsev 10:41552d038a69 35 us_ticker_irq_handler();
Zaitsev 10:41552d038a69 36 }
Zaitsev 10:41552d038a69 37 }
Zaitsev 10:41552d038a69 38
Zaitsev 10:41552d038a69 39 // Channel 2 for HAL tick
Zaitsev 10:41552d038a69 40 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
Zaitsev 10:41552d038a69 41 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
Zaitsev 10:41552d038a69 42 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
Zaitsev 10:41552d038a69 43 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
Zaitsev 10:41552d038a69 44 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
Zaitsev 10:41552d038a69 45 // Increment HAL variable
Zaitsev 10:41552d038a69 46 HAL_IncTick();
Zaitsev 10:41552d038a69 47 // Prepare next interrupt
Zaitsev 10:41552d038a69 48 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
Zaitsev 10:41552d038a69 49 PreviousVal = val;
Zaitsev 10:41552d038a69 50 #if DEBUG_TICK > 0
Zaitsev 10:41552d038a69 51 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
Zaitsev 10:41552d038a69 52 #endif
Zaitsev 10:41552d038a69 53 }
Zaitsev 10:41552d038a69 54 }
Zaitsev 10:41552d038a69 55 }
Zaitsev 10:41552d038a69 56 }
Zaitsev 10:41552d038a69 57
Zaitsev 10:41552d038a69 58 // Reconfigure the HAL tick using a standard timer instead of systick.
Zaitsev 10:41552d038a69 59 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
Zaitsev 10:41552d038a69 60 {
Zaitsev 10:41552d038a69 61 RCC_ClkInitTypeDef RCC_ClkInitStruct;
Zaitsev 10:41552d038a69 62 uint32_t PclkFreq;
Zaitsev 10:41552d038a69 63
Zaitsev 10:41552d038a69 64 // Get clock configuration
Zaitsev 10:41552d038a69 65 // Note: PclkFreq contains here the Latency (not used after)
Zaitsev 10:41552d038a69 66 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
Zaitsev 10:41552d038a69 67
Zaitsev 10:41552d038a69 68 // Get timer clock value
Zaitsev 10:41552d038a69 69 #if TIM_MST_PCLK == 1
Zaitsev 10:41552d038a69 70 PclkFreq = HAL_RCC_GetPCLK1Freq();
Zaitsev 10:41552d038a69 71 #else
Zaitsev 10:41552d038a69 72 PclkFreq = HAL_RCC_GetPCLK2Freq();
Zaitsev 10:41552d038a69 73 #endif
Zaitsev 10:41552d038a69 74
Zaitsev 10:41552d038a69 75 // Enable timer clock
Zaitsev 10:41552d038a69 76 TIM_MST_RCC;
Zaitsev 10:41552d038a69 77
Zaitsev 10:41552d038a69 78 // Reset timer
Zaitsev 10:41552d038a69 79 TIM_MST_RESET_ON;
Zaitsev 10:41552d038a69 80 TIM_MST_RESET_OFF;
Zaitsev 10:41552d038a69 81
Zaitsev 10:41552d038a69 82 // Configure time base
Zaitsev 10:41552d038a69 83 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 84 TimMasterHandle.Init.Period = 0xFFFFFFFF;
Zaitsev 10:41552d038a69 85
Zaitsev 10:41552d038a69 86 // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
Zaitsev 10:41552d038a69 87 #if TIM_MST_PCLK == 1
Zaitsev 10:41552d038a69 88 if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) {
Zaitsev 10:41552d038a69 89 #else
Zaitsev 10:41552d038a69 90 if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) {
Zaitsev 10:41552d038a69 91 #endif
Zaitsev 10:41552d038a69 92 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
Zaitsev 10:41552d038a69 93 }
Zaitsev 10:41552d038a69 94 else {
Zaitsev 10:41552d038a69 95 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
Zaitsev 10:41552d038a69 96 }
Zaitsev 10:41552d038a69 97
Zaitsev 10:41552d038a69 98 TimMasterHandle.Init.ClockDivision = 0;
Zaitsev 10:41552d038a69 99 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
Zaitsev 10:41552d038a69 100 #if !TARGET_STM32L1
Zaitsev 10:41552d038a69 101 TimMasterHandle.Init.RepetitionCounter = 0;
Zaitsev 10:41552d038a69 102 #endif
Zaitsev 10:41552d038a69 103 HAL_TIM_OC_Init(&TimMasterHandle);
Zaitsev 10:41552d038a69 104
Zaitsev 10:41552d038a69 105 NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
Zaitsev 10:41552d038a69 106 NVIC_EnableIRQ(TIM_MST_IRQ);
Zaitsev 10:41552d038a69 107
Zaitsev 10:41552d038a69 108 // Channel 1 for mbed timeout
Zaitsev 10:41552d038a69 109 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
Zaitsev 10:41552d038a69 110
Zaitsev 10:41552d038a69 111 // Channel 2 for HAL tick
Zaitsev 10:41552d038a69 112 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
Zaitsev 10:41552d038a69 113 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
Zaitsev 10:41552d038a69 114 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
Zaitsev 10:41552d038a69 115 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
Zaitsev 10:41552d038a69 116
Zaitsev 10:41552d038a69 117 #if DEBUG_TICK > 0
Zaitsev 10:41552d038a69 118 __GPIOB_CLK_ENABLE();
Zaitsev 10:41552d038a69 119 GPIO_InitTypeDef GPIO_InitStruct;
Zaitsev 10:41552d038a69 120 GPIO_InitStruct.Pin = GPIO_PIN_6;
Zaitsev 10:41552d038a69 121 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
Zaitsev 10:41552d038a69 122 GPIO_InitStruct.Pull = GPIO_PULLUP;
Zaitsev 10:41552d038a69 123 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
Zaitsev 10:41552d038a69 124 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
Zaitsev 10:41552d038a69 125 #endif
Zaitsev 10:41552d038a69 126
Zaitsev 10:41552d038a69 127 return HAL_OK;
Zaitsev 10:41552d038a69 128 }
Zaitsev 10:41552d038a69 129
Zaitsev 10:41552d038a69 130 void HAL_SuspendTick(void)
Zaitsev 10:41552d038a69 131 {
Zaitsev 10:41552d038a69 132 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 133 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
Zaitsev 10:41552d038a69 134 }
Zaitsev 10:41552d038a69 135
Zaitsev 10:41552d038a69 136 void HAL_ResumeTick(void)
Zaitsev 10:41552d038a69 137 {
Zaitsev 10:41552d038a69 138 TimMasterHandle.Instance = TIM_MST;
Zaitsev 10:41552d038a69 139 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
Zaitsev 10:41552d038a69 140 }
Zaitsev 10:41552d038a69 141
Zaitsev 10:41552d038a69 142 #endif // !TIM_MST_16BIT