Skip to content

Commit f6be9d5

Browse files
authored
Merge pull request #1669 from martinling/delay
Firmware: Move delay functions, add M0 version of `delay_us_at_mhz`.
2 parents 67d61ba + 3ef784b commit f6be9d5

14 files changed

Lines changed: 99 additions & 23 deletions

File tree

firmware/blinky/blinky.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "hackrf_core.h"
2323
#include "platform_detect.h"
24+
#include "delay.h"
2425

2526
int main(void)
2627
{

firmware/common/delay.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2026 Great Scott Gadgets <info@greatscottgadgets.com>
3+
*
4+
* This file is part of HackRF.
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2, or (at your option)
9+
* any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; see the file COPYING. If not, write to
18+
* the Free Software Foundation, Inc., 51 Franklin Street,
19+
* Boston, MA 02110-1301, USA.
20+
*/
21+
22+
#include "delay.h"
23+
24+
void delay(uint32_t duration)
25+
{
26+
uint32_t i;
27+
28+
for (i = 0; i < duration; i++) {
29+
__asm__("nop");
30+
}
31+
}
32+
33+
void delay_us_at_mhz(uint32_t us, uint32_t mhz)
34+
{
35+
#if defined(LPC43XX_M4)
36+
// The loop below takes 3 cycles per iteration.
37+
uint32_t loop_iterations = (us * mhz) / 3;
38+
asm volatile("start%=:\n"
39+
" subs %[ITERATIONS], #1\n" // 1 cycle
40+
" bpl start%=\n" // 2 cycles
41+
:
42+
: [ITERATIONS] "r"(loop_iterations));
43+
#elif defined(LPC43XX_M0)
44+
// The loop below takes 4 cycles per iteration.
45+
uint32_t loop_iterations = (us * mhz) / 4;
46+
asm volatile("start%=:\n"
47+
" sub %[ITERATIONS], #1\n" // 1 cycle
48+
" bpl start%=\n" // 3 cycles
49+
:
50+
: [ITERATIONS] "r"(loop_iterations));
51+
#else
52+
#error "No delay loop implementation"
53+
#endif
54+
}

firmware/common/delay.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2026 Great Scott Gadgets <info@greatscottgadgets.com>
3+
*
4+
* This file is part of HackRF.
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2, or (at your option)
9+
* any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; see the file COPYING. If not, write to
18+
* the Free Software Foundation, Inc., 51 Franklin Street,
19+
* Boston, MA 02110-1301, USA.
20+
*/
21+
22+
#ifndef __DELAY_H
23+
#define __DELAY_H
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
#include <stdint.h>
30+
31+
void delay(uint32_t duration);
32+
void delay_us_at_mhz(uint32_t us, uint32_t mhz);
33+
34+
#endif /* __DELAY_H */

firmware/common/fpga_selftest.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "streaming.h"
2525
#include "selftest.h"
2626
#include "fpga.h"
27+
#include "delay.h"
2728

2829
// USB buffer used during selftests.
2930
#define USB_BULK_BUFFER_SIZE 0x8000

firmware/common/hackrf_core.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "hackrf_core.h"
2525
#include "hackrf_ui.h"
26+
#include "delay.h"
2627
#include "sgpio.h"
2728
#include "si5351c.h"
2829
#include "spi_ssp.h"
@@ -441,26 +442,6 @@ jtag_t jtag_cpld = {
441442
.gpio = &jtag_gpio_cpld,
442443
};
443444

444-
void delay(uint32_t duration)
445-
{
446-
uint32_t i;
447-
448-
for (i = 0; i < duration; i++) {
449-
__asm__("nop");
450-
}
451-
}
452-
453-
void delay_us_at_mhz(uint32_t us, uint32_t mhz)
454-
{
455-
// The loop below takes 3 cycles per iteration.
456-
uint32_t loop_iterations = (us * mhz) / 3;
457-
asm volatile("start%=:\n"
458-
" subs %[ITERATIONS], #1\n" // 1 cycle
459-
" bpl start%=\n" // 2 cycles
460-
:
461-
: [ITERATIONS] "r"(loop_iterations));
462-
}
463-
464445
/* GCD algo from wikipedia */
465446
/* http://en.wikipedia.org/wiki/Greatest_common_divisor */
466447
static uint32_t gcd(uint32_t u, uint32_t v)

firmware/common/hackrf_core.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,6 @@ extern "C" {
389389
#define SCU_H1R9_NO_VAA_EN (P6_10) /* GPIO3[6] on P6_10 */
390390
#define SCU_H1R9_TRIGGER_EN (P2_5) /* GPIO5[5] on P2_5 */
391391

392-
void delay(uint32_t duration);
393-
void delay_us_at_mhz(uint32_t us, uint32_t mhz);
394-
395392
/* TODO: Hide these configurations */
396393
extern si5351c_driver_t clock_gen;
397394
extern const ssp_config_t ssp_config_w25q80bv;

firmware/common/ice40_spi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <libopencm3/lpc43xx/scu.h>
2525
#include "hackrf_core.h"
26+
#include "delay.h"
2627

2728
void ice40_spi_target_init(ice40_spi_driver_t* const drv)
2829
{

firmware/common/operacake_sctimer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <libopencm3/lpc43xx/scu.h>
3030
#include <libopencm3/lpc43xx/gima.h>
3131
#include "sct.h"
32+
#include "delay.h"
3233

3334
#define U1CTRL_SET SCT_OUT14_SET
3435
#define U1CTRL_CLR SCT_OUT14_CLR

firmware/common/platform_detect.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "firmware_info.h"
2424
#include "gpio_lpc.h"
2525
#include "hackrf_core.h"
26+
#include "delay.h"
2627
#include "adc.h"
2728

2829
#include <libopencm3/lpc43xx/scu.h>

firmware/common/portapack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "hackrf_core.h"
2626
#include "gpio_lpc.h"
27+
#include "delay.h"
2728

2829
#include <libopencm3/lpc43xx/scu.h>
2930

0 commit comments

Comments
 (0)