Coder Social home page Coder Social logo

baremetal's Introduction

Bare metal for ARM cortex-M using C++

This example show how to write simple startup code in C++ and use Cmake for building. Presented example is very simple "Hello blinky" program for STM32F0xx which blink LED connected to GPIO A15 and blink on maximum speed (no delay or timers are used).

C++ benefits for embedded

  • C++ provides many advantages over C, like OOP, namespaces, templates, overloading, references, ...
  • Also allow to write very clean and readable source code and very efficient final code

Limitations, or not recommended C++ features in embedded

  • Do not use new and delete (unless you define your own on heap or in static array - memory pool)
  • No exceptions are allowed and also no components which using exceptions (like most of std::, ...)
  • No RTTI, ..

Everything from this is also possible to use, but then is necessary to implement some stub functions for heap management and basic IO for newlib. Final compiled program can be much bigger in memory (often more than 80KB) and this also brings some overhead, which is often useless in embedded applications.

Code structure

  • CmakeLists.txt is Cmake building script
  • src folder contain application code
  • src/startup folder contain all startup code
  • ld folder contain linker scripts
  • cmake/arm-none-eabi.cmake is platform file which define compilers and configuration for this project

Dependencies

this don't need any other dependencies like CMSIS library for building. Example is real bare metal code.

Building

mkdir _build
cd _build
cmake -D CMAKE_TOOLCHAIN_FILE=../cmake/arm-none-eabi.cmake -D CMAKE_BUILD_TYPE=Release ..
make

change CMAKE_BUILD_TYPE=Debug to build debug version.

You can use also make bin, srec, hex, flash, ...

Change architecture

in file CmakeLists.txt change CPU_OPTIONS:

  • architecture options:

    • Cortex-M0: set(CPU_OPTIONS -mthumb -mcpu=cortex-m0)
    • Cortex-M0+ set(CPU_OPTIONS -mthumb -mcpu=cortex-m0plus)
    • Cortex-M3 set(CPU_OPTIONS -mthumb -mcpu=cortex-m3)
    • Cortex-M4 set(CPU_OPTIONS -mthumb -mcpu=cortex-m4)
    • Cortex-M7 set(CPU_OPTIONS -mthumb -mcpu=cortex-m7)
    • Cortex-M23: set(CPU_OPTIONS -mthumb -mcpu=cortex-m23)
    • Cortex-M33: set(CPU_OPTIONS -mthumb -mcpu=cortex-m33)
  • floating point, add options to CPU_OPTIONS:

    • soft float -mfloat-abi=softfp
    • hard float -mfloat-abi=hard
  • for floating point also add fpv architecture

    • Cortex-M4 single precision -mfpu=fpv4-sp-d16
    • Cortex-M7 and M33 single-precision -mfpu=fpv5-sp-d16
    • Cortex-M7 and M33 signle and double-precision -mfpu=fpv5-d16

Detailed info about options is in readme: gcc-arm directory: share/doc/gcc-arm-none-eabi/readme.txt or in ARM-Options document

baremetal's People

Contributors

pavelrevak avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.