-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial_Cmd_Monitor.c
251 lines (215 loc) · 4.55 KB
/
Serial_Cmd_Monitor.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "uart.h"
#include "Serial_Cmd_Monitor.h"
#define TEMP_THRESHOLD 4
#define RW_CMD 0x80
#define EXTENSION_BYTE 0x07
//RW CMD TYPE
#define READ 1
#define WRITE 0
//ENDIANNESS
#define BIG_ENDIAN 1
#define LITTLE_ENDIAN 0
//ADDRESSIBLE SIZE
#define ADDRESSIBLE_32_BIT 1
#define ADDRESSIBLE_16_BIT 0
// override these depends on target: CMD_BUFFER_SIZE = 5 + sizeOfMauIn8BitByte * 63
#define CMD_BUFFER_SIZE 68 //1 + 4 + 63 = 68
unsigned char gInCmdBuffer[CMD_BUFFER_SIZE];
unsigned short gInCmdBufferIdx = 0;
volatile unsigned short gInCmdSkipCount;
void ClearBufferRelatedParam();
// override these depends on target
int GetTargetEndianness()
{
return LITTLE_ENDIAN;
}
// override these depends on target
void Write8bitByteToCOM(unsigned char c)
{
uartTxByte(c & 0xff);
}
int GetSizeOfMAUIn8bitByte()
{
unsigned char maxMAUValue = (unsigned char)(-1);
switch (maxMAUValue)
{
case 0xff:
return 1;
case 0xffff:
return 2;
default:
return 0;
}
}
int WriteToCmdBuffer(unsigned char* buf, unsigned short* bufIdx, unsigned char d)
{
if ( (*bufIdx) < CMD_BUFFER_SIZE )
{
buf[*bufIdx] = d & 0xff;
(*bufIdx)++;
return 0;
}
return 1;
}
void ResetInCmdBuffer()
{
gInCmdBufferIdx = 0;
}
int WriteByteToInCmdBuffer(unsigned char d)
{
return WriteToCmdBuffer(gInCmdBuffer, &gInCmdBufferIdx, d);
}
int GetTransferSizeInMAU() //transfer size refer to the words to read/write of a given cmd, not the number of bytes for the whole cmd packet
{
return (gInCmdBuffer[0] & 0x3f);
}
int VerifyInputCmdHeaders()
{
return ((gInCmdBuffer[0] & 0x80) == 0x80) ? 0 : 1;
}
int GetInputCmdType()
{
return (gInCmdBuffer[0] & 0x80);
}
int GetRWFlag()//equivalent to endianness on the MAU in transmission
{
int ret = ((gInCmdBuffer[0] >> 6) & 0x1); //
return ret;
}
unsigned char* GetInCmdAddress()
{
unsigned char* addr = 0;
unsigned long addr_value = 0;
int i = 0;
int addressSize = 4; // always use 32bit address
for (; i < addressSize; i++)
{
addr_value |= (unsigned long)( gInCmdBuffer[1 + i] << 8 * (addressSize - 1 - i) ); //big endian
}
addr = (unsigned char*) addr_value;
return addr;
}
void WriteMAUToCOM(unsigned char d)
{
int MAUSize = GetSizeOfMAUIn8bitByte();
switch (MAUSize)
{
case 1:
Write8bitByteToCOM(d);
break;
case 2:
{
unsigned char MAU[2];
MAU[0] = (unsigned char)(d & 0xff);
MAU[1] = (unsigned char)(d >> 8);
if (GetTargetEndianness() == LITTLE_ENDIAN)
{
Write8bitByteToCOM(MAU[0]);
Write8bitByteToCOM(MAU[1]);
} else {
Write8bitByteToCOM(MAU[1]);
Write8bitByteToCOM(MAU[0]);
}
}
break;
default://only handles 8bit, 16bit MAU
break;
}
}
unsigned char GetWriteCmdDataMAU(int idx)
{
unsigned char startIdx = 1 + 4;
unsigned char val = 0;
int MAUSize = GetSizeOfMAUIn8bitByte();
int byteOffset = idx*MAUSize;
switch (MAUSize)
{
case 1:
val = gInCmdBuffer[startIdx + byteOffset];
break;
case 2:
if (GetTargetEndianness() == LITTLE_ENDIAN)
{
val = ( gInCmdBuffer[startIdx + byteOffset + 1] << 8 ) | gInCmdBuffer[startIdx + byteOffset];
} else {
val = ( gInCmdBuffer[startIdx + byteOffset] | gInCmdBuffer[startIdx + byteOffset + 1] << 8 );
}
break;
default://only handles 8bit, 16bit MAU
break;
}
return val;
}
void ClearBufferRelatedParam()
{
gInCmdSkipCount = 0;
gInCmdBufferIdx = 0;
}
void MemAccessCmd(int RW)
{
unsigned short MAUsToRead = 0;
unsigned char dataChar = 0;
unsigned char* addr = GetInCmdAddress();
unsigned short i, j;
for ( j = 0; j < 1; j++ )
{
Write8bitByteToCOM(gInCmdBuffer[j]);
}
MAUsToRead = GetTransferSizeInMAU();
for ( i = 0; i < MAUsToRead; i++ )
{
if (RW == READ)
{
dataChar = *(addr + i);
WriteMAUToCOM(dataChar);
} else { //WRITE
dataChar = GetWriteCmdDataMAU(i);
*(addr + i) = dataChar;
}
}
}
int ProcessCommand()
{
if ( VerifyInputCmdHeaders() )
{
return 1;
}
switch ( GetInputCmdType() )
{
case RW_CMD:
MemAccessCmd(GetRWFlag());
break;
default:
return 1;
}
return 0;
}
void receivedDataCommand(unsigned char d) // only lower byte will be used even if MAU is bigger than 1 byte
{
WriteByteToInCmdBuffer(d);
if (gInCmdSkipCount > 0)
{
gInCmdSkipCount--;
return;
}
if (gInCmdBufferIdx > 0 && gInCmdSkipCount == 0)
{ //wrong input header, clear cmd buffer
if ( VerifyInputCmdHeaders() )
{
ClearBufferRelatedParam();
return;
}
if (gInCmdBufferIdx == 1) {
if (GetRWFlag() == WRITE)
{
gInCmdSkipCount = 4 - 1 + GetTransferSizeInMAU() * GetSizeOfMAUIn8bitByte();
} else {
gInCmdSkipCount = 4 - 1 ;
}
} else {
ProcessCommand();
ClearBufferRelatedParam();
}
return;
}
}