mbed official / mbed-src

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Committer:
mbed_official
Date:
Mon Apr 14 10:30:07 2014 +0100
Revision:
159:3b23f6d9ecb9
Parent:
135:067cc8ba23da
Child:
216:577900467c9e
Synchronized with git revision 3afbb87c326e6dba456e6421d226611464fab342

Full URL: https://github.com/mbedmicro/mbed/commit/3afbb87c326e6dba456e6421d226611464fab342/

[DISCO_F407VG] Fixed PWM PA_3 (and PC_9) output issue.

Who changed what in which revision?

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