libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
adc_common_v2_multi.c
Go to the documentation of this file.
1/** @addtogroup adc_file ADC peripheral API
2@ingroup peripheral_apis
3
4@author @htmlonly © @endhtmlonly
52016 Karl Palsson <karlp@tweak.net.au>
6
7This provides the "multi" extensions to the "v2" ADC peripheral. This is those
8devices that support injected channels and per channel sampling times.
9At the time of writing, this is the STM32F30x and the STM32L4x
10
11LGPL License Terms @ref lgpl_license
12 */
13
14/*
15 * This file is part of the libopencm3 project.
16 *
17 * Copyright (C) 2016 Karl Palsson <karlp@tweak.net.au>
18 *
19 * This library is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU Lesser General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with this library. If not, see <http://www.gnu.org/licenses/>.
31 */
32
33/**@{*/
34
36
37
38/** @brief ADC Set the Sample Time for a Single Channel
39 *
40 * The sampling time can be selected in ADC clock cycles, exact values
41 * depend on the device.
42 *
43 * @param[in] adc ADC block register address base @ref adc_reg_base
44 * @param[in] channel ADC Channel integer @ref adc_channel
45 * @param[in] time Sampling time selection from @ref adc_sample
46 */
47void adc_set_sample_time(uint32_t adc, uint8_t channel, uint8_t time)
48{
49 uint32_t reg32;
50
51 if (channel < 10) {
52 reg32 = ADC_SMPR1(adc);
53 reg32 &= ~(0x7 << (channel * 3));
54 reg32 |= (time << (channel * 3));
55 ADC_SMPR1(adc) = reg32;
56 } else {
57 reg32 = ADC_SMPR2(adc);
58 reg32 &= ~(0x7 << ((channel - 10) * 3));
59 reg32 |= (time << ((channel - 10) * 3));
60 ADC_SMPR2(adc) = reg32;
61 }
62}
63
64/** @brief ADC Set the Sample Time for All Channels
65 *
66 * The sampling time can be selected in ADC clock cycles, exact values
67 * depend on the device.
68 *
69 * @param[in] adc ADC block register address base @ref adc_reg_base
70 * @param[in] time Sampling time selection from @ref adc_sample
71 */
72void adc_set_sample_time_on_all_channels(uint32_t adc, uint8_t time)
73{
74 uint8_t i;
75 uint32_t reg32 = 0;
76
77 for (i = 0; i <= 9; i++) {
78 reg32 |= (time << (i * 3));
79 }
80 ADC_SMPR1(adc) = reg32;
81
82 for (i = 10; i <= 17; i++) {
83 reg32 |= (time << ((i - 10) * 3));
84 }
85 ADC_SMPR2(adc) = reg32;
86}
87
88/*---------------------------------------------------------------------------*/
89/** @brief ADC Set a Regular Channel Conversion Sequence
90 *
91 * Define a sequence of channels to be converted as a regular group with a
92 * length from 1 to 16 channels. If this is called during conversion, the
93 * current conversion is reset and conversion begins again with the newly
94 * defined group.
95 *
96 * @param[in] adc ADC block register address base @ref adc_reg_base
97 * @param[in] length Number of channels in the group, range 0..16
98 * @param[in] channel Set of channels in sequence, range @ref adc_channel
99 */
100void adc_set_regular_sequence(uint32_t adc, uint8_t length, uint8_t channel[])
101{
102 uint32_t reg32_1 = 0, reg32_2 = 0, reg32_3 = 0, reg32_4 = 0;
103 uint8_t i = 0;
104
105 /* Maximum sequence length is 16 channels. */
106 if (length > 16) {
107 return;
108 }
109
110 for (i = 1; i <= length; i++) {
111 if (i <= 4) {
112 reg32_1 |= (channel[i - 1] << (i * 6));
113 }
114 if ((i > 4) && (i <= 9)) {
115 reg32_2 |= (channel[i - 1] << ((i - 4 - 1) * 6));
116 }
117 if ((i > 9) && (i <= 14)) {
118 reg32_3 |= (channel[i - 1] << ((i - 9 - 1) * 6));
119 }
120 if ((i > 14) && (i <= 16)) {
121 reg32_4 |= (channel[i - 1] << ((i - 14 - 1) * 6));
122 }
123 }
124 reg32_1 |= ((length - 1) << ADC_SQR1_L_SHIFT);
125
126 ADC_SQR1(adc) = reg32_1;
127 ADC_SQR2(adc) = reg32_2;
128 ADC_SQR3(adc) = reg32_3;
129 ADC_SQR4(adc) = reg32_4;
130}
131
132/**@}*/
133
#define ADC_SQR1_L_SHIFT
void adc_set_sample_time_on_all_channels(uint32_t adc, uint8_t time)
ADC Set the Sample Time for All Channels.
void adc_set_sample_time(uint32_t adc, uint8_t channel, uint8_t time)
ADC Set the Sample Time for a Single Channel.
void adc_set_regular_sequence(uint32_t adc, uint8_t length, uint8_t channel[])
ADC Set a Regular Channel Conversion Sequence.
#define ADC_SQR3(adc)
#define ADC_SMPR1(adc)
Sample Time Register 1.
Definition: adc_common_v2.h:52
#define ADC_SQR4(adc)
#define ADC_SQR2(adc)
#define ADC_SQR1(adc)
#define ADC_SMPR2(adc)