USART Tx Pada STM32F4 Discovery

Saya menggunakan compiler Coocox IDE dengan libarary GNU Tools ARM Embedded. Lengkapnya kunjungi website berikut

Pada artikel ini saya menggunakan USART1. Pertama, buatlah new project dengan settingan menggunakan chip STM32F407VG yang merupakan mikrokontroler pada STM32F4 Discovery.Setelah itu, pada setting repository yang muncul kemudian, beri tanda centang pada bagian:
C Library

Setelah itu tambahkan file USART1_lib.c dan USART1_lib.h ke file project dengan cara klik kanan pada project window tepat pada nama project kita. Lalu klik Add Files dan pilih filenya. Ini program utamanya.

#include "stm32f4xx.h"
#include "USART1_lib.h"

char kata[16];
int main(void)
    	sprintf(kata,"oke data %2.2f \n\r",353.34);

Kode di atas akan mengirim data “oke data 353.34 [enter]”.

Berikut lampiran untuk USART1_lib.c

#include "USART1_lib.h"

void RCC_Configuration(void)
	/* enable peripheral clock for USART2 */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

	/* GPIOA clock enable */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

void GPIO_Configuration(void){

	/* Configure USART Tx as alternate function  */
	  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	  GPIO_Init(GPIOB, &GPIO_InitStructure);

	/* Connect PXx to USARTx_Tx*/
	  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);

void USART1_Configuration(void)

		USART_InitStructure.USART_BaudRate = 9600;
		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_Tx;

		USART_Init(USART1, &USART_InitStructure);
		USART_Cmd(USART1, ENABLE); // enable USART2

void USART1_PutChar(char c)
	uint8_t ch;
	ch = c;
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	USART_SendData(USART1, (uint8_t) ch);

	/* Loop until the end of transmission */
	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

void PORTA_Configuration(void)
		/* GPIOD Periph clock enable */
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

		/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3| GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_Init(GPIOA, &GPIO_InitStructure);

void usart_puts(char *data)
	int i=0;
	int n = strlen(data);

void usart_inisialisasi()
	setvbuf( stdout, 0, _IONBF, 0 );

Berikut lampiran untuk USART1_lib.h

#ifndef __USART1_LIB_H
#define __USART1_LIB_H

#include "stm32f4xx.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include <stdio.h>
#include <string.h>

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_Configuration(void);
void USART1_PutChar(char c);
void PORTA_Configuration(void);
void usart_puts(char *data);
void usart_inisialisasi();

Jika terdapat error, bisa diperhatikan beberapa pertanyaan berikut yang saya temukan di forum-forum internet dan solusinya
1.Problem Send Floating Point with “PRINTF”

1 year ago

Hi hendriyawan achmad,

1. The Retarget printf component was once simpilified, and support for float variables was removed, so DO NOT select the component.

2. Besides changing
(void *)&pulStack[STACK_SIZE-1],
(void (*)(void))((unsigned long)pulStack + sizeof(pulStack)),

you need to modify function _write( ) in syscalls.c to

int _write( int file, char *ptr, int len )
int txCount;


for ( txCount = 0; txCount < len; txCount++)
UART_SendData(UART0, ptr[txCount]); //Don't forget to include "lpc11xx_uart.h"

return len;

3. In main.c, before using printf( ) function, add

/* Set unbuffered mode for stdout (newlib) */
setvbuf( stdout, 0, _IONBF, 0 ); //Don't forget to include

Sorry for my late reply. Hope this can do some help.

Best regards
2.STM32F4 Discovery HARDFPU – CooCox CoIDE printf to USART
This is a little howto for setting up of STM32F4 development board with a functional printf which would send it’s conntaints even if it’s float variables into established USART with the processor flag set at HARDFPU in IDE developed by CooCox version 1.6.0

Nice header, heh ?! ๐Ÿ™‚
I searched the internet! That’s what I’ve done.

Firstly set up HSE_VALUE

> in system_stm32f4xx.c change the external oscilator frequency
> discovery board has not default pre-stock frequency (which is 25MHz) it has 8MHz

Secondaly this manual how to set up printf in FPU SOFT by jfragos

Abstract: ”

Coocox – Project – config

> choose โ€œBase C libraryโ€, do not use the โ€œRedirectโ€
> later we are gonna change the “m” library to FPUHARD compatibile so leave it like that

file changes:

> in startup/startup_stm32f4xx.c) change
(void *)&pulStack[STACK_SIZE-1],
> to
(void (*)(void))((unsigned long)pulStack + sizeof(pulStack)),

> bigger stack size = in startup/startup_stm32f4xx.c
#define STACK_SIZE 0x00002000 /*! (8KBs will do)
> asd

> include wherever it’s needed – main.c, syscall.c etc.
> in main before printf cast
/* Set unbuffered mode for stdout (newlib) */
setvbuf( stdout, 0, _IONBF, 0 );

> in syscalls/syscalls.c some USART sending function
> i.e. sending to USART2

int _write(int file, char *ptr, int len)
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
int counter;

counter = len;
for (; counter > 0; counter–)
if (*ptr == 0) break;
USART_SendData(USART2, (uint16_t) (*ptr));
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
return len;

” End of abstract.

Now it should handle doubles, but only for FPU Soft.. just continue.. you must change moooore ๐Ÿ™‚

Now for the FPU HARD tutorial from

Abstract: ”

CooCox – projects – configuration – link

> link appropriet library.. if you inserted “m” earlier, erase it first, than add this Hard FPU
GCCARM\arm-none-eabi\lib\armv7e-m\fpu\libm.a (Hard FPU)
GCCARM\arm-none-eabi\lib\armv7e-m\softft\libm.a (Soft FPU)
> for me the name of the library is acctually libgcc.a

Next items I am not sure about if they need to be done.. I’ve done them and it works

> in startup_stm32f4xx.c un-comment
extern void SystemInit(void); /*! in startup_stm32f4xx.c add SystemInit(); call before main(); function call in
void DefaultResetHandler()

” End of Abstract
and I think next item is optional cause it practicly does the same thing as HSE_Value

> in startup_stm32f4xx.c
#define PLL_M 25

Initialize your USART from
add printf to main ๐Ÿ™‚

int main(void){
setvbuf( stdout, 0, _IONBF, 0 );
int a = 100;
float f = 10.123;
double d = 10.123;
printf(“I will not do anything, until I am done! %d %2.2f %2.2f”, a, f, d);
while(1) {}

Compile download and run..

Now you can add a debug breakpoint into USAR_IRQHandler to the part when the text is read, add an expression of the string into which you read the data, and start debug mode.

Or you just can connect the usart into some display or whatever :).

I hope this will work for you, and I stumble if it helps ๐Ÿ™‚

If some-one nows how to get the printf print into the CooCox debug console, please leave a comment with a link or explanation..

(: please please please ๐Ÿ™‚

Have a better day!
3.STM32F4 compile problem on example project (IOtoggle)
[cc] collect2: ld returned 1 exit status

Total time: 1 second
Terry Guo (terry.guo) said on 2012-09-26: #1

Hi Amin,

The assert_param is a macro defined in file stm32f4xx_conf.h. This file will be included only when another macro USE_STDPERIPH_DRIVER is explicitly defined. You can find this in file stm32f4xx.h. The usage of assert_param is explained in where it is defined.

To solve your problem, please open “Project” -> “Configuration” and add USE_STDPERIPH_DRIVER into table “Defined symbols”. Depends on how you are going to use assert_param, you may need further work.


2 Responses to USART Tx Pada STM32F4 Discovery

  1. Pingback: Accelerometer LIS302DL di STM32F4-Discovery | WanGReadY

  2. azhari says:

    Keren Mas, sudah saya coba juga
    It works ๐Ÿ˜€

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s