Knowledge Base
Example program

If you’ve run out of serial ports, then you can log via Xwire.  It’s tedious to use, but this example shows one way to do it.  It has been tested on DC216, but should work on a MMi203 or MS121 too as it uses the LCD.


;===============================================================================
;PURPOSE:
; Sample DC216 app that writes a datestamp and floating point value to a
; SX10508 1302 logger.
;
;===============================================================================

;===============================================================================
;
;<INITSEG>
;
;===============================================================================
OBLCD_Dim 1,4,1 ;configure LCD dimming
XwireMaster pXwireMaster ;start running as an Xwire master
LaunchTask taskPretend
LaunchTask taskXwireMon
LaunchTask taskLogger
RunTasksForever


;===============================================================================
;
;<CONSTSEG>
;
;===============================================================================
;--Logger Commands
XLkCMD_Idle EQU '00 ;no action
XLkCMD_Write EQU '01 ;write data, length is the number of bytes to write
XLkCMD_Datestamp EQU '02 ;write a datestamp, length is number of directive characters, data is directive characters
XLkCMD_NewFile EQU '03 ;data may be an optional file name, otherwise the automatic sequence number is used
XLkCMD_AppendFile EQU '04 ;data may be an optional file name, otherwise the automatic sequence number is used
XLkCMD_DeleteFile EQU '05 ;data may be an optional file name, otherwise the current file is deleted
XLkCMD_GetTime EQU '06 ;gets the time from the clock in the logger board, returns HOURS,MINUTES,SECONDS
XLkCMD_GetDate EQU '07 ;gets the date from the clock in the logger board, returns DAY,MONTH,YEAR (year is 00 - 99)
XLkCMD_GetDOW EQU '08 ;gets the day of week, 0 = Monday thru 6 = Sunday
XLkCMD_SetTime EQU '09 ;sets the time where DATA1 thru 3 are HOURS,MINUTES,SECONDS
XLkCMD_SetDate EQU '0A ;sets tha date where DATA1 thru 3 are DAY,MONTH,YEAR (year is 00 - 99)
XLkCMD_FlushData EQU '0B ;force any data currently in the cache to be written to the card (this may decrease the life of your card)


;===============================================================================
;
;<MEQUSEG>
;RAM storage
;
;===============================================================================


;===============================================================================
;
;<IOSEG>
;I/O Assignments
;
;===============================================================================


;===============================================================================
;
;AUTOMATIC RAM
;
;===============================================================================
;<DEF_SEM>
sXwireError defSEM ;TRUE if there's an error communicating with the SX10508
sWriteFloat defSEM ;TRUE if the logger task should write the float value

;<DEF_BYTE>

;<DEF_WORD>
wLogResume defWORD ;address to resume log tx

;<DEF_TIME24>

;<DEF_FLOAT>
fNumber defFLOAT ;the example app number
fNumber2 defFLOAT

;<DEF_BLOCK>
;-- Xwire buffers for the SX10508 Logger --
XLkM2SLen #EQU 32 ;we report 32 bytes long, but we'll actually only reserve 10 bytes for data in this controller
defBLOCK 17 ;buffer length
XLbM2SCmd defBYTE ;command
XLxNewCommand EQU 7 ;bit 7 in bM2SCmd is toggled to indicate a new command
XLbM2SLength defBYTE ;length of transmit data
XLabM2SData defBYTE 10 ;data to be logged

XLkS2MLen #EQU 4
defBLOCK XLkS2MLen ;receive buffer
XLbS2MCmdEcho defBYTE ;copy of the last completed command (will match bM2SCmd when done)
XLabS2MResult defBYTE 3 ;command result / datetime value


;===============================================================================
;
;<CODESEG>
;
;===============================================================================

;===============================================================================
;DESCRIPTION:
; Increments a float every 5 seconds and requests it be written to a log file
;===============================================================================
taskPretend:
fRecallW fNumber
fLoadQ 0.1
fAdd
fStore fNumber
fLoadQ 0.1
fAdd
fStore fNumber2
SetS sWriteFloat
OBLCD_Cls
fRecallW fNumber
OBLCD_fDispW 8,1
OBLCD_SetCur 1,0
fRecallW fNumber2
OBLCD_fDispW 8,1
MarkTime
_PretendLoop
YieldTask
GoIfST sXwireError,_DispXWireErr
LoopIfTiming 500,_PretendLoop
Goto taskPretend
_DispXWireErr
OBLCD_Cls
;0123456789012345
OBLCD_Text "SX10508 Xwire "
OBLCD_Text "error"
Off oLedGreen
Blink oLedOn
WaitForSF sXwireError
Off oLedOn
Goto taskPretend


;===============================================================================
;DESCRIPTION:
; Main task manages Xwire transactions with the SX10508
;===============================================================================
taskLogger:
YieldTask
LoadI 0 ;zero the index for the logging routines

GoIfSF sWriteFloat,_NoWriteFloat ;go write the float log if the semaphore is set
ClrS sWriteFloat
GoSub _subWriteFloat
Goto _LoggingLoop
_NoWriteFloat
; --
; add others if required
; --

Goto taskLogger

;-----------------------------
;-- Run the logging routine --
;-----------------------------
_LoggingLoop
FloatMem16U wLogResume ;is the logging task still running?
fGoIfNZ _SaveCommand ;jump if it is
Goto _SendIdle ;otherwise it's no longer running, so send idle
_SaveCommand
ItoX ;store the..
Store XLbM2SLength ;..length of the data
GoSub subSendCommand
_WafCommandDone
YieldTask
GoIfST sXwireError,_XwireError ;abort if error encountered
Recall XLbS2MCmdEcho ;has the logger completed the..
Recall XLbM2SCmd ;..
Compare ;..last command?
GoIfNZ _WafCommandDone ;jump if not
LoadI 0 ;pre-clear the index register so the subroutine start writing at the head of the buffer
GoSub subResumeLog ;resume the logging routine
Goto _LoggingLoop
_SendIdle
LoadX XLkCMD_Idle
GoSub subSendCommand
_WafIdleDone
YieldTask
Recall XLbS2MCmdEcho ;has the logger completed the..
Recall XLbM2SCmd ;..
Compare ;..last command?
GoIfNZ _WafIdleDone ;jump if not
Goto taskLogger

_XwireError ;if there's an Xwire error, we display a message & light the red LEDs
WaitForSF sXwireError
Goto taskLogger


;===============================================================================
;DESCRIPTION:
; This subroutine writes a datestamp and float to the log file
;PARAMETERS:
; -> Nil
;RETURNS:
; <- Nil
;===============================================================================
_subWriteFloat
iiPrintText XLabM2SData,"Y/m/e H:M," ;datestamp format YYYY/MM/DD HH:MM:SS
LoadX XLkCMD_Datestamp
Suspend wLogResume

fRecallW fNumber ;get the float
iifPrintWVW XLabM2SData,8,1 ;write the float
iiPrintText XLabM2SData,"," ;separator
LoadX XLkCMD_Write ;request a write operation
Suspend wLogResume

fRecallW fNumber2 ;get the float
iifPrintWVW XLabM2SData,8,1 ;write the float
iiPrintText XLabM2SData,13,10 ;terminate the line with CR LF
LoadX XLkCMD_Write ;request a write operation
Suspend wLogResume

LoadX XLkCMD_FlushData ;force the log to be..
Suspend wLogResume ;..written to the card (not recommended)

Goto subWriteLogEnd


;===============================================================================
;DESCRIPTION:
; Called by the main routine to run the next Xwire transaction.
;PARAMETERS:
; -> Nil
;RETURNS:
; <- Nil
;===============================================================================
subResumeLog:
Resume wLogResume ;resume from where we left off


;===============================================================================
;DESCRIPTION:
; Loads a command into the Xwire buffer and toggles the new command bit to
; ensure the logger recognises a repeat command as being new.
;PARAMETERS:
; -> X: command to send
;RETURNS:
; <- Nil
;===============================================================================
subSendCommand:
GoIfST XLxNewCommand,XLbM2SCmd,_SaveSendCommand ;jump if the existing toggle bit is high
LoadX %10000000 ;otherwise it's low, so make..
OrM ;..it high
_SaveSendCommand
Store XLbM2SCmd ;issue the command
Return


;===============================================================================
;DESCRIPTION:
; Called once all transactions have completed to stop the logging loop.
;PARAMETERS:
; -> Nil
;RETURNS:
; <- Nil
;===============================================================================
subWriteLogEnd:
fLoadW 0
FixToMem16U wLogResume
Return


;===============================================================================
;DESCRIPTION:
;
;PARAMETERS:
; -> Nil
;RETURNS:
; <- Nil
;===============================================================================
taskXwireMon:
YieldTask
XwireGetErrCount ;any Xwire errors?
GoIfF taskXwireMon ;jump if none
SetS sXwireError
_FoundError
MarkTime
_ErrTimer
YieldTask
XwireGetErrCount ;any Xwire errors?
GoIfT _FoundError ;jump if there are
LoopIfTiming 50,_ErrTimer ;stay in error state for 1.2 second
ClrS sXwireError ;no errors
Goto taskXwireMon


;===============================================================================
;
;<NVEM0DIR>
;
;===============================================================================
NVEM0 ;Start of non-volatile memory


;===============================================================================
;
;<NVEM0DATA>
;Non-volatile memory data
;
;===============================================================================
pXwireMaster
NV0Byte 10,XLbM2SCmd,XLkM2SLen,XLbS2MCmdEcho,XLkS2MLen
NV0Byte 255