OpenCores
URL https://opencores.org/ocsvn/spiflashcontroller/spiflashcontroller/trunk

Subversion Repositories spiflashcontroller

[/] [spiflashcontroller/] [trunk/] [doc/] [spi-registers.txt] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 johannesha
Description of the internal SPI Flash controller:
2
=================================================
3
 
4
The SPI Flash controller occupies the following addresses in the
5
DIY calculator address space:
6
  $F038 : Tx data register (write)
7
  $F018 : Rx data register (read)
8
  $F039 : command register (write)
9
  $F019 : status register  (read)
10 4 johannesha
  $F03A : address low register (write)
11
  $F03B : address mid register (write)
12 5 johannesha
  $F03C : address high register (write)
13 2 johannesha
 
14
 
15
These are the bits of the status register:
16 4 johannesha
  busy:          $01
17
  tx empty:      $02
18
  rx ready:      $04
19
  wait for data: $08
20 2 johannesha
Other bits read as '0'.
21
 
22
 
23
How to communicate with the SPI flash:
24
--------------------------------------
25
 
26
The SPI flash (ST M25P80 chip) understands the following commands:
27
  WREN ($06) .. write enable
28
  WRDI ($04) .. write disable
29
  RDSR ($05) .. read status register
30
  WRSR ($01) .. write status register
31
  RD   ($03) .. read data
32
  F_RD ($0B) .. fast read data
33
  PP   ($02) .. page program
34
  SE   ($D8) .. sector erase
35
  BE   ($C7) .. bulk erase
36
  DP   ($B9) .. deep power down
37
  RES  ($AB) .. read signature
38
 
39
Additionally there is a pseudo-command defined for use with the SPI flash
40
controller:
41
  NOP  ($FF) .. no cmd to execute/end current command
42
 
43
 
44
Command classification:
45
-----------------------
46
 
47
  Write Enable (WREN)              transmit 1 byte ... cmd (0x06)
48
  Write Disable (WRDI)                                     (0x04)
49
  Bulk Erase (BE)                                          (0xC7)
50
  Deep Power Down (DP)                                     (0xB9)
51
 
52
  Write Status reg (WRSR)          transmit 1 byte ... cmd (0x01)
53
                                            1 byte ... SR contents
54
 
55
  Sector Erase (SE)                transmit 1 byte ... cmd (0xD8)
56
                                            3 bytes .. address
57
 
58
  Page Program (PP)                transmit 1 byte ... cmd (0x02)
59
                                            3 bytes .. address
60
                                            1-256 bytes .. data
61
 
62
  Read Status reg (RDSR)           transmit 1 byte ... cmd (0x05)
63
                                   receive  1 byte ... SR contents
64
 
65
  Read Signature (RES)             transmit 1 byte ... cmd (0xAB)
66
                                            3 bytes .. dummy
67
                                   receive  1 byte ... the signature (0x13)
68
 
69
  Read Data (RD)                   transmit 1 byte ... cmd (0x03)
70
                                            3 bytes .. address
71
                                   receive  n bytes .. data
72
 
73
  Fast Read Data (F_RD)            transmit 1 byte ... cmd (0x0B)
74
                                            3 bytes .. address
75
                                            1 byte ... dummy
76
                                   receive  n bytes .. data
77
 
78
 
79
A command sequence depends on the command to be executed. For the simple
80
commands (with no parameters) just the command is written to the SPI Flash
81
controller command register (address $F039). The SPI controller shifts the
82
cmd byte into the SPI flash. The more complex commands (with parameters)
83
require that the parameters (e.g. address) are written first. The action
84
of writing the command register triggers the transmission of the command
85
plus all necessary parameter bytes to the SPI flash chip. With commands
86
that receive a response (read commands) you have to wait for the response
87
to arrive and then read the SPI flash controller data register ($F018).
88
 
89
 
90
Examples
91
--------
92
 
93
1) issue the "Write Enable" command:
94
           LDA     SPI_WREN
95
           STA     [SPI_CMD]
96
 
97
2) issue the "Read Signature" command:
98
           LDA     SPI_RES
99
           STA     [SPI_CMD]
100
; wait for the response to arrive
101
WAIT:      LDA     [SPI_STAT]
102
           AND     SPI_RXR
103
           JZ      [WAIT]
104
; read the response
105
           LDA     [SPI_RX]
106
 
107
3) issue the "Sector Erase" command:
108 4 johannesha
; symbolic constant
109
SECTOR     .EQU    $0F       ; sector number
110
             .
111
             .
112
             .
113
           LDA     SECTOR
114
           STA     [SPI_AHI]
115 2 johannesha
           LDA     0
116 4 johannesha
           STA     [SPI_AMID]
117 2 johannesha
           STA     [SPI_ALO]
118
           LDA     SPI_SE
119
           STA     [SPI_CMD]
120
 
121
4) issue the "Read Data" command:
122
; symbolic constant
123
BUFSIZE:   .EQU    100
124
             .
125
             .
126
             .
127
 
128
; buffer reservation, in RAM
129
BUF:       .BLOCK  BUFSIZE
130
MAXBYTES:  .WORD
131
NUMBYTES:  .WORD
132
             .
133
             .
134
             .
135
 
136
; code:
137
           BLDX    BUFSIZE
138
           BSTX    [MAXBYTES]
139 4 johannesha
           LDA     SECTOR
140
           STA     [SPI_AHI]
141 2 johannesha
           LDA     BUF
142 4 johannesha
           STA     [SPI_AMID]
143 2 johannesha
           LDA     BUF+1
144
           STA     [SPI_ALO]
145
           LDA     SPI_RD
146
           STA     [SPI_CMD]
147
           BLDX    0
148
           BSTX    [NUMBYTES]
149
; now wait for the data to arrive
150
LOOP:      LDA     [SPI_STAT]
151
           AND     SPI_RXR
152
           JZ      [LOOP]
153
; data byte is ready
154
           LDA     [SPI_RX]
155
           STA     [BUF, X]
156
           INCX
157
; check for max number of bytes reached, high byte first
158
           BSTX    [NUMBYTES]
159
           LDA     [MAXBYTES]
160
           CMPA    [NUMBYTES]
161
           JC      [LOOP]         ; not yet reached
162
           JNZ     [DONE]
163
; high bytes are equal => compare low bytes as well
164
           LDA     [MAXBYTES+1]
165
           CMP     [NUMBYTES+1]
166
           JC      [LOOP]         ; not yet reached
167
; we are done now, transfer of desired number of bytes is completed
168
DONE:      LDA     SPI_NOP
169
           STA     [SPI_CMD]      ; write the pseudo "NOP" cmd to reset
170
                                  ; the SPI controller to idle state
171
 
172
Remarks:
173
--------
174
The flash utilizes a 3 byte address (using 20 bits for the 8Mbit
175
M25P80 chip). The highest byte specifies the sector on which the PP, SE,
176 4 johannesha
RD, F_RD commands operate. In the DIY Calculator we use only the topmost
177
sector (no. 0x0F).
178 2 johannesha
 
179
The PP, RD, and F_RD commands are special in that that the number of bytes
180
to be transferred is not known in advance. Therefore the dummy "NOP" command
181
must be issued to the SPI Flash controller after all bytes are transferred
182
to terminate the active command and return the SPI Flash controller to its
183
idle state.
184
 
185
For commands that expect a response (read commands) it is necessary to poll
186
the SPI controller status register (address $F019) to see when the data has
187
arrived.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.