PreviousNext
Handle Peripheral Request
Help > Custom DPA Handler > Events > DPA Request > Handle Peripheral Request

This request is sent whenever there is DPA Request for a peripheral that was not handled by the default DPA code. Typically the code handles requests for user peripherals or overridden embedded peripherals. If the handler does not handle the DPA Request then it must return FALSE to indicated error (then DPA Request contains response code ERROR_PNUM), otherwise, it must return TRUE.

 

Please note how to return an error state in the following code. Set PNUM to PNUM_ERROR_FLAG, set 1st data byte of the DPA Request to the error code, set 2nd byte to the original PNUM, and finally specify that the length of the data is 2. The best way is to use a predefined union member at _DpaMessage.ErrorAnswer.

 

If code saving is not an issue or there are just a few error types returned then it is easier to call DpaApiReturnPeripheralError API to return the error state. Otherwise shared (using goto) central error point is advised. Both methods can be seen in the code example below.

 

Example

 

case DpaEvent_DpaRequest:

else if ( IsDpaPeripheralInfoRequest() )

 // …

else

{

 // 1st user peripheral

 if ( _PNUM == PNUM_USER )

 {

 // Test for some data sent

       if ( DpaDataLength == 0 )

       {

        // Return error ERROR_DATA_LEN

        // DpaApiReturnPeripheralError(ERROR_DATA_LEN); is the easiest way

        _DpaMessage.ErrorAnswer.ErrN = ERROR_DATA_LEN;

UserErrorAnswer:

        _DpaMessage.ErrorAnswer.PNUMoriginal = _PNUM;

        _PNUM = PNUM_ERROR_FLAG;

        _DpaDataLength = sizeof( _DpaMessage.ErrorAnswer );

        return TRUE;

       }

 

       if ( _PCMD == 0 )

       {

        UseDataCmd0(_DpaMessage.Request.PData[0]);

        _DpaDataLength = 0;

        return TRUE;

       }

       else if ( _PCMD == 1 )

       {

        UseDataCmd1(_DpaMessage.Request.PData[0]);

        _DpaMessage.Response.PData[0] = someDataToReturn;

        _DpaDataLength = 1;

        return TRUE;

       }

       else

       {

        // Return error ERROR_PCMD

 // DpaApiReturnPeripheralError(ERROR_PCMD); is the easiest way

        _DpaMessage.ErrorAnswer.ErrN = ERROR_PCMD;

        goto UserErrorAnswer;

       }

 }

 

 return TRUE;

}

 

return FALSE;