Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay
Fork of mbed-dev by
targets/hal/TARGET_NXP/TARGET_LPC176X/analogout_api.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
 - bogdanm
 - Date:
 - Thu Oct 01 15:25:22 2015 +0300
 - Revision:
 - 0:9b334a45a8ff
 - Child:
 - 144:ef7eb2e8f9f7
 
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library | 
| bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited | 
| bogdanm | 0:9b334a45a8ff | 3 | * | 
| bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. | 
| bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at | 
| bogdanm | 0:9b334a45a8ff | 7 | * | 
| bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| bogdanm | 0:9b334a45a8ff | 9 | * | 
| bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software | 
| bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and | 
| bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. | 
| bogdanm | 0:9b334a45a8ff | 15 | */ | 
| bogdanm | 0:9b334a45a8ff | 16 | #include "mbed_assert.h" | 
| bogdanm | 0:9b334a45a8ff | 17 | #include "analogout_api.h" | 
| bogdanm | 0:9b334a45a8ff | 18 | |
| bogdanm | 0:9b334a45a8ff | 19 | #include "cmsis.h" | 
| bogdanm | 0:9b334a45a8ff | 20 | #include "pinmap.h" | 
| bogdanm | 0:9b334a45a8ff | 21 | |
| bogdanm | 0:9b334a45a8ff | 22 | static const PinMap PinMap_DAC[] = { | 
| bogdanm | 0:9b334a45a8ff | 23 | {P0_26, DAC_0, 2}, | 
| bogdanm | 0:9b334a45a8ff | 24 | {NC , NC , 0} | 
| bogdanm | 0:9b334a45a8ff | 25 | }; | 
| bogdanm | 0:9b334a45a8ff | 26 | |
| bogdanm | 0:9b334a45a8ff | 27 | void analogout_init(dac_t *obj, PinName pin) { | 
| bogdanm | 0:9b334a45a8ff | 28 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); | 
| bogdanm | 0:9b334a45a8ff | 29 | MBED_ASSERT(obj->dac != (DACName)NC); | 
| bogdanm | 0:9b334a45a8ff | 30 | |
| bogdanm | 0:9b334a45a8ff | 31 | // power is on by default, set DAC clk divider is /4 | 
| bogdanm | 0:9b334a45a8ff | 32 | LPC_SC->PCLKSEL0 &= ~(0x3 << 22); | 
| bogdanm | 0:9b334a45a8ff | 33 | |
| bogdanm | 0:9b334a45a8ff | 34 | // map out (must be done before accessing registers) | 
| bogdanm | 0:9b334a45a8ff | 35 | pinmap_pinout(pin, PinMap_DAC); | 
| bogdanm | 0:9b334a45a8ff | 36 | |
| bogdanm | 0:9b334a45a8ff | 37 | analogout_write_u16(obj, 0); | 
| bogdanm | 0:9b334a45a8ff | 38 | } | 
| bogdanm | 0:9b334a45a8ff | 39 | |
| bogdanm | 0:9b334a45a8ff | 40 | void analogout_free(dac_t *obj) {} | 
| bogdanm | 0:9b334a45a8ff | 41 | |
| bogdanm | 0:9b334a45a8ff | 42 | static inline void dac_write(int value) { | 
| bogdanm | 0:9b334a45a8ff | 43 | value &= 0x3FF; // 10-bit | 
| bogdanm | 0:9b334a45a8ff | 44 | |
| bogdanm | 0:9b334a45a8ff | 45 | // Set the DAC output | 
| bogdanm | 0:9b334a45a8ff | 46 | LPC_DAC->DACR = (0 << 16) // bias = 0 | 
| bogdanm | 0:9b334a45a8ff | 47 | | (value << 6); | 
| bogdanm | 0:9b334a45a8ff | 48 | } | 
| bogdanm | 0:9b334a45a8ff | 49 | |
| bogdanm | 0:9b334a45a8ff | 50 | static inline int dac_read() { | 
| bogdanm | 0:9b334a45a8ff | 51 | return (LPC_DAC->DACR >> 6) & 0x3FF; | 
| bogdanm | 0:9b334a45a8ff | 52 | } | 
| bogdanm | 0:9b334a45a8ff | 53 | |
| bogdanm | 0:9b334a45a8ff | 54 | void analogout_write(dac_t *obj, float value) { | 
| bogdanm | 0:9b334a45a8ff | 55 | if (value < 0.0f) { | 
| bogdanm | 0:9b334a45a8ff | 56 | dac_write(0); | 
| bogdanm | 0:9b334a45a8ff | 57 | } else if (value > 1.0f) { | 
| bogdanm | 0:9b334a45a8ff | 58 | dac_write(0x3FF); | 
| bogdanm | 0:9b334a45a8ff | 59 | } else { | 
| bogdanm | 0:9b334a45a8ff | 60 | dac_write(value * (float)0x3FF); | 
| bogdanm | 0:9b334a45a8ff | 61 | } | 
| bogdanm | 0:9b334a45a8ff | 62 | } | 
| bogdanm | 0:9b334a45a8ff | 63 | |
| bogdanm | 0:9b334a45a8ff | 64 | void analogout_write_u16(dac_t *obj, uint16_t value) { | 
| bogdanm | 0:9b334a45a8ff | 65 | dac_write(value >> 6); // 10-bit | 
| bogdanm | 0:9b334a45a8ff | 66 | } | 
| bogdanm | 0:9b334a45a8ff | 67 | |
| bogdanm | 0:9b334a45a8ff | 68 | float analogout_read(dac_t *obj) { | 
| bogdanm | 0:9b334a45a8ff | 69 | uint32_t value = dac_read(); | 
| bogdanm | 0:9b334a45a8ff | 70 | return (float)value * (1.0f / (float)0x3FF); | 
| bogdanm | 0:9b334a45a8ff | 71 | } | 
| bogdanm | 0:9b334a45a8ff | 72 | |
| bogdanm | 0:9b334a45a8ff | 73 | uint16_t analogout_read_u16(dac_t *obj) { | 
| bogdanm | 0:9b334a45a8ff | 74 | uint32_t value = dac_read(); // 10-bit | 
| bogdanm | 0:9b334a45a8ff | 75 | return (value << 6) | ((value >> 4) & 0x003F); | 
| bogdanm | 0:9b334a45a8ff | 76 | } | 
