static void led_blinking_setup() { uint32_t compare_evt_addr; uint32_t gpiote_task_addr; nrf_ppi_channel_t ppi_channel; ret_code_t err_code; nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false); err_code = nrf_drv_gpiote_out_init(GPIO_OUTPUT_PIN_NUMBER, &config); APP_ERROR_CHECK(err_code); nrf_drv_timer_extended_compare(&timer, (nrf_timer_cc_channel_t)0, 200 * 1000UL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false); err_code = nrf_drv_ppi_channel_alloc(&ppi_channel); APP_ERROR_CHECK(err_code); compare_evt_addr = nrf_drv_timer_event_address_get(&timer, NRF_TIMER_EVENT_COMPARE0); gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(GPIO_OUTPUT_PIN_NUMBER); err_code = nrf_drv_ppi_channel_assign(ppi_channel, compare_evt_addr, gpiote_task_addr); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_enable(ppi_channel); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_out_task_enable(GPIO_OUTPUT_PIN_NUMBER); }
void setup_example(void) { uint32_t event; nrf_ppi_channel_t ppi_channel; uint32_t err_code; nrf_gpio_cfg_output(BSP_LED_0); err_code = nrf_drv_rtc_init(&rtc, NULL, rtc_evt_handler); APP_ERROR_CHECK(err_code); nrf_drv_rtc_tick_enable(&rtc, false); event = nrf_drv_rtc_event_address_get(&rtc, NRF_RTC_EVENT_TICK); if (!nrf_drv_gpiote_is_init()) { err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); } nrf_drv_gpiote_out_config_t pin_out_config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false); err_code = nrf_drv_gpiote_out_init(BSP_LED_0,&pin_out_config); APP_ERROR_CHECK(err_code); nrf_drv_gpiote_out_task_enable(BSP_LED_0); uint32_t gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(BSP_LED_0); err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_alloc(&ppi_channel); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(ppi_channel,event,gpiote_task_addr); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_enable(ppi_channel); APP_ERROR_CHECK(err_code); nrf_drv_rtc_enable(&rtc); }
// Application main function. int main(void) { uint32_t err_code; // set up timers APP_TIMER_INIT(0, 4, 4, false); // initlialize BLE ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); // init GPIOTE err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); // init PPI err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); // intialize UART uart_init(); // prints to serial port printf("starting...\n"); // Create the instance "PWM1" using TIMER1. APP_PWM_INSTANCE(PWM1,1); // RGB LED pins // (Common cathode) uint32_t pinR = 1; uint32_t pinG = 2; uint32_t pinB = 3; // 2-channel PWM, 200Hz app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, pinR, pinG); /* Initialize and enable PWM. */ err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback); APP_ERROR_CHECK(err_code); app_pwm_enable(&PWM1); // Create the instance "PWM2" using TIMER2. APP_PWM_INSTANCE(PWM2,2); // 1-channel PWM, 200Hz app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, pinB); /* Initialize and enable PWM. */ err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback); APP_ERROR_CHECK(err_code); app_pwm_enable(&PWM2); // Enter main loop. int dir = 1; int val = 0; // main loop: bool pwmEnabled = true; while(1) { // only if not paused if (!pausePWM) { // enable disable as needed if(!enablePWM) { if(pwmEnabled) { app_pwm_disable(&PWM1); app_pwm_disable(&PWM2); // This is required becauase app_pwm_disable() // has a bug. // See: // https://devzone.nordicsemi.com/question/41179/how-to-stop-pwm-and-set-pin-to-clear/ nrf_drv_gpiote_out_task_disable(pinR); nrf_gpio_cfg_output(pinR); nrf_gpio_pin_clear(pinR); nrf_drv_gpiote_out_task_disable(pinG); nrf_gpio_cfg_output(pinG); nrf_gpio_pin_clear(pinG); nrf_drv_gpiote_out_task_disable(pinB); nrf_gpio_cfg_output(pinB); nrf_gpio_pin_clear(pinB); pwmEnabled = false; } } else { if(!pwmEnabled) { // enable PWM nrf_drv_gpiote_out_task_enable(pinR); nrf_drv_gpiote_out_task_enable(pinG); nrf_drv_gpiote_out_task_enable(pinB); app_pwm_enable(&PWM1); app_pwm_enable(&PWM2); pwmEnabled = true; } } if(pwmEnabled) { // Set the duty cycle - keep trying until PWM is ready while (app_pwm_channel_duty_set(&PWM1, 0, val) == NRF_ERROR_BUSY); while (app_pwm_channel_duty_set(&PWM1, 1, val) == NRF_ERROR_BUSY); while (app_pwm_channel_duty_set(&PWM2, 0, val) == NRF_ERROR_BUSY); } // change direction at edges if(val > 99) { dir = -1; } else if (val < 1){ dir = 1; } // increment/decrement val += dir*5; } // delay nrf_delay_ms(delay); } }