tao_z
2021-04-11 6d4ddf7d2a2cd72c58876257700b34c63e5063d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
  ******************************************************************************
  * @file    stm8s_adc2.c
  * @author  MCD Application Team
  * @version V2.2.0
  * @date    30-September-2014
  * @brief   This file contains all the functions/macros for the ADC2 peripheral.
   ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "stm8s_adc2.h"
 
/** @addtogroup STM8S_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
 
/* Public functions ----------------------------------------------------------*/
 
/**
  * @addtogroup ADC2_Public_Functions
  * @{
  */
 
/**
  * @brief  Deinitializes the ADC2 peripheral registers to their default reset
  * values.
  * @param  None
  * @retval None
  */
void ADC2_DeInit(void)
{
  ADC2->CSR  = ADC2_CSR_RESET_VALUE;
  ADC2->CR1  = ADC2_CR1_RESET_VALUE;
  ADC2->CR2  = ADC2_CR2_RESET_VALUE;
  ADC2->TDRH = ADC2_TDRH_RESET_VALUE;
  ADC2->TDRL = ADC2_TDRL_RESET_VALUE;
}
 
/**
  * @brief  Initializes the ADC2 peripheral according to the specified parameters
  * @param   ADC2_ConversionMode: specifies the conversion mode
  * can be one of the values of @ref ADC2_ConvMode_TypeDef.
  * @param   ADC2_Channel: specifies the channel to convert
  * can be one of the values of @ref ADC2_Channel_TypeDef.
  * @param   ADC2_PrescalerSelection: specifies the ADC2 prescaler
  * can be one of the values of @ref ADC2_PresSel_TypeDef.
  * @param   ADC2_ExtTrigger: specifies the external trigger
  * can be one of the values of @ref ADC2_ExtTrig_TypeDef.
  * @param   ADC2_ExtTriggerState: specifies the external trigger new state
  * can be one of the values of @ref FunctionalState.
  * @param   ADC2_Align: specifies the converted data alignment
  * can be one of the values of @ref ADC2_Align_TypeDef.
  * @param   ADC2_SchmittTriggerChannel: specifies the schmitt trigger channel
  * can be one of the values of @ref ADC2_SchmittTrigg_TypeDef.
  * @param   ADC2_SchmittTriggerState: specifies the schmitt trigger state
  * can be one of the values of @ref FunctionalState.
  * @retval None
  */
void ADC2_Init(ADC2_ConvMode_TypeDef ADC2_ConversionMode, ADC2_Channel_TypeDef ADC2_Channel, ADC2_PresSel_TypeDef ADC2_PrescalerSelection, ADC2_ExtTrig_TypeDef ADC2_ExtTrigger, FunctionalState ADC2_ExtTriggerState, ADC2_Align_TypeDef ADC2_Align, ADC2_SchmittTrigg_TypeDef ADC2_SchmittTriggerChannel, FunctionalState ADC2_SchmittTriggerState)
{
  /* Check the parameters */
  assert_param(IS_ADC2_CONVERSIONMODE_OK(ADC2_ConversionMode));
  assert_param(IS_ADC2_CHANNEL_OK(ADC2_Channel));
  assert_param(IS_ADC2_PRESSEL_OK(ADC2_PrescalerSelection));
  assert_param(IS_ADC2_EXTTRIG_OK(ADC2_ExtTrigger));
  assert_param(IS_FUNCTIONALSTATE_OK(((ADC2_ExtTriggerState))));
  assert_param(IS_ADC2_ALIGN_OK(ADC2_Align));
  assert_param(IS_ADC2_SCHMITTTRIG_OK(ADC2_SchmittTriggerChannel));
  assert_param(IS_FUNCTIONALSTATE_OK(ADC2_SchmittTriggerState));
  
  /*-----------------CR1 & CSR configuration --------------------*/
  /* Configure the conversion mode and the channel to convert
  respectively according to ADC2_ConversionMode & ADC2_Channel values  &  ADC2_Align values */
  ADC2_ConversionConfig(ADC2_ConversionMode, ADC2_Channel, ADC2_Align);
  /* Select the prescaler division factor according to ADC2_PrescalerSelection values */
  ADC2_PrescalerConfig(ADC2_PrescalerSelection);
  
  /*-----------------CR2 configuration --------------------*/
  /* Configure the external trigger state and event respectively
  according to ADC2_ExtTrigStatus, ADC2_ExtTrigger */
  ADC2_ExternalTriggerConfig(ADC2_ExtTrigger, ADC2_ExtTriggerState);
  
  /*------------------TDR configuration ---------------------------*/
  /* Configure the schmitt trigger channel and state respectively
  according to ADC2_SchmittTriggerChannel & ADC2_SchmittTriggerNewState  values */
  ADC2_SchmittTriggerConfig(ADC2_SchmittTriggerChannel, ADC2_SchmittTriggerState);
  
  /* Enable the ADC2 peripheral */
  ADC2->CR1 |= ADC2_CR1_ADON;
}
 
/**
  * @brief  Enables or Disables the ADC2 peripheral.
  * @param   NewState: specifies the peripheral enabled or disabled state.
  * @retval None
  */
void ADC2_Cmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  if (NewState != DISABLE)
  {
    ADC2->CR1 |= ADC2_CR1_ADON;
  }
  else /* NewState == DISABLE */
  {
    ADC2->CR1 &= (uint8_t)(~ADC2_CR1_ADON);
  }
}
 
/**
  * @brief  Enables or disables the ADC2 interrupt.
  * @param   NewState specifies the state of ADC2 interrupt.
  * @retval None
  */
void ADC2_ITConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  if (NewState != DISABLE)
  {
    /* Enable the ADC2 interrupts */
    ADC2->CSR |= (uint8_t)ADC2_CSR_EOCIE;
  }
  else  /*NewState == DISABLE */
  {
    /* Disable the ADC2 interrupts */
    ADC2->CSR &= (uint8_t)(~ADC2_CSR_EOCIE);
  }
}
 
/**
  * @brief  Configure the ADC2 prescaler division factor.
  * @param   ADC2_Prescaler: the selected prescaler.
  * It can be one of the values of @ref ADC2_PresSel_TypeDef.
  * @retval None
  */
void ADC2_PrescalerConfig(ADC2_PresSel_TypeDef ADC2_Prescaler)
{
  /* Check the parameter */
  assert_param(IS_ADC2_PRESSEL_OK(ADC2_Prescaler));
  
  /* Clear the SPSEL bits */
  ADC2->CR1 &= (uint8_t)(~ADC2_CR1_SPSEL);
  /* Select the prescaler division factor according to ADC2_PrescalerSelection values */
  ADC2->CR1 |= (uint8_t)(ADC2_Prescaler);
}
 
/**
  * @brief  Enables or disables the ADC2 Schmitt Trigger on a selected channel.
  * @param   ADC2_SchmittTriggerChannel specifies the desired Channel.
  * It can be set of the values of @ref ADC2_SchmittTrigg_TypeDef.
  * @param   NewState specifies the Channel  ADC2 Schmitt Trigger new status.
  * can have one of the values of @ref FunctionalState.
  * @retval None
  */
void ADC2_SchmittTriggerConfig(ADC2_SchmittTrigg_TypeDef ADC2_SchmittTriggerChannel, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_ADC2_SCHMITTTRIG_OK(ADC2_SchmittTriggerChannel));
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  if (ADC2_SchmittTriggerChannel == ADC2_SCHMITTTRIG_ALL)
  {
    if (NewState != DISABLE)
    {
      ADC2->TDRL &= (uint8_t)0x0;
      ADC2->TDRH &= (uint8_t)0x0;
    }
    else /* NewState == DISABLE */
    {
      ADC2->TDRL |= (uint8_t)0xFF;
      ADC2->TDRH |= (uint8_t)0xFF;
    }
  }
  else if (ADC2_SchmittTriggerChannel < ADC2_SCHMITTTRIG_CHANNEL8)
  {
    if (NewState != DISABLE)
    {
      ADC2->TDRL &= (uint8_t)(~(uint8_t)((uint8_t)0x01 << (uint8_t)ADC2_SchmittTriggerChannel));
    }
    else /* NewState == DISABLE */
    {
      ADC2->TDRL |= (uint8_t)((uint8_t)0x01 << (uint8_t)ADC2_SchmittTriggerChannel);
    }
  }
  else /* ADC2_SchmittTriggerChannel >= ADC2_SCHMITTTRIG_CHANNEL8 */
  {
    if (NewState != DISABLE)
    {
      ADC2->TDRH &= (uint8_t)(~(uint8_t)((uint8_t)0x01 << ((uint8_t)ADC2_SchmittTriggerChannel - (uint8_t)8)));
    }
    else /* NewState == DISABLE */
    {
      ADC2->TDRH |= (uint8_t)((uint8_t)0x01 << ((uint8_t)ADC2_SchmittTriggerChannel - (uint8_t)8));
    }
  }
}
 
/**
  * @brief  Configure the ADC2 conversion on selected channel.
  * @param   ADC2_ConversionMode Specifies the conversion type.
  * It can be set of the values of @ref ADC2_ConvMode_TypeDef
  * @param   ADC2_Channel specifies the ADC2 Channel.
  * It can be set of the values of @ref ADC2_Channel_TypeDef
  * @param   ADC2_Align specifies the converted data alignment.
  * It can be set of the values of @ref ADC2_Align_TypeDef
  * @retval None
  */
void ADC2_ConversionConfig(ADC2_ConvMode_TypeDef ADC2_ConversionMode, ADC2_Channel_TypeDef ADC2_Channel, ADC2_Align_TypeDef ADC2_Align)
{
  /* Check the parameters */
  assert_param(IS_ADC2_CONVERSIONMODE_OK(ADC2_ConversionMode));
  assert_param(IS_ADC2_CHANNEL_OK(ADC2_Channel));
  assert_param(IS_ADC2_ALIGN_OK(ADC2_Align));
  
  /* Clear the align bit */
  ADC2->CR2 &= (uint8_t)(~ADC2_CR2_ALIGN);
  /* Configure the data alignment */
  ADC2->CR2 |= (uint8_t)(ADC2_Align);
  
  if (ADC2_ConversionMode == ADC2_CONVERSIONMODE_CONTINUOUS)
  {
    /* Set the continuous conversion mode */
    ADC2->CR1 |= ADC2_CR1_CONT;
  }
  else /* ADC2_ConversionMode == ADC2_CONVERSIONMODE_SINGLE */
  {
    /* Set the single conversion mode */
    ADC2->CR1 &= (uint8_t)(~ADC2_CR1_CONT);
  }
  
  /* Clear the ADC2 channels */
  ADC2->CSR &= (uint8_t)(~ADC2_CSR_CH);
  /* Select the ADC2 channel */
  ADC2->CSR |= (uint8_t)(ADC2_Channel);
}
 
/**
  * @brief  Configure the ADC2 conversion on external trigger event.
  * @par Full description:
  * The selected external trigger event can be enabled or disabled.
  * @param   ADC2_ExtTrigger to select the External trigger event.
  * can have one of the values of @ref ADC2_ExtTrig_TypeDef.
  * @param   NewState to enable/disable the selected external trigger
  * can have one of the values of @ref FunctionalState.
  * @retval None
  */
void ADC2_ExternalTriggerConfig(ADC2_ExtTrig_TypeDef ADC2_ExtTrigger, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_ADC2_EXTTRIG_OK(ADC2_ExtTrigger));
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  /* Clear the external trigger selection bits */
  ADC2->CR2 &= (uint8_t)(~ADC2_CR2_EXTSEL);
  
  if (NewState != DISABLE)
  {
    /* Enable the selected external Trigger */
    ADC2->CR2 |= (uint8_t)(ADC2_CR2_EXTTRIG);
  }
  else /* NewState == DISABLE */
  {
    /* Disable the selected external trigger */
    ADC2->CR2 &= (uint8_t)(~ADC2_CR2_EXTTRIG);
  }
  
  /* Set the selected external trigger */
  ADC2->CR2 |= (uint8_t)(ADC2_ExtTrigger);
}
 
/**
  * @brief  Start ADC2 conversion
  * @par Full description:
  * This function  triggers the start of conversion, after ADC2 configuration.
  * @param  None
  * @retval None
  * @par Required preconditions:
  * Enable the ADC2 peripheral before calling this function
  */
void ADC2_StartConversion(void)
{
  ADC2->CR1 |= ADC2_CR1_ADON;
}
 
/**
  * @brief  Get one sample of measured signal.
  * @param  None
  * @retval ConversionValue:  value of the measured signal.
  * @par Required preconditions:
  * ADC2 conversion finished.
  */
uint16_t ADC2_GetConversionValue(void)
{
  uint16_t temph = 0;
  uint8_t templ = 0;
  
  if ((ADC2->CR2 & ADC2_CR2_ALIGN) != 0) /* Right alignment */
  {
    /* Read LSB first */
    templ = ADC2->DRL;
    /* Then read MSB */
    temph = ADC2->DRH;
    
    temph = (uint16_t)(templ | (uint16_t)(temph << (uint8_t)8));
  }
  else /* Left alignment */
  {
    /* Read MSB first*/
    temph = ADC2->DRH;
    /* Then read LSB */
    templ = ADC2->DRL;
    
    temph = (uint16_t)((uint16_t)((uint16_t)templ << 6) | (uint16_t)((uint16_t)temph << 8));
  }
  
  return ((uint16_t)temph);
}
 
/**
  * @brief  Checks the ADC2 EOC flag status.
  * @param  None
  * @retval FlagStatus Status of the ADC2 EOC flag.
  */
FlagStatus ADC2_GetFlagStatus(void)
{
  /* Get EOC  flag status */
  return (FlagStatus)(ADC2->CSR & ADC2_CSR_EOC);
}
 
/**
  * @brief  Clear the ADC2 EOC Flag.
  * @param  None
  * @retval None
  */
void ADC2_ClearFlag(void)
{
  ADC2->CSR &= (uint8_t)(~ADC2_CSR_EOC);
}
 
/**
  * @brief  Returns the EOC  pending bit status
 * @par Parameters:
  * None
  * @retval FlagStatus: status of the EOC pending bit.
  */
ITStatus ADC2_GetITStatus(void)
{
  return (ITStatus)(ADC2->CSR & ADC2_CSR_EOC);
}
 
/**
  * @brief  Clear the ADC2 End of Conversion pending bit.
  * @param  None
  * @retval None
  */
void ADC2_ClearITPendingBit(void)
{
  ADC2->CSR &= (uint8_t)(~ADC2_CSR_EOC);
}
 
/**
  * @}
  */
  
/**
  * @}
  */
  
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/