; Primary application for the security system.
;
; ### license
;
; ### docco
;
; WRITE-TO and READ-FROM operations are supported.
;
; OPCODE values are defined as:
;   ### todo
;
; PINS:
;   ### TBD
;

        list p = 16f688
        include p16f688.inc
        include ../include/handy.inc

        include ../include/i2c.inc

        __config DEFAULT_CONFIG

; i2c.inc only has the no-prefix entrypoints. Define the prefixed forms.
 extern HOUSE_I2C_S_listen
 extern HOUSE_I2C_S_detect_START
 extern HOUSE_I2C_S_saw_START
 extern HOUSE_I2C_S_read
 extern HOUSE_I2C_S_read_stretch
 extern HOUSE_I2C_S_end_stretch
 extern HOUSE_I2C_S_write

 extern HOUSE_I2C_skip_unknown

 extern LOCAL_I2C_S_listen
 extern LOCAL_I2C_S_detect_START
 extern LOCAL_I2C_S_saw_START
 extern LOCAL_I2C_S_read
 extern LOCAL_I2C_S_read_stretch
 extern LOCAL_I2C_S_end_stretch
 extern LOCAL_I2C_S_write

 extern LOCAL_I2C_skip_unknown


; Pin definitions
 constant HOUSE_SCL = 1         ; PORTA
 constant HOUSE_SDA = 0         ; PORTA

 constant LOCAL_SCL = 4         ; PORTA
 constant LOCAL_SDA = 5         ; PORTA

; ### more


        ; Define this module's variables
.gpr0   udata

; The OPCODE sent in a WRITE-TO request on the I2C bus.
OPCODE  res     1

; Configuration mask
 global CONFIG_MASK
CONFIG_MASK res 1
        ; Bit 0 : ### TBD



.reset: org  0x0000
        goto main


.main:  code                    ; relocatable
main:

        bank0

        ; Disable comparators. We're going to use all RA* as INPUTS/OUTPUTS.
        movlw   0x07
        movwf   CMCON0

        ; ### latch any OUTPUTS

        bank1s

        ; Switch to running at 8 Mhz
        movlw   0x70
        iorwf   OSCCON, F

        ; Ensure AN* are configured as DIGITAL pins.
        clrf    ANSEL

        ; ### leave all tristates as INPUTS (HIGH-Z) for now

        ; Get some configuration information
        read_eeprom EE_CONFIG_MASK
        movwf   CONFIG_MASK

        bank0s

        ; ### need to set up the I2C slave module

busy_loop:
        ; Both I2C busses are IDLE. Any SDA transition is a START condition,
        ; so we don't really need to test SCL. Just need to start reading
        ; fast enough.

        ; Check if either SDA has transitioned LOW (while SCL remains HIGH).
        ; t(HD;STA) is 4.0 µs minimum, then SCL will transition LOW and hold
        ; for t(LOW) of 4.7 µs until SCL then transitions HIGH to deliver
        ; the first bit. That gives us 17 instructions to see SCL LOW, then
        ; watch for the transition edge.

        ; NOTE: HOUSE-SDA might be managed by the Arduino, so the above
        ;       timings are strict. We will test HOUSE_SDA "often" to ensure
        ;       we never miss a START. LOCAL-SDA is from another '688, and
        ;       we will specifically code a stretch of the t(HD;STA) time.

        ; ### check ALERT pin

        ; LOCAL0 needs to look at HOUSE-SDA and the MPR interrupts.

        ; Is HOUSE-SDA still HIGH?
        btfss   PORTA, HOUSE_SDA
        ; HOUSE-SDA just transitioned LOW.
        goto    house_activity

        ; ### anything else?
        goto    busy_loop


house_activity:
        call    HOUSE_I2C_S_saw_START
        bz      busy_loop
        bc      house_read_from

        call    HOUSE_I2C_S_read_stretch
        movwf   OPCODE

        goto    busy_loop

house_read_from:
        ; ### do something
        goto    busy_loop


; stupid assemblers don't realize EOF means *end*
        end
