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

Subversion Repositories spiflashcontroller

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

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

powered by: WebSVN 2.1.0

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