libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
adc.c
Go to the documentation of this file.
1/** @addtogroup adc_file ADC peripheral API
2@ingroup peripheral_apis
3
4@author @htmlonly &copy; @endhtmlonly 2014 Karl Palsson <karlp@tweak.net.au>
5
6
7LGPL License Terms @ref lgpl_license
8 */
9/*
10 * This file is part of the libopencm3 project.
11 *
12 * Copyright (C) 2014 Karl Palsson <karlp@tweak.net.au>
13 *
14 * This library is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 */
27
29
30/**@{*/
31
32/*----------------------------------------------------------------------------*/
33
34/** @brief ADC Set the Sample Time for a Single Channel
35
36The sampling time can be selected in ADC clock cycles from 4 to 384.
37
38@param[in] adc Unsigned int32. ADC block base address @ref adc_reg_base.
39@param[in] channel uint8. ADC Channel integer 0..18 or from @ref adc_channel.
40@param[in] time Unsigned int8. Sampling time selection from @ref adc_sample_rg.
41 */
42void adc_set_sample_time(uint32_t adc, uint8_t channel, uint8_t time)
43{
44 uint32_t reg32;
45
46 if (channel < 10) {
47 reg32 = ADC_SMPR3(adc);
48 reg32 &= ~(0x7 << (channel * 3));
49 reg32 |= (time << (channel * 3));
50 ADC_SMPR3(adc) = reg32;
51 } else if (channel < 20) {
52 reg32 = ADC_SMPR2(adc);
53 reg32 &= ~(0x7 << ((channel - 10) * 3));
54 reg32 |= (time << ((channel - 10) * 3));
55 ADC_SMPR2(adc) = reg32;
56 } else {
57 reg32 = ADC_SMPR1(adc);
58 reg32 &= ~(0x7 << ((channel - 20) * 3));
59 reg32 |= (time << ((channel - 20) * 3));
60 ADC_SMPR1(adc) = reg32;
61 }
62}
63
64/*----------------------------------------------------------------------------*/
65/** @brief ADC Set the Sample Time for All Channels
66
67The sampling time can be selected in ADC clock cycles, same for
68all channels.
69
70@param[in] adc Unsigned int32. ADC block base address @ref adc_reg_base.
71@param[in] time Unsigned int8. Sampling time selection from @ref adc_sample_rg.
72*/
73
74void adc_set_sample_time_on_all_channels(uint32_t adc, uint8_t time)
75{
76 uint8_t i;
77 uint32_t reg32 = 0;
78
79 for (i = 0; i <= 9; i++) {
80 reg32 |= (time << (i * 3));
81 }
82 ADC_SMPR0(adc) = reg32;
83 ADC_SMPR1(adc) = reg32;
84 ADC_SMPR2(adc) = reg32;
85 ADC_SMPR3(adc) = reg32;
86}
87
88/**@}*/
89
90
#define ADC_SMPR1(block)
Definition: adc_common_v1.h:61
#define ADC_SMPR2(block)
Definition: adc_common_v1.h:64
void adc_set_sample_time_on_all_channels(uint32_t adc, uint8_t time)
ADC Set the Sample Time for All Channels.
Definition: adc.c:74
void adc_set_sample_time(uint32_t adc, uint8_t channel, uint8_t time)
ADC Set the Sample Time for a Single Channel.
Definition: adc.c:42
#define ADC_SMPR3(block)
Definition: l1/adc.h:43
#define ADC_SMPR0(block)
Definition: l1/adc.h:88