tested
Dependencies: Queue mbed-rtos mbed
ringbuf.cpp@0:265e6a986bf1, 2017-07-27 (annotated)
- Committer:
- andreixc
- Date:
- Thu Jul 27 12:53:20 2017 +0000
- Revision:
- 0:265e6a986bf1
init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andreixc | 0:265e6a986bf1 | 1 | #include "ringbuf.h" |
andreixc | 0:265e6a986bf1 | 2 | #include "types.h" |
andreixc | 0:265e6a986bf1 | 3 | |
andreixc | 0:265e6a986bf1 | 4 | uint8 ICACHE_FLASH_ATTR ringbuf_init(ringbuf_t *r, uint8* buf, uint16 size) |
andreixc | 0:265e6a986bf1 | 5 | { |
andreixc | 0:265e6a986bf1 | 6 | if( (r == NULL) || (buf == NULL) || (size < 2) ) |
andreixc | 0:265e6a986bf1 | 7 | return FALSE; |
andreixc | 0:265e6a986bf1 | 8 | |
andreixc | 0:265e6a986bf1 | 9 | r->p_o = r->p_r = r->p_w = buf; |
andreixc | 0:265e6a986bf1 | 10 | r->fill_cnt = 0; |
andreixc | 0:265e6a986bf1 | 11 | r->size = size; |
andreixc | 0:265e6a986bf1 | 12 | r->p_e = r->p_o + r->size; |
andreixc | 0:265e6a986bf1 | 13 | return TRUE; |
andreixc | 0:265e6a986bf1 | 14 | } |
andreixc | 0:265e6a986bf1 | 15 | |
andreixc | 0:265e6a986bf1 | 16 | uint8 ICACHE_FLASH_ATTR ringbuf_put(ringbuf_t *r, uint8 c) |
andreixc | 0:265e6a986bf1 | 17 | { |
andreixc | 0:265e6a986bf1 | 18 | if(r->fill_cnt>=r->size) |
andreixc | 0:265e6a986bf1 | 19 | return FALSE; // ring buffer is full, this should be atomic operation |
andreixc | 0:265e6a986bf1 | 20 | |
andreixc | 0:265e6a986bf1 | 21 | r->fill_cnt++; // increase filled slots count, this should be atomic operation |
andreixc | 0:265e6a986bf1 | 22 | |
andreixc | 0:265e6a986bf1 | 23 | *r->p_w++ = c; // put character into buffer |
andreixc | 0:265e6a986bf1 | 24 | |
andreixc | 0:265e6a986bf1 | 25 | if(r->p_w >= r->p_e) // rollback if write pointer go pass |
andreixc | 0:265e6a986bf1 | 26 | r->p_w = r->p_o; // the physical boundary |
andreixc | 0:265e6a986bf1 | 27 | |
andreixc | 0:265e6a986bf1 | 28 | return TRUE; |
andreixc | 0:265e6a986bf1 | 29 | } |
andreixc | 0:265e6a986bf1 | 30 | |
andreixc | 0:265e6a986bf1 | 31 | uint8 ICACHE_FLASH_ATTR ringbuf_get(ringbuf_t *r, uint8 * c) |
andreixc | 0:265e6a986bf1 | 32 | { |
andreixc | 0:265e6a986bf1 | 33 | if(r->fill_cnt == 0) |
andreixc | 0:265e6a986bf1 | 34 | return FALSE; // ring buffer is empty, this should be atomic operation |
andreixc | 0:265e6a986bf1 | 35 | |
andreixc | 0:265e6a986bf1 | 36 | r->fill_cnt--; // decrease filled slots count |
andreixc | 0:265e6a986bf1 | 37 | |
andreixc | 0:265e6a986bf1 | 38 | *c = *r->p_r++; // get the character out |
andreixc | 0:265e6a986bf1 | 39 | |
andreixc | 0:265e6a986bf1 | 40 | if(r->p_r >= r->p_e) // rollback if write pointer go pass |
andreixc | 0:265e6a986bf1 | 41 | r->p_r = r->p_o; // the physical boundary |
andreixc | 0:265e6a986bf1 | 42 | |
andreixc | 0:265e6a986bf1 | 43 | return TRUE; |
andreixc | 0:265e6a986bf1 | 44 | } |
andreixc | 0:265e6a986bf1 | 45 | |
andreixc | 0:265e6a986bf1 | 46 | uint16 ICACHE_FLASH_ATTR ringbuf_gets(ringbuf_t *r, uint8 * buff, uint16 size) |
andreixc | 0:265e6a986bf1 | 47 | { |
andreixc | 0:265e6a986bf1 | 48 | uint16 read=0; |
andreixc | 0:265e6a986bf1 | 49 | while(size) |
andreixc | 0:265e6a986bf1 | 50 | { |
andreixc | 0:265e6a986bf1 | 51 | if(ringbuf_get(r,buff++) == FALSE) |
andreixc | 0:265e6a986bf1 | 52 | break; |
andreixc | 0:265e6a986bf1 | 53 | |
andreixc | 0:265e6a986bf1 | 54 | read++; |
andreixc | 0:265e6a986bf1 | 55 | size--; |
andreixc | 0:265e6a986bf1 | 56 | } |
andreixc | 0:265e6a986bf1 | 57 | return read; |
andreixc | 0:265e6a986bf1 | 58 | } |
andreixc | 0:265e6a986bf1 | 59 | |
andreixc | 0:265e6a986bf1 | 60 | uint16 ICACHE_FLASH_ATTR ringbuf_puts(ringbuf_t *r, uint8 * buff, uint16 size) |
andreixc | 0:265e6a986bf1 | 61 | { |
andreixc | 0:265e6a986bf1 | 62 | uint16 write=0; |
andreixc | 0:265e6a986bf1 | 63 | while(size) |
andreixc | 0:265e6a986bf1 | 64 | { |
andreixc | 0:265e6a986bf1 | 65 | if(ringbuf_put(r,*buff++) == FALSE) |
andreixc | 0:265e6a986bf1 | 66 | break; |
andreixc | 0:265e6a986bf1 | 67 | |
andreixc | 0:265e6a986bf1 | 68 | write++; |
andreixc | 0:265e6a986bf1 | 69 | size--; |
andreixc | 0:265e6a986bf1 | 70 | } |
andreixc | 0:265e6a986bf1 | 71 | return write; |
andreixc | 0:265e6a986bf1 | 72 | } |
andreixc | 0:265e6a986bf1 | 73 | |
andreixc | 0:265e6a986bf1 | 74 | uint8 ICACHE_FLASH_ATTR ringbuf_clear(ringbuf_t *r) |
andreixc | 0:265e6a986bf1 | 75 | { |
andreixc | 0:265e6a986bf1 | 76 | r->p_r = r->p_w = r->p_o; |
andreixc | 0:265e6a986bf1 | 77 | r->fill_cnt = 0; |
andreixc | 0:265e6a986bf1 | 78 | return TRUE; |
andreixc | 0:265e6a986bf1 | 79 | } |