|
| 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 | +} |
0 commit comments