inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 2 * Copyright (c) 2015, STMicroelectronics
NYX 0:85b3fd62ea1a 3 * All rights reserved.
NYX 0:85b3fd62ea1a 4 *
NYX 0:85b3fd62ea1a 5 * Redistribution and use in source and binary forms, with or without
NYX 0:85b3fd62ea1a 6 * modification, are permitted provided that the following conditions are met:
NYX 0:85b3fd62ea1a 7 *
NYX 0:85b3fd62ea1a 8 * 1. Redistributions of source code must retain the above copyright notice,
NYX 0:85b3fd62ea1a 9 * this list of conditions and the following disclaimer.
NYX 0:85b3fd62ea1a 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
NYX 0:85b3fd62ea1a 11 * this list of conditions and the following disclaimer in the documentation
NYX 0:85b3fd62ea1a 12 * and/or other materials provided with the distribution.
NYX 0:85b3fd62ea1a 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
NYX 0:85b3fd62ea1a 14 * may be used to endorse or promote products derived from this software
NYX 0:85b3fd62ea1a 15 * without specific prior written permission.
NYX 0:85b3fd62ea1a 16 *
NYX 0:85b3fd62ea1a 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
NYX 0:85b3fd62ea1a 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
NYX 0:85b3fd62ea1a 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NYX 0:85b3fd62ea1a 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
NYX 0:85b3fd62ea1a 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
NYX 0:85b3fd62ea1a 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
NYX 0:85b3fd62ea1a 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
NYX 0:85b3fd62ea1a 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
NYX 0:85b3fd62ea1a 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
NYX 0:85b3fd62ea1a 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NYX 0:85b3fd62ea1a 27 */
NYX 0:85b3fd62ea1a 28 #include "analogout_api.h"
NYX 0:85b3fd62ea1a 29
NYX 0:85b3fd62ea1a 30 #if DEVICE_ANALOGOUT
NYX 0:85b3fd62ea1a 31
NYX 0:85b3fd62ea1a 32 #include "cmsis.h"
NYX 0:85b3fd62ea1a 33 #include "pinmap.h"
NYX 0:85b3fd62ea1a 34 #include "mbed_error.h"
NYX 0:85b3fd62ea1a 35 #include "stm32f4xx_hal.h"
NYX 0:85b3fd62ea1a 36 #include "PeripheralPins.h"
NYX 0:85b3fd62ea1a 37
NYX 0:85b3fd62ea1a 38 void analogout_init(dac_t *obj, PinName pin) {
NYX 0:85b3fd62ea1a 39 DAC_ChannelConfTypeDef sConfig;
NYX 0:85b3fd62ea1a 40
NYX 0:85b3fd62ea1a 41 // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
NYX 0:85b3fd62ea1a 42 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
NYX 0:85b3fd62ea1a 43 // Get the functions (dac channel) from the pin and assign it to the object
NYX 0:85b3fd62ea1a 44 uint32_t function = pinmap_function(pin, PinMap_DAC);
NYX 0:85b3fd62ea1a 45 MBED_ASSERT(function != (uint32_t)NC);
NYX 0:85b3fd62ea1a 46
NYX 0:85b3fd62ea1a 47 // Save the channel for the write and read functions
NYX 0:85b3fd62ea1a 48 switch (STM_PIN_CHANNEL(function)) {
NYX 0:85b3fd62ea1a 49 case 1:
NYX 0:85b3fd62ea1a 50 obj->channel = DAC_CHANNEL_1;
NYX 0:85b3fd62ea1a 51 break;
NYX 0:85b3fd62ea1a 52 #if defined(DAC_CHANNEL_2)
NYX 0:85b3fd62ea1a 53 case 2:
NYX 0:85b3fd62ea1a 54 obj->channel = DAC_CHANNEL_2;
NYX 0:85b3fd62ea1a 55 break;
NYX 0:85b3fd62ea1a 56 #endif
NYX 0:85b3fd62ea1a 57 default:
NYX 0:85b3fd62ea1a 58 error("Unknown DAC channel");
NYX 0:85b3fd62ea1a 59 break;
NYX 0:85b3fd62ea1a 60 }
NYX 0:85b3fd62ea1a 61
NYX 0:85b3fd62ea1a 62 if (obj->dac == (DACName)NC) {
NYX 0:85b3fd62ea1a 63 error("DAC pin mapping failed");
NYX 0:85b3fd62ea1a 64 }
NYX 0:85b3fd62ea1a 65
NYX 0:85b3fd62ea1a 66 // Configure GPIO
NYX 0:85b3fd62ea1a 67 pinmap_pinout(pin, PinMap_DAC);
NYX 0:85b3fd62ea1a 68
NYX 0:85b3fd62ea1a 69 __HAL_RCC_GPIOA_CLK_ENABLE();
NYX 0:85b3fd62ea1a 70
NYX 0:85b3fd62ea1a 71 __HAL_RCC_DAC_CLK_ENABLE();
NYX 0:85b3fd62ea1a 72
NYX 0:85b3fd62ea1a 73 obj->handle.Instance = DAC;
NYX 0:85b3fd62ea1a 74 if (HAL_DAC_Init(&obj->handle) != HAL_OK ) {
NYX 0:85b3fd62ea1a 75 error("HAL_DAC_Init failed");
NYX 0:85b3fd62ea1a 76 }
NYX 0:85b3fd62ea1a 77
NYX 0:85b3fd62ea1a 78 sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
NYX 0:85b3fd62ea1a 79 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
NYX 0:85b3fd62ea1a 80
NYX 0:85b3fd62ea1a 81 if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) {
NYX 0:85b3fd62ea1a 82 error("HAL_DAC_ConfigChannel failed");
NYX 0:85b3fd62ea1a 83 }
NYX 0:85b3fd62ea1a 84
NYX 0:85b3fd62ea1a 85 analogout_write_u16(obj, 0);
NYX 0:85b3fd62ea1a 86 }
NYX 0:85b3fd62ea1a 87
NYX 0:85b3fd62ea1a 88 void analogout_free(dac_t *obj) {
NYX 0:85b3fd62ea1a 89 }
NYX 0:85b3fd62ea1a 90
NYX 0:85b3fd62ea1a 91 #endif // DEVICE_ANALOGOUT