mbed library sources
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/analogout_api.c@76:aeb1df146756, 2014-01-27 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Jan 27 14:30:07 2014 +0000
- Revision:
- 76:aeb1df146756
- Child:
- 94:6519f69185ce
Synchronized with git revision a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd
Full URL: https://github.com/mbedmicro/mbed/commit/a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd/
Add NUCLEO_L152RE
Who changed what in which revision?
User | Revision | Line number | New 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 | 76:aeb1df146756 | 28 | #include "analogout_api.h" |
mbed_official | 76:aeb1df146756 | 29 | |
mbed_official | 76:aeb1df146756 | 30 | #if DEVICE_ANALOGOUT |
mbed_official | 76:aeb1df146756 | 31 | |
mbed_official | 76:aeb1df146756 | 32 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 33 | #include "pinmap.h" |
mbed_official | 76:aeb1df146756 | 34 | #include "error.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 | 76:aeb1df146756 | 40 | //{PA_5, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT2 - Cannot be used due to the LED connected on it |
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 | 76:aeb1df146756 | 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 | 76:aeb1df146756 | 49 | |
mbed_official | 76:aeb1df146756 | 50 | if (obj->dac == (DACName)NC) { |
mbed_official | 76:aeb1df146756 | 51 | error("DAC pin mapping failed"); |
mbed_official | 76:aeb1df146756 | 52 | } |
mbed_official | 76:aeb1df146756 | 53 | |
mbed_official | 76:aeb1df146756 | 54 | // Configure GPIO |
mbed_official | 76:aeb1df146756 | 55 | pinmap_pinout(pin, PinMap_DAC); |
mbed_official | 76:aeb1df146756 | 56 | |
mbed_official | 76:aeb1df146756 | 57 | // Save the channel for the write and read functions |
mbed_official | 76:aeb1df146756 | 58 | obj->channel = pin; |
mbed_official | 76:aeb1df146756 | 59 | |
mbed_official | 76:aeb1df146756 | 60 | // Enable DAC clock |
mbed_official | 76:aeb1df146756 | 61 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); |
mbed_official | 76:aeb1df146756 | 62 | |
mbed_official | 76:aeb1df146756 | 63 | // Configure and enable DAC channel |
mbed_official | 76:aeb1df146756 | 64 | DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; |
mbed_official | 76:aeb1df146756 | 65 | DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; |
mbed_official | 76:aeb1df146756 | 66 | DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; |
mbed_official | 76:aeb1df146756 | 67 | |
mbed_official | 76:aeb1df146756 | 68 | if (pin == PA_4) { |
mbed_official | 76:aeb1df146756 | 69 | DAC_Init(DAC_Channel_1, &DAC_InitStructure); |
mbed_official | 76:aeb1df146756 | 70 | DAC_Cmd(DAC_Channel_1, ENABLE); |
mbed_official | 76:aeb1df146756 | 71 | } |
mbed_official | 76:aeb1df146756 | 72 | else { // PA_5 |
mbed_official | 76:aeb1df146756 | 73 | DAC_Init(DAC_Channel_2, &DAC_InitStructure); |
mbed_official | 76:aeb1df146756 | 74 | DAC_Cmd(DAC_Channel_2, ENABLE); |
mbed_official | 76:aeb1df146756 | 75 | } |
mbed_official | 76:aeb1df146756 | 76 | |
mbed_official | 76:aeb1df146756 | 77 | analogout_write_u16(obj, 0); |
mbed_official | 76:aeb1df146756 | 78 | } |
mbed_official | 76:aeb1df146756 | 79 | |
mbed_official | 76:aeb1df146756 | 80 | void analogout_free(dac_t *obj) { |
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 | 76:aeb1df146756 | 84 | if (obj->channel == PA_4) { |
mbed_official | 76:aeb1df146756 | 85 | DAC_SetChannel1Data(DAC_Align_12b_R, value); |
mbed_official | 76:aeb1df146756 | 86 | } |
mbed_official | 76:aeb1df146756 | 87 | else { // PA_5 |
mbed_official | 76:aeb1df146756 | 88 | DAC_SetChannel2Data(DAC_Align_12b_R, value); |
mbed_official | 76:aeb1df146756 | 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 | 76:aeb1df146756 | 93 | if (obj->channel == PA_4) { |
mbed_official | 76:aeb1df146756 | 94 | return (int)DAC_GetDataOutputValue(DAC_Channel_1); |
mbed_official | 76:aeb1df146756 | 95 | } |
mbed_official | 76:aeb1df146756 | 96 | else { // PA_5 |
mbed_official | 76:aeb1df146756 | 97 | return (int)DAC_GetDataOutputValue(DAC_Channel_2); |
mbed_official | 76:aeb1df146756 | 98 | } |
mbed_official | 76:aeb1df146756 | 99 | } |
mbed_official | 76:aeb1df146756 | 100 | |
mbed_official | 76:aeb1df146756 | 101 | void analogout_write(dac_t *obj, float value) { |
mbed_official | 76:aeb1df146756 | 102 | if (value < 0.0f) { |
mbed_official | 76:aeb1df146756 | 103 | dac_write(obj, 0); // Min value |
mbed_official | 76:aeb1df146756 | 104 | } else if (value > 1.0f) { |
mbed_official | 76:aeb1df146756 | 105 | dac_write(obj, (uint16_t)RANGE_12BIT); // Max value |
mbed_official | 76:aeb1df146756 | 106 | } else { |
mbed_official | 76:aeb1df146756 | 107 | dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT)); |
mbed_official | 76:aeb1df146756 | 108 | } |
mbed_official | 76:aeb1df146756 | 109 | } |
mbed_official | 76:aeb1df146756 | 110 | |
mbed_official | 76:aeb1df146756 | 111 | void analogout_write_u16(dac_t *obj, uint16_t value) { |
mbed_official | 76:aeb1df146756 | 112 | if (value > (uint16_t)RANGE_12BIT) { |
mbed_official | 76:aeb1df146756 | 113 | dac_write(obj, (uint16_t)RANGE_12BIT); // Max value |
mbed_official | 76:aeb1df146756 | 114 | } |
mbed_official | 76:aeb1df146756 | 115 | else { |
mbed_official | 76:aeb1df146756 | 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 |