今天调试stm32w108发现Hal_Driver库中hal_uart.c文件内有一处错误,会导致串口读取到无效数据。
原始中断函数如下
void halSc1Isr(void)
{uint32_t interrupt;
/* this read and mask is performed in two steps otherwise the compiler
will complain about undefined order of __IO access */
interrupt = SC1_IT->ISR;
interrupt &= SC1_IT->IER;
while (interrupt != 0) {
SC1_IT->ISR = interrupt; /* acknowledge the interrupts early */ /*这里清标志位不起作用*/
/* RX events */
if ( interrupt & (SC_IER_RXNEIE | /* RX has data */
SC_IER_OVRIE | /* RX Overrun error */
SC_IER_FEIE | /* RX Frame error */
SC_IER_PEIE ) /* RX Parity error */
) {
halInternalUart1RxIsr();
}
/* TX events */
if ( interrupt & (SC_IER_TXEIE | /* TX has room */
SC_IER_IDLEIE ) /* TX idle (more room) */
) {
halInternalUart1TxIsr();
}
interrupt = SC1_IT->ISR;
interrupt &= SC1_IT->IER;
}
}
SC1_IT->ISR = interrupt;这句不能清RXNE,因为数据还没有被读取,即使清了标志位,芯片又自动会打上标志位,所以应该放到读取DR之后。对于TXE标志位也是如此。