Serial Communication: A “Don Quichotte” story !

Serial communication with Arduino :

The ATmega328p shipped with Arduino can do a lot of interesting things, including communication over several different protocols: SPI, I2E, and “classical” serial communication.

The serial communication is very useful as it allow us to send data to the host computer “easily” mainly for debugging purposes. Has i don’t know for the moment if a “real” debugger like GDB will operate withArduino, i will rely on simple “printf like” function to debug my code. That’s why Serial communication is pretty important.

Serial communication is provided by the ATmega328p used on Arduino Uno board. Arduino provide us facilities to use it, and among those facilities, there is the:

Serial over USB !!

This was the first point in this project where i get stuck for several hours trying to understand why things where not working as i except, i finally solve the problem, which one, like most of the time in programming, much more simpler then i thought !

In order for me to remember and for you to laugh at me here is the story :

Fighting against Windmill:

Because my development computer, a Raspberry pi, does not have any serial port, i need to use the usb port connected to the Arduino to communicate with it.

On the Arduino uno revision 3 this feature is provided by a small chip soldered on the board and connected to the ATmega328p. This chip, an ATmega8u2 (an other avr chip by atmel) is responsible of routing all the serial data going out of the main ATmega328p to the usb port of the arduino in the appropriate format.

When the arduino is connected to the Raspberry pi, a new tty is detected by the linux kernel:
/dev/ttyACM0
It’s through this tty that you can read what the arduino send.

This sound pretty straight forward, but after uploading my first program to the Arduino, i did not receive any serial data on this tty…

At first i tough it was coming from a problem in my build options, or maybe from a wrong version of the Avr-Libc… so i spend several hours reading stuff about how the USB over serial was working until i realize something by looking at a typical sketch created using the Arduino IDE.

Roughly, when you write a sketch in Arduino IDE, you need to write only two function: Init() and Loop(). Those functions are then called by the code provided in the main.cpp file:

#include <Arduino.h>

int main(void)
{
        init();
 
#if defined(USBCON)
        USBDevice.attach();
#endif
        
        setup();
    
        for (;;) {
                loop();
                if (serialEventRun) serialEventRun();
        }
        
        return 0;
}

What is miss in my own program was the call to init(), which in defined in wiring.c. This function initialize some serial communication related stuff, and, as the comment in the code specifies:

“// this needs to be called before setup() or some functions won’t
// work there”

So after adding a call to init() in my own code, i was able to send and receive data over USB and start debugging my application ! Great !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s