Should i switch to Arduino Mega ?

I’m facing some troubles related with something i did not anticipate:

Program Size !

Arduino Uno Flash Memory is smaaalll !

As you may know, the ATmega328p use a “Harvard architecture”. This mean that program memory is separated from data memory.

Concerning the data memory, i already knew that the 2k bytes available on the Arduino Uno platform were not going to be enough for this project. That’s was why i anticipated that, and investigate a solution based upon the 23k256 chip (see this post).

But what i did not anticipated is that the program memory was also going to be too small. The Flash memory that contain program run by the Arduino Uno is 32k bytes:

  • 5k bytes are already used by the Arduino boot loader.
  • Linking and using the avr-libc + the Arduino Core Library also consume several kilo bytes
  • Linking and using the SPI + MIDI library + SPIRAM library + LiquidCrystal Library also consume several kilo bytes

The only thing, is that now, after writing some code for the MIDILooper itself, i only have less than 6k bytes available for all the rest of the code

I can’t be sure for  now, but it may not be enough to store the entire program…

There is no way to extend the Flash memory, so i may switch from Arduino Uno to Arduino Mega, which contain 512k bytes of program memory…

Advertisements

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 !