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 "analogout_api.h"
sahilmgandhi 18:6a4db94011d3 18 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 19 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 static const PinMap PinMap_DAC[] = {
sahilmgandhi 18:6a4db94011d3 22 {P0_26, DAC_0, 2},
sahilmgandhi 18:6a4db94011d3 23 {NC , NC , 0}
sahilmgandhi 18:6a4db94011d3 24 };
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 void analogout_init(dac_t *obj, PinName pin) {
sahilmgandhi 18:6a4db94011d3 27 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
sahilmgandhi 18:6a4db94011d3 28 MBED_ASSERT(obj->dac != (DACName)NC);
sahilmgandhi 18:6a4db94011d3 29
sahilmgandhi 18:6a4db94011d3 30 // DAC enable bit must be set
sahilmgandhi 18:6a4db94011d3 31 LPC_IOCON->P0_26 |= (1 << 16); // DACEN
sahilmgandhi 18:6a4db94011d3 32
sahilmgandhi 18:6a4db94011d3 33 // map out (must be done before accessing registers)
sahilmgandhi 18:6a4db94011d3 34 pinmap_pinout(pin, PinMap_DAC);
sahilmgandhi 18:6a4db94011d3 35
sahilmgandhi 18:6a4db94011d3 36 analogout_write_u16(obj, 0);
sahilmgandhi 18:6a4db94011d3 37 }
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39 void analogout_free(dac_t *obj) {}
sahilmgandhi 18:6a4db94011d3 40
sahilmgandhi 18:6a4db94011d3 41 static inline void dac_write(int value) {
sahilmgandhi 18:6a4db94011d3 42 value &= 0x3FF; // 10-bit
sahilmgandhi 18:6a4db94011d3 43
sahilmgandhi 18:6a4db94011d3 44 // Set the DAC output
sahilmgandhi 18:6a4db94011d3 45 LPC_DAC->CR = (0 << 16) // bias = 0
sahilmgandhi 18:6a4db94011d3 46 | (value << 6);
sahilmgandhi 18:6a4db94011d3 47 }
sahilmgandhi 18:6a4db94011d3 48
sahilmgandhi 18:6a4db94011d3 49 static inline int dac_read() {
sahilmgandhi 18:6a4db94011d3 50 return (LPC_DAC->CR >> 6) & 0x3FF;
sahilmgandhi 18:6a4db94011d3 51 }
sahilmgandhi 18:6a4db94011d3 52
sahilmgandhi 18:6a4db94011d3 53 void analogout_write(dac_t *obj, float value) {
sahilmgandhi 18:6a4db94011d3 54 if (value < 0.0f) {
sahilmgandhi 18:6a4db94011d3 55 dac_write(0);
sahilmgandhi 18:6a4db94011d3 56 } else if (value > 1.0f) {
sahilmgandhi 18:6a4db94011d3 57 dac_write(0x3FF);
sahilmgandhi 18:6a4db94011d3 58 } else {
sahilmgandhi 18:6a4db94011d3 59 dac_write(value * (float)0x3FF);
sahilmgandhi 18:6a4db94011d3 60 }
sahilmgandhi 18:6a4db94011d3 61 }
sahilmgandhi 18:6a4db94011d3 62
sahilmgandhi 18:6a4db94011d3 63 void analogout_write_u16(dac_t *obj, uint16_t value) {
sahilmgandhi 18:6a4db94011d3 64 dac_write(value >> 6); // 10-bit
sahilmgandhi 18:6a4db94011d3 65 }
sahilmgandhi 18:6a4db94011d3 66
sahilmgandhi 18:6a4db94011d3 67 float analogout_read(dac_t *obj) {
sahilmgandhi 18:6a4db94011d3 68 uint32_t value = dac_read();
sahilmgandhi 18:6a4db94011d3 69 return (float)value * (1.0f / (float)0x3FF);
sahilmgandhi 18:6a4db94011d3 70 }
sahilmgandhi 18:6a4db94011d3 71
sahilmgandhi 18:6a4db94011d3 72 uint16_t analogout_read_u16(dac_t *obj) {
sahilmgandhi 18:6a4db94011d3 73 uint32_t value = dac_read(); // 10-bit
sahilmgandhi 18:6a4db94011d3 74 return (value << 6) | ((value >> 4) & 0x003F);
sahilmgandhi 18:6a4db94011d3 75 }