forked from zhuhuijia0001/ch559-usb-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecvBuffer.c
139 lines (112 loc) · 2.76 KB
/
RecvBuffer.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
#include "Type.h"
#include "Protocol.h"
#include "RecvBuffer.h"
//buffer struct
typedef struct _BUFFER_STRUCT
{
UINT8 *pBufferIn;
UINT8 *pBufferOut;
UINT8 *pBufferStart;
UINT8 *pBufferEnd;
UINT8 BlockSize;
} BUFFER_STRUCT;
//receive struct
typedef struct _RECV_STRUCT
{
BUFFER_STRUCT RecvBuffer;
UINT8 RPtr;
UINT8 TimerCounter;
} RECV_STRUCT;
#define BUFFER_BLOCK_SIZE 4
#define BUFFER_BLOCK_COUNT 4
static UINT8X s_buffer[BUFFER_BLOCK_SIZE * BUFFER_BLOCK_COUNT];
static RECV_STRUCT volatile s_recvStruct;
typedef enum
{
RECV_STATE_IDLE = 0,
RECV_STATE_PACKET
} RECV_STATE;
//max interval
#define BYTE_INTERVAL 3
static UINT8 volatile s_sum = 0;
static UINT8 volatile s_left = 0;
static RECV_STATE volatile s_recvState = RECV_STATE_IDLE;
void RecvBufferTimerout(void) using 2
{
if (s_recvStruct.TimerCounter > 0)
{
s_recvStruct.TimerCounter--;
if (s_recvStruct.TimerCounter == 0)
{
s_recvState = RECV_STATE_IDLE;
}
}
}
void RecvBufferOneByte(UINT8 ch) using 1
{
if (s_recvState == RECV_STATE_IDLE)
{
BOOL validCh = FALSE;
if (ch == ID_LED_STATUS)
{
s_left = LED_LEN + 1;
validCh = TRUE;
}
if (validCh)
{
s_recvStruct.RecvBuffer.pBufferIn[0] = ch;
s_recvStruct.RPtr = 1;
s_recvStruct.TimerCounter = BYTE_INTERVAL;
s_recvState = RECV_STATE_PACKET;
s_sum = ch;
}
}
else if (s_recvState == RECV_STATE_PACKET)
{
if (--s_left > 0)
{
s_recvStruct.RecvBuffer.pBufferIn[s_recvStruct.RPtr++] = ch;
s_recvStruct.TimerCounter = BYTE_INTERVAL;
s_sum ^= ch;
}
else
{
//check sum
if (s_sum == ch)
{
s_recvStruct.RecvBuffer.pBufferIn += s_recvStruct.RecvBuffer.BlockSize;
if (s_recvStruct.RecvBuffer.pBufferIn == s_recvStruct.RecvBuffer.pBufferEnd)
{
s_recvStruct.RecvBuffer.pBufferIn = s_recvStruct.RecvBuffer.pBufferStart;
}
}
s_recvStruct.RPtr = 0;
s_recvStruct.TimerCounter = 0;
s_recvState = RECV_STATE_IDLE;
}
}
}
void InitRecvBuffer(void)
{
s_recvStruct.RecvBuffer.pBufferStart = s_buffer;
s_recvStruct.RecvBuffer.pBufferEnd = s_buffer + sizeof(s_buffer);
s_recvStruct.RecvBuffer.pBufferIn = s_buffer;
s_recvStruct.RecvBuffer.pBufferOut = s_buffer;
s_recvStruct.RecvBuffer.BlockSize = BUFFER_BLOCK_SIZE;
s_recvStruct.RPtr = 0;
s_recvStruct.TimerCounter = 0;
}
BOOL IsRecvBufferEmpty(void)
{
return s_recvStruct.RecvBuffer.pBufferIn == s_recvStruct.RecvBuffer.pBufferOut;
}
UINT8 *GetOutputBuffer(void)
{
UINT8 *p = s_recvStruct.RecvBuffer.pBufferOut;
s_recvStruct.RecvBuffer.pBufferOut += s_recvStruct.RecvBuffer.BlockSize;
if (s_recvStruct.RecvBuffer.pBufferOut == s_recvStruct.RecvBuffer.pBufferEnd)
{
s_recvStruct.RecvBuffer.pBufferOut = s_recvStruct.RecvBuffer.pBufferStart;
}
return p;
}