4545#include "nrfx.h"
4646#include "hal/nrf_gpio.h"
4747#include "nrfx_gpiote.h"
48+ #if !defined(NRF54H20_XXAA )
4849#include "nrfx_power.h"
50+ #endif
4951#include "nrfx_uarte.h"
5052#include "nrfx_spim.h"
5153
5860#pragma GCC diagnostic pop
5961#endif
6062
61-
62- // There is API changes between nrfx v2 and v3
63- #if 85301 >= (10000 * MDK_MAJOR_VERSION + 100 * MDK_MINOR_VERSION + MDK_MICRO_VERSION )
64- // note MDK 8.53.1 is also used by nrfx v3.0.0, just skip this version and use later 3.x
65- #define NRFX_VER 2
66- #else
67- #define NRFX_VER 3
63+ // example only supports nrfx v3 for code simplicity
64+ #if !(defined(NRFX_CONFIG_API_VER_MAJOR ) && NRFX_CONFIG_API_VER_MAJOR >= 3 ) && \
65+ !(85301 >= (10000 * MDK_MAJOR_VERSION + 100 * MDK_MINOR_VERSION + MDK_MICRO_VERSION ))
66+ #error "Example requires nrfx v3.0.0 or later"
6867#endif
6968
7069//--------------------------------------------------------------------+
7170// Forward USB interrupt events to TinyUSB IRQ Handler
7271//--------------------------------------------------------------------+
72+ #if defined(NRF54H20_XXAA )
73+ #define USBD_IRQn USBHS_IRQn
74+ void USBHS_IRQHandler (void ) {
75+ tusb_int_handler (0 , true);
76+ }
77+
78+ #else
7379void USBD_IRQHandler (void ) {
7480 tud_int_handler (0 );
7581}
82+ #endif
7683
7784/*------------------------------------------------------------------*/
7885/* MACRO TYPEDEF CONSTANT ENUM
@@ -96,39 +103,39 @@ enum {
96103 #define OUTPUTRDY_Msk POWER_USBREGSTATUS_OUTPUTRDY_Msk
97104#endif
98105
99- static nrfx_uarte_t _uart_id = NRFX_UARTE_INSTANCE (0 );
106+ static nrfx_uarte_t _uart_id = NRFX_UARTE_INSTANCE (CFG_NRFX_UARTE_INSTANCE_ID );
100107
101108// tinyusb function that handles power event (detected, ready, removed)
102109// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
103110extern void tusb_hal_nrf_power_event (uint32_t event );
104111
112+ #if !defined(NRF54H20_XXAA )
105113// nrf power callback, could be unused if SD is enabled or usb is disabled (board_test example)
106114TU_ATTR_UNUSED static void power_event_handler (nrfx_power_usb_evt_t event ) {
107115 tusb_hal_nrf_power_event ((uint32_t ) event );
108116}
117+ #endif
109118
110119//------------- Host using MAX2341E -------------//
111120#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421 ) && CFG_TUH_MAX3421
112121static void max3421_init (void );
113122static nrfx_spim_t _spi = NRFX_SPIM_INSTANCE (1 );
114-
115- #if NRFX_VER > 2
116123static nrfx_gpiote_t _gpiote = NRFX_GPIOTE_INSTANCE (0 );
117124#endif
118125
119- #endif
120-
121126//--------------------------------------------------------------------+
122127//
123128//--------------------------------------------------------------------+
124129
125130void board_init (void ) {
131+ #if !defined(NRF54H20_XXAA )
126132 // stop LF clock just in case we jump from application without reset
127133 NRF_CLOCK -> TASKS_LFCLKSTOP = 1UL ;
128134
129135 // Use Internal OSC to compatible with all boards
130136 NRF_CLOCK -> LFCLKSRC = LFCLK_SRC_RC ;
131137 NRF_CLOCK -> TASKS_LFCLKSTART = 1UL ;
138+ #endif
132139
133140 // LED
134141 nrf_gpio_cfg_output (LED_PIN );
@@ -140,6 +147,7 @@ void board_init(void) {
140147#if CFG_TUSB_OS == OPT_OS_NONE
141148 // 1ms tick timer
142149 SysTick_Config (SystemCoreClock / 1000 );
150+
143151#elif CFG_TUSB_OS == OPT_OS_ZEPHYR
144152 #ifdef CONFIG_HAS_HW_NRF_USBREG
145153 // IRQ_CONNECT(USBREGULATOR_IRQn, DT_IRQ(DT_INST(0, nordic_nrf_clock), priority), nrfx_isr, nrfx_usbreg_irq_handler, 0);
@@ -153,21 +161,6 @@ void board_init(void) {
153161#endif
154162
155163 // UART
156- #if NRFX_VER <= 2
157- nrfx_uarte_config_t uart_cfg = {
158- .pseltxd = UART_TX_PIN ,
159- .pselrxd = UART_RX_PIN ,
160- .pselcts = NRF_UARTE_PSEL_DISCONNECTED ,
161- .pselrts = NRF_UARTE_PSEL_DISCONNECTED ,
162- .p_context = NULL ,
163- .baudrate = NRF_UARTE_BAUDRATE_115200 , // CFG_BOARD_UART_BAUDRATE
164- .interrupt_priority = 7 ,
165- .hal_cfg = {
166- .hwfc = NRF_UARTE_HWFC_DISABLED ,
167- .parity = NRF_UARTE_PARITY_EXCLUDED ,
168- }
169- };
170- #else
171164 nrfx_uarte_config_t uart_cfg = {
172165 .txd_pin = UART_TX_PIN ,
173166 .rxd_pin = UART_RX_PIN ,
@@ -181,7 +174,6 @@ void board_init(void) {
181174 .parity = NRF_UARTE_PARITY_EXCLUDED ,
182175 }
183176 };
184- #endif
185177
186178 nrfx_uarte_init (& _uart_id , & uart_cfg , NULL );
187179
@@ -191,6 +183,7 @@ void board_init(void) {
191183 // 2 is highest for application
192184 NVIC_SetPriority (USBD_IRQn , 2 );
193185
186+ #if !defined(NRF54H20_XXAA )
194187 // USB power may already be ready at this time -> no event generated
195188 // We need to invoke the handler based on the status initially
196189 uint32_t usb_reg ;
@@ -234,6 +227,7 @@ void board_init(void) {
234227 tusb_hal_nrf_power_event (USB_EVT_READY );
235228 }
236229#endif
230+ #endif
237231
238232#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421 ) && CFG_TUH_MAX3421
239233 max3421_init ();
@@ -255,7 +249,9 @@ uint32_t board_button_read(void) {
255249size_t board_get_unique_id (uint8_t id [], size_t max_len ) {
256250 (void ) max_len ;
257251
258- #ifdef NRF5340_XXAA
252+ #if defined(NRF54H20_XXAA )
253+ uintptr_t did_addr = (uintptr_t ) NRF_FICR -> BLE .ADDR ;
254+ #elif defined(NRF5340_XXAA )
259255 uintptr_t did_addr = (uintptr_t ) NRF_FICR -> INFO .DEVICEID ;
260256#else
261257 uintptr_t did_addr = (uintptr_t ) NRF_FICR -> DEVICEID ;
@@ -277,11 +273,7 @@ int board_uart_read(uint8_t* buf, int len) {
277273}
278274
279275int board_uart_write (void const * buf , int len ) {
280- nrfx_err_t err = nrfx_uarte_tx (& _uart_id , (uint8_t const * ) buf , (size_t ) len
281- #if NRFX_VER > 2
282- ,0
283- #endif
284- );
276+ nrfx_err_t err = nrfx_uarte_tx (& _uart_id , (uint8_t const * ) buf , (size_t ) len ,0 );
285277 return (NRFX_SUCCESS == err ) ? len : 0 ;
286278}
287279
@@ -352,18 +344,16 @@ void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info) {
352344// API: SPI transfer with MAX3421E, must be implemented by application
353345//--------------------------------------------------------------------+
354346#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421 ) && CFG_TUH_MAX3421
355-
356- #if NRFX_VER <= 2
357- void max3421_int_handler (nrfx_gpiote_pin_t pin , nrf_gpiote_polarity_t action ) {
358- if (action != NRF_GPIOTE_POLARITY_HITOLO ) return ;
359- #else
360347void max3421_int_handler (nrfx_gpiote_pin_t pin , nrfx_gpiote_trigger_t action , void * p_context ) {
361348 (void ) p_context ;
362- if (action != NRFX_GPIOTE_TRIGGER_HITOLO ) return ;
363- #endif
349+ if (action != NRFX_GPIOTE_TRIGGER_HITOLO ) {
350+ return ;
351+ }
352+ if (pin != MAX3421_INTR_PIN ) {
353+ return ;
354+ }
364355
365- if (pin != MAX3421_INTR_PIN ) return ;
366- tuh_int_handler (1 , true);
356+ tusb_int_handler (1 , true);
367357}
368358
369359static void max3421_init (void ) {
@@ -378,13 +368,8 @@ static void max3421_init(void) {
378368 .sck_pin = MAX3421_SCK_PIN ,
379369 .mosi_pin = MAX3421_MOSI_PIN ,
380370 .miso_pin = MAX3421_MISO_PIN ,
381- #if NRFX_VER <= 2
382- .ss_pin = NRFX_SPIM_PIN_NOT_USED ,
383- .frequency = NRF_SPIM_FREQ_4M ,
384- #else
385371 .ss_pin = NRF_SPIM_PIN_NOT_CONNECTED ,
386372 .frequency = 4000000u ,
387- #endif
388373 .ss_active_high = false,
389374 .irq_priority = 3 ,
390375 .orc = 0xFF ,
@@ -398,14 +383,6 @@ static void max3421_init(void) {
398383 TU_ASSERT (NRFX_SUCCESS == nrfx_spim_init (& _spi , & cfg , NULL , NULL ), );
399384
400385 // max3421e interrupt pin
401- #if NRFX_VER <= 2
402- nrfx_gpiote_init (1 );
403- nrfx_gpiote_in_config_t in_config = NRFX_GPIOTE_CONFIG_IN_SENSE_HITOLO (true);
404- in_config .pull = NRF_GPIO_PIN_PULLUP ;
405- NVIC_SetPriority (GPIOTE_IRQn , 2 );
406- nrfx_gpiote_in_init (MAX3421_INTR_PIN , & in_config , max3421_int_handler );
407- nrfx_gpiote_trigger_enable (MAX3421_INTR_PIN , true);
408- #else
409386 nrf_gpio_pin_pull_t intr_pull = NRF_GPIO_PIN_PULLUP ;
410387 nrfx_gpiote_trigger_config_t intr_trigger = {
411388 .trigger = NRFX_GPIOTE_TRIGGER_HITOLO ,
@@ -426,7 +403,6 @@ static void max3421_init(void) {
426403
427404 nrfx_gpiote_input_configure (& _gpiote , MAX3421_INTR_PIN , & intr_config );
428405 nrfx_gpiote_trigger_enable (& _gpiote , MAX3421_INTR_PIN , true);
429- #endif
430406}
431407
432408// API to enable/disable MAX3421 INTR pin interrupt
0 commit comments