mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jun 09 15:30:08 2015 +0100
Revision:
563:9f26fcd0c9ce
Parent:
227:7bd0639b8911
Synchronized with git revision a140fc60a6f74003a3d46c4ce8bf8c742148b9fd

Full URL: https://github.com/mbedmicro/mbed/commit/a140fc60a6f74003a3d46c4ce8bf8c742148b9fd/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 155:8435094ec241 1 /* mbed Microcontroller Library
mbed_official 155:8435094ec241 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 155:8435094ec241 3 * All rights reserved.
mbed_official 155:8435094ec241 4 *
mbed_official 155:8435094ec241 5 * Redistribution and use in source and binary forms, with or without
mbed_official 155:8435094ec241 6 * modification, are permitted provided that the following conditions are met:
mbed_official 155:8435094ec241 7 *
mbed_official 155:8435094ec241 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 155:8435094ec241 9 * this list of conditions and the following disclaimer.
mbed_official 155:8435094ec241 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 155:8435094ec241 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 155:8435094ec241 12 * and/or other materials provided with the distribution.
mbed_official 155:8435094ec241 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 155:8435094ec241 14 * may be used to endorse or promote products derived from this software
mbed_official 155:8435094ec241 15 * without specific prior written permission.
mbed_official 155:8435094ec241 16 *
mbed_official 155:8435094ec241 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 155:8435094ec241 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 155:8435094ec241 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 155:8435094ec241 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 155:8435094ec241 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 155:8435094ec241 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 155:8435094ec241 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 155:8435094ec241 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 155:8435094ec241 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 155:8435094ec241 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 155:8435094ec241 27 */
mbed_official 227:7bd0639b8911 28 #include "mbed_assert.h"
mbed_official 155:8435094ec241 29 #include "analogout_api.h"
mbed_official 155:8435094ec241 30
mbed_official 155:8435094ec241 31 #if DEVICE_ANALOGOUT
mbed_official 155:8435094ec241 32
mbed_official 155:8435094ec241 33 #include "cmsis.h"
mbed_official 155:8435094ec241 34 #include "pinmap.h"
mbed_official 155:8435094ec241 35
mbed_official 155:8435094ec241 36 #define RANGE_12BIT (0xFFF)
mbed_official 155:8435094ec241 37
mbed_official 155:8435094ec241 38 static const PinMap PinMap_DAC[] = {
mbed_official 155:8435094ec241 39 {PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1
mbed_official 155:8435094ec241 40 {NC, NC, 0}
mbed_official 155:8435094ec241 41 };
mbed_official 155:8435094ec241 42
mbed_official 155:8435094ec241 43 void analogout_init(dac_t *obj, PinName pin) {
mbed_official 155:8435094ec241 44 DAC_TypeDef *dac;
mbed_official 155:8435094ec241 45 DAC_InitTypeDef DAC_InitStructure;
mbed_official 155:8435094ec241 46
mbed_official 155:8435094ec241 47 // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
mbed_official 155:8435094ec241 48 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
mbed_official 227:7bd0639b8911 49 MBED_ASSERT(obj->dac == (DACName)NC);
mbed_official 155:8435094ec241 50
mbed_official 155:8435094ec241 51 dac = (DAC_TypeDef *)(obj->dac);
mbed_official 155:8435094ec241 52
mbed_official 155:8435094ec241 53 // Configure GPIO
mbed_official 155:8435094ec241 54 pinmap_pinout(pin, PinMap_DAC);
mbed_official 155:8435094ec241 55
mbed_official 155:8435094ec241 56 // Save the channel for the write and read functions
mbed_official 155:8435094ec241 57 obj->channel = pin;
mbed_official 155:8435094ec241 58
mbed_official 155:8435094ec241 59 // Enable DAC clock
mbed_official 155:8435094ec241 60 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
mbed_official 155:8435094ec241 61
mbed_official 155:8435094ec241 62 // Configure and enable DAC channel
mbed_official 155:8435094ec241 63 DAC_StructInit(&DAC_InitStructure);
mbed_official 155:8435094ec241 64 DAC_Init(dac, DAC_Channel_1, &DAC_InitStructure);
mbed_official 155:8435094ec241 65 DAC_Cmd(dac, DAC_Channel_1, ENABLE);
mbed_official 155:8435094ec241 66
mbed_official 155:8435094ec241 67 analogout_write_u16(obj, 0);
mbed_official 155:8435094ec241 68 }
mbed_official 155:8435094ec241 69
mbed_official 155:8435094ec241 70 void analogout_free(dac_t *obj) {
mbed_official 155:8435094ec241 71 }
mbed_official 155:8435094ec241 72
mbed_official 155:8435094ec241 73 static inline void dac_write(dac_t *obj, uint16_t value) {
mbed_official 155:8435094ec241 74 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
mbed_official 155:8435094ec241 75 DAC_SetChannel1Data(dac, DAC_Align_12b_R, value);
mbed_official 155:8435094ec241 76 }
mbed_official 155:8435094ec241 77
mbed_official 155:8435094ec241 78 static inline int dac_read(dac_t *obj) {
mbed_official 155:8435094ec241 79 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
mbed_official 155:8435094ec241 80 return (int)DAC_GetDataOutputValue(dac, DAC_Channel_1);
mbed_official 155:8435094ec241 81 }
mbed_official 155:8435094ec241 82
mbed_official 155:8435094ec241 83 void analogout_write(dac_t *obj, float value) {
mbed_official 155:8435094ec241 84 if (value < 0.0f) {
mbed_official 155:8435094ec241 85 dac_write(obj, 0); // Min value
mbed_official 155:8435094ec241 86 } else if (value > 1.0f) {
mbed_official 155:8435094ec241 87 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
mbed_official 155:8435094ec241 88 } else {
mbed_official 155:8435094ec241 89 dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
mbed_official 155:8435094ec241 90 }
mbed_official 155:8435094ec241 91 }
mbed_official 155:8435094ec241 92
mbed_official 155:8435094ec241 93 void analogout_write_u16(dac_t *obj, uint16_t value) {
mbed_official 155:8435094ec241 94 if (value > (uint16_t)RANGE_12BIT) {
mbed_official 155:8435094ec241 95 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
mbed_official 155:8435094ec241 96 } else {
mbed_official 155:8435094ec241 97 dac_write(obj, value);
mbed_official 155:8435094ec241 98 }
mbed_official 155:8435094ec241 99 }
mbed_official 155:8435094ec241 100
mbed_official 155:8435094ec241 101 float analogout_read(dac_t *obj) {
mbed_official 155:8435094ec241 102 uint32_t value = dac_read(obj);
mbed_official 155:8435094ec241 103 return (float)value * (1.0f / (float)RANGE_12BIT);
mbed_official 155:8435094ec241 104 }
mbed_official 155:8435094ec241 105
mbed_official 155:8435094ec241 106 uint16_t analogout_read_u16(dac_t *obj) {
mbed_official 155:8435094ec241 107 return (uint16_t)dac_read(obj);
mbed_official 155:8435094ec241 108 }
mbed_official 155:8435094ec241 109
mbed_official 155:8435094ec241 110 #endif // DEVICE_ANALOGOUT