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 /*
sahilmgandhi 18:6a4db94011d3 2 * PackageLicenseDeclared: Apache-2.0
sahilmgandhi 18:6a4db94011d3 3 * Copyright (c) 2015 ARM Limited
sahilmgandhi 18:6a4db94011d3 4 *
sahilmgandhi 18:6a4db94011d3 5 * Licensed under the Apache License, Version 2.0 (the "License");
sahilmgandhi 18:6a4db94011d3 6 * you may not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 7 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 8 *
sahilmgandhi 18:6a4db94011d3 9 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 10 *
sahilmgandhi 18:6a4db94011d3 11 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 12 * distributed under the License is distributed on an "AS IS" BASIS,
sahilmgandhi 18:6a4db94011d3 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 14 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 15 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 16 */
sahilmgandhi 18:6a4db94011d3 17
sahilmgandhi 18:6a4db94011d3 18 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 19 #include "device.h"
sahilmgandhi 18:6a4db94011d3 20 #include "objects.h"
sahilmgandhi 18:6a4db94011d3 21 #include "lp_ticker_api.h"
sahilmgandhi 18:6a4db94011d3 22
sahilmgandhi 18:6a4db94011d3 23 /* Private lp_ticker data */
sahilmgandhi 18:6a4db94011d3 24 /* lp_ticker initialize */
sahilmgandhi 18:6a4db94011d3 25 static uint32_t lp_ticker_initialized = 0;
sahilmgandhi 18:6a4db94011d3 26 /* lp_ticker reload value */
sahilmgandhi 18:6a4db94011d3 27 static uint32_t lp_ticker_reload = 0x0; /* Max Value */
sahilmgandhi 18:6a4db94011d3 28 /* Store Overflow Delta */
sahilmgandhi 18:6a4db94011d3 29 static uint32_t lp_ticker_overflows_delta = 0;
sahilmgandhi 18:6a4db94011d3 30 /* lp_ticker Overflow limit */
sahilmgandhi 18:6a4db94011d3 31 static uint32_t lp_ticker_overflow_limit = 0;
sahilmgandhi 18:6a4db94011d3 32
sahilmgandhi 18:6a4db94011d3 33 #if DEVICE_LOWPOWERTIMER
sahilmgandhi 18:6a4db94011d3 34 /**
sahilmgandhi 18:6a4db94011d3 35 * Interrupt Handler
sahilmgandhi 18:6a4db94011d3 36 */
sahilmgandhi 18:6a4db94011d3 37 void __lp_ticker_irq_handler(void)
sahilmgandhi 18:6a4db94011d3 38 {
sahilmgandhi 18:6a4db94011d3 39 if (DualTimer_GetIRQInfo(DUALTIMER0) == SINGLETIMER2) {
sahilmgandhi 18:6a4db94011d3 40 DualTimer_ClearInterrupt(DUALTIMER0);
sahilmgandhi 18:6a4db94011d3 41 /*
sahilmgandhi 18:6a4db94011d3 42 * For each overflow event adds the timer max represented value to
sahilmgandhi 18:6a4db94011d3 43 * the delta. This allows the lp_ticker to keep track of the elapsed
sahilmgandhi 18:6a4db94011d3 44 * time:
sahilmgandhi 18:6a4db94011d3 45 * elapsed_time = (num_overflow * overflow_limit) + current_time
sahilmgandhi 18:6a4db94011d3 46 */
sahilmgandhi 18:6a4db94011d3 47 lp_ticker_overflows_delta += lp_ticker_overflow_limit;
sahilmgandhi 18:6a4db94011d3 48 } else {
sahilmgandhi 18:6a4db94011d3 49 lp_ticker_irq_handler();
sahilmgandhi 18:6a4db94011d3 50 }
sahilmgandhi 18:6a4db94011d3 51 }
sahilmgandhi 18:6a4db94011d3 52
sahilmgandhi 18:6a4db94011d3 53 /**
sahilmgandhi 18:6a4db94011d3 54 * Initialize the low power ticker
sahilmgandhi 18:6a4db94011d3 55 */
sahilmgandhi 18:6a4db94011d3 56 void lp_ticker_init(void)
sahilmgandhi 18:6a4db94011d3 57 {
sahilmgandhi 18:6a4db94011d3 58 uint32_t lp_ticker_irqn = 0;
sahilmgandhi 18:6a4db94011d3 59 /* Verify if lp_ticker has been already Initialized */
sahilmgandhi 18:6a4db94011d3 60 if (lp_ticker_initialized == 1)
sahilmgandhi 18:6a4db94011d3 61 {
sahilmgandhi 18:6a4db94011d3 62 return;
sahilmgandhi 18:6a4db94011d3 63 }
sahilmgandhi 18:6a4db94011d3 64 lp_ticker_initialized = 1;
sahilmgandhi 18:6a4db94011d3 65
sahilmgandhi 18:6a4db94011d3 66 /* Dualtimer Initialize */
sahilmgandhi 18:6a4db94011d3 67 DualTimer_Initialize(DUALTIMER0, lp_ticker_reload);
sahilmgandhi 18:6a4db94011d3 68 /* Dualtimer Enable */
sahilmgandhi 18:6a4db94011d3 69 DualTimer_Enable(DUALTIMER0, DUALTIMER_COUNT_32
sahilmgandhi 18:6a4db94011d3 70 //| DUALTIMER_PERIODIC
sahilmgandhi 18:6a4db94011d3 71 );
sahilmgandhi 18:6a4db94011d3 72 /* DualTimer get IRQn */
sahilmgandhi 18:6a4db94011d3 73 lp_ticker_irqn = DualTimer_GetIRQn(DUALTIMER0);
sahilmgandhi 18:6a4db94011d3 74 /* Enable lp_ticker IRQ */
sahilmgandhi 18:6a4db94011d3 75 NVIC_SetVector((IRQn_Type)lp_ticker_irqn,
sahilmgandhi 18:6a4db94011d3 76 (uint32_t)__lp_ticker_irq_handler);
sahilmgandhi 18:6a4db94011d3 77 NVIC_EnableIRQ((IRQn_Type)lp_ticker_irqn);
sahilmgandhi 18:6a4db94011d3 78
sahilmgandhi 18:6a4db94011d3 79 /* DualTimer set interrupt on SINGLETIMER2 */
sahilmgandhi 18:6a4db94011d3 80 DualTimer_SetInterrupt_2(DUALTIMER0, DUALTIMER_DEFAULT_RELOAD,
sahilmgandhi 18:6a4db94011d3 81 DUALTIMER_COUNT_32 | DUALTIMER_PERIODIC);
sahilmgandhi 18:6a4db94011d3 82
sahilmgandhi 18:6a4db94011d3 83 /*
sahilmgandhi 18:6a4db94011d3 84 * Set lp_ticker Overflow limit. The lp_ticker overflow limit is required
sahilmgandhi 18:6a4db94011d3 85 * to calculated the return value of the lp_ticker read function in us
sahilmgandhi 18:6a4db94011d3 86 * on 32bit.
sahilmgandhi 18:6a4db94011d3 87 * A 32bit us value cannot be represented directly in the Dual Timer Load
sahilmgandhi 18:6a4db94011d3 88 * register if it is greater than (0xFFFFFFFF ticks)/DUALTIMER_DIVIDER_US.
sahilmgandhi 18:6a4db94011d3 89 */
sahilmgandhi 18:6a4db94011d3 90 lp_ticker_overflow_limit = DualTimer_GetReloadValue(DUALTIMER0,
sahilmgandhi 18:6a4db94011d3 91 SINGLETIMER2);
sahilmgandhi 18:6a4db94011d3 92 }
sahilmgandhi 18:6a4db94011d3 93
sahilmgandhi 18:6a4db94011d3 94 /**
sahilmgandhi 18:6a4db94011d3 95 * Read the current counter
sahilmgandhi 18:6a4db94011d3 96 * @return: The current timer's counter value in microseconds
sahilmgandhi 18:6a4db94011d3 97 */
sahilmgandhi 18:6a4db94011d3 98 uint32_t lp_ticker_read(void)
sahilmgandhi 18:6a4db94011d3 99 {
sahilmgandhi 18:6a4db94011d3 100 uint32_t microseconds = 0;
sahilmgandhi 18:6a4db94011d3 101
sahilmgandhi 18:6a4db94011d3 102 /* Verify if lp_ticker has not been Initialized */
sahilmgandhi 18:6a4db94011d3 103 if (lp_ticker_initialized == 0)
sahilmgandhi 18:6a4db94011d3 104 lp_ticker_init();
sahilmgandhi 18:6a4db94011d3 105
sahilmgandhi 18:6a4db94011d3 106 /* Read Timer Value */
sahilmgandhi 18:6a4db94011d3 107 microseconds = lp_ticker_overflows_delta + DualTimer_Read_2(DUALTIMER0);
sahilmgandhi 18:6a4db94011d3 108
sahilmgandhi 18:6a4db94011d3 109 return microseconds;
sahilmgandhi 18:6a4db94011d3 110 }
sahilmgandhi 18:6a4db94011d3 111
sahilmgandhi 18:6a4db94011d3 112 /**
sahilmgandhi 18:6a4db94011d3 113 * Set interrupt for specified timestamp
sahilmgandhi 18:6a4db94011d3 114 * timestamp: The time in microseconds to be set
sahilmgandhi 18:6a4db94011d3 115 */
sahilmgandhi 18:6a4db94011d3 116 void lp_ticker_set_interrupt(timestamp_t timestamp)
sahilmgandhi 18:6a4db94011d3 117 {
sahilmgandhi 18:6a4db94011d3 118 int32_t delta = 0;
sahilmgandhi 18:6a4db94011d3 119
sahilmgandhi 18:6a4db94011d3 120 /* Verify if lp_ticker has been not Initialized */
sahilmgandhi 18:6a4db94011d3 121 if (lp_ticker_initialized == 0)
sahilmgandhi 18:6a4db94011d3 122 lp_ticker_init();
sahilmgandhi 18:6a4db94011d3 123
sahilmgandhi 18:6a4db94011d3 124 /* Calculate the delta */
sahilmgandhi 18:6a4db94011d3 125 delta = (int32_t)(timestamp - lp_ticker_read());
sahilmgandhi 18:6a4db94011d3 126 /* Check if the event was in the past */
sahilmgandhi 18:6a4db94011d3 127 if (delta <= 0) {
sahilmgandhi 18:6a4db94011d3 128 /* This event was in the past */
sahilmgandhi 18:6a4db94011d3 129 DualTimer_SetInterrupt_1(DUALTIMER0, 0,
sahilmgandhi 18:6a4db94011d3 130 DUALTIMER_COUNT_32 | DUALTIMER_ONESHOT);
sahilmgandhi 18:6a4db94011d3 131 return;
sahilmgandhi 18:6a4db94011d3 132 }
sahilmgandhi 18:6a4db94011d3 133
sahilmgandhi 18:6a4db94011d3 134 /* Enable interrupt on SingleTimer1 */
sahilmgandhi 18:6a4db94011d3 135 DualTimer_SetInterrupt_1(DUALTIMER0, delta,
sahilmgandhi 18:6a4db94011d3 136 DUALTIMER_COUNT_32 | DUALTIMER_ONESHOT);
sahilmgandhi 18:6a4db94011d3 137 }
sahilmgandhi 18:6a4db94011d3 138
sahilmgandhi 18:6a4db94011d3 139 /**
sahilmgandhi 18:6a4db94011d3 140 * Disable low power ticker interrupt
sahilmgandhi 18:6a4db94011d3 141 */
sahilmgandhi 18:6a4db94011d3 142 void lp_ticker_disable_interrupt(void)
sahilmgandhi 18:6a4db94011d3 143 {
sahilmgandhi 18:6a4db94011d3 144 /* Disable Interrupt */
sahilmgandhi 18:6a4db94011d3 145 DualTimer_DisableInterrupt(DUALTIMER0,
sahilmgandhi 18:6a4db94011d3 146 SINGLETIMER1);
sahilmgandhi 18:6a4db94011d3 147 }
sahilmgandhi 18:6a4db94011d3 148
sahilmgandhi 18:6a4db94011d3 149 /**
sahilmgandhi 18:6a4db94011d3 150 * Clear the low power ticker interrupt
sahilmgandhi 18:6a4db94011d3 151 */
sahilmgandhi 18:6a4db94011d3 152 void lp_ticker_clear_interrupt(void)
sahilmgandhi 18:6a4db94011d3 153 {
sahilmgandhi 18:6a4db94011d3 154 /* Clear Interrupt */
sahilmgandhi 18:6a4db94011d3 155 DualTimer_ClearInterrupt(DUALTIMER0);
sahilmgandhi 18:6a4db94011d3 156 }
sahilmgandhi 18:6a4db94011d3 157
sahilmgandhi 18:6a4db94011d3 158 #endif