Setting the command byte, waiting for an echo
| You need to understand both MultiTrack and SPLat semaphores to understand the following. |
Down in the core of the program is a mechanism for setting the Tx command byte (bTxCmd in the Tx data block) and then waiting for an echo. This consists of a subroutine that is called with the required command byte in the X register, and a semaphore that is set only after the SX10509 has echoed the command. The steps for using this are:
- Load the required command byte into X
- Call the subroutine
- Wait for the “all done” semaphore to get set.
The SX10509 will only respond to a different command to the last one processed. For several commands of the same type in a row, the trick is to toggle (invert) bit 7 of the command byte. The subroutine below takes care of that under the hood.
Here’s the first part of the code. This is the subroutine that gets called in step 2 above, with X set to the command byte.
(Click here for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help (.chm) files)
SCH_SetCommand:
Push
Recall bTxCmd
Swap
Store bTxCmd
Compare
GoIfNZ SCH_SC1 ; g/ new <> old
NotS 7,bTxCmd ; old = new ... toggle bit 7
SCH_SC1:
ClrS sSCH_GotEcho ;Clear the Echo semaphore
LaunchTask tsk_SCH_AckWait ;Task that watches out for an echo.
Return
sSCH_GotEcho: defSEM ;Signals completion of a SCH/Xwire command
The above code compares the new command with the previous one, which it retrieves from the Tx data block. It then toggles bit 7 if they are the same. Once that is done is clears the “all done” semaphore sSCH_GotEcho, and launches a task tsk_SCH_AckWait that is going to watch for a match between the sent command bTxCmd and the received echo byte bRxCmdEcho.
Here’s the “wait task” tsk_SCH_AckWait:
tsk_SCH_AckWait:
YieldTask
Recall bTxCmd ;Get back the command just set
Recall bRxCmdEcho ;read the Rx echo location
Compare
GoIfNZ tsk_SCH_AckWait ; g/ different
SetS sSCH_GotEcho ;Signal that the echo is received
KillTask ;Job done!
This task just sits looping until the echo matches what was sent, then sets the semaphore and kills itself.