Firmware SDK
twr_fifo.c
1 #include <twr_fifo.h>
2 #include <twr_irq.h>
3 
4 void twr_fifo_init(twr_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 twr_fifo_write(twr_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
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
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
58 
59  // Return number of bytes written
60  return length;
61 }
62 
63 size_t twr_fifo_read(twr_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
88 
89  // Return number of bytes read
90  return i;
91  }
92  }
93 
94  // Enable interrupts
96 
97  // Return number of bytes read
98  return length;
99 }
100 
101 size_t twr_fifo_irq_write(twr_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 twr_fifo_irq_read(twr_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  twr_irq_disable();
164 
165  bool result = fifo->tail == fifo->head;
166 
167  twr_irq_enable();
168 
169  return result;
170 }
size_t twr_fifo_irq_read(twr_fifo_t *fifo, void *buffer, size_t length)
Read data from FIFO from interrupt.
Definition: twr_fifo.c:133
void twr_fifo_purge(twr_fifo_t *fifo)
Purge FIFO buffer.
Definition: twr_fifo.c:12
size_t twr_fifo_write(twr_fifo_t *fifo, const void *buffer, size_t length)
Write data to FIFO.
Definition: twr_fifo.c:18
bool twr_fifo_is_empty(twr_fifo_t *fifo)
Is empty.
Definition: twr_fifo.c:161
size_t twr_fifo_irq_write(twr_fifo_t *fifo, const void *buffer, size_t length)
Write data to FIFO from interrupt.
Definition: twr_fifo.c:101
size_t twr_fifo_read(twr_fifo_t *fifo, void *buffer, size_t length)
Read data from FIFO.
Definition: twr_fifo.c:63
void twr_fifo_init(twr_fifo_t *fifo, void *buffer, size_t size)
Initialize FIFO buffer.
Definition: twr_fifo.c:4
void twr_irq_disable(void)
Disable interrupt requests globally (call can be nested)
Definition: twr_irq.c:7
void twr_irq_enable(void)
Enable interrupt requests globally (call can be nested)
Definition: twr_irq.c:21
Structure of FIFO instance.
Definition: twr_fifo.h:13
void * buffer
Pointer to buffer where FIFO holds data.
Definition: twr_fifo.h:15
size_t head
Position of FIFO's head.
Definition: twr_fifo.h:21
size_t tail
Position of FIFO's tail.
Definition: twr_fifo.h:24
size_t size
Size of buffer where FIFO holds data.
Definition: twr_fifo.h:18