Programming Arduino Uno Hardware Memory Types

In a previous post, I referenced a Linux Journal article looking at Arduino code development from a programmer’s viewpoint. Today’s ‘Arduino programming’ post will address what types of memory hardware are in the Arduino Uno and a little bit about how those different types of memory are used.
Arduino Uno hardware components (from Zenbike.com)

All the memory of a standard Arduino Uno R3 board is found in the Atmel ATmega328P microcontroller (MCU). If you look at the Arduino Uno Zenbike photo on the right that has the Arduino hardware components labeled, you can see where the microcontroller is located on the board. To begin understanding Arduino programming from a hardware standpoint, we need to know what chunks of memory hardware are inside that Atmel MCU.

The ATmega328P has three types of memory hardware:

  • 32 KB of In-System Programmable (ISP) Flash program memory
  • 2 KB of SRAM (Static Random-Access Memory)
  • 1 KB of EEPROM (Electrically Erasable Programmable Read-Only Memory

Memory and CPU from ATmega328P block diagram
ISP flash memory is where most of programming is stored. The Atmel MCU in the Arduino Uno has 32 KB of ISP flash. This is more room for program storage than some MCUs, especially other 8-bit MCUs, but there are many MCUs that have more memory. So if you’re working with a complex program on the Arduino Uno and run out of storage space, one option might be to use a different MCU that has more program memory capacity. For more info regarding the specifics of ISP flash on the Atmel AVR MCUs, see Atmels application note about that topic. Heres how Adafruit describes the ATmega328s flash:
Flash memory is used to store your program image and any initialized data. You can execute program code from flash, but you cant modify data in flash memory from your executing code. To modify the data, it must first be copied into SRAM. Flash memory is the same technology used for thumb-drives and SD cards. It is non-volatile, so your program will still be there when the system is powered off. Flash memory has a finite lifetime of about 100,000 write cycles.”
The ATmega328P’s 2 KB of SRAM is used in three main ways in the Arduino (info from above Adafruit link):
  • Static Data - This is a block of reserved space in SRAM for all the global and static variables from your program. For variables with initial values, the runtime system copies the initial value from Flash when the program starts.
  • Heap - The heap is for dynamically allocated data items. The heap grows from the top of the static data area up as data items are allocated. 
  • Stack - The stack is for local variables and for maintaining a record of interrupts and function calls.”
If you’ve gotten to the point where your MCU project’s program is controlling many items, receiving lots of inputs and just generally doing a lot of work, you might start taxing the SRAM. If that happens, or maybe to prevent that from happening, you might want to read Adafruit’s guide to optimizing use of SRAM, which says,
SRAM is the most precious memory commodity on the Arduino...SRAM shortages are probably the most common memory problems on the Arduino...If your program is failing in an otherwise inexplicable fashion, the chances are good you have crashed
Hackaday -- CPLD shield with 2 MB SRAM
the stack due to a SRAM shortage. There are a number of things that you can do to reduce SRAM usage. These are just a few guidelines
...”
If you think you’ve crashed your Arduino because of program complexity, especially a program which might be expecting a lot in the areas of static data, heap and stack, try some of Adafruit’s suggestions for improving SRAM use. Also, because SRAM is the “most precious memory commodity on the Arduino,” you may want to consider a shield that provides more SRAM. Hackaday has a post showing a CPLD (Complex Programmable Logic Devices) shield that increases SRAM from the standard 1 KB by three orders of magnitude up to 2 MB.

Tronixstuff.com has a pretty good post about what the 1 KB of EEPROM in the Arduino Uno can be used for.
EEPROM...is a form of non-volatile memory that can remember things with the power being turned off, or after resetting the Arduino...we can store data generated within a sketch on a more permanent basis...where data that is unique to a situation needs a more permanent home. For example, storing the unique serial number and manufacturing date of a commercial Arduino-based project – a function of the sketch could display the serial number on an LCD, or the data could be read by uploading a ‘service sketch’. Or you may need to count certain events and not allow the user to reset them – such as an odometer or operation cycle-counter.”  
If you find yourself needing more than 1 KB for storing data, there are EEPROM shields like the one shown at the left. However, as mentioned above, if memory capacity becomes an issue, you should first determine if a different MCU with more internal memory might be more appropriate for your use case.

Arduino beginners don’t need to be too concerned about where the different parts of their programs and data are being stored while they’re learning how to make an LED blink or doing the early Blum tutorials. As your Arduino programs get larger and more complex, however, you’ll probably want to put more effort into managing memory use on your Arduino. If this memory hardware guide for your Arduino doesn’t point you to a helpful resource for that memory management, you can put Google to work finding other resources for you.

**********
Read More..

Looking At Arduino From A Programming Viewpoint

Because the Arduino single-board microcontroller is a piece of hardware, most of my focus in trying to understand the world of microcontrollers (MCUs) so far has been on the hardware aspect. You know -- what components hook up to which pins on the Arduino, and what do the different electronic components do in each of the circuits I cobble together.

To get the most out of microcontrollers, though, one also needs to understand the software or firmware side of things. To get a better picture of how Arduino software functions, you may want to consider reading an article John H from the Humboldt Microcontrollers Group linked me to. The article is titled, "Understand Arduino development" and starts out this way,
"Arduino is a fantastic platform for getting started with embedded software development. You are provided a development board, and programming IDE all configured to work together immediately. As a developer, all you need to do is write your program, press a button, and it is compiled and uploaded to the board where it begins execution. Its important you understand the fundamentals of what is happening in the background to make this work for you."
I need to do more online research about the differences between learning C and learning C++. Then I need to buy Nick or John a beverage and have a discussion about whether my C / C++ studies should be done with tutorials, websites and books that focus on C or on C++. If I need the basics of C to do much of anything in C++, it might be helpful to know what specific aspects of C++ should spice up my C studies. According to the article linked above,
"The Arduino IDE compiles your project as C++ using the GNU AVR toolchain...So you are writing "real" C++ code, though there are a few hardware limitations to keep in mind. Its called the GNU AVR toolchain because it compiles code for the AVR micro-controller architecture."
I am especially interested in the section of the article that talks about large projects. Most of my learning projects will be pretty simple, but down the road I hope to get to the point where Ill build an MCU project with fairly complex functions and programming requirements. It appears it
will likely be helpful to learn how to write libraries in the Arduino IDE as well as sketches. According to the "Understand Arduino development" article,
"The Arduino IDE has the concept of a "sketchbook", and the programs that you write are called "sketches". So the sketchbook is a folder which contains all of your sketches...The sketchbook should also contain a folder called "libraries", which allows you to share code across sketches. Similar to structure of a sketch, a library needs to be a folder where the name of the folder is the name of the library...The main reason you would consider this is because you should split your project into multiple files by their logical function. You are not limited to using libraries to achieve this either, a sketch can have multiple source files...Splitting the project into modular files is key for long term development. It allows you to reuse code later on without having to copy and paste individual functions out of source files, rather you just include the library or add the source files to your sketch. When you make your project modular, you can document each module on its own, which allows others to use that module in their own projects without having to understand how the entire program works."
There are lots of two hour introductory classes or sessions on Learning The Basics Of Arduino where a person starting with no knowledge about MCUs or electronics can hook together a basic circuit and make an LED blink. But like anything complex and powerful, there are many hours of study and experimentation required to become reasonably skilled in the Art of Microcontrollers. Reading about "splitting the project into modular files" reinforces that blinking an LED is significantly different from becoming skilled with MCUs.

Reading the article (twice, so far) that John linked me to and doing a bit of related online research has had a definite impact. It convinced me I need to block out more time in my schedule for completing the work in the Jeremy Blum video tutorials and for gathering background information for a couple microcontroller projects that arent in the videos.

**********
Read More..



Blog Archive

Diberdayakan oleh Blogger.