ch32v30x_adc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_adc.h
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file contains all the functions prototypes for the
  7. * ADC firmware library.
  8. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #ifndef __CH32V30x_ADC_H
  14. #define __CH32V30x_ADC_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "ch32v30x.h"
  19. /* ADC Init structure definition */
  20. typedef struct
  21. {
  22. uint32_t ADC_Mode; /* Configures the ADC to operate in independent or
  23. dual mode.
  24. This parameter can be a value of @ref ADC_mode */
  25. FunctionalState ADC_ScanConvMode; /* Specifies whether the conversion is performed in
  26. Scan (multichannels) or Single (one channel) mode.
  27. This parameter can be set to ENABLE or DISABLE */
  28. FunctionalState ADC_ContinuousConvMode; /* Specifies whether the conversion is performed in
  29. Continuous or Single mode.
  30. This parameter can be set to ENABLE or DISABLE. */
  31. uint32_t ADC_ExternalTrigConv; /* Defines the external trigger used to start the analog
  32. to digital conversion of regular channels. This parameter
  33. can be a value of @ref ADC_external_trigger_sources_for_regular_channels_conversion */
  34. uint32_t ADC_DataAlign; /* Specifies whether the ADC data alignment is left or right.
  35. This parameter can be a value of @ref ADC_data_align */
  36. uint8_t ADC_NbrOfChannel; /* Specifies the number of ADC channels that will be converted
  37. using the sequencer for regular channel group.
  38. This parameter must range from 1 to 16. */
  39. uint32_t ADC_OutputBuffer; /* Specifies whether the ADC channel output buffer is enabled or disabled.
  40. This parameter can be a value of @ref ADC_OutputBuffer */
  41. uint32_t ADC_Pga; /* Specifies the PGA gain multiple.
  42. This parameter can be a value of @ref ADC_Pga */
  43. }ADC_InitTypeDef;
  44. /* ADC_mode */
  45. #define ADC_Mode_Independent ((uint32_t)0x00000000)
  46. #define ADC_Mode_RegInjecSimult ((uint32_t)0x00010000)
  47. #define ADC_Mode_RegSimult_AlterTrig ((uint32_t)0x00020000)
  48. #define ADC_Mode_InjecSimult_FastInterl ((uint32_t)0x00030000)
  49. #define ADC_Mode_InjecSimult_SlowInterl ((uint32_t)0x00040000)
  50. #define ADC_Mode_InjecSimult ((uint32_t)0x00050000)
  51. #define ADC_Mode_RegSimult ((uint32_t)0x00060000)
  52. #define ADC_Mode_FastInterl ((uint32_t)0x00070000)
  53. #define ADC_Mode_SlowInterl ((uint32_t)0x00080000)
  54. #define ADC_Mode_AlterTrig ((uint32_t)0x00090000)
  55. /* ADC_external_trigger_sources_for_regular_channels_conversion */
  56. #define ADC_ExternalTrigConv_T1_CC1 ((uint32_t)0x00000000)
  57. #define ADC_ExternalTrigConv_T1_CC2 ((uint32_t)0x00020000)
  58. #define ADC_ExternalTrigConv_T2_CC2 ((uint32_t)0x00060000)
  59. #define ADC_ExternalTrigConv_T3_TRGO ((uint32_t)0x00080000)
  60. #define ADC_ExternalTrigConv_T4_CC4 ((uint32_t)0x000A0000)
  61. #define ADC_ExternalTrigConv_Ext_IT11_TIM8_TRGO ((uint32_t)0x000C0000)
  62. #define ADC_ExternalTrigConv_T1_CC3 ((uint32_t)0x00040000)
  63. #define ADC_ExternalTrigConv_None ((uint32_t)0x000E0000)
  64. #define ADC_ExternalTrigConv_T3_CC1 ((uint32_t)0x00000000)
  65. #define ADC_ExternalTrigConv_T2_CC3 ((uint32_t)0x00020000)
  66. #define ADC_ExternalTrigConv_T8_CC1 ((uint32_t)0x00060000)
  67. #define ADC_ExternalTrigConv_T8_TRGO ((uint32_t)0x00080000)
  68. #define ADC_ExternalTrigConv_T5_CC1 ((uint32_t)0x000A0000)
  69. #define ADC_ExternalTrigConv_T5_CC3 ((uint32_t)0x000C0000)
  70. /* ADC_data_align */
  71. #define ADC_DataAlign_Right ((uint32_t)0x00000000)
  72. #define ADC_DataAlign_Left ((uint32_t)0x00000800)
  73. /* ADC_channels */
  74. #define ADC_Channel_0 ((uint8_t)0x00)
  75. #define ADC_Channel_1 ((uint8_t)0x01)
  76. #define ADC_Channel_2 ((uint8_t)0x02)
  77. #define ADC_Channel_3 ((uint8_t)0x03)
  78. #define ADC_Channel_4 ((uint8_t)0x04)
  79. #define ADC_Channel_5 ((uint8_t)0x05)
  80. #define ADC_Channel_6 ((uint8_t)0x06)
  81. #define ADC_Channel_7 ((uint8_t)0x07)
  82. #define ADC_Channel_8 ((uint8_t)0x08)
  83. #define ADC_Channel_9 ((uint8_t)0x09)
  84. #define ADC_Channel_10 ((uint8_t)0x0A)
  85. #define ADC_Channel_11 ((uint8_t)0x0B)
  86. #define ADC_Channel_12 ((uint8_t)0x0C)
  87. #define ADC_Channel_13 ((uint8_t)0x0D)
  88. #define ADC_Channel_14 ((uint8_t)0x0E)
  89. #define ADC_Channel_15 ((uint8_t)0x0F)
  90. #define ADC_Channel_16 ((uint8_t)0x10)
  91. #define ADC_Channel_17 ((uint8_t)0x11)
  92. #define ADC_Channel_TempSensor ((uint8_t)ADC_Channel_16)
  93. #define ADC_Channel_Vrefint ((uint8_t)ADC_Channel_17)
  94. /*ADC_output_buffer*/
  95. #define ADC_OutputBuffer_Enable ((uint32_t)0x04000000)
  96. #define ADC_OutputBuffer_Disable ((uint32_t)0x00000000)
  97. /*ADC_pga*/
  98. #define ADC_Pga_1 ((uint32_t)0x00000000)
  99. #define ADC_Pga_4 ((uint32_t)0x08000000)
  100. #define ADC_Pga_16 ((uint32_t)0x10000000)
  101. #define ADC_Pga_64 ((uint32_t)0x18000000)
  102. /* ADC_sampling_time */
  103. #define ADC_SampleTime_1Cycles5 ((uint8_t)0x00)
  104. #define ADC_SampleTime_7Cycles5 ((uint8_t)0x01)
  105. #define ADC_SampleTime_13Cycles5 ((uint8_t)0x02)
  106. #define ADC_SampleTime_28Cycles5 ((uint8_t)0x03)
  107. #define ADC_SampleTime_41Cycles5 ((uint8_t)0x04)
  108. #define ADC_SampleTime_55Cycles5 ((uint8_t)0x05)
  109. #define ADC_SampleTime_71Cycles5 ((uint8_t)0x06)
  110. #define ADC_SampleTime_239Cycles5 ((uint8_t)0x07)
  111. /* ADC_external_trigger_sources_for_injected_channels_conversion */
  112. #define ADC_ExternalTrigInjecConv_T2_TRGO ((uint32_t)0x00002000)
  113. #define ADC_ExternalTrigInjecConv_T2_CC1 ((uint32_t)0x00003000)
  114. #define ADC_ExternalTrigInjecConv_T3_CC4 ((uint32_t)0x00004000)
  115. #define ADC_ExternalTrigInjecConv_T4_TRGO ((uint32_t)0x00005000)
  116. #define ADC_ExternalTrigInjecConv_Ext_IT15_TIM8_CC4 ((uint32_t)0x00006000)
  117. #define ADC_ExternalTrigInjecConv_T1_TRGO ((uint32_t)0x00000000)
  118. #define ADC_ExternalTrigInjecConv_T1_CC4 ((uint32_t)0x00001000)
  119. #define ADC_ExternalTrigInjecConv_None ((uint32_t)0x00007000)
  120. #define ADC_ExternalTrigInjecConv_T4_CC3 ((uint32_t)0x00002000)
  121. #define ADC_ExternalTrigInjecConv_T8_CC2 ((uint32_t)0x00003000)
  122. #define ADC_ExternalTrigInjecConv_T8_CC4 ((uint32_t)0x00004000)
  123. #define ADC_ExternalTrigInjecConv_T5_TRGO ((uint32_t)0x00005000)
  124. #define ADC_ExternalTrigInjecConv_T5_CC4 ((uint32_t)0x00006000)
  125. /* ADC_injected_channel_selection */
  126. #define ADC_InjectedChannel_1 ((uint8_t)0x14)
  127. #define ADC_InjectedChannel_2 ((uint8_t)0x18)
  128. #define ADC_InjectedChannel_3 ((uint8_t)0x1C)
  129. #define ADC_InjectedChannel_4 ((uint8_t)0x20)
  130. /* ADC_analog_watchdog_selection */
  131. #define ADC_AnalogWatchdog_SingleRegEnable ((uint32_t)0x00800200)
  132. #define ADC_AnalogWatchdog_SingleInjecEnable ((uint32_t)0x00400200)
  133. #define ADC_AnalogWatchdog_SingleRegOrInjecEnable ((uint32_t)0x00C00200)
  134. #define ADC_AnalogWatchdog_AllRegEnable ((uint32_t)0x00800000)
  135. #define ADC_AnalogWatchdog_AllInjecEnable ((uint32_t)0x00400000)
  136. #define ADC_AnalogWatchdog_AllRegAllInjecEnable ((uint32_t)0x00C00000)
  137. #define ADC_AnalogWatchdog_None ((uint32_t)0x00000000)
  138. /* ADC_interrupts_definition */
  139. #define ADC_IT_EOC ((uint16_t)0x0220)
  140. #define ADC_IT_AWD ((uint16_t)0x0140)
  141. #define ADC_IT_JEOC ((uint16_t)0x0480)
  142. /* ADC_flags_definition */
  143. #define ADC_FLAG_AWD ((uint8_t)0x01)
  144. #define ADC_FLAG_EOC ((uint8_t)0x02)
  145. #define ADC_FLAG_JEOC ((uint8_t)0x04)
  146. #define ADC_FLAG_JSTRT ((uint8_t)0x08)
  147. #define ADC_FLAG_STRT ((uint8_t)0x10)
  148. void ADC_DeInit(ADC_TypeDef* ADCx);
  149. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct);
  150. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct);
  151. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  152. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  153. void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState);
  154. void ADC_ResetCalibration(ADC_TypeDef* ADCx);
  155. FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx);
  156. void ADC_StartCalibration(ADC_TypeDef* ADCx);
  157. FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx);
  158. void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  159. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx);
  160. void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number);
  161. void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  162. void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  163. void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  164. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
  165. uint32_t ADC_GetDualModeConversionValue(void);
  166. void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  167. void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  168. void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv);
  169. void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  170. void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  171. FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx);
  172. void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  173. void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length);
  174. void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset);
  175. uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel);
  176. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog);
  177. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t LowThreshold);
  178. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel);
  179. void ADC_TempSensorVrefintCmd(FunctionalState NewState);
  180. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  181. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  182. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  183. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  184. s32 TempSensor_Volt_To_Temper(s32 Value);
  185. void ADC_BufferCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  186. int16_t Get_CalibrationValue(ADC_TypeDef* ADCx);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif