Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

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 <stddef.h>
sahilmgandhi 18:6a4db94011d3 17 #include "us_ticker_api.h"
sahilmgandhi 18:6a4db94011d3 18 #include "PeripheralNames.h"
sahilmgandhi 18:6a4db94011d3 19 #include "hal_tick.h"
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 // A 16-bit timer is used
sahilmgandhi 18:6a4db94011d3 22 #if TIM_MST_16BIT
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 TIM_HandleTypeDef TimMasterHandle;
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 volatile uint32_t SlaveCounter = 0;
sahilmgandhi 18:6a4db94011d3 27 volatile uint32_t oc_int_part = 0;
sahilmgandhi 18:6a4db94011d3 28 volatile uint16_t oc_rem_part = 0;
sahilmgandhi 18:6a4db94011d3 29 volatile uint8_t tim_it_update; // TIM_IT_UPDATE event flag set in timer_irq_handler()
sahilmgandhi 18:6a4db94011d3 30 volatile uint32_t tim_it_counter = 0; // Time stamp to be updated by timer_irq_handler()
sahilmgandhi 18:6a4db94011d3 31
sahilmgandhi 18:6a4db94011d3 32 static int us_ticker_inited = 0;
sahilmgandhi 18:6a4db94011d3 33
sahilmgandhi 18:6a4db94011d3 34 void set_compare(uint16_t count)
sahilmgandhi 18:6a4db94011d3 35 {
sahilmgandhi 18:6a4db94011d3 36 TimMasterHandle.Instance = TIM_MST;
sahilmgandhi 18:6a4db94011d3 37 // Set new output compare value
sahilmgandhi 18:6a4db94011d3 38 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
sahilmgandhi 18:6a4db94011d3 39 // Enable IT
sahilmgandhi 18:6a4db94011d3 40 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
sahilmgandhi 18:6a4db94011d3 41 }
sahilmgandhi 18:6a4db94011d3 42
sahilmgandhi 18:6a4db94011d3 43 void us_ticker_init(void)
sahilmgandhi 18:6a4db94011d3 44 {
sahilmgandhi 18:6a4db94011d3 45 if (us_ticker_inited) return;
sahilmgandhi 18:6a4db94011d3 46 us_ticker_inited = 1;
sahilmgandhi 18:6a4db94011d3 47
sahilmgandhi 18:6a4db94011d3 48 TimMasterHandle.Instance = TIM_MST;
sahilmgandhi 18:6a4db94011d3 49
sahilmgandhi 18:6a4db94011d3 50 HAL_InitTick(0); // The passed value is not used
sahilmgandhi 18:6a4db94011d3 51 }
sahilmgandhi 18:6a4db94011d3 52
sahilmgandhi 18:6a4db94011d3 53 uint32_t us_ticker_read()
sahilmgandhi 18:6a4db94011d3 54 {
sahilmgandhi 18:6a4db94011d3 55 uint32_t counter;
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 TimMasterHandle.Instance = TIM_MST;
sahilmgandhi 18:6a4db94011d3 58
sahilmgandhi 18:6a4db94011d3 59 if (!us_ticker_inited) us_ticker_init();
sahilmgandhi 18:6a4db94011d3 60
sahilmgandhi 18:6a4db94011d3 61 #if defined(TARGET_STM32L0)
sahilmgandhi 18:6a4db94011d3 62 uint16_t cntH_old, cntH, cntL;
sahilmgandhi 18:6a4db94011d3 63 do {
sahilmgandhi 18:6a4db94011d3 64 // For some reason on L0xx series we need to read and clear the
sahilmgandhi 18:6a4db94011d3 65 // overflow flag which give extra time to propelry handle possible
sahilmgandhi 18:6a4db94011d3 66 // hiccup after ~60s
sahilmgandhi 18:6a4db94011d3 67 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF) == SET) {
sahilmgandhi 18:6a4db94011d3 68 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF);
sahilmgandhi 18:6a4db94011d3 69 }
sahilmgandhi 18:6a4db94011d3 70 cntH_old = SlaveCounter;
sahilmgandhi 18:6a4db94011d3 71 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
sahilmgandhi 18:6a4db94011d3 72 cntH_old += 1;
sahilmgandhi 18:6a4db94011d3 73 }
sahilmgandhi 18:6a4db94011d3 74 cntL = TIM_MST->CNT;
sahilmgandhi 18:6a4db94011d3 75 cntH = SlaveCounter;
sahilmgandhi 18:6a4db94011d3 76 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
sahilmgandhi 18:6a4db94011d3 77 cntH += 1;
sahilmgandhi 18:6a4db94011d3 78 }
sahilmgandhi 18:6a4db94011d3 79 } while(cntH_old != cntH);
sahilmgandhi 18:6a4db94011d3 80 // Glue the upper and lower part together to get a 32 bit timer
sahilmgandhi 18:6a4db94011d3 81 return (uint32_t)(cntH << 16 | cntL);
sahilmgandhi 18:6a4db94011d3 82 #else
sahilmgandhi 18:6a4db94011d3 83 tim_it_update = 0; // Clear TIM_IT_UPDATE event flag
sahilmgandhi 18:6a4db94011d3 84 counter = TIM_MST->CNT + (uint32_t)(SlaveCounter << 16); // Calculate new time stamp
sahilmgandhi 18:6a4db94011d3 85 if (tim_it_update == 1) {
sahilmgandhi 18:6a4db94011d3 86 return tim_it_counter; // In case of TIM_IT_UPDATE return the time stamp that was calculated in timer_irq_handler()
sahilmgandhi 18:6a4db94011d3 87 }
sahilmgandhi 18:6a4db94011d3 88 else {
sahilmgandhi 18:6a4db94011d3 89 return counter; // Otherwise return the time stamp calculated here
sahilmgandhi 18:6a4db94011d3 90 }
sahilmgandhi 18:6a4db94011d3 91 #endif
sahilmgandhi 18:6a4db94011d3 92 }
sahilmgandhi 18:6a4db94011d3 93
sahilmgandhi 18:6a4db94011d3 94 void us_ticker_set_interrupt(timestamp_t timestamp)
sahilmgandhi 18:6a4db94011d3 95 {
sahilmgandhi 18:6a4db94011d3 96 int delta = (int)((uint32_t)timestamp - us_ticker_read());
sahilmgandhi 18:6a4db94011d3 97
sahilmgandhi 18:6a4db94011d3 98 uint16_t cval = TIM_MST->CNT;
sahilmgandhi 18:6a4db94011d3 99
sahilmgandhi 18:6a4db94011d3 100 if (delta <= 0) { // This event was in the past
sahilmgandhi 18:6a4db94011d3 101 us_ticker_irq_handler();
sahilmgandhi 18:6a4db94011d3 102 } else {
sahilmgandhi 18:6a4db94011d3 103 oc_int_part = (uint32_t)(delta >> 16);
sahilmgandhi 18:6a4db94011d3 104 oc_rem_part = (uint16_t)(delta & 0xFFFF);
sahilmgandhi 18:6a4db94011d3 105 if (oc_rem_part <= (0xFFFF - cval)) {
sahilmgandhi 18:6a4db94011d3 106 set_compare(cval + oc_rem_part);
sahilmgandhi 18:6a4db94011d3 107 oc_rem_part = 0;
sahilmgandhi 18:6a4db94011d3 108 } else {
sahilmgandhi 18:6a4db94011d3 109 set_compare(0xFFFF);
sahilmgandhi 18:6a4db94011d3 110 oc_rem_part = oc_rem_part - (0xFFFF - cval);
sahilmgandhi 18:6a4db94011d3 111 }
sahilmgandhi 18:6a4db94011d3 112 }
sahilmgandhi 18:6a4db94011d3 113 }
sahilmgandhi 18:6a4db94011d3 114
sahilmgandhi 18:6a4db94011d3 115 void us_ticker_disable_interrupt(void)
sahilmgandhi 18:6a4db94011d3 116 {
sahilmgandhi 18:6a4db94011d3 117 TimMasterHandle.Instance = TIM_MST;
sahilmgandhi 18:6a4db94011d3 118 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
sahilmgandhi 18:6a4db94011d3 119 }
sahilmgandhi 18:6a4db94011d3 120
sahilmgandhi 18:6a4db94011d3 121 void us_ticker_clear_interrupt(void)
sahilmgandhi 18:6a4db94011d3 122 {
sahilmgandhi 18:6a4db94011d3 123 TimMasterHandle.Instance = TIM_MST;
sahilmgandhi 18:6a4db94011d3 124 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
sahilmgandhi 18:6a4db94011d3 125 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
sahilmgandhi 18:6a4db94011d3 126 }
sahilmgandhi 18:6a4db94011d3 127 }
sahilmgandhi 18:6a4db94011d3 128
sahilmgandhi 18:6a4db94011d3 129 #endif // TIM_MST_16BIT