A31G12x F/W Packages  2.5.0
ABOV Cortex-M0+ Core based MCUs Integrated Driver
A31G12x_hal_adc.c
Go to the documentation of this file.
1 /***************************************************************************//****************************************************************************/
34 
35 /* Includes ----------------------------------------------------------------- */
36 //******************************************************************************
37 // Include
38 //******************************************************************************
39 
40 #include "A31G12x_hal_adc.h"
41 #include "A31G12x_hal_scu.h"
42 
43 /* Public Functions --------------------------------------------------------- */
44 //******************************************************************************
45 // Function
46 //******************************************************************************
47 
48 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
58 HAL_Status_Type HAL_ADC_Init( ADC_Type* ADCx, ADC_CFG_Type* ADC_Config )
59 {
60  uint32_t tempreg;
61 
62  /* Check ADC handle */
63  if( ADCx == NULL )
64  {
65  return HAL_ERROR;
66  }
67 
68  HAL_SCU_Peripheral_EnableClock2( PPCLKEN2_ADCLKE, PPxCLKE_Enable );
69 
70  ADCx->CR = ( 1 << ADC_CR_ADCEN_Pos ); // ADCEN;
71 
72  tempreg = 0
73  | ( 1 << ADC_CR_ADCEN_Pos ) // ADCEN
74  | ( ( ( ADC_Config->TrgSel ) & 7 ) << ADC_CR_TRIG_Pos ) // TRGSRC
75  | ( ( ( ADC_Config->RefSel ) & 1 ) << ADC_CR_REFSEL_Pos )
76  | ( 1 << ADC_CR_ADCIFLAG_Pos ) // clear flag
77  ;
78  ADCx->CR = tempreg;
79 
80  ADCx->PREDR = ( ADC_Config->InClkDiv & ADC_PREDR_PRED_Msk );
81 
82  return HAL_OK;
83 }
84 
85 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
93 {
94  /* Check ADC handle */
95  if( ADCx == NULL )
96  {
97  return HAL_ERROR;
98  }
99 
100  ADCx->CR = 0;
101  HAL_SCU_Peripheral_EnableClock2( PPCLKEN2_ADCLKE, PPxCLKE_Disable );
102 
103  return HAL_OK;
104 }
105 
106 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
117 {
118  uint32_t tempreg;
119 
120  /* Check ADC handle */
121  if( ADCx == NULL )
122  {
123  return HAL_ERROR;
124  }
125 
126  tempreg = ADCx->CR;
127  tempreg &= ~( 1 << ADC_CR_ADCIEN_Pos );
128 
129  if( NewState )
130  {
131  tempreg |= ( 1 << ADC_CR_ADCIEN_Pos );
132  }
133  ADCx->CR = tempreg;
134 
135  return HAL_OK;
136 }
137 
138 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
148 HAL_Status_Type HAL_ADC_ChannelSel( ADC_Type* ADCx, uint32_t Channel )
149 {
150  uint32_t temp_reg;
151 
152  /* Check ADC handle */
153  if( ADCx == NULL )
154  {
155  return HAL_ERROR;
156  }
157 
158  temp_reg = ADCx->CR & ( uint32_t )( ~ADC_CR_ADSEL_Msk );
159  temp_reg |= ( uint32_t )( Channel & ADC_CR_ADSEL_Msk );
160  ADCx->CR = temp_reg;
161 
162  return HAL_OK;
163 }
164 
165 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
173 {
174  /* Check ADC handle */
175  if( ADCx == NULL )
176  {
177  return HAL_ERROR;
178  }
179 
180  ADCx->CR |= ( 1 << ADC_CR_ADST_Pos );
181 
182  return HAL_OK;
183 }
184 
185 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
194 HAL_Status_Type HAL_ADC_Stop( ADC_Type* ADCx )
195 {
196  /* Check ADC handle */
197  if( ADCx == NULL )
198  {
199  return HAL_ERROR;
200  }
201 
202  ADCx->CR &= ~( 1 << ADC_CR_ADST_Pos );
203 
204  return HAL_OK;
205 }
206 
207 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
215 {
216  uint32_t tempreg;
217 
218  /* Check ADC handle */
219  if( ADCx == NULL )
220  {
221  return HAL_ERROR;
222  }
223 
224  tempreg = ADCx->CR;
225  tempreg |= ( 1 << ADC_CR_ADCIFLAG_Pos );
226 
227  ADCx->CR = tempreg; // clear flag;
228 
229  return HAL_OK;
230 }
231 
232 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
239 uint32_t HAL_ADC_GetStatus( ADC_Type* ADCx )
240 {
241  uint32_t tempreg;
242 
243  tempreg = ADCx->CR;
244  tempreg &= ( 1 << ADC_CR_ADCIFLAG_Pos );
245 
246  return tempreg;
247 }
248 
249 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
256 uint16_t HAL_ADC_GetData( ADC_Type* ADCx )
257 {
258  uint16_t adc_value;
259 
260  adc_value = ADCx->DR;
261 
262  return ADC_DR_RESULT( adc_value );
263 }
264 
HAL_Status_Type HAL_ADC_Init(ADC_Type *ADCx, ADC_CFG_Type *ADC_Config)
Initialize the ADC peripheral with the specified parameters.
HAL_Status_Type HAL_ADC_ChannelSel(ADC_Type *ADCx, uint32_t Channel)
Select ADC Channel Number.
uint32_t InClkDiv
void HAL_SCU_Peripheral_EnableClock2(uint32_t u32PeriClk2, uint32_t u32Ind)
Set Each Peripheral Clock.
HAL_Status_Type
uint32_t HAL_ADC_GetStatus(ADC_Type *ADCx)
Get ADC channel status.
HAL_Status_Type HAL_ADC_Stop(ADC_Type *ADCx)
Stop A/D conversion If this function called after a conversion cycle starts, the current conversion i...
uint16_t HAL_ADC_GetData(ADC_Type *ADCx)
Get Result conversion from A/D data register.
HAL_Status_Type HAL_ADC_Start(ADC_Type *ADCx)
Start A/D conversion.
HAL_Status_Type HAL_ADC_DeInit(ADC_Type *ADCx)
Close ADC.
HAL_Status_Type HAL_ADC_ClearStatus(ADC_Type *ADCx)
Clear ADC channel status.
HAL_Status_Type HAL_ADC_ConfigInterrupt(ADC_Type *ADCx, FunctionalState NewState)
ADC interrupt configuration.
FunctionalState
Contains all macro definitions and function prototypes support for adc firmware library on A31G12x.
Contains all macro definitions and function prototypes support for scu firmware library on A31G12x.