Stm32 Peripheral Drivers from Scartch

Posted by Eddie on August 06, 2016

Series Introduction

What is a microcontroller(µC) without its peripherals?  There is not much you can do, if anything at all, with a µC if you do not know how to use the GPIO,USART, I2C,SPI,CAN, TIMERS etc...

So what better way to teach yourself how to use a µC than to attempt to write register level drivers for the peripherals.

I have always steered away from using third party libraries when attempting to learn how to program a certain µC especially the vendor specific libraries for 2 reasons.
One being the fact that you dont learn much from calling a function that sets everything up for you, unless you wrote the function yourself. Reason two is that the vendor specific libraries have layer after layer of abstraction. Trying to work backwards from a function call to figure out how it works is tedious, doable but tedious. On another note those libraries tend to make your program size a lot bigger than necessary, almost 3 times times bigger.

To not misconstrued things I will clear up that for fast prototyping and getting a project up and off the ground and if you have a µC with enough memory then yes the libraries are easy peezy to use and get things going. However my goal is to become an embedded systems engineer of some sort, so relying on fat libraries will not benefit me in the future when I am asked about something like how the SPI protocol works and I have no idea because all I ever did was call an  enable_SPI() ;  function.

This series is NOT an intro to MicroControllersI will not explain things like what is a PIN what does HIGH or LOW state on a pin means..etc.. if you are looking for that type of introduction this is not it and I will perhaps work on a series of that type on a later date. This series is to show you how to use this specific Microcontroller (STM32F103C8) and related MicroControllers from the STM32 family. As well as provide an introduction to the Cortex core.

Parts and Software

For this series I will be using the STM32F103C8 µC pictured above. It is dirt cheap, about $2.99 on ebay just search for STM32F103  there is another version that looks a little different, square in shape with female headers Arduino style, either way its the same thing. This µC sports an ARM Cortex M3 core and maxes out at 72Mhz. 
Full specs:

Key Features
  • ARM® 32-bit Cortex® -M3 CPU Core
    • 72 MHz maximum frequency,1.25 DMIPS/MHz (Dhrystone 2.1) performance at 0 wait state memory access
    • Single-cycle multiplication and hardware division
  • Memories
    • 64 or 128 Kbytes of Flash memory
    • 20 Kbytes of SRAM
  • Clock, reset and supply management
    • 2.0 to 3.6 V application supply and I/Os
    • POR, PDR, and programmable voltage detector (PVD)
    • 4-to-16 MHz crystal oscillator
    • Internal 8 MHz factory-trimmed RC
    • Internal 40 kHz RC
    • PLL for CPU clock
    • 32 kHz oscillator for RTC with calibration
  • Low-power
    • Sleep, Stop and Standby modes
    • VBAT supply for RTC and backup registers
  • 2 x 12-bit, 1 μs A/D converters (up to 16 channels)
    • Conversion range: 0 to 3.6 V
    • Dual-sample and hold capability
    • Temperature sensor
  • DMA
    • 7-channel DMA controller
    • Peripherals supported: timers, ADC, SPIs, I2 Cs and USARTs
  • Up to 80 fast I/O ports
    • 26/37/51/80 I/Os, all mappable on 16 external interrupt vectors and almost all 5 V-tolerant
  • Debug mode
    • Serial wire debug (SWD) & JTAG interfaces
  • 7 timers
    • Three 16-bit timers, each with up to 4 IC/OC/PWM or pulse counter and quadrature (incremental) encoder input
    • 16-bit, motor control PWM timer with dead-time generation and emergency stop
    • 2 watchdog timers (Independent and Window)
    • SysTick timer 24-bit downcounter
  • Up to 9 communication interfaces
    • Up to 2 x I2 C interfaces (SMBus/PMBus)
    • Up to 3 USARTs (ISO 7816 interface, LIN, IrDA capability, modem control)
    • Up to 2 SPIs (18 Mbit/s)
    • CAN interface (2.0B Active)
    • USB 2.0 full-speed interface
  • CRC calculation unit, 96-bit unique ID
  • Packages are ECOPACK®

 You will need a programming unit pictured below which cost about $4 on ebay Just search ST-link V2 the color does not matter:

Another thing you will need is the datasheet for the STM32 which you can download HERE , when you click that link scroll down to "Reference Manual" and download the latest version of RM00008.

Next you will need to download Keil's µVision software. They have free version with code size limit on it, for learning purposes this will suffice and once you learn you can port your skills over to a different IDE. 
You can register HERE to download their free software, at the bottom select the ST and Cortex M3 and M4 options as well as any other chips you may want to experiment with. 

Once you have that up and running watch the following video where I show you how to configure the IDe to work with this programmer and this specific µC.

Now that we have the IDE setup we can start programming, I will start with the GPIO because it is the obvious place to start since all other peripherals depend on the GPIO, along with GPIO I will show you how to enable the clocks for specific peripherals and anything else that may present itself along the way. This series is to be read and watched alongside the reference manual because this is where I get and show you the information from.

NEXT: GPIO register overview >>