串口(printf)重定向

本文對應的開發板是:stm32

在kail環境中(參考野火f429源代碼):

#include<stdio.h>
//重寫printf的底層函數fputc
int fputc(int ch, FILE *f)
{
		
		
		/* 等待串口發送寄存器爲空 */
		while (USART_GetFlagStatus(DEBUG_USART, USART_FLAG_TXE) == RESET);	

        /* 發送串口數據 */
		USART_SendData(DEBUG_USART, (uint8_t) ch);	
	
		return (ch);
}

int mian()
{
    uart_init();
    printf("****** uart test*****\n");

}

在 GCC環境中

#include<stdio.h>
//在本地文件中重寫printf的底層函數_write 
int _write (int fd, char *pBuffer, int size)  
{  
    for (int i = 0; i < size; i++)  
    {
		
		while (UART_GetFlagStatus(uart1, UART_FLAG_TXE) == RESET);	
        UART_SendData(uart1, (uint8_t) *pBuffer++);
    }  
    return size;  
}

int mian()
{
    uart_init();
    printf("****** uart test*****\n");

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章