STM32 UART DEBUG포트 사용하기

CPU/STM32F103 2013. 2. 7. 18:08

이번엔 UART를 포팅할 예정이다.

말이 포팅이지 ST에서 제공하는 소스 가지고, 해당 보드에 확인 테스트 하는 것이다.

예전엔 CPU DATASHEET가지고 하나하나 코딩해서 레지스터 세팅하고 했는데. 지금은 카피하고 해당포트만 맞추어 주면 끝...나도 나이가 많이 먹었나 보다..옛날 이야기 하니..

자그럼 디버그를 위한 UART를 설정하자.


1. 사용방법

1.1 stm32f10x_conf.h 설정

#define DEBUG    1

#define _USART

#define _USART1


stm32f10x_lib.h -->  stm32f01x_map.h -->  stm32f10x.conf.h 요렇게 링크하네요.

위와 같이 해당 모듈에 대한 define를 해주어야 이후 stm32f10x_lib.h 을 통해 해당 h들이 포함된다.


1.2 UART 입출력 포트 설정

아래와 같이 uart를 사용하기 위하여 rx,tx핀에 대한 입출력을 설정하야한다.

avr은 그냥 자동으로 변경되었는데..stm32는 해줘어야 되나보다.

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;


  /* Configure USART1 Tx (PA.09) as alternate function push-pull */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

    

  /* Configure USART1 Rx (PA.10) as input floating */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

}



1.3  클럭관련 설정한다.

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

 

- RCC_Configuration함수에서 마지막에 위를 호출하여 USART1에 대하여 클럭를 설정한다.


1.4 UART1에 대한 초기화 작업

void USART_Configuration(void)

{

  USART_InitTypeDef USART_InitStructure;


/* USART1 configuration ------------------------------------------------------*/

  /* USART1 configured as follow:

        - BaudRate = 115200 baud  

        - Word Length = 8 Bits

        - One Stop Bit

        - No parity

        - Hardware flow control disabled (RTS and CTS signals)

        - Receive and transmit enabled

  */

  USART_InitStructure.USART_BaudRate = 115200;

  USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_No;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;


  USART_Init(USART1, &USART_InitStructure);

    

  /* Enable USART1 */

  USART_Cmd(USART1, ENABLE); //인제 동작시작

}


일단 UART에 대한 초기작업은 되었고 이후는 사용만 하면 된다.


1.5 PUTCHAR_PROTOTYPE 선언

PUTCHAR_PROTOTYPE

{

  /* Place your implementation of fputc here */

  /* e.g. write a character to the USART */

  USART_SendData(USART1, (u8) ch);


  /* Loop until the end of transmission */

  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

  {

  }


  return ch;

}


원래main에서 printf를 사용하면 <stdio.h>에서 호출하고 printf함수에서 PUTCHAR_PROTOTYPE를 호출하여 사용하는것 같다.

1.6 사용

printf("\r\n STM32F10x Firmware Library compiled in DEBUG mode... \n\r");

이럼... UART1에서 TERMINAL로 출력이 생긴다.


2. 전체

int main(void)

{

   int i=0;

   

#ifdef DEBUG

  debug();  //요건 몰까요?

#endif


  /* Configure the system clocks */

  RCC_Configuration(); // 클럭관련 설정후 UART 클럭도 설정

    

  /* NVIC Configuration */

  NVIC_Configuration();


   /* Configure the GPIOs */

  GPIO_Configuration();            // uart관련 io설정


  /* Configure the USART1 */

  USART_Configuration(); // UART1에 대하여 초기화 작업


  printf("\r\n STM32F10x Firmware Library compiled in DEBUG mode... \n\r");

  printf("...Run-time checking enabled  \n\r");

 

  while (1)

  {

    Delay(0xFFFFF);

    printf("==== %d ====\r\n",i++); // 뺑뺑이 돌려보았습니다.

  }

}




'CPU > STM32F103' 카테고리의 다른 글

STM32 ADC2 Interrupt Mode 사용  (1) 2013.02.08
STM32 ADC1 DMA 사용방법1  (1) 2013.02.07
STM32 GPIO INPUT 사용하기  (0) 2013.02.07
STM32 GPIO 제어  (0) 2013.02.07
STM32F103VB  (0) 2013.02.06