My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
/*
* This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/).
*
* Written and maintained by the Bus Pirate project.
*
* To the extent possible under law, the project has
* waived all copyright and related or neighboring rights to Bus Pirate. This
* work is published from United States.
*
* For details see: http://creativecommons.org/publicdomain/zero/1.0/.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
//convert commands to actual character in a #DEFINE CMD_READ 'R' and get rid of the silly switch statement
//convert menu commands to defines, use caps only (lowercase adjusted automatically)
//create processing structure with proper variable names

#include "base.h"
#include "AUXpin.h"
#include "busPirateCore.h"

extern struct _bpConfig bpConfig;
extern struct _modeConfig modeConfig;
extern struct _command bpCommand; //holds the current active command so we don't ahve to put so many variables on the stack

//reads forward to grab repeat values following read, delay, and clock
unsigned int bpGetRepeatVal(unsigned int totalBytes, unsigned int *currentByte, unsigned char *commandArr);
//convert ASCII text input in the syntax string to a command
unsigned char ASCII2CMD(unsigned char ASCIIcommand);

/* static struct _syn{
unsigned int currentByte;
unsigned char command;
unsigned char ASCIInumber[12];
unsigned char writeVal;
unsigned char repeatVal;
unsigned char macroVal;
unsigned char error:1;
} syntax;
*/

//master function that processes terminal input
//scans for valid syntax, converts to values and general bus commands
void processSyntaxString(unsigned int totalBytes, unsigned char *commandArr){
static unsigned int currentByte,numVal,repeatVal;
static unsigned char command,error=0;

if(bpConfig.busMode==0){bpWmessage(MSG_ERROR_NOMODE); return;}

//process each byte of the command string and act on it
for(currentByte=0;currentByte<totalBytes;currentByte++){

command=ASCII2CMD(commandArr[currentByte]);//lookup command code

if(command==CMD_WRITE || command==CMD_MACRO){ //get number for MACRO or value
if(command==CMD_MACRO){ //position beyond '(' to get macro
currentByte++;//increment the pointer
if(currentByte==totalBytes){ //check for buffer overrun
error++;
currentByte--;
}//don't overrun the array
}

numVal=bpGetASCIInumber(totalBytes, &currentByte, commandArr); //pass currentByte by reference because it has to be updated
}

repeatVal=bpGetRepeatVal(totalBytes, &currentByte, commandArr); //check for repeat value

if(command==CMD_MACRO){//check if number ends with ), check for unexpected end of sequence
currentByte++;
if(currentByte==totalBytes || commandArr[currentByte]!=')'){
currentByte--;
error++;
//break;
}
}

if(error){//stop processing if there are errors.
bpWmessage(MSG_ERROR_SYNTAX);
error=0;
return;
break;
}

switch(command){
case CMD_NOP://space (0x20 , 0x0a 0x0d)
break;//do nothing...
case CMD_AUXH: //aux output to high
bpAuxHigh();
break;
case CMD_AUXL: //aux output to low
bpAuxLow();
break;
case CMD_AUXIN://aux to input, read
bpAuxRead();
break;
case CMD_DELAY://delay
bpWstring(OUMSG_PS_DELAY);
bpWbyte(repeatVal);
bpWline(OUMSG_PS_DELAY_US);
bpDelayUS(repeatVal);
break;
#if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2)
case CMD_PWR_EN://enable any active power supplies
BP_VREG_ON();
bpWmessage(MSG_VREG_ON);
modeConfig.vregEN=1;
break;
case CMD_PWR_DIS://disable the power supplies
BP_VREG_OFF();
bpWmessage(MSG_VREG_OFF);
modeConfig.vregEN=0;
break;
case CMD_ADC://do an adc reading
bpWstring(OUMSG_PS_ADC_VOLT_PROBE);
bpADCprobe();
bpWline(OUMSG_PS_ADC_VOLTS);
break;
case CMD_ADCC://dumb voltmeter mode
bpADCCprobe();
break;

#endif
default: //send command to library
bpCommand.cmd=command; //move to global command struct
bpCommand.num=numVal;
bpCommand.repeat=repeatVal;
bpProcess();//process the command using the values in the global command struct
}//switch
}//for

bpCommand.cmd=CMD_ENDOFSYNTAX;
bpProcess(); //tell library it's the end of syntax, do any cleanup

}//function

//reads forward to grab repeat values following read, delay, and clock
//i must be passed by reference (&i) because we move it forward to avoid reprocessing the same bytes next time.
unsigned int bpGetRepeatVal(unsigned int totalBytes, unsigned int *currentByte, unsigned char *commandArr){
unsigned int repeatVal=0;

//check for repeat indicator, check for unexpected end of sequence
if((*currentByte)+1<totalBytes && commandArr[*currentByte+1]==':'){//don't overrun the array
(*currentByte)+=2;
repeatVal=bpGetASCIInumber(totalBytes, &(*currentByte), commandArr);
return repeatVal;
}
return 1;
}

//convert ASCII text input in the syntax string to a command
//CMD statements are generic and processed by each bus library
//CMD statements are defined and enumerated in base.h
unsigned char ASCII2CMD(unsigned char ASCIIcommand){
//process as a number...
if(ASCIIcommand>0x2F && ASCIIcommand<0x3A) return CMD_WRITE;

switch(ASCIIcommand){
case 'A': return CMD_AUXH;
case 'a': return CMD_AUXL;
case '@': return CMD_AUXIN;
case '&': return CMD_DELAY;
case '(': return CMD_MACRO;
//case ')': return CMD_ERROR; //should never get here CMD_NOP;
case '{': return CMD_STARTR; //start bus with read=true
case '[': return CMD_START;//start bus
case '}'://stop bus
case ']': return CMD_STOP;
case 'r'://read one byte from the bus
case 'R': return CMD_READ;
case '!': return CMD_BIT_READ;//read bit with clock
case '.': return CMD_BIT_PEEK;//read bit no clock
case '/': return CMD_BIT_CLKH;//clock high
case 0x5c:return CMD_BIT_CLKL; //(\)clock low
case '^': return CMD_BIT_CLK; //clock pulse
case '_': return CMD_BIT_DATL; //data low
case '-': return CMD_BIT_DATH;//data high
case 0x20: return CMD_NOP;//space
case ',': return CMD_NOP;
case 0x0a: return CMD_NOP;
case 0x0d: return CMD_NOP;
#if defined( BUSPIRATEV1A) || defined( BUSPIRATEV2)
case 'W':return CMD_PWR_EN;
case 'w':return CMD_PWR_DIS;
case 'D':return CMD_ADCC;
case 'd':return CMD_ADC;
#endif
default: return CMD_ERROR;
}
}

Change log

r354 by ianlesnet on May 20, 2010   Diff
Input parser updated to handle correct
range of numbers.
Go to: 
Project members, sign in to write a code review

Older revisions

r251 by sjaak2...@msn.com on Jan 16, 2010   Diff
- Continuous Volt measure added
- Voltmeasurement more preciser
- added bpInit() before jumping to
bootloader
r48 by ianlesnet on Oct 16, 2009   Diff
Major change to global command struct.
Will make it easier to pass new
variable to libraries in the future
without editing each individually. It
also keeps a bunch of stuff off the
...
r1 by ianlesnet on Sep 17, 2009   Diff
Reset SVN, removed non public domain
files. Adding from backup.
All revisions of this file

File info

Size: 6698 bytes, 192 lines
Powered by Google Project Hosting