Merge pull request #63 from MagnaboscoL/master
minor improvement ecx_readstate()
This commit is contained in:
commit
1b0635ca28
|
@ -756,12 +756,62 @@ int ecx_FPRD_multi(ecx_contextt *context, int n, uint16 *configlst, ec_alstatust
|
|||
*/
|
||||
int ecx_readstate(ecx_contextt *context)
|
||||
{
|
||||
uint16 slave, fslave, lslave, configadr, lowest, rval;
|
||||
uint16 slave, fslave, lslave, configadr, lowest, rval, bitwisestate;
|
||||
ec_alstatust sl[MAX_FPRD_MULTI];
|
||||
uint16 slca[MAX_FPRD_MULTI];
|
||||
boolean noerrorflag, allslavessamestate;
|
||||
|
||||
lowest = 0xff;
|
||||
/* Try to establish the state of all slaves sending only one broadcast datargam.
|
||||
* This way a number of datagrams equal to the number of slaves will be sent only if needed.*/
|
||||
rval = 0;
|
||||
ecx_BRD(context->port, 0, ECT_REG_ALSTAT, sizeof(rval), &rval, EC_TIMEOUTRET);
|
||||
rval = etohs(rval);
|
||||
bitwisestate = (rval & 0x0f);
|
||||
|
||||
if ((rval & EC_STATE_ERROR) == 0)
|
||||
{
|
||||
noerrorflag = TRUE;
|
||||
context->slavelist[0].ALstatuscode = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
noerrorflag = FALSE;
|
||||
}
|
||||
|
||||
switch (bitwisestate)
|
||||
{
|
||||
case EC_STATE_INIT:
|
||||
case EC_STATE_PRE_OP:
|
||||
case EC_STATE_BOOT:
|
||||
case EC_STATE_SAFE_OP:
|
||||
case EC_STATE_OPERATIONAL:
|
||||
allslavessamestate = TRUE;
|
||||
context->slavelist[0].state = bitwisestate;
|
||||
break;
|
||||
default:
|
||||
allslavessamestate = FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (noerrorflag && allslavessamestate)
|
||||
{
|
||||
/* No slave has toggled the error flag so the alstatuscode
|
||||
* (even if different from 0) should be ignored and
|
||||
* the slaves have reached the same state so the internal state
|
||||
* can be updated without sending any datagram. */
|
||||
for (slave = 1; slave <= *(context->slavecount); slave++)
|
||||
{
|
||||
context->slavelist[slave].ALstatuscode = 0x0000;
|
||||
context->slavelist[slave].state = bitwisestate;
|
||||
}
|
||||
lowest = bitwisestate;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Not all slaves have the same state or at least one is in error so one datagram per slave
|
||||
* is needed. */
|
||||
context->slavelist[0].ALstatuscode = 0;
|
||||
lowest = 0xff;
|
||||
fslave = 1;
|
||||
do
|
||||
{
|
||||
|
@ -794,6 +844,7 @@ int ecx_readstate(ecx_contextt *context)
|
|||
fslave = lslave + 1;
|
||||
} while (lslave < *(context->slavecount));
|
||||
context->slavelist[0].state = lowest;
|
||||
}
|
||||
|
||||
return lowest;
|
||||
}
|
||||
|
@ -827,8 +878,9 @@ int ecx_writestate(ecx_contextt *context, uint16 slave)
|
|||
|
||||
/** Check actual slave state.
|
||||
* This is a blocking function.
|
||||
* To refresh the state of all slaves ecx_readstate()should be called
|
||||
* @param[in] context = context struct
|
||||
* @param[in] slave = Slave number, 0 = all slaves
|
||||
* @param[in] slave = Slave number, 0 = all slaves (only the "slavelist[0].state" is refreshed)
|
||||
* @param[in] reqstate = Requested state
|
||||
* @param[in] timeout = Timout value in us
|
||||
* @return Requested state, or found state after timeout.
|
||||
|
|
Loading…
Reference in New Issue