spi.inc
author Tero Marttila <terom@fixme.fi>
Sun, 09 May 2010 23:27:12 +0300
changeset 30 5226e512755c
parent 26 db2ec641c955
child 31 dfb246ecaf23
permissions -rw-r--r--
Use Timer0 for async SPI updates (semi-useful)
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
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    50
        ldi         r16, 64         ; every 64k cycles
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    51
        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
    52
    
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
    ; 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
    54
        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
    55
30
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    56
;; 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
    57
;;  Run from timer interrupt context
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    58
SPI_Update:
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    59
        ; skip if updating
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    60
        sbic        SPI_FLAGS, SPI_BUSY
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    61
        ret
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    62
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    63
    ;; Continue
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    64
    ; XXX: blocks too much?
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    65
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    66
;; 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
    67
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
    68
    ; 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
    69
        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
    70
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    71
    ; 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
    72
        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
    73
    
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    74
    ; Init buffers
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    75
        ldi         r16, SPI_BUFLEN
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    76
        
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    77
        ; send/recv in reverse order
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    78
        ldi         XL, low(spi_inbuf + SPI_BUFLEN)
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    79
        ldi         XH, high(spi_inbuf + SPI_BUFLEN)
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    80
        ldi         YL, low(spi_outbuf + SPI_BUFLEN)
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    81
        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
    82
    
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    83
    ; Write
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    84
spi_sr_next:
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    85
        ; load+send tail byte
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    86
        ld          r1, -Y
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    87
        out         SPDR, r1
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    88
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    89
    ; Wait
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    90
spi_sr_wait:        
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    91
        in          r1, SPSR
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    92
        sbrs        r1, SPIF
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    93
        rjmp        spi_sr_wait
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    94
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    95
    ; Read
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    96
        ; read+store tail byte
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    97
        in          r1, SPDR
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
    98
        st          -X, r1
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    99
 
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   100
    ; Done?
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   101
        dec         r16
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   102
        brne        spi_sr_next         ; if nonzero
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   103
    
26
db2ec641c955 refactor code to move buffers to SPI
Tero Marttila <terom@fixme.fi>
parents: 24
diff changeset
   104
    ; End of packet
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   105
        sbi         SPI_PORT, SPI_SS
30
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   106
        
5226e512755c Use Timer0 for async SPI updates (semi-useful)
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
   107
        cbi         SPI_FLAGS, SPI_BUSY
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   108
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
   109
    ; 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
   110
        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
   111
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
;; 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
   113
SPI_Interrupt:
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   114
    ; 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
   115
        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
   116
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