我在开发板上使用USB转串口模块将PC(windows)设备与TMS320F280025C连接进行串口通信。当串口正常连接后,PC和单片机能够正常通信。但是如果我将串口拔出后再重新插回,单片机就会无法进入RX中断,导致收不到数据,但是PC是仍然能够收到单片机的TX发送出的数据的。如果想要恢复通信,只能将单片机复位重启。
根据上面的情况,我还测试了:先断开PC和单片机的串口连接,当单片机启动后再重新连接串口,此时也会出现同样的情况,即单片机无法进入RX中断。
我的串口初始化和中断代码如下:
//RX中断函数 interrupt void Rx1Isr(void) // SCI-A { UCHAR data = SciaRegs.SCIRXBUF.all; //要对ALL寄存器读一次 SCIFFRX 为21~24可选1~4字节触发RXINT int processBytes = CProtocol_AddToStream(&g_proUart1, data & 0xFF); SciaRegs.SCIFFRX.bit.RXFFOVRCLR=1; // Clear Overflow flag PieCtrlRegs.PIEACK.bit.ACK9 = 1; SciaRegs.SCIFFRX.bit.RXFFINTCLR = 1; // clear Receive interrupt flag } void InitUart1Gpio() //SEARCH "GPIO Muxed Pins",GPIO16(TX),17(RX)复用表格序各为6, { EALLOW; //6=0110b, 01为高两位,为GMUX值,10为低两位,为MUX值 GpioCtrlRegs.GPAGMUX2.bit.GPIO16 = 0x01; GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 0x02; //16=TX GpioCtrlRegs.GPAGMUX2.bit.GPIO17 = 0x01; GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 0x02; //17=RX /* GpioCtrlRegs.GPAGMUX2.bit.GPIO29 = 0; GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1; //29=TX GpioCtrlRegs.GPAGMUX2.bit.GPIO28 = 0; GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1; //=RX EDIS; */ } void InitUart1(struct SCI_REGS *UART, u32 LSPCLK, u32 baud) //volatile struct { InitUart1Gpio(); SciaRegs.SCICTL1.all =0x0023; UART->SCICTL1.bit.SWRESET =0; UART->SCICTL1.bit.RXENA = 1;//Enable receiver UART->SCICTL1.bit.TXENA = 1;//Enable transmitter UART->SCIHBAUD.bit.BAUD = 0; UART->SCILBAUD.bit.BAUD = LSPCLK/baud/8 - 1; UART->SCICCR.all = 7; /* 8N1,8b data,1b stop,no parity UART->SCICCR.bit.SCICHAR = 7; // 8-bit UART->SCICCR.bit.PARITY = 0; //0=ODD 1=EVEN UART->SCICCR.bit.PARITYENA = 0; //0=disabled UART->SCICCR.bit.STOPBITS = 0; //One stop bit UART->SCICCR.bit.LOOPBKENA = 0; */ UART->SCICTL2.bit.RXBKINTENA =0; UART->SCICTL2.bit.TXINTENA =0; /* SciaRegs.SCIFFTX.bit.TXFIFORESET = 0; SciaRegs.SCIFFTX.bit.SCIRST = 1; SciaRegs.SCIFFTX.bit.TXFFIENA=1; SciaRegs.SCIFFTX.bit.SCIFFENA=1; SciaRegs.SCIFFTX.bit.TXFFIL = 1; //高吞吐量可以用4, 往同一地址一次写4个Byte,如下: for(i) SciaRegs.SCITXBUF=data[i]; */ // 中断方式 SciaRegs.SCIFFTX.all = 0xC021; SciaRegs.SCIFFRX.all= 0x0021; //21是1字节触发RXINT,22,23,24为2,3,4字节触发接收中断,高吞吐量时选择4 SciaRegs.SCIFFCT.all=0x00; /* //POLL方式, 关闭了TXFFIENA 和 RXFFIENA SciaRegs.SCIFFTX.all = 0xC001; SciaRegs.SCIFFRX.all= 0x0001; //21是1字节触发RXINT,22,23,24为2,3,4字节触发接收中断,高吞吐量时选择4 SciaRegs.SCIFFCT.all=0x00; */ UART->SCICTL1.bit.SWRESET = 1; //active SciaRegs.SCIFFRX.bit.RXFIFORESET=1; //active rx //memset(rx1, 0, MAX_PACK_LEN); memset(tx1_buf, 0, MAX_SEND_LEN); //rx1_cnt = 0; tx1_len = tx1_cnt = 0; }
我尝试了对SciaRegs寄存器的值进行检查发现,当能够正常进行串口通信的时候,SciaRegs.SCIRXST.all的值为2,当PC与单片机断开连接后这个值会变为176,恢复PC与单片机的连接后这个值又会变为178,由此我猜测可能是RX出现了错误但是没有复位导致的无法进入中断,但是我应该怎么做让RX能够支持串口热插拔呢?