Knowledge Base

Example: Advanced Simple I/O expansion using Xwire

Let’s consider a slightly more advanced example of SEXi operation. This time, we’ll use a DA8, which has 8 digital inputs, 6 digital outputs, 8 analogue inputs, and 2 analogue ouputs.

Master

By default, the Xwire Master script will look like this:

XWpMaster
   ;DA8,          Address, 255=SEXi, 8 din, 6 dout, 8 ain,  2 aout
   NV0Byte        0,       255,      8,     6,      8,      2,      0,0,0,0
   NV0Byte        255   ; Terminate the script

When this script is executed, the I/O for the DA8 simply gets added on to the end of the list for the master device. Firmware in the master takes care of tracking those I/Os. For example, if we have an HMI430 or HMI700, the I/O map will look something like this:

;-- digital inputs --
;all digital inputs are bi-directional, hence are shared with the digital outputs
iIp0              iEQU  0
iIp1              iEQU  1
iIp2              iEQU  2
iIp3              iEQU  3
iIp4              iEQU  4
iIp5              iEQU  5
iIp6              iEQU  6
iIp7              iEQU  7
                       ;8 reserved
;-- DA8
iIn0              iEQU  9
iIn1              iEQU  10
iIn2              iEQU  11
iIn3              iEQU  12
iDipSw1           iEQU  13
iDipSw2           iEQU  14
iDipSw3           iEQU  15
iDipSw4           iEQU  16

;-- digital outputs --
;all digital outputs are bi-directional, hence are shared with the digital inputs
oOp0              oEQU  0
oOp1              oEQU  1
oOp2              oEQU  2
oOp3              oEQU  3
oOp4              oEQU  4
oOp5              oEQU  5
oOp6              oEQU  6
oOp7              oEQU  7
oBuzzer           oEQU  8
;-- DA8
oOut0             oEQU  9
oOut1             oEQU  10 
oOut2             oEQU  11
oOut3             oEQU  12
oLedOn            oEQU  13
oLedGreen         oEQU  14

;-- analogue inputs --
aiA               EQU   0
aiB               EQU   1
;-- DA8
aiIn0             EQU   2
aiIn1             EQU   3
aiIn2             EQU   4
aiIn3             EQU   5
aiIn4             EQU   6
aiIn5             EQU   7
aiIn6             EQU   8
aiIn7             EQU   9

;-- analogue outputs --
aoC               EQU   0
;-- DA8
aoOut0            EQU   1
aoOut1            EQU   2

Slave

Surprisingly, the firmware in the slave end doesn’t handle SEXi. Instead, we have to build the code ourselves. This is what’s running in a task on the DA8 by default:

taskSEXI:
   ;Xwire communications lost, turn off all outputs
   LoadX          0
   LoadX          %00001111
   OutputM        0                                   ;digital 0 thru 3
   fLoadW         0.0
   fAnOut         0
   fAnOut         1
   SetMem         XWbOutput0_7,0
   SetMem         XWbBlink0_7,0
   fStore         XWfAnalogOut0
   fStore         XWfAnalogOut1
   Off            oLedGreen                           ;signal we have..
   On             oLedOn                              ;..no connection
   SetMem         XWbInputSeq,0                       ;setup start up synchronisation
   SetMem         XWbInputSeqEcho,255
   SetS           XWsWafEqual

_XWWafFirstPacket
   MarkTime                                           ;start the interface toggle timer
_XWWafFirstPacketLoop
   YieldTask
   GoIfMz         XWbInputSeqEcho,_XWGotConnection    ;wait for master's first output update
   LoopIfTiming   100,_XWWafFirstPacketLoop           ;waf 1 second
   GoSub          subTogglePhys                       ;try the other interface
   Goto           _XWWafFirstPacket                   ;restart the timer
_XWGotConnection
   On             oLedGreen                           ;signal we are now..
   On             oLedOn                              ;..connected

_XWSendInputs
   ;-- capture latches
   LoadX          %00001111                           ;capture rising edge flags, but not the DIP switches
   InputMK        0
   Store          XWbInputK0_7
   RecallS        XWsWafEqual                         ;TRUE if this is the first packet
   StoreS         XWiSlaveOK,XWbInputK0_7             ;set slave ok latch if this is the first pass

   ;-- capture digital inputs
   InputM         0                                   ;capture debounced input levels
   LoadX          %00001111                           ;filter off the..
   AndM                                               ;..DIP switches
   Store          XWbInputs0_7
   SetS           XWiSlaveOK,XWbInputs0_7             ;keep slave ok input active

   ;-- capture analogue inputs
   fAnIn          0                                   ;pass latest analogue values to the master
   fStore         XWfAnalogIn0
   fAnIn          1
   fStore         XWfAnalogIn1
   fAnIn          2
   fStore         XWfAnalogIn2
   fAnIn          3
   fStore         XWfAnalogIn3
   fAnIn          4
   fStore         XWfAnalogIn4
   fAnIn          5
   fStore         XWfAnalogIn5
   fAnIn          6
   fStore         XWfAnalogIn6
   fAnIn          7
   fStore         XWfAnalogIn7

   Recall         XWbInputSeq                         ;increment the sEQUence number but avoid zero
   IncX
   Push
   GoIfnz         _XWStoreSeq
   IncX                    
_XWStoreSeq
   Store          XWbInputSeq
   XwireGetPollCntr                                   ;reset the poll counter
   MarkTime

_XWWafNewPacket
   YieldTask
   Recall         XWbInputSeqEcho                     ;get the seq number echoed back from the master
   GoIfST         XWsWafEqual,_XWChkEqual             ;for the first packet, we await sync
   XwireGetPollCntr                                   ;have we been polled by the master?
   GoIfZ          _XWChkEqual                         ;jump if not, wait for master to echo our sEQUence number back to us
   IncX                                               ;owise, we'll allow the master's seq number to be one behind ours
   Push                                               ;but skip..
   GoIfnz         _XWChkEqual
   IncX                                               ;..zero
_XWChkEqual
   Recall         XWbInputSeq                         ;get our seq number
   Compare
   GoIfz          _XWHandleNewPacket                  ;jump if the sequence numbers match (or we've been polled and it's one behind)
   LoopIfTiming   XWkErrorTime,_XWWafNewPacket        ;check for Xwire communication error
   Goto           taskSEXI                            ;owise comms lost, outputs off and re-synchronise

_XWHandleNewPacket
   ClrS           XWsWafEqual                         ;now we've been polled, we'll allow our sync to be one ahead for speed

   ;--echo digital outputs
   Recall         XWbOutput0_7                        ;update outputs if not being blinked
   Recall         XWbBlink0_7
   NotM
   LoadX          %00001111                           ;only update..
   AndM                                               ;..the 4 outputs (not the LED)
   OutputM        0

   ;--echo blink
   Recall         XWbBlink0_7                         ;update blink outputs
   LoadX          %00001111                           ;only update..
   AndM                                               ;..the 4 outputs (not the LED)
   BlinkM         0

   ;--echo analogue outputs
   fRecallW       XWfAnalogOut0                       ;update the analogue outputs
   fAnOut         0
   fRecallW       XWfAnalogOut1
   fAnOut         1

   Goto           _XWSendInputs
...
XWpSlave
   NV0Byte        XWabInputBuffer,XWkInputlen,XWabOutputBuffer,XWkOutputLen

The memory blocks are defined this way:

XWkInputLen       #EQU        35
XWabInputBuffer   defBLOCK    XWkInputLen
XWbInputSeq       defBYTE
XWbInputs0_7      defBYTE
XWbInputK0_7      defBYTE
XWiSlaveOK           #EQU  4
XWfAnalogIn0      defFLOAT
XWfAnalogIn1      defFLOAT
XWfAnalogIn2      defFLOAT
XWfAnalogIn3      defFLOAT
XWfAnalogIn4      defFLOAT
XWfAnalogIn5      defFLOAT
XWfAnalogIn6      defFLOAT
XWfAnalogIn7      defFLOAT

XWkOutputLen      #EQU        11
XWabOutputBuffer  defBLOCK    XWkOutputLen
XWbInputSeqEcho   defBYTE
XWbOutput0_7      defBYTE
XWbBlink0_7       defBYTE
XWfAnalogOut0     defFLOAT
XWfAnalogOut1     defFLOAT

Note that the LEDs are explicitly NOT controlled by the master. Similarly, the DIP switches are not sent. You can, in fact, omit these from the Xwire map and it will still work. If you do, of course, you’ll need to remove them from the I/O map as well.

Digital Inputs and Outputs Beyond the First 8

The DA8 has less than 8 inputs and less than 8 outputs. What happens if the device has more? For example, a DM16 or D T16?

Easy, just duplicate the Inputs and/or Outputs sections. Here’s the memory allocation for a DM16:

XWkInputLen       #EQU        13
XWabInputBuffer   defBLOCK    XWkInputLen
XWbInputSeq       defBYTE
XWbInputs0_7      defBYTE
XWbInputs8_15     defBYTE
XWbInputK0_7      defBYTE
XWbInputK8_15     defBYTE
XWfAnalogIn0      defFLOAT
XWfAnalogIn1      defFLOAT

XWkOutputLen      #EQU        21
XWabOutputBuffer  defBLOCK    XWkOutputLen
XWbInputSeqEcho   defBYTE
XWbOutput0_7      defBYTE
XWbOutput8_15     defBYTE
XWbBlink0_7       defBYTE
XWbBlink8_15      defBYTE
XWfAnalogOut0     defFLOAT
XWfAnalogDOut7    defFLOAT
XWfAnalogDOut8    defFLOAT
XWfAnalogDOut9    defFLOAT

You can see that each element is duplicated in turn: The “XWbInputs” lines, followed by the matching “XWbInputK” lines (which report changes of states); then the “XWbOutput” lines and the “XWbBlink” lines. The code is duplicated as well.

Virtual Outputs and Inputs

By extending this code, it’s also possible to add extra virtual outputs and inputs to do other functions. For example, a board such as a DA8 usually has at least one or two serial ports available, which can be used as Modbus masters (for example) to read sensors, and/or to control actuators. The results of these sensors can be reported as additional “virtual” inputs (digital or analogue). The actuators can be controlled using additional “virtual” outputs.

A very common “virtual” input is a a “Slave OK” input. Similarly, a very common “virtual” output is a “Master OK” output. If Xwire stops working for some reason (such as the wire being unplugged or breaking), the firmware will turn these signals off.

Memory Transfer

Now, you may have noticed that “0,0,0,0” at the end of the Master script near the top of this page. What’s that all about, then?

These are for memory transfer. The first two numbers are the address (in the master) and length of memory to be sent to the slave, and the second are the address (in the master) and length of memory to be sent from the slave to the master. Here’s an example of a Master script that receives some state and fault code from a modified DA8:

XWkHPStateLen        EQU   2
XWpHpState           mEQU  156,XWkHPStateLen
XWbHpMainState       mEQU  156,1
XWbHpFaultCode       mEQU  157,1
...
XWpMaster
   ;DA8,                   8 din, 9 dout, 12 ain, 4 aout
   NV0Byte        0, 255,  8,     9,      12,     4,      0,0,XWpHpState,XWkHPStateLen  ; DA8
   NV0Byte        255

This particular DA8 also has some extra virtual inputs and outputs.

At the other end (the DA8 in this case), these extra bits of memory are simply tacked onto the end of the memory block for the Xwire transfer:

XWkInputLen       #EQU        53
XWabInputBuffer   defBLOCK    XWkInputLen
XWbInputSeq       defBYTE
XWbInputs0_7      defBYTE
XWbInputK0_7      defBYTE     ; For changes of state
XWiSlaveOK           #EQU  4
XWiHeatPumpOK        #EQU  5
XWfAnalogIn0      defFLOAT
XWfAnalogIn1      defFLOAT
XWfAnalogIn2      defFLOAT
XWfAnalogIn3      defFLOAT
XWfAnalogIn4      defFLOAT
XWfAnalogIn5      defFLOAT
XWfAnalogIn6      defFLOAT
XWfAnalogIn7      defFLOAT
XWfTankTemp       defFLOAT    ; Temperatures read from the heat pump
XWfDefrostTemp    defFLOAT
XWfHotSP          defFLOAT    ; Setpoints read from the heat pump
XWfColdSP         defFLOAT
XWbMainState      defBYTE
XWbFaultCode      defBYTE

You can also see where the additional inputs are added.

Caveats

All Xwire memory blocks must be in the lower 250 bytes of RAM. If you’re using Dash and 16-bit addressing, you may need to use mEQU statements to place the memory blocks correctly. Xwire firmware only works in that lower 250 bytes,