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