55
votes
MCU programming - C++ O2 optimization breaks while loop
The code optimizer has analyzed the code and from what it can see the value of choice will never change. And since it will never change, there's no point in ...
22
votes
Accepted
Avr-gcc using repeated addition instead of MULU instructions
Assuming that you want to optimize for speed:
Unless you really find out how many cycles it takes if you use mul, you can't compare.
So let’s try:
If you use the <...
20
votes
Concept of the static keyword from the perspective of embedded C
The keyword 'static' in C has two fundamentally different meanings.
Limiting Scope
In this context, 'static' pairs up with 'extern' to control the scope of a variable or function name. Static causes ...
19
votes
Accepted
Version control of schematics and source code
Most of it comes down to personal preferences.
I track everything I do for a project in Git. Especially since Git handles most types of files, even binary, sufficiently efficiently. (In stead of ...
19
votes
Accepted
What does a function mean that doesn't have "void", "int", etc in front of it mean?
The functions are interrupt handlers, defined using the ISR(vector, attributes) macro. The macro generates a proper platform-specific function signature, including ...
17
votes
Accepted
Why use const variables instead of preprocessor directives when programming embedded with C++
Using a const value lets the compiler do some type checking, but a value you assign with a #define is just used for text ...
14
votes
Accepted
MCU programming - C++ O2 optimization breaks while loop
(Cross site duplicate on SO about the thread case, rather than interrupt/signal-handler case). Also related: When to use volatile with multi threading?
A data race on a non-...
11
votes
Accepted
STM32 interrupts and C++ don't go well together
I've been searching for a solution for half a day, only to find it a few minutes after I posted this question. The answer is given by this post:
Bug in Keil ARM compiler with interrupt handlers and C+...
11
votes
Is C++ suitable for embedded systems?
I hope to add more light than heat to this discussion about C++ on bare metal and resource constrained systems.
Problems in C++:
Exceptions are especially a RAM problem as the required "emergency ...
Community wiki
11
votes
Accepted
STM32 crashes when FreeRTOS project is converted to C++
FreeRTOS wasn't designed to support C++.
https://interactive.freertos.org/hc/en-us/community/posts/210028906-Using-FreeRTOS-with-C-
Since I don't see any other references to USART3_EXTI28_IRQHandler ...
10
votes
Accepted
Estimating the frequency at which your microcontroller is running
Take a look at the compiler output here https://godbolt.org/g/TxZSgt. Notice that the inner for loop and the asm("nop") lines ...
10
votes
Embedded C++ and multiple copies of same structure
Consider the alternative: what else would you use the memory for?
If nothing, then it doesn't matter how you allocate it; fill a whole bunch extra with gibberish for all that matters!
Or, if you have ...
9
votes
Accepted
How to enable c++11 support for Atmel Studio?
I just started a new C++ project under Atmel Studio 7 and with the default options the following code failed with a nullptr not declared error:
...
9
votes
Concept of the static keyword from the perspective of embedded C
statics are not visible outside of the current compilation unit, or "translation unit". This is not the same as the same file.
Notice that you include the header ...
9
votes
Accepted
What is the simplest instruction set that has a C++/C compiler to write an emulator for?
Easiest would be to invent my own instruction set
uh, ok, we might come from very different experiences here…
With easiest I mean the least amount of instructions.
That's not necessarily the ...
8
votes
Are virtual-functions called at a high frequency suitable for embedded systems?
Just like on X86, this is going to depend too much on the specific code that any one answer will be useful. It is also going to depend a lot on the processor (and the memory architecture that ...
8
votes
What is the simplest instruction set that has a C++/C compiler to write an emulator for?
You should take al look at the PIC microcontroller family. The instruction set is limited to 35 different instructions, while the controller is actually still used.
Look at the datasheet at page 228: ...
8
votes
Accepted
Detecting the push button outside the loop function in Arduino
delay() is generally bad programming except for very simple applications as it ties up the processor.
Use the elapsedMillis() library function instead. In use you ...
8
votes
Accepted
Program never goes into interrupt
If you are 100% sure about the interrupt setup being correct, this is a generic how to make C++ code callable from C question which is much better explained elsewhere, but in short, you don't have a ...
7
votes
What is the simplest instruction set that has a C++/C compiler to write an emulator for?
You need a One Instruction Set Computer (OISC)
A one instruction set computer (OISC), sometimes called an ultimate
reduced instruction set computer (URISC), is an abstract machine that
uses ...
7
votes
What is the simplest instruction set that has a C++/C compiler to write an emulator for?
A simple instruction set of only 8 instructions used for teaching is known as the MU0 instruction set. It originated at Manchester University and is used for teaching both compiler writing and ...
7
votes
What is the simplest instruction set that has a C++/C compiler to write an emulator for?
Donald Knuth's MMIX architecture has a 64-bit RISC instruction set with 256 opcodes and existing C compilers (GCC, actively maintained) and emulators (mixvm, etc.).
6
votes
Are virtual-functions called at a high frequency suitable for embedded systems?
Andrew's answer is totally right, I just want to emphasize on a few point:
If you really need a virtual method (because the code to execute depends on the type of object), then, most probably, a ...
6
votes
Accepted
STM32F3 CAN Hardware not Entering Init Mode
This code is wrong:
while(~(MY_CAN_PORT->MSR & CAN_MSR_INAK));
~ is not the logical NOT operator, but the bitwise ...
6
votes
What is the simplest instruction set that has a C++/C compiler to write an emulator for?
I hope for something with like 50 instructions. Also, 32 bit and c++
The "Beta" architecture used in MIT's 6.004 core track class is a 32-bit RISC design often referred to as a simplification of the ...
6
votes
Why use const variables instead of preprocessor directives when programming embedded with C++
static const values respect the scope of namespace and class — ...
5
votes
Version control of schematics and source code
1) Its definitely worth versioning schematic/board files. Even if you can't track differences so easily, you have a clean way to revert to a specific hardware release if you have to work with an old ...
5
votes
RTOS: Micrium uCOS vs Mentor Graphics Nucleus
I'm Jean Labrosse, I wrote the uC/OS series of kernels (uC/OS in 1992, uC/OS-II in 1998 and uC/OS-III in 2008).
There are quite a lot of things to like and in fact prefer over Nucleus. For one thing,...
5
votes
Estimating the frequency at which your microcontroller is running
You can't test the speed of a microcontroller by measuring how long it takes to execute unknown numbers of instruction.
There is no guarantee what exact instructions the compiler generates from your ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c++ × 218arduino × 53
microcontroller × 52
c × 51
embedded × 27
stm32 × 25
avr × 18
programming × 17
pic × 11
arm × 11
interrupts × 8
esp32 × 8
serial × 7
compiler × 7
i2c × 6
esp8266 × 6
attiny × 6
keil × 6
freertos × 6
sensor × 5
timer × 5
assembly × 5
adc × 4
raspberry-pi × 4
atmega × 4