This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PWM inverted when not using Segger

I'm rolling my own very simple PWM implementation (I'm not using the PWM example code). My code is below:

#define GPIOTE_CHANNEL_NUMBER 0
#define PCLK16M             16000000
#define FSK_FREQ            17000
#define FSK_CC0             (PCLK16M / FSK_FREQ)
#define FSK_PULSE_PERIOD_US 15
#define FSK_CC1             ((PCLK16M / 1000000) * FSK_PULSE_PERIOD_US)

nrf_gpiote_task_config(GPIOTE_CHANNEL_NUMBER, 25, \
         NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_HIGH);

uint32_t ppi_ch_num = 0;
sd_ppi_channel_assign(ppi_ch_num, &NRF_TIMER1->EVENTS_COMPARE[1], &NRF_GPIOTE->TASKS_OUT[GPIOTE_CHANNEL_NUMBER]);
sd_ppi_channel_assign(ppi_ch_num + 1, &NRF_TIMER1->EVENTS_COMPARE[0], &NRF_GPIOTE->TASKS_OUT[GPIOTE_CHANNEL_NUMBER]);
sd_ppi_channel_enable_set(1 << ppi_ch_num | 1 << (ppi_ch_num + 1));

NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer;
NRF_TIMER1->PRESCALER   = 0;
NRF_TIMER1->BITMODE     = TIMER_BITMODE_BITMODE_16Bit;
NRF_TIMER1->CC[0]       = FSK_CC0; // 941
NRF_TIMER1->CC[1]       = FSK_CC1; // 240
NRF_TIMER1->SHORTS      = TIMER_SHORTS_COMPARE0_CLEAR_Enabled;
NRF_TIMER1->TASKS_START = 1;

This works fine when I have the SEGGER debugger attached (either in Debug or Run mode). However, as soon as I detach the debugger and reset, the PWM signal randomly inverts itself. I'm using the SoftDevice, BTW. I've heard of PWM signals inverting themselves with a SoftDevice, but I thought that was specific to the PWM example code / changing the pulse width while the timer is running.

Has anyone else run into this? Any ideas?

Related