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