From fdfc040d19079e9aa6e6d9e5c40f831b48ff28d8 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sat, 24 Jan 2026 01:03:31 +0100 Subject: [PATCH] avr: use macros instead of costly calls for SPI on ILI9341. --- avr/modules/lcd2/ili9341/font12x16.asm | 10 +++--- avr/modules/lcd2/ili9341/font6x8.asm | 10 +++--- avr/modules/lcd2/ili9341/graphops.asm | 47 +++++++++++++++++++++----- avr/modules/lcd2/ili9341/images.asm | 20 ++++++----- avr/modules/spi_hw/main.asm | 16 +++++++++ 5 files changed, 79 insertions(+), 24 deletions(-) diff --git a/avr/modules/lcd2/ili9341/font12x16.asm b/avr/modules/lcd2/ili9341/font12x16.asm index 29f456c..5e754e9 100644 --- a/avr/modules/lcd2/ili9341/font12x16.asm +++ b/avr/modules/lcd2/ili9341/font12x16.asm @@ -68,10 +68,12 @@ ili9341Font12x16MonoRenderChar_writeForeground: mov r18, r2 mov r19, r3 ili9341Font12x16MonoRenderChar_sendToDisplay: - mov r16, r19 - rcall SPIHW_MasterTransfer ; (R16) - mov r16, r18 - rcall SPIHW_MasterTransfer ; (R16) +; mov r16, r19 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r19 +; mov r16, r18 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r18 ili9341Font12x16MonoRenderChar_loop2end: dec r24 brne ili9341Font12x16MonoRenderChar_loop2 diff --git a/avr/modules/lcd2/ili9341/font6x8.asm b/avr/modules/lcd2/ili9341/font6x8.asm index 58a8415..1290a59 100644 --- a/avr/modules/lcd2/ili9341/font6x8.asm +++ b/avr/modules/lcd2/ili9341/font6x8.asm @@ -64,10 +64,12 @@ ili9341Font6x8WriteChar_writeForeground: mov r22, r2 mov r23, r3 ili9341Font6x8WriteChar_sendToDisplay: - mov r16, r23 - rcall SPIHW_MasterTransfer ; (R16) - mov r16, r22 - rcall SPIHW_MasterTransfer ; (R16) +; mov r16, r23 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r23 +; mov r16, r22 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r22 ili9341Font6x8WriteChar_loop2end: dec r24 brne ili9341Font6x8WriteChar_loop2 diff --git a/avr/modules/lcd2/ili9341/graphops.asm b/avr/modules/lcd2/ili9341/graphops.asm index b36fb00..788606d 100644 --- a/avr/modules/lcd2/ili9341/graphops.asm +++ b/avr/modules/lcd2/ili9341/graphops.asm @@ -23,6 +23,7 @@ ; @clobbers R16, R17, R20, R21, R22, R23, R24, R25 Display_FillRect: +#if 1 push r15 in r15, SREG cli @@ -42,9 +43,11 @@ Display_FillRect_loopH: mov r25, r9 ; W high Display_FillRect_loopW: mov r16, r3 - rcall SPIHW_MasterTransfer ; (R16) +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r16 ; (R16) mov r16, r2 - rcall SPIHW_MasterTransfer ; (R16) +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r16 ; (R16) sbiw r25:r24, 1 brne Display_FillRect_loopW mov r24, r22 ; remaining H low @@ -58,6 +61,30 @@ Display_FillRect_loopW: out SREG, r15 pop r15 ret +#else + push r15 + in r15, SREG + cli + + rcall ili9341BeginSpi ; (R16, R17) + rcall ili9341SetAddressWindow ; (R16, r20, r21) + + mov r20, r8 + mov r21, r9 + mov r22, r10 + mov r23, r11 + bigcall Utils_Mulu16x16_32 + mov r24, r16 + mov r25, r17 + rcall displaySendColor + + sbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN ; SS high + rcall ili9341EndSpi + out SREG, r15 + pop r15 + ret + +#endif ; @end @@ -200,9 +227,11 @@ displaySendColor: sbi ILI9341_DC_OUTPUT, ILI9341_DC_PIN ; D high (DATA) displaySendColor_loop: mov r16, r3 - rcall SPIHW_MasterTransfer ; (R16) +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r16 mov r16, r2 - rcall SPIHW_MasterTransfer ; (R16) +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r16 sbiw r25:r24, 1 brne displaySendColor_loop sbi SPIHW_SS_OUTPUT, SPIHW_SS_PIN ; SS high @@ -298,10 +327,12 @@ Display_BitBlit_loopH: Display_BitBlit_loopW: ld r18, X+ ; read color from X ld r19, X+ - mov r16, r19 - rcall SPIHW_MasterTransfer ; (R16) - mov r16, r18 - rcall SPIHW_MasterTransfer ; (R16) +; mov r16, r19 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r19 +; mov r16, r18 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r18 sbiw r25:r24, 1 brne Display_BitBlit_loopW mov r24, r22 diff --git a/avr/modules/lcd2/ili9341/images.asm b/avr/modules/lcd2/ili9341/images.asm index ddabe4a..13e5010 100644 --- a/avr/modules/lcd2/ili9341/images.asm +++ b/avr/modules/lcd2/ili9341/images.asm @@ -179,10 +179,12 @@ ili9341ImageIdx2WritePixels_l1: ld r17, X+ ld r16, X ; send to SPI (high byte first) - rcall SPIHW_MasterTransfer ; (R16) - mov r16, r17 - rcall SPIHW_MasterTransfer ; (R16) - +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r16 +; mov r16, r17 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r17 + dec r22 ; pixelCounterInByte dec r20 ; runWidth brne ili9341ImageIdx2WritePixels_horizLoop @@ -280,10 +282,12 @@ ili9341ImageIdx2rleWritePixels_l1: ld r17, X+ ld r16, X ; send to SPI (high byte first) - rcall SPIHW_MasterTransfer ; (R16) - mov r16, r17 - rcall SPIHW_MasterTransfer ; (R16) - +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r16 +; mov r16, r17 +; rcall SPIHW_MasterTransfer ; (R16) + M_SPIHW_MASTER_SEND_BYTE r17 + dec r22 ; pixelCounterInByte dec r20 ; runWidth brne ili9341ImageIdx2rleWritePixels_horizLoop diff --git a/avr/modules/spi_hw/main.asm b/avr/modules/spi_hw/main.asm index 8a70ee4..17f584f 100644 --- a/avr/modules/spi_hw/main.asm +++ b/avr/modules/spi_hw/main.asm @@ -8,6 +8,22 @@ ; *************************************************************************** +; *************************************************************************** +; macros + +; @param @0 register to use + +.macro M_SPIHW_MASTER_SEND_BYTE + outr SPDR, @0 ; masterSend Byte + l1_%: + inr @0, SPSR + sbrs @0, SPIF + rjmp l1_% + inr @0, SPDR +.endm + + + ; *************************************************************************** ; defines