Skip to content

Instantly share code, notes, and snippets.

@felipeLeast
Created Nov 7, 2018
Embed
What would you like to do?
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define SYS_LOG_DOMAIN "main"
#include <logging/sys_log.h>
#include <gpio.h>
#include <kernel.h>
#include <zephyr.h>
#include <nrf.h>
#include <nrf_timer.h>
#include <nrf_ppi.h>
#include <nrf_gpiote.h>
#include <nrf_common.h>
#define GPIO_FREQ 8
#define GPIOTE_CHANNEL 0
#define GPIOTE_IN_CHAN NRF_GPIOTE_INT_IN0_MASK
#define TIMER NRF_TIMER1
#define PPI_CHANNEL NRF_PPI_CHANNEL0
#define TIMER_CC_CHANNEL NRF_TIMER_CC_CHANNEL0
#define TIMER_CAPTURE_CHANNEL NRF_TIMER_TASK_CAPTURE0
void main(void)
{
struct device *dev = device_get_binding("GPIO_0");
if (!dev) {
SYS_LOG_ERR("Couldn't find gpio");
return;
}
/*
* GPIO Configure and GPIOTE Channel
*/
gpio_pin_configure(dev,
GPIO_FREQ,
GPIO_DIR_IN | GPIO_PUD_NORMAL); // Config gpio
nrf_gpiote_event_configure(GPIOTE_CHANNEL,
GPIO_FREQ,
NRF_GPIOTE_POLARITY_LOTOHI); // Configure gpiote for pin
nrf_gpiote_event_enable(GPIOTE_CHANNEL); // Enable event for gpiote
// nrf_gpiote_int_enable(GPIOTE_IN_CHAN); // Enable interruption for gpiote
/*
* Timer Channel
*/
nrf_timer_frequency_set(TIMER, NRF_TIMER_FREQ_16MHz); // Set prescale to 16MHz
nrf_timer_bit_width_set(TIMER, NRF_TIMER_BIT_WIDTH_32); // Set bit mode to 32 bits
nrf_timer_mode_set(TIMER, NRF_TIMER_MODE_COUNTER); // Set mode to counter
/*
* PPI Channel
*/
nrf_ppi_channel_enable(PPI_CHANNEL); // Enable ppi
nrf_ppi_channel_endpoint_setup(PPI_CHANNEL,
(u32_t)&NRF_GPIOTE->EVENTS_IN[NRF_GPIOTE_EVENTS_IN_0],
(u32_t)&TIMER->TASKS_COUNT); // Connect gpiote in event to timer counter
while (1) {
// Clears timer count and starts it
nrf_timer_task_trigger(TIMER, NRF_TIMER_TASK_CLEAR);
nrf_timer_task_trigger(TIMER, NRF_TIMER_TASK_START);
// Wait a second and copy the value to CC channel
k_sleep(1000);
nrf_timer_task_trigger(TIMER, TIMER_CAPTURE_CHANNEL);
// Get the value from CC channel
uint32_t timer_counter = nrf_timer_cc_read(TIMER, TIMER_CC_CHANNEL);
SYS_LOG_DBG("Channel counter: %d", timer_counter);
// Clears timer task capture flag stop and shutdown timer
TIMER->TASKS_CAPTURE[0] = 0;
nrf_timer_task_trigger(TIMER, NRF_TIMER_TASK_STOP);
nrf_timer_task_trigger(TIMER, NRF_TIMER_TASK_SHUTDOWN);
}
}
// uint32_t count = 0;
// static inline void gpio3_int_handler(struct device *port, struct gpio_callback *cb, u32_t pins)
// {
// count++;
// }
// void main(void)
// {
// struct device *dev = device_get_binding("GPIO_0");
// struct gpio_callback rx_tx_cb;
// gpio_pin_configure(dev, GPIO_FREQ, GPIO_DIR_IN | GPIO_PUD_NORMAL | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW);
// gpio_init_callback(&rx_tx_cb, gpio3_int_handler, BIT(GPIO_FREQ));
// gpio_add_callback(dev, &rx_tx_cb);
// gpio_pin_enable_callback(dev, GPIO_FREQ);
// while (1) {
// SYS_LOG_DBG("%d", count);
// k_sleep(1000);
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment