/** * @brief Function for application main entry. */ int main(void) { ret_code_t err_code; err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(err_code); nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; err_code = nrf_drv_timer_init(&timer, &timer_cfg, timer_dummy_handler); APP_ERROR_CHECK(err_code); #ifdef NRF51 //Workaround for PAN-73. *(uint32_t *)0x40008C0C = 1; #endif // Setup PPI channel with event from TIMER compare and task GPIOTE pin toggle. led_blinking_setup(); // Enable timer nrf_drv_timer_enable(&timer); while (true) { // Do Nothing - GPIO can be toggled without software intervention. } }
void saadc_sampling_event_init(void) { ret_code_t err_code; err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); nrf_drv_timer_config_t timer_config = NRF_DRV_TIMER_DEFAULT_CONFIG; timer_config.frequency = NRF_TIMER_FREQ_31250Hz; err_code = nrf_drv_timer_init(&m_timer, &timer_config, timer_handler); APP_ERROR_CHECK(err_code); /* setup m_timer for compare event */ uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer,SAADC_SAMPLE_RATE); nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false); nrf_drv_timer_enable(&m_timer); uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0); uint32_t saadc_sample_event_addr = nrf_drv_saadc_sample_task_get(); /* setup ppi channel so that timer compare event is triggering sample task in SAADC */ err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_event_addr); APP_ERROR_CHECK(err_code); }
/** @brief Function for initializing the PPI peripheral. */ static void ppi_init(void) { uint32_t err_code = NRF_SUCCESS; err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); // Configure 1st available PPI channel to stop TIMER0 counter on TIMER1 COMPARE[0] match, which is every even number of seconds. err_code = nrf_drv_ppi_channel_alloc(&ppi_channel1); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(ppi_channel1, nrf_drv_timer_event_address_get(&timer1, NRF_TIMER_EVENT_COMPARE0), nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_STOP)); APP_ERROR_CHECK(err_code); // Configure 2nd available PPI channel to start timer0 counter at TIMER2 COMPARE[0] match, which is every odd number of seconds. err_code = nrf_drv_ppi_channel_alloc(&ppi_channel2); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(ppi_channel2, nrf_drv_timer_event_address_get(&timer2, NRF_TIMER_EVENT_COMPARE0), nrf_drv_timer_task_address_get(&timer0, NRF_TIMER_TASK_START)); APP_ERROR_CHECK(err_code); // Enable both configured PPI channels err_code = nrf_drv_ppi_channel_enable(ppi_channel1); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_enable(ppi_channel2); APP_ERROR_CHECK(err_code); }
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); nrf_gpiote_task_config(0, BSP_LED_0, NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW); 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,(uint32_t)&NRF_GPIOTE->TASKS_OUT[0]); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_enable(ppi_channel); APP_ERROR_CHECK(err_code); nrf_drv_rtc_enable(&rtc); }
/** * @brief Function for initializing and enabling PPI channels. * * @retval NRF_ERROR_INTERNAL If there were error initializing or enabling PPI channels. * @retval NRF_SUCCESS If PPI channels were initialized and enabled successfully. */ static ret_code_t ppi_init(void) { ret_code_t err_code; uint8_t i; err_code = nrf_drv_ppi_init(); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED)) { return NRF_ERROR_INTERNAL; } for(i = 0; i < PPI_REQUIRED_CHANNELS ; i++) { err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channels[i]); if (NRF_SUCCESS != err_code) { return NRF_ERROR_INTERNAL; } } err_code = nrf_drv_ppi_channel_assign(m_ppi_channels[0], nrf_drv_comp_event_address_get(NRF_COMP_EVENT_CROSS), nrf_drv_timer_task_address_get(&m_timer0, NRF_TIMER_TASK_COUNT)); if (NRF_SUCCESS != err_code) { return NRF_ERROR_INTERNAL; } err_code = nrf_drv_ppi_channel_assign(m_ppi_channels[1], nrf_drv_timer_event_address_get(&m_timer0, NRF_TIMER_EVENT_COMPARE0), nrf_drv_timer_task_address_get(&m_timer1, NRF_TIMER_TASK_CAPTURE1)); if (NRF_SUCCESS != err_code) { return NRF_ERROR_INTERNAL; } err_code = nrf_drv_ppi_channel_fork_assign(m_ppi_channels[1], nrf_drv_comp_task_address_get(NRF_COMP_TASK_STOP)); if (NRF_SUCCESS != err_code) { return NRF_ERROR_INTERNAL; } err_code = nrf_drv_ppi_channel_assign(m_ppi_channels[2], nrf_drv_comp_event_address_get(NRF_COMP_EVENT_READY), nrf_drv_timer_task_address_get(&m_timer0, NRF_TIMER_TASK_CLEAR)); if (NRF_SUCCESS != err_code) { return NRF_ERROR_INTERNAL; } err_code = nrf_drv_ppi_channel_fork_assign(m_ppi_channels[2], nrf_drv_timer_task_address_get(&m_timer1, NRF_TIMER_TASK_CLEAR)); if (NRF_SUCCESS != err_code) { return NRF_ERROR_INTERNAL; } for(i = 0; i < PPI_REQUIRED_CHANNELS ; i++) { err_code = nrf_drv_ppi_channel_enable(m_ppi_channels[i]); if (NRF_SUCCESS != err_code) { return NRF_ERROR_INTERNAL; } } return NRF_SUCCESS; }
int main(void) { uint32_t err_code; //Initialize GPIO nrf_gpiote_init(); pin_output_init(); err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); // Initialize PWM pwm_init(); // Initialize timers_init(); ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); //Starts advertising advertising_timer_init(); advertising_start(); //Initialize shields twi_motordriver_init(); twi_rfid_init(); // Initialize the IR lib. Must be done after initializing the SoftDevice ir_lib_init(IR_OUTPUT_PIN); err_code = ir_ppi_init(); APP_ERROR_CHECK(err_code); //Feedback, notifying the user that the DK is ready set_rgb_color(0); playNote(1607); nrf_delay_ms(30); playNote(1516); nrf_delay_ms(30); playNote(1431); // Enter main loop. for (;;) { power_manage(); } }
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); } }