Hi,
I would like to control gpio pins in micro seconds with timer, I know that it is very difficult to implement with only timer owing to soft device (interrupt handing), so need maybe to use PPI driver... is it right ?
I update my code, it can work well after setting build optimization option to balanced in compiler, but after pushing reset key, it showed sometimes strange behavior(shaking of gpio timing), if possible to check the following codes, Could you please kindly guide the solution ?
thankful for your support in advance,
thanks.
void tcs_1khz_timer_event_handler_us(nrf_timer_event_t event_type, void*
p_context)//love_1212 1 khz(1ms) t1/t2/t3 : 25 us
{
switch(event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
tcs_on_count++;
if(tcs_on_count == 40)//667 133
{
stim_con_check = 1;
tcs_on_count = 0;
}
else
stim_con_check = 0;
if(tcs_on_count == 1)//25 us
nrf_drv_gpiote_out_set(GPIO_OUTPUT_PIN_NUMBER_1);
else if(tcs_on_count == 3)//25 us
nrf_drv_gpiote_out_clear(GPIO_OUTPUT_PIN_NUMBER_1);
else if(tcs_on_count == 4)//25 us
nrf_drv_gpiote_out_set(GPIO_OUTPUT_PIN_NUMBER_2);
else if(tcs_on_count == 9)
nrf_drv_gpiote_out_clear(GPIO_OUTPUT_PIN_NUMBER_2);
break;
default:
//Do nothing.
break;
}
}
void Hal_Tcs_Timer_Start(void)//love_1120
{
nrf_drv_timer_enable(&TIMER_1);
}
void Hal_Tcs_Timer_Stop(void)//love_1120
{
nrf_drv_timer_disable(&TIMER_1);
}
void Hal_Tcs_1Khz_Timer_Init(void)
{
uint32_t time_in_us = 5;//love_1212 for 1 khz 25;//16666; //Time(in miliseconds)
between consecutive compare events.
uint32_t time_ticks;
uint32_t err_code = NRF_SUCCESS;
tcs_on_count = 0;//love_1120
//Configure all leds on board.
// nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
err_code = nrf_drv_timer_init(&TIMER_1, NULL, tcs_1khz_timer_event_handler_us);
APP_ERROR_CHECK(err_code);
time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_1, time_in_us);//love_1115
nrf_drv_timer_extended_compare(
&TIMER_1, NRF_TIMER_CC_CHANNEL0, time_ticks,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
}
void timer_dummy_handler(nrf_timer_event_t event_type, void * p_context){}
static void led_blinking_setup()
{
uint32_t compare_evt_addr;
uint32_t gpiote_task_addr_1;
uint32_t gpiote_task_addr_2;//love_1213
uint32_t time_ticks;//love_1214
nrf_ppi_channel_t ppi_channel_1;
nrf_ppi_channel_t ppi_channel_2;
ret_code_t err_code;
nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false);
// nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_SIMPLE(false);//love_1218
err_code = nrf_drv_gpiote_out_init(GPIO_OUTPUT_PIN_NUMBER_1, &config);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_gpiote_out_init(GPIO_OUTPUT_PIN_NUMBER_2, &config);
APP_ERROR_CHECK(err_code);
time_ticks = nrf_drv_timer_us_to_ticks(&ppi_timer, 5);//love_1214
nrf_drv_timer_extended_compare(&ppi_timer, (nrf_timer_cc_channel_t)0, time_ticks,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);//400 = 25 us, For 1khz,
// nrf_drv_timer_extended_compare(&ppi_timer, (nrf_timer_cc_channel_t)0, 64000,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);//1 ms = 16000
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_2);//love_1213
APP_ERROR_CHECK(err_code);
compare_evt_addr = nrf_drv_timer_event_address_get(&ppi_timer, NRF_TIMER_EVENT_COMPARE0);
gpiote_task_addr_1 = nrf_drv_gpiote_out_task_addr_get(GPIO_OUTPUT_PIN_NUMBER_1);
gpiote_task_addr_2 = nrf_drv_gpiote_out_task_addr_get(GPIO_OUTPUT_PIN_NUMBER_2);
err_code = nrf_drv_ppi_channel_assign(ppi_channel_1, compare_evt_addr,
gpiote_task_addr_1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_enable(ppi_channel_1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_assign(ppi_channel_2, compare_evt_addr,
gpiote_task_addr_2);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_enable(ppi_channel_2);
APP_ERROR_CHECK(err_code);
// nrf_drv_gpiote_out_task_enable(GPIO_OUTPUT_PIN_NUMBER_1);
// nrf_drv_gpiote_out_task_enable(GPIO_OUTPUT_PIN_NUMBER_2);
}
void Hal_Tcs_1khz_ppi_init(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(&ppi_timer, &timer_cfg, timer_dummy_handler);
APP_ERROR_CHECK(err_code);
//
// // Setup PPI channel with event from TIMER compare and task GPIOTE pin toggle.
led_blinking_setup();
//
// Enable timer
// nrf_drv_timer_enable(&ppi_timer);
}