Apr 27, 2014 0 Comments in Learning, Mega 2560 by
Mega 2560 control terminal

In most cases having a simple and quick access to the system GPIOs, ADCs and other ports is useful, here I’m going to show to you how to implement a simple control terminal for the on-board CPU of Cosino’s Mega 2560 extension.

In this example I’m going to use simple commands from the command line as well as a bit more complex PHP function to send commands for a WEB based monitoring of GPIOs status.

First of all you need a running Debian OS on your Cosino then you need the arduino-dev project from our github site.

Installing the software

Well, once you get an HEX file by using command make into the projects/arduino-dev/mega2560_ctrl directory you should upload it into Mega 2560 CPU. On Cosino Mega 2560 board you can do it by using command:

warning Note: the script mega2560.sh can be download from our Github site with the modified avrdude program. See here for further info.

warning Note that to compile the Mega 2560 code you can use the standard Arduino suite too!

Now a quick&dirty trick to get access to the this tool from bash is:

warning Note: On Cosino Mega 2560 board the Mega 2560 CPU is connected to the serial port /dev/ttyS3, so you should change this name accordingly to your system configuration.

Now enter the help command (just the h char) to get a list of available commands:

So, for instance, to set GPIO 52 to high level (logic 1) you can use command:

warning Warning: this trick doesn’t manage message handshaking with the control terminal… so you cannot send multiple commands at time (you must wait for ok\n or ko\n sequence before issuing a new command)!

Usage examples: Screen & PHP

If you don’t want use stty and other bash tools you may wish using the screen tool from a terminal for a quick access without other settings as follow:

Now you are connected with the control terminal with the right settings! Just hit the command h (help) followed by the ENTER key to get the help message as above:

Again, to set GPIO 52 to 1 you can use the command:

On the other hand if you wish communicate with this tool within a program you can use a function as the following (PHP code):

Note that this time we correctly wait for the system answer before returning the result to the user. So, the well-known command to set GPIO 52 to 1 can be issued by using:

In the out variable you can find the control terminal answer where each line is separated by \r char for an easy parsing.

If you take a look at projects/arduino-dev/mega2560_ctrl/php directory you will see a complete WEB example regarding how to use the above PHP function. Just copy both files mega2560_ctrl.jpg and mega2560_io_func.inc.php to you HTTP server root directory and then point your browser to http://<server_ip>/mega2560.php address to get the screen below:

mega2560_ctrl web page

Note that this example is very minimal since we do not auto-magically update GPIOs statuses as soon as they change but we must hit the load button to refresh the screen!

Another important notice is that the string protocol we use to talk with the Mega 2560 CPU is quite slow so, for better performance, we should considering to use a binary protocol. However for low speed tasks and for a quick pins settings during the prototyping stages this tool is quite useful!

FAQ

1) I try to run /opt/mega2560.sh prog /opt/mega2560_ctrl.hex on Cosino Mega but i get this error :

Any ideas ?

You need to use the modified version of avrdude program as described here.

Facebooktwitterlinkedin
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

− 6 = 4