Monday 21 October 2013

BBB, the UARTs and C/C++

Ok a primer

You've got your BBB setup, running Angstrom with a connection over the debug serial channel and powered from the 5V plug.

Why this setup?  Apparently, there's some issues with the usb-ethernet code on the kernel which logs a lot, using up disk space and cpu resources.  See here for  a description.

Now what we would like is to enable UARTs at runtime and send/receive data over them, right?  Fortunately there is a convention setup for this using device tree overlays (DTO) which allows us to do this. The description here is probably the most succinct on how to get these running from a terminal. Copied over to here all we do is this:

echo BB-UART1 > /sys/devices/bone_capemgr.8/slots

To check if it worked you can do this:

cat /sys/devices/bone_capemgr.8/slots

where you should see the list of devices installed in the slots.

But wait we want to do this at runtime!  No worries we can print to the slots file the same as we did in the terminal.  See this guys code for a description on how to enable pretty much any hardware you want. For our purposes the bbb_enableSerial function is the one we want.

So now that we've enabled the hardware during runtime we would still like to configure and preferably send and receive data.  Read this.  Basically we can set parameters using stty:

stty -F /dev/ttyO1 115200
stty -F /dev/ttyO1 raw

And then we can read and write in in the terminal like this:

read ---> cat /dev/ttyO1
write ---> echo hello! > /dev/ttyO1

And now if we want to do this in c/c++ we simply open /dev/ttyO1 as a file for reading or writing and read and write to it using the standard c-library.  For reference look here. I could not open it for read and write access as of yet.

/* fopen example from cpp reference */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  return 0;
}

Thats all.





No comments:

Post a Comment