samedi 16 mars 2013

Blinky with STM32F4DISCOVERY board

In this tutorial I'm going to show you one of the most used IP in any ST microcontroller the GPIO. What you need to know witch could be obvious to some that we are actually going to program the microcontroller in the board and not the board!!

The microcontroller of the STM32F4DISCOVERY board is STM32F407VGT ==> Datasheet

I'm using coocox for developing and it's an eclipse based IDE very friendly and intuitive.

The GPIO IP:
As I said it stands for Gneral purpose Input Output,
Each of the GPIO pins can be configured by software as output (push-pull or open-drain,
with or without pull-up or pull-down), as input (floating, with or without pull-up or pull-down)
or as peripheral alternate function. Most of the GPIO pins are shared with digital or analog
alternate functions. All GPIOs are high-current-capable and have speed selection to better
manage internal noise, power consumption and electromagnetic emission.
The I/O configuration can be locked if needed by following a specific sequence in order to
avoid spurious writing to the I/Os registers.
Fast I/O handling allowing maximum I/O toggling up to 84 MHz.
(From datasheet)

There are 5 GPIOs in the STM32 microcontroller (GPIOA,GPIOB,GPIOC,GPIOD,GPIOE) every GPIO has 16 configurable pin and each has 7 registers:
Two are used to configure the sixteen port bits individually, (CRL,CRH)
two are used to read/write the sixteen port bits in parallel, (ODR,IDR)
two are used to set/reset the sixteen port bits individually, (BSRR,BRR)
and one is used to implement a “locking sequence” that is intended to prevent rogue code from accidentally modifying the port configuration (LCKR)

First start new project choose chip (ST-STM32F407VG) . Later on the repositry window will appear, we will do some blinking so we definitely need the GPIO, click on the GPIO check box, RCC, CMSIS boot and M4 CMSIS Core are automatically checked.



In the project tree double click on main.c

GPIO as output :

What are we going to do is to make the leds embedded on the board to blink together every 1sec but to do so we need to know to witch pin and witch GPIO these leds are connected in our STM32F4DISCOVERY board.

According to the board schematics in pag 6
The leds are connected to (PD12/PD13/PD14/PD15) the GPIO used is then GPIOD and the pins are (12, 13, 14, 15)

So what we need to do is explained in the following flowchart:

The program is :
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"

GPIO_InitTypeDef GPIO_InitStructure;

void Delay(__IO uint32_t nCount);
/**
* @brief GPIO pin toggle program
* @param None
* @retval None
*/
void main(void)
{
/* GPIOD Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);

while (1)
{
/* LEDZ on */
GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
/* Insert delay */
Delay(0xFFFFF);
/* LEDZ off */
GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
/* Insert delay */
Delay(0xFFFFF);
}
}

/**
* @brief Delay Function.
* @param nCount:specifies the Delay time length.
* @retval None
*/
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}

GPIO as INPUT : 
Say that we need to read input from the outside world like buttons or switches we have to use the GPIO too.
In this case we are going to read the embedded button in the STM32F4DISCOVERY board.
 According to the board schematics this button is connected to the PA0 (GPIOA, pin 0).
What are we going to do is to set the leds on when the button is set and resetting them when the button is not set.

But we need to configure the GPIOA and the pin 0 as input before we can read data from it.

The program :


#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"

GPIO_InitTypeDef GPIO_InitStructure_Button;
GPIO_InitTypeDef GPIO_InitStructure;

void main(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//for buttons

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure_Button.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure_Button.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure_Button.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStructure_Button);
 int i;
        while(1){
                i = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
                GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15,i);
        }
}







STM32F4DISCOVERY board

I got my STM32F4DISCOVERY board a few days ago and I'm so excited to try it.
This board is definitely one and unique board made by st for amateurs and professional to discover easily and with pleasure the strength of the STM32F4 series.

The board is really rich with features (copy past from the STM32F4DISCOVERY datasheet)
  1. STM32F407VGT6 microcontroller featuring 32-bit ARM Cortex-M4F core, 1 MB Flash, 192 KB RAM in an LQFP100 package
  2. On-board ST-LINK/V2 with selection mode switch to use the kit as a standalone ST-LINK/V2 (with SWD connector for programming and debugging)
  3. Board power supply: through USB bus or from an external 5 V supply voltage
  4. External application power supply: 3 V and 5 V
  5.  LIS302DL, ST MEMS motion sensor, 3-axis digital output accelerometer (Amazing !!!)
  6.   MP45DT02, ST MEMS audio sensor, omni-directional digital microphone
  7. CS43L22, audio DAC with integrated class D speaker driver
  8. Eight LEDs:LD1 (red/green) for USB communicationLD2 (red) for 3.3 V power onFour user LEDs, LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue)2 USB OTG LEDs LD7 (green) VBus and LD8 (red) over-current
  9. Two push buttons (user and reset)
  10.  USB OTG FS with micro-AB connector (amazing for a microcontroller) 
  11. Extension header for all LQFP100 I/Os for quick connection to prototyping board and easy probing


This board can be used in many application with its DSP it can solve complex Digital signal functions used in filter computing plus it has the FPU (Floating point unit) that allows the STM32F4 to deal with floats up to 10exp-18 precision and that would be very useful in application that needs media processing or precision calculations.

I will post some tutorials talking about my experience with board.

Cheers ;)