Autogenerating STM Code

ST has developed several tools for writing STM code quickly. The main example is autogenerated code via STMCubeMX, which lets you select which chip or board you're using, the clock, and pin configurations. CubeMX can then autogenerate the init functions and clock configurations for your project. You can even generate it with FreeRTOS! However, autogenerated code can be hard to read and can be very hard to modify. We'd mostly use generating code for reference, quick hardware testing, and getting the HAL libraries for a chip.


Downloads:


Creating your project:

  • Open CubeMX
  • If you are trying to generate code for only a chip press "Access to MCU Selector" or "Access to Board Selector" if you need to generate code for a specific development board 
    • The rest of the article will be about "MCU selector" since that is primarily what we're doing
  • Select your MCU in the "Commercial Part Number" section
  • Press "Start Project"


Configuring Your Project:

  • In the pinout menu, you can hover over a pin and select what functionality you want that pin to have (I2C, SPI, CAN, GPIO)

  • Don't touch "Clock Configuration" unless you know exactly what you're doing
  • Add a project name, project location, and make sure to set Toolchain/IDE to Makefile 
  • Keep Code Generator the same
  • Keep Advanced Settings the same
  • Press Generate Code

Getting the HAL:

  • Open the folder with your project
  • Go to the Drivers folder
  • The HAL is in the STM32[whatever your chip is]xx_HAL_Driver folder
    • For example, STM32L4xx_HAL_Driver


Adding chips to Embedded Sharepoint:

  • Follow this as an example of which files you need in BSP
  • Follow this as an example of which files you need in CMSIS
    • You only need the startup script specific to your chip
  • Make sure to follow the same directory structure