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-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
sahilmgandhi 18:6a4db94011d3 17 #include "mbed_assert.h"
sahilmgandhi 18:6a4db94011d3 18 #include "analogout_api.h"
sahilmgandhi 18:6a4db94011d3 19
sahilmgandhi 18:6a4db94011d3 20 #if DEVICE_ANALOGOUT
sahilmgandhi 18:6a4db94011d3 21
sahilmgandhi 18:6a4db94011d3 22 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 23 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 24 #include "PeripheralPins.h"
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 #define RANGE_12BIT 0xFFF
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 void analogout_init(dac_t *obj, PinName pin) {
sahilmgandhi 18:6a4db94011d3 29 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
sahilmgandhi 18:6a4db94011d3 30 MBED_ASSERT(obj->dac != (DACName)NC);
sahilmgandhi 18:6a4db94011d3 31
sahilmgandhi 18:6a4db94011d3 32 SIM->SCGC2 |= SIM_SCGC2_DAC0_MASK;
sahilmgandhi 18:6a4db94011d3 33
sahilmgandhi 18:6a4db94011d3 34 uint32_t port = (uint32_t)pin >> PORT_SHIFT;
sahilmgandhi 18:6a4db94011d3 35 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
sahilmgandhi 18:6a4db94011d3 36
sahilmgandhi 18:6a4db94011d3 37 DAC0->DAT[obj->dac].DATH = 0;
sahilmgandhi 18:6a4db94011d3 38 DAC0->DAT[obj->dac].DATL = 0;
sahilmgandhi 18:6a4db94011d3 39
sahilmgandhi 18:6a4db94011d3 40 DAC0->C1 = DAC_C1_DACBFMD_MASK; // One-Time Scan Mode
sahilmgandhi 18:6a4db94011d3 41
sahilmgandhi 18:6a4db94011d3 42 DAC0->C0 = DAC_C0_DACEN_MASK // Enable
sahilmgandhi 18:6a4db94011d3 43 | DAC_C0_DACSWTRG_MASK // Software Trigger
sahilmgandhi 18:6a4db94011d3 44 | DAC_C0_DACRFS_MASK; // VDDA selected
sahilmgandhi 18:6a4db94011d3 45
sahilmgandhi 18:6a4db94011d3 46 analogout_write_u16(obj, 0);
sahilmgandhi 18:6a4db94011d3 47 }
sahilmgandhi 18:6a4db94011d3 48
sahilmgandhi 18:6a4db94011d3 49 void analogout_free(dac_t *obj) {}
sahilmgandhi 18:6a4db94011d3 50
sahilmgandhi 18:6a4db94011d3 51 static inline void dac_write(dac_t *obj, int value) {
sahilmgandhi 18:6a4db94011d3 52 DAC0->DAT[obj->dac].DATL = (uint8_t)( value & 0xFF);
sahilmgandhi 18:6a4db94011d3 53 DAC0->DAT[obj->dac].DATH = (uint8_t)((value >> 8) & 0xFF);
sahilmgandhi 18:6a4db94011d3 54 }
sahilmgandhi 18:6a4db94011d3 55
sahilmgandhi 18:6a4db94011d3 56 static inline int dac_read(dac_t *obj) {
sahilmgandhi 18:6a4db94011d3 57 return ((DAC0->DAT[obj->dac].DATH << 8) | DAC0->DAT[obj->dac].DATL);
sahilmgandhi 18:6a4db94011d3 58 }
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 void analogout_write(dac_t *obj, float value) {
sahilmgandhi 18:6a4db94011d3 61 if (value < 0.0) {
sahilmgandhi 18:6a4db94011d3 62 dac_write(obj, 0);
sahilmgandhi 18:6a4db94011d3 63 } else if (value > 1.0) {
sahilmgandhi 18:6a4db94011d3 64 dac_write(obj, RANGE_12BIT);
sahilmgandhi 18:6a4db94011d3 65 } else {
sahilmgandhi 18:6a4db94011d3 66 dac_write(obj, value * (float)RANGE_12BIT);
sahilmgandhi 18:6a4db94011d3 67 }
sahilmgandhi 18:6a4db94011d3 68 }
sahilmgandhi 18:6a4db94011d3 69
sahilmgandhi 18:6a4db94011d3 70 void analogout_write_u16(dac_t *obj, uint16_t value) {
sahilmgandhi 18:6a4db94011d3 71 dac_write(obj, value >> 4); // 12-bit
sahilmgandhi 18:6a4db94011d3 72 }
sahilmgandhi 18:6a4db94011d3 73
sahilmgandhi 18:6a4db94011d3 74 float analogout_read(dac_t *obj) {
sahilmgandhi 18:6a4db94011d3 75 uint32_t value = dac_read(obj);
sahilmgandhi 18:6a4db94011d3 76 return (float)value * (1.0f / (float)RANGE_12BIT);
sahilmgandhi 18:6a4db94011d3 77 }
sahilmgandhi 18:6a4db94011d3 78
sahilmgandhi 18:6a4db94011d3 79 uint16_t analogout_read_u16(dac_t *obj) {
sahilmgandhi 18:6a4db94011d3 80 uint32_t value = dac_read(obj); // 12-bit
sahilmgandhi 18:6a4db94011d3 81 return (value << 4) | ((value >> 8) & 0x003F);
sahilmgandhi 18:6a4db94011d3 82 }
sahilmgandhi 18:6a4db94011d3 83
sahilmgandhi 18:6a4db94011d3 84 #endif