#include <stdio.h>
#include <string.h>
#include <nrfx_pwm.h>
#include <zephyr.h>

// Use Zephyr macro to get access to the LED0 on the board
//#define OUTPUT_PIN DT_GPIO_PIN(DT_ALIAS(led0), gpios)

// Use nrfx to create a PWM instance which we will connect to the LED0 later
static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);

// Declare variables holding PWM sequence values. In this example only one channel is used 
// We start off with a counter to 50 and a top value of 100 giving a 50% duty cyle.
nrf_pwm_values_wave_form_t seq_values[] = {50, 0, 0, 100};

nrf_pwm_sequence_t const seq =
{
    .values.p_wave_form = seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 0,
    .end_delay       = 0
};


nrfx_pwm_config_t const config0 =
    {
        .output_pins =
        {
            4,                        // channel 0 now connected to LED0
            NRFX_PWM_PIN_NOT_USED,             // channel 1
            NRFX_PWM_PIN_NOT_USED,             // channel 2
            NRFX_PWM_PIN_NOT_USED,             // channel 3
        },
        .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,
        .base_clock   = NRF_PWM_CLK_1MHz,       // Here we select which prescaler to use
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 100,                    // This is ignored when in wave form mode
        .load_mode    = NRF_PWM_LOAD_WAVE_FORM,
        .step_mode    = NRF_PWM_STEP_AUTO
    };

int main(void) {

// Init PWM without error handler
nrfx_pwm_init(&m_pwm0, &config0, NULL, NULL);

// Set and start
nrfx_pwm_simple_playback(&m_pwm0, &seq, 1, NRFX_PWM_FLAG_LOOP);

while(1) 
    k_msleep(3000);

}