cancel
Showing results for 
Search instead for 
Did you mean: 

is possible to use printf?

fullcaos
Associate II
Posted on October 16, 2012 at 17:00

I would use the printf function to transmit strings of hyperterminal, but if I use the printf function, the microcontroller stops!

I am developing my code on STM32F4 Discovery
9 REPLIES 9
Posted on October 16, 2012 at 19:25

Where do you expect the data to go?

Retargeting Keil's output to a USART is pretty easy.

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/DispForm.aspx?ID=2606

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
fullcaos
Associate II
Posted on October 16, 2012 at 21:50

I need to send data to the PC through the RS232, I do not need to receive from the PC but only print strings from the microcontroller to the PC, I need to use a function like

printf (string myString) I used this code include

#include <
stm32f4_discovery.h
>
#include <
stdio.h
>

statement GPIO

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
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_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); 

GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); 

configuration USART

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(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);

If I use USART_SendData(USART3, 0x49); it's all right but i I us printf(''hello''); the microcontrollers stops ''this the code you suggested to another user some time ago, I found in an old debate'' I have already provided the converter levels USART -> RS232
Posted on October 16, 2012 at 23:00

it's all right but i I us printf(''hello''); the microcontrollers stops

Because it's probably ploughing into an SWI hook that it expects you to be servicing. A disassembly or trace should be illustrative. Keil suppresses this with a pragma.

#pragma import(__use_no_semihosting_swi)

And then targeted to your hardware via character output routines

//******************************************************************************
// Hosting of stdio functionality through USART3
//******************************************************************************
#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(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART3);
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, ch);
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
fullcaos
Associate II
Posted on October 16, 2012 at 23:14

I'm sorry but I can not understand ...

to solve my problem di I have to define # pragma import (__use_no_semihosting_swi)??

Posted on October 17, 2012 at 03:21

The take away here is that by default it links in library code using SWI calls (Software Interrupts) to perform OS level functions. Functions you don't provide, and it consequently crashes, Hard Faults, whatever.

You have to suppress that and provide IO routines to your hardware, as the library code has no idea about specific chips or boards.

I have provided specific code to do this, for Keil, but you could refer to other documentation/examples of retargeting if this would help you.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
fullcaos
Associate II
Posted on October 17, 2012 at 21:43

thanks

for your help

and

your kindness

!

I would like

to ask your advice

,

some time

ago

you suggested

''

The Definitive

Guide to the

ARM

Cortex-

M3''

but is

there 

a

similar

guide for the

M4

microcontroller

?

Posted on October 18, 2012 at 01:36

There isn't an M4 volume, but the M0 one is the newest and much thicker, to be honest they are covering a lot of the same material. So with the exception of the floating point unit they are all fairly similar. The M3 editions seem to exist fairly readily on the internet so I'd browse those first.

ARM has some very good TRM (Technical Reference Manuals)

See also the resources tab here

http://www.arm.com/products/processors/cortex-m/cortex-m4-processor.php

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andrewlucas9
Associate II
Posted on October 19, 2012 at 20:20

Actually the M0 book is only about 50pages longer.  It looks and feels much ''thicker'', because the paper used in the M0 book is of a heavier stock.  I wish they had used the same thinner paper that was used on the M3 book though.

Posted on October 24, 2015 at 23:11

Since this thread started, Joseph Yiu has a book covering the M3/M4

http://www.amazon.com/Definitive-Cortex%C2%AE-M3-Cortex%C2%AE-M4-Processors-Edition/dp/0124080820

An there is also this book from Trevor Martin

http://www.amazon.com/Designers-Guide-Cortex-M-Processor-Family/dp/0080982964

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..