I am trying to set a PWM signal on a GPIO pin on the Seeed Stuido XIAO BLE board.
To my understanding:
- I would need to get an instance of the PWM Driver using NRFX_PWM_INSTANCE
- Configure the PWM signal using the nrfx_pwm_config_t type.
- Initialize the PWM driver using nrfx_pwm_init
- Configure the nrf_pwm_sequence_t type for the pwm generations
- Use nrfx_pwm_simple_playback to generate the wave
I am getting a NRFX_ERROR_INVALID_STATE when I initialize the PWM driver. Does anyone know what the direct cause of this error is? To my understanding it is due to when the PWM driver is in a state and an invalid operation is used on it. However at this point of the code I am not applying an operation that is not used in the default config code that is available in the library.
The below is my code for the main script:
int main(void)
{
k_sleep(K_SECONDS(3));
int err;
int blink_status = 0;
err = dk_leds_init();
if (err) {
printk("LEDs init failed (err %d)n", err);
}
nrfx_pwm_t pwm0 = NRFX_PWM_INSTANCE(0);
nrfx_pwm_config_t pwm0_config = {
.output_pins = {
2,
0xFF,
0xFF,
0xFF,
},
.pin_inverted = {
false,
false,
false,
false,
},
.irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,
.base_clock = NRF_PWM_CLK_16MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = PWM_TOP_VALUE,
.load_mode = NRF_PWM_LOAD_COMMON,
.step_mode = NRF_PWM_STEP_AUTO, //need to set the repeat value to one so this moves every time through the memory.
.skip_gpio_cfg = false,
.skip_psel_cfg = false
};
err = nrfx_pwm_init(&pwm0,&pwm0_config,NULL,NULL);
if (err != NRFX_SUCCESS){
printk("Error Occured when initializing the pwm: %dn", err);
}
nrf_pwm_values_common_t* pulse_array = (nrf_pwm_values_common_t*)malloc(PWM_ARRAY_SIZE * sizeof(nrf_pwm_values_common_t));
if (pulse_array == NULL) {
printk("Error occured when intializing the pulse array in memoryn");
}
for (int i = 0; i < PWM_ARRAY_SIZE-1; i++){
pulse_array[i] = PWM_0;
}
pulse_array[PWM_ARRAY_SIZE-1] = 0;
nrf_pwm_sequence_t pw0_seq_config = {
.values = pulse_array, //this is suppose to be a 16 bit value but lets see how a 8 bit value impacts
.length = PWM_ARRAY_SIZE,
.repeats = 0, //only play the duty cycle in each cell once.
.end_delay = RESET_CODE_DURATION,
};
nrfx_pwm_simple_playback(&pwm0, &pw0_seq_config, 1, NRFX_PWM_FLAG_LOOP);
for (;;) {
dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
}
return 0;
}
The below is my configuration file code:
CONFIG_PWM=y
CONFIG_NRFX_PWM0=y
CONFIG_NRFX_PWM1=y
CONFIG_PWM_NRFX=y
CONFIG_COMMON_LIBC_MALLOC=y
CONFIG_DK_LIBRARY=y
Can anyone provide insight as to what the INVALID STATE Error could be referring to?
thank you in advance