-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_buffer.c
182 lines (143 loc) · 6.24 KB
/
bus_buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "global.h"
#include "buffer_types.h"
// API Methods //
void putByteRx(tBufferPair *ptrBufferPair, uint8_t byte, eBufferResponse *ptrStatus);
void putByteTx(tBufferPair *ptrBufferPair, uint8_t byte, eBufferResponse *ptrStatus);
uint8_t getByteRx(tBufferPair *ptrBufferPair, eBufferResponse *ptrStatus);
uint8_t getByteTx(tBufferPair *ptrBufferPair, eBufferResponse *ptrStatus);
uint8_t getRxDataByteCount(tBufferPair *ptrBufferPair);
uint8_t getTxDataByteCount(tBufferPair *ptrBufferPair);
void setupBusBuffer(tBufferPair *ptrBufferPair);
void runRxStaging(tBufferPair *ptrBufferPair);
void runTxStaging(tBufferPair *ptrBufferPair);
uint8_t isTxDataReadyToRead(tBufferPair *ptrBufferPair);
// Common Methods //
uint8_t getByte(eBufferResponse *ptrStatus, tBufferData *ptrBufferData);
void putByte(uint8_t byte, eBufferResponse *ptrStatus, tBufferData *ptrBufferData);
void runStaging(tBufferData *ptrBufferData);
void setupBuffer(tBufferData *ptrBufferData);
void bufferWrite(tRingBuffer *ptrBuffer, uint8_t byte);
uint8_t bufferRead(tRingBuffer *ptrBuffer);
//----------------------------------------------------------------------------//
// API Methods //
//----------------------------------------------------------------------------//
void setupBusBuffer(tBufferPair *ptrBufferPair) {
setupBuffer(&ptrBufferPair->bufferRx);
setupBuffer(&ptrBufferPair->bufferTx);
}
void runRxStaging(tBufferPair *ptrBufferPair) {
runStaging(&ptrBufferPair->bufferRx);
}
void runTxStaging(tBufferPair *ptrBufferPair) {
runStaging(&ptrBufferPair->bufferTx);
}
void putByteRx(tBufferPair *ptrBufferPair, uint8_t byte, eBufferResponse *ptrStatus) {
putByte(byte, ptrStatus, &(ptrBufferPair->bufferRx));
}
void putByteTx(tBufferPair *ptrBufferPair, uint8_t byte, eBufferResponse *ptrStatus) {
putByte(byte, ptrStatus, &(ptrBufferPair->bufferTx));
}
uint8_t getByteRx(tBufferPair *ptrBufferPair, eBufferResponse *ptrStatus) {
return getByte(ptrStatus, &(ptrBufferPair->bufferRx));
}
uint8_t getByteTx(tBufferPair *ptrBufferPair, eBufferResponse *ptrStatus) {
return getByte(ptrStatus, &(ptrBufferPair->bufferTx));
}
uint8_t getRxDataByteCount(tBufferPair *ptrBufferPair) {
return ptrBufferPair->bufferRx.buffer.fillLevel;
}
uint8_t getTxDataByteCount(tBufferPair *ptrBufferPair) {
return ptrBufferPair->bufferTx.buffer.fillLevel;
}
uint8_t isRxDataReadyToRead(tBufferPair *ptrBufferPair) {
return ptrBufferPair->bufferRx.buffer.fillLevel > 0 && !ptrBufferPair->bufferRx.flags.bufferLocked;
}
uint8_t isTxDataReadyToRead(tBufferPair *ptrBufferPair) {
return ptrBufferPair->bufferTx.buffer.fillLevel > 0 && !ptrBufferPair->bufferTx.flags.bufferLocked;
}
//----------------------------------------------------------------------------//
// Common Methods //
//----------------------------------------------------------------------------//
void setupBuffer(tBufferData *ptrBufferData) {
tRingBuffer *ptrBuffer = &(ptrBufferData->buffer);
ptrBuffer -> ptrStart = &(ptrBuffer -> data[0]);
ptrBuffer -> ptrEnd = &(ptrBuffer -> data[(BUFFER_SIZE - 1)]);
ptrBuffer -> ptrRead = ptrBuffer -> ptrStart;
ptrBuffer -> ptrWrite = ptrBuffer -> ptrStart;
ptrBuffer -> fillLevel = 0;
ptrBufferData -> flags.byte = 0;
ptrBuffer -> size = BUFFER_SIZE;
ptrBuffer = &(ptrBufferData->stagingBuffer);
ptrBuffer -> ptrStart = &(ptrBuffer -> data[0]);
ptrBuffer -> ptrEnd = &(ptrBuffer -> data[(BUFFER_SIZE - 1)]);
ptrBuffer -> ptrRead = ptrBuffer -> ptrStart;
ptrBuffer -> ptrWrite = ptrBuffer -> ptrStart;
ptrBuffer -> fillLevel = 0;
ptrBufferData -> flags.byte = 0;
ptrBuffer -> size = BUFFER_SIZE;
}
void putByte(uint8_t byte, eBufferResponse *ptrStatus, tBufferData *ptrBufferData) {
tRingBuffer *ptrBuffer = &(ptrBufferData->stagingBuffer);
if(ptrBufferData -> flags.stagingLocked) {
*ptrStatus = BUFFER_ERROR_LOCKED;
return;
}
if(ptrBuffer -> fillLevel >= ptrBuffer -> size) {
*ptrStatus = BUFFER_ERROR_FULL;
return;
}
ptrBufferData -> flags.stagingLocked = 1;
bufferWrite(ptrBuffer,byte);
ptrBufferData -> flags.stagingLocked = 0;
*ptrStatus = BUFFER_OPERATION_OK;
}
uint8_t getByte(eBufferResponse *ptrStatus, tBufferData *ptrBufferData) {
tRingBuffer *ptrBuffer = &(ptrBufferData->buffer);
tBufferFlags *ptrFlags = &(ptrBufferData->flags);
*ptrStatus = BUFFER_OPERATION_NONE;
if(ptrBuffer -> fillLevel == 0) {
*ptrStatus = BUFFER_ERROR_EMPTY;
return 0;
}
if(ptrFlags -> bufferLocked) {
*ptrStatus = BUFFER_ERROR_LOCKED;
return 0;
}
ptrFlags -> bufferLocked = 1;
uint8_t byte = bufferRead(ptrBuffer);
ptrFlags -> bufferLocked = 0;
*ptrStatus = BUFFER_OPERATION_OK;
return byte;
}
uint8_t bufferRead(tRingBuffer *ptrBuffer) {
uint8_t byte = *(ptrBuffer -> ptrRead);
ptrBuffer -> ptrRead++;
ptrBuffer -> fillLevel--;
if(ptrBuffer -> ptrRead > ptrBuffer -> ptrEnd) {
ptrBuffer -> ptrRead = ptrBuffer -> ptrStart;
}
return byte;
}
void bufferWrite(tRingBuffer *ptrBuffer, uint8_t byte) {
*(ptrBuffer -> ptrWrite) = byte;
ptrBuffer -> fillLevel++;
ptrBuffer -> ptrWrite++;
if(ptrBuffer -> ptrWrite > ptrBuffer -> ptrEnd) {
ptrBuffer -> ptrWrite = ptrBuffer -> ptrStart;
}
}
void runStaging(tBufferData *ptrBufferData) {
if(!ptrBufferData -> flags.stagingLocked && !ptrBufferData -> flags.bufferLocked) {
tRingBuffer *ptrStagingBuffer = &(ptrBufferData->stagingBuffer);
if(ptrStagingBuffer -> fillLevel > 0) {
tRingBuffer *ptrBuffer = &(ptrBufferData->buffer);
if(ptrBuffer -> fillLevel >= ptrBuffer -> size) {
return;
}
ptrBufferData -> flags.stagingLocked = 1;
uint8_t byte = bufferRead(ptrStagingBuffer);
ptrBufferData -> flags.stagingLocked = 0;
bufferWrite(ptrBuffer,byte);
}
}
}