我正在为STM32F处理器上的UART开发一个双缓冲区。下面的示例在UART ISR每次运行时将数据添加到缓冲区。当主进程想要从中提取数据时,这个缓冲区就会被交换。
我担心可能的拐角情况。在交换缓冲区之后,在重置接收缓冲区计数之前,ISR可能会触发。这意味着我的缓冲区大小和计数将不同步。
处理这个角落的案子最好的方法是什么?

uint8_t rxBufferA[MAX_RX_BUFFER_SIZE];
uint8_t rxBufferB[MAX_RX_BUFFER_SIZE];
uint8_t *receivingBuffer;
uint8_t *processingBuffer;
volatile uint32_t rxBufferSize;

uart_isr() {
  receivingBuffer[rxBufferSize++] = RECEIVE_DATA_REGISTER
}


main() {

  receivingBuffer = &rxBufferA;
  processingBuffer = &rxBufferB;

  while(1) {

    if (rxBufferSize > 0) {
      uint32_t i = 0;
      uint8_t *ptemp = receivingBuffer;
      uint8_t bufferSize = 0;

      /* zero out processing buffer */
      memset(processingBuffer, 0, MAX_RX_BUFFER_SIZE);

      /* swap receiving and processing buffers */
      receivingBuffer = processingBuffer
      processingBuffer = ptemp;

      /* WHAT IF THE ISR FIRES HERE??? */

      /* save off buffer size and reset count */
      bufferSize = rxBufferSize;
      rxBufferSize = 0;

      /* process rx data */
    }
  }
}

最佳答案

你需要关键部分。交换缓冲区时禁用用户中断。
BTW不需要调零

关于c - 在双缓冲区交换期间处理UART ISR,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56137758/

10-10 18:35
查看更多