MacroRat / MouseCode

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Wed May 24 01:57:01 2017 +0000
Revision:
29:ec2c5a69acd6
Parent:
18:6a4db94011d3
Need to change ir2-ir3 to now be ir1 - ir4

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