00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "TIComm.h"
00025
00026
00027 static const char writeCommand[] = "WVAR";
00028 static const char readCommand[] = "RVAR";
00029 static const char rtCommand[] = "CVAR";
00030 static const char delCommand[] = "DVAR";
00031 static const char rstCommand[] = "RSTM";
00032 static const char startCommand[] = "INIT";
00033 static const char stopCommand[] = "STOP";
00034 struct Commands
00035 {
00036 Uint16 onWriteCommand:1;
00037 Uint16 onReadCommand:1;
00038 Uint16 onRTCommand:1;
00039 Uint16 onRSTCommand:1;
00040 Uint16 onDelCommand:1;
00041 Uint16 onStartCommand:1;
00042 Uint16 onStopCommand:1;
00043 Uint16 rsvd:9;
00044 };
00045 union OnCommand
00046 {
00047 Uint16 all;
00048 struct Commands command;
00049 };
00050 static const char COMMAND_SIZE = 4;
00051 static const char MSG_SIZE = 12;
00052 static const char RTMSG_SIZE = 8;
00053 volatile union Msg msg;
00054 volatile union Msg dspMsg;
00055 volatile union RTMsg rtMsg;
00056 volatile union OnCommand onCommand;
00057 static struct RTVariables rtVariables[MAX_ACCSESIBLE_VARS];
00058 volatile char usedVariables;
00059 volatile char isrMonitorEnabled=0;
00060 volatile unsigned char RTMonitorStart = 0;
00061
00062 static inline char IsCommand(volatile char origin[], const char command[]);
00063
00064
00065
00066 void InitTIComm(unsigned char isrMonitor)
00067 {
00068 __byte((int*)(rtMsg.data.command),0)='R';
00069 __byte((int*)(rtMsg.data.command),1)='V';
00070 __byte((int*)(rtMsg.data.command),2)='A';
00071 __byte((int*)(rtMsg.data.command),3)='R';
00072 usedVariables = 0;
00073 ConfigureSCI(SCIARxInterrupt, SCIA, 115200, ONE_STOP_BIT , ODD_PARITY, PARITY_DISABLED, 8);
00074 isrMonitorEnabled = isrMonitor;
00075 }
00076
00077
00078
00079
00080
00081
00082 interrupt void SCIARxInterrupt(void)
00083 {
00084 int cc;
00085 volatile char needToSend;
00086 volatile char sciaRXDT;
00087 volatile char sciaTXDT;
00088 ToggleGPIOPort(_GPIO18);
00089 while(SciaRegs.SCIFFRX.bit.RXFFST == 0) {}
00090 sciaRXDT = SciaRegs.SCIRXBUF.bit.RXDT;
00091
00092 needToSend=TICommProtocol(sciaRXDT,&dspMsg);
00093 if(needToSend){
00094 for(cc=0;cc<MSG_SIZE;cc++){
00095 sciaTXDT = __byte((int*)(dspMsg.raw),cc);
00096 SendDataCharSciA(&(sciaTXDT));
00097 }
00098
00099 }
00100 SCIARxPIEAck();
00101 }
00102
00103
00104
00105
00106
00107
00108 interrupt void SCIATxInterrupt(void)
00109 {
00110 RTMonitor();
00111 SCIATxPIEAck();
00112 }
00113
00114
00115
00116
00117 char TICommProtocol(volatile char sciaRXDT, volatile union Msg* dspMsg)
00118 {
00119 volatile char needToSend = 0;
00120 static unsigned readedBytes = 0;
00121 int cc;
00122 __byte((int*)(msg.raw),readedBytes) = sciaRXDT;
00123 readedBytes += 1;
00124
00125
00126 if(readedBytes == COMMAND_SIZE){
00127
00128 if(IsCommand(msg.data.command,writeCommand)){
00129 onCommand.all=1;
00130 }
00131 else if(IsCommand(msg.data.command,readCommand)){
00132 onCommand.all=2;
00133 }
00134 else if(IsCommand(msg.data.command,rtCommand)){
00135 onCommand.all=4;
00136 }
00137 else if(IsCommand(msg.data.command,rstCommand)){
00138 onCommand.all=8;
00139 }
00140 else if(IsCommand(msg.data.command,delCommand)){
00141 onCommand.all=16;
00142 }
00143 else if(IsCommand(msg.data.command,startCommand)){
00144 onCommand.all=32;
00145 }
00146 else if(IsCommand(msg.data.command,stopCommand)){
00147 onCommand.all=64;
00148 }
00149 else {
00150 onCommand.all=0;
00151 readedBytes = 0;
00152 }
00153 }
00154
00155
00156
00157 if(readedBytes == MSG_SIZE){
00158 readedBytes = 0;
00159 needToSend = 1;
00160
00161 *dspMsg = msg;
00162 if (onCommand.command.onReadCommand) {
00163 __byte((int*)(dspMsg->data.command),1)='A';
00164 __byte((int*)(dspMsg->data.command),2)='C';
00165 __byte((int*)(dspMsg->data.command),3)='K';
00166 dspMsg->data.val = *((Uint32*)(msg.data.dir));
00167 }
00168 else if (onCommand.command.onWriteCommand) {
00169 *((Uint32*)(dspMsg->data.dir)) = dspMsg->data.val;
00170 dspMsg->data.val = *((Uint32*)(dspMsg->data.dir));
00171
00172 __byte((int*)(dspMsg->data.command),1)='A';
00173 __byte((int*)(dspMsg->data.command),2)='C';
00174 __byte((int*)(dspMsg->data.command),3)='K';
00175 }
00176 else if (onCommand.command.onRTCommand) {
00177 needToSend = 1;
00178 __byte((int*)(dspMsg->data.command),1)='A';
00179 __byte((int*)(dspMsg->data.command),2)='C';
00180 __byte((int*)(dspMsg->data.command),3)='K';
00181
00182 if(usedVariables<MAX_ACCSESIBLE_VARS){
00183 rtVariables[usedVariables].dir = (Uint32*)msg.data.dir;
00184 rtVariables[usedVariables].pos = usedVariables;
00185 dspMsg->data.val = rtVariables[usedVariables].pos;
00186 rtVariables[usedVariables++].used = 1;
00187 }
00188 }
00189 else if (onCommand.command.onDelCommand) {
00190 needToSend = 0;
00191 for (cc=0;cc<usedVariables;cc++){
00192 if((Uint32)(rtVariables[cc].dir) == msg.data.dir){
00193 rtVariables[cc].used = 0;
00194 usedVariables--;
00195 break;
00196 }
00197 }
00198 }
00199 else if (onCommand.command.onRSTCommand) {
00200 needToSend = 0;
00201 for (cc=0;cc<usedVariables;cc++)
00202 rtVariables[cc].used = 0;
00203 usedVariables = 0;
00204 }
00205 else if (onCommand.command.onStartCommand) {
00206 needToSend = 0;
00207 RTMonitorStart = 1;
00208 if(isrMonitorEnabled == 1){
00209 EnableSCITXInterrupt(SCIA, SCIATxInterrupt, 8);
00210 }
00211 }
00212 else if (onCommand.command.onStopCommand) {
00213 needToSend = 0;
00214 RTMonitorStart = 0;
00215 if(isrMonitorEnabled == 1){
00216 DisableSCITXInterrupt(SCIA);
00217 }
00218 }
00219 }
00220
00221 return needToSend;
00222 }
00223
00224
00225
00226 void RTMonitor(void)
00227 {
00228 static int cc=0;
00229 int mm;
00230 volatile char sciaTXDT;
00231 if(RTMonitorStart == 0)
00232 return;
00233
00234 if(cc==usedVariables) cc=0;
00235 if(rtVariables[cc].used){
00236 __byte((int*)(rtMsg.raw),3) = rtVariables[cc].pos;
00237 rtMsg.data.val = *((Uint32*)(rtVariables[cc].dir));
00238 for(mm=0;mm<RTMSG_SIZE;mm++){
00239 sciaTXDT = __byte((int*)(rtMsg.raw),mm);
00240 SendDataCharSciANow(&(sciaTXDT));
00241 }
00242 cc++;
00243 }
00244
00245 }
00246
00247
00248
00249 static inline char IsCommand(volatile char origin[], const char command[])
00250 {
00251 int cc;
00252 char isCommand=1;
00253 for(cc=0;cc<COMMAND_SIZE;cc++)
00254 isCommand &= (__byte((int*)(origin),cc) == command[cc]);
00255 return isCommand;
00256 }
00257
00258
00259
00260
00261
00262
00263