Unverified Commit 03e740a5 authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

Add CPU cycle counter accessors (#226)

Use the 24-bit SYSTICK peripheral, wrapped in logic to extend it to a
full 32 or 64bits.  W/o the wrapper, SYSTICK will wrap around in ~100ms.

Adds rp2040.getCycleCount() and rp2040.getCycleCount64()

Clean up the libpico build process as crt0.S from the pico-sdk should
be directly used.

Clean up the keywords file.
parent e628c8e2
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "hardware/regs/m0plus.h"
#include "hardware/platform_defs.h"
#include "hardware/regs/addressmap.h"
#include "hardware/regs/sio.h"
#include "pico/binary_info/defs.h"
#ifdef NDEBUG
#ifndef COLLAPSE_IRQS
#define COLLAPSE_IRQS
#endif
#endif
.syntax unified
.cpu cortex-m0plus
.thumb
.section .vectors, "ax"
.align 2
.global __vectors
__vectors:
.word __StackTop
.word _reset_handler
.word isr_nmi
.word isr_hardfault
.word isr_invalid // Reserved, should never fire
.word isr_invalid // Reserved, should never fire
.word isr_invalid // Reserved, should never fire
.word isr_invalid // Reserved, should never fire
.word isr_invalid // Reserved, should never fire
.word isr_invalid // Reserved, should never fire
.word isr_invalid // Reserved, should never fire
.word isr_svcall
.word isr_invalid // Reserved, should never fire
.word isr_invalid // Reserved, should never fire
.word isr_pendsv
.word isr_systick
.word isr_irq0
.word isr_irq1
.word isr_irq2
.word isr_irq3
.word isr_irq4
.word isr_irq5
.word isr_irq6
.word isr_irq7
.word isr_irq8
.word isr_irq9
.word isr_irq10
.word isr_irq11
.word isr_irq12
.word isr_irq13
.word isr_irq14
.word isr_irq15
.word isr_irq16
.word isr_irq17
.word isr_irq18
.word isr_irq19
.word isr_irq20
.word isr_irq21
.word isr_irq22
.word isr_irq23
.word isr_irq24
.word isr_irq25
.word isr_irq26
.word isr_irq27
.word isr_irq28
.word isr_irq29
.word isr_irq30
.word isr_irq31
// Declare a weak symbol for each ISR.
// By default, they will fall through to the undefined IRQ handler below (breakpoint),
// but can be overridden by C functions with correct name.
.macro decl_isr_bkpt name
.weak \name
.type \name,%function
.thumb_func
\name:
bkpt #0
.endm
// these are separated out for clarity
decl_isr_bkpt isr_invalid
decl_isr_bkpt isr_nmi
decl_isr_bkpt isr_hardfault
decl_isr_bkpt isr_svcall
decl_isr_bkpt isr_pendsv
decl_isr_bkpt isr_systick
.macro decl_isr name
.weak \name
.type \name,%function
.thumb_func
\name:
.endm
decl_isr isr_irq0
decl_isr isr_irq1
decl_isr isr_irq2
decl_isr isr_irq3
decl_isr isr_irq4
decl_isr isr_irq5
decl_isr isr_irq6
decl_isr isr_irq7
decl_isr isr_irq8
decl_isr isr_irq9
decl_isr isr_irq10
decl_isr isr_irq11
decl_isr isr_irq12
decl_isr isr_irq13
decl_isr isr_irq14
decl_isr isr_irq15
decl_isr isr_irq16
decl_isr isr_irq17
decl_isr isr_irq18
decl_isr isr_irq19
decl_isr isr_irq20
decl_isr isr_irq21
decl_isr isr_irq22
decl_isr isr_irq23
decl_isr isr_irq24
decl_isr isr_irq25
decl_isr isr_irq26
decl_isr isr_irq27
decl_isr isr_irq28
decl_isr isr_irq29
decl_isr isr_irq30
decl_isr isr_irq31
// All unhandled USER IRQs fall through to here
.global __unhandled_user_irq
.thumb_func
__unhandled_user_irq:
bl __get_current_exception
subs r0, #16
.global unhandled_user_irq_num_in_r0
unhandled_user_irq_num_in_r0:
bkpt #0
.section .reset, "ax"
// This is the beginning of the image, which is entered from stage2 or bootrom USB MSD watchdog reboot
// note if we are NO_FLASH then start: below is currently identical anyway, so save 4 bytes
#if !PICO_NO_FLASH
// We simply install our own vector table and redirect through it
ldr r0, =__vectors
b __vector_entry
#endif
// ELF entry point generally called when we load an ELF via debugger
.type _entry_point,%function
.thumb_func
.global _entry_point
_entry_point:
#if PICO_NO_FLASH
// non flash
ldr r0, =__vectors
#else
// todo clear watchdog?
// When using flash, we install and use the ROM vector table to go thru regular bootrom/stage2 flash sequence
movs r0, #0
#endif
__vector_entry:
ldr r1, =(PPB_BASE + M0PLUS_CPUID_OFFSET)
str r0, [r1, #8]
ldmia r0!, {r1, r2}
msr msp, r1
bx r2
// ----------------------------------------------------------------------------
// Reset handler:
// - initialises .data
// - clears .bss
// - calls runtime_init
// - calls main
// - calls exit (which should eventually hang the processor via _exit)
.type _reset_handler,%function
.thumb_func
_reset_handler:
// Hang all cores except core 0
ldr r0, =(SIO_BASE + SIO_CPUID_OFFSET)
ldr r0, [r0]
cmp r0, #0
bne wait_for_vector
adr r4, data_cpy_table
// assume there is at least one entry
1:
ldmia r4!, {r1-r3}
cmp r1, #0
beq 2f
bl data_cpy
b 1b
2:
// Zero out the BSS
ldr r1, =__bss_start__
ldr r2, =__bss_end__
movs r0, #0
b bss_fill_test
bss_fill_loop:
stm r1!, {r0}
bss_fill_test:
cmp r1, r2
bne bss_fill_loop
platform_entry: // symbol for stack traces
// Use 32-bit jumps, in case these symbols are moved out of branch range
// (e.g. if main is in SRAM and crt0 in flash)
ldr r1, =runtime_init
blx r1
ldr r1, =main
blx r1
ldr r1, =exit
blx r1
// exit should not return. If it does, hang the core.
// (fall thru into our hang _exit impl
.weak _exit
.type _exit,%function
.thumb_func
_exit:
1: // separate label because _exit can be moved out of branch range
bkpt #0
b 1b
data_cpy_loop:
ldm r1!, {r0}
stm r2!, {r0}
data_cpy:
cmp r2, r3
blo data_cpy_loop
bx lr
#if !PICO_NO_BINARY_INFO
binary_info_header:
.word BINARY_INFO_MARKER_START
.word __binary_info_start
.word __binary_info_end
.word data_cpy_table // we may need to decode pointers that are in RAM at runtime.
.word BINARY_INFO_MARKER_END
#endif
.align 2
data_cpy_table:
#if PICO_COPY_TO_RAM
.word __ram_text_source__
.word __ram_text_start__
.word __ram_text_end__
#endif
.word __etext
.word __data_start__
.word __data_end__
.word __scratch_x_source__
.word __scratch_x_start__
.word __scratch_x_end__
.word __scratch_y_source__
.word __scratch_y_start__
.word __scratch_y_end__
.word 0 // null terminator
// ----------------------------------------------------------------------------
// Provide safe defaults for _exit and runtime_init
// Full implementations usually provided by platform.c
.weak runtime_init
.type runtime_init,%function
.thumb_func
runtime_init:
bx lr
// ----------------------------------------------------------------------------
// In case core 1's VTOR has already been moved into flash, we need to handle
// core 1 reset. However, we do so by just jumping back into bootrom version of
// wait_for_vector
wait_for_vector:
ldr r0, = 'W' | ('V' << 8)
bl rom_func_lookup
bx r0
.global __get_current_exception
.thumb_func
__get_current_exception:
mrs r0, ipsr
uxtb r0, r0
bx lr
// ----------------------------------------------------------------------------
// Stack/heap dummies to set size
.section .stack
// align to allow for memory protection (although this alignment is pretty much ignored by linker script)
.align 5
.equ StackSize, PICO_STACK_SIZE
.space StackSize
.section .heap
.align 2
.equ HeapSize, PICO_HEAP_SIZE
.space HeapSize
\ No newline at end of file
......@@ -21,6 +21,8 @@
#include <hardware/clocks.h>
#include <hardware/irq.h>
#include <hardware/pio.h>
#include <hardware/exception.h>
#include <hardware/structs/systick.h>
#include <pico/multicore.h>
#include <pico/util/queue.h>
#include <CoreMutex.h>
......@@ -114,8 +116,21 @@ private:
static constexpr int _GOTOSLEEP = 0x66666666;
};
class RP2040;
extern RP2040 rp2040;
class RP2040 {
public:
RP2040() {
_epoch = 0;
// Enable SYSTICK exception
exception_set_exclusive_handler(SYSTICK_EXCEPTION, _SystickHandler);
systick_hw->csr = 0x7;
systick_hw->rvr = 0x00FFFFFF;
}
~RP2040() { /* noop */ }
// Convert from microseconds to PIO clock cycles
static int usToPIOCycles(int us) {
// Parenthesis needed to guarantee order of operations to avoid 32bit overflow
......@@ -127,18 +142,44 @@ public:
return clock_get_hz(clk_sys);
}
// Get CPU cycle count. Needs to do magic to extens 24b HW to something longer
volatile uint64_t _epoch = 0;
inline uint32_t getCycleCount() {
uint32_t epoch;
uint32_t ctr;
do {
epoch = (uint32_t)_epoch;
ctr = systick_hw->cvr;
} while (epoch != (uint32_t)_epoch);
return epoch + (1 << 24) - ctr; /* CTR counts down from 1<<24-1 */
}
inline uint64_t getCycleCount64() {
uint64_t epoch;
uint64_t ctr;
do {
epoch = _epoch;
ctr = systick_hw->cvr;
} while (epoch != _epoch);
return epoch + (1LL << 24) - ctr;
}
void idleOtherCore() {
fifo.idleOtherCore();
}
void resumeOtherCore() {
fifo.resumeOtherCore();
}
// Multicore comms FIFO
_MFIFO fifo;
};
extern RP2040 rp2040;
private:
static void _SystickHandler() {
rp2040._epoch += 1LL << 24;
}
};
// Wrapper class for PIO programs, abstracting common operations out
// TODO - Add unload/destructor
......
......@@ -9,12 +9,27 @@
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setup1 KEYWORD2
loop2 KEYWORD2
setup1 KEYWORD2
loop2 KEYWORD2
analogWriteFreq KEYWORD2
analogWriteRange KEYWORD2
analogWriteResolution KEYWORD2
analogWriteResolution KEYWORD2
######################################
# Constants (LITERAL1)
#######################################
push KEYWORD2
push_nb KEYWORD2
pop KEYWORD2
pop_nb KEYWORD2
rp2040 KEYWORD2
RP2040 KEYWORD2
usToPIOCycles KEYWORD2
f_cpu KEYWORD2
getCycleCount KEYWORD2
getCycleCount64 KEYWORD2
idleOtherCore KEYWORD2
resumeOtherCore KEYWORD2
PIOProgram KEYWORD2
prepare KEYWORD2
No preview for this file type
......@@ -25,6 +25,7 @@
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_clocks/include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_divider/include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_dma/include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_exception/include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_flash/include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_gpio/include
-iwithprefixbefore/pico-sdk/src/rp2_common/hardware_i2c/include
......
void setup() {
Serial.begin(115200);
delay(3000);
Serial.println("High precision timing measurements/delays can be built using");
Serial.println("rp2040.getCycleCount() and rp2040.getCycleCount64().\n");
uint32_t a = rp2040.getCycleCount();
delay(1000);
uint32_t b = rp2040.getCycleCount();
Serial.printf("There are %d cycles in one second.\n\n\n", b - a);
delay(3000);
Serial.println("Note that the (32-bit) getCycleCount() will wraparound in ~30 seconds.");
Serial.println("Using the 64-bit getCycleCount64() makes it practically infinite.");
Serial.printf("\n%15s - %15s\n", "getCycleCount", "getCycleCount64");
}
void loop() {
Serial.printf("%15u - %15llu\n", rp2040.getCycleCount(), rp2040.getCycleCount64());
delay(1500);
}
......@@ -33,6 +33,7 @@ target_link_libraries(pico
hardware_clocks
hardware_divider
hardware_dma
hardware_exception
hardware_flash
hardware_gpio
hardware_i2c
......@@ -73,12 +74,6 @@ target_link_libraries(pico
pico_audio_i2s
)
add_custom_command(TARGET pico PRE_BUILD
COMMAND arm-none-eabi-gcc -g -c ../../../assembly/crt0.S -I "${PICO_SDK_PATH}/src/rp2040/hardware_regs/include" -I "${PICO_SDK_PATH}/src/common/pico_binary_info/include/"
)
add_custom_command(TARGET pico POST_BUILD
COMMAND ar d libpico.a crt0.S.obj
COMMAND ar r libpico.a crt0.o
COMMAND ar d libpico.a stdio.c.obj stdio_usb.c.obj stdio_usb_descriptors.c.obj
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment