refactor code to move buffers to SPI
authorTero Marttila <terom@fixme.fi>
Sat, 08 May 2010 21:19:53 +0300
changeset 26 db2ec641c955
parent 25 33496b1a964f
child 27 359c1771c366
refactor code to move buffers to SPI
led7seg.inc
led7seg.s
spi.inc
--- a/led7seg.inc	Sat May 08 20:59:22 2010 +0300
+++ b/led7seg.inc	Sat May 08 21:19:53 2010 +0300
@@ -39,13 +39,8 @@
 .equ LED7_DOT   = 16
 .equ LED7_EMPTY = 17
 
-; output buffer
-.set LED7_COUNT = 2
-
-.dseg
-led7_buffer:    .byte LED7_COUNT
-
-.cseg
+; LEDs are on beginning of SPI universe
+.set led7_buffer = spi_outbuf + 0
 
 ;; Initialize LCD to empty, and enable
 LED7_Init:
@@ -58,11 +53,8 @@
         sts         led7_buffer + 0, r16
         sts         led7_buffer + 1, r16
 
-    ; Output
-        ldi         r16, LED7_COUNT
-        ldi         XL, low(led7_buffer + LED7_COUNT)
-        ldi         XH, high(led7_buffer + LED7_COUNT)
-        rcall       SPI_SendBuf
+    ; Update display
+        rcall       SPI_SendRecv
 
     ; Enable output once the initial display has been shifted out
         cbi         LED7_PORT, LED7_OE
@@ -151,16 +143,11 @@
 ;; Display a raw segment mask
 ;;  Input: r16, r17
 LED7_ShowRaw:
-    ; Set buffer
+    ; Store buffer
         sts         led7_buffer + 0, r16
         sts         led7_buffer + 1, r17
 
-    ; Output
-        ldi         r16, LED7_COUNT
-        ldi         XL, low(led7_buffer + LED7_COUNT)
-        ldi         XH, high(led7_buffer + LED7_COUNT)
-
-    ; Display
-        rjmp        SPI_SendBuf
+    ; Update display
+        rjmp        SPI_SendRecv
 
 
--- a/led7seg.s	Sat May 08 20:59:22 2010 +0300
+++ b/led7seg.s	Sat May 08 21:19:53 2010 +0300
@@ -198,11 +198,11 @@
         rcall       LED7_Init    
     
     ; Run
-        rcall       Main_ShowADC
+        ; rcall       Main_ShowADC
         ; rcall       Main_Spin
-        ; rcall       Main_Countup
+        rcall       Main_Countup
         ; rcall       Main_Countdown
-        ; rcall       Main_Blink
+        rcall       Main_Blink
 
 end:
         rjmp        end
--- a/spi.inc	Sat May 08 20:59:22 2010 +0300
+++ b/spi.inc	Sat May 08 21:19:53 2010 +0300
@@ -13,6 +13,15 @@
 .equ SPI_FLAGS  = GPIOR0
 .equ SPI_BUSY   = 0
 
+; Number of in/out bytes
+.set SPI_BUFLEN  = 2
+
+.dseg
+spi_inbuf:  .byte SPI_BUFLEN
+spi_outbuf: .byte SPI_BUFLEN
+
+.cseg
+
 ;; Initialize SPI subsystem for master operation
 SPI_Init:
     ; Set modes
@@ -40,67 +49,45 @@
     ; Done
         ret
 
-;; Send/recv one byte
-;;  Input: r16
-;;  Output: r10
-;;  Note: should not be busy...
+;; Send/Recv from/to SPI buffers
 SPI_SendRecv:
     ; Flag
         sbi         SPI_FLAGS, SPI_BUSY
 
-    ; Enable slave (low)
+    ; Start of packet
         cbi         SPI_PORT, SPI_SS
     
-    ; Write byte (starts SCK)
-        out         SPDR, r16
+    ; Init buffers
+        ldi         r16, SPI_BUFLEN
+        
+        ; send/recv in reverse order
+        ldi         XL, low(spi_inbuf + SPI_BUFLEN)
+        ldi         XH, high(spi_inbuf + SPI_BUFLEN)
+        ldi         YL, low(spi_outbuf + SPI_BUFLEN)
+        ldi         YH, high(spi_outbuf + SPI_BUFLEN)
     
-    ; Wait for byte to be sent
-spi_sr_wait:
+    ; Write
+spi_sr_next:
+        ; load+send tail byte
+        ld          r1, -Y
+        out         SPDR, r1
+
+    ; Wait
+spi_sr_wait:        
         in          r1, SPSR
         sbrs        r1, SPIF
         rjmp        spi_sr_wait
 
     ; Read
-        mov         r10, r11
-        in          r11, SPDR
-    
-    ; Drive SS high (end of packet)
-        sbi         SPI_PORT, SPI_SS
-
-    ; Done
-        ret
-
-;; Send buffer bytes
-;;  Input: post-buffer byte in X, buffer length in r16
-;;  Output: r10
-;;  Note: should not be busy...
-SPI_SendBuf:
-    ; Flag
-        sbi         SPI_FLAGS, SPI_BUSY
-
-    ; Enable slave (low)
-        cbi         SPI_PORT, SPI_SS
-
-    ; Write byte from X (starts SCK)
-spi_srb_next:
-        ld          r1, -X
-        out         SPDR, r1
-
-    ; Wait for byte to be sent
-spi_srb_wait:        
-        in          r1, SPSR
-        sbrs        r1, SPIF
-        rjmp        spi_srb_wait
-
-    ; Read
-        mov         r10, r11
-        in          r11, SPDR
+        ; read+store tail byte
+        in          r1, SPDR
+        st          -X, r1
  
     ; Done?
         dec         r16
-        brne        spi_srb_next
+        brne        spi_sr_next         ; if nonzero
     
-    ; Drive SS high (end of packet)
+    ; End of packet
         sbi         SPI_PORT, SPI_SS
 
     ; Done