cancel
Showing results for 
Search instead for 
Did you mean: 

Problem example USART_Printf for Nucleo stm32f411re

didos.nicky
Associate II
Posted on August 10, 2015 at 16:55

Hello everyone !! I have a

http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1847/PF260320

and use IAR with

http://www.st.com/st-web-ui/static/active/en/st_prod_software_internet/resource/technical/software/firmware/stm32f4_dsp_stdperiph_lib.zip

. I did not understand how to run the following example

FW_STM32F3_F4_ST\STM32F4xx_DSP_StdPeriph_Lib_V1.5.1\Project\STM32F4xx_StdPeriph_Examples\USART\USART_Printf with IAR and NUCLEO-F411RE

&sharpinclude ''main.h''

&sharpinclude <stdio.h>

&sharpinclude <string.h>

static void USART_Config(void);

&sharpifdef __GNUC__

  &sharpdefine PUTCHAR_PROTOTYPE int __io_putchar(int ch)

&sharpelse

  &sharpdefine PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

&sharpendif

static void USART_Config(void)

{

  USART_InitTypeDef USART_InitStructure;

  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;

  STM_EVAL_COMInit(COM1, &USART_InitStructure);

}

int main(void)

{

  USART_Config();

  printf(''\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r'');

  while (1)

  {

  }

}

PUTCHAR_PROTOTYPE

{

 USART_SendData(EVAL_COM1, (uint8_t) ch);

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

  {}

  return ch;

}

&sharpifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

   while (1)

  {

  }

}

&sharpendif

I get

by

iar

:

Error[Pe020]: identifier ''STM_EVAL_COMInit'' is undefined FW_STM32F3_F4_ST\STM32F4xx_DSP_StdPeriph_Lib_V1.5.1\Project\STM32F4xx_StdPeriph_USART\main.c 62

Error[Pe020]: identifier ''COM1'' is undefined FW_STM32F3_F4_ST\STM32F4xx_DSP_StdPeriph_Lib_V1.5.1\Project\STM32F4xx_StdPeriph_USART\main.c 62

Error[Pe020]: identifier ''EVAL_COM1'' is undefined FW_STM32F3_F4_ST\STM32F4xx_DSP_StdPeriph_Lib_V1.5.1\Project\STM32F4xx_StdPeriph_USART\main.c 99

Error while running C/C++ Compiler 

How can I solve

this

problem

?

#stm32f4xx_dsp_st #nucleof411re
2 REPLIES 2
Posted on August 10, 2015 at 17:16

How can I solve this problem?

You'd need to fix the dependencies on external files, that provide the board level functions and defines you're using. You'd want to review the example you pulled this from more carefully, and look for the source/include files for the board support code in the Utilities directory. Here's how you can configure the USART and pins on the 401RE NUCLEO which is a close enough match to your board. [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=1286]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FUSART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=1286 This is how you would do the retargeting in Keil, IAR might just need the fputc(), but that's between you and the tool-chain documentation to resolve.

// STM32 Keil printf USART2 (Tx PA.2, Rx PA.3) STM32F401RE NUCLEO - sourcer32@gmail.com
// Use MicroLIB checked in options
#include <
stdio.h
>
#include <
stdlib.h
>
#include ''stm32f4xx.h''
//****************************************************************************
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
//****************************************************************************
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // PA.2 USART2_TX, PA.3 USART2_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
}
//****************************************************************************
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600 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 = 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_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
//****************************************************************************
void OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}
//****************************************************************************
int main(void)
{
int i;
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
OutString(''Welcome to Nucleo F401RE

'');
printf(''Hello World!
'');
while(1); // Don't want to exit
}
//****************************************************************************
// Hosting of stdio functionality through USART2
//****************************************************************************
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART2);
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, ch);
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
//****************************************************************************
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
//****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
didos.nicky
Associate II
Posted on August 10, 2015 at 23:31

Thank you. It seems your code perfectly works:

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FUSART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=1286

but although we desire to send and receive a specific string, we always receive a sequence of strange characters. Inside the strange characters we receive, we don't see the string we desired to send.

Please give us some hints.

Best Regards