Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Fri May 26 17:21:04 2017 +0000
Revision:
34:69342782fb68
Parent:
18:6a4db94011d3
Added small reverse turns before the break so that we can stop faster.

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-2015 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 "clk_freqs.h"
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 static int us_ticker_inited = 0;
sahilmgandhi 18:6a4db94011d3 22
sahilmgandhi 18:6a4db94011d3 23 void us_ticker_init(void) {
sahilmgandhi 18:6a4db94011d3 24 if (us_ticker_inited)
sahilmgandhi 18:6a4db94011d3 25 return;
sahilmgandhi 18:6a4db94011d3 26 us_ticker_inited = 1;
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 SIM->SCGC6 |= SIM_SCGC6_PIT_MASK; // Clock PIT
sahilmgandhi 18:6a4db94011d3 29 PIT->MCR = 0; // Enable PIT
sahilmgandhi 18:6a4db94011d3 30
sahilmgandhi 18:6a4db94011d3 31 //Timer on PIT0+1, ticker on PIT 2+3
sahilmgandhi 18:6a4db94011d3 32 //Init timer
sahilmgandhi 18:6a4db94011d3 33 PIT->CHANNEL[1].LDVAL = 0xFFFFFFFF;
sahilmgandhi 18:6a4db94011d3 34 PIT->CHANNEL[1].TCTRL = PIT_TCTRL_CHN_MASK | PIT_TCTRL_TEN_MASK; // Start timer 1, chained to timer 0
sahilmgandhi 18:6a4db94011d3 35
sahilmgandhi 18:6a4db94011d3 36 // Use channel 0 as a prescaler for channel 1
sahilmgandhi 18:6a4db94011d3 37 uint32_t ldval = (bus_frequency() + 500000) / 1000000 - 1;
sahilmgandhi 18:6a4db94011d3 38 PIT->CHANNEL[0].LDVAL = ldval;
sahilmgandhi 18:6a4db94011d3 39 PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK; // Start timer
sahilmgandhi 18:6a4db94011d3 40
sahilmgandhi 18:6a4db94011d3 41 //Init ticker
sahilmgandhi 18:6a4db94011d3 42 PIT->CHANNEL[2].LDVAL = ldval;
sahilmgandhi 18:6a4db94011d3 43 PIT->CHANNEL[2].TCTRL = PIT_TCTRL_TEN_MASK; // Start timer 2 as prescaler
sahilmgandhi 18:6a4db94011d3 44
sahilmgandhi 18:6a4db94011d3 45 NVIC_SetVector(PIT3_IRQn, (uint32_t)us_ticker_irq_handler);
sahilmgandhi 18:6a4db94011d3 46 NVIC_EnableIRQ(PIT3_IRQn);
sahilmgandhi 18:6a4db94011d3 47 }
sahilmgandhi 18:6a4db94011d3 48
sahilmgandhi 18:6a4db94011d3 49 /******************************************************************************
sahilmgandhi 18:6a4db94011d3 50 * Timer for us timing.
sahilmgandhi 18:6a4db94011d3 51 ******************************************************************************/
sahilmgandhi 18:6a4db94011d3 52
sahilmgandhi 18:6a4db94011d3 53 uint32_t us_ticker_read() {
sahilmgandhi 18:6a4db94011d3 54 if (!us_ticker_inited)
sahilmgandhi 18:6a4db94011d3 55 us_ticker_init();
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 return ~(PIT->CHANNEL[1].CVAL);
sahilmgandhi 18:6a4db94011d3 58 }
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 /******************************************************************************
sahilmgandhi 18:6a4db94011d3 61 * Ticker Event
sahilmgandhi 18:6a4db94011d3 62 ******************************************************************************/
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 void us_ticker_disable_interrupt(void) {
sahilmgandhi 18:6a4db94011d3 65 PIT->CHANNEL[3].TCTRL &= ~PIT_TCTRL_TIE_MASK;
sahilmgandhi 18:6a4db94011d3 66 }
sahilmgandhi 18:6a4db94011d3 67
sahilmgandhi 18:6a4db94011d3 68 void us_ticker_clear_interrupt(void) {
sahilmgandhi 18:6a4db94011d3 69 PIT->CHANNEL[3].TFLG = 1;
sahilmgandhi 18:6a4db94011d3 70 }
sahilmgandhi 18:6a4db94011d3 71
sahilmgandhi 18:6a4db94011d3 72 void us_ticker_set_interrupt(timestamp_t timestamp) {
sahilmgandhi 18:6a4db94011d3 73 int delta = (int)((uint32_t)timestamp - us_ticker_read());
sahilmgandhi 18:6a4db94011d3 74 if (delta <= 0) {
sahilmgandhi 18:6a4db94011d3 75 // This event was in the past:
sahilmgandhi 18:6a4db94011d3 76 us_ticker_irq_handler();
sahilmgandhi 18:6a4db94011d3 77 return;
sahilmgandhi 18:6a4db94011d3 78 }
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 PIT->CHANNEL[3].TCTRL = 0;
sahilmgandhi 18:6a4db94011d3 81 PIT->CHANNEL[3].LDVAL = delta;
sahilmgandhi 18:6a4db94011d3 82 PIT->CHANNEL[3].TCTRL = PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK | PIT_TCTRL_CHN_MASK;
sahilmgandhi 18:6a4db94011d3 83
sahilmgandhi 18:6a4db94011d3 84 }