Mbed for VNG board

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Nov 25 07:15:09 2014 +0000
Revision:
414:4ec4c5b614b0
Parent:
340:28d1f895c6fe
Synchronized with git revision fbc74e874ac089c6cb05add312fb5422be628886

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

Targets: NUCLEOs - Add PeripheralPins files for all nucleo targets

Who changed what in which revision?

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