For simplicity, I extracted the Demo 1 routine from the "pwm_driver" example which has 5 demos 'cause I thought it is simpler.
The code is shown below:
#include <stdbool.h>
#include <stdint.h>
#include "boards.h"
#include "nrf_drv_pwm.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "boards.h"
#include "bsp.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
static uint16_t const m_demo1_top = 10000;
static uint16_t const m_demo1_step = 50;
static uint8_t m_demo1_phase;
static nrf_pwm_values_individual_t m_demo1_seq_values;
static nrf_pwm_sequence_t const m_demo1_seq =
{
.values.p_individual = &m_demo1_seq_values,
.length = NRF_PWM_VALUES_LENGTH(m_demo1_seq_values),
.repeats = 0,
.end_delay = 0
};
static void demo1_handler( nrf_drv_pwm_evt_type_t event_type )
{
if( event_type == NRF_DRV_PWM_EVT_FINISHED )
{
uint8_t channel = m_demo1_phase >> 1;
bool down = m_demo1_phase & 1;
bool next_phase = false;
uint16_t * p_channels = (uint16_t*)&m_demo1_seq_values;
uint16_t value = p_channels[channel];
if(down)
{
value -= m_demo1_step;
if( value == 0 )
{
next_phase = true;
}
}else
{
value += m_demo1_step;
if( value >= m_demo1_top )
{
next_phase = true;
}
}
p_channels[channel] = value;
if (next_phase)
{
if (++m_demo1_phase >= 2 * NRF_PWM_CHANNEL_COUNT)
{
m_demo1_phase = 0;
}
}
}
}
/**
* @brief Function for application main entry.
*/
int main(void)
{
uint32_t err_code;
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // channel 0
BSP_LED_1 | NRF_DRV_PWM_PIN_INVERTED, // channel 1
BSP_LED_3 | NRF_DRV_PWM_PIN_INVERTED, // channel 2
BSP_LED_2 | NRF_DRV_PWM_PIN_INVERTED // channel 3
},
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
.base_clock = NRF_PWM_CLK_1MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 10000,
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
err_code = nrf_drv_pwm_init(&m_pwm0, &config0, demo1_handler);
APP_ERROR_CHECK(err_code);
m_demo1_seq_values.channel_0 = 0;
m_demo1_seq_values.channel_1 = 0;
m_demo1_seq_values.channel_2 = 0;
m_demo1_seq_values.channel_3 = 0;
m_demo1_phase = 0;
(void)nrf_drv_pwm_simple_playback(&m_pwm0, &m_demo1_seq, 1,
NRF_DRV_PWM_FLAG_LOOP);
while (true)
{
// Wait for an event.
__WFE();
// Clear the event register.
__SEV();
__WFE();
NRF_LOG_FLUSH();
}
}
Can you please explain in detail what is going on inside the demo1_handler?
I can't seem to understand.
Thank you.