;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Copyright (c) 2001-2002 TIMEX Corporation.
;
; This document is the property of, and contains information proprietary to
; TIMEX Corporation.  It is to be returned upon request.  Reproduction by any
; means, disclosure, or use of its contents for other than the expressed
; purpose for which it is loaned, without the written consent of TIMEX
; Corporation, is strictly prohibited.
;
; File Name:       comm.h
;
; Purpose:         Kernel Communications Header File
;
; Change History:  05/15/2000 ACN - Created file
;
; M851 - Development:
;                  01/02/2002 SER - Modified for M851 Development.  Changes
;                                   were made throughout, per the Timex USB
;                                   Communications Protocol document.
;                  05/21/2002 SER - ECR#851-20020416-002 - Added flag bit to
;                                   support return to home check.
;                  05/30/2002 SER - ECR#851-20020503-116 - Added timeout constant.
;                  06/13/2002 SER - ECR#851-20020430-087 - Changed packet timeout
;                                   back to the original design value of 2 seconds.
;
; Dependencies:    core.h
;                  corevars.asm
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Bit Definitions
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;COMMFlags1
bCOMMSynchronousPortInitialized         equ     00000001B       ; synchronous port is initialized
bCOMMError                              equ     00000010B       ; error has occurred during communication
bCOMMTransmissionComplete               equ     00000100B       ; transmission has been completed
bCOMMDeviceInfoRequestReceived          equ     00001000B       ; device information request has been received
bCOMMMultiPacketWriteStarted            equ     00010000B       ; multiple write was started
bCOMMAlertOnEveryPacket                 equ     00100000B       ; alert is generated after every packet
bCOMMPacketTimeout                      equ     01000000B       ; packet timeout is being monitored
bCOMMReturnToHome                       equ     10000000B       ; return to home position is being checked
                                                                ; after disconnect event


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Communication Variables Equates
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; The following buffer is used only while the watch is in the communications mode.  It is used
; a temporary storage buffer when the COMMAPPLICATIONINITIALIZATIONINTERNAL packet is received.
; The packet is copied to the work buffer, and a new packet is formed as defined for the
; COMMAPPLICATIONINITIALIZATIONEXTERNAL packet.  Memory will be as follows while this packet is
; being formed:
; COREWorkBuffer:
;   COMMAPPLICATIONINITIALIZATIONEXTERNAL packet to be formed.  Currently maximum size is 58 bytes.
; COREWorkBuffer+60 ==> COMMWorkBuffer address
;   COMMAPPLICATIONINITIALIZATIONINTERNAL packet has temporarily been copied here.  Currently maximum size
;   is 35 bytes.
; NOTE:  These two buffers added together should never exceed the total size allocated to the COREWorkBuffer.

; This is just a label to indicate the area where the communications system data starts.

COMMWorkBuffer:                 equ     COREWorkBuffer+60


; The following variables are used only while the watch is in the communications mode.  They are
; placed right after the COREWorkBuffer so that these variables can also be used as common RAM
; areas which the foreground application can use for temporary storage.

; This is just a label to indicate the area where the communications system data starts.

COMMVarsStart:                  equ     COREForegroundCommonBuffer  ; comm variables address


; Holds the number of bytes for memory data transfers.

COMMNumberOfBytes:              equ     COMMVarsStart+0         ; init value: undefined
                                                                ; size: 1 bytes

; Holds the address for memory data transfers.

COMMMemoryAddress:              equ     COMMVarsStart+1         ; init value: undefined
                                                                ; size: 2 bytes

; Holds the memory type that is being accessed.  Its initialization value should be set to zero on
; state entry.

COMMMemoryType:                 equ     COMMVarsStart+3         ; size: 1 byte

; Holds the status flags 1 of the communications mode.  Its initialization value should be set to
; zero on state entry.

COMMFlags1:                     equ     COMMVarsStart+4         ; size: 1 byte

; Holds the packet pointer for the interrupt handler.  This should be initialized to zero
; for each RX/TX packet.

COMMPacketPtr:                  equ     COMMVarsStart+5         ; size: 2 bytes

; Holds the number of bytes RX/TX within the interrupt handler.

COMMNumBytesRXTX:               equ     COMMVarsStart+7         ; size: 1 byte

; Holds the current state of the RX/TX indicator on the UI.  This needs to be maintained since these
; indicators are not shown when the crown is out.  The display will need to be refreshed once the crown is
; pushed back in.

COMMRXTXIndicator:              equ     COMMVarsStart+8         ; size: 1 byte

; Holds the number of bytes within a USB RX/TX packet.

COMMNumUSBPacketBytes:          equ     COMMVarsStart+9         ; size: 1 byte


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   State Names
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

COMMDEFAULTSTATE                equ     COREBANNERSTATE         ; default state
COMMSETSTATE                    equ     COREDEFAULTSTATE        ; set state


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Switch Equates
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Switches are not monitored within COMM mode.  No actions are taken based on
; switch presses.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Offset to the data in the packet.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

COMMPACKETLENGTHOFFSET                  equ     0               ; number of all bytes in a packet

COMMCOMMANDOFFSET                       equ     1               ; command or response

COMMFIRSTDATAOFFSET                     equ     2               ; generic first data following the command
COMMERRORTYPEOFFSET                     equ     2               ; error type
COMMABSOLUTEADDRESSOFFSET               equ     2               ; absolute address in EEPROM/ROM/RAM
COMMDATABASESIZEOFFSETPOS1              equ     2               ; database size - position 1
COMMRESPONSECOMMANDOFFSET               equ     2               ; response command offset

COMMRESPONSEDATAOFFSET                  equ     3               ; response data offset

COMMMEMORYTYPEOFFSET                    equ     4               ; memory type either EEPROM/ROM/RAM
COMMACDOFFSETPOS1                       equ     4               ; ACD information - position 1

COMMABSOLUTEMEMORYDATAOFFSET            equ     5               ; first data byte to be written to absolute memory
COMMNUMOFBYTESOFFSET                    equ     5               ; number of bytes to be read from absolute memory
COMMAPPINSTANCEOFFSETPOS                equ     5               ; application instance

COMMPORAPPOFFSETOFFSET                  equ     6               ; POR application address table offset

COMMMODESTRINGOFFSETPOS1                equ     7               ; user supplied mode string - position 1

COMMDATABASESIZEOFFSETPOS2              equ    15               ; database size - position 2

COMMACDOFFSETPOS2                       equ    17               ; ACD information - position 2

COMMAPPUNIQUEIDOFFSETPOS                equ    18               ; application unique identifier

COMMMODESTRINGOFFSETPOS2                equ    30               ; user supplied mode string - position 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Commands
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Transceiver Notification Messages
COMMTRANSMISSIONRECEIVEERROR            equ     00H             ; indicates tranmission receive errors occurred

; Communication Session
COMMDEVICEINFOREQ                       equ     01H             ; indicates the device information request made
COMMPROCESSCOMPLETE                     equ     02H             ; indicates communications process is complete

; System Session
COMMDELETEALLAPPS                       equ     03H             ; deletes all applications prior to a full download
COMMBEEP                                equ     04H             ; beeps the watch
COMMIDLE                                equ     05H             ; do nothin'
COMMCALLANABSOLUTEADDRESS               equ     06H             ; calls a routine at an absolute address
COMMWRITETOTODTIMEZONEDATA              equ     07H             ; writes time data into the various timezones

; Application Initialization
COMMAPPLICATIONINITIALIZATIONINTERNAL   equ     08H             ; initialize an application in internal memory
COMMAPPLICATIONINITIALIZATIONEXTERNAL   equ     09H             ; initialize an application in external memory

; Single and Multiple Packet Transfer Messages
COMMMULTIPLEPACKETWRITESTART            equ     0AH             ; sets up for a multiple packet write to memory
COMMWRITETOABSOLUTEADDRESS              equ     0BH             ; writes data into an absolute address
COMMREADFROMABSOLUTEADDRESS             equ     0CH             ; reads data from an absolute address

COMMLASTCOMMANDVALUE                    equ     0CH             ; last command number value

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ACK/NACK Responses
;   NOTE: The ACK character defined below should be one plus the last command defined in the table above.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

COMMACKCHARACTER                        equ     0DH             ; ACK character
COMMNACKCHARACTER                       equ    0FFH             ; NACK  character

COMMNUMBYTESINSIMPLERESPONSEPACKET      equ     04H             ; number of bytes in simple ACK or NACK
                                                                ; based response packet


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Number of Non-data Bytes for Various Packet Types
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COMMWRITETOABSOLUTEADDRESSOVERHEAD          equ     06H         ; number of overhead bytes for write data into
                                                                ; an absolute address

COMMRESPONSEPACKETWITHDATAOVERHEAD          equ     04H         ; number of overhead bytes for response
                                                                ; packet with data


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Error Processing
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Error timeout values.
COMMERRORTIMEOUTLORES_6SEC              equ     6               ; 6 second low resolution timeout
COMMERRORTIMEOUTHIRES_2SEC              equ    16               ; 2 second high resolution timeout

; Number of bytes for internally generated error packet.
COMMNUMBYTESINERRORPACKET               equ     03H             ; number of bytes in internal error packet

; Errors seen by the hardware/core area.
COMMINCORRECTCHECKSUM                   equ     00H             ; incorrect checksum
COMMINVALIDMESSAGELENGTH                equ     01H             ; message or packet length is less than the minimum or
                                                                ; larger than the maximum

; Errors seen by the communications area.
COMMDEVICEINFOREQUESTNOTRECEIVED        equ     02H             ; device information request packet has not been
                                                                ; received yet
COMMPCANDWATCHMISMATCH                  equ     03H             ; pc and watch data has been mismatched
COMMTIMEOUTEXPIRED                      equ     04H             ; packet delay timeout
COMMDISCONNECTED                        equ     04H             ; communication disconnected

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;   Memory Types
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

COMMMEMORYTYPEINTERNAL                  equ     00H             ; internal memory
COMMMEMORYTYPEEXTERNAL                  equ     01H             ; external memory


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Types of Beep Status
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

COMMAUTOMATICBEEP                       equ     00H             ; beep after every packet
COMMCONTROLLEDBEEP                      equ     01H             ; beep only after the beep packet


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Display Communication Line States
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

COMMIDLELINE                            equ     0               ; RX / TX idle 
COMMRXSTARTED                           equ     1               ; RX started
COMMTXSTARTED                           equ     2               ; TX started

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Miscellaneous Equates
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

COMMNUMBERBYTESINUSBPACKET              equ     8               ; number of bytes received in USB packet

COMMMINIMUMPACKETBUFFERLENGTH           equ     3               ; minimum size of inbound packet

COMMMAXIMUMPACKETBUFFERLENGTH           equ     72              ; maximum size of inbound/outbound packet
                                                                ; transfer buffer - needs to be a multiple of
                                                                ; eight.  NOTE: this equate is different than
                                                                ; the maximum inbound packet size defined by
                                                                ; the CORE.

COMMENDOFMODELIST                       equ     COREFREEBYTE    ; signifies the end of mode list
COMMNEWAPPLICATION                      equ     COREERRORCODE   ; a new application is being spawned
COMMPCWATCHDATAERROR                    equ     COREERRORCODE   ; PC and watch data mismatch
COMMCHECKSUMLENGTH                      equ     1               ; number of checksum bytes in a packet

; Applications heap size requirements.
COMMASDHEAPSIZEREQUIREMENTS             equ     0               ; number of ASD heap bytes required
COMMADDHEAPSIZEREQUIREMENTS             equ     0               ; number of ADD heap bytes required
COMMHEAPSIZEREQUIREMENTS                equ     (COMMASDHEAPSIZEREQUIREMENTS + COMMADDHEAPSIZEREQUIREMENTS)


; Mask to disable all switches included in this definition.
COMMDISALLOWALLSWITCHES equ (bCORESwitch1|bCORESwitch2|bCORESwitch3|bCORECWSwitch|bCORECCWSwitch|bCOREELSwitch)
