A31L12x F/W Packages  1.4.0
ABOV Cortex-M0+ Core based MCUs Integrated Driver
A31L12x_hal_adc.c
Go to the documentation of this file.
1 /***************************************************************************//****************************************************************************/
34 
35 /* Includes ----------------------------------------------------------------- */
36 //******************************************************************************
37 // Include
38 //******************************************************************************
39 
40 #include "A31L12x_hal_adc.h"
41 #include "A31L12x_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  tempreg = 0
71  | ( ( ( ADC_Config->TrgSel ) & 7 ) << ADC_CR_TRIG_Pos ) // TRGSRC
72  | ( ( ( ADC_Config->ExtTrgPolSel ) & 3 ) << ADC_CR_ETRGP_Pos ) // Ext Trigger Polarity
73  | ( ( ( ADC_Config->ConvModSel ) & 3 ) << ADC_CR_MDSEL_Pos ) // Conversion Mode
74  ;
75  ADCx->CR = tempreg;
76 
77  ADCx->PREDR = ( ADC_Config->InClkDiv & ADC_PREDR_PRED_Msk );
78 
79  ADCx->SAMR = ( ADC_Config->SamplingClk & ADC_SAMR_SAMCK_Msk ); // Sampling Cycles: SamplingClk + 2
80 
81  return HAL_OK;
82 }
83 
84 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
92 {
93  /* Check ADC handle */
94  if( ADCx == NULL )
95  {
96  return HAL_ERROR;
97  }
98 
99  ADCRDY_Stop(); // Stop subsequent steps
100  ADCDIS(); // Disable ADC module
101  HAL_SCU_Peripheral_EnableClock2( PPCLKEN2_ADCLKE, PPxCLKE_Disable );
102 
103  return HAL_OK;
104 }
105 
106 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
119 {
120  uint32_t tempreg;
121 
122  /* Check ADC handle */
123  if( ADCx == NULL )
124  {
125  return HAL_ERROR;
126  }
127 
128  // get mask
129  switch( ADCIntCfg )
130  {
131  case ADC_INTCFG_EOS:
132  tempreg = ADC_IESR_EOSIEN_Msk;
133  break;
134  case ADC_INTCFG_EOC:
135  tempreg = ADC_IESR_EOCIEN_Msk;
136  break;
137  case ADC_INTCFG_OVRUN:
138  tempreg = ADC_IESR_OVRUNIEN_Msk;
139  break;
140  case ADC_INTCFG_STB:
141  tempreg = ADC_IESR_STBIEN_Msk;
142  break;
143  default:
144  return HAL_ERROR;
145  }
146 
147  // enable/disable
148  if( NewState == ENABLE )
149  {
150  ADCx->IESR = ( ( ADCx->IESR & ADC_INT_STATUS_MSK ) | tempreg );
151  }
152  else
153  {
154  ADCx->IESR = ( ( ADCx->IESR & ADC_INT_STATUS_MSK ) & ( ~tempreg ) );
155  }
156 
157  return HAL_OK;
158 }
159 
160 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
173 HAL_Status_Type HAL_ADC_OvSamplingConfig( ADC_Type* ADCx, ADC_OV_SAMPLING_RATIO OvSampRatio, ADC_OVSCR_OVSHT_Enum DataShiftRight )
174 {
175  uint32_t tempreg;
176 
177  /* Check ADC handle */
178  if( ADCx == NULL )
179  {
180  return HAL_ERROR;
181  }
182 
183  if( DataShiftRight > ADC_OVSCR_OVSHT_ShiftRight8Bit )
184  {
185  DataShiftRight = ADC_OVSCR_OVSHT_NoShift;
186  }
187  tempreg = 0
188  | ( ( ( OvSampRatio ) & 7 ) << ADC_OVSCR_OVSMPR_Pos ) // Oversampling ratio
189  | ( ( DataShiftRight ) << ADC_OVSCR_OVSHT_Pos ) // Shift right bits
190  ;
191  ADCx->OVSCR = tempreg;
192  ADC_OVSMPEN(); // ADC Oversampling Enable
193 
194  return HAL_OK;
195 }
196 
197 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
207 HAL_Status_Type HAL_ADC_ChannelSel( ADC_Type* ADCx, uint32_t Channels )
208 {
209  /* Check ADC handle */
210  if( ADCx == NULL )
211  {
212  return HAL_ERROR;
213  }
214 
215  ADCx->CHSELR = Channels;
216 
217  return HAL_OK;
218 }
219 
220 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
228 {
229  /* Check ADC handle */
230  if( ADCx == NULL )
231  {
232  return HAL_ERROR;
233  }
234 
235  ADCADST_Set(); // Start conversion by s/w
236 
237  return HAL_OK;
238 }
239 
240 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
248 {
249  /* Check ADC handle */
250  if( ADCx == NULL )
251  {
252  return HAL_ERROR;
253  }
254 
255  ADCEN(); // ADCEN;
256  while( !ADCInt_GetStbFg() ) {} // Check ADC stabiliaztion
257  ADCInt_ClrStbFg(); // Clear the stabiliaztion flag
258  ADCRDY_Conv(); // Ready for conversion
259 
260  return HAL_OK;
261 }
262 
263 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
273 {
274  uint32_t tempreg;
275 
276  /* Check ADC handle */
277  if( ADCx == NULL )
278  {
279  return HAL_ERROR;
280  }
281 
282  switch( ADCInt_status )
283  {
284  case ADC_INT_STATUS_EOS:
285  tempreg = ADC_IESR_EOSIFLAG_Msk;
286  break;
287  case ADC_INT_STATUS_EOC:
288  tempreg = ADC_IESR_EOCIFLAG_Msk;
289  break;
291  tempreg = ADC_IESR_OVRUNIFLAG_Msk;
292  break;
293  case ADC_INT_STATUS_STB:
294  tempreg = ADC_IESR_STBIFLAG_Msk;
295  break;
296  case ADC_INT_STATUS_ALL:
297  tempreg = ADC_IESR_EOSIFLAG_Msk | ADC_IESR_EOCIFLAG_Msk | ADC_IESR_OVRUNIFLAG_Msk | ADC_IESR_STBIFLAG_Msk;
298  break;
299  default:
300  return HAL_ERROR;
301  }
302 
303  ADCx->IESR = ( ADCx->IESR & ADC_INT_STATUS_MSK ) | tempreg;
304 
305  return HAL_OK;
306 }
307 
308 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
315 uint8_t HAL_ADC_GetStatus( ADC_Type* ADCx )
316 {
317  return ( uint8_t )( ( ADCx->IESR ) & ( ~ADC_INT_STATUS_MSK ) );
318 }
319 
320 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
327 uint16_t HAL_ADC_GetData( ADC_Type* ADCx )
328 {
329  uint16_t adc_value;
330 
331  adc_value = ADCx->DR;
332  return ADC_DR_RESULT( adc_value );
333 }
334 
HAL_Status_Type HAL_ADC_Init(ADC_Type *ADCx, ADC_CFG_Type *ADC_Config)
Initialize the ADC peripheral with the specified parameters.
ADC_INT_Type
uint32_t SamplingClk
uint32_t InClkDiv
uint8_t HAL_ADC_GetStatus(ADC_Type *ADCx)
Get ADC interrupt status.
void HAL_SCU_Peripheral_EnableClock2(uint32_t u32PeriClk2, uint32_t u32Ind)
Set Each Peripheral Clock.
HAL_Status_Type
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_ChannelSel(ADC_Type *ADCx, uint32_t Channels)
Select ADC Channels to conversion (Ex: ADC_AN0 | ADC_AN5 | ADC_AN14 | ADC_AVDD)
HAL_Status_Type HAL_ADC_ConfigInterrupt(ADC_Type *ADCx, ADC_INT_Type ADCIntCfg, FunctionalState NewState)
ADC interrupt configuration.
HAL_Status_Type HAL_ADC_ClearStatus(ADC_Type *ADCx, ADC_INT_STATUS_Type ADCInt_status)
Clear ADC interrupt status.
ADC_OV_SAMPLING_RATIO
HAL_Status_Type HAL_ADC_OvSamplingConfig(ADC_Type *ADCx, ADC_OV_SAMPLING_RATIO OvSampRatio, ADC_OVSCR_OVSHT_Enum DataShiftRight)
ADC oversampling configuration.
ADC_INT_STATUS_Type
uint32_t ExtTrgPolSel
FunctionalState
uint32_t ConvModSel
HAL_Status_Type HAL_ADC_RdyCmd(ADC_Type *ADCx)
Ready procedure for ADC conversion.
Contains all macro definitions and function prototypes support for adc firmware library on A31L12x.
Contains all macro definitions and function prototypes support for scu firmware library on A31L12x.