Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_fifo.c
1 #include <bc_fifo.h>
2 #include <bc_irq.h>
3 
4 void bc_fifo_init(bc_fifo_t *fifo, void *buffer, size_t size)
5 {
6  fifo->buffer = buffer;
7  fifo->size = size;
8  fifo->head = 0;
9  fifo->tail = 0;
10 }
11 
13 {
14  fifo->head = 0;
15  fifo->tail = 0;
16 }
17 
18 size_t bc_fifo_write(bc_fifo_t *fifo, const void *buffer, size_t length)
19 {
20  // Disable interrupts
22 
23  // For each byte in buffer...
24  for (size_t i = 0; i < length; i++)
25  {
26  if ((fifo->head + 1) == fifo->tail)
27  {
28  // Enable interrupts
29  bc_irq_enable();
30 
31  // Return number of bytes written
32  return i;
33  }
34 
35  if (((fifo->head + 1) == fifo->size) && (fifo->tail == 0))
36  {
37  // Enable interrupts
38  bc_irq_enable();
39 
40  // Return number of bytes written
41  return i;
42  }
43 
44  *((uint8_t *) fifo->buffer + fifo->head) = *(uint8_t *) buffer;
45 
46  buffer = (uint8_t *) buffer + 1;
47 
48  fifo->head++;
49 
50  if (fifo->head == fifo->size)
51  {
52  fifo->head = 0;
53  }
54  }
55 
56  // Enable interrupts
57  bc_irq_enable();
58 
59  // Return number of bytes written
60  return length;
61 }
62 
63 size_t bc_fifo_read(bc_fifo_t *fifo, void *buffer, size_t length)
64 {
65  // Disable interrupts
67 
68  // For desired number of bytes...
69  for (size_t i = 0; i < length; i++)
70  {
71  if (fifo->tail != fifo->head)
72  {
73  *(uint8_t *) buffer = *((uint8_t *) fifo->buffer + fifo->tail);
74 
75  buffer = (uint8_t *) buffer + 1;
76 
77  fifo->tail++;
78 
79  if (fifo->tail == fifo->size)
80  {
81  fifo->tail = 0;
82  }
83  }
84  else
85  {
86  // Enable interrupts
87  bc_irq_enable();
88 
89  // Return number of bytes read
90  return i;
91  }
92  }
93 
94  // Enable interrupts
95  bc_irq_enable();
96 
97  // Return number of bytes read
98  return length;
99 }
100 
101 size_t bc_fifo_irq_write(bc_fifo_t *fifo, const void *buffer, size_t length)
102 {
103  for (size_t i = 0; i < length; i++)
104  {
105  if ((fifo->head + 1) == fifo->tail)
106  {
107  // Return number of bytes written
108  return i;
109  }
110 
111  if (((fifo->head + 1) == fifo->size) && (fifo->tail == 0))
112  {
113  // Return number of bytes written
114  return i;
115  }
116 
117  *((uint8_t *) fifo->buffer + fifo->head) = *(uint8_t *) buffer;
118 
119  buffer = (uint8_t *) buffer + 1;
120 
121  fifo->head++;
122 
123  if (fifo->head == fifo->size)
124  {
125  fifo->head = 0;
126  }
127  }
128 
129  // Return number of bytes written
130  return length;
131 }
132 
133 size_t bc_fifo_irq_read(bc_fifo_t *fifo, void *buffer, size_t length)
134 {
135  for (size_t i = 0; i < length; i++)
136  {
137  if (fifo->tail != fifo->head)
138  {
139  *(uint8_t *) buffer = *((uint8_t *) fifo->buffer + fifo->tail);
140 
141  buffer = (uint8_t *) buffer + 1;
142 
143  fifo->tail++;
144 
145  if (fifo->tail == fifo->size)
146  {
147  fifo->tail = 0;
148  }
149  }
150  else
151  {
152  // Return number of bytes read
153  return i;
154  }
155  }
156 
157  // Return number of bytes read
158  return length;
159 }
160 
162 {
163  bc_irq_disable();
164 
165  bool result = fifo->tail == fifo->head;
166 
167  bc_irq_enable();
168 
169  return result;
170 }
size_t bc_fifo_irq_read(bc_fifo_t *fifo, void *buffer, size_t length)
Read data from FIFO from interrupt.
Definition: bc_fifo.c:133
void bc_irq_enable(void)
Enable interrupt requests globally (call can be nested)
Definition: bc_irq.c:21
Structure of FIFO instance.
Definition: bc_fifo.h:12
size_t tail
Position of FIFO's tail.
Definition: bc_fifo.h:24
void * buffer
Pointer to buffer where FIFO holds data.
Definition: bc_fifo.h:15
size_t bc_fifo_irq_write(bc_fifo_t *fifo, const void *buffer, size_t length)
Write data to FIFO from interrupt.
Definition: bc_fifo.c:101
size_t head
Position of FIFO's head.
Definition: bc_fifo.h:21
void bc_fifo_init(bc_fifo_t *fifo, void *buffer, size_t size)
Initialize FIFO buffer.
Definition: bc_fifo.c:4
void bc_fifo_purge(bc_fifo_t *fifo)
Purge FIFO buffer.
Definition: bc_fifo.c:12
size_t bc_fifo_write(bc_fifo_t *fifo, const void *buffer, size_t length)
Write data to FIFO.
Definition: bc_fifo.c:18
size_t bc_fifo_read(bc_fifo_t *fifo, void *buffer, size_t length)
Read data from FIFO.
Definition: bc_fifo.c:63
void bc_irq_disable(void)
Disable interrupt requests globally (call can be nested)
Definition: bc_irq.c:7
bool bc_fifo_is_empty(bc_fifo_t *fifo)
Is empty.
Definition: bc_fifo.c:161
size_t size
Size of buffer where FIFO holds data.
Definition: bc_fifo.h:18