Handyboard Networking using the Cricket Bus

 

 

Background Info -- One of the ideas that spawned with some brainstorming was the idea of networking a bunch of Handyboards together so that each person could work and test their own part of the power wheels project with out having to share Handyboards with other people testing and designing.  I thought it sounded like a good challenge so I offered to program the networking.  Also, Fred wanted the networking to work identically to the specifications of the Cricket Bus.

 

Some Details of the Cricket Bus -- There is only one master and any number of slaves that each have unique one byte id's.  The master is the only one in the network that can initiate communications. However this does not limit the slaves from sending the master data or the master sending slaves data.  For each slave, you will have a predetermined number of bytes to send and receive.  The way it works is that the master sends out a command byte where the byte is the unique ID of a slave.  Then the master and slave can send each other the predetermined number of data bytes.

 

How to Connect the Handyboards Together -- The signal pin of all the Handyboards digital input 9 must be connected together. And all the Handyboards need to share a common ground.  This is how I did it:

First I made 4 or 5 of these networking cables.

 

As you can see in this picture, the bigger end goes into digital input 9 of the Handyboard.  The white wire goes into the signal pin and the black wire goes to the ground pin.

 

Now to connect all the handy boards together I used part of a solderless bread board. On side marked positive, I connected all the white wires together. This connected the signal of digital input 9 of all the Handyboards together.  Then on the minus side I effectively made the common ground between all the handy boards by connecting the black wires together.

 

How to Use My Driver -- Here is a list of the functions and variables provided by alc_cbus.icb

 

Code to Initialize the Networking -- This is the same whether you are writing master code or slave code.

  1. Include alc_cbus.icb in your program
  2. Before the main loop of your code add these three lines:

    install_PAI_interrupt(0);   // install interrupt into vector table
    start_press();                   // wait for all connected Handyboards to be turned on before pressing start
    listen_to_cbus(0);            // this enables the interrupt to happen

 

Writing Code for a Slave Handyboard -- If you are writing code for a slave, in your main loop you must add an if statement to poll two global variables set by the networking interrupt routine.  The if statement would looks like this:

if((is_cbus_cmd) && (cbus_byte == myID)) //myID is just an arbitrary name for a variable that you could store the slaves unique ID in.

Now I think the easiest way to explain how to the slave should respond is with examples:

  1. In this example you want the slave to respond by sending one byte, the knob value:

    if((is_cbus_cmd) && (cbus_byte == myID))
    {
            is_cbus_cmd = 0;             // clear to zero
            cbus_send_data(knob()); // respond by sending data back
    }
     
  2. In this example you want to receive one byte from the master and store it in a predefined variable 'temp'

    if((is_cbus_cmd) && (cbus_byte == myID))
    {
            while(is_cbus_cmd);        // wait for data byte to be received
            temp = cbus_byte;           // do something with the received data
    }
     
  3. In this example you want to receive two bytes of data from the master

    if((is_cbus_cmd) && (cbus_byte == myID))
    {
            while(is_cbus_cmd);        // wait for data byte to be received
            temp1 = cbus_byte;         // do something with the received data
            is_cbus_cmd = 1;            // reset this flag to 1
            while(is_cbus_cmd);        // wait for data byte to be received
            temp2 = cbus_byte;         // do something with the received data
    }
     

 

Slaves and the LCD -- Just a quick note that using printf for displaying stuff on the LCD is very slow.  In one case I could not figure out what I couldn't get a slave to receive three bytes from the master and it was because my printf on the slave was slow enough to miss bytes.  So if you are having trouble debugging one of the slaves, try commenting out any printf's.

 

Some Master Code Examples -- Once again, I think the easiest way to show how to do this is with an example.  This block of code was intended to get the Compass sensor's value from a slave Handyboard.

#define COMPASS_HB 5
int getCompass()
// this will return the value of the compass
{
        is_cbus_cmd = 1; // reset flag (this value will be set to one after a byte of data is received)
        cbus_send_cmd(COMPASS_HB); // tell the compass HB to return the compass direction
        while(is_cbus_cmd == 1); // wait for compass to send data byte back
        return (int) (((((float) cbus_byte) * 360.0) / 255.0) + .5);
}

Anywhere I want to check the compass value in my master code I can call the function listed above.  Note: The big conversion on at the end of the function is there because the slave code scaled the compass value(0 to 359) to a 1 byte number (0 to 255) and this master code scales it back to a 0 to 359 number.

 

Download My Driver -- (note: I modified the old file names to add the 'alc_' to the front of them)

 

 

 

Handyboard Networking Links

Detailed Specs of the Cricket Bus

My post on the ikonboard that talks about the networking