spi.inc
author Tero Marttila <terom@fixme.fi>
Sat, 08 May 2010 20:09:08 +0300
changeset 23 a6afc0eb347f
parent 18 79b25e81721f
child 24 8100038e3d58
permissions -rw-r--r--
synchronous SPI, two-digit led7
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
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
    16
;; 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
    17
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
    18
    ; 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
    19
        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
    20
        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
    21
        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
    22
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
    23
    ; 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
    24
        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
    25
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
    ; Set control mode
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    27
        ; 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
    28
        ; 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
    29
        ; 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
    30
        ; 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
    31
        ; 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
    32
        ; Clock rate 1/16
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    33
        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
    34
        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
    35
    
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
    36
    ; 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
    37
        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
    38
        out         SPI_FLAGS, 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
    39
    
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
    ; 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
    41
        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
    42
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    43
;; Send/recv one byte
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
    44
;;  Input: r16
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    45
;;  Output: r10
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    46
;;  Note: should not be busy...
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
    47
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
    48
    ; 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
    49
        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
    50
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
    51
    ; Enable slave (low)
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
        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
    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
    ; Write byte (starts SCK)
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
        out         SPDR, 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
    56
    
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    57
    ; Wait for byte to be sent
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    58
spi_sr_wait:
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    59
        in          r1, SPSR
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    60
        sbrs        r1, SPIF
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    61
        rjmp        spi_sr_wait
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    62
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    63
    ; Read
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    64
        in         r10, SPDR
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    65
    
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    66
    ; Drive SS high (end of packet)
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    67
        sbi         SPI_PORT, SPI_SS
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    68
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
    69
    ; 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
    70
        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
    71
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    72
;; Send buffer bytes
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    73
;;  Input: post-buffer byte in X, buffer length in r16
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    74
;;  Output: r10
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    75
;;  Note: should not be busy...
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    76
SPI_SendBuf:
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    77
    ; Flag
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    78
        sbi         SPI_FLAGS, SPI_BUSY
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    79
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    80
    ; Enable slave (low)
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    81
        cbi         SPI_PORT, SPI_SS
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    82
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    83
    ; Write byte from X (starts SCK)
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    84
spi_srb_next:
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    85
        ld          r1, -X
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    86
        out         SPDR, r1
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    87
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    88
    ; Wait for byte to be sent
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    89
spi_srb_wait:        
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    90
        in          r1, SPSR
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    91
        sbrs        r1, SPIF
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    92
        rjmp        spi_srb_wait
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    93
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    94
    ; Read
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    95
        in          r10, SPDR
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    96
 
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    97
    ; Done?
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    98
        dec         r16
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    99
        brne        spi_srb_next
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
    ; Drive SS high (end of packet)
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   102
        sbi         SPI_PORT, SPI_SS
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   103
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
   104
    ; 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
   105
        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
   106
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
   107
;; 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
   108
SPI_Interrupt:
23
a6afc0eb347f synchronous SPI, two-digit led7
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   109
    ; 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
   110
        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
   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