lxig 发表于 2020-9-25 19:39:52

stm32f7系列使用printf打印在串口上显示不出来,请各位大佬k...

#include "stm32f7xx.h"
#include"stm32f7xx_hal.h"

#include <stdio.h>       
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

static void Error_Handler(void);
static void CPU_CACHE_Enable(void);
void systemclockinit(int plln,int pllm,int pllp,int pllq);
void uartinit(void);

static UART_HandleTypeDef UARThandle;

int main(void)
{
/* Enable the CPU Cache */
CPU_CACHE_Enable();

HAL_Init();
        systemclockinit(432,25,2,9);
        uartinit();
        printf("hello");
//        HAL_icInit();
// printf("my printf test,compile time:%s %s\r\n",__DATE__,__TIME__);
        printf("hello");
        while(1)
{
/*        if(TIM5CH1_Capture_STA&0X40)
               
        {
        temp=TIM5CH1_Capture_STA;
        temp*=0xFFFFFFFF;
        temp+=TIM5CH1_Capture_VAL;
        sprintf(text, "AD value = 0x%04X", temp);
       
        }                               
                */
}
}

static void Error_Handler(void)
{
/* User may add here some code to deal with this error */
while(1)
{
}
}


static void CPU_CACHE_Enable(void)
{
/* Enable I-Cache */
SCB_EnableICache();

/* Enable D-Cache */
SCB_EnableDCache();
}


//串口初始化
void uartinit(void)
{
        UARThandle.Instance=USART1;
        UARThandle.Init.BaudRate=115200;
        UARThandle.Init.WordLength=UART_WORDLENGTH_8B;
        UARThandle.Init.StopBits=UART_STOPBITS_1;
        UARThandle.Init.HwFlowCtl=UART_HWCONTROL_NONE;
        UARThandle.Init.Parity=UART_PARITY_NONE;   
        UARThandle.Init.Mode=UART_MODE_TX_RX;
        if        (HAL_UART_Init(&UARThandle)!=HAL_OK)
        {
Error_Handler();       
        }
}


//串口回调函数
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDefGPIO_InitStruct;

RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;

/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
__HAL_RCC_GPIOA_CLK_ENABLE();

/* Select SysClk as source of USART1 clocks */
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);

/* Enable USARTx clock */
__HAL_RCC_USART1_CLK_ENABLE();

/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration*/
GPIO_InitStruct.Pin       = GPIO_PIN_9;
GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull      = GPIO_PULLUP;
GPIO_InitStruct.Speed   = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/* UART RX GPIO pin configuration*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}


//printf重定向
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&UARThandle, (uint8_t *)&ch, 1, 0xFFFF);

return ch;
}


发表于 2020-9-26 13:38:12

从代码上看是正常的,检查一下串口的连接,RX接TX。如果还是不确定,用示波器看一下TX的波形。注意开发板的串口连接引脚和实际的是否一致。

lxig 发表于 2020-9-26 19:52:57

安 发表于 2020-9-26 13:38
从代码上看是正常的,检查一下串口的连接,RX接TX。如果还是不确定,用示波器看一下TX的波形。注意开发板的 ...

硬件连接应该没问题,因为用开发板的附带的例程测试是可以在窜口上显示的:'(

lxig 发表于 2020-9-27 16:23:34

安 发表于 2020-9-26 13:38
从代码上看是正常的,检查一下串口的连接,RX接TX。如果还是不确定,用示波器看一下TX的波形。注意开发板的 ...

谢谢,搞明白了。原来是工程目标选项配置的c/c++编译中勾选了支持GNU扩展选项(为什么这样并不知道)
页: [1]
查看完整版本: stm32f7系列使用printf打印在串口上显示不出来,请各位大佬k...