배우고픈 공돌이

STM32F4 디스커버리 보드 : LED blink 본문

ARM/Tutorial

STM32F4 디스커버리 보드 : LED blink

내 마음 아홉수 2017. 5. 8. 18:07



- main.c -

#include "stm32f4xx_conf.h"


GPIO_InitTypeDef  GPIO_InitStructure;

void Delay(__IO uint32_t nCount);


int 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)

  {

    /* LED to be toggled */

    GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);


    /* Insert delay */

    Delay(0x7FFFFF);


    GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);


    /* Insert delay */

    Delay(0xFFFFFF);

  }

}


void Delay(__IO uint32_t nCount)

{

  while(nCount--)

  {

  }

}




1. 전체적인 동작 순서


1) GPIO 그룹의 peripheral 버스를 동작시킨다.


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);



STM32F40x 모델의 블럭 다이어그램


2) LED에 연결된 포트핀 번호를 알고 GPIO 설정 및 초기화한다.


  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);


3) 비트를 set / reset을 번갈아가면서 LED가 깜박인다.


   GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);


   /* Insert delay */

   Delay(0x7FFFFF);


   GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);


   /* Insert delay */

   Delay(0xFFFFFF);



2. GPIO_InitTypeDef의 구조


IDE에서 'find declaration to ...' 의 기능으로 어떤 것을 참조했는지 알 수 있다.


-stm32f4xx_gpio.h-


typedef struct

{

  uint32_t GPIO_Pin;              /*!< Specifies the GPIO pins to be configured.

                                       This parameter can be any value of @ref GPIO_pins_define */


  GPIOMode_TypeDef GPIO_Mode;     /*!< Specifies the operating mode for the selected pins.

                                       This parameter can be a value of @ref GPIOMode_TypeDef */


  GPIOSpeed_TypeDef GPIO_Speed;   /*!< Specifies the speed for the selected pins.

                                       This parameter can be a value of @ref GPIOSpeed_TypeDef */


  GPIOOType_TypeDef GPIO_OType;   /*!< Specifies the operating output type for the selected pins.

                                       This parameter can be a value of @ref GPIOOType_TypeDef */


  GPIOPuPd_TypeDef GPIO_PuPd;     /*!< Specifies the operating Pull-up/Pull down for the selected pins.

                                       This parameter can be a value of @ref GPIOPuPd_TypeDef */

}GPIO_InitTypeDef;


 

이 외에 GPIO에서 사용하는 각 정의된 타입들과 함수들은 stm32f4xx_gpio.h로 확인할 수 있다.



3. 레지스터에 접근하여 LED 사용


위와 같은 구조체를 사용하였을 시, 코드의 재사용률이 뛰어나다.


하지만 유저 레벨이 아닌 개발 레벨에서는 레지스터를 직접 건드리는 것이 예외 사항을 줄이고


원하는 동작을 이해하는데 도움이 된다.


* 레지스터를 참조하는 방법


http://www.st.com/en/microcontrollers/stm32f407vg.html에서 매뉴얼들을 다운 받는다.


1) 메모리 맵을 보고 해당 peripheral(지금은 GPIOD)의 주소를 찾는다.



2) GPIO의 레지스터 비트 설정을 확인한다.



* 1)의 바운더리 주소와 2)의 오프셋 주소를 고려하여 비트설정한다. 

* 모르는 기능은 건들지 않고, 사용시에는 논리연산자 or를 사용하여 다른 비트를 건들지 않도록한다.


ex)   *(volatile uint32 *)0x40020C00 |= 0x01;    →    GPIOD_MODER = output mode




3) 레지스터는 모두 정의되어 있음으로 안에 비트만 넣어 사용하기도 한다.



ex)    GPIOD -> MODER = 0x01;    →    

GPIO_InitTypeDef  GPIO_InitStructure; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;


'ARM > Tutorial' 카테고리의 다른 글

[교육] ARM 프로세서와 명령어  (0) 2017.09.09
[교육] ARM 개요  (0) 2017.09.04
stm32cubemux 사용  (0) 2017.07.31
ARM 컴파일과정  (0) 2017.04.29
ARM이란?  (0) 2017.04.27
Comments