Andrei Carp / Mbed 2 deprecated mbed_uart0_echo

Dependencies:   Queue mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ringbuf.cpp Source File

ringbuf.cpp

00001 #include "ringbuf.h"
00002 #include "types.h"
00003 
00004 uint8 ICACHE_FLASH_ATTR ringbuf_init(ringbuf_t *r, uint8* buf, uint16 size)
00005 {
00006     if( (r == NULL) || (buf == NULL) || (size < 2) )
00007         return FALSE;
00008     
00009     r->p_o = r->p_r = r->p_w = buf;
00010     r->fill_cnt = 0;
00011     r->size = size;
00012     r->p_e = r->p_o + r->size;
00013     return TRUE;
00014 }
00015 
00016 uint8 ICACHE_FLASH_ATTR ringbuf_put(ringbuf_t *r, uint8 c)
00017 {
00018     if(r->fill_cnt>=r->size)
00019         return FALSE;                       // ring buffer is full, this should be atomic operation
00020 
00021     r->fill_cnt++;                          // increase filled slots count, this should be atomic operation
00022 
00023     *r->p_w++ = c;                          // put character into buffer
00024 
00025     if(r->p_w >= r->p_e)                        // rollback if write pointer go pass
00026         r->p_w = r->p_o;                        // the physical boundary
00027     
00028     return TRUE;
00029 }
00030 
00031 uint8 ICACHE_FLASH_ATTR ringbuf_get(ringbuf_t *r, uint8 * c)
00032 {
00033     if(r->fill_cnt == 0)
00034         return FALSE;               // ring buffer is empty, this should be atomic operation
00035     
00036     r->fill_cnt--;                              // decrease filled slots count
00037     
00038     *c = *r->p_r++;                             // get the character out
00039     
00040     if(r->p_r >= r->p_e)                        // rollback if write pointer go pass
00041         r->p_r = r->p_o;                        // the physical boundary
00042     
00043     return TRUE;
00044 }
00045 
00046 uint16 ICACHE_FLASH_ATTR ringbuf_gets(ringbuf_t *r, uint8 * buff, uint16 size)
00047 {
00048     uint16 read=0;
00049     while(size)
00050     {
00051         if(ringbuf_get(r,buff++) == FALSE)
00052             break;
00053 
00054         read++;
00055         size--;
00056     }
00057     return read;
00058 }
00059 
00060 uint16 ICACHE_FLASH_ATTR ringbuf_puts(ringbuf_t *r, uint8 * buff, uint16 size)
00061 {
00062     uint16 write=0;
00063     while(size)
00064     {
00065         if(ringbuf_put(r,*buff++) == FALSE)
00066             break;
00067 
00068         write++;
00069         size--;
00070     }
00071     return write;
00072 }
00073 
00074 uint8 ICACHE_FLASH_ATTR ringbuf_clear(ringbuf_t *r)
00075 {
00076     r->p_r = r->p_w = r->p_o;
00077     r->fill_cnt = 0;
00078     return TRUE;
00079 }