Hi,
I want to implement simple module:
- gpiote event on pin change (around 6.7MHz signal)
- Route gpiote event through PPI to increment counter
- Toggle GPIO pin on Compare event
Im using this code (modified example of 8mhz-pwm), but it doesn't seem to work properly. Is there any way to capture such high frequency signal?
nrf_gpio_range_cfg_output(LED_0, LED_1);
nrf_gpio_pin_set(LED_0);
NRF_CLOCK->TASKS_HFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
{
}
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
// Workaround for PAN item 11, nRF51822-PAN v2.0.pdf. Not needed on second revision chips.
NRF_POWER->TASKS_CONSTLAT = 1;
nrf_gpio_cfg_sense_input(BUTTON_0, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
NRF_GPIOTE->CONFIG[0] = GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos |
GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos |
LED_1 << GPIOTE_CONFIG_PSEL_Pos |
GPIOTE_CONFIG_OUTINIT_Low << GPIOTE_CONFIG_OUTINIT_Pos;
NRF_GPIOTE->CONFIG[1] = GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos |
GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos |
BUTTON_0 << GPIOTE_CONFIG_PSEL_Pos;
NRF_TIMER1->PRESCALER = 0;
// Adjust the output frequency by adjusting the CC.
// Due to PAN item 33, you can't have this at 1 for first revision chips, and will hence be limited to 4 MHz.
NRF_TIMER1->CC[0] = 1;
NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos;
NRF_TIMER1->TASKS_START = 1;
NRF_TIMER2->MODE = TIMER_MODE_MODE_Counter;
NRF_TIMER2->TASKS_CLEAR = 1;
NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
NRF_TIMER2->CC[0] = 10;
NRF_TIMER2->TASKS_START = 1;
NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
// NRF_PPI->CH[0].EEP = (uint32_t) &NRF_TIMER1->EVENTS_COMPARE[0];
NRF_PPI->CH[0].EEP = (uint32_t) &NRF_GPIOTE->EVENTS_IN[1];
NRF_PPI->CH[0].TEP = (uint32_t) &NRF_TIMER2->TASKS_COUNT;
NRF_PPI->CH[1].EEP = (uint32_t) &NRF_TIMER2->EVENTS_COMPARE[0];
NRF_PPI->CH[1].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
NRF_PPI->CHENSET = PPI_CHENSET_CH0_Enabled << PPI_CHENSET_CH0_Pos;
NRF_PPI->CHENSET = PPI_CHENSET_CH1_Enabled << PPI_CHENSET_CH1_Pos;
while (true)
{
NRF_TIMER2->TASKS_CAPTURE[2] = 1;
NRF_LOG_PRINTF("cc[2] = %d\n", NRF_TIMER2->CC[2]);
nrf_delay_ms(200);
}