00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "states.h"
00024 #include "def.h"
00025 #include "dcf.h"
00026 #include "nmtSlave.h"
00027
00028
00029 void switchCommunicationState(CO_Data* d,
00030 s_state_communication *newCommunicationState);
00031
00032
00033 e_nodeState getState(CO_Data* d)
00034 {
00035 return d->nodeState;
00036 }
00037
00038
00039 void canDispatch(CO_Data* d, Message *m)
00040 {
00041 switch(m->cob_id.w >> 7)
00042 {
00043 case SYNC:
00044 if(d->CurrentCommunicationState.csSYNC)
00045 proceedSYNC(d,m);
00046 break;
00047
00048 case PDO1tx:
00049 case PDO1rx:
00050 case PDO2tx:
00051 case PDO2rx:
00052 case PDO3tx:
00053 case PDO3rx:
00054 case PDO4tx:
00055 case PDO4rx:
00056 if (d->CurrentCommunicationState.csPDO)
00057 proceedPDO(d,m);
00058 break;
00059 case SDOtx:
00060 case SDOrx:
00061 if (d->CurrentCommunicationState.csSDO)
00062 proceedSDO(d,m);
00063 break;
00064 case NODE_GUARD:
00065 if (d->CurrentCommunicationState.csHeartbeat)
00066 proceedNODE_GUARD(d,m);
00067 break;
00068 case NMT:
00069 if (*(d->iam_a_slave))
00070 {
00071 proceedNMTstateChange(d,m);
00072 }
00073 }
00074 }
00075
00076 #define StartOrStop(CommType, FuncStart, FuncStop) \
00077 if(newCommunicationState->CommType && !d->CurrentCommunicationState.CommType){\
00078 MSG_WAR(0x9999,#FuncStart, 9999);\
00079 d->CurrentCommunicationState.CommType = 1;\
00080 FuncStart;\
00081 }else if(!newCommunicationState->CommType && d->CurrentCommunicationState.CommType){\
00082 MSG_WAR(0x9999,#FuncStop, 9999);\
00083 d->CurrentCommunicationState.CommType = 0;\
00084 FuncStop;\
00085 }
00086 #define None
00087
00088
00089 void switchCommunicationState(CO_Data* d, s_state_communication *newCommunicationState)
00090 {
00091 StartOrStop(csSDO, None, resetSDO(d))
00092 StartOrStop(csSYNC, startSYNC(d), stopSYNC(d))
00093 StartOrStop(csHeartbeat, heartbeatInit(d), heartbeatStop(d))
00094
00095 StartOrStop(csPDO, None, None)
00096 StartOrStop(csBoot_Up, None, slaveSendBootUp(d))
00097 }
00098
00099
00100 UNS8 setState(CO_Data* d, e_nodeState newState)
00101 {
00102 UNS16 wIndex = 0x1F22;
00103 const indextable *ptrTable;
00104 ODCallback_t *Callback;
00105 UNS32 errorCode;
00106 while(newState != d->nodeState){
00107 switch( newState ){
00108 case Initialisation:
00109 {
00110 s_state_communication newCommunicationState = {1, 0, 0, 0, 0, 0};
00111
00112 d->nodeState = Initialisation;
00113 newState = Pre_operational;
00114 switchCommunicationState(d, &newCommunicationState);
00115
00116 (*d->initialisation)();
00117
00118 }
00119 break;
00120
00121 case Pre_operational:
00122 {
00123
00124 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 0};
00125 d->nodeState = Pre_operational;
00126 newState = Pre_operational;
00127 switchCommunicationState(d, &newCommunicationState);
00128 if (!(*(d->iam_a_slave)))
00129 {
00130 ptrTable =(*d->scanIndexOD)(wIndex, &errorCode, &Callback);
00131
00132 if (errorCode != OD_SUCCESSFUL)
00133 {
00134 (*d->preOperational)();
00135 }
00136 else
00137 {
00138 UNS32 res;
00139 res = decompo_dcf(d,0x01);
00140 }
00141 }
00142 else
00143 {
00144 (*d->preOperational)();
00145 }
00146 }
00147 break;
00148
00149 case Operational:
00150 if(d->nodeState == Initialisation) return 0xFF;
00151 {
00152 s_state_communication newCommunicationState = {0, 1, 1, 1, 1, 1};
00153 d->nodeState = Operational;
00154 newState = Operational;
00155 switchCommunicationState(d, &newCommunicationState);
00156 (*d->operational)();
00157 }
00158 break;
00159
00160 case Stopped:
00161 if(d->nodeState == Initialisation) return 0xFF;
00162 {
00163 s_state_communication newCommunicationState = {0, 0, 0, 0, 1, 0};
00164 d->nodeState = Stopped;
00165 newState = Stopped;
00166 switchCommunicationState(d, &newCommunicationState);
00167 (*d->stopped)();
00168 }
00169 break;
00170
00171 default:
00172 return 0xFF;
00173 }
00174
00175 }
00176 return 0;
00177 }
00178
00179
00180 UNS8 getNodeId(CO_Data* d)
00181 {
00182 return *d->bDeviceNodeId;
00183 }
00184
00185
00186 void setNodeId(CO_Data* d, UNS8 nodeId)
00187 {
00188 UNS16 offset = d->firstIndex->SDO_SVR;
00189 if(offset){
00190
00191 *(UNS32*)d->objdict[offset].pSubindex[1].pObject = 0x600 + nodeId;
00192
00193 *(UNS32*)d->objdict[offset].pSubindex[2].pObject = 0x580 + nodeId;
00194
00195
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 {
00205 UNS8 i = 0;
00206 UNS16 offset = d->firstIndex->PDO_RCV;
00207 UNS16 lastIndex = d->lastIndex->PDO_RCV;
00208 UNS32 cobID[] = {0x200, 0x300, 0x400, 0x500};
00209 if( offset ) while( (offset <= lastIndex) && (i < 4)) {
00210
00211 *(UNS32*)d->objdict[offset].pSubindex[1].pObject = cobID[i] + nodeId;
00212 i ++;
00213 offset ++;
00214 }
00215 }
00216
00217 {
00218 UNS8 i = 0;
00219 UNS16 offset = d->firstIndex->PDO_TRS;
00220 UNS16 lastIndex = d->lastIndex->PDO_TRS;
00221 UNS32 cobID[] = {0x180, 0x280, 0x380, 0x480};
00222 i = 0;
00223 if( offset ) while ((offset <= lastIndex) && (i < 4)) {
00224
00225 *(UNS32*)d->objdict[offset].pSubindex[1].pObject = cobID[i] + nodeId;
00226 i ++;
00227 offset ++;
00228 }
00229 }
00230
00231 *d->bDeviceNodeId = nodeId;
00232 }
00233
00234 void _initialisation(){}
00235 void _preOperational(){}
00236 void _operational(){}
00237 void _stopped(){}