gpt4 book ai didi

C: UART, ISR, circular FIFO buffer: 有时会以错误的顺序发送字节

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:58 26 4
gpt4 key购买 nike

我因间歇性错误而拔头发。我正在异步接收和传输字节(在 PIC16F77 上),并实现了一个用于接收和传输的循环软件 FIFO 缓冲区,并结合了一个在可以发送或接收到字节时触发的中断服务例程。

问题 是有时要传输 的字节是以错误的顺序 完成的。

我将非常感谢:

  1. 关于调试它的建议,或者
  2. 协助发现代码中的问题

到目前为止的进展:

  • 它似乎只在接收到一些字节时才会发生——但是我一直没有成功地进一步缩小范围。据我所知,我没有遇到任何欠载或重载。
  • 当我将 send_char() 更改为以下之一时,它可以正常工作:1. 通过等待硬件缓冲区中的空间来绕过软件缓冲区,并将字节直接放入其中,或者 2. 将字节放入进入软件缓冲区,即使硬件缓冲区中有空间。

代码:(硬件变量的描述见问题底部)

unsigned volatile char volatile rc_buff[16];
unsigned char volatile rc_begin = 0;
unsigned char volatile rc_next_free = 0;
unsigned char volatile rc_count = 0;

unsigned volatile char volatile tx_buff[16];
unsigned char volatile tx_begin = 0;
unsigned char volatile tx_next_free = 0;
unsigned char volatile tx_count = 0;

__interrupt isr(){
// If a character has arrived in the hardware buffer
if (RCIF){
// Put it in the software buffer
if (rc_count >= 16) die(ERROR_RC_OVERFLOW);
rc_buff[rc_next_free] = RCREG;
rc_next_free = (rc_next_free + 1) % 16;
rc_count++;
}
// If there is space in hardware FIFO, and interrupt
// has been enabled because stuff in software FIFO needs to be sent.
if (TXIE && TXIF){
// Put a byte from s/w fifo to h/w fifo.
// (Here, tx_count is always > 0 (in theory))
TXREG = tx_buff[tx_begin];
tx_count--;
tx_begin = (tx_begin + 1) % 16;
// If this was the last byte in the s/w FIFO,
// disable the interrupt: we don't care
// when it has finished sending.
if(tx_count==0) TXIE = 0;
}
}
void send_char(char c){
// disable interrupts to avoid bad things happening
di();
// if the hardware buffer is empty,
if (TXIF){
// put a byte directly into the hardware FIFO
TXREG = c;
} else {
// cannot send byte directly so put in the software FIFO
if (tx_count >= 16) die(ERROR_TX_OVERFLOW);
tx_buff[tx_next_free] = c;
tx_next_free = (tx_next_free + 1) % 16;
tx_count++;
// Enable TX interrupt since it now has something
// it needs to transfer from the s/w FIFO to the h/w FIFO
TXIE = 1;
}
ei();
}
char get_char(){
// wait for a byte to appear in the s/w buffer
while (!rc_count) {
// If the h/w buffer overflowed, die with error
if (OERR) die(ERROR_RC_HW_OVERFLOW)
}
// disable interrupts to avoid bad things happening
di();
unsigned char c = rc_buff[rc_begin];
rc_count--;
rc_begin = (rc_begin + 1) % 16;
ei();
return c;
}
void send_str(const unsigned char * str){
unsigned char char_idx = 0;
// until we reach the end-of-string null character,
while (str[char_idx]){
// queue a character for sending
send_char(str[char_idx++]);
}
}

硬件变量说明:

作为引用,以下是映射到硬件寄存器和标志的( volatile )变量:

RCIF // Read-only receive flag:  True == byte(s) are waiting in hardware receive FIFO
TXIF // Read-only transmit flag: True == there is space in the hardware transmit FIFO
RCREG // Read only: Holds the next byte from the hardware FIFO that has been received
TXREG // Write-only: Assigning a byte to this transfers the byte to the hardware transmit FIFO
TXIE // Read/Write: Enable transmit interrupt: True == trigger ISR when TX h/w FIFO has space
RCIE // Read/Write: Enable receive interrupt: True == trigger ISR when RC h/w FIFO has a byte to be read

此外,以下是特殊的内联函数,它们暂停/恢复中断以保持多个分组操作的原子性。 (ISR不能被任何东西打断,包括其他中断)

di() // suspend interrupts
ei() // re-enable interrupts

最佳答案

嗯。我认为您的程序中缺少一些逻辑(我只介绍发送部分,因为接收部分似乎可以正常工作?):

如果发送硬件 FIFO 中有空间,则会触发中断例程。然后,您从 sw 缓冲区发送一个字节,调整索引并返回(请注意,在那之后 sw 缓冲区中可能仍有一些字节在排队)。

每当您发送一个字节时,您都会在 HW fifo 中寻找空间并将该字节直接放在那里,如果没有,您将它放入 SW 缓冲区中排队。

在我看来,问题似乎是您希望中断例程在返回 send_char() 之前耗尽软件缓冲区,但事实并非如此。从中断返回后,下一条指令将被完全执行(一条指令中间没有中断)。如果下一条指令是 send_char() 中的 di(),则不会发生此中断,并且 sw 缓冲区中仍有字节只能稍后发送(太晚了)。

我宁愿将字节从 send_char() 排入 sw 缓冲区,而不是直接从 send_char() 写入 fifo,或者在直接访问 hw fifo 之前额外检查 sw 缓冲区是否为空。

关于C: UART, ISR, circular FIFO buffer: 有时会以错误的顺序发送字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22439376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com