spi.inc
author Tero Marttila <terom@fixme.fi>
Fri, 14 May 2010 18:19:22 +0300
changeset 31 dfb246ecaf23
parent 30 5226e512755c
child 33 0d0309787be3
permissions -rw-r--r--
8x8 LED Matrix driver
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
;; vim: filetype=avr
18
79b25e81721f use timer for 1s delay
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
     2
;;
79b25e81721f use timer for 1s delay
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
     3
;; SPI interface control and use
79b25e81721f use timer for 1s delay
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
     4
;;
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
.equ SPI_DDR    = DDRB
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
.equ SPI_PORT   = PORTB
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
.equ SPI_SCK    = PORTB5
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
.equ SPI_MISO   = PORTB4
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
.equ SPI_MOSI   = PORTB3
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
.equ SPI_SS     = PORTB2
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
.equ SPI_FLAGS  = GPIOR0
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
.equ SPI_BUSY   = 0
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    16
; Number of in/out bytes
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    17
.set SPI_BUFLEN  = 2
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    18
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    19
.dseg
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    20
spi_inbuf:  .byte SPI_BUFLEN
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    21
spi_outbuf: .byte SPI_BUFLEN
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    22
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    23
.cseg
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    24
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
;; Initialize SPI subsystem for master operation
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
SPI_Init:
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    ; Set modes
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        sbi         SPI_DDR, SPI_SCK        ; Out
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        sbi         SPI_DDR, SPI_MOSI       ; Out
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
        sbi         SPI_DDR, SPI_SS         ; Out
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    ; Drive SS high (off)
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        sbi         SPI_PORT, SPI_SS
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    ; Set control mode
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    36
        ; XXX: Enable interrupt
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        ; Enable SPI
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        ; MSB first
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        ; Master mode
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        ; Polarity/phase: Mode 0 (sample on rising edge)
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        ; Clock rate 1/16
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    42
        ldi         r16, (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR) | (0 << CPOL) | (0 << CPHA) | (0b01 << SPR0)
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        out         SPCR, r16
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    ; Flags
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        clr         r0
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        out         SPI_FLAGS, r0
30
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    48
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    49
    ; Start update timer
31
dfb246ecaf23 8x8 LED Matrix driver
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    50
    ; XXX: also used for ADC
dfb246ecaf23 8x8 LED Matrix driver
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    51
;        ldi         r16, 64         ; every 64k cycles
dfb246ecaf23 8x8 LED Matrix driver
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    52
;        rcall       Timer0_Start
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    ; Done
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        ret
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
30
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    57
;; Triggered by Timer0, updates the spi_bufs
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    58
;;  Run from timer interrupt context
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    59
SPI_Update:
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    60
        ; skip if updating
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    61
        sbic        SPI_FLAGS, SPI_BUSY
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    62
        ret
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    63
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    64
    ;; Continue
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    65
    ; XXX: blocks too much?
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    66
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    67
;; Send/Recv from/to SPI buffers
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
SPI_SendRecv:
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
    ; Flag
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
        sbi         SPI_FLAGS, SPI_BUSY
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    72
    ; Start of packet
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        cbi         SPI_PORT, SPI_SS
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    75
    ; Init buffers
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    76
        ldi         r16, SPI_BUFLEN
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    77
        
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    78
        ; send/recv in reverse order
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    79
        ldi         XL, low(spi_inbuf + SPI_BUFLEN)
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    80
        ldi         XH, high(spi_inbuf + SPI_BUFLEN)
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    81
        ldi         YL, low(spi_outbuf + SPI_BUFLEN)
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    82
        ldi         YH, high(spi_outbuf + SPI_BUFLEN)
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    84
    ; Write
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    85
spi_sr_next:
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    86
        ; load+send tail byte
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    87
        ld          r1, -Y
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    88
        out         SPDR, r1
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    89
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    90
    ; Wait
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    91
spi_sr_wait:        
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    92
        in          r1, SPSR
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    93
        sbrs        r1, SPIF
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    94
        rjmp        spi_sr_wait
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    95
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    96
    ; Read
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    97
        ; read+store tail byte
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    98
        in          r1, SPDR
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    99
        st          -X, r1
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   100
 
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   101
    ; Done?
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   102
        dec         r16
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   103
        brne        spi_sr_next         ; if nonzero
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   104
    
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   105
    ; End of packet
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   106
        sbi         SPI_PORT, SPI_SS
30
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   107
        
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   108
        cbi         SPI_FLAGS, SPI_BUSY
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   109
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
    ; Done
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
        ret
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
;; Service SPI interrupt
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
SPI_Interrupt:
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   115
    ; XXX: disabled
17
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
        reti
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
a7c668003a19 split led7seg.s into .inc modules, and update Makefile to use .s -> .hex, and above .inc's for led7seg
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118