2017-07-25 07:25 AM
I use chip STM32L073RzTx and STM32F103C8 , compiler and IDE True Studio, HAL base coding, develop board stm32nucleo
I want using printf() in UART Communication
I used TDR register UART Communication good working but printf() don't working
now situation explain...
=======================================
int fputc(int ch, FILE *f)
{ HAL_UART_Transmit (&huart2, (uint8_t*)ch, 1, 2); return(ch);}add this code and using printf() in while() function
========================================
that code .. don't working
so, how to use printf() in UART Communication and teach me principal printf()
and I have another question.
I look at stm32nucleo board circuit that board uart line pass through another MCU and connect micro usb, so nucleo board recognize USB well on my computer.
but STM32F103C8 is cheap develop board in internet that circuit is direct tx, rx(USART Pin) ,vcc, gnd connected noting pass through, so... that is not recognizing...
so... how to use direct connected siation UART communication
English sentences are strange but thank you for reading them.
2017-07-25 07:38 AM
Hi
polar8950
,For printf probably refering to
http://www.st.com/resource/en/application_note/dm00354pdf
chapter 7 may help you.BR,
Olivier
2017-07-25 07:39 AM
You can't apply Keil methods to GNU/GCC tools.
For the latter you need to use a 'newlib' approach
Your HAL equivalent of this, plus whatever USART initialization code you require
//******************************************************************************
/**
* @brief Retargets the C library printf function to the USART (GNU)
* @param None
* @retval None
*/
int __io_putchar(int ch)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
/* Loop until the transmission buffer is empty */
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, (uint8_t) ch);
return(ch);
}
//******************************************************************************
/**
* @brief Retargets the C library scanf function to the USART (GNU)
* @param None
* @retval None
*/
int __io_getchar(void)
{
/* Place your implementation of fgetc here */
/* e.g. read a character from the USART */
/* Loop until the reception buffer is not empty */
while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
return((int)USART_ReceiveData(USART3));
}
//******************************************************************************
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
And newlib_stubs.c like this
/*
* newlib_stubs.c
*
* Minimal implementation after nanoage.co.uk
*/
#include <errno.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/unistd.h>
#include 'stm32f4xx.h'
extern int __io_putchar(int ch);
extern int __io_getchar(void);
#undef errno
extern int errno;
/*
environ
A pointer to a list of environment variables and their values.
For a minimal environment, this empty list is adequate:
*/
char *__env[1] = { 0 };
char **environ = __env;
int _write(int file, char *ptr, int len);
void _exit(int status) {
_write(1, 'exit', 4);
while (1) {
;
}
}
int _close(int file) {
return -1;
}
/*
execve
Transfer control to a new process. Minimal implementation (for a system without processes):
*/
int _execve(char *name, char **argv, char **env) {
errno = ENOMEM;
return -1;
}
/*
fork
Create a new process. Minimal implementation (for a system without processes):
*/
int _fork() {
errno = EAGAIN;
return -1;
}
/*
fstat
Status of an open file. For consistency with other minimal implementations in these examples,
all files are regarded as character special devices.
The `sys/stat.h' header file required is distributed in the `include' subdirectory for this C library.
*/
int _fstat(int file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
/*
getpid
Process-ID; this is sometimes used to generate strings unlikely to conflict with other processes. Minimal implementation, for a system without processes:
*/
int _getpid() {
return 1;
}
/*
isatty
Query whether output stream is a terminal. For consistency with the other minimal implementations,
*/
int _isatty(int file) {
switch (file){
case STDOUT_FILENO:
case STDERR_FILENO:
case STDIN_FILENO:
return 1;
default:
//errno = ENOTTY;
errno = EBADF;
return 0;
}
}
/*
kill
Send a signal. Minimal implementation:
*/
int _kill(int pid, int sig) {
errno = EINVAL;
return (-1);
}
/*
link
Establish a new name for an existing file. Minimal implementation:
*/
int _link(char *old, char *new) {
errno = EMLINK;
return -1;
}
/*
lseek
Set position in a file. Minimal implementation:
*/
int _lseek(int file, int ptr, int dir) {
return 0;
}
/*
sbrk
Increase program data space.
Malloc and related functions depend on this
*/
caddr_t _sbrk(int incr) {
extern char _ebss; // Defined by the linker
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0) {
heap_end = &_ebss;
}
prev_heap_end = heap_end;
char * stack = (char*) __get_MSP();
if (heap_end + incr > stack)
{
_write (STDERR_FILENO, 'Heap and stack collision
', 25);
errno = ENOMEM;
return (caddr_t) -1;
//abort ();
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
/*
read
Read a character to a file. `libc' subroutines will use this system routine for input from all files, including stdin
Returns -1 on error or blocks until the number of characters have been read.
*/
int _read(int file, char *ptr, int len) {
int n;
int num = 0;
switch (file) {
case STDIN_FILENO:
for (n = 0; n < len; n++) {
*ptr++ = __io_getchar();
num++;
}
break;
default:
errno = EBADF;
return -1;
}
return num;
}
/*
stat
Status of a file (by name). Minimal implementation:
int _EXFUN(stat,( const char *__path, struct stat *__sbuf ));
*/
int _stat(const char *filepath, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
/*
times
Timing information for current process. Minimal implementation:
*/
clock_t _times(struct tms *buf) {
return -1;
}
/*
unlink
Remove a file's directory entry. Minimal implementation:
*/
int _unlink(char *name) {
errno = ENOENT;
return -1;
}
/*
wait
Wait for a child process. Minimal implementation:
*/
int _wait(int *status) {
errno = ECHILD;
return -1;
}
/*
write
Write a character to a file. `libc' subroutines will use this system routine for output to all files, including stdout
Returns -1 on error or number of bytes sent
*/
int _write(int file, char *ptr, int len) {
int n;
switch (file) {
case STDOUT_FILENO: /*stdout*/
case STDERR_FILENO: /* stderr */
for (n = 0; n < len; n++) {
__io_putchar(*ptr++);
}
break;
default:
errno = EBADF;
return -1;
}
return len;
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
2017-07-25 08:44 PM
Hi Oliver
Thank your answer that too halpful~
I readed reference chapter 7 .. that is why different? gcc compiler and keil compiler that both based on C++/C ?
2017-07-25 09:48 PM
Ford and Toyota make cars, differently, parts are generally incompatible.
2017-07-26 10:16 AM
Oh~ thank you so much
Now I can usa USART communication with PC
2017-07-26 10:26 AM
Minseo Kim wrote:
Oh~ thank you so much
Now I can usa USART communication with PC
2017-07-27 10:24 PM
There is a very easy way to do this with TrueSTUDIO. It is described in the user's manual in the I/O Redirection section. Open syscalls.c and rewrite the _write function.