Hello,
I use NRF52832 with SDK 17.0.2.
I run app timer and pwm driver independently ok.
I merged App timer with PWM Driver. But only PWM run, I can't make app timer app run. If I comment PWM driver so app timer run.
This my code
APP_TIMER_DEF(m_repeated_timer_id);
/**@brief Timeout handler for the repeated timer.
*/
static void repeated_timer_handler(void * p_context)
{
nrf_gpio_pin_toggle(LED_2);
}
/**@brief Function starting the internal LFCLK oscillator.
*
* @details This is needed by RTC1 which is used by the Application Timer
* (When SoftDevice is enabled the LFCLK is always running and this is not needed).
*/
static void lfclk_request(void)
{
ret_code_t err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
}
static void create_timers()
{
ret_code_t err_code;
// Create timers
err_code = app_timer_create(&m_repeated_timer_id,
APP_TIMER_MODE_REPEATED,
repeated_timer_handler);
APP_ERROR_CHECK(err_code);
}
int main(void)
{
lfclk_request();
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
pwm_init(LED_1, LED_3);
nrf_gpio_cfg_output(LED_2);
APP_ERROR_CHECK(app_timer_init());
create_timers();
int err_code = app_timer_start(m_repeated_timer_id, APP_TIMER_TICKS(200), NULL);
APP_ERROR_CHECK(err_code);
for (;;)
{
// Wait for an event.
__WFE();
// Clear the event register.
__SEV();
__WFE();
NRF_LOG_FLUSH();
}
}
and my PWM Init
static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
// This is for tracking PWM instances being used, so we can unintialize only
// the relevant ones when switching from one demo to another.
#define USED_PWM(idx) (1UL << idx)
static uint8_t m_used = 0;
static uint16_t const m_pwm_top = 255;
static uint16_t const m_pwm_step = 1;
static nrf_pwm_values_individual_t channel;
int count = 0;
static nrf_pwm_sequence_t const m_pwm_seq =
{
.values.p_individual = &channel,
.length = NRF_PWM_VALUES_LENGTH(channel),
.repeats = 0,
.end_delay = 0
};
static void pwm_handler(nrf_drv_pwm_evt_type_t event_type)
{
if (event_type == NRF_DRV_PWM_EVT_FINISHED)
{
static bool down = false;
int value = channel.channel_0;
if (down)
{
value -= m_pwm_step;
if (value == 0)
{
down = false;
}
}
else
{
value += m_pwm_step;
if (value >= m_pwm_top)
{
down = true;
}
}
channel.channel_0 = value;
channel.channel_1 = value;
nrf_delay_ms(10);
}
}
void pwm_init(int pin1, int pin2)
{
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
pin1 | NRF_DRV_PWM_PIN_INVERTED, // channel 0
pin2 | NRF_DRV_PWM_PIN_INVERTED, // channel 1
},
.irq_priority = _PRIO_APP_LOWEST,
.base_clock = NRF_PWM_CLK_1MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = m_pwm_top,
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, pwm_handler));
channel.channel_0 = 0;
channel.channel_1 = 0;
(void)nrf_drv_pwm_simple_playback(&m_pwm0, &m_pwm_seq, 1,
NRFX_PWM_FLAG_LOOP);
}
Please show me why and how to fix.
Thank you