Number of cycles when using C with PRU

Hello!

I’ve been experimenting a bit with writing PRU code with c, and I wrote a program that turns on the USR1 light when a button connected to P8-12 is pressed:

https://github.com/AlekMabry/PRU_tutorials/blob/master/button-pru.c

Although I want to start using the PRU code for time-specific functions like reading a signal from the HC-SR04 (which is a pulse the length of the distance the sensor sensed). In order to do that I think that I would have to poll the sensor, and each time I check the sensor’s value it would increment a value some specified amount. If I where to write a program that polls in exactly, say 20 cycles, I would know that each +1 added to the variable would represent 100ns.

However in the C code, I have no idea what it’s actually doing in assembly.

Take a function for example, if I make a void function, will the assembler have a command that jumps to that part of the code, and then jumps back (adding two cycles or so), or will it just insert that code into wherever the function is called?

Is there any way to force the C program to do something in a specified number of cycles, figure out how many cycles the C code is taking, or just write assembly for time-critical parts directly into the C code itself?

Thanks,
Alek

However in the C code, I have no idea what it's actually doing in assembly.

Take a function for example, if I make a void function, will the assembler
have a command that jumps to that part of the code, and then jumps back
(adding two cycles or so), or will it just insert that code into wherever
the function is called?

Well, the C compiler is just essentially generating machine code, just
lilke assembler---it's just that in assembler you specify the instructions,
so there's a simple 1:1 correspondence. You can force the compiler to list
the generated assembly and count instructions. You can also inject assembly
into your function using asm() compiler extensions; typically, you would
insert such assembly in the middle of a function. You can also call your C
functions from such assembly but it's a little tricky because you have to
observe the compiler ABI for register saving, stack operations and
parameter passing.

Is there any way to force the C program to do something in a specified
number of cycles, figure out how many cycles the C code is taking, or just
write assembly for time-critical parts directly into the C code itself?

You can inspect the generated code, and count instructions to get the
cycles. You obviously can't force the compiler to use less cycles than it
already uses in an optimized compile, but you can in principle pad with
NOPs to artificially increase the cycle count.

Is there a particular way I am supposed to type out commands in asm(); ?

asm (

"SET R30, R30, 0x15"

);

Assembly statement “SET R30, R30, 0x15”

creates a label, which may not be what was intended.

the compiler you're using. I know GCC asm() better, but I assume you're
using TI clpru. I don't know of a good explanation of clpur asm(); there
are examples in texane's pru_sdk, so
git clone https://github.com/texane/pru_sdk
and look in e.g. example/pruss_c/pru_hal.c