Week 15

Networking and communication.

Networking and communication

The assignment is to make more than two processors communicate with each other. I used Neil's serial communication example as a start point and modified some things in the code.

The assignment breakdown

There is two kinds of the boards, bridge and node. The only difference is that the bridge board has FTDI connected on the bus to allow the computer to communicate with the boards. So when the boards recieve a special character that matches its identifier it toggles the led and send to the computer its name.

... ...

You can download the files from the assignment webpage.

Machining and soldering

... ...

Programming

I modified Neil's code to make the node toggle the LED everytime you call it. So if you send to node 0, it will turn the led on then if you send to it again it will turn the LED off. I also kept the flashing part to detect the traffic on the bus.

This macro toggles the pin between 1 and 0 everytime i call it. So basically i'm using logical XOR on the PORT pin and 1. If the PORT pin is 1, it will be 0 (1 XOR 1 = 0) and if the PORT pin is 0, it will be 1.

#define toggleLed() (led_port ^= led_pin)

I changed the flash function to be using toggle macro instead of directly writing to the pin high or low.

void flash() {
   // flash the led
   toggleLed();
   led_delay();
   toggleLed();
}

The main loop still the same. It flashes when something is revieved, and if the node_id matches, it sends back to the computer and toggle the LED.

// main loop
//
while (1) {
  get_char(&serial_pins, serial_pin_in, &chr);
  flash();
  if (chr == node_id) {
     output(serial_direction, serial_pin_out);
     static const char message[] PROGMEM = "node ";
     put_string(&serial_port, serial_pin_out, (PGM_P) message);
     put_char(&serial_port, serial_pin_out, chr);
     put_char(&serial_port, serial_pin_out, '\r'); // new line
     put_char(&serial_port, serial_pin_out, '\n'); // new line
     led_delay();
     flash();
     input(serial_direction, serial_pin_out);
     toggleLed();
     }
  }

Download the modified source code:

hello.bus.45.c
hello.bus.45.make

Testing

...

I used screen to communicate over serial with the boards.

screen /dev/ttyUSB0 9600

Networking and communication - Serial from Mohamed Kamel on Vimeo.



Happy networking