; ***************************************************************************
; copyright   : (C) 2025 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************

#ifndef AQH_AVR_ILI9341_IO_SPI_ASM
#define AQH_AVR_ILI9341_IO_SPI_ASM



; generally we use the following parameters here:
; @param r1:r0   background color
; @param r3:r2   foreground color
; @param r5:r4   X0
; @param r7:r6   Y0
; @param r9:r8   X1/W
; @param r11:r10 Y1/H


; ***************************************************************************
; defines

.equ ILI9341_SPIMODE = (0<<SPIHW_MODE_SPEED0_BIT) | \
                       (0<<SPIHW_MODE_SPEED1_BIT) | \
                       (1<<SPIHW_MODE_DOUBLESPEED_BIT) | \
                       (0<<SPIHW_MODE_DATAORDER_BIT) | \
                       (0<<SPIHW_MODE_CPOL_BIT) | \
                       (0<<SPIHW_MODE_CPHA_BIT)



; ***************************************************************************
; code

.cseg



; ---------------------------------------------------------------------------
; @routine ILI9341IoInit @global

ILI9341IoInit:
  ; setup pins
  sbi ILI9341_RESET_DDR, ILI9341_RESET_PIN    ; RESET= output
  sbi ILI9341_RESET_OUTPUT, ILI9341_RESET_PIN ; RESET= high
  sbi ILI9341_DC_DDR, ILI9341_DC_PIN          ; DC   = output
  sbi ILI9341_LED_DDR, ILI9341_LED_PIN        ; LED  = output
  cbi ILI9341_LED_OUTPUT, ILI9341_LED_PIN     ; LED  = low

  ret
; @end



; ---------------------------------------------------------------------------
; @routine ili9341BeginSpi
;
; @clobbers r16, r17

ili9341BeginSpi:
  ldi r16, ILI9341_SPIMODE
  ldi r17, ILI9341_DEVICENUM
  rjmp SPIHW_MasterStart                   ; (R17)
; @end



; ---------------------------------------------------------------------------
; @routine ili9341EndSpi
;
; @clobbers r16

ili9341EndSpi:
  rjmp SPIHW_MasterStop                    ; (R16)
; @end



; ---------------------------------------------------------------------------
; @routine ili9341SendCommand
;
; @param  r16 byte to send
; @return r16 byte received
; @clobbers r16

ili9341SendCommand:
  cbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN       ; SS low
  cbi ILI9341_DC_OUTPUT, ILI9341_DC_PIN   ; D low
  nop
  rcall SPIHW_MasterTransfer              ; (none)
  sbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN       ; SS low
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ili9341SendData
;
; @param  r16 byte to send
; @return r16 byte received
; @clobbers r16

ili9341SendData:
  cbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN       ; SS low
  sbi ILI9341_DC_OUTPUT, ILI9341_DC_PIN   ; D high
  rcall SPIHW_MasterTransfer              ; (R16)
  sbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN       ; SS low
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ili9341Send16BitData
;
; @param r19:18 data
; @clobbers r16

ili9341Send16BitData:
  cbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN       ; SS low
  sbi ILI9341_DC_OUTPUT, ILI9341_DC_PIN   ; D high
  mov r16, r19
  rcall SPIHW_MasterTransfer              ; (R16)
  mov r16, r18
  rcall SPIHW_MasterTransfer              ; (R16)
  sbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN       ; SS low
  ret
; @end



#endif ; AQH_AVR_ILI9341_IO_SPI_ASM

