This blog has permanently moved to

Contact Form | Email Address

© 2018 All rights reserved by Boseji

Thank you for all your support.

Thursday, October 4, 2012

First Steps with STM32 Board

We were trying to get our new STM32 board working and in the pursuit of that we cane to know some interesting insights that are worth sharing.
This particular board contains the STM32F103VE series chip from ST Microelectronics. This chip is a Cortex-M3 with 72MHz clocking and quite impressive peripheral line up. We did not select this board, rather it was borrowed from one of our close friends. We wanted to evaluate the low power and speed features of this nice Cortex-M3 device.

To start with we first need the Toolchain.
There are the following options available:
1. Keil ARM IDE: http://www.keil.com/arm/mdk.asp
We basically used the trial version of this IDE, and later switched to arm-gcc.
2. Arm-GCC from GNU Tools : https://launchpad.net/gcc-arm-embedded/
We are yet to evaluate this fully. Possibly integrating this with uVision4 is a good idea since that option is available in the Keil MDK Arm version 4.5x onwards.
3. Coocox IDE: http://www.coocox.com/CooCox_CoIDE.htm
This is the best and easy to use IDE we found however this needs the Arm-GCC again.

Both the above are freely available and easy to install.

Next comes the Debugger:
1. ARM-USB-OCD from Olimex: https://www.olimex.com/Products/ARM/JTAG/ARM-USB-OCD/
We already had this debugger so we used it, however there are many cheaper choices available.
2. Amontec JTAGkey-Tiny : http://www.amontec.com/jtagkey-tiny.shtml
This too works, we have tested this also in our setup.
This list can go on and we would stop at above two as they are test one.

Now comes some actual coding. We would like to outline the steps that we took to reach to the basic blinky program.
1. After reading the internals from the STM32 manual we found that there is clock gating available at the AHB level. So in order to make the GPIO work we need to have the clock for that port enabled.
We found that the "RCC" peripheral has a register "RCC_APB2ENR":

This is used to enable the I/O section clock by setting the particular bit.
We had the LED on the board at PORTD at Pin 2 so we needed to activate the "IOPDEN" bit in the RCC_APB2ENR register.

 #include <stm32f10x.h>

Also in the Keil project dont forget to define the constant "STM32F10X_MD" else the code would not compile.

2. Now we also need to configure the Port Direction. For PORTD that register is called as "GPIOD_CRL".
Now this register shows the settings for only the Lower 8 pins of the PORTD. There is another register "GPIOD_CRH" that represents the Higher 8 pins of the PORTD. However is our package of the STM32 the higher order pins as well as a few lower order pins were missing. This must be taken into consideration while making the settings. The settings are in two folds - First the configuration "CNFx" and then the Mode "MODEx" for each of the pins. The bit patterns correspond to the following table:
For us we wanted to configure PD2 to Output with the General Purpose Input Output mode, so:
MODE2 = 11 For Maximum Speed as Output
CNF2 = 0 For General Purpose
Hence We set the register as:

GPIOD->CRL = 0x44444344;//Taking clue from the default reset value

3. We need to make the LED blink. To do that we need to write to the LED pin of the driving GPIO pin PD2. To do this STM32 offers several means. The first is directly writing to the output data register "GPIOD_ODR" in the corresponding pin. However this method is not fast. The next is to write to the Set/Reset register that is "GPIOD_BSRR", this would allow it to toggle the Pins by writing to this register. Thats exactly what we want to do while blinking.
We first made the Pin to high using the "GPIOD_ODR" and then toggle it using the "GPIOD_BSRR".

GPIOD->ODR |= (1<<2);
   GPIOD->BSRR |= (1<<2);
   for(i=0 ; i<0x1FFFF ; i++) __nop();

Here is the complete program:

#include <stm32f10x.h>
void SystemInit(void)//Dummy function
int main()
    unsigned long i;
    GPIOD->CRL = 0x44444344;
    GPIOD->ODR &= ~(1 << 2); // ON
        GPIOD->BSRR |= (1 << 2);
    //return 0;

Hope that this would be helpful for those who wish to start on the STM32.

No comments:

Post a Comment