azhel12 / Zhele

Framework for Stm32 MCU on C++ templates. Project based on "mcucpp" by Konstantin Chizhov.
BSD 2-Clause "Simplified" License
41 stars 9 forks source link
cpp20 modern-cpp stm32 templates-cpp

Build all examples

Zhele

Framework for Stm32 MCU on C++ templates. Project based on "mcucpp" by Konstantin Chizhov.

Support: I created public group in telegram, where I'll try help everyone with using the framework. Sorry, but I can answer only on russian:)

Also I write small lessons about applying c++ templates (and my framework) for stm32.

Getting started

I'm using following stack:

Basically c++17 is required, c++20 needed only for USB. But I'm planning to use more new features from modern C++ (such as concepts and maybe coroutines and some features from c++23), so it's strongly recommended to use latest toolchain.

  1. Download Visual Studio Code from official site and install it.

  2. Download new Arm Embedded Toolchain from official site. Choose latest (arm-gnu-toolchain-13.2.rel1-mingw-w64-i686-arm-none-eabi). Download exe and install, or you can download zip and add bin folder location to system PATH.\ If you use *nix system download appropriate linux hosted cross toolchains. There is no latest version in official repo (I tried on ubuntu), so unpack toolchain manually and add to path. Tutorial post.\ Check that compiler available from terminal. gcc

  3. Download and install Cmake. Check that cmake is available from terminal. cmake

  4. Install C++ extension pack and Cmake Tools in VSCode (it's not requires but useful). Extensions

  5. Create project folder or download template.\ You can download stm32-cmake manually. Read README.

  6. Add CMakeLists to project root

    
    cmake_minimum_required(VERSION 3.16)
    set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32_gcc.cmake)
    set (CMAKE_CXX_STANDARD 23)

project(zhele_template CXX C ASM)

Populate CMSIS using stm32-cmake project

stm32_fetch_cmsis(F1) find_package(CMSIS COMPONENTS STM32F1 REQUIRED)

Add zhele

You can download zhele and use add_subdirectory instead:

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/Zhele)

include(FetchContent) FetchContent_Declare(Zhele GIT_REPOSITORY https://github.com/azhel12/Zhele.git GIT_TAG zhele_v0.1.1 ) FetchContent_MakeAvailable(Zhele)

Project sources. Add other cpp if needed

set(PROJECT_SOURCES src/main.cpp )

Add executable (firmware) target

add_executable(zhele_template ${PROJECT_SOURCES})

Link CMSIS and Zhele. Add other dependences here

target_link_libraries(zhele_template zhele::zhele CMSIS::STM32::F103C8 STM32::NoSys STM32::Nano)

Recommended options for stm32 dev

target_compile_options(zhele_template PRIVATE -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections)

Print size

stm32_print_size_of_target(zhele_template)

Generate bin file

stm32_generate_binary_file(zhele_template)


6. Write code in source file.
```c++
// Define target cpu frequence.
#define F_CPU 8000000

#include <zhele/iopins.h>

using namespace Zhele::IO;

// Connect LED on A4 for this example (or edit example code, it's simple).
int main()
{
    Pa4::Port::Enable();
    Pa4::SetConfiguration(Pa4::Configuration::Out);
    Pa4::SetDriverType(Pa4::DriverType::PushPull);
    Pa4::Clear();

    for (;;)
    {
    }
}
  1. Press Build button in CMake extension. First time you need choose toolchain. build

  2. Profit! You can flash files i output files