cancel
Showing results for 
Search instead for 
Did you mean: 

C++ usage on STM32F427

dhiry2k1
Associate II
Posted on February 18, 2015 at 19:38

Hi team,

we are using STM32F427 and using C++ as our primary language for development.

I would like to know :

1. Will it affect my performance with C++.

2. Can i use template Library

3. Can I use use vectors or different searching algorithms?

I am using GNU 4.6 Compiler for my project.

Thanks,
6 REPLIES 6
AvaTar
Lead
Posted on February 19, 2015 at 15:28

IMHO this much more an issue of the compiler/toolchain you use, than of the MCU.

Check what your toolchain supports, and what libraries it comes with.

Some time ago I read an article from IAR, stating that C++ can produce as fast and small applications as plain C. I have not tried yet - it might be o.k. if you use/port something like a GUI toolkit or an OS HAL, written in C++. For self-contained bare-metal applications, it doesn't make much sense.

Generally, you need to be careful, since MCUs are generally low in RAM, and have no real

new()/delete()

equivalent (i.e. no virtual memory). The C++ habit of promiscuous temporary object copies can push you over the cliff quickly.

Liviu Ionescu
Associate III
Posted on February 19, 2015 at 17:01

C++ can produce as fast and small applications as plain C

I concur.

Carefully written inline templates can produce very good code, since the compiler has the full context available and can optimise to the limit. I did some tests and, for simple blinky applications, my template based GPIO classes generate smaller code than usual HAL or StdPeriph based implementations.

On the other hand, if you try to use C++ libraries not designed for embedded environments, i.e. use classes that by design use dynamic memory (like String), or use exceptions, RTTI, you'll most probably run into troubles.

C++ is a great language, and this stands for embedded application too, but you need to know what you're doing.

carl2399
Associate II
Posted on February 20, 2015 at 11:10

I agree with Liviu.

In my experience, my C++ applications are smaller than the similar C application (on STM32). The primary reason is that the code can be better structured. I use GCC as my compiler, and in order to keep my code small / fast / stable I have a number of guidelines that I code by.

1. No memory allocation. No malloc / free / new / delete.

2. All classes are defined and instantiated at compile time - this is a personal preference, but it does result in a static memory map.

3. Rule 1 means that very little of the standard C++ libraries can be used. In fact I don't use any at all. My code is ''raw'' C++, calling only C functions (strcpy etc).

4. I don't tend to use templates, but probably because I don't have much experience with them.

5. I don't use RTTI, exceptions, or the streaming operators. (No << or >>, instead I use printf, putch, getch, gotch).

The above all sound rather limiting, but as described C++ makes for an extremely nice improvement to C. As an example I have a FIFO_Class, and also a virtual Stream_Class. Derived from the Stream class is a UART_Class, which contains two of the FIFO_Classes. All of it statically assigned at compile time - including the memory areas for the FIFOs. In my project, there are 6 instances of the UART_Class - because that's how many UARTs there are in an STM32F405. I don't use classes for everything, and still have a lot of boring old C - although even that is better when compiled with the C++ compiler.  

childresss
Associate II
Posted on February 21, 2015 at 20:50

I use about the same conventions for C++ with embedded systems.

Are you able to share your UART base class and FIFO class?

carl2399
Associate II
Posted on February 23, 2015 at 11:14

I don't share the UART class, although it is derived from a more generic Stream class, which allows me to putch() getch() and gotch() in a reasonably generic manner. The FIFO class is a reasonably generic fifo. Please forgive my coding style, which can at times be a bit terse :)

class

Fifo_C
{
private

:
u8 *pBuffer;

// Pointer to the data storage buffer

u16 BufferSize;

// The length of the data buffer

u16 PutIdx;

// Destination for putch

u16 GetIdx;

// Source for getch

void

IncGetter(

void

)
{ GetIdx++;

if

(GetIdx >= BufferSize) GetIdx = 0; }
public

:
u16 Used;
u16 MaxUsed;
void

Reset(

void

)
{ MaxUsed = Used = PutIdx = GetIdx = 0; }
void

SetBuffer(u8 *pData, u16 DataLength)
{ pBuffer = pData; BufferSize = DataLength; Reset(); }
volatile
bool

gotch(

void

)

// Anything in the FIFO?

{

return

PutIdx != GetIdx; }
u8 getch(

void

)
{

if

(! gotch())

return

0; u8 ch = pBuffer[GetIdx];
IncGetter();

return

ch; }
u8 peek(

void

)
{

return

pBuffer[GetIdx]; }
void

putch(u8 c)
{ u16 tmpGetIdx = GetIdx; pBuffer[PutIdx] = c;
PutIdx++;

if

(PutIdx >= BufferSize) PutIdx = 0;
if

(PutIdx == tmpGetIdx)

printf

(

'' FIFO Overflow %p ''

,

this

);
Used = Length(); MaxUsed = max(MaxUsed, Used); }
volatile

u16 Length(

void

)

// The current number of items in the fifo

{

if

(PutIdx >= GetIdx)

return

PutIdx - GetIdx;
return

BufferSize + PutIdx - GetIdx; }
u16 Spare(

void

)

// The amount of space left in the fifo.

{

return

BufferSize - Length(); }
};
To use it, give it some memory:
u8 FifoMem[300];
Fifo_C MyFifo;
void

OnStartup(

void

)
{
// Apply memory buffer to fifo
MyFifo.SetBuffer(FifoMem,

sizeof

(FifoMem));
// Use the fifo
}

Liviu Ionescu
Associate III
Posted on August 26, 2016 at 09:45

For those interested in C++, here is a project that is written in C++:

http://micro-os-plus.github.io

Some demo Eclipse projects with CubeMX integration, are available from:

https://github.com/micro-os-plus/eclipse-demo-projects