Aditya Mehrotra / mbed-dev

Dependents:   CAN_TEST SPIne_Plus_DYNO_SENSORS SPIne_Plus_v2 SPIne_Plus_Dyno_v2

Committer:
adimmit
Date:
Tue Mar 09 20:33:24 2021 +0000
Revision:
3:993b4d6ff61e
Parent:
0:083111ae2a11
added CAN3

Who changed what in which revision?

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