admin 管理员组文章数量: 887006
stm32duino 文件结构分析
库地址 GitHub - stm32duino/Arduino_Core_STM32: STM32 core support for Arduino
简介
与arduino 相关的文件主要在 /library 与 /corel/arduino 下
最终的实现还是通过LL HAL库实现对寄存器的操作,arduino 对其进行了封装,封装的位置为 /library 与 /corel/arduino 下
使用文件搜索文本探索+KEIL5 加载源文件与头文件 进行文件分析
从最简单的BLINK示列,查看ARDUINO下STM32的调用 过程
函数 定义位置 涉及函数
pinMode wiring_digital.c 见下方定义
digitalWrite wiring_digital.c
digitalRead wiring_digital.c
analogRead wiring_analog.c
analogWrite wiring_analog.c
详细地址为
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.4.0\cores\arduino\wiring_digital.c
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.4.0\cores\arduino\wiring_analog.c
主要涉及函数为 文件 涉及函数 函数
digitalWriteFast digital_io.h digital_io_write LL_GPIO_SetOutputPin
digitalRead digital_io.h digital_io_read LL_GPIO_IsInputPinSet
digitalPinToPinName
dac_stop() analog.cpp
pwm_stop analog.cpp
pin_in_pinmap
pin_function
详细地址为:
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.4.0\cores\arduino\wiring_digital.c
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.4.0\cores\arduino\wiring_analog.c
详见下方源文件 ,通过调用LL HAL库进行操作,最终达到对寄存器进行修改设置
corel/arduino 内的analogWrite analogRead 使用 digital_write() ;内部主要使用 pwm_start(),adc_xxx_() (定义所在地址为 Library的xx文件中 ,详见下方代码,注释*** 头文件处)
源文件中关于 pinMode digitalRead digitalWrite,analogRead,analogWrite的定义
##Wiring 中关于 pinMode digitalRead digitalWrite的定义
C:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.4.0\cores\arduino\wiring_digital.cC:\Users\Administrator\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.4.0\cores\arduino\wiring_analog.cvoid pinMode(uint32_t ulPin, uint32_t ulMode)
{PinName p = digitalPinToPinName(ulPin);if (p != NC) {// If the pin that support PWM or DAC output, we need to turn it off
#if (defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)) ||\(defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY))if (is_pin_configured(p, g_anOutputPinConfigured)) {
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_DAC)) {dac_stop(p);} else
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_TIM)) {pwm_stop(p);}
#endif //HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY{reset_pin_configured(p, g_anOutputPinConfigured);}}
#endifswitch (ulMode) {case INPUT: /* INPUT_FLOATING */pin_function(p, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));break;case INPUT_PULLUP:pin_function(p, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLUP, 0));break;case INPUT_PULLDOWN:pin_function(p, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLDOWN, 0));break;case INPUT_ANALOG:pin_function(p, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));break;case OUTPUT:pin_function(p, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));break;case OUTPUT_OPEN_DRAIN:pin_function(p, STM_PIN_DATA(STM_MODE_OUTPUT_OD, GPIO_NOPULL, 0));break;default:Error_Handler();break;}}
}void digitalWrite(uint32_t ulPin, uint32_t ulVal)
{digitalWriteFast(digitalPinToPinName(ulPin), ulVal);
}int digitalRead(uint32_t ulPin)
{return digitalReadFast(digitalPinToPinName(ulPin));
}void digitalToggle(uint32_t ulPin)
{digitalToggleFast(digitalPinToPinName(ulPin));
}
#WIRING 中 关于 analogRead analogWrite的定义 uint32_t analogRead(uint32_t ulPin)
{uint32_t value = 0;
#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)PinName p = analogInputToPinName(ulPin);if (p != NC) {value = adc_read_value(p, _internalReadResolution);value = mapResolution(value, _internalReadResolution, _readResolution);}
#elseUNUSED(ulPin);
#endifreturn value;
}void analogOutputInit(void)
{
}// Right now, PWM output only works on the pins with
// hardware support. These are defined in the appropriate
// variant.cpp file. For the rest of the pins, we default
// to digital output.
void analogWrite(uint32_t ulPin, uint32_t ulValue)
{
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)uint8_t do_init = 0;
#endifPinName p = digitalPinToPinName(ulPin);if (p != NC) {
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_DAC)) {if (is_pin_configured(p, g_anOutputPinConfigured) == false) {do_init = 1;set_pin_configured(p, g_anOutputPinConfigured);}ulValue = mapResolution(ulValue, _writeResolution, DACC_RESOLUTION);dac_write_value(p, ulValue, do_init);} else
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_TIM)) {if (is_pin_configured(p, g_anOutputPinConfigured) == false) {set_pin_configured(p, g_anOutputPinConfigured);}ulValue = mapResolution(ulValue, _writeResolution, _internalWriteResolution);pwm_start(p, _writeFreq, ulValue, _internalWriteResolution);} else
#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */{//DIGITAL PIN ONLY// Defaults to digital writepinMode(ulPin, OUTPUT);ulValue = mapResolution(ulValue, _writeResolution, 8);if (ulValue < 128) {digitalWrite(ulPin, LOW);} else {digitalWrite(ulPin, HIGH);}}}
}
digitalRead digitalWrite的具体实现
D:\stm32\STMicroelectronics\hardware\stm32\2.4.0\cores\arduino\stm32\digital_io.hstatic inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val)
{if (val) {LL_GPIO_SetOutputPin(port, pin);} else {LL_GPIO_ResetOutputPin(port, pin);}
}/*** @brief This function read the value of an IO* @param port : one of the gpio port* @param pin : one of the gpio pin* @retval The pin state (LOW or HIGH)*/
static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin)
{return LL_GPIO_IsInputPinSet(port, pin);
}/*** @brief This function toggle value of an IO* @param port : one of the gpio port* @param pin : one of the gpio pin* @retval None*/
static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin)
{LL_GPIO_TogglePin(port, pin);
}/*** @brief This function set a value to an IO* @param pn : Pin name* @param val : 0 to set to low, any other value to set to high* @retval None*/
static inline void digitalWriteFast(PinName pn, uint32_t ulVal)
{digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn), ulVal);
}/*** @brief This function read the value of an IO* @param pn : Pin name* @retval The pin state (LOW or HIGH)*/
static inline int digitalReadFast(PinName pn)
{uint8_t level = 0;level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));return (level) ? HIGH : LOW;
}/*** @brief This function toggle value of an IO* @param port : one of the gpio port* @param pin : one of the gpio pin* @retval None*/
static inline void digitalToggleFast(PinName pn)
{digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));
}#ifdef __cplusplus
}
#endif#endif /* __DIGITAL_IO_H *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
dac_stop pwm_start 具体实现
D:\stm32\STMicroelectronics\hardware\stm32\2.4.0\libraries\SrcWrapper\src\stm32\analog.cpp/* Exported Functions */
/*** @brief Return ADC HAL channel linked to a PinName* @param pin: PinName* @param bank: pointer to get ADC channel bank if required* @retval Valid HAL channel*/
uint32_t get_adc_channel(PinName pin, uint32_t *bank)
{uint32_t function = pinmap_function(pin, PinMap_ADC);uint32_t channel = 0;switch (STM_PIN_CHANNEL(function)) {
#ifdef ADC_CHANNEL_0case 0:channel = ADC_CHANNEL_0;break;
#endifcase 1:channel = ADC_CHANNEL_1;break;case 2:channel = ADC_CHANNEL_2;break;case 3:channel = ADC_CHANNEL_3;break;case 4:channel = ADC_CHANNEL_4;break;case 5:channel = ADC_CHANNEL_5;break;case 6:channel = ADC_CHANNEL_6;break;case 7:channel = ADC_CHANNEL_7;break;case 8:channel = ADC_CHANNEL_8;break;case 9:channel = ADC_CHANNEL_9;break;case 10:channel = ADC_CHANNEL_10;break;case 11:channel = ADC_CHANNEL_11;break;case 12:channel = ADC_CHANNEL_12;break;case 13:channel = ADC_CHANNEL_13;break;case 14:channel = ADC_CHANNEL_14;break;case 15:channel = ADC_CHANNEL_15;break;
#ifdef ADC_CHANNEL_16case 16:channel = ADC_CHANNEL_16;break;
#endifcase 17:channel = ADC_CHANNEL_17;break;
#ifdef ADC_CHANNEL_18case 18:channel = ADC_CHANNEL_18;break;
#endif
#ifdef ADC_CHANNEL_19case 19:channel = ADC_CHANNEL_19;break;
#endif
#ifdef ADC_CHANNEL_20case 20:channel = ADC_CHANNEL_20;break;case 21:channel = ADC_CHANNEL_21;break;case 22:channel = ADC_CHANNEL_22;break;case 23:channel = ADC_CHANNEL_23;break;
#ifdef ADC_CHANNEL_24case 24:channel = ADC_CHANNEL_24;break;case 25:channel = ADC_CHANNEL_25;break;case 26:channel = ADC_CHANNEL_26;break;
#ifdef ADC_CHANNEL_27case 27:channel = ADC_CHANNEL_27;break;case 28:channel = ADC_CHANNEL_28;break;case 29:channel = ADC_CHANNEL_29;break;case 30:channel = ADC_CHANNEL_30;break;case 31:channel = ADC_CHANNEL_31;break;
#endif
#endif
#endifdefault:_Error_Handler("ADC: Unknown adc channel", (int)(STM_PIN_CHANNEL(function)));break;}
#ifdef ADC_CHANNELS_BANK_Bif (STM_PIN_ANALOG_CHANNEL_BANK_B(function)) {*bank = ADC_CHANNELS_BANK_B;} else {*bank = ADC_CHANNELS_BANK_A;}
#elseUNUSED(bank);
#endifreturn channel;
}/*** @brief Return ADC HAL internal channel linked to a PinName* @param pin: specific PinName's for ADC internal. Value can be:* PADC_TEMP, PADC_TEMP_ADC5, PADC_VREF, PADC_VBAT* Note that not all of these values may be available for all series.* @retval Valid HAL internal channel.*/
uint32_t get_adc_internal_channel(PinName pin)
{uint32_t channel = 0;switch (pin) {
#if defined(ADC_CHANNEL_TEMPSENSOR)case PADC_TEMP:channel = ADC_CHANNEL_TEMPSENSOR;break;
#endif
#if defined(ADC_CHANNEL_TEMPSENSOR_ADC1)case PADC_TEMP:channel = ADC_CHANNEL_TEMPSENSOR_ADC1;break;
#endif
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)case PADC_TEMP_ADC5:channel = ADC_CHANNEL_TEMPSENSOR_ADC5;break;
#endif
#ifdef ADC_CHANNEL_VREFINTcase PADC_VREF:channel = ADC_CHANNEL_VREFINT;break;
#endif
#ifdef ADC_CHANNEL_VBATcase PADC_VBAT:channel = ADC_CHANNEL_VBAT;break;
#endifdefault:_Error_Handler("ADC: Unknown adc internal PiName", (int)(pin));break;}return channel;
}
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY */#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
/*** @brief Return DAC HAL channel linked to a PinName* @param pin: specific PinName's for ADC internal.* @retval Valid HAL channel*/
uint32_t get_dac_channel(PinName pin)
{uint32_t function = pinmap_function(pin, PinMap_DAC);uint32_t channel = 0;switch (STM_PIN_CHANNEL(function)) {
#ifdef DAC_CHANNEL_0case 0:channel = DAC_CHANNEL_0;break;
#endifcase 1:channel = DAC_CHANNEL_1;break;
#ifdef DAC_CHANNEL_2case 2:channel = DAC_CHANNEL_2;break;
#endifdefault:_Error_Handler("DAC: Unknown dac channel", (int)(STM_PIN_CHANNEL(function)));break;}return channel;
}// DAC INTERFACE FUNCTIONS //*** @brief DAC MSP Initialization* This function configures the hardware resources used in this example:* - Peripheral's clock enable* - Peripheral's GPIO Configuration* @param hdac: DAC handle pointer* @retval None*/
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
{/* DAC Periph clock enable */if (hdac->Instance == DAC1) {
#ifdef __HAL_RCC_DAC_CLK_ENABLE__HAL_RCC_DAC_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC1_CLK_ENABLE__HAL_RCC_DAC1_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#ifdef DAC2else if (hdac->Instance == DAC2) {
#ifdef __HAL_RCC_DAC2_CLK_ENABLE__HAL_RCC_DAC2_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#endif
#ifdef DAC3else if (hdac->Instance == DAC3) {
#ifdef __HAL_RCC_DAC3_CLK_ENABLE__HAL_RCC_DAC3_CLK_ENABLE();
#endif}
#endif
#ifdef DAC4else if (hdac->Instance == DAC4) {
#ifdef __HAL_RCC_DAC4_CLK_ENABLE__HAL_RCC_DAC4_CLK_ENABLE();
#endif}
#endif/* Configure DAC GPIO pins */pinmap_pinout(g_current_pin, PinMap_DAC);
}/*** @brief This function will set the DAC to the required value* @param port : the gpio port to use* @param pin : the gpio pin to use* @param value : the value to push on the adc output* @param do_init : if set to 1 the initialization of the adc is done* @retval None*/
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init)
{DAC_HandleTypeDef DacHandle = {};DAC_ChannelConfTypeDef dacChannelConf = {};uint32_t dacChannel;DacHandle.Instance = (DAC_TypeDef *)pinmap_peripheral(pin, PinMap_DAC);if (DacHandle.Instance == NP) {return;}dacChannel = get_dac_channel(pin);
#if defined(STM32G4xx)if (!IS_DAC_CHANNEL(DacHandle.Instance, dacChannel)) {
#elseif (!IS_DAC_CHANNEL(dacChannel)) {
#endifreturn;}if (do_init == 1) {/*##-1- Configure the DAC peripheral #######################################*/g_current_pin = pin;if (HAL_DAC_Init(&DacHandle) != HAL_OK) {/* Initialization Error */return;}dacChannelConf.DAC_Trigger = DAC_TRIGGER_NONE;dacChannelConf.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
#if defined(DAC_OUTPUTSWITCH_ENABLE)dacChannelConf.DAC_OutputSwitch = DAC_OUTPUTSWITCH_ENABLE;
#endif/*##-2- Configure DAC channel1 #############################################*/if (HAL_DAC_ConfigChannel(&DacHandle, &dacChannelConf, dacChannel) != HAL_OK) {/* Channel configuration Error */return;}}/*##-3- Set DAC Channel1 DHR register ######################################*/if (HAL_DAC_SetValue(&DacHandle, dacChannel, DAC_ALIGN_12B_R, value) != HAL_OK) {/* Setting value Error */return;}/*##-4- Enable DAC Channel1 ################################################*/HAL_DAC_Start(&DacHandle, dacChannel);
}/*** @brief DeInitialize the DAC MSP.* @param hdac: pointer to a DAC_HandleTypeDef structure that contains* the configuration information for the specified DAC.* @retval None*/
void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
{/* DAC Periph clock disable */if (hdac->Instance == DAC1) {
#ifdef __HAL_RCC_DAC_FORCE_RESET__HAL_RCC_DAC_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC1_FORCE_RESET__HAL_RCC_DAC1_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_FORCE_RESET__HAL_RCC_DAC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC_RELEASE_RESET__HAL_RCC_DAC_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC1_RELEASE_RESET__HAL_RCC_DAC1_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_RELEASE_RESET__HAL_RCC_DAC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC_CLK_DISABLE__HAL_RCC_DAC_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_DAC1_CLK_DISABLE__HAL_RCC_DAC1_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#ifdef DAC2else if (hdac->Instance == DAC2) {
#ifdef __HAL_RCC_DAC2_FORCE_RESET__HAL_RCC_DAC2_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_FORCE_RESET__HAL_RCC_DAC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC2_RELEASE_RESET__HAL_RCC_DAC2_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_RELEASE_RESET__HAL_RCC_DAC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC2_CLK_ENABLE__HAL_RCC_DAC2_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#endif
#ifdef DAC3else if (hdac->Instance == DAC3) {
#ifdef __HAL_RCC_DAC3_FORCE_RESET__HAL_RCC_DAC3_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC3_RELEASE_RESET__HAL_RCC_DAC3_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC3_CLK_DISABLE__HAL_RCC_DAC3_CLK_DISABLE();
#endif}
#endif
#ifdef DAC4else if (hdac->Instance == DAC4) {
#ifdef __HAL_RCC_DAC4_FORCE_RESET__HAL_RCC_DAC4_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC4_RELEASE_RESET__HAL_RCC_DAC4_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC4_CLK_DISABLE__HAL_RCC_DAC4_CLK_DISABLE();
#endif}
#endif
}/*** @brief This function will stop the DAC* @param port : the gpio port to use* @param pin : the gpio pin to use* @retval None*/
void dac_stop(PinName pin)
{DAC_HandleTypeDef DacHandle;uint32_t dacChannel;DacHandle.Instance = (DAC_TypeDef *)pinmap_peripheral(pin, PinMap_DAC);if (DacHandle.Instance == NP) {return;}dacChannel = get_dac_channel(pin);
#if defined(STM32G4xx)if (!IS_DAC_CHANNEL(DacHandle.Instance, dacChannel)) {
#elseif (!IS_DAC_CHANNEL(dacChannel)) {
#endifreturn;}HAL_DAC_Stop(&DacHandle, dacChannel);if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) {/* DeInitialization Error */return;}
}
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)
// ADC INTERFACE FUNCTIONS //*** @brief ADC MSP Initialization* This function configures the hardware resources used in this example:* - Peripheral's clock enable* - Peripheral's GPIO Configuration* @param hadc: ADC handle pointer* @retval None*/
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
{/*##-1- Enable peripherals and GPIO Clocks #################################*//* ADC Periph clock enable */if (hadc->Instance == ADC1) {
#ifdef __HAL_RCC_ADC1_CLK_ENABLE__HAL_RCC_ADC1_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC12_CLK_ENABLE__HAL_RCC_ADC12_CLK_ENABLE();
#endif}
#ifdef ADC2else if (hadc->Instance == ADC2) {
#ifdef __HAL_RCC_ADC2_CLK_ENABLE__HAL_RCC_ADC2_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC12_CLK_ENABLE__HAL_RCC_ADC12_CLK_ENABLE();
#endif}
#endif
#ifdef ADC3else if (hadc->Instance == ADC3) {
#ifdef __HAL_RCC_ADC3_CLK_ENABLE__HAL_RCC_ADC3_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC34_CLK_ENABLE__HAL_RCC_ADC34_CLK_ENABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_CLK_ENABLE();
#endif}
#endif
#ifdef ADC4else if (hadc->Instance == ADC4) {
#ifdef __HAL_RCC_ADC4_CLK_ENABLE__HAL_RCC_ADC4_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC34_CLK_ENABLE__HAL_RCC_ADC34_CLK_ENABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_CLK_ENABLE();
#endif}
#endif
#ifdef ADC5else if (hadc->Instance == ADC5) {
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_CLK_ENABLE();
#endif}
#endif
#ifdef __HAL_RCC_ADC_CLK_ENABLE__HAL_RCC_ADC_CLK_ENABLE();
#endif/* For STM32F1xx, STM32H7xx, and STM32MP1xx ADC prescaler is configured inSystemClock_Config (variant.cpp) */
#if defined(__HAL_RCC_ADC_CONFIG) && !defined(STM32F1xx) && \!defined(STM32H7xx) && !defined(STM32MP1xx)hsem_lock(CFG_HW_RCC_CRRCR_CCIPR_SEMID, HSEM_LOCK_DEFAULT_RETRY);/* ADC Periph interface clock configuration */__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);hsem_unlock(CFG_HW_RCC_CRRCR_CCIPR_SEMID);
#endif/* Configure ADC GPIO pin */if (!(g_current_pin & PADC_BASE)) {pinmap_pinout(g_current_pin, PinMap_ADC);}
}/*** @brief DeInitializes the ADC MSP.* @param hadc: ADC handle* @retval None*/
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
{
#ifdef __HAL_RCC_ADC_FORCE_RESET__HAL_RCC_ADC_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC_RELEASE_RESET__HAL_RCC_ADC_RELEASE_RESET();
#endifif (hadc->Instance == ADC1) {
#ifdef __HAL_RCC_ADC1_FORCE_RESET__HAL_RCC_ADC1_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC1_RELEASE_RESET__HAL_RCC_ADC1_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_FORCE_RESET__HAL_RCC_ADC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_RELEASE_RESET__HAL_RCC_ADC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC1_CLK_DISABLE__HAL_RCC_ADC1_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC12_CLK_DISABLE__HAL_RCC_ADC12_CLK_DISABLE();
#endif}
#ifdef ADC2else if (hadc->Instance == ADC2) {
#ifdef __HAL_RCC_ADC2_FORCE_RESET__HAL_RCC_ADC2_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC2_RELEASE_RESET__HAL_RCC_ADC2_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_FORCE_RESET__HAL_RCC_ADC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_RELEASE_RESET__HAL_RCC_ADC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC2_CLK_DISABLE__HAL_RCC_ADC2_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC2_CLK_DISABLE__HAL_RCC_ADC2_CLK_DISABLE();
#endif}
#endif
#ifdef ADC3else if (hadc->Instance == ADC3) {
#ifdef __HAL_RCC_ADC3_FORCE_RESET__HAL_RCC_ADC3_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC3_RELEASE_RESET__HAL_RCC_ADC3_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_FORCE_RESET__HAL_RCC_ADC34_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_RELEASE_RESET__HAL_RCC_ADC34_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC3_CLK_DISABLE__HAL_RCC_ADC3_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC34_CLK_DISABLE__HAL_RCC_ADC34_CLK_DISABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_FORCE_RESET();__HAL_RCC_ADC345_RELEASE_RESET();__HAL_RCC_ADC345_CLK_DISABLE();
#endif}
#endif
#ifdef ADC4else if (hadc->Instance == ADC4) {
#ifdef __HAL_RCC_ADC4_FORCE_RESET__HAL_RCC_ADC4_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC4_RELEASE_RESET__HAL_RCC_ADC4_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC4_CLK_DISABLE__HAL_RCC_ADC4_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC34_FORCE_RESET__HAL_RCC_ADC34_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_RELEASE_RESET__HAL_RCC_ADC34_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_CLK_DISABLE__HAL_RCC_ADC34_CLK_DISABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_FORCE_RESET();__HAL_RCC_ADC345_RELEASE_RESET();__HAL_RCC_ADC345_CLK_DISABLE();
#endif}
#endif
#ifdef ADC5else if (hadc->Instance == ADC5) {
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_FORCE_RESET();__HAL_RCC_ADC345_RELEASE_RESET();__HAL_RCC_ADC345_CLK_DISABLE();
#endif}
#endif
#ifdef __HAL_RCC_ADC_CLK_DISABLE__HAL_RCC_ADC_FORCE_RESET();__HAL_RCC_ADC_RELEASE_RESET();__HAL_RCC_ADC_CLK_DISABLE();
#endif
}/*** @brief This function will set the ADC to the required value* @param pin : the pin to use* @param resolution : resolution for converted data: 6/8/10/12/14/16* @retval the value of the adc*/
uint16_t adc_read_value(PinName pin, uint32_t resolution)
{ADC_HandleTypeDef AdcHandle = {};ADC_ChannelConfTypeDef AdcChannelConf = {};__IO uint16_t uhADCxConvertedValue = 0;uint32_t samplingTime = ADC_SAMPLINGTIME;uint32_t channel = 0;uint32_t bank = 0;if ((pin & PADC_BASE) && (pin < ANA_START)) {
#if defined(STM32H7xx) || defined(STM32MP1xx)
#ifdef ADC3AdcHandle.Instance = ADC3;
#elseAdcHandle.Instance = ADC2;
#endif
#elseAdcHandle.Instance = ADC1;
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)if (pin == PADC_TEMP_ADC5) {AdcHandle.Instance = ADC5;}
#endif
#endifchannel = get_adc_internal_channel(pin);samplingTime = ADC_SAMPLINGTIME_INTERNAL;} else {AdcHandle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);channel = get_adc_channel(pin, &bank);
#if defined(ADC_VER_V5_V90)if (AdcHandle.Instance == ADC3) {samplingTime = ADC3_SAMPLINGTIME;}
#endif
#if defined(ADC4_SAMPLINGTIME)if (AdcHandle.Instance == ADC4) {samplingTime = ADC4_SAMPLINGTIME;}
#endif}if (AdcHandle.Instance == NP) {return 0;}#ifdef ADC_CLOCK_DIVAdcHandle.Init.ClockPrescaler = ADC_CLOCK_DIV; /* (A)synchronous clock mode, input ADC clock divided */
#endif
#ifdef ADC_RESOLUTION_12Bswitch (resolution) {
#ifdef ADC_RESOLUTION_6Bcase 6:AdcHandle.Init.Resolution = ADC_RESOLUTION_6B; /* resolution for converted data */break;
#endifcase 8:AdcHandle.Init.Resolution = ADC_RESOLUTION_8B; /* resolution for converted data */break;case 10:AdcHandle.Init.Resolution = ADC_RESOLUTION_10B; /* resolution for converted data */break;case 12:default:AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* resolution for converted data */break;
#ifdef ADC_RESOLUTION_14Bcase 14:AdcHandle.Init.Resolution = ADC_RESOLUTION_14B; /* resolution for converted data */break;
#endif
#ifdef ADC_RESOLUTION_16Bcase 16:AdcHandle.Init.Resolution = ADC_RESOLUTION_16B; /* resolution for converted data */break;
#endif}
#elseUNUSED(resolution);
#endif
#ifdef ADC_DATAALIGN_RIGHTAdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Right-alignment for converted data */
#endif
#ifdef ADC_SCAN_SEQ_FIXEDAdcHandle.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
#elseAdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
#endif
#ifdef ADC_EOC_SINGLE_CONVAdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicate conversion end */
#endif
#if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32F4xx) && \!defined(STM32F7xx) && !defined(ADC1_V2_5)AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled */
#endif
#if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32F3xx) && \!defined(STM32F4xx) && !defined(STM32F7xx) && !defined(STM32G4xx) && \!defined(STM32H7xx) && !defined(STM32L4xx) && !defined(STM32L5xx) && \!defined(STM32MP1xx) && !defined(STM32WBxx)AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; /* ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered */
#endif
#ifdef ADC_CHANNELS_BANK_BAdcHandle.Init.ChannelsBank = bank;
#elif defined(ADC_CHANNELS_BANK_A)AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
#endifAdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */
#if !defined(STM32F0xx) && !defined(STM32L0xx)AdcHandle.Init.NbrOfConversion = 1; /* Specifies the number of ranks that will be converted within the regular group sequencer. */
#endifAdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx) && \!defined(STM32WLxx)AdcHandle.Init.NbrOfDiscConversion = 0; /* Parameter discarded because sequencer is disabled */
#endifAdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external event */
#if !defined(STM32F1xx) && !defined(ADC1_V2_5)AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
#endif
#if !defined(STM32F1xx) && !defined(STM32H7xx) && !defined(STM32MP1xx) && \!defined(ADC1_V2_5)AdcHandle.Init.DMAContinuousRequests = DISABLE; /* DMA one-shot mode selected (not applied to this example) */
#endif
#ifdef ADC_CONVERSIONDATA_DRAdcHandle.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; /* Regular Conversion data stored in DR register only */
#endif
#ifdef ADC_OVR_DATA_OVERWRITTENAdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is overwritten with the last conversion result in case of overrun */
#endif
#ifdef ADC_LEFTBITSHIFT_NONEAdcHandle.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; /* No bit shift left applied on the final ADC conversion data */
#endif#if defined(STM32F0xx)AdcHandle.Init.SamplingTimeCommon = samplingTime;
#endif
#if defined(STM32G0xx) || defined(STM32U5xx) || defined(STM32WLxx)AdcHandle.Init.SamplingTimeCommon1 = samplingTime; /* Set sampling time common to a group of channels. */AdcHandle.Init.SamplingTimeCommon2 = samplingTime; /* Set sampling time common to a group of channels, second common setting possible.*/
#endif
#if defined(STM32L0xx)AdcHandle.Init.LowPowerFrequencyMode = DISABLE; /* To be enabled only if ADC clock < 2.8 MHz */AdcHandle.Init.SamplingTime = samplingTime;
#endif
#if !defined(STM32F0xx) && !defined(STM32F1xx) && !defined(STM32F2xx) && \!defined(STM32F3xx) && !defined(STM32F4xx) && !defined(STM32F7xx) && \!defined(STM32L1xx)AdcHandle.Init.OversamplingMode = DISABLE;/* AdcHandle.Init.Oversample ignore for STM32L0xx as oversampling disabled *//* AdcHandle.Init.Oversampling ignored for other as oversampling disabled */
#endif
#if defined(ADC_CFGR_DFSDMCFG) && defined(DFSDM1_Channel0)AdcHandle.Init.DFSDMConfig = ADC_DFSDM_MODE_DISABLE; /* ADC conversions are not transferred by DFSDM. */
#endif
#ifdef ADC_TRIGGER_FREQ_HIGHAdcHandle.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
#endif
#ifdef ADC_VREF_PPROT_NONEAdcHandle.Init.VrefProtection = ADC_VREF_PPROT_NONE;
#endifAdcHandle.State = HAL_ADC_STATE_RESET;AdcHandle.DMA_Handle = NULL;AdcHandle.Lock = HAL_UNLOCKED;/* Some other ADC_HandleTypeDef fields exists but not required */g_current_pin = pin; /* Needed for HAL_ADC_MspInit*/if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {return 0;}AdcChannelConf.Channel = channel; /* Specifies the channel to configure into ADC */#if defined(STM32G4xx) || defined(STM32L4xx) || defined(STM32L5xx) || \defined(STM32WBxx)if (!IS_ADC_CHANNEL(&AdcHandle, AdcChannelConf.Channel)) {
#elseif (!IS_ADC_CHANNEL(AdcChannelConf.Channel)) {
#endifreturn 0;}
#if defined(ADC_SCAN_SEQ_FIXED) && defined(ADC_RANK_CHANNEL_NUMBER)AdcChannelConf.Rank = ADC_RANK_CHANNEL_NUMBER; /* Enable the rank of the selected channels when not fully configurable */
#elseAdcChannelConf.Rank = ADC_REGULAR_RANK_1; /* Specifies the rank in the regular group sequencer */
#endif
#if !defined(STM32L0xx)
#if !defined(STM32G0xx)AdcChannelConf.SamplingTime = samplingTime; /* Sampling time value to be set for the selected channel */
#elseAdcChannelConf.SamplingTime = ADC_SAMPLINGTIME_COMMON_1; /* Sampling time value to be set for the selected channel */
#endif
#endif
#if defined(ADC_DIFFERENTIAL_ENDED) && !defined(ADC1_V2_5)AdcChannelConf.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */AdcChannelConf.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
#endif
#if !defined(STM32F0xx) && !defined(STM32F1xx) && !defined(STM32F2xx) && \!defined(STM32G0xx) && !defined(STM32L0xx) && !defined(STM32L1xx) && \!defined(STM32WBxx) && !defined(STM32WLxx) && \!defined(ADC1_V2_5)AdcChannelConf.Offset = 0; /* Parameter discarded because offset correction is disabled */
#endif
#if defined (STM32H7xx) || defined(STM32MP1xx)AdcChannelConf.OffsetRightShift = DISABLE; /* No Right Offset Shift */AdcChannelConf.OffsetSignedSaturation = DISABLE; /* Signed saturation feature is not used */
#endif/*##-2- Configure ADC regular channel ######################################*/if (HAL_ADC_ConfigChannel(&AdcHandle, &AdcChannelConf) != HAL_OK) {/* Channel Configuration Error */return 0;}#if defined(ADC_CR_ADCAL) || defined(ADC_CR2_RSTCAL)/*##-2.1- Calibrate ADC then Start the conversion process ####################*/
#if defined(ADC_CALIB_OFFSET)if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
#elif defined(ADC_SINGLE_ENDED) && !defined(ADC1_V2_5)if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
#elseif (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)
#endif{/* ADC Calibration Error */return 0;}
#endif/*##-3- Start the conversion process ####################*/if (HAL_ADC_Start(&AdcHandle) != HAL_OK) {/* Start Conversion Error */return 0;}/*##-4- Wait for the end of conversion #####################################*//* For simplicity reasons, this example is just waiting till the end of theconversion, but application may perform other tasks while conversionoperation is ongoing. */if (HAL_ADC_PollForConversion(&AdcHandle, 10) != HAL_OK) {/* End Of Conversion flag not set on time */return 0;}/* Check if the continuous conversion of regular channel is finished */if ((HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC) {/*##-5- Get the converted value of regular channel ########################*/uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);}if (HAL_ADC_Stop(&AdcHandle) != HAL_OK) {/* Stop Conversation Error */return 0;}if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) {return 0;}if (__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance) != 0U) {LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance), LL_ADC_PATH_INTERNAL_NONE);}return uhADCxConvertedValue;
}
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY*/#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
// PWM INTERFACE FUNCTIONS //*** @brief This function will set the PWM to the required value* @param port : the gpio port to use* @param pin : the gpio pin to use* @param clock_freq : frequency of the tim clock* @param value : the value to push on the PWM output* @retval None*/
void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value, TimerCompareFormat_t resolution)
{TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM);HardwareTimer *HT;TimerModes_t previousMode;uint32_t index = get_timer_index(Instance);if (HardwareTimer_Handle[index] == NULL) {HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM));}HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin, PinMap_TIM));previousMode = HT->getMode(channel);if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);}HT->setOverflow(PWM_freq, HERTZ_FORMAT);HT->setCaptureCompare(channel, value, resolution);if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {HT->resume();}
}
/*** @brief This function will disable the PWM* @param port : the gpio port to use* @param pin : the gpio pin to use* @retval None*/
void pwm_stop(PinName pin)
{TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM);HardwareTimer *HT;uint32_t index = get_timer_index(Instance);if (HardwareTimer_Handle[index] == NULL) {HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM));}HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);if (HT != NULL) {delete (HT);HT = NULL;}
}
D:\stm32\STMicroelectronics\hardware\stm32\2.4.0\libraries\SrcWrapper\src\stm32\analog.cpp
帮助 wiki
system:stm32的启动文件
corel/arduino :包含与arduino相关的文件
library:示列,以及于ARDUINO相关的函数(SRCWRAPPER文件)
variant:stm32不同板子的信息
stm32 外设时钟使能与arduino中
目录:corel/arduino
相关文件:
涉及相应函数 来源于LL
相关文件 | 相关函数 | |
wiring.h | 主要函数 | |
wring_analog.c | digital_wirte() digitalPinToPinName()[pins_arduino.h] | |
wring_digital.c | digitalWriteFast() digitalReadFast() | |
#----------WIRING_ANALOG.Cuint32_t analogRead(uint32_t ulPin) *************
{uint32_t value = 0;
#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)PinName p = analogInputToPinName(ulPin);if (p != NC) {value = adc_read_value(p, _internalReadResolution);value = mapResolution(value, _internalReadResolution, _readResolution);}
#elseUNUSED(ulPin);
#endifreturn value;
}void analogWrite(uint32_t ulPin, uint32_t ulValue) **************
{
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)uint8_t do_init = 0;
#endifPinName p = digitalPinToPinName(ulPin);if (p != NC) {
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_DAC)) {if (is_pin_configured(p, g_anOutputPinConfigured) == false) {do_init = 1;set_pin_configured(p, g_anOutputPinConfigured); *****************}ulValue = mapResolution(ulValue, _writeResolution, DACC_RESOLUTION);dac_write_value(p, ulValue, do_init); *******************} else
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_TIM)) {if (is_pin_configured(p, g_anOutputPinConfigured) == false) {set_pin_configured(p, g_anOutputPinConfigured);}ulValue = mapResolution(ulValue, _writeResolution, _internalWriteResolution);pwm_start(p, _writeFreq, ulValue, _internalWriteResolution);} else
#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */{//DIGITAL PIN ONLY// Defaults to digital writepinMode(ulPin, OUTPUT); // ***************ulValue = mapResolution(ulValue, _writeResolution, 8);if (ulValue < 128) {digitalWrite(ulPin, LOW); //(********** DIGITALWRITE} else {digitalWrite(ulPin, HIGH);}}}
}------------------------------------------------------------------#WIRING_DIGITAL.Cvoid digitalWrite(uint32_t ulPin, uint32_t ulVal)
{digitalWriteFast(digitalPinToPinName(ulPin), ulVal);
}int digitalRead(uint32_t ulPin)
{return digitalReadFast(digitalPinToPinName(ulPin));
}void digitalToggle(uint32_t ulPin)
{digitalToggleFast(digitalPinToPinName(ulPin));
}void pinMode(uint32_t ulPin, uint32_t ulMode)
{PinName p = digitalPinToPinName(ulPin); ***************if (p != NC) {// If the pin that support PWM or DAC output, we need to turn it off
#if (defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)) ||\(defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY))if (is_pin_configured(p, g_anOutputPinConfigured)) {
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_DAC)) {dac_stop(p); *********主要函数} else
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)if (pin_in_pinmap(p, PinMap_TIM)) {pwm_stop(p); *******主要函数}
#endif //HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY{reset_pin_configured(p, g_anOutputPinConfigured); **********}}
#endifswitch (ulMode) {case INPUT: /* INPUT_FLOATING */pin_function(p, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));break;case INPUT_PULLUP:pin_function(p, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLUP, 0));break;case INPUT_PULLDOWN:pin_function(p, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLDOWN, 0));break;case INPUT_ANALOG:pin_function(p, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));break;case OUTPUT:pin_function(p, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));break;case OUTPUT_OPEN_DRAIN:pin_function(p, STM_PIN_DATA(STM_MODE_OUTPUT_OD, GPIO_NOPULL, 0));break;default:Error_Handler();break;}}
}-------------------------------------------------------------
#HARDWARESERIAL.CPPvoid HardwareSerial::begin(unsigned long baud, byte config)
{uint32_t databits = 0;uint32_t stopbits = 0;uint32_t parity = 0;_baud = baud;_config = config;// Manage databitsswitch (config & 0x07) {case 0x02:databits = 6;break;case 0x04:databits = 7;break;case 0x06:databits = 8;break;default:databits = 0;break;}if ((config & 0x30) == 0x30) {parity = UART_PARITY_ODD;databits++;} else if ((config & 0x20) == 0x20) {parity = UART_PARITY_EVEN;databits++;} else {parity = UART_PARITY_NONE;}if ((config & 0x08) == 0x08) {stopbits = UART_STOPBITS_2;} else {stopbits = UART_STOPBITS_1;}switch (databits) {
#ifdef UART_WORDLENGTH_7Bcase 7:databits = UART_WORDLENGTH_7B;break;
#endifcase 8:databits = UART_WORDLENGTH_8B;break;case 9:databits = UART_WORDLENGTH_9B;break;default:case 0:Error_Handler();break;}uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits); **********enableHalfDuplexRx();uart_attach_rx_callback(&_serial, _rx_complete_irq);
}-------------------------------------------------------------
#PINS_ARDUINO.H
**digitalPinToPinName#define digitalPinToPinName(p) ((((uint32_t)(p) & PNUM_MASK) < NUM_DIGITAL_PINS) ? \(PinName)(digitalPin[(uint32_t)(p) & PNUM_MASK] | ((p) & ALTX_MASK)) : NC)
#endif /* NUM_ANALOG_INPUTS > 0 */
//***********digitalPin
//主要涉及函数:
digitalPin
dac_stop(p)
pwm_stop(p) #定义在 ANALOG.H
reset_pin_configured
set_pin_configured
dac_write_value
pwm_start #定义在 ANALOG.H
adc_read_valuE
uart_init--------------------------------------
#ANALOG.H
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ANALOG_H
#define __ANALOG_H/* Includes ------------------------------------------------------------------*/
#include "stm32_def.h"
#include "PeripheralPins.h"
#include "HardwareTimer.h"#ifdef __cplusplus
extern "C" {
#endif/* Exported functions ------------------------------------------------------- */
#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)
uint32_t get_adc_channel(PinName pin, uint32_t *bank);
uint32_t get_adc_internal_channel(PinName pin);
uint16_t adc_read_value(PinName pin, uint32_t resolution);
#endif
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
uint32_t get_dac_channel(PinName pin);
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init);
void dac_stop(PinName pin);
#endif
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution);
void pwm_stop(PinName pin);
#endif
PINMAP.H
bool pin_in_pinmap(PinName pin, const PinMap *map);
void pin_function(PinName pin, int function);
PINFUNCTION
主要函数所在地址 :
STMicroelectronics\hardware\stm32\2.4.0\libraries\SrcWrapper\src\stm32
STMicroelectronics\hardware\stm32\2.4.0\libraries\SrcWrapper\src
/********************************************************************************* Copyright (c) 2016-2021, STMicroelectronics* All rights reserved.** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource/licenses/BSD-3-Clause*********************************************************************************/
#include "analog.h"
#include "lock_resource.h"
#include "stm32yyxx_ll_adc.h"
#include "PinAF_STM32F1.h"#ifdef __cplusplus
extern "C" {
#endif/* Private_Variables */
#if (defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)) ||\(defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY))
static PinName g_current_pin = NC;
#endif/* Private_Defines */
#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)#ifndef ADC_SAMPLINGTIME
#if defined(ADC_SAMPLETIME_8CYCLES_5)
#define ADC_SAMPLINGTIME ADC_SAMPLETIME_8CYCLES_5;
#elif defined(ADC_SAMPLETIME_12CYCLES)
#define ADC_SAMPLINGTIME ADC_SAMPLETIME_12CYCLES;
#elif defined(ADC_SAMPLETIME_12CYCLES_5)
#define ADC_SAMPLINGTIME ADC_SAMPLETIME_12CYCLES_5;
#elif defined(ADC_SAMPLETIME_13CYCLES_5)
#define ADC_SAMPLINGTIME ADC_SAMPLETIME_13CYCLES_5;
#elif defined(ADC_SAMPLETIME_15CYCLES)
#define ADC_SAMPLINGTIME ADC_SAMPLETIME_15CYCLES;
#elif defined(ADC_SAMPLETIME_16CYCLES)
#define ADC_SAMPLINGTIME ADC_SAMPLETIME_16CYCLES;
#elif defined(ADC_SAMPLETIME_19CYCLES_5)
#define ADC_SAMPLINGTIME ADC_SAMPLETIME_19CYCLES_5;
#endif
#endif /* !ADC_SAMPLINGTIME */#if defined(ADC_VER_V5_V90) && !defined(ADC3_SAMPLINGTIME)
#define ADC3_SAMPLINGTIME ADC3_SAMPLETIME_24CYCLES_5;
#endif#if defined(ADC4_SAMPLETIME_19CYCLES_5) && !defined(ADC4_SAMPLINGTIME)
#define ADC4_SAMPLINGTIME ADC4_SAMPLETIME_19CYCLES_5;
#endif/** Minimum ADC sampling time is required when reading* internal channels so set it to max possible value.* It can be defined more precisely by defining:* ADC_SAMPLINGTIME_INTERNAL* to the desired ADC sample time.*/
#ifndef ADC_SAMPLINGTIME_INTERNAL
#if defined(ADC_SAMPLETIME_480CYCLES)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_480CYCLES
#elif defined(ADC_SAMPLETIME_384CYCLES)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_384CYCLES
#elif defined(ADC_SAMPLETIME_810CYCLES_5)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_810CYCLES_5
#elif defined(ADC_SAMPLETIME_814CYCLES)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_814CYCLES
#elif defined(ADC_SAMPLETIME_640CYCLES_5)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_640CYCLES_5
#elif defined(ADC_SAMPLETIME_601CYCLES_5)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_601CYCLES_5
#elif defined(ADC_SAMPLETIME_247CYCLES_5)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_247CYCLES_5
#elif defined(ADC_SAMPLETIME_239CYCLES_5)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_239CYCLES_5
#elif defined(ADC_SAMPLETIME_160CYCLES_5)
#define ADC_SAMPLINGTIME_INTERNAL ADC_SAMPLETIME_160CYCLES_5
#else
#error "ADC sampling time could not be defined for internal channels!"
#endif
#endif /* !ADC_SAMPLINGTIME_INTERNAL */#ifndef ADC_CLOCK_DIV
#ifdef ADC_CLOCK_SYNC_PCLK_DIV4
#define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV4
#elif ADC_CLOCK_SYNC_PCLK_DIV2
#define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV2
#elif defined(ADC_CLOCK_ASYNC_DIV4)
#define ADC_CLOCK_DIV ADC_CLOCK_ASYNC_DIV4
#endif
#endif /* !ADC_CLOCK_DIV */#ifndef ADC_REGULAR_RANK_1
#define ADC_REGULAR_RANK_1 1
#endif/* Exported Functions */
/*** @brief Return ADC HAL channel linked to a PinName* @param pin: PinName* @param bank: pointer to get ADC channel bank if required* @retval Valid HAL channel*/
uint32_t get_adc_channel(PinName pin, uint32_t *bank)
{uint32_t function = pinmap_function(pin, PinMap_ADC);uint32_t channel = 0;switch (STM_PIN_CHANNEL(function)) {
#ifdef ADC_CHANNEL_0case 0:channel = ADC_CHANNEL_0;break;
#endifcase 1:channel = ADC_CHANNEL_1;break;case 2:channel = ADC_CHANNEL_2;break;case 3:channel = ADC_CHANNEL_3;break;case 4:channel = ADC_CHANNEL_4;break;case 5:channel = ADC_CHANNEL_5;break;case 6:channel = ADC_CHANNEL_6;break;case 7:channel = ADC_CHANNEL_7;break;case 8:channel = ADC_CHANNEL_8;break;case 9:channel = ADC_CHANNEL_9;break;case 10:channel = ADC_CHANNEL_10;break;case 11:channel = ADC_CHANNEL_11;break;case 12:channel = ADC_CHANNEL_12;break;case 13:channel = ADC_CHANNEL_13;break;case 14:channel = ADC_CHANNEL_14;break;case 15:channel = ADC_CHANNEL_15;break;
#ifdef ADC_CHANNEL_16case 16:channel = ADC_CHANNEL_16;break;
#endifcase 17:channel = ADC_CHANNEL_17;break;
#ifdef ADC_CHANNEL_18case 18:channel = ADC_CHANNEL_18;break;
#endif
#ifdef ADC_CHANNEL_19case 19:channel = ADC_CHANNEL_19;break;
#endif
#ifdef ADC_CHANNEL_20case 20:channel = ADC_CHANNEL_20;break;case 21:channel = ADC_CHANNEL_21;break;case 22:channel = ADC_CHANNEL_22;break;case 23:channel = ADC_CHANNEL_23;break;
#ifdef ADC_CHANNEL_24case 24:channel = ADC_CHANNEL_24;break;case 25:channel = ADC_CHANNEL_25;break;case 26:channel = ADC_CHANNEL_26;break;
#ifdef ADC_CHANNEL_27case 27:channel = ADC_CHANNEL_27;break;case 28:channel = ADC_CHANNEL_28;break;case 29:channel = ADC_CHANNEL_29;break;case 30:channel = ADC_CHANNEL_30;break;case 31:channel = ADC_CHANNEL_31;break;
#endif
#endif
#endifdefault:_Error_Handler("ADC: Unknown adc channel", (int)(STM_PIN_CHANNEL(function)));break;}
#ifdef ADC_CHANNELS_BANK_Bif (STM_PIN_ANALOG_CHANNEL_BANK_B(function)) {*bank = ADC_CHANNELS_BANK_B;} else {*bank = ADC_CHANNELS_BANK_A;}
#elseUNUSED(bank);
#endifreturn channel;
}/*** @brief Return ADC HAL internal channel linked to a PinName* @param pin: specific PinName's for ADC internal. Value can be:* PADC_TEMP, PADC_TEMP_ADC5, PADC_VREF, PADC_VBAT* Note that not all of these values 鈥嬧€媘ay be available for all series.* @retval Valid HAL internal channel.*/
uint32_t get_adc_internal_channel(PinName pin)
{uint32_t channel = 0;switch (pin) {
#if defined(ADC_CHANNEL_TEMPSENSOR)case PADC_TEMP:channel = ADC_CHANNEL_TEMPSENSOR;break;
#endif
#if defined(ADC_CHANNEL_TEMPSENSOR_ADC1)case PADC_TEMP:channel = ADC_CHANNEL_TEMPSENSOR_ADC1;break;
#endif
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)case PADC_TEMP_ADC5:channel = ADC_CHANNEL_TEMPSENSOR_ADC5;break;
#endif
#ifdef ADC_CHANNEL_VREFINTcase PADC_VREF:channel = ADC_CHANNEL_VREFINT;break;
#endif
#ifdef ADC_CHANNEL_VBATcase PADC_VBAT:channel = ADC_CHANNEL_VBAT;break;
#endifdefault:_Error_Handler("ADC: Unknown adc internal PiName", (int)(pin));break;}return channel;
}
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY */#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
/*** @brief Return DAC HAL channel linked to a PinName* @param pin: specific PinName's for ADC internal.* @retval Valid HAL channel*/
uint32_t get_dac_channel(PinName pin)
{uint32_t function = pinmap_function(pin, PinMap_DAC);uint32_t channel = 0;switch (STM_PIN_CHANNEL(function)) {
#ifdef DAC_CHANNEL_0case 0:channel = DAC_CHANNEL_0;break;
#endifcase 1:channel = DAC_CHANNEL_1;break;
#ifdef DAC_CHANNEL_2case 2:channel = DAC_CHANNEL_2;break;
#endifdefault:_Error_Handler("DAC: Unknown dac channel", (int)(STM_PIN_CHANNEL(function)));break;}return channel;
}// DAC INTERFACE FUNCTIONS //*** @brief DAC MSP Initialization* This function configures the hardware resources used in this example:* - Peripheral's clock enable* - Peripheral's GPIO Configuration* @param hdac: DAC handle pointer* @retval None*/
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
{/* DAC Periph clock enable */if (hdac->Instance == DAC1) {
#ifdef __HAL_RCC_DAC_CLK_ENABLE__HAL_RCC_DAC_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC1_CLK_ENABLE__HAL_RCC_DAC1_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#ifdef DAC2else if (hdac->Instance == DAC2) {
#ifdef __HAL_RCC_DAC2_CLK_ENABLE__HAL_RCC_DAC2_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#endif
#ifdef DAC3else if (hdac->Instance == DAC3) {
#ifdef __HAL_RCC_DAC3_CLK_ENABLE__HAL_RCC_DAC3_CLK_ENABLE();
#endif}
#endif
#ifdef DAC4else if (hdac->Instance == DAC4) {
#ifdef __HAL_RCC_DAC4_CLK_ENABLE__HAL_RCC_DAC4_CLK_ENABLE();
#endif}
#endif/* Configure DAC GPIO pins */pinmap_pinout(g_current_pin, PinMap_DAC);
}/*** @brief This function will set the DAC to the required value* @param port : the gpio port to use* @param pin : the gpio pin to use* @param value : the value to push on the adc output* @param do_init : if set to 1 the initialization of the adc is done* @retval None*/
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init)
{DAC_HandleTypeDef DacHandle = {};DAC_ChannelConfTypeDef dacChannelConf = {};uint32_t dacChannel;DacHandle.Instance = (DAC_TypeDef *)pinmap_peripheral(pin, PinMap_DAC);if (DacHandle.Instance == NP) {return;}dacChannel = get_dac_channel(pin);
#if defined(STM32G4xx)if (!IS_DAC_CHANNEL(DacHandle.Instance, dacChannel)) {
#elseif (!IS_DAC_CHANNEL(dacChannel)) {
#endifreturn;}if (do_init == 1) {/*##-1- Configure the DAC peripheral #######################################*/g_current_pin = pin;if (HAL_DAC_Init(&DacHandle) != HAL_OK) {/* Initialization Error */return;}dacChannelConf.DAC_Trigger = DAC_TRIGGER_NONE;dacChannelConf.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
#if defined(DAC_OUTPUTSWITCH_ENABLE)dacChannelConf.DAC_OutputSwitch = DAC_OUTPUTSWITCH_ENABLE;
#endif/*##-2- Configure DAC channel1 #############################################*/if (HAL_DAC_ConfigChannel(&DacHandle, &dacChannelConf, dacChannel) != HAL_OK) {/* Channel configuration Error */return;}}/*##-3- Set DAC Channel1 DHR register ######################################*/if (HAL_DAC_SetValue(&DacHandle, dacChannel, DAC_ALIGN_12B_R, value) != HAL_OK) {/* Setting value Error */return;}/*##-4- Enable DAC Channel1 ################################################*/HAL_DAC_Start(&DacHandle, dacChannel);
}/*** @brief DeInitialize the DAC MSP.* @param hdac: pointer to a DAC_HandleTypeDef structure that contains* the configuration information for the specified DAC.* @retval None*/
void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
{/* DAC Periph clock disable */if (hdac->Instance == DAC1) {
#ifdef __HAL_RCC_DAC_FORCE_RESET__HAL_RCC_DAC_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC1_FORCE_RESET__HAL_RCC_DAC1_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_FORCE_RESET__HAL_RCC_DAC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC_RELEASE_RESET__HAL_RCC_DAC_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC1_RELEASE_RESET__HAL_RCC_DAC1_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_RELEASE_RESET__HAL_RCC_DAC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC_CLK_DISABLE__HAL_RCC_DAC_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_DAC1_CLK_DISABLE__HAL_RCC_DAC1_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#ifdef DAC2else if (hdac->Instance == DAC2) {
#ifdef __HAL_RCC_DAC2_FORCE_RESET__HAL_RCC_DAC2_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_FORCE_RESET__HAL_RCC_DAC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC2_RELEASE_RESET__HAL_RCC_DAC2_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC12_RELEASE_RESET__HAL_RCC_DAC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC2_CLK_ENABLE__HAL_RCC_DAC2_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_DAC12_CLK_ENABLE__HAL_RCC_DAC12_CLK_ENABLE();
#endif}
#endif
#ifdef DAC3else if (hdac->Instance == DAC3) {
#ifdef __HAL_RCC_DAC3_FORCE_RESET__HAL_RCC_DAC3_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC3_RELEASE_RESET__HAL_RCC_DAC3_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC3_CLK_DISABLE__HAL_RCC_DAC3_CLK_DISABLE();
#endif}
#endif
#ifdef DAC4else if (hdac->Instance == DAC4) {
#ifdef __HAL_RCC_DAC4_FORCE_RESET__HAL_RCC_DAC4_FORCE_RESET();
#endif
#ifdef __HAL_RCC_DAC4_RELEASE_RESET__HAL_RCC_DAC4_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_DAC4_CLK_DISABLE__HAL_RCC_DAC4_CLK_DISABLE();
#endif}
#endif
}/*** @brief This function will stop the DAC* @param port : the gpio port to use* @param pin : the gpio pin to use* @retval None*/
void dac_stop(PinName pin)
{DAC_HandleTypeDef DacHandle;uint32_t dacChannel;DacHandle.Instance = (DAC_TypeDef *)pinmap_peripheral(pin, PinMap_DAC);if (DacHandle.Instance == NP) {return;}dacChannel = get_dac_channel(pin);
#if defined(STM32G4xx)if (!IS_DAC_CHANNEL(DacHandle.Instance, dacChannel)) {
#elseif (!IS_DAC_CHANNEL(dacChannel)) {
#endifreturn;}HAL_DAC_Stop(&DacHandle, dacChannel);if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) {/* DeInitialization Error */return;}
}
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY#if defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)
// ADC INTERFACE FUNCTIONS //*** @brief ADC MSP Initialization* This function configures the hardware resources used in this example:* - Peripheral's clock enable* - Peripheral's GPIO Configuration* @param hadc: ADC handle pointer* @retval None*/
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
{/*##-1- Enable peripherals and GPIO Clocks #################################*//* ADC Periph clock enable */if (hadc->Instance == ADC1) {
#ifdef __HAL_RCC_ADC1_CLK_ENABLE__HAL_RCC_ADC1_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC12_CLK_ENABLE__HAL_RCC_ADC12_CLK_ENABLE();
#endif}
#ifdef ADC2else if (hadc->Instance == ADC2) {
#ifdef __HAL_RCC_ADC2_CLK_ENABLE__HAL_RCC_ADC2_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC12_CLK_ENABLE__HAL_RCC_ADC12_CLK_ENABLE();
#endif}
#endif
#ifdef ADC3else if (hadc->Instance == ADC3) {
#ifdef __HAL_RCC_ADC3_CLK_ENABLE__HAL_RCC_ADC3_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC34_CLK_ENABLE__HAL_RCC_ADC34_CLK_ENABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_CLK_ENABLE();
#endif}
#endif
#ifdef ADC4else if (hadc->Instance == ADC4) {
#ifdef __HAL_RCC_ADC4_CLK_ENABLE__HAL_RCC_ADC4_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC34_CLK_ENABLE__HAL_RCC_ADC34_CLK_ENABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_CLK_ENABLE();
#endif}
#endif
#ifdef ADC5else if (hadc->Instance == ADC5) {
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_CLK_ENABLE();
#endif}
#endif
#ifdef __HAL_RCC_ADC_CLK_ENABLE__HAL_RCC_ADC_CLK_ENABLE();
#endif/* For STM32F1xx, STM32H7xx, and STM32MP1xx ADC prescaler is configured inSystemClock_Config (variant.cpp) */
#if defined(__HAL_RCC_ADC_CONFIG) && !defined(STM32F1xx) && \!defined(STM32H7xx) && !defined(STM32MP1xx)hsem_lock(CFG_HW_RCC_CRRCR_CCIPR_SEMID, HSEM_LOCK_DEFAULT_RETRY);/* ADC Periph interface clock configuration */__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);hsem_unlock(CFG_HW_RCC_CRRCR_CCIPR_SEMID);
#endif/* Configure ADC GPIO pin */if (!(g_current_pin & PADC_BASE)) {pinmap_pinout(g_current_pin, PinMap_ADC);}
}/*** @brief DeInitializes the ADC MSP.* @param hadc: ADC handle* @retval None*/
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
{
#ifdef __HAL_RCC_ADC_FORCE_RESET__HAL_RCC_ADC_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC_RELEASE_RESET__HAL_RCC_ADC_RELEASE_RESET();
#endifif (hadc->Instance == ADC1) {
#ifdef __HAL_RCC_ADC1_FORCE_RESET__HAL_RCC_ADC1_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC1_RELEASE_RESET__HAL_RCC_ADC1_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_FORCE_RESET__HAL_RCC_ADC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_RELEASE_RESET__HAL_RCC_ADC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC1_CLK_DISABLE__HAL_RCC_ADC1_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC12_CLK_DISABLE__HAL_RCC_ADC12_CLK_DISABLE();
#endif}
#ifdef ADC2else if (hadc->Instance == ADC2) {
#ifdef __HAL_RCC_ADC2_FORCE_RESET__HAL_RCC_ADC2_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC2_RELEASE_RESET__HAL_RCC_ADC2_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_FORCE_RESET__HAL_RCC_ADC12_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC12_RELEASE_RESET__HAL_RCC_ADC12_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC2_CLK_DISABLE__HAL_RCC_ADC2_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC2_CLK_DISABLE__HAL_RCC_ADC2_CLK_DISABLE();
#endif}
#endif
#ifdef ADC3else if (hadc->Instance == ADC3) {
#ifdef __HAL_RCC_ADC3_FORCE_RESET__HAL_RCC_ADC3_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC3_RELEASE_RESET__HAL_RCC_ADC3_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_FORCE_RESET__HAL_RCC_ADC34_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_RELEASE_RESET__HAL_RCC_ADC34_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC3_CLK_DISABLE__HAL_RCC_ADC3_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC34_CLK_DISABLE__HAL_RCC_ADC34_CLK_DISABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_FORCE_RESET();__HAL_RCC_ADC345_RELEASE_RESET();__HAL_RCC_ADC345_CLK_DISABLE();
#endif}
#endif
#ifdef ADC4else if (hadc->Instance == ADC4) {
#ifdef __HAL_RCC_ADC4_FORCE_RESET__HAL_RCC_ADC4_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC4_RELEASE_RESET__HAL_RCC_ADC4_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC4_CLK_DISABLE__HAL_RCC_ADC4_CLK_DISABLE();
#endif
#ifdef __HAL_RCC_ADC34_FORCE_RESET__HAL_RCC_ADC34_FORCE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_RELEASE_RESET__HAL_RCC_ADC34_RELEASE_RESET();
#endif
#ifdef __HAL_RCC_ADC34_CLK_DISABLE__HAL_RCC_ADC34_CLK_DISABLE();
#endif
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_FORCE_RESET();__HAL_RCC_ADC345_RELEASE_RESET();__HAL_RCC_ADC345_CLK_DISABLE();
#endif}
#endif
#ifdef ADC5else if (hadc->Instance == ADC5) {
#if defined(ADC345_COMMON)__HAL_RCC_ADC345_FORCE_RESET();__HAL_RCC_ADC345_RELEASE_RESET();__HAL_RCC_ADC345_CLK_DISABLE();
#endif}
#endif
#ifdef __HAL_RCC_ADC_CLK_DISABLE__HAL_RCC_ADC_FORCE_RESET();__HAL_RCC_ADC_RELEASE_RESET();__HAL_RCC_ADC_CLK_DISABLE();
#endif
}/*** @brief This function will set the ADC to the required value* @param pin : the pin to use* @param resolution : resolution for converted data: 6/8/10/12/14/16* @retval the value of the adc*/
uint16_t adc_read_value(PinName pin, uint32_t resolution)
{ADC_HandleTypeDef AdcHandle = {};ADC_ChannelConfTypeDef AdcChannelConf = {};__IO uint16_t uhADCxConvertedValue = 0;uint32_t samplingTime = ADC_SAMPLINGTIME;uint32_t channel = 0;uint32_t bank = 0;if ((pin & PADC_BASE) && (pin < ANA_START)) {
#if defined(STM32H7xx) || defined(STM32MP1xx)
#ifdef ADC3AdcHandle.Instance = ADC3;
#elseAdcHandle.Instance = ADC2;
#endif
#elseAdcHandle.Instance = ADC1;
#if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5)if (pin == PADC_TEMP_ADC5) {AdcHandle.Instance = ADC5;}
#endif
#endifchannel = get_adc_internal_channel(pin);samplingTime = ADC_SAMPLINGTIME_INTERNAL;} else {AdcHandle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);channel = get_adc_channel(pin, &bank);
#if defined(ADC_VER_V5_V90)if (AdcHandle.Instance == ADC3) {samplingTime = ADC3_SAMPLINGTIME;}
#endif
#if defined(ADC4_SAMPLINGTIME)if (AdcHandle.Instance == ADC4) {samplingTime = ADC4_SAMPLINGTIME;}
#endif}if (AdcHandle.Instance == NP) {return 0;}#ifdef ADC_CLOCK_DIVAdcHandle.Init.ClockPrescaler = ADC_CLOCK_DIV; /* (A)synchronous clock mode, input ADC clock divided */
#endif
#ifdef ADC_RESOLUTION_12Bswitch (resolution) {
#ifdef ADC_RESOLUTION_6Bcase 6:AdcHandle.Init.Resolution = ADC_RESOLUTION_6B; /* resolution for converted data */break;
#endifcase 8:AdcHandle.Init.Resolution = ADC_RESOLUTION_8B; /* resolution for converted data */break;case 10:AdcHandle.Init.Resolution = ADC_RESOLUTION_10B; /* resolution for converted data */break;case 12:default:AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* resolution for converted data */break;
#ifdef ADC_RESOLUTION_14Bcase 14:AdcHandle.Init.Resolution = ADC_RESOLUTION_14B; /* resolution for converted data */break;
#endif
#ifdef ADC_RESOLUTION_16Bcase 16:AdcHandle.Init.Resolution = ADC_RESOLUTION_16B; /* resolution for converted data */break;
#endif}
#elseUNUSED(resolution);
#endif
#ifdef ADC_DATAALIGN_RIGHTAdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Right-alignment for converted data */
#endif
#ifdef ADC_SCAN_SEQ_FIXEDAdcHandle.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
#elseAdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
#endif
#ifdef ADC_EOC_SINGLE_CONVAdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicate conversion end */
#endif
#if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32F4xx) && \!defined(STM32F7xx) && !defined(ADC1_V2_5)AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled */
#endif
#if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32F3xx) && \!defined(STM32F4xx) && !defined(STM32F7xx) && !defined(STM32G4xx) && \!defined(STM32H7xx) && !defined(STM32L4xx) && !defined(STM32L5xx) && \!defined(STM32MP1xx) && !defined(STM32WBxx)AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; /* ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered */
#endif
#ifdef ADC_CHANNELS_BANK_BAdcHandle.Init.ChannelsBank = bank;
#elif defined(ADC_CHANNELS_BANK_A)AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
#endifAdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */
#if !defined(STM32F0xx) && !defined(STM32L0xx)AdcHandle.Init.NbrOfConversion = 1; /* Specifies the number of ranks that will be converted within the regular group sequencer. */
#endifAdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */
#if !defined(STM32F0xx) && !defined(STM32G0xx) && !defined(STM32L0xx) && \!defined(STM32WLxx)AdcHandle.Init.NbrOfDiscConversion = 0; /* Parameter discarded because sequencer is disabled */
#endifAdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external event */
#if !defined(STM32F1xx) && !defined(ADC1_V2_5)AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
#endif
#if !defined(STM32F1xx) && !defined(STM32H7xx) && !defined(STM32MP1xx) && \!defined(ADC1_V2_5)AdcHandle.Init.DMAContinuousRequests = DISABLE; /* DMA one-shot mode selected (not applied to this example) */
#endif
#ifdef ADC_CONVERSIONDATA_DRAdcHandle.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; /* Regular Conversion data stored in DR register only */
#endif
#ifdef ADC_OVR_DATA_OVERWRITTENAdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is overwritten with the last conversion result in case of overrun */
#endif
#ifdef ADC_LEFTBITSHIFT_NONEAdcHandle.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; /* No bit shift left applied on the final ADC conversion data */
#endif#if defined(STM32F0xx)AdcHandle.Init.SamplingTimeCommon = samplingTime;
#endif
#if defined(STM32G0xx) || defined(STM32U5xx) || defined(STM32WLxx)AdcHandle.Init.SamplingTimeCommon1 = samplingTime; /* Set sampling time common to a group of channels. */AdcHandle.Init.SamplingTimeCommon2 = samplingTime; /* Set sampling time common to a group of channels, second common setting possible.*/
#endif
#if defined(STM32L0xx)AdcHandle.Init.LowPowerFrequencyMode = DISABLE; /* To be enabled only if ADC clock < 2.8 MHz */AdcHandle.Init.SamplingTime = samplingTime;
#endif
#if !defined(STM32F0xx) && !defined(STM32F1xx) && !defined(STM32F2xx) && \!defined(STM32F3xx) && !defined(STM32F4xx) && !defined(STM32F7xx) && \!defined(STM32L1xx)AdcHandle.Init.OversamplingMode = DISABLE;/* AdcHandle.Init.Oversample ignore for STM32L0xx as oversampling disabled *//* AdcHandle.Init.Oversampling ignored for other as oversampling disabled */
#endif
#if defined(ADC_CFGR_DFSDMCFG) && defined(DFSDM1_Channel0)AdcHandle.Init.DFSDMConfig = ADC_DFSDM_MODE_DISABLE; /* ADC conversions are not transferred by DFSDM. */
#endif
#ifdef ADC_TRIGGER_FREQ_HIGHAdcHandle.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
#endif
#ifdef ADC_VREF_PPROT_NONEAdcHandle.Init.VrefProtection = ADC_VREF_PPROT_NONE;
#endifAdcHandle.State = HAL_ADC_STATE_RESET;AdcHandle.DMA_Handle = NULL;AdcHandle.Lock = HAL_UNLOCKED;/* Some other ADC_HandleTypeDef fields exists but not required */g_current_pin = pin; /* Needed for HAL_ADC_MspInit*/if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {return 0;}AdcChannelConf.Channel = channel; /* Specifies the channel to configure into ADC */#if defined(STM32G4xx) || defined(STM32L4xx) || defined(STM32L5xx) || \defined(STM32WBxx)if (!IS_ADC_CHANNEL(&AdcHandle, AdcChannelConf.Channel)) {
#elseif (!IS_ADC_CHANNEL(AdcChannelConf.Channel)) {
#endifreturn 0;}
#if defined(ADC_SCAN_SEQ_FIXED) && defined(ADC_RANK_CHANNEL_NUMBER)AdcChannelConf.Rank = ADC_RANK_CHANNEL_NUMBER; /* Enable the rank of the selected channels when not fully configurable */
#elseAdcChannelConf.Rank = ADC_REGULAR_RANK_1; /* Specifies the rank in the regular group sequencer */
#endif
#if !defined(STM32L0xx)
#if !defined(STM32G0xx)AdcChannelConf.SamplingTime = samplingTime; /* Sampling time value to be set for the selected channel */
#elseAdcChannelConf.SamplingTime = ADC_SAMPLINGTIME_COMMON_1; /* Sampling time value to be set for the selected channel */
#endif
#endif
#if defined(ADC_DIFFERENTIAL_ENDED) && !defined(ADC1_V2_5)AdcChannelConf.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */AdcChannelConf.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
#endif
#if !defined(STM32F0xx) && !defined(STM32F1xx) && !defined(STM32F2xx) && \!defined(STM32G0xx) && !defined(STM32L0xx) && !defined(STM32L1xx) && \!defined(STM32WBxx) && !defined(STM32WLxx) && \!defined(ADC1_V2_5)AdcChannelConf.Offset = 0; /* Parameter discarded because offset correction is disabled */
#endif
#if defined (STM32H7xx) || defined(STM32MP1xx)AdcChannelConf.OffsetRightShift = DISABLE; /* No Right Offset Shift */AdcChannelConf.OffsetSignedSaturation = DISABLE; /* Signed saturation feature is not used */
#endif/*##-2- Configure ADC regular channel ######################################*/if (HAL_ADC_ConfigChannel(&AdcHandle, &AdcChannelConf) != HAL_OK) {/* Channel Configuration Error */return 0;}#if defined(ADC_CR_ADCAL) || defined(ADC_CR2_RSTCAL)/*##-2.1- Calibrate ADC then Start the conversion process ####################*/
#if defined(ADC_CALIB_OFFSET)if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
#elif defined(ADC_SINGLE_ENDED) && !defined(ADC1_V2_5)if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
#elseif (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)
#endif{/* ADC Calibration Error */return 0;}
#endif/*##-3- Start the conversion process ####################*/if (HAL_ADC_Start(&AdcHandle) != HAL_OK) {/* Start Conversion Error */return 0;}/*##-4- Wait for the end of conversion #####################################*//* For simplicity reasons, this example is just waiting till the end of theconversion, but application may perform other tasks while conversionoperation is ongoing. */if (HAL_ADC_PollForConversion(&AdcHandle, 10) != HAL_OK) {/* End Of Conversion flag not set on time */return 0;}/* Check if the continuous conversion of regular channel is finished */if ((HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC) {/*##-5- Get the converted value of regular channel ########################*/uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);}if (HAL_ADC_Stop(&AdcHandle) != HAL_OK) {/* Stop Conversation Error */return 0;}if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) {return 0;}if (__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance) != 0U) {LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(AdcHandle.Instance), LL_ADC_PATH_INTERNAL_NONE);}return uhADCxConvertedValue;
}
#endif /* HAL_ADC_MODULE_ENABLED && !HAL_ADC_MODULE_ONLY*/#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
// PWM INTERFACE FUNCTIONS //*** @brief This function will set the PWM to the required value* @param port : the gpio port to use* @param pin : the gpio pin to use* @param clock_freq : frequency of the tim clock* @param value : the value to push on the PWM output* @retval None*/
void pwm_start(PinName pin, uint32_t PWM_freq, uint32_t value, TimerCompareFormat_t resolution)
{TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM);HardwareTimer *HT;TimerModes_t previousMode;uint32_t index = get_timer_index(Instance);if (HardwareTimer_Handle[index] == NULL) {HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM));}HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin, PinMap_TIM));previousMode = HT->getMode(channel);if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);}HT->setOverflow(PWM_freq, HERTZ_FORMAT);HT->setCaptureCompare(channel, value, resolution);if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) {HT->resume();}
}
/*** @brief This function will disable the PWM* @param port : the gpio port to use* @param pin : the gpio pin to use* @retval None*/
void pwm_stop(PinName pin)
{TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM);HardwareTimer *HT;uint32_t index = get_timer_index(Instance);if (HardwareTimer_Handle[index] == NULL) {HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin, PinMap_TIM));}HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);if (HT != NULL) {delete (HT);HT = NULL;}
}
#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */#ifdef __cplusplus
}
#endif/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
TOOLS
MDK
WINDOWS SEARTH
arduino中:
analogRead 与analogWrite是不同的,使用adc输入测量,但输出是用pwm 而不是dac
本文标签: stm32duino 文件结构分析
版权声明:本文标题:stm32duino 文件结构分析 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732355653h1534359.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论