mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
227:7bd0639b8911
test with CLOCK_SETUP = 0

Who changed what in which revision?

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