ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Software FIFO Buffer for UART Communication

2021-09-05 15:02:49  阅读:218  来源: 互联网

标签:tx Buffer Communication rx fifo UART flag buffer data


sw_fifo.c

////////////////////////////////////////////////////////////////////////////////////////
/* enter necessary header files for proper interrupt vector and UART/USART visibility */
////////////////////////////////////////////////////////////////////////////////////////
 
#include <sw_fifo.h>
 
typedef struct {
  uint8_t  data_buf[FIFO_BUFFER_SIZE]; // FIFO buffer
  uint16_t i_first;                    // index of oldest data byte in buffer
  uint16_t i_last;                     // index of newest data byte in buffer
  uint16_t num_bytes;                  // number of bytes currently in buffer
}sw_fifo_typedef;
 
sw_fifo_typedef rx_fifo = { {0}, 0, 0, 0 }; // declare a receive software buffer
sw_fifo_typedef tx_fifo = { {0}, 0, 0, 0 }; // declare a transmit software buffer
 
 
/***************************************************************************************************************/
// UART receive interrupt sub-routine
//  - interrupts when valid data exists in rx hardware buffer
//  - checks if there's room in the rx software buffer
//  - if there's room, it transfers the received data into the sw buffer
//  - automatically handles "uart_rx_buffer_full_flag"
//  - sets overflow flag upon software buffer overflow (doesn't overwrite existing data)
//////////////////////////////////////////////
/* enter name of UART RX IRQ Handler here */ {
//////////////////////////////////////////////
   
  /* Explicitly clear the source of interrupt if necessary */
   
  if(rx_fifo.num_bytes == FIFO_BUFFER_SIZE) {      // if the sw buffer is full
    uart_rx_fifo_ovf_flag = 1;                     // set the overflow flag
  }else if(rx_fifo.num_bytes < FIFO_BUFFER_SIZE) { // if there's room in the sw buffer
     
    ///////////////////////////////////////////////////
    /* read error/status reg here if desired         */
    /* handle any hardware RX errors here if desired */
    ///////////////////////////////////////////////////
     
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    rx_fifo.data_buf[rx_fifo.i_last] = /* enter pointer to UART rx hardware buffer here */ // store the received data as the newest data element in the sw buffer
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    rx_fifo.i_last++;                              // increment the index of the most recently added element
    rx_fifo.num_bytes++;                           // increment the bytes counter
  }
  if(rx_fifo.num_bytes == FIFO_BUFFER_SIZE) {      // if sw buffer just filled up
    uart_rx_fifo_full_flag = 1;                    // set the RX FIFO full flag
  }
  if(rx_fifo.i_last == FIFO_BUFFER_SIZE) {         // if the index has reached the end of the buffer,
    rx_fifo.i_last = 0;                            // roll over the index counter
  }
  uart_rx_fifo_not_empty_flag = 1;                 // set received-data flag
} // end UART RX IRQ handler
/***************************************************************************************************************/
 
 
/***************************************************************************************************************/
// UART transmit interrupt sub-routine
//  - interrupts when the tx hardware buffer is empty
//  - checks if data exists in the tx software buffer
//  - if data exists, it places the oldest element of the sw buffer into the tx hardware buffer
//  - if the sw buffer is emptied, it disables the "hw buffer empty" interrupt
//  - automatically handles "uart_tx_buffer_full_flag"
//////////////////////////////////////////////
/* enter name of UART TX IRQ Handler here */ {
//////////////////////////////////////////////
   
  /* Explicitly clear the source of interrupt if necessary */
   
  if(tx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // if the sw buffer is full
    uart_tx_fifo_full_flag = 0;               // clear the buffer full flag because we are about to make room
  }
  if(tx_fifo.num_bytes > 0) {                 // if data exists in the sw buffer
     
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /* enter pointer to UART tx hardware buffer here */ = tx_fifo.data_buf[tx_fifo.i_first]; // place oldest data element in the TX hardware buffer
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    tx_fifo.i_first++;                        // increment the index of the oldest element
    tx_fifo.num_bytes--;                      // decrement the bytes counter
  }
  if(tx_fifo.i_first == FIFO_BUFFER_SIZE) {   // if the index has reached the end of the buffer,
    tx_fifo.i_first = 0;                      // roll over the index counter
  }
  if(tx_fifo.num_bytes == 0) {                // if no more data exists
 
    uart_tx_fifo_not_empty_flag = 0;          // clear flag
 
    //////////////////////////////////////////////////////////////////////////
    /* disable UART "TX hw buffer empty" interrupt here                     */
    /* if using shared RX/TX hardware buffer, enable RX data interrupt here */
    //////////////////////////////////////////////////////////////////////////
     
  }
}// end UART TX IRQ handler
/***************************************************************************************************************/
 
 
/***************************************************************************************************************/
// UART data transmit function
//  - checks if there's room in the transmit sw buffer
//  - if there's room, it transfers data byte to sw buffer
//  - automatically handles "uart_tx_buffer_full_flag"
//  - sets the overflow flag upon software buffer overflow (doesn't overwrite existing data)
//  - if this is the first data byte in the buffer, it enables the "hw buffer empty" interrupt
void uart_send_byte(uint8_t byte) {
   
  ///////////////////////////////////////////////////////////
  /* disable interrupts while manipulating buffer pointers */
  ///////////////////////////////////////////////////////////
 
  if(tx_fifo.num_bytes == FIFO_BUFFER_SIZE) {      // no room in the sw buffer
    uart_tx_fifo_ovf_flag = 1;                     // set the overflow flag
  }else if(tx_fifo.num_bytes < FIFO_BUFFER_SIZE) { // if there's room in the sw buffer
    tx_fifo.data_buf[tx_fifo.i_last] = byte;       // transfer data byte to sw buffer
    tx_fifo.i_last++;                              // increment the index of the most recently added element
    tx_fifo.num_bytes++;                           // increment the bytes counter
  }
  if(tx_fifo.num_bytes == FIFO_BUFFER_SIZE) {      // if sw buffer is full
    uart_tx_fifo_full_flag = 1;                    // set the TX FIFO full flag
  }
  if(tx_fifo.i_last == FIFO_BUFFER_SIZE) {         // if the "new data" index has reached the end of the buffer,
    tx_fifo.i_last = 0;                            // roll over the index counter
  }
  
  ///////////////////////
  /* enable interrupts */
  ///////////////////////
 
  if(tx_fifo.num_bytes > 0) {                      // if there is data in the buffer
 
    uart_tx_fifo_not_empty_flag = 1;               // set flag
     
    ///////////////////////////////////////////////////////////////////////////
    /* if using shared RX/TX hardware buffer, disable RX data interrupt here */
    /* enable UART "TX hw buffer empty" interrupt here                       */
    ///////////////////////////////////////////////////////////////////////////
     
  }
}
/***************************************************************************************************************/
 
 
/***************************************************************************************************************/
// UART data receive function
//  - checks if data exists in the receive sw buffer
//  - if data exists, it returns the oldest element contained in the buffer
//  - automatically handles "uart_rx_buffer_full_flag"
//  - if no data exists, it clears the uart_rx_flag
uint8_t uart_get_byte(void) {          
      
  ///////////////////////////////////////////////////////////
  /* disable interrupts while manipulating buffer pointers */
  ///////////////////////////////////////////////////////////
   
  uint8_t byte = 0;
  if(rx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // if the sw buffer is full
    uart_rx_fifo_full_flag = 0;               // clear the buffer full flag because we are about to make room
  }
  if(rx_fifo.num_bytes > 0) {                 // if data exists in the sw buffer
    byte = rx_fifo.data_buf[rx_fifo.i_first]; // grab the oldest element in the buffer
    rx_fifo.i_first++;                        // increment the index of the oldest element
    rx_fifo.num_bytes--;                      // decrement the bytes counter
  }else{                                      // RX sw buffer is empty
    uart_rx_fifo_not_empty_flag = 0;          // clear the rx flag
  }
  if(rx_fifo.i_first == FIFO_BUFFER_SIZE) {   // if the index has reached the end of the buffer,
    rx_fifo.i_first = 0;                      // roll over the index counter
  }
   
  ///////////////////////
  /* enable interrupts */
  ///////////////////////
 
  return byte;                                // return the data byte
}
/***************************************************************************************************************/

sw_fifo.h

#define FIFO_BUFFER_SIZE 128 // software buffer size (in bytes)
 
// UART data transmit function
//  - checks if there's room in the transmit sw buffer
//  - if there's room, it transfers data byte to sw buffer
//  - automatically handles "uart_tx_buffer_full_flag"
//  - sets the overflow flag upon software buffer overflow (doesn't overwrite existing data)
//  - if this is the first data byte in the buffer, it enables the "hw buffer empty" interrupt
void uart_send_byte(uint8_t byte);
 
 
// UART data receive function
//  - checks if data exists in the receive sw buffer
//  - if data exists, it returns the oldest element contained in the buffer
//  - automatically handles "uart_rx_buffer_full_flag"
//  - if no data exists, it clears the uart_rx_flag
uint8_t uart_get_byte(void);
 
volatile extern uint8_t uart_rx_fifo_not_empty_flag; // this flag is automatically set and cleared by the software buffer
volatile extern uint8_t uart_rx_fifo_full_flag;      // this flag is automatically set and cleared by the software buffer
volatile extern uint8_t uart_rx_fifo_ovf_flag;       // this flag is not automatically cleared by the software buffer
volatile extern uint8_t uart_tx_fifo_full_flag;      // this flag is automatically set and cleared by the software buffer
volatile extern uint8_t uart_tx_fifo_ovf_flag;       // this flag is not automatically cleared by the software buffer
volatile extern uint8_t uart_tx_fifo_not_empty_flag; // this flag is automatically set and cleared by the software buffer

 

copyright

https://forum.digikey.com/t/software-fifo-buffer-for-uart-communication/13476

 

标签:tx,Buffer,Communication,rx,fifo,UART,flag,buffer,data
来源: https://www.cnblogs.com/dong1/p/15229427.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有