📄 dataflash.lst
字号:
1 .file "dataflash.c"
2 __SREG__ = 0x3f
3 __SP_H__ = 0x3e
4 __SP_L__ = 0x3d
5 __CCP__ = 0x34
6 __tmp_reg__ = 0
7 __zero_reg__ = 1
8 .global __do_copy_data
9 .global __do_clear_bss
11 .text
12 .Ltext0:
91 .global dataflash_init
93 dataflash_init:
1:io/dataflash.c **** /*
2:io/dataflash.c **** ,-----------------------------------------------------------------------------------------.
3:io/dataflash.c **** | io/dataflash
4:io/dataflash.c **** |-----------------------------------------------------------------------------------------
5:io/dataflash.c **** | this file implements the driver for a Atmel Dataflash (AT45DB041B)
6:io/dataflash.c **** | -
7:io/dataflash.c **** |
8:io/dataflash.c **** | Author : {{removed according to contest rules}}
9:io/dataflash.c **** | -> circuitcellar.com avr design contest 2006
10:io/dataflash.c **** | -> Entry #AT2616
11:io/dataflash.c **** |
12:io/dataflash.c **** |-----------------------------------------------------------------------------------------
13:io/dataflash.c **** | License:
14:io/dataflash.c **** | This program is free software; you can redistribute it and/or modify it under
15:io/dataflash.c **** | the terms of the GNU General Public License as published by the Free Software
16:io/dataflash.c **** | Foundation; either version 2 of the License, or (at your option) any later
17:io/dataflash.c **** | version.
18:io/dataflash.c **** | This program is distributed in the hope that it will be useful, but
19:io/dataflash.c **** |
20:io/dataflash.c **** | WITHOUT ANY WARRANTY;
21:io/dataflash.c **** |
22:io/dataflash.c **** | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
23:io/dataflash.c **** | PURPOSE. See the GNU General Public License for more details.
24:io/dataflash.c **** |
25:io/dataflash.c **** | You should have received a copy of the GNU General Public License along with
26:io/dataflash.c **** | this program; if not, write to the Free Software Foundation, Inc., 51
27:io/dataflash.c **** | Franklin St, Fifth Floor, Boston, MA 02110, USA
28:io/dataflash.c **** |
29:io/dataflash.c **** | http://www.gnu.de/gpl-ger.html
30:io/dataflash.c **** `-----------------------------------------------------------------------------------------*/
31:io/dataflash.c **** #include "dataflash.h"
32:io/dataflash.c **** #include "main.h"
33:io/dataflash.c **** #include <avr/delay.h>
34:io/dataflash.c ****
35:io/dataflash.c **** //ACTIVATE DEBUG by editing this file:
36:io/dataflash.c **** #include "debug.h"
37:io/dataflash.c ****
38:io/dataflash.c ****
39:io/dataflash.c **** //initialise dataflash
40:io/dataflash.c **** // -> SPI must be initialised externally!
41:io/dataflash.c **** void dataflash_init(void){
94 bn 68,0,43,.LM1-.LFBB1
95 .LM1:
96 lds r24,97
97 ori r24,lo8(-64)
98 sts 97,r24
42:io/dataflash.c **** |= (1<<DATAFLASH_PIN_CS) | (1<<DATAFLASH_PIN_RST);
43:io/dataflash.c ****
99 bn 68,0,72,.LM2-.LFBB1
100 .LM2:
101 0000 8091 6100 lds r24,98
102 0004 806C andi r24,lo8(127)
103 0006 8093 6100 sts 98,r24
44:io/dataflash.c **** if DATAFLASH_DEBUG
45:io/dataflash.c **** softuart_puts_progmem("DF : dataflash init: ");
46:io/dataflash.c **** #endif
47:io/dataflash.c ****
48:io/dataflash.c **** //SPI init (is done in enc28j60...
49:io/dataflash.c **** #if 0
50:io/dataflash.c **** // initialize I/O
51:io/dataflash.c **** //SPI init
52:io/dataflash.c **** // initialize I/O
53:io/dataflash.c **** PORTB |= (1<<7); //sck = hi
54:io/dataflash.c ****
55:io/dataflash.c **** //spi = output
56:io/dataflash.c **** //mega8//DDRB |= (1<<2)|(1<<3)|(1<<5); //SS,MOSI,SCK = OUT
57:io/dataflash.c **** DDRB |= (1<<4)|(1<<5)|(1<<7); //SS,MOSI,SCK = OUT
58:io/dataflash.c **** DDRB &= ~(1<<6); //MISO = IN
59:io/dataflash.c ****
60:io/dataflash.c **** //SPI init:
61:io/dataflash.c **** // - master mode
62:io/dataflash.c **** // - positive clock phase
63:io/dataflash.c **** // - msb first
64:io/dataflash.c **** // - maximum SPI speed (fosc/2)
65:io/dataflash.c **** // - enable spi
66:io/dataflash.c **** SPCR = (0<<CPOL)|(1<<MSTR)|(0<<DORD)|(0<<SPR1)|(0<<SPR0)|(1<<SPE);
67:io/dataflash.c **** SPSR = (1<<SPI2X);
68:io/dataflash.c **** #endif
69:io/dataflash.c ****
70:io/dataflash.c **** //reset dataflash:
71:io/dataflash.c **** DATAFLASH_RESET_LO();
72:io/dataflash.c **** DATAFLASH_CS_LO();
104 bn 68,0,73,.LM3-.LFBB1
105 .LM3:
106 000a 8091 6200 lds r24,98
107 000e 8F77 andi r24,lo8(-65)
108 0010 8093 6200 sts 98,r24
73:io/dataflash.c **** or(unsigned char i=0; i<20; i++)
109 r24,lo8(0)
110 .L3:
111 0014 8091 6200 .LBB176:
112 0018 8F7B .LBB177:
113 001a 8093 6200 .LBB178:
115 .LM4:
116 out 47-32,__zero_reg__
117 .L2:
74:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
75:io/dataflash.c ****
76:io/dataflash.c **** _delay_us(20); //min 10us
77:io/dataflash.c **** DATAFLASH_CS_HI();
78:io/dataflash.c **** _delay_us(1); //min 200ns
79:io/dataflash.c **** DATAFLASH_RESET_HI();
80:io/dataflash.c ****
81:io/dataflash.c **** //dataflash is now in SPI Mode 3
82:io/dataflash.c **** #if DATAFLASH_DEBUG
83:io/dataflash.c **** softuart_puts_progmem("done.");
84:io/dataflash.c **** softuart_putnewline();
85:io/dataflash.c **** #endif
86:io/dataflash.c **** }
87:io/dataflash.c ****
88:io/dataflash.c **** void dataflash_dump(){
89:io/dataflash.c **** for (int a=0; a<DATAFLASH_MAX_PAGE; a++){
90:io/dataflash.c **** dataflash_copy_page_to_buffer(a, 0);
91:io/dataflash.c **** for(int b=0; b<(256+8); b++){
92:io/dataflash.c **** softuart_putc(dataflash_read_buffer(b,0));
93:io/dataflash.c **** }
94:io/dataflash.c **** }
95:io/dataflash.c **** }
96:io/dataflash.c ****
97:io/dataflash.c ****
98:io/dataflash.c **** //copy given buffer to flash page
99:io/dataflash.c **** void dataflash_copy_buffer_to_page(unsigned int page, unsigned char buffer){
100:io/dataflash.c **** //wait for device ready
101:io/dataflash.c **** dataflash_busy_wait();
102:io/dataflash.c ****
103:io/dataflash.c **** //select device again:
104:io/dataflash.c **** DATAFLASH_CS_LO();
105:io/dataflash.c ****
106:io/dataflash.c **** //send buffer select cmd:
107:io/dataflash.c **** if (buffer == 0)
108:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_BUFFER0_TO_MEM_WE);
109:io/dataflash.c **** else
110:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_BUFFER1_TO_MEM_WE);
111:io/dataflash.c ****
112:io/dataflash.c **** //send 0000 0ppp pppp pppp
113:io/dataflash.c **** dataflash_spi_writeread_byte((page>>7)&0x0F);
114:io/dataflash.c **** dataflash_spi_writeread_byte((page<<1)&0xFE);
115:io/dataflash.c ****
116:io/dataflash.c **** //send 8 dont care bits:
117:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
118:io/dataflash.c ****
119:io/dataflash.c **** DATAFLASH_CS_HI();
120:io/dataflash.c **** }
121:io/dataflash.c ****
122:io/dataflash.c **** //write len bytes to given page buffer starting at <byte>
123:io/dataflash.c **** void dataflash_write_n_to_page_buffer(unsigned int byte, unsigned char buffer, unsigned char *data,
124:io/dataflash.c **** //wait for device ready
125:io/dataflash.c **** dataflash_busy_wait();
126:io/dataflash.c ****
127:io/dataflash.c **** //select device again:
128:io/dataflash.c **** DATAFLASH_CS_LO();
129:io/dataflash.c ****
130:io/dataflash.c **** //send buffer select cmd:
131:io/dataflash.c **** if (buffer == 0)
132:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_WRITE_BUFFER0);
133:io/dataflash.c **** else
134:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_WRITE_BUFFER1);
135:io/dataflash.c ****
136:io/dataflash.c **** //15 dont care + 9 adress bits:
137:io/dataflash.c ****
138:io/dataflash.c **** //send 8 dont care bits:
139:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
140:io/dataflash.c ****
141:io/dataflash.c **** //send 0000 000b bbbb bbbb
142:io/dataflash.c **** dataflash_spi_writeread_byte((byte>>8)&0x01);
143:io/dataflash.c **** dataflash_spi_writeread_byte((byte)&0xFF);
144:io/dataflash.c ****
145:io/dataflash.c **** //send data
146:io/dataflash.c **** while(len>0){
147:io/dataflash.c **** dataflash_spi_writeread_byte((*data));
148:io/dataflash.c **** *data++;
149:io/dataflash.c **** len--;
150:io/dataflash.c **** }
151:io/dataflash.c ****
152:io/dataflash.c **** //deselect device:
153:io/dataflash.c **** DATAFLASH_CS_HI();
154:io/dataflash.c **** }
155:io/dataflash.c ****
156:io/dataflash.c ****
157:io/dataflash.c **** //write one byte to given page buffer
158:io/dataflash.c **** void dataflash_write_to_page_buffer(unsigned int byte, unsigned char buffer, unsigned char data){
159:io/dataflash.c **** //wait for device ready
160:io/dataflash.c **** dataflash_busy_wait();
161:io/dataflash.c ****
162:io/dataflash.c **** //select device again:
163:io/dataflash.c **** DATAFLASH_CS_LO();
164:io/dataflash.c ****
165:io/dataflash.c **** //send buffer select cmd:
166:io/dataflash.c **** if (buffer == 0)
167:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_WRITE_BUFFER0);
168:io/dataflash.c **** else
169:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_WRITE_BUFFER1);
170:io/dataflash.c ****
171:io/dataflash.c **** //15 dont care + 9 adress bits:
172:io/dataflash.c ****
173:io/dataflash.c **** //send 8 dont care bits:
174:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
175:io/dataflash.c ****
176:io/dataflash.c **** //send 0000 000b bbbb bbbb
177:io/dataflash.c **** dataflash_spi_writeread_byte((byte>>8)&0x01);
178:io/dataflash.c **** dataflash_spi_writeread_byte((byte)&0xFF);
179:io/dataflash.c ****
180:io/dataflash.c **** //send data
181:io/dataflash.c **** dataflash_spi_writeread_byte(data);
182:io/dataflash.c ****
183:io/dataflash.c **** //deselect device:
184:io/dataflash.c **** DATAFLASH_CS_HI();
185:io/dataflash.c **** }
186:io/dataflash.c ****
187:io/dataflash.c **** //copy flash page to given buffer
188:io/dataflash.c **** void dataflash_copy_page_to_buffer(unsigned int page, unsigned char buffer){
189:io/dataflash.c **** //wait for device ready
190:io/dataflash.c **** dataflash_busy_wait();
191:io/dataflash.c ****
192:io/dataflash.c **** //select device again:
193:io/dataflash.c **** DATAFLASH_CS_LO();
194:io/dataflash.c ****
195:io/dataflash.c **** //send buffer select cmd:
196:io/dataflash.c **** if (buffer == 0)
197:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_MEM_TO_BUFFER0);
198:io/dataflash.c **** else
199:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_MEM_TO_BUFFER1);
200:io/dataflash.c ****
201:io/dataflash.c **** //send 0000 pppp pppp ppp0
202:io/dataflash.c **** dataflash_spi_writeread_byte((page>>7)&0x0F);
203:io/dataflash.c **** dataflash_spi_writeread_byte((page<<1)&0xFE);
204:io/dataflash.c ****
205:io/dataflash.c **** //send 8 dont care bits:
206:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
207:io/dataflash.c ****
208:io/dataflash.c **** DATAFLASH_CS_HI();
209:io/dataflash.c **** }
210:io/dataflash.c ****
211:io/dataflash.c **** //read one byte from buffer
212:io/dataflash.c **** unsigned char dataflash_read_buffer(unsigned int byte, unsigned char buffer){
213:io/dataflash.c **** unsigned char data;
214:io/dataflash.c ****
215:io/dataflash.c **** //wait for device ready
216:io/dataflash.c **** dataflash_busy_wait();
217:io/dataflash.c ****
218:io/dataflash.c **** //select device again:
219:io/dataflash.c **** DATAFLASH_CS_LO();
220:io/dataflash.c ****
221:io/dataflash.c **** //send buffer select cmd:
222:io/dataflash.c **** if (buffer == 0)
223:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_READ_BUFFER0);
224:io/dataflash.c **** else
225:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_READ_BUFFER1);
226:io/dataflash.c ****
227:io/dataflash.c **** //15 dont care + 9 address + 8 dont care:
228:io/dataflash.c ****
229:io/dataflash.c **** //send address: 0000 0000 0000 000b bbbb bbbb
230:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
231:io/dataflash.c **** dataflash_spi_writeread_byte((byte>>8)&0x01);
232:io/dataflash.c **** dataflash_spi_writeread_byte((byte)&0xFF);
233:io/dataflash.c ****
234:io/dataflash.c **** //send one dummy byte
235:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
236:io/dataflash.c ****
237:io/dataflash.c **** //now read the data
238:io/dataflash.c **** data = dataflash_spi_writeread_byte(0x00);
239:io/dataflash.c ****
240:io/dataflash.c **** //deselect device
241:io/dataflash.c **** DATAFLASH_CS_HI();
242:io/dataflash.c ****
243:io/dataflash.c **** //return data
244:io/dataflash.c **** return (data);
245:io/dataflash.c **** }
246:io/dataflash.c ****
247:io/dataflash.c **** //read n byte from buffer
248:io/dataflash.c **** void dataflash_read_n_to_buffer(unsigned char *buffer, unsigned char selbuf){
249:io/dataflash.c **** //wait for device ready
250:io/dataflash.c **** dataflash_busy_wait();
251:io/dataflash.c ****
252:io/dataflash.c **** //select device again:
253:io/dataflash.c **** DATAFLASH_CS_LO();
254:io/dataflash.c ****
255:io/dataflash.c **** //send buffer select cmd:
256:io/dataflash.c **** if (selbuf == 0)
257:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_READ_BUFFER0);
258:io/dataflash.c **** else
259:io/dataflash.c **** dataflash_spi_writeread_byte(DATAFLASH_READ_BUFFER1);
260:io/dataflash.c ****
261:io/dataflash.c **** //15 dont care + 9 address + 8 dont care:
262:io/dataflash.c ****
263:io/dataflash.c **** //send address: 0000 0000 0000 000b bbbb bbbb
264:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
265:io/dataflash.c **** dataflash_spi_writeread_byte(0);//start with first byte
266:io/dataflash.c **** dataflash_spi_writeread_byte(0);
267:io/dataflash.c ****
268:io/dataflash.c **** //send one dummy byte
269:io/dataflash.c **** dataflash_spi_writeread_byte(0x00);
270:io/dataflash.c ****
271:io/dataflash.c **** //now read the data
272:io/dataflash.c **** *buffer++ = dataflash_spi_writeread_byte(0x00);
273:io/dataflash.c **** for(unsigned char c=0; c<255; c++){
274:io/dataflash.c **** *buffer++ = dataflash_spi_writeread_byte(0x00);
275:io/dataflash.c **** }
276:io/dataflash.c ****
277:io/dataflash.c **** //deselect device
278:io/dataflash.c **** DATAFLASH_CS_HI();
279:io/dataflash.c **** }
280:io/dataflash.c ****
281:io/dataflash.c ****
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -