From 1b0ec6b1bd3135209451fab7909130a7b12c23f8 Mon Sep 17 00:00:00 2001 From: Hardy Griech Date: Fri, 22 Sep 2023 18:26:36 +0200 Subject: [PATCH 1/5] make it compile also with old Nordic SDK --- src/portable/nordic/nrf5x/dcd_nrf5x.c | 71 +++++++++++++++++++++------ 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 80a285ef0..e831a09de 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -40,7 +40,6 @@ #include "nrf.h" #include "nrf_clock.h" -#include "nrf_power.h" #include "nrfx_usbd_errata.h" #ifdef __GNUC__ @@ -57,6 +56,13 @@ #include "mcu/mcu.h" #endif +#if 1000*MDK_MAJOR_VERSION + MDK_MINOR_VERSION <= 8040 +// Nordic actually has generated a mess here: nrfx==1.9.0 has MDK 8.40.3 while nrfx==2.0.0 has MDK 8.29.0 +// Unfortunately there are API differences between nrfx<2.0.0 and nrfx>=2.0.0. Hoping that everyone uses +// nRF Connect SDK keeps the libraries up to date. +#define OLD_NORDIC_SDK +#endif + /*------------------------------------------------------------------*/ /* MACRO TYPEDEF CONSTANT ENUM *------------------------------------------------------------------*/ @@ -110,21 +116,6 @@ static struct /* Control / Bulk / Interrupt (CBI) Transfer *------------------------------------------------------------------*/ -// NVIC_GetEnableIRQ is only available in CMSIS v5 -#ifndef NVIC_GetEnableIRQ -static inline uint32_t NVIC_GetEnableIRQ(IRQn_Type IRQn) -{ - if ((int32_t)(IRQn) >= 0) - { - return((uint32_t)(((NVIC->ISER[(((uint32_t)(int32_t)IRQn) >> 5UL)] & (1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL))) != 0UL) ? 1UL : 0UL)); - } - else - { - return(0U); - } -} -#endif - // check if we are in ISR TU_ATTR_ALWAYS_INLINE static inline bool is_in_isr(void) { @@ -253,13 +244,27 @@ void dcd_init (uint8_t rhport) void dcd_int_enable(uint8_t rhport) { (void) rhport; +#ifdef SOFTDEVICE_PRESENT + if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) + { + NVIC_EnableIRQ(USBD_IRQn); + } +#else NVIC_EnableIRQ(USBD_IRQn); +#endif } void dcd_int_disable(uint8_t rhport) { (void) rhport; +#ifdef SOFTDEVICE_PRESENT + if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) + { + NVIC_DisableIRQ(USBD_IRQn); + } +#else NVIC_DisableIRQ(USBD_IRQn); +#endif } void dcd_set_address (uint8_t rhport, uint8_t dev_addr) @@ -916,7 +921,11 @@ static bool hfclk_running(void) } #endif +#ifdef OLD_NORDIC_SDK + return nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY); +#else return nrf_clock_hf_is_running(NRF_CLOCK, NRF_CLOCK_HFCLK_HIGH_ACCURACY); +#endif } static void hfclk_enable(void) @@ -937,9 +946,14 @@ static void hfclk_enable(void) } #endif +#ifdef OLD_NORDIC_SDK + nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED); + nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART); +#else nrf_clock_event_clear(NRF_CLOCK, NRF_CLOCK_EVENT_HFCLKSTARTED); nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTART); #endif +#endif } static void hfclk_disable(void) @@ -957,8 +971,12 @@ static void hfclk_disable(void) } #endif +#ifdef OLD_NORDIC_SDK + nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP); +#else nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTOP); #endif +#endif } // Power & Clock Peripheral on nRF5x to manage USB @@ -1100,13 +1118,27 @@ void tusb_hal_nrf_power_event (uint32_t event) NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk; // Enable interrupt, priorities should be set by application +#ifdef SOFTDEVICE_PRESENT + if (sd_nvic_ClearPendingIRQ(USBD_IRQn) != NRF_SUCCESS) + { + NVIC_ClearPendingIRQ(USBD_IRQn); + } +#else NVIC_ClearPendingIRQ(USBD_IRQn); +#endif // Don't enable USBD interrupt yet, if dcd_init() did not finish yet // Interrupt will be enabled by tud_init(), when USB stack is ready // to handle interrupts. if (tud_inited()) { +#ifdef SOFTDEVICE_PRESENT + if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) + { + NVIC_EnableIRQ(USBD_IRQn); + } +#else NVIC_EnableIRQ(USBD_IRQn); +#endif } // Wait for HFCLK @@ -1127,7 +1159,14 @@ void tusb_hal_nrf_power_event (uint32_t event) __ISB(); __DSB(); // for sync // Disable Interrupt +#ifdef SOFTDEVICE_PRESENT + if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) + { + NVIC_DisableIRQ(USBD_IRQn); + } +#else NVIC_DisableIRQ(USBD_IRQn); +#endif // disable all interrupt NRF_USBD->INTENCLR = NRF_USBD->INTEN; From 2567fe3fd8b69e896b1d29de36d295ee007e5c91 Mon Sep 17 00:00:00 2001 From: Hardy Griech Date: Fri, 22 Sep 2023 18:39:00 +0200 Subject: [PATCH 2/5] softdevice nvic calls only if old SDK --- src/portable/nordic/nrf5x/dcd_nrf5x.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index e831a09de..0e645ec46 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -244,7 +244,7 @@ void dcd_init (uint8_t rhport) void dcd_int_enable(uint8_t rhport) { (void) rhport; -#ifdef SOFTDEVICE_PRESENT +#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_EnableIRQ(USBD_IRQn); @@ -257,7 +257,7 @@ void dcd_int_enable(uint8_t rhport) void dcd_int_disable(uint8_t rhport) { (void) rhport; -#ifdef SOFTDEVICE_PRESENT +#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_DisableIRQ(USBD_IRQn); @@ -1118,7 +1118,7 @@ void tusb_hal_nrf_power_event (uint32_t event) NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk; // Enable interrupt, priorities should be set by application -#ifdef SOFTDEVICE_PRESENT +#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) if (sd_nvic_ClearPendingIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_ClearPendingIRQ(USBD_IRQn); @@ -1131,7 +1131,7 @@ void tusb_hal_nrf_power_event (uint32_t event) // to handle interrupts. if (tud_inited()) { -#ifdef SOFTDEVICE_PRESENT +#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_EnableIRQ(USBD_IRQn); @@ -1159,7 +1159,7 @@ void tusb_hal_nrf_power_event (uint32_t event) __ISB(); __DSB(); // for sync // Disable Interrupt -#ifdef SOFTDEVICE_PRESENT +#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_DisableIRQ(USBD_IRQn); From c535a4d42be742f235126fa679bf65a6751360cc Mon Sep 17 00:00:00 2001 From: Hardy Griech Date: Sat, 25 Nov 2023 14:49:49 +0100 Subject: [PATCH 3/5] catch all nrfx versions with an old API --- src/portable/nordic/nrf5x/dcd_nrf5x.c | 40 ++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 0e645ec46..f5eea1310 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -56,11 +56,25 @@ #include "mcu/mcu.h" #endif -#if 1000*MDK_MAJOR_VERSION + MDK_MINOR_VERSION <= 8040 -// Nordic actually has generated a mess here: nrfx==1.9.0 has MDK 8.40.3 while nrfx==2.0.0 has MDK 8.29.0 -// Unfortunately there are API differences between nrfx<2.0.0 and nrfx>=2.0.0. Hoping that everyone uses -// nRF Connect SDK keeps the libraries up to date. -#define OLD_NORDIC_SDK +// Unfortunately there are API differences between nrfx<2.0.0 and nrfx>=2.0.0 +// Nordic actually has generated a mess here: nrfx==1.9.0 has MDK 8.40.3 while nrfx==2.0.0 has MDK 8.29.0. +// See the below statement to catch all nrfx versions with an old API. +#define _MDK_VERSION 10000*MDK_MAJOR_VERSION + 100*MDK_MINOR_VERSION + MDK_MICRO_VERSION +#if _MDK_VERSION <= 82701 + // nrfx <= 1.8.1 + #define NORDIC_SDK_OLD_API +#elif _MDK_VERSION == 83201 + // nrfx 1.8.2 / 1.8.4 + #define NORDIC_SDK_OLD_API +#elif _MDK_VERSION == 83203 + // nrfx 1.8.5 + #define NORDIC_SDK_OLD_API +#elif _MDK_VERSION == 83500 + // nrfx 1.8.6 + #define NORDIC_SDK_OLD_API +#elif _MDK_VERSION == 84003 + // nrfx 1.9.0 + #define NORDIC_SDK_OLD_API #endif /*------------------------------------------------------------------*/ @@ -244,7 +258,7 @@ void dcd_init (uint8_t rhport) void dcd_int_enable(uint8_t rhport) { (void) rhport; -#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) +#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_EnableIRQ(USBD_IRQn); @@ -257,7 +271,7 @@ void dcd_int_enable(uint8_t rhport) void dcd_int_disable(uint8_t rhport) { (void) rhport; -#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) +#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_DisableIRQ(USBD_IRQn); @@ -921,7 +935,7 @@ static bool hfclk_running(void) } #endif -#ifdef OLD_NORDIC_SDK +#ifdef NORDIC_SDK_OLD_API return nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY); #else return nrf_clock_hf_is_running(NRF_CLOCK, NRF_CLOCK_HFCLK_HIGH_ACCURACY); @@ -946,7 +960,7 @@ static void hfclk_enable(void) } #endif -#ifdef OLD_NORDIC_SDK +#ifdef NORDIC_SDK_OLD_API nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED); nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART); #else @@ -971,7 +985,7 @@ static void hfclk_disable(void) } #endif -#ifdef OLD_NORDIC_SDK +#ifdef NORDIC_SDK_OLD_API nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP); #else nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTOP); @@ -1118,7 +1132,7 @@ void tusb_hal_nrf_power_event (uint32_t event) NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk; // Enable interrupt, priorities should be set by application -#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) +#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) if (sd_nvic_ClearPendingIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_ClearPendingIRQ(USBD_IRQn); @@ -1131,7 +1145,7 @@ void tusb_hal_nrf_power_event (uint32_t event) // to handle interrupts. if (tud_inited()) { -#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) +#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_EnableIRQ(USBD_IRQn); @@ -1159,7 +1173,7 @@ void tusb_hal_nrf_power_event (uint32_t event) __ISB(); __DSB(); // for sync // Disable Interrupt -#if defined(SOFTDEVICE_PRESENT) && defined(OLD_NORDIC_SDK) +#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) { NVIC_DisableIRQ(USBD_IRQn); From 37a05e02647a188f562e763ab24cdbd9f48cf434 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 16 Apr 2024 11:23:38 +0700 Subject: [PATCH 4/5] revert nvic changes --- src/portable/nordic/nrf5x/dcd_nrf5x.c | 36 +-------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index f5eea1310..5f2de6446 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -258,27 +258,13 @@ void dcd_init (uint8_t rhport) void dcd_int_enable(uint8_t rhport) { (void) rhport; -#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) - if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) - { - NVIC_EnableIRQ(USBD_IRQn); - } -#else NVIC_EnableIRQ(USBD_IRQn); -#endif } void dcd_int_disable(uint8_t rhport) { (void) rhport; -#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) - if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) - { - NVIC_DisableIRQ(USBD_IRQn); - } -#else NVIC_DisableIRQ(USBD_IRQn); -#endif } void dcd_set_address (uint8_t rhport, uint8_t dev_addr) @@ -1132,27 +1118,14 @@ void tusb_hal_nrf_power_event (uint32_t event) NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk; // Enable interrupt, priorities should be set by application -#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) - if (sd_nvic_ClearPendingIRQ(USBD_IRQn) != NRF_SUCCESS) - { - NVIC_ClearPendingIRQ(USBD_IRQn); - } -#else NVIC_ClearPendingIRQ(USBD_IRQn); -#endif + // Don't enable USBD interrupt yet, if dcd_init() did not finish yet // Interrupt will be enabled by tud_init(), when USB stack is ready // to handle interrupts. if (tud_inited()) { -#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) - if (sd_nvic_EnableIRQ(USBD_IRQn) != NRF_SUCCESS) - { - NVIC_EnableIRQ(USBD_IRQn); - } -#else NVIC_EnableIRQ(USBD_IRQn); -#endif } // Wait for HFCLK @@ -1173,14 +1146,7 @@ void tusb_hal_nrf_power_event (uint32_t event) __ISB(); __DSB(); // for sync // Disable Interrupt -#if defined(SOFTDEVICE_PRESENT) && defined(NORDIC_SDK_OLD_API) - if (sd_nvic_DisableIRQ(USBD_IRQn) != NRF_SUCCESS) - { - NVIC_DisableIRQ(USBD_IRQn); - } -#else NVIC_DisableIRQ(USBD_IRQn); -#endif // disable all interrupt NRF_USBD->INTENCLR = NRF_USBD->INTEN; From d82ee79bd0a834a83694c91ce9cff67575283846 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 16 Apr 2024 11:55:03 +0700 Subject: [PATCH 5/5] - in case of mdk conflict to use nrfx v2 - allow to force nrfx version with CFG_TUD_NRF_NRFX_VERSION=1 --- src/portable/nordic/nrf5x/dcd_nrf5x.c | 43 +++++++++++++-------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 5f2de6446..df5f275db 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -56,25 +56,24 @@ #include "mcu/mcu.h" #endif -// Unfortunately there are API differences between nrfx<2.0.0 and nrfx>=2.0.0 -// Nordic actually has generated a mess here: nrfx==1.9.0 has MDK 8.40.3 while nrfx==2.0.0 has MDK 8.29.0. -// See the below statement to catch all nrfx versions with an old API. -#define _MDK_VERSION 10000*MDK_MAJOR_VERSION + 100*MDK_MINOR_VERSION + MDK_MICRO_VERSION -#if _MDK_VERSION <= 82701 - // nrfx <= 1.8.1 - #define NORDIC_SDK_OLD_API -#elif _MDK_VERSION == 83201 - // nrfx 1.8.2 / 1.8.4 - #define NORDIC_SDK_OLD_API -#elif _MDK_VERSION == 83203 - // nrfx 1.8.5 - #define NORDIC_SDK_OLD_API -#elif _MDK_VERSION == 83500 - // nrfx 1.8.6 - #define NORDIC_SDK_OLD_API -#elif _MDK_VERSION == 84003 - // nrfx 1.9.0 - #define NORDIC_SDK_OLD_API +/* Try to detect nrfx version if not configured with CFG_TUD_NRF_NRFX_VERSION + * nrfx v1 and v2 are concurrently developed. There is no NRFX_VERSION only MDK VERSION which is as follows: + * - v2.6.0: 8.44.1, v2.5.0: 8.40.2, v2.4.0: 8.37.0, v2.3.0: 8.35.0, v2.2.0: 8.32.1, v2.1.0: 8.30.2, v2.0.0: 8.29.0 + * - v1.9.0: 8.40.3, v1.8.6: 8.35.0 (conflict with v2.3.0), v1.8.5: 8.32.3, v1.8.4: 8.32.1 (conflict with v2.2.0), + * v1.8.2: 8.32.1 (conflict with v2.2.0), v1.8.1: 8.27.1 + * Therefore the check for v1 would be: + * - MDK < 8.29.0 (v2.0), MDK == 8.32.3, 8.40.3 + * - in case of conflict User of those version must upgrade to other 1.x version or set CFG_TUD_NRF_NRFX_VERSION +*/ +#ifndef CFG_TUD_NRF_NRFX_VERSION + #define _MDK_VERSION (10000*MDK_MAJOR_VERSION + 100*MDK_MINOR_VERSION + MDK_MICRO_VERSION) + + #if _MDK_VERSION < 82900 || _MDK_VERSION == 83203 || _MDK_VERSION == 84003 + // nrfx <= 1.8.1, or 1.8.5 or 1.9.0 + #define CFG_TUD_NRF_NRFX_VERSION 1 + #else + #define CFG_TUD_NRF_NRFX_VERSION 2 + #endif #endif /*------------------------------------------------------------------*/ @@ -921,7 +920,7 @@ static bool hfclk_running(void) } #endif -#ifdef NORDIC_SDK_OLD_API +#if CFG_TUD_NRF_NRFX_VERSION == 1 return nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY); #else return nrf_clock_hf_is_running(NRF_CLOCK, NRF_CLOCK_HFCLK_HIGH_ACCURACY); @@ -946,7 +945,7 @@ static void hfclk_enable(void) } #endif -#ifdef NORDIC_SDK_OLD_API +#if CFG_TUD_NRF_NRFX_VERSION == 1 nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED); nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART); #else @@ -971,7 +970,7 @@ static void hfclk_disable(void) } #endif -#ifdef NORDIC_SDK_OLD_API +#if CFG_TUD_NRF_NRFX_VERSION == 1 nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP); #else nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTOP);