LPC1769 UART读取混淆

时间:2014-01-17 20:00:56

标签: gps uart lpc

我有一个连接到LPC1769的GPS模块。当我的软件读取它们时,它们经常被搞砸了:

[gps] Parsing: GPGGA,021447.80$GPGGA,021448.800,,,,,0,0,,,M,,M,,*4B
[gps] Parsing: GPGSA,A,1,,,,,,,,,,,,,,,*1E
[gps] Parsing: GPGSV,1,1,00*79
[gps] Parsing: GPRMC,021448.800,V,,,,,0.00,0.00,060180,,,N*41
[gps] Parsing: GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
[gps] GPS location: 231200246:21448, no fix
[gps] Parsing: GPGGA,021449.80$GPGGA,021450.800,,,,,0,0,,,M,,M,,*42
[gps] Parsing: GPGSA,A,1,,,,,,,,,,,,,,,*1E
[gps] Parsing: GPRMC,021450.800,V,,,,,0.00,0.00,060180,,,N*48
[gps] Parsing: GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
[gps] GPS location: 45200246:21450, no fix
[gps] Parsing: GPGGA,021451.80$GPGGA,021452.800,,,,,0,0,,,M,,M,,*40
[gps] Parsing: GPGSA,A,1,,,,,,,,,,,,,,,*1E
[gps] Parsing: GPRMC,021452.800,V,,,,,0.00,0.00,060180,,,N*4A
[gps] Parsing: GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
[gps] GPS location: 135200246:21452, no fix

每个批处理的第一句以16个字节开头(它总是16个;我的代码在解析之前吞下初始$)。我的代码如下所示:

void gps_task(void) {
    char buffer[128], *ptr;

    locupd_packet_t loc;

    for (;;) {
        while (Uart_ReadChar(3) != '$');
        ptr = buffer;
        while ((*ptr++ = Uart_ReadChar(3)) != '\n');
        *(ptr - 1) = 0;

        Log_Debug("Parsing: %s", buffer);
        // Parse buffer
        Zeptos_Sleep(1000);
    }
}

如果我删除睡眠,问题只发生在第一次更新,然后所有其他人都读得很好。这是ReadChar代码:

char Uart_ReadChar(int uartn) {
    LPC_UART_TypeDef *uart = uarts[uartn];

    uint32_t status;
    while (((status = uart->LSR) & 1) == 0) {
        if (status & (1 << 1)) Log_Error("RX buffer overflow on UART %i", uartn);
        Zeptos_BlockOnIrq(UART0_IRQn + uartn);
    }

    return uart->RBR & 0xff;
}

当适当的UART IRQ触发时,BlockOnIrq调用返回。我检查溢出,但它不会发生。注意事项:

  • UART FIFO也恰好是16字节
  • 更新是1Hz,021447等是时间戳,因此可以看到所有其他更新都被完全读取,中间的更新在某处丢失

1 个答案:

答案 0 :(得分:0)

看起来您只在接收缓冲区为空时检查溢出。 在读取RBR寄存器之前,尝试检查cicle之后的溢出。