Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
analoginDMA_api.c
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2014, STMicroelectronics 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00020 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00023 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00024 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00025 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 #include "mbed_assert.h" 00029 #include "analoginDMA_api.h" 00030 00031 #if DEVICE_ANALOGIN 00032 00033 #include "wait_api.h" 00034 #include "cmsis.h" 00035 #include "pinmap.h" 00036 #include "PeripheralPins.h" 00037 00038 ADC_HandleTypeDef AdcDMAHandle; 00039 DMA_InitTypeDef DMA_InitStructure; //Variable used to setup the DMA 00040 DMA_HandleTypeDef DMAHandle; 00041 00042 int adcDMA_inited = 0; 00043 00044 void analoginDMA_init(analogin_t *obj, PinName pin) 00045 { 00046 // Get the peripheral name from the pin and assign it to the object 00047 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); 00048 MBED_ASSERT(obj->adc != (ADCName)NC); 00049 00050 // Configure GPIO 00051 pinmap_pinout(pin, PinMap_ADC); 00052 00053 // Save pin number for the read function 00054 obj->pin = pin; 00055 00056 // The ADC initialization is done once 00057 if (adcDMA_inited == 0) { 00058 adcDMA_inited = 1; 00059 00060 // Enable ADC clock 00061 __ADC1_CLK_ENABLE(); 00062 00063 // Configure ADC 00064 AdcDMAHandle.Instance = (ADC_TypeDef *)(obj->adc); 00065 AdcDMAHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; 00066 AdcDMAHandle.Init.Resolution = ADC_RESOLUTION12b; 00067 AdcDMAHandle.Init.ScanConvMode = DISABLE; 00068 AdcDMAHandle.Init.ContinuousConvMode = ENABLE; 00069 AdcDMAHandle.Init.DiscontinuousConvMode = DISABLE; 00070 AdcDMAHandle.Init.NbrOfDiscConversion = 0; 00071 AdcDMAHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; 00072 AdcDMAHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; 00073 AdcDMAHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; 00074 AdcDMAHandle.Init.NbrOfConversion = 1; 00075 AdcDMAHandle.Init.DMAContinuousRequests = ENABLE; 00076 AdcDMAHandle.Init.EOCSelection = ENABLE; 00077 HAL_ADC_Init(&AdcDMAHandle); 00078 00079 } 00080 } 00081 00082 static inline HAL_StatusTypeDef adc_read(analogin_t *obj,uint16_t* pData, uint32_t Length) 00083 { 00084 ADC_ChannelConfTypeDef sConfig; 00085 AdcDMAHandle.Instance = (ADC_TypeDef *)(obj->adc); 00086 00087 // Configure ADC channel 00088 sConfig.Rank = 1; 00089 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; 00090 sConfig.Offset = 0; 00091 00092 switch (obj->pin) { 00093 case PA_0: 00094 sConfig.Channel = ADC_CHANNEL_0; 00095 break; 00096 case PA_1: 00097 sConfig.Channel = ADC_CHANNEL_1; 00098 break; 00099 case PA_2: 00100 sConfig.Channel = ADC_CHANNEL_2; 00101 break; 00102 case PA_3: 00103 sConfig.Channel = ADC_CHANNEL_3; 00104 break; 00105 case PA_4: 00106 sConfig.Channel = ADC_CHANNEL_4; 00107 break; 00108 case PA_5: 00109 sConfig.Channel = ADC_CHANNEL_5; 00110 break; 00111 case PA_6: 00112 sConfig.Channel = ADC_CHANNEL_6; 00113 break; 00114 case PA_7: 00115 sConfig.Channel = ADC_CHANNEL_7; 00116 break; 00117 case PB_0: 00118 sConfig.Channel = ADC_CHANNEL_8; 00119 break; 00120 case PB_1: 00121 sConfig.Channel = ADC_CHANNEL_9; 00122 break; 00123 case PC_0: 00124 sConfig.Channel = ADC_CHANNEL_10; 00125 break; 00126 case PC_1: 00127 sConfig.Channel = ADC_CHANNEL_11; 00128 break; 00129 case PC_2: 00130 sConfig.Channel = ADC_CHANNEL_12; 00131 break; 00132 case PC_3: 00133 sConfig.Channel = ADC_CHANNEL_13; 00134 break; 00135 case PC_4: 00136 sConfig.Channel = ADC_CHANNEL_14; 00137 break; 00138 case PC_5: 00139 sConfig.Channel = ADC_CHANNEL_15; 00140 break; 00141 default: 00142 return HAL_ERROR; 00143 } 00144 00145 HAL_ADC_ConfigChannel(&AdcDMAHandle, &sConfig); 00146 00147 HAL_ADC_Start_DMA(&AdcDMAHandle, (uint32_t*) pData, Length); 00148 00149 // Wait end of conversion and get value 00150 return (HAL_ADC_PollForConversion(&AdcDMAHandle, 1)); 00151 } 00152 00153 HAL_StatusTypeDef analogin_readdMA_u16(analogin_t *obj,uint16_t* pData, uint32_t Length) 00154 { 00155 return adc_read(obj,pData, Length); 00156 00157 } 00158 00159 HAL_StatusTypeDef analoginDMA_read(analogin_t *obj,uint16_t* pData, uint32_t Length) 00160 { 00161 return adc_read(obj, pData, Length); 00162 } 00163 00164 #endif
Generated on Thu Jul 21 2022 04:50:03 by
1.7.2