Number of cycles when using C with PRU

134 views
Skip to first unread message

Alek Mabry

unread,
Nov 5, 2017, 10:07:55 AM11/5/17
to BeagleBoard
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:


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

Przemek Klosowski

unread,
Nov 5, 2017, 5:12:17 PM11/5/17
to beagl...@googlegroups.com
On Sun, Nov 5, 2017 at 10:07 AM, Alek Mabry <alekm...@gmail.com> wrote:


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. 

Alek Mabry

unread,
Nov 6, 2017, 7:31:23 AM11/6/17
to BeagleBoard
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.

Przemek Klosowski

unread,
Nov 6, 2017, 3:47:20 PM11/6/17
to beagl...@googlegroups.com
On Mon, Nov 6, 2017 at 7:31 AM, Alek Mabry <alekm...@gmail.com> wrote:
Is there a particular way I am supposed to type out commands in asm(); ?

asm() is not part of the C language definition, so the syntax depends on 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 
and look in e.g. example/pruss_c/pru_hal.c

Reply all
Reply to author
Forward
0 new messages