mbed
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32F2/analogout_device.c@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 178:d650f5d4c87a
mbed-dev library. Release version 159.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 168:9672193075cf | 1 | /* mbed Microcontroller Library |
AnnaBridge | 178:d650f5d4c87a | 2 | * Copyright (c) 2017, STMicroelectronics |
AnnaBridge | 168:9672193075cf | 3 | * All rights reserved. |
AnnaBridge | 168:9672193075cf | 4 | * |
AnnaBridge | 168:9672193075cf | 5 | * Redistribution and use in source and binary forms, with or without |
AnnaBridge | 168:9672193075cf | 6 | * modification, are permitted provided that the following conditions are met: |
AnnaBridge | 168:9672193075cf | 7 | * |
AnnaBridge | 168:9672193075cf | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
AnnaBridge | 168:9672193075cf | 9 | * this list of conditions and the following disclaimer. |
AnnaBridge | 168:9672193075cf | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
AnnaBridge | 168:9672193075cf | 11 | * this list of conditions and the following disclaimer in the documentation |
AnnaBridge | 168:9672193075cf | 12 | * and/or other materials provided with the distribution. |
AnnaBridge | 168:9672193075cf | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
AnnaBridge | 168:9672193075cf | 14 | * may be used to endorse or promote products derived from this software |
AnnaBridge | 168:9672193075cf | 15 | * without specific prior written permission. |
AnnaBridge | 168:9672193075cf | 16 | * |
AnnaBridge | 168:9672193075cf | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
AnnaBridge | 168:9672193075cf | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
AnnaBridge | 168:9672193075cf | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
AnnaBridge | 168:9672193075cf | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
AnnaBridge | 168:9672193075cf | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
AnnaBridge | 168:9672193075cf | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
AnnaBridge | 168:9672193075cf | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
AnnaBridge | 168:9672193075cf | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
AnnaBridge | 168:9672193075cf | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
AnnaBridge | 168:9672193075cf | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
AnnaBridge | 168:9672193075cf | 27 | */ |
AnnaBridge | 178:d650f5d4c87a | 28 | #include "mbed_assert.h" |
AnnaBridge | 168:9672193075cf | 29 | #include "analogout_api.h" |
AnnaBridge | 168:9672193075cf | 30 | |
AnnaBridge | 168:9672193075cf | 31 | #if DEVICE_ANALOGOUT |
AnnaBridge | 168:9672193075cf | 32 | |
AnnaBridge | 168:9672193075cf | 33 | #include "cmsis.h" |
AnnaBridge | 168:9672193075cf | 34 | #include "pinmap.h" |
AnnaBridge | 168:9672193075cf | 35 | #include "mbed_error.h" |
AnnaBridge | 168:9672193075cf | 36 | #include "PeripheralPins.h" |
AnnaBridge | 168:9672193075cf | 37 | |
AnnaBridge | 168:9672193075cf | 38 | void analogout_init(dac_t *obj, PinName pin) |
AnnaBridge | 168:9672193075cf | 39 | { |
AnnaBridge | 178:d650f5d4c87a | 40 | DAC_ChannelConfTypeDef sConfig = {0}; |
AnnaBridge | 168:9672193075cf | 41 | |
AnnaBridge | 178:d650f5d4c87a | 42 | // Get the peripheral name from the pin and assign it to the object |
AnnaBridge | 168:9672193075cf | 43 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
AnnaBridge | 178:d650f5d4c87a | 44 | MBED_ASSERT(obj->dac != (DACName)NC); |
AnnaBridge | 178:d650f5d4c87a | 45 | |
AnnaBridge | 178:d650f5d4c87a | 46 | // Get the pin function and assign the used channel to the object |
AnnaBridge | 168:9672193075cf | 47 | uint32_t function = pinmap_function(pin, PinMap_DAC); |
AnnaBridge | 168:9672193075cf | 48 | MBED_ASSERT(function != (uint32_t)NC); |
AnnaBridge | 178:d650f5d4c87a | 49 | |
AnnaBridge | 168:9672193075cf | 50 | switch (STM_PIN_CHANNEL(function)) { |
AnnaBridge | 168:9672193075cf | 51 | case 1: |
AnnaBridge | 168:9672193075cf | 52 | obj->channel = DAC_CHANNEL_1; |
AnnaBridge | 168:9672193075cf | 53 | break; |
AnnaBridge | 168:9672193075cf | 54 | #if defined(DAC_CHANNEL_2) |
AnnaBridge | 168:9672193075cf | 55 | case 2: |
AnnaBridge | 168:9672193075cf | 56 | obj->channel = DAC_CHANNEL_2; |
AnnaBridge | 168:9672193075cf | 57 | break; |
AnnaBridge | 168:9672193075cf | 58 | #endif |
AnnaBridge | 168:9672193075cf | 59 | default: |
AnnaBridge | 168:9672193075cf | 60 | error("Unknown DAC channel"); |
AnnaBridge | 168:9672193075cf | 61 | break; |
AnnaBridge | 168:9672193075cf | 62 | } |
AnnaBridge | 168:9672193075cf | 63 | |
AnnaBridge | 168:9672193075cf | 64 | // Configure GPIO |
AnnaBridge | 168:9672193075cf | 65 | pinmap_pinout(pin, PinMap_DAC); |
AnnaBridge | 168:9672193075cf | 66 | |
AnnaBridge | 178:d650f5d4c87a | 67 | // Save the pin for future use |
AnnaBridge | 178:d650f5d4c87a | 68 | obj->pin = pin; |
AnnaBridge | 168:9672193075cf | 69 | |
AnnaBridge | 178:d650f5d4c87a | 70 | // Enable DAC clock |
AnnaBridge | 178:d650f5d4c87a | 71 | __HAL_RCC_DAC_CLK_ENABLE(); |
AnnaBridge | 168:9672193075cf | 72 | |
AnnaBridge | 178:d650f5d4c87a | 73 | // Configure DAC |
AnnaBridge | 178:d650f5d4c87a | 74 | obj->handle.Instance = (DAC_TypeDef *)(obj->dac); |
AnnaBridge | 178:d650f5d4c87a | 75 | obj->handle.State = HAL_DAC_STATE_RESET; |
AnnaBridge | 178:d650f5d4c87a | 76 | |
AnnaBridge | 168:9672193075cf | 77 | if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { |
AnnaBridge | 168:9672193075cf | 78 | error("HAL_DAC_Init failed"); |
AnnaBridge | 168:9672193075cf | 79 | } |
AnnaBridge | 168:9672193075cf | 80 | |
AnnaBridge | 168:9672193075cf | 81 | sConfig.DAC_Trigger = DAC_TRIGGER_NONE; |
AnnaBridge | 168:9672193075cf | 82 | sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; |
AnnaBridge | 168:9672193075cf | 83 | |
AnnaBridge | 168:9672193075cf | 84 | if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { |
AnnaBridge | 168:9672193075cf | 85 | error("HAL_DAC_ConfigChannel failed"); |
AnnaBridge | 168:9672193075cf | 86 | } |
AnnaBridge | 168:9672193075cf | 87 | |
AnnaBridge | 168:9672193075cf | 88 | analogout_write_u16(obj, 0); |
AnnaBridge | 168:9672193075cf | 89 | } |
AnnaBridge | 168:9672193075cf | 90 | |
AnnaBridge | 168:9672193075cf | 91 | void analogout_free(dac_t *obj) |
AnnaBridge | 168:9672193075cf | 92 | { |
AnnaBridge | 178:d650f5d4c87a | 93 | // Reset DAC and disable clock |
AnnaBridge | 178:d650f5d4c87a | 94 | __HAL_RCC_DAC_FORCE_RESET(); |
AnnaBridge | 178:d650f5d4c87a | 95 | __HAL_RCC_DAC_RELEASE_RESET(); |
AnnaBridge | 178:d650f5d4c87a | 96 | __HAL_RCC_DAC_CLK_DISABLE(); |
AnnaBridge | 178:d650f5d4c87a | 97 | |
AnnaBridge | 178:d650f5d4c87a | 98 | // Configure GPIO |
AnnaBridge | 178:d650f5d4c87a | 99 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
AnnaBridge | 168:9672193075cf | 100 | } |
AnnaBridge | 168:9672193075cf | 101 | |
AnnaBridge | 168:9672193075cf | 102 | #endif // DEVICE_ANALOGOUT |