1 /*
2 * Copyright (c) 2022 - 2023, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <nrfx_example.h>
35 #include <saadc_examples_common.h>
36 #include <nrfx_saadc.h>
37
38 #define NRFX_LOG_MODULE EXAMPLE
39 #define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
40 #define NRFX_EXAMPLE_CONFIG_LOG_LEVEL 3
41 #include <nrfx_log.h>
42
43 /**
44 * @defgroup nrfx_saadc_simple_blocking_example Simple blocking SAADC example
45 * @{
46 * @ingroup nrfx_saadc_examples
47 *
48 * @brief Example showing simple functionality of nrfx_saadc driver operating in the blocking mode.
49 *
50 * @details Application initializes nrfx_saadc driver and starts operating in the blocking mode.
51 * Program works as a simple state machine and starts in state @ref m_current_state == STATE_SINGLE_CONFIG.
52 * In the state ( @ref m_current_state ):
53 * - STATE_SINGLE_CONFIG - SAADC driver is configured to work with only one channel ( @ref m_single_channel )
54 * in the blocking mode. @ref m_current_state is changed to STATE_SINGLE_SAMPLING afterward.
55 * - STATE_SINGLE_SAMPLING - sampling on a single channel ( @ref m_single_channel ) is performed specified
56 * number of times ( @ref SAMPLING_ITERATIONS ), after that @ref m_current_state is changed to STATE_MULTIPLE_CONFIG.
57 * - STATE_MULTIPLE_CONFIG - SAADC driver is configured to work with multiple channels ( @ref m_multiple_channels )
58 * in the blocking mode. @ref m_current_state is changed to STATE_MULTIPLE_SAMPLING afterward.
59 * - STATE_MULTIPLE_SAMPLING - sampling on multiple channels ( @ref m_multiple_channels ) is performed specified
60 * number of times ( @ref SAMPLING_ITERATIONS ).
61 * Before every sampling, calibration in a blocking manner is performed. After it, sampling is invoked
62 * by @p nrfx_saadc_mode_trigger() function.
63 *
64 * In the example there are GPIOTE tasks configured to toggle specified ( @ref m_out_pins ) loopback pins. Those tasks
65 * are being triggered between successive samplings to verify the functionality of the SAADC on the non-constant analog signal.
66 */
67
68 /** @brief Symbol specifying analog input to be observed by SAADC channel 0. */
69 #define CH0_AIN ANALOG_INPUT_TO_SAADC_AIN(ANALOG_INPUT_A0)
70
71 /** @brief Symbol specifying analog input to be observed by SAADC channel 1. */
72 #define CH1_AIN ANALOG_INPUT_TO_SAADC_AIN(ANALOG_INPUT_A1)
73
74 /** @brief Symbol specifying analog input to be observed by SAADC channel 2. */
75 #define CH2_AIN ANALOG_INPUT_TO_SAADC_AIN(ANALOG_INPUT_A2)
76
77 /** @brief Declaration of enum containing a set of states for the simple state machine. */
78 typedef enum
79 {
80 STATE_SINGLE_CONFIG, ///< Configure a single SAADC channel and set the SAADC driver in the simple mode.
81 STATE_SINGLE_SAMPLING, ///< Trigger SAADC sampling on the single channel.
82 STATE_MULTIPLE_CONFIG, ///< Configure multiple SAADC channels and set the SAADC driver in the simple mode.
83 STATE_MULTIPLE_SAMPLING, ///< Trigger SAADC sampling on multiple channels.
84 } state_t;
85
86 /** @brief SAADC channel configuration structure for single channel use. */
87 static const nrfx_saadc_channel_t m_single_channel = NRFX_SAADC_DEFAULT_CHANNEL_SE(CH0_AIN, 0);
88
89 /** @brief SAADC channel configuration structure for multiple channel use. */
90 static const nrfx_saadc_channel_t m_multiple_channels[] =
91 {
92 NRFX_SAADC_DEFAULT_CHANNEL_SE(CH0_AIN, 0),
93 NRFX_SAADC_DEFAULT_CHANNEL_SE(CH1_AIN, 1),
94 NRFX_SAADC_DEFAULT_CHANNEL_SE(CH2_AIN, 2),
95 };
96
97 /** @brief Symbol specifying numbers of multiple channels ( @ref m_multiple_channels) used by SAADC. */
98 #define CHANNEL_COUNT NRFX_ARRAY_SIZE(m_multiple_channels)
99
100 /** @brief Array specifying GPIO pins used to test the functionality of SAADC. */
101 static uint8_t m_out_pins[CHANNEL_COUNT] = {LOOPBACK_PIN_1B, LOOPBACK_PIN_2B, LOOPBACK_PIN_3B};
102
103 /** @brief Samples buffer defined with the size of @ref CHANNEL_COUNT symbol to store values from each channel ( @ref m_multiple_channels). */
104 static nrf_saadc_value_t samples_buffer[CHANNEL_COUNT];
105
106 /** @brief Symbol specifying the number of SAADC samplings to trigger. */
107 #define SAMPLING_ITERATIONS 8
108
109 /** @brief Enum with the current state of the simple state machine. */
110 static state_t m_current_state = STATE_SINGLE_CONFIG;
111
112 /**
113 * @brief Function for application main entry.
114 *
115 * @return Nothing.
116 */
main(void)117 int main(void)
118 {
119 nrfx_err_t status;
120 (void)status;
121
122 NRFX_EXAMPLE_LOG_INIT();
123 NRFX_LOG_INFO("Starting nrfx_saadc simple blocking example.");
124 NRFX_EXAMPLE_LOG_PROCESS();
125
126 status = nrfx_saadc_init(NRFX_SAADC_DEFAULT_CONFIG_IRQ_PRIORITY);
127 NRFX_ASSERT(status == NRFX_SUCCESS);
128
129 uint8_t i;
130 for (i = 0; i < CHANNEL_COUNT; i++)
131 {
132 gpiote_pin_toggle_task_setup(m_out_pins[i]);
133 }
134
135 uint32_t sampling_index = 0;
136 while (1)
137 {
138 switch (m_current_state)
139 {
140 case STATE_SINGLE_CONFIG:
141 NRFX_LOG_INFO("Single channel SAADC test.");
142
143 status = nrfx_saadc_channel_config(&m_single_channel);
144 NRFX_ASSERT(status == NRFX_SUCCESS);
145
146 uint32_t channels_mask = nrfx_saadc_channels_configured_get();
147 status = nrfx_saadc_simple_mode_set(channels_mask,
148 NRF_SAADC_RESOLUTION_8BIT,
149 NRF_SAADC_OVERSAMPLE_DISABLED,
150 NULL);
151 NRFX_ASSERT(status == NRFX_SUCCESS);
152
153 status = nrfx_saadc_buffer_set(samples_buffer, 1);
154 NRFX_ASSERT(status == NRFX_SUCCESS);
155
156 m_current_state = STATE_SINGLE_SAMPLING;
157 break;
158
159 case STATE_SINGLE_SAMPLING:
160 if (sampling_index++ < SAMPLING_ITERATIONS)
161 {
162 nrfx_gpiote_out_task_trigger(m_out_pins[0]);
163
164 status = nrfx_saadc_offset_calibrate(NULL);
165 NRFX_ASSERT(status == NRFX_SUCCESS);
166 NRFX_LOG_INFO("Calibration in the blocking manner finished successfully.");
167
168 NRFX_LOG_INFO("Sampling %d / %d", sampling_index, SAMPLING_ITERATIONS);
169 NRFX_EXAMPLE_LOG_PROCESS();
170
171 status = nrfx_saadc_mode_trigger();
172 NRFX_ASSERT(status == NRFX_SUCCESS);
173
174 NRFX_LOG_INFO("[CHANNEL %u] Sampled value == %d",
175 m_multiple_channels[0].channel_index, samples_buffer[0]);
176 }
177 else
178 {
179 m_current_state = STATE_MULTIPLE_CONFIG;
180 sampling_index = 0;
181 }
182 break;
183
184 case STATE_MULTIPLE_CONFIG:
185 NRFX_LOG_INFO("Multiple channels SAADC test.");
186
187 status = nrfx_saadc_channels_config(m_multiple_channels, CHANNEL_COUNT);
188 NRFX_ASSERT(status == NRFX_SUCCESS);
189
190 channels_mask = nrfx_saadc_channels_configured_get();
191 status = nrfx_saadc_simple_mode_set(channels_mask,
192 NRF_SAADC_RESOLUTION_8BIT,
193 NRF_SAADC_OVERSAMPLE_DISABLED,
194 NULL);
195 NRFX_ASSERT(status == NRFX_SUCCESS);
196
197 status = nrfx_saadc_buffer_set(samples_buffer, CHANNEL_COUNT);
198 NRFX_ASSERT(status == NRFX_SUCCESS);
199
200 m_current_state = STATE_MULTIPLE_SAMPLING;
201 break;
202
203 case STATE_MULTIPLE_SAMPLING:
204 if (sampling_index++ < SAMPLING_ITERATIONS)
205 {
206 for (i = 0; i < CHANNEL_COUNT; i++)
207 {
208 nrfx_gpiote_out_task_trigger(m_out_pins[i]);
209 }
210
211 status = nrfx_saadc_offset_calibrate(NULL);
212 NRFX_ASSERT(status == NRFX_SUCCESS);
213 NRFX_LOG_INFO("Calibration in the blocking manner finished successfully.");
214
215 NRFX_LOG_INFO("Sampling %d / %d", sampling_index, SAMPLING_ITERATIONS);
216 NRFX_EXAMPLE_LOG_PROCESS();
217
218 status = nrfx_saadc_mode_trigger();
219 NRFX_ASSERT(status == NRFX_SUCCESS);
220
221 for (i = 0; i < CHANNEL_COUNT; i++)
222 {
223 NRFX_LOG_INFO("[CHANNEL %u] Sampled value == %d",
224 m_multiple_channels[i].channel_index, samples_buffer[i]);
225 }
226 }
227 break;
228
229 default:
230 break;
231 }
232 NRFX_EXAMPLE_LOG_PROCESS();
233 }
234 }
235
236 /** @} */
237