1 /*
2  * Copyright (c) 2022 - 2024, 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_common_example Common mode PWM example
44  * @{
45  * @ingroup nrfx_pwm_examples
46  *
47  * @brief Example showing basic functionality of nrfx_pwm driver for sequence loaded in common mode.
48  *
49  * @details Application initializes nrfx_pmw driver. It plays a simple sequence on LEDs ("breath"
50  *          effect) and replays this sequence @ref NUM_OF_LOOPS times. The @ref pwm_handler() is
51  *          executed with relevant log message after every loop.
52  */
53 
54 /** @brief Symbol specifying PWM instance to be used. */
55 #define PWM_INST_IDX 0
56 
57 /**
58  * @brief Symbol specifying number of times that each duty cycle is to be repeated (after being
59  *        played once) and is strictly correlated with the "breath" effect speed.
60  */
61 #define VALUE_REPEATS 150UL
62 
63 /** @brief Symbol specifying number of loops (breaths) to be performed. */
64 #define NUM_OF_LOOPS 3UL
65 
66 /**
67  * @brief Symbol specifying number of playbacks to be performed. In this example couple of
68  *        playbacks might be considered as one loop.
69  */
70 #define PLAYBACK_COUNT 1UL
71 
72 /** @brief Array with duty cycle values. */
73 nrf_pwm_values_common_t pwm_val []=
74 {
75     0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,
76     900, 800, 700, 600, 500, 400, 300, 200, 100, 0
77 };
78 
79 /**
80  * @brief Function for handling PWM driver events.
81  *
82  * @param[in] event_type PWM event.
83  * @param[in] p_context  General purpose parameter set during initialization of
84  *                       the timer. This parameter can be used to pass
85  *                       additional information to the handler function.
86  */
pwm_handler(nrfx_pwm_evt_type_t event_type,void * p_context)87 static void pwm_handler(nrfx_pwm_evt_type_t event_type, void * p_context)
88 {
89     nrfx_pwm_t * inst = p_context;
90     static uint32_t curr_loop = 1;
91 
92     NRFX_LOG_INFO("Loops: %u / %lu", curr_loop, NUM_OF_LOOPS);
93 
94     if (curr_loop == NUM_OF_LOOPS)
95     {
96         NRFX_LOG_INFO("PWM finished");
97         nrfx_pwm_uninit(inst);
98     }
99     curr_loop++;
100 }
101 
102 /**
103  * @brief Function for application main entry.
104  *
105  * @return Nothing.
106  */
main(void)107 int main(void)
108 {
109     nrfx_err_t status;
110     (void)status;
111 
112 #if defined(__ZEPHYR__)
113     IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_PWM_INST_GET(PWM_INST_IDX)), IRQ_PRIO_LOWEST,
114                 NRFX_PWM_INST_HANDLER_GET(PWM_INST_IDX), 0, 0);
115 #endif
116 
117     NRFX_EXAMPLE_LOG_INIT();
118 
119     NRFX_LOG_INFO("Starting nrfx_pwm example for sequence loaded in common mode.");
120     NRFX_EXAMPLE_LOG_PROCESS();
121 
122     nrfx_pwm_t pwm_instance = NRFX_PWM_INSTANCE(PWM_INST_IDX);
123     nrfx_pwm_config_t config = NRFX_PWM_DEFAULT_CONFIG(LED1_PIN, LED2_PIN, LED3_PIN, LED4_PIN);
124     status = nrfx_pwm_init(&pwm_instance, &config, pwm_handler, &pwm_instance);
125     NRFX_ASSERT(status == NRFX_SUCCESS);
126 
127     nrf_pwm_sequence_t seq =
128     {
129         .values = {pwm_val},
130         .length = NRFX_ARRAY_SIZE(pwm_val),
131         .repeats = VALUE_REPEATS,
132         .end_delay = 0
133     };
134 
135     nrfx_pwm_simple_playback(&pwm_instance, &seq, PLAYBACK_COUNT, NRFX_PWM_FLAG_LOOP);
136 
137     while (1)
138     {
139         NRFX_EXAMPLE_LOG_PROCESS();
140     }
141 }
142 
143 /** @} */
144