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 "mbed_assert.h"
sahilmgandhi 18:6a4db94011d3 17 #include "analogin_api.h"
sahilmgandhi 18:6a4db94011d3 18
sahilmgandhi 18:6a4db94011d3 19 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 20 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 21 #include "clk_freqs.h"
sahilmgandhi 18:6a4db94011d3 22 #include "PeripheralPins.h"
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 #define MAX_FADC 6000000
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 void analogin_init(analogin_t *obj, PinName pin) {
sahilmgandhi 18:6a4db94011d3 27 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
sahilmgandhi 18:6a4db94011d3 28 MBED_ASSERT(obj->adc != (ADCName)NC);
sahilmgandhi 18:6a4db94011d3 29
sahilmgandhi 18:6a4db94011d3 30 SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;
sahilmgandhi 18:6a4db94011d3 31
sahilmgandhi 18:6a4db94011d3 32 uint32_t port = (uint32_t)pin >> PORT_SHIFT;
sahilmgandhi 18:6a4db94011d3 33 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
sahilmgandhi 18:6a4db94011d3 34
sahilmgandhi 18:6a4db94011d3 35 // bus clk
sahilmgandhi 18:6a4db94011d3 36 uint32_t PCLK = bus_frequency();
sahilmgandhi 18:6a4db94011d3 37 uint32_t clkdiv;
sahilmgandhi 18:6a4db94011d3 38 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
sahilmgandhi 18:6a4db94011d3 39 if ((PCLK >> clkdiv) <= MAX_FADC)
sahilmgandhi 18:6a4db94011d3 40 break;
sahilmgandhi 18:6a4db94011d3 41 }
sahilmgandhi 18:6a4db94011d3 42 if (clkdiv == 4) //Set max div
sahilmgandhi 18:6a4db94011d3 43 clkdiv = 0x7;
sahilmgandhi 18:6a4db94011d3 44
sahilmgandhi 18:6a4db94011d3 45 ADC0->SC1[1] = ADC_SC1_ADCH(obj->adc);
sahilmgandhi 18:6a4db94011d3 46
sahilmgandhi 18:6a4db94011d3 47 ADC0->CFG1 = ADC_CFG1_ADLPC_MASK // Low-Power Configuration
sahilmgandhi 18:6a4db94011d3 48 | ADC_CFG1_ADIV(clkdiv & 0x3) // Clock Divide Select
sahilmgandhi 18:6a4db94011d3 49 | ADC_CFG1_ADLSMP_MASK // Long Sample Time
sahilmgandhi 18:6a4db94011d3 50 | ADC_CFG1_MODE(3) // (16)bits Resolution
sahilmgandhi 18:6a4db94011d3 51 | ADC_CFG1_ADICLK(clkdiv >> 2); // Input Clock
sahilmgandhi 18:6a4db94011d3 52
sahilmgandhi 18:6a4db94011d3 53 ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK // ADxxb or ADxxa channels
sahilmgandhi 18:6a4db94011d3 54 | ADC_CFG2_ADHSC_MASK // High-Speed Configuration
sahilmgandhi 18:6a4db94011d3 55 | ADC_CFG2_ADLSTS(0); // Long Sample Time Select
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 ADC0->SC2 = ADC_SC2_REFSEL(0); // Default Voltage Reference
sahilmgandhi 18:6a4db94011d3 58
sahilmgandhi 18:6a4db94011d3 59 ADC0->SC3 = ADC_SC3_AVGE_MASK // Hardware Average Enable
sahilmgandhi 18:6a4db94011d3 60 | ADC_SC3_AVGS(0); // 4 Samples Averaged
sahilmgandhi 18:6a4db94011d3 61
sahilmgandhi 18:6a4db94011d3 62 pinmap_pinout(pin, PinMap_ADC);
sahilmgandhi 18:6a4db94011d3 63 }
sahilmgandhi 18:6a4db94011d3 64
sahilmgandhi 18:6a4db94011d3 65 uint16_t analogin_read_u16(analogin_t *obj) {
sahilmgandhi 18:6a4db94011d3 66 // start conversion
sahilmgandhi 18:6a4db94011d3 67 ADC0->SC1[0] = ADC_SC1_ADCH(obj->adc);
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 // Wait Conversion Complete
sahilmgandhi 18:6a4db94011d3 70 while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK);
sahilmgandhi 18:6a4db94011d3 71
sahilmgandhi 18:6a4db94011d3 72 return (uint16_t)ADC0->R[0];
sahilmgandhi 18:6a4db94011d3 73 }
sahilmgandhi 18:6a4db94011d3 74
sahilmgandhi 18:6a4db94011d3 75 float analogin_read(analogin_t *obj) {
sahilmgandhi 18:6a4db94011d3 76 uint16_t value = analogin_read_u16(obj);
sahilmgandhi 18:6a4db94011d3 77 return (float)value * (1.0f / (float)0xFFFF);
sahilmgandhi 18:6a4db94011d3 78 }