cancel
Showing results for 
Search instead for 
Did you mean: 

printf keil stm32 eval2 board

pncardella
Associate II
Posted on July 07, 2014 at 10:33

Hey guys,

I have been looking up how to do this for a while and I haven�t been able to figure it out.  How do I use printf in keil for the stm32439i-eval2 board? Is there code that will make this work anywhere?

Thanks for any help.

#eval #semihosting #stm32 #stm32f4 #keil
4 REPLIES 4
Posted on July 07, 2014 at 15:02

Try this

//******************************************************************************
// Keil uv4 USART Retargeting Example - sourcer32@gmail.com
//******************************************************************************
#include <
stdio.h
>
#include <
stdlib.h
>
#include ''stm32f4xx.h''
//******************************************************************************
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable USART1 Clock */
/* USART RS232 - CN8 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); /* Connect PA9 to USART1_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); /* Connect PA10 to USART1_Rx */
/* Configure USART1 Tx & Rx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_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;
USART_Init(USART1, &USART_InitStructure); /* Configure USART */
USART_Cmd(USART1, ENABLE); /* Enable the USART */
}
//******************************************************************************
int main(void)
{
USART1_Configuration();
printf(''Hello World!'');
while(1); /* Infinite loop */
}
//******************************************************************************
// Hosting of stdio functionality through USART1
//******************************************************************************
#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(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART1);
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 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) */
while(1); /* Infinite loop */
}
#endif
//******************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pncardella
Associate II
Posted on July 08, 2014 at 10:31

Thanks for the response clive.

I am using the sample project from st for the eval board and a lot of the things in the code you sent me are undefined. So I guess I will try to define them.

Posted on July 08, 2014 at 16:34

I think you need to review how a project is constructed, the defines it sets, and the include paths uses.

STM32F4xx_DSP_StdPeriph_Lib_V1.3.0\Project\STM32F4xx_StdPeriph_Templates\MDK-ARM
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pncardella
Associate II
Posted on July 09, 2014 at 09:33

I cannot find some of those variables anywhere in the project, so I can´t include whatever file they are in. Maybe I am missing something.