lilijin1995 发表于 2022-3-10 00:45

chv32v103C8T6的USART1配置接收中断后,测试没有进入接收中断...

本帖最后由 lilijin1995 于 2022-3-10 16:52 编辑

chv32v103C8T6的USART1配置接收中断后,测试没有进入接收中断程序;


/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2020/04/30
* Description      : Main program body.
*******************************************************************************/

/*
*@Note
USART中断例程:
Master:USART2_Tx(PA2)、USART2_Rx(PA3)。
Slave:USART3_Tx(PB10)、USART3_Rx(PB11)。

本例程演示 UART2 和 USART3 使用查询发送,中断接收。
注:
   硬件连线:PA2 —— PB11
               PA3 —— PB10

*/

#include "debug.h"

/* Global define */
#define TxSize1   (size(TxBuffer1))
#define TxSize2   (size(TxBuffer2))
#define size(a)   (sizeof(a) / sizeof(*(a)))

/* Global typedef */
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;

/* Global Variable */
u8 TxBuffer1[] = "*Buffer1 Send from USART2 to USART3 using Interrupt!";   /* Send by UART2 */
u8 TxBuffer2[] = "#Buffer2 Send from USART3 to USART2 using Interrupt!";   /* Send by UART3 */
u8 RxBuffer1={0};                                                   /* USART2 Using */
u8 RxBuffer2={0};                                                   /* USART3 Using*/

u8 TxCnt1 = 0, RxCnt1 = 0;
u8 TxCnt2 = 0, RxCnt2 = 0;

u8 Rxfinish1=0,Rxfinish2=0;

TestStatus TransferStatus1 = FAILED;
TestStatus TransferStatus2 = FAILED;

void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/*******************************************************************************
* Function Name: Buffercmp
* Description    : Compares two buffers
* Input          : Buf1,Buf2:buffers to be compared
*                  BufferLength: buffer's length
* Return         : PASSED: Buf1 identical to Buf2
*                  FAILED: Buf1 differs from Buf2
*******************************************************************************/
TestStatus Buffercmp(uint8_t* Buf1, uint8_t* Buf2, uint16_t BufLength)
{
while(BufLength--)
{
    if(*Buf1 != *Buf2)
    {
      return FAILED;
    }
    Buf1++;
    Buf2++;
}
return PASSED;
}


/*******************************************************************************
* Function Name: USARTx_CFG
* Description    : Initializes the USART2 & USART3 peripheral.
* Input          : None
* Return         : None
*******************************************************************************/
void USARTx_CFG(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;
      NVIC_InitTypeDefNVIC_InitStructure;

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB , ENABLE);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
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_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART3 TX-->B.10RX-->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);

      USART_InitStructure.USART_BaudRate = 115200;
      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_Tx | USART_Mode_Rx;

USART_Init(USART1, &USART_InitStructure);
      USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

      USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

      NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

      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);

      USART_Cmd(USART1, ENABLE);
      USART_Cmd(USART3, ENABLE);
}
/*******************************************************************************
* Function Name: main
* Description    : Main program.
* Input          : None
* Return         : None
*******************************************************************************/
int main(void)
{
      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Delay_Init();
      USART_Printf_Init(115200);
      printf("SystemClk:%d\r\n",SystemCoreClock);

      printf("USART Interrupt TEST\r\n");
USARTx_CFG();                                                 /* USART2 & USART3 INIT */



      while(1)
{
      }
}


/*******************************************************************************
* Function Name: USART2_IRQHandler
* Description    : This function handles USART2 global interrupt request.
* Input          : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
//    RxBuffer1 = USART_ReceiveData(USART1);
    printf("R");
//    if(RxCnt1 == TxSize2)
//    {
//      USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
//                        Rxfinish1=1;
//    }
}

}

/*******************************************************************************
* Function Name: USART3_IRQHandler
* Description    : This function handles USART3 global interrupt request.
* Input          : None
* Return         : None
*******************************************************************************/
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
    RxBuffer2 = USART_ReceiveData(USART3);

    if(RxCnt2 == TxSize1)
    {
      USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
                        Rxfinish2=1;
    }
}
}



另外debug中配置usart2打印log,我们main函数中声明了中断;
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
但通过测试,始终无法进入USART1_IRQHandler的接收中断;补充一下,是通过串口调试工具的TX发到USART1的RX的,然后通过USART2的TX打印log

WCHTech2 发表于 2022-3-10 19:04

您好,附件为串口1接收中断例程,可以参考一下。

七毛钱 发表于 2022-3-11 10:10

参考一下相关例程把

lilijin1995 发表于 2022-3-11 14:00

WCHTech2 发表于 2022-3-10 19:04
您好,附件为串口1接收中断例程,可以参考一下。

感谢大佬

Bowclad 发表于 2022-9-10 21:21

参考下相关例程
页: [1]
查看完整版本: chv32v103C8T6的USART1配置接收中断后,测试没有进入接收中断...