1

First, let me say that I am very new to writing drivers so I apologize if my terminology is completely off.

I am attempting to write a driver to control an LED and read back some registers on my FPGA development board. As nicely pointed out here I understand that I can supply my binary (or C program) input values by using (in my C program):

int main(int argc, char *argv[]) { /* ... */ }

So, as an example, in my Linux shell if I run:

root@socfpga:~# ./LED_BLINK 182

I can send an input integer of 182 to my binary that is being executed in the linux shell script. In my case this blinks my LED 182 times (this part works). So I know how to give my program inputs, but how do I properly extract outputs from my program?

My main questions are as follows:

1) In general, suppose my binary has variable(s) that I would like to return as outputs, how can I do this? Example, if I want my program to output the value at register 5, I input in the shell:

root@socfpga:~# ./LED_BLINK 1 5

The LED will blink once and then my shell will return the value at register 5 (this register is just a variable in the binary program).

Another way to describe what I am after, suppose if I have a single register (let's say it is a 16 bit value) and I would like my program to output that value, how can I properly achieve this? Can I pass a variable to my binary (such as a pointer?) and have my program write to that value? Can you provide an example of this or a link to a tutorial?

2) Similar to above, what if I have a list of registers (say 20 or more 16 bit values), how could my binary properly output these values ( a pointer array, maybe)?

3) I am sure that this process has a technical name. What is this 'technically' called (so that I can have smarter google search entries)?

Thank you for your help, James

CakeMaster
  • 31
  • 4
  • Generally, the Unix way of returning data from a program is to simply print it to `stdout`. – Christian Gibbons May 26 '20 at 19:49
  • Hello, thank you for your reply. Oh wow, so I might have seriously been overthinking this, haha. – CakeMaster May 26 '20 at 19:58
  • Printing to stdout or stderr is an option, but if you're writing C code, you can return a value. That's why ```main()``` is type ```int``` and not ```void```. Simply end your main routine with ```return(value)``` and you can return a single value. Generally the command line will normally consider this an exit status, but you can treat it however you like. You can access it using ```echo $?``` in bash. – Fubar May 26 '20 at 20:02
  • *"I can send an input integer of 182 to my binary ..."* -- It's not an *"integer"*. The arguments to the program are passed as strings (in this case a string composed of digits). BTW you overuse & misuse the term *"binary"*; if you are referring to your program (or its compiled/executable version), then write "my program" rather than use an ambiguous entity. – sawdust May 26 '20 at 21:36

1 Answers1

1

The most direct way to pass data between programs in a Unix setting is to use standard input and output "pipes" (also known as stdin and stdout).

By default the C statement "printf" will default to writing to stdout and you will see the output on the terminal by default or you can pass it to another program as input using the "|", or to another file-type operation using the ">" operand.

This is explained in more detail on this post: What does it mean to write to stdout in C?

  • Thank you for your response. It seems like I was overthinking this in my original post. – CakeMaster May 26 '20 at 20:27
  • I started my coding life on mainframes. When I moved to UNIX and its variants it took me a while to get to the point where I understood that "treat everything like a file" was a way of life at the core of the system. You're on the right path! – Eliel Mamousette May 26 '20 at 22:04