使用EVT里面的GPIO_Toggle中使用下面的串口测试程序,发现printf无法将数据通过WCH-link的串口发送到上位机,但是USART3却可以。
有问题是下面这行:
printf("%c", d);
如果改成了,下面这样,又可以打印出来了,这个是什么原因呢?
printf("%c\r\n", d);
USART3可以的代码片段是:
USART_SendData(USART3, d);
复现这个问题,可以将下面的代码片段替换GPIO_Toggle的main.c:
/********************************** (C) COPYRIGHT ******************************* * File Name : main.c * Author : WCH * Version : V1.0.0 * Date : 2021/06/06 * Description : Main program body. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. * SPDX-License-Identifier: Apache-2.0 *******************************************************************************/ /* *@Note GPIO例程: PA0推挽输出。 */ #include "debug.h" /* Global define */ /* Global Variable */ void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); /********************************************************************* * @fn GPIO_Toggle_INIT * * @brief Initializes GPIOA.0 * * @return none */ void GPIO_Toggle_INIT(void) { GPIO_InitTypeDef GPIO_InitStructure = { 0 }; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void USART3_Init(void) { int parity = USART_Parity_No; //ToDo: for RS485 in nonParity stop bit should be 2 int stopBit = USART_StopBits_1; int dataBit = USART_WordLength_8b; u32 ulBaudRate = 9600; GPIO_InitTypeDef GPIO_InitStructure = { 0 }; USART_InitTypeDef USART_InitStructure = { 0 }; NVIC_InitTypeDef NVIC_InitStructure = { 0 }; RCC_APB1PeriphClockCmd( #ifdef USE_UART2 RCC_APB1Periph_USART2 | #endif RCC_APB1Periph_USART3, ENABLE); //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); #ifdef USE_UART2 /* USART2 TX-->A.2 RX-->A.3 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif /* USART3 TX-->B.10 RX-->B.11 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); // ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity USART_InitStructure.USART_BaudRate = ulBaudRate; //115200; USART_InitStructure.USART_WordLength = dataBit; //USART_WordLength_8b; USART_InitStructure.USART_StopBits = stopBit; //USART_StopBits_1; USART_InitStructure.USART_Parity = parity; //USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; #ifdef USE_UART2 USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); #endif USART_Init(USART3, &USART_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); #ifdef USE_UART2 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #ifdef USE_UART2 USART_Cmd(USART2, ENABLE); #endif USART_Cmd(USART3, ENABLE); } /********************************************************************* * @fn USART3_IRQHandler * * @brief This function handles USART3 global interrupt request. * * @return none */ void USART3_IRQHandler(void) { //printf("IRQ\r\n"); //RX not empty, data came if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { u16 d = USART_ReceiveData(USART3); printf("%c", d); USART_SendData(USART3, d); //USART_ClearITPendingBit(USART3, USART_IT_RXNE); } //USART_IT_TC or USART_IT_TXE? // TC: tramsimit done // USART_IT_TXE : TX done, TX buffer empty // From name "prvvUARTTxReadyISR" should be the TXE else if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) { printf("U3 IRQ TX\r\n"); USART_ClearITPendingBit(USART3, USART_IT_TC); } else { printf("U3 Interrupt flag:\r\n"); if (USART_GetITStatus(USART3, USART_IT_CTS) != RESET) { printf(" USART_IT_CTS "); } else if (USART_GetITStatus(USART3, USART_IT_LBD) != RESET) { printf(" USART_IT_LBD "); } else if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET) { printf(" USART_IT_TXE "); } else if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) { printf(" USART_IT_LBD "); } else if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) { printf(" USART_IT_IDLE "); } else if (USART_GetITStatus(USART3, USART_IT_ORE_RX) != RESET) { printf(" USART_IT_ORE_RX "); } else if (USART_GetITStatus(USART3, USART_IT_ORE_ER) != RESET) { printf(" USART_IT_ORE_ER "); } else if (USART_GetITStatus(USART3, USART_IT_NE) != RESET) { printf(" USART_IT_NE "); } else if (USART_GetITStatus(USART3, USART_IT_FE) != RESET) { printf(" USART_IT_FE "); } else if (USART_GetITStatus(USART3, USART_IT_PE) != RESET) { printf(" USART_IT_PE "); } else { printf("Other UART Flag\r\n"); } printf("\r\n"); } } /********************************************************************* * @fn main * * @brief Main program. * * @return none */ int main(void) { u8 i = 0; u8 j = 1; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); Delay_Init(); USART3_Init(); USART_Printf_Init(115200); printf("SystemClk:%d\r\n", SystemCoreClock); printf("GPIO Toggle TEST\r\n"); GPIO_Toggle_INIT(); while(1) { Delay_Ms(250); GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i == 0) ? (i = Bit_SET) : (i = Bit_RESET)); //GPIO_WriteBit(GPIOA, GPIO_Pin_1, (j == 0) ? (j = Bit_SET) : (j = Bit_RESET)); } }
热门产品 :
CH585: 集成高速USB和NFC的蓝牙MCU