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-2013 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 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 19 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 #define ANALOGIN_MEDIAN_FILTER 1
sahilmgandhi 18:6a4db94011d3 22 #define ADC_10BIT_RANGE 0x3FF
sahilmgandhi 18:6a4db94011d3 23 #define ADC_RANGE ADC_10BIT_RANGE
sahilmgandhi 18:6a4db94011d3 24
sahilmgandhi 18:6a4db94011d3 25 static const PinMap PinMap_ADC[] = {
sahilmgandhi 18:6a4db94011d3 26 {P0_1, ADC0_0, 4},
sahilmgandhi 18:6a4db94011d3 27 {P0_2, ADC0_0, 8},
sahilmgandhi 18:6a4db94011d3 28 {P0_3, ADC0_0, 16},
sahilmgandhi 18:6a4db94011d3 29 {P0_4, ADC0_0, 32},
sahilmgandhi 18:6a4db94011d3 30 {P0_5, ADC0_0, 64},
sahilmgandhi 18:6a4db94011d3 31 {P0_6, ADC0_0, 128},
sahilmgandhi 18:6a4db94011d3 32 #ifndef TARGET_NRF51_DONGLE
sahilmgandhi 18:6a4db94011d3 33 {P0_26, ADC0_0, 1},
sahilmgandhi 18:6a4db94011d3 34 {P0_27, ADC0_0, 2},
sahilmgandhi 18:6a4db94011d3 35 #endif
sahilmgandhi 18:6a4db94011d3 36 {NC, NC, 0}
sahilmgandhi 18:6a4db94011d3 37 };
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39 void analogin_init(analogin_t *obj, PinName pin)
sahilmgandhi 18:6a4db94011d3 40 {
sahilmgandhi 18:6a4db94011d3 41 int analogInputPin = 0;
sahilmgandhi 18:6a4db94011d3 42 const PinMap *map = PinMap_ADC;
sahilmgandhi 18:6a4db94011d3 43
sahilmgandhi 18:6a4db94011d3 44 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *)
sahilmgandhi 18:6a4db94011d3 45 MBED_ASSERT(obj->adc != (ADCName)NC);
sahilmgandhi 18:6a4db94011d3 46
sahilmgandhi 18:6a4db94011d3 47 while (map->pin != NC) {
sahilmgandhi 18:6a4db94011d3 48 if (map->pin == pin) {
sahilmgandhi 18:6a4db94011d3 49 analogInputPin = map->function;
sahilmgandhi 18:6a4db94011d3 50 break;
sahilmgandhi 18:6a4db94011d3 51 }
sahilmgandhi 18:6a4db94011d3 52 map++;
sahilmgandhi 18:6a4db94011d3 53 }
sahilmgandhi 18:6a4db94011d3 54 obj->adc_pin = (uint8_t)analogInputPin;
sahilmgandhi 18:6a4db94011d3 55
sahilmgandhi 18:6a4db94011d3 56 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
sahilmgandhi 18:6a4db94011d3 57 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
sahilmgandhi 18:6a4db94011d3 58 (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
sahilmgandhi 18:6a4db94011d3 59 (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) |
sahilmgandhi 18:6a4db94011d3 60 (analogInputPin << ADC_CONFIG_PSEL_Pos) |
sahilmgandhi 18:6a4db94011d3 61 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
sahilmgandhi 18:6a4db94011d3 62 }
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 uint16_t analogin_read_u16(analogin_t *obj)
sahilmgandhi 18:6a4db94011d3 65 {
sahilmgandhi 18:6a4db94011d3 66 NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
sahilmgandhi 18:6a4db94011d3 67 NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos;
sahilmgandhi 18:6a4db94011d3 68 NRF_ADC->EVENTS_END = 0;
sahilmgandhi 18:6a4db94011d3 69 NRF_ADC->TASKS_START = 1;
sahilmgandhi 18:6a4db94011d3 70
sahilmgandhi 18:6a4db94011d3 71 while (!NRF_ADC->EVENTS_END) {
sahilmgandhi 18:6a4db94011d3 72 }
sahilmgandhi 18:6a4db94011d3 73
sahilmgandhi 18:6a4db94011d3 74 return (uint16_t)NRF_ADC->RESULT; // 10 bit
sahilmgandhi 18:6a4db94011d3 75 }
sahilmgandhi 18:6a4db94011d3 76
sahilmgandhi 18:6a4db94011d3 77 float analogin_read(analogin_t *obj)
sahilmgandhi 18:6a4db94011d3 78 {
sahilmgandhi 18:6a4db94011d3 79 uint16_t value = analogin_read_u16(obj);
sahilmgandhi 18:6a4db94011d3 80 return (float)value * (1.0f / (float)ADC_RANGE);
sahilmgandhi 18:6a4db94011d3 81 }