mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Apr 08 09:15:06 2014 +0100
Revision:
155:8435094ec241
Child:
227:7bd0639b8911
Synchronized with git revision d616c415fc62f490b915ff0ff39c8d24b2917c0f

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

[STM32F3XX] Initial port

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 155:8435094ec241 28 #include "analogout_api.h"
mbed_official 155:8435094ec241 29
mbed_official 155:8435094ec241 30 #if DEVICE_ANALOGOUT
mbed_official 155:8435094ec241 31
mbed_official 155:8435094ec241 32 #include "cmsis.h"
mbed_official 155:8435094ec241 33 #include "pinmap.h"
mbed_official 155:8435094ec241 34 #include "error.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 155:8435094ec241 49
mbed_official 155:8435094ec241 50 if (obj->dac == (DACName)NC) {
mbed_official 155:8435094ec241 51 error("DAC pin mapping failed");
mbed_official 155:8435094ec241 52 }
mbed_official 155:8435094ec241 53
mbed_official 155:8435094ec241 54 dac = (DAC_TypeDef *)(obj->dac);
mbed_official 155:8435094ec241 55
mbed_official 155:8435094ec241 56 // Configure GPIO
mbed_official 155:8435094ec241 57 pinmap_pinout(pin, PinMap_DAC);
mbed_official 155:8435094ec241 58
mbed_official 155:8435094ec241 59 // Save the channel for the write and read functions
mbed_official 155:8435094ec241 60 obj->channel = pin;
mbed_official 155:8435094ec241 61
mbed_official 155:8435094ec241 62 // Enable DAC clock
mbed_official 155:8435094ec241 63 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
mbed_official 155:8435094ec241 64
mbed_official 155:8435094ec241 65 // Configure and enable DAC channel
mbed_official 155:8435094ec241 66 DAC_StructInit(&DAC_InitStructure);
mbed_official 155:8435094ec241 67 DAC_Init(dac, DAC_Channel_1, &DAC_InitStructure);
mbed_official 155:8435094ec241 68 DAC_Cmd(dac, DAC_Channel_1, ENABLE);
mbed_official 155:8435094ec241 69
mbed_official 155:8435094ec241 70 analogout_write_u16(obj, 0);
mbed_official 155:8435094ec241 71 }
mbed_official 155:8435094ec241 72
mbed_official 155:8435094ec241 73 void analogout_free(dac_t *obj) {
mbed_official 155:8435094ec241 74 }
mbed_official 155:8435094ec241 75
mbed_official 155:8435094ec241 76 static inline void dac_write(dac_t *obj, uint16_t value) {
mbed_official 155:8435094ec241 77 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
mbed_official 155:8435094ec241 78 DAC_SetChannel1Data(dac, DAC_Align_12b_R, value);
mbed_official 155:8435094ec241 79 }
mbed_official 155:8435094ec241 80
mbed_official 155:8435094ec241 81 static inline int dac_read(dac_t *obj) {
mbed_official 155:8435094ec241 82 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
mbed_official 155:8435094ec241 83 return (int)DAC_GetDataOutputValue(dac, DAC_Channel_1);
mbed_official 155:8435094ec241 84 }
mbed_official 155:8435094ec241 85
mbed_official 155:8435094ec241 86 void analogout_write(dac_t *obj, float value) {
mbed_official 155:8435094ec241 87 if (value < 0.0f) {
mbed_official 155:8435094ec241 88 dac_write(obj, 0); // Min value
mbed_official 155:8435094ec241 89 } else if (value > 1.0f) {
mbed_official 155:8435094ec241 90 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
mbed_official 155:8435094ec241 91 } else {
mbed_official 155:8435094ec241 92 dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
mbed_official 155:8435094ec241 93 }
mbed_official 155:8435094ec241 94 }
mbed_official 155:8435094ec241 95
mbed_official 155:8435094ec241 96 void analogout_write_u16(dac_t *obj, uint16_t value) {
mbed_official 155:8435094ec241 97 if (value > (uint16_t)RANGE_12BIT) {
mbed_official 155:8435094ec241 98 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
mbed_official 155:8435094ec241 99 } else {
mbed_official 155:8435094ec241 100 dac_write(obj, value);
mbed_official 155:8435094ec241 101 }
mbed_official 155:8435094ec241 102 }
mbed_official 155:8435094ec241 103
mbed_official 155:8435094ec241 104 float analogout_read(dac_t *obj) {
mbed_official 155:8435094ec241 105 uint32_t value = dac_read(obj);
mbed_official 155:8435094ec241 106 return (float)value * (1.0f / (float)RANGE_12BIT);
mbed_official 155:8435094ec241 107 }
mbed_official 155:8435094ec241 108
mbed_official 155:8435094ec241 109 uint16_t analogout_read_u16(dac_t *obj) {
mbed_official 155:8435094ec241 110 return (uint16_t)dac_read(obj);
mbed_official 155:8435094ec241 111 }
mbed_official 155:8435094ec241 112
mbed_official 155:8435094ec241 113 #endif // DEVICE_ANALOGOUT