Hot answers tagged

30

Optimisation Low-level programming for embedded systems is quite different from programming for general purpose devices, such as computers and cell phones. Efficiency (in terms of speed and space) is far more important because resources are at a premium. That means the very first thing to do if you run out of space is to look at what parts of your code you ...


25

When you upload your code to your Arduino, say an Uno for example, it'll tell you how many bytes it uses up out of the 32K available. That's how much flash memory you have (think computer hard disk). While your program is running, it's using whats called SRAM, and there is much less of that available. Sometimes you'll notice your program behaving oddly at a ...


17

In addition to what others have said (on which I fully agree), I would advise to read this adafruit article about memory; it's well written, explains a lot of things about memory and provide hints on how to optimize it. At the end of the read, I think you would get a quite complete answer to your question. To sum it up, you have 2 possible optimization ...


15

When booted, the Arduino will run whatever program was put on it last. Solution: Don't turn it on if you don't want it to run. When out of the box, the Arduino usually runs the Blink program. So you can just load that and call it a day if you want to "reset" it. Or have a loop program with a sleep command. If you've been messing with the USB bootloader ...


10

AVR is a modified Harvard architecture family, so code is stored in flash only, whereas data exists primarily in RAM when being manipulated. With that in mind, let's address your questions. Why on earth do I have to copy the damn content to RAM before accessing it? You don't need to per se, but by default code assumes that data is in RAM unless the code ...


9

There are two things to do if you run out of storage: Somehow "optimize" your code so it needs less storage; or at least uses less of the particular kind of storage that you ran out of (and uses more of the kind of storage that you still have plenty of). Or, Add more storage. There are lots of tips online on how to do the first (and for the vast majority ...


9

If safety is a major concern, then you should almost always load a stop* sketch onto the board before calling it a day. This would be a good idea, for example, when your board is semi-permanently a part of a larger system. You could do so by: Upload the Bare Minimum sketch found in File -> Examples -> Basic in the arduino-ide Upload a sketch with a large ...


8

This is how Print::print prints from program memory in the Arduino library: size_t Print::print(const __FlashStringHelper *ifsh) { const char PROGMEM *p = (const char PROGMEM *)ifsh; size_t n = 0; while (1) { unsigned char c = pgm_read_byte(p++); if (c == 0) break; n += write(c); } return n; } __FlashStringHelper* is an empty class ...


7

Short answer: No; From the atmega328's data sheet (though it applies to all AVR's): AVR uses a Harvard architecture – with separate memories and buses for program and data. Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the program ...


7

The microcontroller is executing one command at a time from..where? Flash memory directly. The AVR core can only execute instructions from Flash memory. It uses a two-bus system known as Harvard Architecture, where the Instruction Bus is connected to Flash and is used to execute instructions, and the Data Bus is connected to SRAM, EEPROM, all the ...


7

You are saving a String object in EEPROM, which is useless. A string object does not store the contents of your string. Instead, it just stores: the memory address where the actual contents (the characters) is stored the amount of memory allocated for this contents the number of characters that actually make the string This is what you are storing to, and ...


6

An easy way of accessing any type of data in PROGMEM is to use this small library: PROGMEM_readAnything.h #include <Arduino.h> // for type definitions template <typename T> void PROGMEM_readAnything (const T * sce, T& dest) { memcpy_P (&dest, sce, sizeof (T)); } template <typename T> T PROGMEM_getAnything (const T * sce) ...


6

I am afraid there is no good solution to this problem. One option I do like is to use the __flash qualifier instead of PROGMEM: const uint8_t ram_array[] = { 1, 2, 3, 4 }; __flash const uint8_t flash_array[] = { 5, 6, 7, 8 }; void function_reading_ram(const uint8_t *array) { uint8_t secondElement = array[1]; // ... } void function_reading_flash(...


5

After further digging, this appears to be from Mikal Hart's Flash library. The reason this library was developed is: Storing static program data in flash/PROGMEM is a tricky part of Arduino programming. To save precious RAM, a novice user already at odds with unfamiliar C++ syntax must digest such daunting concepts as prog_char, PSTR(), PROGMEM, ...


5

No. The foundation of a Harvard architecture such as AVR is to only allow code that exists within program space to be executed, and EEPROM is not within program space. It is possible, however, to write a virtual machine that will run from flash. This VM can then read program-become-data from anywhere and take action based on it.


5

Paul covered the most important points. This answer is intended to be a complement to his. First, you should keep in mind the general rules, which are more important than the more specific ones. The general rule for saving RAM is: be aware of what you are storing, and do not store anything you do not really need. The general rule for saving flash is: be ...


5

Yes you can. This Arduino IDE add on lets you make calls to code store in the bootload of memory so you can make changes to the flash memory. https://majek.sh/en/writing-to-internal-flash-on-arduino/ The number of times you rewrite a cell are limited. I'd suggest you are better off with adding a FRAM memory chip. Get a 5V part with SPI interface. Fast reads,...


5

The Optiboot version 7 has a do_spm function which can be called from application. The Optiboot repository contains an example for the use of this function. SPM is the AVR CPU instruction to write to flash memory. The Optiboot wraps this in a function. A pointer to this function is put in a 'vector table' at the beginning of the bootloader. The first pointer ...


4

I researched this once when I was getting consistent, exactly 85 degC readings from my sensors. The Maxim DS18b20 data sheet (rev 042208, page 4, in a footnote to Table 1) says: "The power-on reset value of the temperature register is +85°C. Double check that the Arduino code is follows the sensor communication protocol correctly.


4

The flash array is from Mikal Hart's flash library as you show, and what it does is that they declare an array or a matrix (table) object in the PROGMEM memory: #define FLASH_ARRAY(type, name, values...) \ static const type name##_flash[] PROGMEM = { values }; \ _FLASH_ARRAY<type> name(name##_flash, sizeof(name##_flash) / sizeof(type)); which is an ...


4

There is a helper macro in avr-libc that is designed just for this purpose: #define pgm_read_float_near(address_short) __LPM_float((uint16_t)(address_short)) Read a float from the program space with a 16-bit (near) address. Note The address is a byte address. The address is in the program space. Use it like: PROGMEM float pi=3.141592653; // ...


4

This application already exists. It's called AVRDUDE: http://www.nongnu.org/avrdude/ You can download it compiled for Windows at the link above. This is the very flashing tool that the Arduino IDE uses for the AVR boards such as Nano. You can get the AVRDUDE command the Arduino IDE uses to upload to the Nano by following these steps: Start the Arduino IDE ...


4

The answer is "yes, you can use extra AVR Flash memory as non-volatile EEPROM-like Flash memory storage, very similar to an STM32 microcontroller." One of these days I'm going to write an Arduino library to make this really easy for the beginner. For now, however, I'm going to simply post links to valuable references that contain the full answers and that I ...


4

There is also an OO solution possible, using the strategy design pattern, but this comes with some (slight) memory and performance penalties. You need to use C++ for this. Put each array you want to differ between flash/SRAM in a separate class (one per type). You get data hiding and possible separation of concerns for free. Instead of putting the array in ...


3

The Due (more specifically the SAM3X) uses its own proprietary protocol. It's all embedded in the bootloader, which is hard wired in the ROM and can never be changed (idiotic if you ask me). I know of two systems for communicating with it. Atmel's SAM-BA program, and the open source BOSSA. The command line version of BOSSA (bossac) is used by the Arduino ...


3

There should be no difference between turning on your computer and plugging in a USB cable. The embedded devices typically plugged into a USB ports such as mice, keyboards, wireless adapters, hard drives, etc don't have their FLASH memory corrupted when the computer is turned on so it is unlikely that your Arduino would. To confirm there isn't something ...


3

I think here is a confusion about the three types of memory. EEPROM, FLASH and SRAM are completely separate. EEPROM is like your file storage. FLASH contains you program and static values. SRAM is the memory the program is working with. Static values are loaded from FLASH into SRAM upon start unless you declare your static content not to load automatically ...


3

The simplest way would be to have two sketches. One that will store all the strings into EEPROM. After that you can upload the second actual sketch that uses the values stored in EEPROM. You only need to upload the first sketch if any of the strings change. There is a way to upload a hex directly to the EEPROM using avrdude, but you'd have to use the ...


3

If I understand your question correctly, you need to integrate this data into a .ino script. Probably as a byte array. Then the script can read the array and Serial.write its content. You could write a small tool in Python or else on your computer, that takes the text file and converts it into a .h header. Include that into your .ino script. Or use your ...


3

I don't think you need a file system: that is usually needed when you want to manage many files. Your weather data log sounds like a single file. What you seem to need is to access EEPROM as a stream, exactly like you would for an open file. There are stream wrappers around some EEPROM libraries, e.g. this one for ESP. I suggest you check it out and see if ...


Only top voted, non community-wiki answers of a minimum length are eligible