写了一个串口接收的 FIFO ,一直都看不出有什么问题!

2019-12-23 18:38发布

bsp_US1_RX_int (u8 Data)       这一条是串口接收中断的时候调用,并把出带入。
bsp_US1_RX    (u8 * Data)      这一条是取出缓冲区的数据

现在问题是这样的,是不是的数据会丢一两个位,并且丢了之后,以后收到的数据都会出现错位!
正确的应该是  0123456789  ,但错位后收到的数据  5012345678。  



u16     bsp_US1_RX       (u8 * Data)
{   
    short temp=0;
    while(US1_RX_FIFO_user)
    {
        *Data++ = US1_RX_FIFO[US1_RX_FIFO_tail++];              
        if(US1_RX_FIFO_tail == US1_RX_FIFO_Siz)     
            US1_RX_FIFO_tail = 0;                  
        US1_RX_FIFO_user--;

        temp++;
    }
   
    return temp;
}
inline s8      bsp_US1_RX_int   (u8 Data)
{   
    if(US1_RX_FIFO_Siz - US1_RX_FIFO_user -1 )
    {
        US1_RX_FIFO[US1_RX_FIFO_head++] = Data;      
        if(US1_RX_FIFO_head == US1_RX_FIFO_Siz)     
            US1_RX_FIFO_head = 0;                  
        US1_RX_FIFO_user++;
    }
    else
    {
        return RXbuff_overflow;
    }
   
    return 0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
30条回答
迈锐数据
1楼-- · 2019-12-25 23:24
t3486784401 发表于 2016-8-15 17:21
中断和主程序可以认为是两个线程,访问公用的数据(临界区)时,需要通过加锁来确保原子操作。否则数据可能 ...

  中断只写   main只读     需要强行上锁吗?
shangdawei
2楼-- · 2019-12-26 01:38
 精彩回答 2  元偷偷看……
shangdawei
3楼-- · 2019-12-26 01:43
环形缓冲器

https://zh.wikipedia.org/wiki/%E ... 9%E8%A1%9D%E5%8D%80

Capture_0048.jpg (52.41 KB, 下载次数: 0)

下载附件

2016-8-16 18:55 上传



Linux内核的kfifo

在Linux内核文件kfifo.h和kfifo.c中,定义了一个先进先出圆形缓冲区实现。

如果只有一个读线程、一个写线程,二者没有共享的被修改的控制变量,那么可以证明这种情况下不需要并发控制。

kfifo就满足上述条件。

kfifo要求缓冲区长度必须为2的幂。

读、写指针分别是无符号整型变量。把读写指针变换为缓冲区内的索引值,仅需要“按位与”操作:
(指针值 按位与 (缓冲区长度-1))。这避免了计算代价高昂的“求余”操作。且下述关系总是成立:

读指针 + 缓冲区存储的数据长度 == 写指针

即使在写指针达到了无符号整型的上界,上溢出后写指针的值小于读指针的值,上述关系仍然保持成立(这是因为无符号整型加法的性质)。
kfifo的写操作,首先计算缓冲区中当前可写入存储空间的数据长度:

len = min{待写入数据长度, 缓冲区长度 - (写指针 - 读指针)}

然后,分两段写入数据。

第一段是从写指针开始向缓冲区末尾方向;
第二段是从缓冲区起始处写入余下的可写入数据,这部分可能数据长度为0即并无实际数据写入。
shangdawei
4楼-- · 2019-12-26 05:28

POSIX优化实现


  1. #include <sys/mman.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>

  4. #define report_exceptional_condition() abort ()

  5. struct ring_buffer
  6. {
  7.   void * address;

  8.   unsigned long count_bytes;
  9.   unsigned long write_offset_bytes;
  10.   unsigned long read_offset_bytes;
  11. };

  12. //Warning order should be at least 12 for Linux
  13. void ring_buffer_create( struct ring_buffer * buffer, unsigned long order )
  14. {
  15.   char path[ ] = "/dev/shm/ring-buffer-XXXXXX";
  16.   int file_descriptor;
  17.   void * address;
  18.   int status;

  19.   file_descriptor = mkstemp( path );
  20.   if ( file_descriptor < 0 )
  21.     report_exceptional_condition();

  22.   status = unlink( path );
  23.   if ( status )
  24.     report_exceptional_condition();

  25.   buffer->count_bytes = 1UL << order;
  26.   buffer->write_offset_bytes = 0;
  27.   buffer->read_offset_bytes = 0;

  28.   status = ftruncate( file_descriptor, buffer->count_bytes );
  29.   if ( status )
  30.     report_exceptional_condition();

  31.   buffer->address = mmap( NULL, buffer->count_bytes << 1, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE,  - 1, 0 );

  32.   if ( buffer->address == MAP_FAILED )
  33.     report_exceptional_condition();

  34.   address = mmap( buffer->address, buffer->count_bytes, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, file_descriptor,
  35.     0 );

  36.   if ( address != buffer->address )
  37.     report_exceptional_condition();

  38.   address = mmap( buffer->address + buffer->count_bytes, buffer->count_bytes, PROT_READ | PROT_WRITE, MAP_FIXED |
  39.     MAP_SHARED, file_descriptor, 0 );

  40.   if ( address != buffer->address + buffer->count_bytes )
  41.     report_exceptional_condition();

  42.   status = close( file_descriptor );
  43.   if ( status )
  44.     report_exceptional_condition();
  45. }

  46. void ring_buffer_free( struct ring_buffer * buffer )
  47. {
  48.   int status;

  49.   status = munmap( buffer->address, buffer->count_bytes << 1 );
  50.   if ( status )
  51.     report_exceptional_condition();
  52. }

  53. void * ring_buffer_write_address( struct ring_buffer * buffer )
  54. {
  55.   /*** void pointer arithmetic is a constraint violation. ***/
  56.   return buffer->address + buffer->write_offset_bytes;
  57. }

  58. void ring_buffer_write_advance( struct ring_buffer * buffer, unsigned long count_bytes )
  59. {
  60.   buffer->write_offset_bytes += count_bytes;
  61. }

  62. void * ring_buffer_read_address( struct ring_buffer * buffer )
  63. {
  64.   return buffer->address + buffer->read_offset_bytes;
  65. }

  66. void ring_buffer_read_advance( struct ring_buffer * buffer, unsigned long count_bytes )
  67. {
  68.   buffer->read_offset_bytes += count_bytes;

  69.   if ( buffer->read_offset_bytes >= buffer->count_bytes )
  70.   {
  71.      /*如果读指针大于等于缓冲区长度,那些读写指针同时折返回[0, buffer_size]范围内  */
  72.     buffer->read_offset_bytes -= buffer->count_bytes;
  73.     buffer->write_offset_bytes -= buffer->count_bytes;
  74.   }
  75. }

  76. unsigned long ring_buffer_count_bytes( struct ring_buffer * buffer )
  77. {
  78.   return buffer->write_offset_bytes - buffer->read_offset_bytes;
  79. }

  80. unsigned long ring_buffer_count_free_bytes( struct ring_buffer * buffer )
  81. {
  82.   return buffer->count_bytes - ring_buffer_count_bytes( buffer );
  83. }

  84. void ring_buffer_clear( struct ring_buffer * buffer )
  85. {
  86.   buffer->write_offset_bytes = 0;
  87.   buffer->read_offset_bytes = 0;
  88. }

  89. /*
  90. * Note, that initial anonymous mmap() can be avoided - after initial mmap() for descriptor fd,
  91. * you can try mmap() with hinted address as (buffer->address + buffer->count_bytes)
  92. * and if it fails - another one with hinted address as (buffer->address - buffer->count_bytes).
  93. *
  94. * Make sure MAP_FIXED is not used in such case, as under certain situations it could end with segfault.
  95. * The advantage of such approach is, that it avoids requirement to map twice the amount
  96. * you need initially (especially useful e.g. if you want to use hugetlbfs and the allowed amount is limited)
  97. * and in context of gcc/glibc - you can avoid certain feature macros
  98. * (MAP_ANONYMOUS usually requires one of: _BSD_SOURCE, _SVID_SOURCE or _GNU_SOURCE).
  99. */
复制代码
huangqi412
5楼-- · 2019-12-26 10:15
我是只能写n-1个免得判断相等时候是空还是满 为了一个元素多费功夫
yy8047
6楼-- · 2019-12-26 15:29
感觉那个US1_RX_FIFO_user用得太乱了,我都是一个IP只管往里写,一个IP只管往外读,如果写到IP重合那就溢出丢数据了,只要两个IP不等则有数据缓存

一周热门 更多>