Skip to main content
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 ...
Ignacio Vazquez-Abrams's user avatar
23 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 <...
Curd's user avatar
  • 16.5k
21 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 ...
jonk's user avatar
  • 78.7k
20 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 ...
Asmyldof's user avatar
  • 18.5k
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 ...
Kuba hasn't forgotten Monica's user avatar
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 ...
Elliot Alderson's user avatar
15 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-...
Peter Cordes's user avatar
  • 1,365
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+...
John's user avatar
  • 1,411
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 ...
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 ...
MarkU's user avatar
  • 15.1k
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 ...
C_Elegans's user avatar
  • 2,941
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 ...
Tim Williams's user avatar
  • 45.6k
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 ...
JimmyB's user avatar
  • 3,913
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 ...
Marcus Müller's user avatar
9 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 ...
Justme's user avatar
  • 172k
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: ...
jusaca's user avatar
  • 9,749
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 ...
Transistor's user avatar
  • 180k
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 ...
Dirk Bruere's user avatar
  • 14.3k
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 ...
Brian Tompsett - 汤莱恩's user avatar
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.).
You's user avatar
  • 171
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 ...
Lundin's user avatar
  • 22.2k
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 ...
Chris Stratton's user avatar
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 — ...
CSM's user avatar
  • 171
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 ...
Olin Lathrop's user avatar
5 votes
Accepted

Is "new" acceptable when using C++ objects in a FreeRTOS application?

This isn't about malloc vs new, rather it is about "should you use heap allocation in an embedded system, or not?" which is more of a philosophical question. Heap memory management is very useful ...
bobflux's user avatar
  • 81.7k
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 ...
Rev's user avatar
  • 10.2k
5 votes

Concept of the static keyword from the perspective of embedded C

I will try to summarize comments and @JimmyB's answer with an explanatory example: Suppose this set of files: static_test.c: ...
gustavovelascoh's user avatar
5 votes
Accepted

How to make water flow sensor work to measure flowed water and not flow rate

If you look at the flow rate vs output frequency, it's fairly linear. If you don't mind a few % error and just assume that it is linear that makes this very easy. Simply count the pulses coming out of ...
Drew's user avatar
  • 7,636
5 votes
Accepted

Drivers in C++ produces bigger assembly

First the question is, why does the class produces bigger assembly? You just introduced a class! That means you get longer method names (keyword: name mangling), if you're building with such embedded ...
Marcus Müller's user avatar

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