Is there a method that allows me to find out how many clock cycles a certain piece of code will use on a Leonardo?
For example, I could imagine a simulator that runs a sketch and keeps track of the number of clock cycles used.
Note that I don't own an Arduino yet, and I've never worked with one. I'm considering a Leonardo as a USB keyboard controller and simultaneously as a PS/2-to-USB mouse adapter. PS/2 timing, I've read, is very critical. So I want to know if there is enough time in between processing PS/2 signals to do the keyboard controlling.
AWOL:
Cycle counts are detailed in the processor data sheet, available from the Atmel website
OK, so the tool would be my brain and the data sheet which indeed lists the clock cycles. Machine instructions are executed strictly serial, i.e. nothing parallel, right? After all, they write: "Up to 16 MIPS Throughput at 16 MHz"
Robin2:
The problem is that it's not obvious how many assembler program instructions will be generated from C++ code.
That much I know. If there is no automatic tool, my idea is to look at the machine code and check how many cycles the instructions take according to the data sheet.
microseconds() just before and after critical section of code 'd provide 3-4 usec accuracy, may be better down to 1usec.
That's an option. But I'd like to run code in a Leonardo simulator if some such thing exists. Then the timing would probably be way off compared to the real thing.
there are 3 timers you may use, read TCNT0/1/2 - accuracy to 1 cycle, same read before and after, subtract. Depends on board, there may be more timers