From 9d82bf91e3de19bd502ff1f833990d5151e9c46b Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Wed, 27 Nov 2024 11:41:19 +0100 Subject: [PATCH] main: support for more gpios and timers --- src/main.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/main.c b/src/main.c index 351c1a5..a3fdc0c 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,7 @@ #include #include +#include #include "audio_sync_timer.h" #define LOG_MODULE_NAME hci_uart @@ -369,6 +370,9 @@ static const struct gpio_dt_spec timesync_pin = GPIO_DT_SPEC_GET(TIMESYNC_GPIO, #error "No timesync gpio available!" #endif +#define ALTERNATE_TOGGLE_GPIO DT_NODELABEL(alternate_toggle) +static const struct gpio_dt_spec alternate_toggle_pin = GPIO_DT_SPEC_GET(ALTERNATE_TOGGLE_GPIO, gpios); + #define HCI_CMD_ISO_TIMESYNC (0x200) struct hci_cmd_iso_timestamp_response { @@ -468,8 +472,48 @@ static uint32_t toggle_and_get_time(void) { #endif + +#define TIME_TO_WAIT_MS 1000 +#define SYNC_TOGGLE_TIMER_INSTANCE_NUMBER 2 +static const nrfx_timer_t sync_toggle_timer_instance = + NRFX_TIMER_INSTANCE(SYNC_TOGGLE_TIMER_INSTANCE_NUMBER); + +static nrfx_timer_config_t cfg = {.frequency = NRFX_MHZ_TO_HZ(1UL), + .mode = NRF_TIMER_MODE_TIMER, + .bit_width = NRF_TIMER_BIT_WIDTH_32, + .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, + .p_context = NULL}; + +static void sync_toggle_timer_isr_handler(nrf_timer_event_t event_type, void *p_context){ + ARG_UNUSED(p_context); + if(event_type == NRF_TIMER_EVENT_COMPARE1) + { + uint32_t remainder_us = nrf_timer_cc_get(NRF_TIMER2, NRF_TIMER_CC_CHANNEL1); + gpio_pin_toggle_dt( &alternate_toggle_pin ); + + LOG_INF("HS Timer: %d", remainder_us ); + + uint32_t next_timeout_ticks = remainder_us + nrfx_timer_ms_to_ticks(&sync_toggle_timer_instance, TIME_TO_WAIT_MS); + nrfx_timer_compare(&sync_toggle_timer_instance, NRF_TIMER_CC_CHANNEL1, next_timeout_ticks, true); + } +} + int main(void) { + // timer setup + nrfx_err_t ret; + ret = nrfx_timer_init(&sync_toggle_timer_instance, &cfg, sync_toggle_timer_isr_handler); + if (ret - NRFX_ERROR_BASE_NUM) { + LOG_ERR("nrfx timer init error: %d", ret); + return -ENODEV; + } + uint32_t desired_ticks = nrfx_timer_ms_to_ticks(&sync_toggle_timer_instance, TIME_TO_WAIT_MS); + nrfx_timer_compare(&sync_toggle_timer_instance, NRF_TIMER_CC_CHANNEL1, desired_ticks, true); + IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(SYNC_TOGGLE_TIMER_INSTANCE_NUMBER)), IRQ_PRIO_LOWEST, + NRFX_TIMER_INST_HANDLER_GET(SYNC_TOGGLE_TIMER_INSTANCE_NUMBER), 0, 0); + nrfx_timer_enable(&sync_toggle_timer_instance); + + /* incoming events and data from the controller */ static K_FIFO_DEFINE(rx_queue); int err;