Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firmware/blinky/blinky.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "hackrf_core.h"
#include "platform_detect.h"
#include "delay.h"

int main(void)
{
Expand Down
54 changes: 54 additions & 0 deletions firmware/common/delay.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2026 Great Scott Gadgets <info@greatscottgadgets.com>
*
* 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)
{
#if defined(LPC43XX_M4)
// 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));
#elif defined(LPC43XX_M0)
// The loop below takes 4 cycles per iteration.
uint32_t loop_iterations = (us * mhz) / 4;
asm volatile("start%=:\n"
" sub %[ITERATIONS], #1\n" // 1 cycle
" bpl start%=\n" // 3 cycles
:
: [ITERATIONS] "r"(loop_iterations));
#else
#error "No delay loop implementation"
#endif
}
34 changes: 34 additions & 0 deletions firmware/common/delay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2026 Great Scott Gadgets <info@greatscottgadgets.com>
*
* 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 <stdint.h>

void delay(uint32_t duration);
void delay_us_at_mhz(uint32_t us, uint32_t mhz);

#endif /* __DELAY_H */
1 change: 1 addition & 0 deletions firmware/common/fpga_selftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 1 addition & 20 deletions firmware/common/hackrf_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions firmware/common/hackrf_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions firmware/common/ice40_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <libopencm3/lpc43xx/scu.h>
#include "hackrf_core.h"
#include "delay.h"

void ice40_spi_target_init(ice40_spi_driver_t* const drv)
{
Expand Down
1 change: 1 addition & 0 deletions firmware/common/operacake_sctimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <libopencm3/lpc43xx/scu.h>
#include <libopencm3/lpc43xx/gima.h>
#include "sct.h"
#include "delay.h"

#define U1CTRL_SET SCT_OUT14_SET
#define U1CTRL_CLR SCT_OUT14_CLR
Expand Down
1 change: 1 addition & 0 deletions firmware/common/platform_detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "firmware_info.h"
#include "gpio_lpc.h"
#include "hackrf_core.h"
#include "delay.h"
#include "adc.h"

#include <libopencm3/lpc43xx/scu.h>
Expand Down
1 change: 1 addition & 0 deletions firmware/common/portapack.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "hackrf_core.h"
#include "gpio_lpc.h"
#include "delay.h"

#include <libopencm3/lpc43xx/scu.h>

Expand Down
1 change: 1 addition & 0 deletions firmware/common/rad1o/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "gpio_lpc.h"
#include "hackrf_core.h"
#include "delay.h"

#include <libopencm3/lpc43xx/scu.h>
#include <libopencm3/lpc43xx/ssp.h>
Expand Down
1 change: 1 addition & 0 deletions firmware/common/rffc5071.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <libopencm3/lpc43xx/scu.h>
#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] = {
Expand Down
1 change: 1 addition & 0 deletions firmware/hackrf-common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions firmware/hackrf_usb/hackrf_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__;
Expand Down