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