diff --git a/firmware/blinky/blinky.c b/firmware/blinky/blinky.c index 95b1173e..6d071c97 100644 --- a/firmware/blinky/blinky.c +++ b/firmware/blinky/blinky.c @@ -21,6 +21,7 @@ #include "hackrf_core.h" #include "platform_detect.h" +#include "delay.h" int main(void) { diff --git a/firmware/common/delay.c b/firmware/common/delay.c new file mode 100644 index 00000000..8d2045ed --- /dev/null +++ b/firmware/common/delay.c @@ -0,0 +1,42 @@ +/* + * Copyright 2026 Great Scott Gadgets + * + * This file is part of HackRF. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "delay.h" + +void delay(uint32_t duration) +{ + uint32_t i; + + for (i = 0; i < duration; i++) { + __asm__("nop"); + } +} + +void delay_us_at_mhz(uint32_t us, uint32_t mhz) +{ + // The loop below takes 3 cycles per iteration. + uint32_t loop_iterations = (us * mhz) / 3; + asm volatile("start%=:\n" + " subs %[ITERATIONS], #1\n" // 1 cycle + " bpl start%=\n" // 2 cycles + : + : [ITERATIONS] "r"(loop_iterations)); +} diff --git a/firmware/common/delay.h b/firmware/common/delay.h new file mode 100644 index 00000000..123141ba --- /dev/null +++ b/firmware/common/delay.h @@ -0,0 +1,34 @@ +/* + * Copyright 2026 Great Scott Gadgets + * + * This file is part of HackRF. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __DELAY_H +#define __DELAY_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +void delay(uint32_t duration); +void delay_us_at_mhz(uint32_t us, uint32_t mhz); + +#endif /* __DELAY_H */ diff --git a/firmware/common/fpga_selftest.c b/firmware/common/fpga_selftest.c index 287c6adc..fd63ea77 100644 --- a/firmware/common/fpga_selftest.c +++ b/firmware/common/fpga_selftest.c @@ -24,6 +24,7 @@ #include "streaming.h" #include "selftest.h" #include "fpga.h" +#include "delay.h" // USB buffer used during selftests. #define USB_BULK_BUFFER_SIZE 0x8000 diff --git a/firmware/common/hackrf_core.c b/firmware/common/hackrf_core.c index 5d59da00..0a420536 100644 --- a/firmware/common/hackrf_core.c +++ b/firmware/common/hackrf_core.c @@ -23,6 +23,7 @@ #include "hackrf_core.h" #include "hackrf_ui.h" +#include "delay.h" #include "sgpio.h" #include "si5351c.h" #include "spi_ssp.h" @@ -441,26 +442,6 @@ jtag_t jtag_cpld = { .gpio = &jtag_gpio_cpld, }; -void delay(uint32_t duration) -{ - uint32_t i; - - for (i = 0; i < duration; i++) { - __asm__("nop"); - } -} - -void delay_us_at_mhz(uint32_t us, uint32_t mhz) -{ - // The loop below takes 3 cycles per iteration. - uint32_t loop_iterations = (us * mhz) / 3; - asm volatile("start%=:\n" - " subs %[ITERATIONS], #1\n" // 1 cycle - " bpl start%=\n" // 2 cycles - : - : [ITERATIONS] "r"(loop_iterations)); -} - /* GCD algo from wikipedia */ /* http://en.wikipedia.org/wiki/Greatest_common_divisor */ static uint32_t gcd(uint32_t u, uint32_t v) diff --git a/firmware/common/hackrf_core.h b/firmware/common/hackrf_core.h index 301cb7aa..6bc8e909 100644 --- a/firmware/common/hackrf_core.h +++ b/firmware/common/hackrf_core.h @@ -389,9 +389,6 @@ extern "C" { #define SCU_H1R9_NO_VAA_EN (P6_10) /* GPIO3[6] on P6_10 */ #define SCU_H1R9_TRIGGER_EN (P2_5) /* GPIO5[5] on P2_5 */ -void delay(uint32_t duration); -void delay_us_at_mhz(uint32_t us, uint32_t mhz); - /* TODO: Hide these configurations */ extern si5351c_driver_t clock_gen; extern const ssp_config_t ssp_config_w25q80bv; diff --git a/firmware/common/ice40_spi.c b/firmware/common/ice40_spi.c index d3ac713c..1bc38261 100644 --- a/firmware/common/ice40_spi.c +++ b/firmware/common/ice40_spi.c @@ -23,6 +23,7 @@ #include #include "hackrf_core.h" +#include "delay.h" void ice40_spi_target_init(ice40_spi_driver_t* const drv) { diff --git a/firmware/common/operacake_sctimer.c b/firmware/common/operacake_sctimer.c index fca5d03b..1b297cbd 100644 --- a/firmware/common/operacake_sctimer.c +++ b/firmware/common/operacake_sctimer.c @@ -29,6 +29,7 @@ #include #include #include "sct.h" +#include "delay.h" #define U1CTRL_SET SCT_OUT14_SET #define U1CTRL_CLR SCT_OUT14_CLR diff --git a/firmware/common/platform_detect.c b/firmware/common/platform_detect.c index ec00a3b8..199878d8 100644 --- a/firmware/common/platform_detect.c +++ b/firmware/common/platform_detect.c @@ -23,6 +23,7 @@ #include "firmware_info.h" #include "gpio_lpc.h" #include "hackrf_core.h" +#include "delay.h" #include "adc.h" #include diff --git a/firmware/common/portapack.c b/firmware/common/portapack.c index e123affa..035d95f0 100644 --- a/firmware/common/portapack.c +++ b/firmware/common/portapack.c @@ -24,6 +24,7 @@ #include "hackrf_core.h" #include "gpio_lpc.h" +#include "delay.h" #include diff --git a/firmware/common/rad1o/display.c b/firmware/common/rad1o/display.c index 517db0a0..b8e512b9 100644 --- a/firmware/common/rad1o/display.c +++ b/firmware/common/rad1o/display.c @@ -2,6 +2,7 @@ #include "gpio_lpc.h" #include "hackrf_core.h" +#include "delay.h" #include #include diff --git a/firmware/common/rffc5071.c b/firmware/common/rffc5071.c index 541dde2a..959fbc27 100644 --- a/firmware/common/rffc5071.c +++ b/firmware/common/rffc5071.c @@ -39,6 +39,7 @@ #include #include "hackrf_core.h" +#include "delay.h" /* Default register values from vendor documentation or software. */ static const uint16_t rffc5071_regs_default[RFFC5071_NUM_REGS] = { diff --git a/firmware/hackrf-common.cmake b/firmware/hackrf-common.cmake index 788f66c0..70cc9fe8 100644 --- a/firmware/hackrf-common.cmake +++ b/firmware/hackrf-common.cmake @@ -168,6 +168,7 @@ endmacro() macro(DeclareTargets) SET(SRC_M4 ${SRC_M4} + ${PATH_HACKRF_FIRMWARE_COMMON}/delay.c ${PATH_HACKRF_FIRMWARE_COMMON}/hackrf_core.c ${PATH_HACKRF_FIRMWARE_COMMON}/sgpio.c ${PATH_HACKRF_FIRMWARE_COMMON}/rf_path.c diff --git a/firmware/hackrf_usb/hackrf_usb.c b/firmware/hackrf_usb/hackrf_usb.c index 0b5d303a..718e3bb9 100644 --- a/firmware/hackrf_usb/hackrf_usb.c +++ b/firmware/hackrf_usb/hackrf_usb.c @@ -62,6 +62,7 @@ #include "clkin.h" #include "fpga.h" #include "selftest.h" +#include "delay.h" extern uint32_t __m0_start__; extern uint32_t __m0_end__;