stm32plus – A C++ library for STM32 development

The latest release is now 3.0.0. Be sure to check out the announcement here.

This post will introduce stm32plus, an open source library for the STM32 ARM Cortex M3 series of microcontrollers. stm32plus aims to abstract away the nitty gritty details of programming the on-chip peripherals and contains many high-level drivers for common hardware such as LCD displays.

Requirements

  • An STM32 development board. I use a board based on the STM32F103ZET6 running at 72Mhz. In general this library assumes that you are using the F103 series although most of the code should work unmodified on other F1 devices. As yet the library has not been ported to run on F2 or F4 devices.

    My dev board. Less than £30 on ebay gets you the STM32F103ZET6 with many external peripherals including SRAM, NAND flash, NOR flash, SPI flash, RTC, two USART serial ports, five buttons and five LEDs, JTAG and USB ports.

  • A toolchain for building ARM projects. I use the Eclipse Helios IDE with the GNU ARM plugin, CodeSourcery g++ and an ARM-USB-TINY-H debugger with OpenOCD 0.5.0. All of these components except the debugger are free.

    If you’re considering proceeding without a debugger then I urge you to reconsider. The ARM devices are complex MCUs far in advance of the 8 bit AVRs that people typically graduate from. The ability to single step your code in the Eclipse IDE is invaluable and saves hours of frustration.

Some of the features

  • FSMC-based drivers for many TFT LCD controllers, including ILI9325, ILI9327, ILI9481, HX8347A.
  • Graphics library supports drawing primitives, bitmaps, jpegs and multiple fonts (font converter included).
  • Drivers for HD44780 character based LCD displays.
  • Full featured FAT16 and FAT32 drivers for any block device, including SDIO SD cards. Supported features include long filename read/write, directory create/delete and device formatting.
  • USART abstractions including polling, interrupt and DMA modes.
  • Stream IO, observer patterns for interrupt handling.
  • The standard template library (STL) is included.
  • Full error-handing is supported throughout the library.

Installation

You can download the zip file containing the library from my downloads page. The README file in the package contains installation instructions and I’m going to assume that you have got as far as installing the library and headers on your computer

Supported build environments

I build and test stm32plus using Debian Linux and Windows 7 x64 with Cygwin. You should have no problems with other flavours of Linux.

Library design policies

Here’s a list of a few of the core stm32plus policies.

  • Object and memory ownership.

    As a general rule if an stm32plus object receives an object by reference then you own that object and you are responsible for not letting it go out of scope until you’re done using the stm32plus object that receives it.

    If an stm32plus object gives you back a pointer then you are responsible for releasing the memory allocated for that object. In all cases it’s assumed that the delete operator can be used to de-allocate objects.

    Where there are unavoidable exceptions to the above rules they will be noted in the documentation.

    File *file;
    FileSystem *fileSystem;
    
    // create filesystem omitted for brevity
    if(!fileSystem->openFile("/320x240_landscape.64",file))
      handleError();
    

    The file pointer returned by openFile is owned by you and must be deleted when you’re done with it.

  • Object construction

    The development world is split over the use of getters and setters versus constructor arguments for initialising objects. stm32plus uses constructor arguments. The advantage of this approach is that you can’t fail to set something because the constructor won’t let you.

  • Error handing

    Where it’s possible for something to go wrong then the stm32plus member function signature will be declared to return a bool type and it will return false if there is a problem. A nested failure will be propagated up the stack to you with the original cause of the problem intact.

    There is a class called ErrorProvider that is used to encapsulate the source and cause of the error. There is one global instance of ErrorProvider called errorProvider that can be examined to find problem causes.

    #include "error/ErrorProvider.h"
    
    void handleError() {
      uint32_t errorCode=errorProvider.getLast();
    // [...] display or interpret error
    }
    

    Where a constructor can result in an error condition it will be noted in the documentation. After the constructor has returned the caller should call errorProvider.hasError() to determine if the constructor failed.

A first project

It seems to be traditional in the embedded world to create an example program that blinks an LED on the development board to prove that the basics are working and I’m going to follow that tradition here.

My example will go through the steps required to create a new Eclipse project, add the settings required to build with stm32plus and then build the sample project. If you’re not using an Eclipse/CodeSourcery toolchain then you will need to make the necessary changes to fit your environment.

Create the project

In Eclipse, select File->New->C++ Project. This will get you the screen shown below.

Ensure that you’ve selected ARM Cross Target Application and that the toolchain you’re using is selected. Enter a name for the project and hit Finish. You can hit Next if you want to modify the default build configurations (Debug and Release) but you can do that later so I’m not going to bother.

Eclipse will have created the project for you, and now we have to modify the build settings so that it picks up stm32plus, sets the required macros and has appropriate optimisation settings.

Select Project->Properties from the main menu. A large form with sections down the left will appear. Make the following changes.

  1. C/C++ build -> Settings -> ARM Sourcery Windows GCC C++ Compiler -> Preprocessor.

    Add the following three symbols.

    STM32F10X_HD
    HSE_VALUE=8000000
    SYSCLK_FREQ_72MHz=72000000
    

    Modify the 8000000 (8Mhz) constant to fit whatever value your external oscilator is set to. For a 72Mhz device, 8Mhz is correct.

  2. C/C++ build -> Settings -> ARM Sourcery Windows GCC C++ Compiler -> Optimization.

    Check the function sections and data sections boxes. These settings cause the compiler to create a uniquely named code or data section for every function and data item in your code. Then, when the linker comes to assemble the program we tell it to discard unused sections, thus making your output binary as small as it can be.

  3. Now go back and repeat steps (1) and (2) for the ARM Sourcery Windows GCC C Compiler section. Heaven forbid I won’t be writing any C code but the ST firmware library is C and we must ensure that it compiles correctly.

  4. C/C++ build -> Settings -> ARM Sourcery Windows GCC C++ Linker -> General.

    Check Remove unused sections and enter the file name of the linker script for your project.

    Hang on a minute, what’s a linker script? A linker script tells the linker how to assemble the different parts of your program, which order they should go in and at what memory locations. If this is the first time that you’ve done embedded development then the chances are you’ve never seen one before. They’ve always existed it’s just that they’re usually hidden from you by your tools.

    Unfortunately because devices like the stm32 come in such a myriad of memory configurations it’s necessary for us to supply our own script. The following example shows the linker script that I use for my 512Kb/64Kb STM32F10ZET6. You can customise the FLASH and RAM sections of you have a different memory configuration.

    [expand title=”Click to show Linker.ld” swaptitle=”Click to hide Linker.ld”]

    ENTRY(Reset_Handler)
    
    _estack = 0x2000FFFF;     /* end of $(RAM_LENGTH) RAM */
    
    _Min_Heap_Size = 0;       /* required amount of heap  */
    _Min_Stack_Size = 4K;     /* required amount of stack */
    
    MEMORY
    {
      FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 512K
      RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 64K
      MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
    }
    
    SECTIONS
    {
      /* The startup code goes first into FLASH */
      .isr_vector :
      {
        . = ALIGN(4);
        KEEP(*(.isr_vector))
        . = ALIGN(4);
      } >FLASH
    
      /* The program code and other data goes into FLASH */
      .text :
      {
        . = ALIGN(4);
        *(.text)           /* .text sections (code) */
        *(.text*)          /* .text* sections (code) */
        *(.rodata)         /* .rodata sections (constants, strings, etc.) */
        *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
        *(.glue_7)         /* glue arm to thumb code */
        *(.glue_7t)        /* glue thumb to arm code */
      *(.eh_frame)
    
        KEEP (*(.init))
        KEEP (*(.fini))
    
        . = ALIGN(4);
        _etext = .;        /* define a global symbols at end of code */
      } >FLASH
    
    
       .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
        .ARM : {
        __exidx_start = .;
          *(.ARM.exidx*)
          __exidx_end = .;
        } >FLASH
    
      .preinit_array     :
      {
        PROVIDE_HIDDEN (__preinit_array_start = .);
        KEEP (*(.preinit_array*))
        PROVIDE_HIDDEN (__preinit_array_end = .);
      } >FLASH
      .init_array :
      {
        PROVIDE_HIDDEN (__init_array_start = .);
        KEEP (*(SORT(.init_array.*)))
        KEEP (*(.init_array*))
        PROVIDE_HIDDEN (__init_array_end = .);
      } >FLASH
      .fini_array :
      {
        PROVIDE_HIDDEN (__fini_array_start = .);
        KEEP (*(.fini_array*))
        KEEP (*(SORT(.fini_array.*)))
        PROVIDE_HIDDEN (__fini_array_end = .);
      } >FLASH
    
      /* used by the startup to initialize data */
      _sidata = .;
    
      /* Initialized data sections goes into RAM, load LMA copy after code */
      .data : AT ( _sidata )
      {
        . = ALIGN(4);
        _sdata = .;        /* create a global symbol at data start */
        *(.data)           /* .data sections */
        *(.data*)          /* .data* sections */
    
        . = ALIGN(4);
        _edata = .;        /* define a global symbol at data end */
      } >RAM
    
      /* Uninitialized data section */
      . = ALIGN(4);
      .bss :
      {
        /* This is used by the startup in order to initialize the .bss secion */
        _sbss = .;         /* define a global symbol at bss start */
        __bss_start__ = _sbss;
        *(.bss)
        *(.bss*)
        *(COMMON)
    
        . = ALIGN(4);
        _ebss = .;         /* define a global symbol at bss end */
        __bss_end__ = _ebss;
      } >RAM
    
      PROVIDE ( end = _ebss );
      PROVIDE ( _end = _ebss );
    
      /* User_heap_stack section, used to check that there is enough RAM left */
      ._user_heap_stack :
      {
        . = ALIGN(4);
        . = . + _Min_Heap_Size;
        . = . + _Min_Stack_Size;
        . = ALIGN(4);
      } >RAM
    
      /* MEMORY_bank1 section, code must be located here explicitly            */
      /* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
      .memory_b1_text :
      {
        *(.mb1text)        /* .mb1text sections (code) */
        *(.mb1text*)       /* .mb1text* sections (code)  */
        *(.mb1rodata)      /* read-only data (constants) */
        *(.mb1rodata*)
      } >MEMORY_B1
    
      /* Remove information from the standard libraries */
      /DISCARD/ :
      {
        libc.a ( * )
        libm.a ( * )
        libgcc.a ( * )
      }
    
      .ARM.attributes 0 : { *(.ARM.attributes) }
    }
    

    [/expand]

  5. C/C++ build -> Settings -> ARM Sourcery Windows GCC C++ Linker -> Libraries.

    Add ‘stm32plus-{version}-debug’ to the ‘Libraries (-l)’ box. Replace {version} with the version of stm32plus that you are using. Add the stm32plus ‘lib’ directory to the ‘Library search path (-L)’ box.

C++ tweaks

The default GNU C++ compiler that ships with CodeSourcery g++ is not optimised for embedded development. By default it will pull in 10’s of K of un-necessary code as well as not having default support for the essential new and delete operators. We solve all these problems with one source code file.

Add a new C++ file to your project and call it LibraryHacks.cpp. Here’s what to put in it:


/*
 * LibraryHacks.cpp
 *
 *  Created on: 23 Jan 2011
 *      Author: Andy
 */

#include <cstdlib>
#include <sys/types.h>


/*
 * The default pulls in 70K of garbage
 */

namespace __gnu_cxx {

  void __verbose_terminate_handler() {
    for(;;);
  }
}


/*
 * The default pulls in about 12K of garbage
 */

extern "C" void __cxa_pure_virtual() {
  for(;;);
}


/*
 * Implement C++ new/delete operators using the heap
 */

void *operator new(size_t size) {
  return malloc(size);
}

void *operator new[](size_t size) {
  return malloc(size);
}

void operator delete(void *p) {
  free(p);
}

void operator delete[](void *p) {
  free(p);
}


/*
 * sbrk function for getting space for malloc and friends
 */

extern int  _end;

extern "C" {
  caddr_t _sbrk ( int incr ) {

    static unsigned char *heap = NULL;
    unsigned char *prev_heap;

    if (heap == NULL) {
      heap = (unsigned char *)&_end;
    }
    prev_heap = heap;
    /* check removed to show basic approach */

    heap += incr;

    return (caddr_t) prev_heap;
  }
}

System startup files

Two more boilerplates and we’re ready to write some real code! The ST framework needs an assembly language file ‘Startup.S’ that contains the code that will be executed straight after reset that does some basic initialisation before calling the ‘SystemInit’ function in ‘System.c’. System.c contains the code that starts the stm32 core clocks.

Startup.S (note the upper-case .S, eclipse won’t recognise it as an assembly code file if the case is not correct). This file is taken straight from the ST standard peripheral library and is appropriate for the HD (high-density) line of MCUs

[expand title=”Click to show Startup.S” swaptitle=”Click to hide Startup.S”]

/**
 ******************************************************************************
 * @file      startup_stm32f10x_hd.s
 * @author    MCD Application Team
 * @version   V3.3.0
 * @date      04/16/2010
 * @brief     STM32F10x High Density Devices vector table for RIDE7 toolchain.
 *            This module performs:
 *                - Set the initial SP
 *                - Set the initial PC == Reset_Handler,
 *                - Set the vector table entries with the exceptions ISR address
 *                - Configure the clock system and the external SRAM mounted on
 *                  STM3210E-EVAL board to be used as data memory (optional,
 *                  to be enabled by user)
 *                - Branches to main in the C library (which eventually
 *                  calls main()).
 *            After Reset the Cortex-M3 processor is in Thread mode,
 *            priority is Privileged, and the Stack is set to Main.
 *******************************************************************************
 * @copy
 *
 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 *
 * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
 */

  .syntax unified
  .cpu cortex-m3
  .fpu softvfp
  .thumb

.global  g_pfnVectors
.global  Default_Handler

/* start address for the initialization values of the .data section.
defined in linker script */
.word  _sidata
/* start address for the .data section. defined in linker script */
.word  _sdata
/* end address for the .data section. defined in linker script */
.word  _edata
/* start address for the .bss section. defined in linker script */
.word  _sbss
/* end address for the .bss section. defined in linker script */
.word  _ebss
/* stack used for SystemInit_ExtMemCtl; always internal RAM used */

.equ  BootRAM,        0xF1E0F85F
/**
 * @brief  This is the code that gets called when the processor first
 *          starts execution following a reset event. Only the absolutely
 *          necessary set is performed, after which the application
 *          supplied main() routine is called.
 * @param  None
 * @retval : None
*/

    .section  .text.Reset_Handler
  .weak  Reset_Handler
  .type  Reset_Handler, %function
Reset_Handler:

/* Copy the data segment initializers from flash to SRAM */
  movs  r1, #0
  b  LoopCopyDataInit

CopyDataInit:
  ldr  r3, =_sidata
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4

LoopCopyDataInit:
  ldr  r0, =_sdata
  ldr  r3, =_edata
  adds  r2, r0, r1
  cmp  r2, r3
  bcc  CopyDataInit
  ldr  r2, =_sbss
  b  LoopFillZerobss
/* Zero fill the bss segment. */
FillZerobss:
  movs  r3, #0
  str  r3, [r2], #4

LoopFillZerobss:
  ldr  r3, = _ebss
  cmp  r2, r3
  bcc  FillZerobss
/* Call the clock system intitialization function.*/
  bl  SystemInit
/* Call the application's entry point.*/
  bl  main
  bx  lr
.size  Reset_Handler, .-Reset_Handler

/**
 * @brief  This is the code that gets called when the processor receives an
 *         unexpected interrupt.  This simply enters an infinite loop, preserving
 *         the system state for examination by a debugger.
 * @param  None
 * @retval None
*/
    .section  .text.Default_Handler,"ax",%progbits
Default_Handler:
Infinite_Loop:
  b  Infinite_Loop
  .size  Default_Handler, .-Default_Handler
/******************************************************************************
*
* The minimal vector table for a Cortex M3. Note that the proper constructs
* must be placed on this to ensure that it ends up at physical address
* 0x0000.0000.
*
*******************************************************************************/
   .section  .isr_vector,"a",%progbits
  .type  g_pfnVectors, %object
  .size  g_pfnVectors, .-g_pfnVectors


g_pfnVectors:
  .word  _estack
  .word  Reset_Handler
  .word  NMI_Handler
  .word  HardFault_Handler
  .word  MemManage_Handler
  .word  BusFault_Handler
  .word  UsageFault_Handler
  .word  0
  .word  0
  .word  0
  .word  0
  .word  SVC_Handler
  .word  DebugMon_Handler
  .word  0
  .word  PendSV_Handler
  .word  SysTick_Handler
  .word  WWDG_IRQHandler
  .word  PVD_IRQHandler
  .word  TAMPER_IRQHandler
  .word  RTC_IRQHandler
  .word  FLASH_IRQHandler
  .word  RCC_IRQHandler
  .word  EXTI0_IRQHandler
  .word  EXTI1_IRQHandler
  .word  EXTI2_IRQHandler
  .word  EXTI3_IRQHandler
  .word  EXTI4_IRQHandler
  .word  DMA1_Channel1_IRQHandler
  .word  DMA1_Channel2_IRQHandler
  .word  DMA1_Channel3_IRQHandler
  .word  DMA1_Channel4_IRQHandler
  .word  DMA1_Channel5_IRQHandler
  .word  DMA1_Channel6_IRQHandler
  .word  DMA1_Channel7_IRQHandler
  .word  ADC1_2_IRQHandler
  .word  USB_HP_CAN1_TX_IRQHandler
  .word  USB_LP_CAN1_RX0_IRQHandler
  .word  CAN1_RX1_IRQHandler
  .word  CAN1_SCE_IRQHandler
  .word  EXTI9_5_IRQHandler
  .word  TIM1_BRK_IRQHandler
  .word  TIM1_UP_IRQHandler
  .word  TIM1_TRG_COM_IRQHandler
  .word  TIM1_CC_IRQHandler
  .word  TIM2_IRQHandler
  .word  TIM3_IRQHandler
  .word  TIM4_IRQHandler
  .word  I2C1_EV_IRQHandler
  .word  I2C1_ER_IRQHandler
  .word  I2C2_EV_IRQHandler
  .word  I2C2_ER_IRQHandler
  .word  SPI1_IRQHandler
  .word  SPI2_IRQHandler
  .word  USART1_IRQHandler
  .word  USART2_IRQHandler
  .word  USART3_IRQHandler
  .word  EXTI15_10_IRQHandler
  .word  RTCAlarm_IRQHandler
  .word  USBWakeUp_IRQHandler
  .word  TIM8_BRK_IRQHandler
  .word  TIM8_UP_IRQHandler
  .word  TIM8_TRG_COM_IRQHandler
  .word  TIM8_CC_IRQHandler
  .word  ADC3_IRQHandler
  .word  FSMC_IRQHandler
  .word  SDIO_IRQHandler
  .word  TIM5_IRQHandler
  .word  SPI3_IRQHandler
  .word  UART4_IRQHandler
  .word  UART5_IRQHandler
  .word  TIM6_IRQHandler
  .word  TIM7_IRQHandler
  .word  DMA2_Channel1_IRQHandler
  .word  DMA2_Channel2_IRQHandler
  .word  DMA2_Channel3_IRQHandler
  .word  DMA2_Channel4_5_IRQHandler
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  0
  .word  BootRAM       /* @0x1E0. This is for boot in RAM mode for
                         STM32F10x High Density devices. */
/*******************************************************************************
*
* Provide weak aliases for each Exception handler to the Default_Handler.
* As they are weak aliases, any function with the same name will override
* this definition.
*
*******************************************************************************/

  .weak  NMI_Handler
  .thumb_set NMI_Handler,Default_Handler

  .weak  HardFault_Handler
  .thumb_set HardFault_Handler,Default_Handler

  .weak  MemManage_Handler
  .thumb_set MemManage_Handler,Default_Handler

  .weak  BusFault_Handler
  .thumb_set BusFault_Handler,Default_Handler

  .weak  UsageFault_Handler
  .thumb_set UsageFault_Handler,Default_Handler

  .weak  SVC_Handler
  .thumb_set SVC_Handler,Default_Handler

  .weak  DebugMon_Handler
  .thumb_set DebugMon_Handler,Default_Handler

  .weak  PendSV_Handler
  .thumb_set PendSV_Handler,Default_Handler

  .weak  SysTick_Handler
  .thumb_set SysTick_Handler,Default_Handler

  .weak  WWDG_IRQHandler
  .thumb_set WWDG_IRQHandler,Default_Handler

  .weak  PVD_IRQHandler
  .thumb_set PVD_IRQHandler,Default_Handler

  .weak  TAMPER_IRQHandler
  .thumb_set TAMPER_IRQHandler,Default_Handler

  .weak  RTC_IRQHandler
  .thumb_set RTC_IRQHandler,Default_Handler

  .weak  FLASH_IRQHandler
  .thumb_set FLASH_IRQHandler,Default_Handler

  .weak  RCC_IRQHandler
  .thumb_set RCC_IRQHandler,Default_Handler

  .weak  EXTI0_IRQHandler
  .thumb_set EXTI0_IRQHandler,Default_Handler

  .weak  EXTI1_IRQHandler
  .thumb_set EXTI1_IRQHandler,Default_Handler

  .weak  EXTI2_IRQHandler
  .thumb_set EXTI2_IRQHandler,Default_Handler

  .weak  EXTI3_IRQHandler
  .thumb_set EXTI3_IRQHandler,Default_Handler

  .weak  EXTI4_IRQHandler
  .thumb_set EXTI4_IRQHandler,Default_Handler

  .weak  DMA1_Channel1_IRQHandler
  .thumb_set DMA1_Channel1_IRQHandler,Default_Handler

  .weak  DMA1_Channel2_IRQHandler
  .thumb_set DMA1_Channel2_IRQHandler,Default_Handler

  .weak  DMA1_Channel3_IRQHandler
  .thumb_set DMA1_Channel3_IRQHandler,Default_Handler

  .weak  DMA1_Channel4_IRQHandler
  .thumb_set DMA1_Channel4_IRQHandler,Default_Handler

  .weak  DMA1_Channel5_IRQHandler
  .thumb_set DMA1_Channel5_IRQHandler,Default_Handler

  .weak  DMA1_Channel6_IRQHandler
  .thumb_set DMA1_Channel6_IRQHandler,Default_Handler

  .weak  DMA1_Channel7_IRQHandler
  .thumb_set DMA1_Channel7_IRQHandler,Default_Handler

  .weak  ADC1_2_IRQHandler
  .thumb_set ADC1_2_IRQHandler,Default_Handler

  .weak  USB_HP_CAN1_TX_IRQHandler
  .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler

  .weak  USB_LP_CAN1_RX0_IRQHandler
  .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler

  .weak  CAN1_RX1_IRQHandler
  .thumb_set CAN1_RX1_IRQHandler,Default_Handler

  .weak  CAN1_SCE_IRQHandler
  .thumb_set CAN1_SCE_IRQHandler,Default_Handler

  .weak  EXTI9_5_IRQHandler
  .thumb_set EXTI9_5_IRQHandler,Default_Handler

  .weak  TIM1_BRK_IRQHandler
  .thumb_set TIM1_BRK_IRQHandler,Default_Handler

  .weak  TIM1_UP_IRQHandler
  .thumb_set TIM1_UP_IRQHandler,Default_Handler

  .weak  TIM1_TRG_COM_IRQHandler
  .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler

  .weak  TIM1_CC_IRQHandler
  .thumb_set TIM1_CC_IRQHandler,Default_Handler

  .weak  TIM2_IRQHandler
  .thumb_set TIM2_IRQHandler,Default_Handler

  .weak  TIM3_IRQHandler
  .thumb_set TIM3_IRQHandler,Default_Handler

  .weak  TIM4_IRQHandler
  .thumb_set TIM4_IRQHandler,Default_Handler

  .weak  I2C1_EV_IRQHandler
  .thumb_set I2C1_EV_IRQHandler,Default_Handler

  .weak  I2C1_ER_IRQHandler
  .thumb_set I2C1_ER_IRQHandler,Default_Handler

  .weak  I2C2_EV_IRQHandler
  .thumb_set I2C2_EV_IRQHandler,Default_Handler

  .weak  I2C2_ER_IRQHandler
  .thumb_set I2C2_ER_IRQHandler,Default_Handler

  .weak  SPI1_IRQHandler
  .thumb_set SPI1_IRQHandler,Default_Handler

  .weak  SPI2_IRQHandler
  .thumb_set SPI2_IRQHandler,Default_Handler

  .weak  USART1_IRQHandler
  .thumb_set USART1_IRQHandler,Default_Handler

  .weak  USART2_IRQHandler
  .thumb_set USART2_IRQHandler,Default_Handler

  .weak  USART3_IRQHandler
  .thumb_set USART3_IRQHandler,Default_Handler

  .weak  EXTI15_10_IRQHandler
  .thumb_set EXTI15_10_IRQHandler,Default_Handler

  .weak  RTCAlarm_IRQHandler
  .thumb_set RTCAlarm_IRQHandler,Default_Handler

  .weak  USBWakeUp_IRQHandler
  .thumb_set USBWakeUp_IRQHandler,Default_Handler

  .weak  TIM8_BRK_IRQHandler
  .thumb_set TIM8_BRK_IRQHandler,Default_Handler

  .weak  TIM8_UP_IRQHandler
  .thumb_set TIM8_UP_IRQHandler,Default_Handler

  .weak  TIM8_TRG_COM_IRQHandler
  .thumb_set TIM8_TRG_COM_IRQHandler,Default_Handler

  .weak  TIM8_CC_IRQHandler
  .thumb_set TIM8_CC_IRQHandler,Default_Handler

  .weak  ADC3_IRQHandler
  .thumb_set ADC3_IRQHandler,Default_Handler

  .weak  FSMC_IRQHandler
  .thumb_set FSMC_IRQHandler,Default_Handler

  .weak  SDIO_IRQHandler
  .thumb_set SDIO_IRQHandler,Default_Handler

  .weak  TIM5_IRQHandler
  .thumb_set TIM5_IRQHandler,Default_Handler

  .weak  SPI3_IRQHandler
  .thumb_set SPI3_IRQHandler,Default_Handler

  .weak  UART4_IRQHandler
  .thumb_set UART4_IRQHandler,Default_Handler

  .weak  UART5_IRQHandler
  .thumb_set UART5_IRQHandler,Default_Handler

  .weak  TIM6_IRQHandler
  .thumb_set TIM6_IRQHandler,Default_Handler

  .weak  TIM7_IRQHandler
  .thumb_set TIM7_IRQHandler,Default_Handler

  .weak  DMA2_Channel1_IRQHandler
  .thumb_set DMA2_Channel1_IRQHandler,Default_Handler

  .weak  DMA2_Channel2_IRQHandler
  .thumb_set DMA2_Channel2_IRQHandler,Default_Handler

  .weak  DMA2_Channel3_IRQHandler
  .thumb_set DMA2_Channel3_IRQHandler,Default_Handler

  .weak  DMA2_Channel4_5_IRQHandler
  .thumb_set DMA2_Channel4_5_IRQHandler,Default_Handler

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

[/expand]

System.c. This contains the clock startup code. The following file is appropriate for 72Mhz devices.

[expand title=”Click to show System.c” swaptitle=”Click to hide System.c”]

/*
 * System.c
 *
 *  Created on: 8 Oct 2011
 *      Author: Andy
 */

#include "stm32f10x_rcc.h"
#include "stm32f10x_flash.h"

static volatile ErrorStatus HSEStartUpStatus=SUCCESS;

uint32_t SystemCoreClock=SYSCLK_FREQ_72MHz;

void SystemInit() {
  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration
   RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus=RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS) {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);

    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);

    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

    /* Enable PLL */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08) {
    }
  }

}

[/expand]

The blink source file

Finally we’re ready to create the source file that will execute the blink test. Create a new C++ source file in eclipse, call it ‘blink.cpp. Here it is:


/*
 * blink.cpp
 *
 *  Created on: 18 Dec 2011
 *      Author: Andy
 */

#include "gpio/GpioPort.h"
#include "timing/MillisecondTimer.h"

using namespace stm32plus;


/*
 * Blink class, turns an LED on and off at 1 second
 * intervals. Assumes that the LED is connected
 * through PF6.
 */

class BlinkTest {

  public:

    void run() {

      GpioPort portf(GPIOF);

      portf[6].initialise(GPIO_Speed_50MHz,GPIO_Mode_Out_PP);

      for(;;) {

        portf[6].set();
        MillisecondTimer::delay(1000);

        portf[6].reset();
        MillisecondTimer::delay(1000);
      }
    }
};


/*
 * Main entry point
 */

int main() {

  // set up SysTick at 1ms resolution
  MillisecondTimer::initialise();

  BlinkTest test;
  test.run();

  // not reached
  return 0;
}

Note how there are no fiddly peripheral clock initialisers to remember. stm32plus will do all of that for you in the library code.

This sample introduces a few core stm32plus features:

  • The MillisecondTimer class. Use the static methods on this class to perform millisecond resolution timings. You must call the initialise static member function once so that the cortex SysTick timer is started.

  • GPIO port manipulation through the GpioPort and Gpio objects. Note how you can address the pins using the convenient [] array operator. Again, no need to remember peripheral clocks as this is done for you in the GpioPort constructor.

Over the coming period I will introduce many more features of stm32plus by example including complete LCD and OLED drivers.

License

Copyright (c) 2011, Andrew Brown. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of stm32plus nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANDREW BROWN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  • Adolfo

    Hi, how are you ?, I have tried to compile your blink project, but I cant can you be more specific with this

    C/C++ build -> Settings -> ARM Sourcery Windows GCC C++ Linker -> Libraries.

    you said I must add ‘stm32plus-{version}-debug’ to the ‘Libraries (-l)’ box. Replace {version} with the version of stm32plus that you are using.

    I put this one ;

    C:Documents and SettingsadoMy Documentsstm32_eclipsestm32stm32plus-1.1.3stm32plus

    and add the stm32plus ‘lib’ directory to the ‘Library search path (-L)’ box. Which libraries must be added
    your libraries or stm32 Firmware libraries.

    In my case I added this libraries

    "C:Documents and SettingsadoMy Documentsstm32_eclipsestm32stm32plus-1.1.3stm32plusfwlibf1stdperiphinc"
    "C:Documents and SettingsadoMy Documentsstm32_eclipsestm32stm32plus-1.1.3stm32plussrc"
    "C:Documents and SettingsadoMy Documentsstm32_eclipsestm32stm32plus-1.1.3stm32plusfwlib"
    "C:Documents and SettingsadoMy Documentsstm32_eclipsestm32stm32plus-1.1.3stm32plus"
    "C:Documents and SettingsadoMy Documentsstm32_eclipsestm32stm32plus-1.1.3stm32plusinclude"

    Please, can you help me, I dont understand which libraries.
    Best Regards.

    • The "-l" box at the top should have the name (not the full pathname) of the library. So if you've downloaded v1.1.3 then the correct name for a debug build would be: "stm32plus-1.1.3-debug" (without the quotes). It is the name of the library file without the ".a" on the end and without the "lib" on the front.

      The "-L" box adds directories for the linker to search when it's looking for libraries (.a files). So you need to enter the full pathname of the directory where you can find the "libstm32plus-1.1.3-debug.a" file on your system.

      PS. I hope the tools are happy with pathnames that include spaces such as the ones you are using. These tools are ports from Unix systems which generally do not like pathnames with spaces. You may be OK though, I have played it safe and avoided such pathnames.

  • Joe

    Hi,
    how do I use the timers with stm32plus?

    thank you,
    Joe

  • simeon

    Hi, I have an error on make:
    /tmp/ccWgNnxM.s:512: Error: registers may not be the same — `strexb r0,r0,[r1]'

    using gcc version 4.6.3 (Sourcery CodeBench Lite 2012.03-56)

    What am I missing here?

    • I did a little google research on that error message and it looks like a problem with the CMSIS peripheral library. There are documented workarounds, including changing your compiler optimisation level. If there's any fix I can do to the CMSIS library code in the stm32plus package then I'll include it in the next release.

      • simeon

        I edited the compiler flags and added -O0 , -O1 and -O2 in Makefile.inc. With no optimization the compiler succeeded. with -O1 and -O2 I got the same error. (It also looks like the comiler optimization in Makefile.inc overwrites the optimization values in submodules). Using no optimization doesn't seem to be a solution, so maybe I can dig deeper when I find the time (==never? ;)). Sorry I can't provide a patch by now.

        • The optimisation settings I use are -O0 (debug), -Os (small) and -O3 (fast) and have never seen the error. Compiler version used is 4.5.2 for me. I'll try -O2 and see if that triggers the error.

          • Yuri

            I use Sourcery G++ Lite 2012.09 toolchain and have same problem. I think that is a compiler bug. When i use older version of SourceryG++ lite 2011.03 (4.5.2) the problem disappeared.
            PS. I work on Sabayon x64. May be you have 64-bit OS? But I'm not sure what the problem with this

    • Here's a patch that should fix the problem:
      http://bin.longlandclan.yi.org/index.cgi/20121201

      Here's the bug report that the patch is based on:
      https://github.com/texane/stlink/issues/65

  • Andrey B

    Congratulations on version 1.2.0 and thanks for sharing all the great work. I believe there should be a 'Donate' button somewhere 🙂

    What are your plans on compatibility with say F4? I have looked into porting the code to work with LCD screens and seems like it's quite doable (still waiting for the actual screen to show up so that I can test it). Would you be open to applying my patch?

    Not sure what is the best approach to make it F4 compatible, but as a step may I suggest encapsulating GPIO mode since it's implemented differently between F1 and F4? (one combined field in F1 and two separate fields in F4). So instead of GPIOMode_TypeDef parameter library specific STM32_GPIOMode could be implemented. But as I said, I am not sure if that's the right way to go – maybe a simple #ifdef STM32F14XX_HD pre-processor branching should be used in all gpio initializations within the library.

    • Hi Andrey, thank you for your comments. Work on F2 and F4 compatibility has started already and the new LCD interfaces should give some idea of the direction I'm taking.

      The peripheral interface classes are moving to template implementations parameterised by the MCU type. Where the peripherals are identical across the range then a generic implementation will suffice. Where they differ (e.g. GPIO) then there will be specialisations for each MCU. Using templates means that much of the library customises itself to your use-case at compile time and should be more efficient.

      If I get it right then just one #define will be enough to select the correct ST peripheral library and also select all the correct templates. One nice side effect of this restructuring is that all the AF pin remappings for each peripheral are going to be supported.

  • Emmanuel

    Hi Andy, Great Lib, however I can't compile it with EWARM, can you give me some tips? I get to many errors and asm invalid instructions :s Regards

    • Hi Emmanuel, the CodeSourcery g++ toolchain is the only one that I support.

  • oh thanks that answered my question too

  • Stuart Longland

    Hi,

    Just out of interest, what version of CodeSourcery do you use? I've been trying to compile the ili9325 (running it on identical hardware to yourself) and not having much luck — the JPEG photo often comes up as a garbled mess, or nothing at all, and it always seems to hard lock at the displaying Pi stage.

    Presently, I'm using a Gentoo Crossdev-generated toolchain; I've tried gcc 4.7.2, 4.6.3 and am just building 4.5.2.

    • Hi Stuart, I'm using gcc version 4.5.2 (Sourcery G++ Lite 2011.03-42). Did you try uploading one of the pre-built images? They are tested working so if they don't work then that would mean a hardware issue.

  • CABrouwers

    Thanks a lot for developing this library. I tested it today and ran into an issue with the blink led example. The led would not blink if I used any code optimization option! Long story short, I traced the problem to the variable MillisecondTimer::_counter . It should be defined as volatile. Otherwise the code optimizer assumes its value is fixed and thus creates a well optimized never end loop! By the way, I use Crossworks, which is based on the GNU toolchain.

  • Rick

    Do you have nand flash library included?tx

    • Hi, an FsmcNand class is provided to help setting up the FSMC for communicating with a NAND flash device. A while back when I looked into these devices I thought it might be a good idea to provide a BlockDevice implementation for NAND so you could run a file system but couldn't find a suitable wear-leveling algorithm that didn't need loads of RAM to run. Without wear-levelling a file system would quickly wear out the NAND device.

      • Rick

        I saw on examples directory "fsmc_sram", is it the one ?
        thanks

  • jacky

    How can I build and debug examples in Eclipse?

    • At the time of writing you need to install Kepler, CDT, GNU Arm Eclipse Plugin and the GNU Tools for ARM embedded processors toolchain. When that’s done you can import the main library .project as an “existing project” into your workspace. Build it, then you can import and build the examples. Each one has its own “.project” that you can import. If you run into trouble then please start a thread in the forum and I’ll do my best to help.

  • TonyR

    Great work, but I wonder why C++ for embedded systems ? (Yes I know some people put there even Python, .NET, java, but…. )
    Here are the cons: 1) The memory management during processing slows down the applications; 2) Object methods calling is slower also; 3) In a MCU we have limited resources, mostly we have 1 instance of something (SPI0, UART1, ring buffer0) and these would be singletons, otherwise the possible heap fragmentation just begs for trouble…
    Pros: Using classes instead of modules with static functions improves code readability, ….