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_non_blocking_example Simple non-blocking SAADC example
45  * @{
46  * @ingroup nrfx_saadc_examples
47  *
48  * @brief Example showing simple functionality of nrfx_saadc driver operating in the non-blocking mode.
49  *
50  * @details Application initializes nrfx_saadc driver and starts operating in the non-blocking mode.
51  *          Program works as a simple state machine and starts in state @ref m_current_state == STATE_SINGLE_CONFIG.
52  *          In 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 non-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 non-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 non-blocking manner is performed. It triggers @ref saadc_handler()
62  *          where sampling is invoked 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 m_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 /** @brief Flag indicating that sampling on every specified channel is finished and buffer ( @ref m_samples_buffer ) is filled with samples. */
113 static bool m_saadc_ready;
114 
115 /**
116  * @brief Function for handling SAADC driver events.
117  *
118  * @param[in] p_event Pointer to an SAADC driver event.
119  */
saadc_handler(nrfx_saadc_evt_t const * p_event)120 static void saadc_handler(nrfx_saadc_evt_t const * p_event)
121 {
122     nrfx_err_t status;
123     (void)status;
124 
125     uint16_t samples_number;
126 
127     switch (p_event->type)
128     {
129         case NRFX_SAADC_EVT_DONE:
130             NRFX_LOG_INFO("SAADC event: DONE");
131 
132             samples_number = p_event->data.done.size;
133             for (uint16_t i = 0; i < samples_number; i++)
134             {
135                 NRFX_LOG_INFO("[Sample %d] value == %d", i, p_event->data.done.p_buffer[i]);
136             }
137 
138             m_saadc_ready = true;
139             break;
140 
141         case NRFX_SAADC_EVT_CALIBRATEDONE:
142             NRFX_LOG_INFO("SAADC event: CALIBRATEDONE");
143             status = nrfx_saadc_mode_trigger();
144             NRFX_ASSERT(status == NRFX_SUCCESS);
145             break;
146 
147         default:
148             break;
149     }
150 }
151 
152 /**
153  * @brief Function for application main entry.
154  *
155  * @return Nothing.
156  */
main(void)157 int main(void)
158 {
159     nrfx_err_t status;
160     (void)status;
161 
162     NRFX_EXAMPLE_LOG_INIT();
163     NRFX_LOG_INFO("Starting nrfx_saadc simple non-blocking example.");
164     NRFX_EXAMPLE_LOG_PROCESS();
165 
166     status = nrfx_saadc_init(NRFX_SAADC_DEFAULT_CONFIG_IRQ_PRIORITY);
167     NRFX_ASSERT(status == NRFX_SUCCESS);
168 
169     uint8_t i;
170     for (i = 0; i < CHANNEL_COUNT; i++)
171     {
172         gpiote_pin_toggle_task_setup(m_out_pins[i]);
173     }
174 
175 #if defined(__ZEPHYR__)
176     IRQ_DIRECT_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SAADC), IRQ_PRIO_LOWEST, nrfx_saadc_irq_handler, 0);
177 #endif
178 
179     uint32_t sampling_index = 0;
180     while (1)
181     {
182         switch (m_current_state)
183         {
184             case STATE_SINGLE_CONFIG:
185                 NRFX_LOG_INFO("Single channel SAADC test.");
186 
187                 status = nrfx_saadc_channel_config(&m_single_channel);
188                 NRFX_ASSERT(status == NRFX_SUCCESS);
189 
190                 uint32_t 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                                                     saadc_handler);
195                 NRFX_ASSERT(status == NRFX_SUCCESS);
196 
197                 status = nrfx_saadc_buffer_set(m_samples_buffer, 1);
198                 NRFX_ASSERT(status == NRFX_SUCCESS);
199 
200                 m_saadc_ready = true;
201                 m_current_state = STATE_SINGLE_SAMPLING;
202                 break;
203 
204             case STATE_SINGLE_SAMPLING:
205                 if (m_saadc_ready && sampling_index < SAMPLING_ITERATIONS)
206                 {
207                     nrfx_gpiote_out_task_trigger(m_out_pins[0]);
208 
209                     m_saadc_ready = false;
210                     status = nrfx_saadc_offset_calibrate(saadc_handler);
211                     NRFX_ASSERT(status == NRFX_SUCCESS);
212 
213                     sampling_index++;
214                 }
215                 else if (m_saadc_ready && sampling_index == SAMPLING_ITERATIONS)
216                 {
217                     m_current_state = STATE_MULTIPLE_CONFIG;
218                     sampling_index = 0;
219                 }
220                 break;
221 
222             case STATE_MULTIPLE_CONFIG:
223                 NRFX_LOG_INFO("Multiple channels SAADC test.");
224 
225                 status = nrfx_saadc_channels_config(m_multiple_channels, CHANNEL_COUNT);
226                 NRFX_ASSERT(status == NRFX_SUCCESS);
227 
228                 channels_mask = nrfx_saadc_channels_configured_get();
229                 status = nrfx_saadc_simple_mode_set(channels_mask,
230                                                     NRF_SAADC_RESOLUTION_8BIT,
231                                                     NRF_SAADC_OVERSAMPLE_DISABLED,
232                                                     saadc_handler);
233                 NRFX_ASSERT(status == NRFX_SUCCESS);
234 
235                 status = nrfx_saadc_buffer_set(m_samples_buffer, CHANNEL_COUNT);
236                 NRFX_ASSERT(status == NRFX_SUCCESS);
237 
238                 m_current_state = STATE_MULTIPLE_SAMPLING;
239                 break;
240 
241             case STATE_MULTIPLE_SAMPLING:
242                 if (m_saadc_ready && sampling_index < SAMPLING_ITERATIONS)
243                 {
244                     for (i = 0; i < CHANNEL_COUNT; i++)
245                     {
246                         nrfx_gpiote_out_task_trigger(m_out_pins[i]);
247                     }
248 
249                     m_saadc_ready = false;
250                     status = nrfx_saadc_offset_calibrate(saadc_handler);
251                     NRFX_ASSERT(status == NRFX_SUCCESS);
252 
253                     sampling_index++;
254                 }
255                 break;
256 
257             default:
258                 break;
259         }
260         NRFX_EXAMPLE_LOG_PROCESS();
261     }
262 }
263 
264 /** @} */
265