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 <nrfx_pwm.h>
36
37 #define NRFX_LOG_MODULE EXAMPLE
38 #define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
39 #define NRFX_EXAMPLE_CONFIG_LOG_LEVEL 3
40 #include <nrfx_log.h>
41
42 /**
43 * @defgroup nrfx_pwm_grouped_example Grouped mode PWM example
44 * @{
45 * @ingroup nrfx_pwm_examples
46 *
47 * @brief Example showing basic functionality of nrfx_pwm driver for sequences loaded in grouped
48 * mode.
49 *
50 * @details Application initializes nrfx_pmw driver. It starts two-sequence playback on LEDs and
51 * replays this sequence @ref NUM_OF_LOOPS times. The @ref pwm_handler() is executed with
52 * relevant log message after every loop. Additionally, it changes SEQ1 each time it is
53 * called.
54 */
55
56 /** @brief Symbol specifying PWM instance to be used. */
57 #define PWM_INST_IDX 0
58
59 /**
60 * @brief Symbol specifying number of times that each duty cycle is to be repeated (after being
61 * played once) and is strictly correlated with the speed of LEDs brightness change.
62 */
63 #define VALUE_REPEATS 220UL
64
65 /**
66 * @brief Symbol specifying number of loops to be performed (one loop means to complete
67 * SEQ0 and SEQ1 @ref PLAYBACK_COUNT times).
68 */
69 #define NUM_OF_LOOPS 6UL
70
71 /**
72 * @brief Symbol specifying number of playbacks to be performed. In this example couple of
73 * playbacks might be considered as one loop.
74 */
75 #define PLAYBACK_COUNT 1UL
76
77 /**
78 * @brief Sequence default configuration in NRF_PWM_LOAD_GROUPED mode.
79 *
80 * This configuration sets up sequence with the following options:
81 * - end delay: 0 PWM periods
82 * - length: actual number of 16-bit values in the array pointed by @p _pwm_val
83 * - repeats: VALUE_REPEATS
84 *
85 * @param[in] _pwm_val pointer to an array with duty cycle values.
86 */
87 #define SEQ_CONFIG(_pwm_val) \
88 { \
89 .values.p_grouped = _pwm_val, \
90 .length = 2 * NRFX_ARRAY_SIZE(_pwm_val), \
91 .repeats = VALUE_REPEATS, \
92 .end_delay = 0 \
93 }
94
95 /** @brief Array with duty cycle values that are used to achieve blinking effect on LEDs. */
96 static nrf_pwm_values_grouped_t pwm_val0[] =
97 {
98 {100, 1000},
99 {1000, 100},
100 {100, 1000},
101 {1000, 100},
102 {100, 1000},
103 {1000, 100}
104 };
105
106 /** @brief Array with duty cycle values that are used to achieve smooth brightness change effect on LEDs. */
107 static nrf_pwm_values_grouped_t pwm_val1[] =
108 {
109 {0, 1000},
110 {100, 900},
111 {200, 800},
112 {300, 700},
113 {400, 600},
114 {500, 500},
115 {600, 400},
116 {700, 300},
117 {800, 200},
118 {900, 100},
119 {1000, 0}
120 };
121
122 /** @brief Array with duty cycle values that are used to achieve slight breath effect on LEDs. */
123 static nrf_pwm_values_grouped_t pwm_val2[] =
124 {
125 {0, 0},
126 {25, 25},
127 {50, 50},
128 {75, 75},
129 {100, 100},
130 {125, 125},
131 {150, 150},
132 {125, 125},
133 {100, 100},
134 {75, 75},
135 {50, 50},
136 {25, 25},
137 {0, 0}
138 };
139
140 /** @brief Array containing sequences to be used in this example. */
141 static nrf_pwm_sequence_t seq[] =
142 {
143 SEQ_CONFIG(pwm_val0),
144 SEQ_CONFIG(pwm_val1),
145 SEQ_CONFIG(pwm_val2)
146 };
147
148 /**
149 * @brief Function for handling PWM driver events.
150 *
151 * @param[in] event_type PWM event.
152 * @param[in] p_context General purpose parameter set during initialization of
153 * the timer. This parameter can be used to pass
154 * additional information to the handler function.
155 */
pwm_handler(nrfx_pwm_evt_type_t event_type,void * p_context)156 static void pwm_handler(nrfx_pwm_evt_type_t event_type, void * p_context)
157 {
158 nrfx_pwm_t * inst = p_context;
159 static uint32_t m_curr_loop = 1;
160 static bool m_playback_mode = false;
161
162 m_playback_mode = !m_playback_mode;
163
164 NRFX_LOG_INFO("Loops: %u / %lu", m_curr_loop, NUM_OF_LOOPS);
165
166 if (m_playback_mode)
167 {
168 nrfx_pwm_sequence_update(inst, 1, &seq[2]);
169 NRFX_LOG_INFO("SEQ1 changed to sequence number 2 from the seq array");
170 }
171 else
172 {
173 nrfx_pwm_sequence_update(inst, 1, &seq[1]);
174 NRFX_LOG_INFO("SEQ1 changed to sequence number 1 from the seq array");
175 }
176
177 if (m_curr_loop == NUM_OF_LOOPS)
178 {
179 NRFX_LOG_INFO("PWM finished");
180 nrfx_pwm_uninit(inst);
181 }
182
183 m_curr_loop++;
184 }
185
186 /**
187 * @brief Function for application main entry.
188 *
189 * @return Nothing.
190 */
main(void)191 int main(void)
192 {
193 nrfx_err_t status;
194 (void)status;
195
196 NRFX_EXAMPLE_LOG_INIT();
197
198 NRFX_LOG_INFO("Starting nrfx_pwm example for sequences loaded in grouped mode.");
199 NRFX_EXAMPLE_LOG_PROCESS();
200
201 nrfx_pwm_t pwm_instance = NRFX_PWM_INSTANCE(PWM_INST_IDX);
202 nrfx_pwm_config_t config = NRFX_PWM_DEFAULT_CONFIG(LED1_PIN, LED4_PIN, LED2_PIN, LED3_PIN);
203 config.load_mode = NRF_PWM_LOAD_GROUPED;
204 status = nrfx_pwm_init(&pwm_instance, &config, pwm_handler, &pwm_instance);
205 NRFX_ASSERT(status == NRFX_SUCCESS);
206
207 #if defined(__ZEPHYR__)
208 IRQ_DIRECT_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_PWM_INST_GET(PWM_INST_IDX)), IRQ_PRIO_LOWEST,
209 NRFX_PWM_INST_HANDLER_GET(PWM_INST_IDX), 0);
210 #endif
211
212 nrfx_pwm_complex_playback(&pwm_instance, &seq[0], &seq[1], PLAYBACK_COUNT, NRFX_PWM_FLAG_LOOP);
213
214 while (1)
215 {
216 NRFX_EXAMPLE_LOG_PROCESS();
217 }
218 }
219
220 /** @} */
221