Mercurial > public > mk2
annotate code_part1/OSTC_code_asm_part1/i2c_eeprom.asm @ 85:8b813f67220f
renaming "Max Depth" to "max."
author | heinrichsweikamp |
---|---|
date | Wed, 08 Dec 2010 08:37:03 +0100 |
parents | 3e351e25f5d1 |
children | a1960295433b |
rev | line source |
---|---|
0 | 1 |
2 ; OSTC - diving computer code | |
3 ; Copyright (C) 2008 HeinrichsWeikamp GbR | |
4 ; This program is free software: you can redistribute it and/or modify | |
5 ; it under the terms of the GNU General Public License as published by | |
6 ; the Free Software Foundation, either version 3 of the License, or | |
7 ; (at your option) any later version. | |
8 ; This program is distributed in the hope that it will be useful, | |
9 ; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 ; GNU General Public License for more details. | |
12 ; You should have received a copy of the GNU General Public License | |
13 ; along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 ; provides routines for external EEPROM via I2C | |
15 ; written by: Matthias Heinrichs, info@heinrichsweikamp.com | |
16 ; written: 10/30/05 | |
17 ; last updated: 08/21/06 | |
18 ; known bugs: | |
19 ; ToDo: use 2nd 32KB from external EEPROM for something | |
20 | |
21 incf_eeprom_address macro ext_ee_temp1 ; Will increase eeprom_address:2 with the 8Bit value "ext_ee_temp1" and takes | |
22 movlw ext_ee_temp1 ; care of bank switching at 0x8000 | |
23 call incf_eeprom_address0 | |
24 endm | |
25 | |
26 incf_eeprom_address0: | |
27 movwf ext_ee_temp1 | |
28 incf_eeprom_address1: | |
29 movlw d'1' ; increase address | |
30 addwf eeprom_address+0,F | |
31 movlw d'0' | |
32 addwfc eeprom_address+1,F | |
33 btfss eeprom_address+1,7 ; at address 8000? | |
34 bra incf_eeprom_address2 ; No, continue | |
35 | |
36 ; Yes, clear eeprom_address:2 | |
37 clrf eeprom_address+0 ; Clear eeprom address | |
38 clrf eeprom_address+1 | |
39 | |
40 incf_eeprom_address2: | |
41 decfsz ext_ee_temp1,F ; All done? | |
42 bra incf_eeprom_address1 ; No, continue | |
43 return ; Done. | |
44 | |
45 decf_eeprom_address macro ext_ee_temp1 ; Will decrease eeprom_address:2 with the 8Bit value "ext_ee_temp1" and takes | |
46 movlw ext_ee_temp1 ; care of bank switching at 0x8000 | |
47 call decf_eeprom_address0 | |
48 endm | |
49 | |
50 decf_eeprom_address0: | |
51 movwf ext_ee_temp1 | |
52 decf_eeprom_address1: | |
53 movlw d'1' ; decrease address | |
54 subwf eeprom_address+0,F | |
55 movlw d'0' | |
56 subwfb eeprom_address+1,F | |
57 | |
58 btfss eeprom_address+1,7 ; at address 8000? | |
59 bra decf_eeprom_address2 ; No, continue | |
60 | |
61 movlw b'01111111' ; yes, reset highbyte | |
62 movwf eeprom_address+1 | |
63 | |
64 decf_eeprom_address2: | |
65 decfsz ext_ee_temp1,F ; All done? | |
66 bra decf_eeprom_address1 ; No, continue | |
67 return ; Done. | |
68 | |
69 | |
70 write_external_eeprom: ; data in WREG | |
71 ; increase address eeprom_address+0:eeprom_address+1 after write | |
72 ; with banking after 7FFF | |
81
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
73 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
74 ; When Simulating with MPLabSIM, there is no way to emulate external EEPROM... |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
75 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
76 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
77 |
0 | 78 rcall I2CWRITE ; writes WREG into EEPROM@eeprom_address |
79 movlw d'1' ; increase address | |
80 addwf eeprom_address+0,F | |
81 movlw d'0' | |
82 addwfc eeprom_address+1,F | |
83 bcf eeprom_overflow | |
84 btfss eeprom_address+1,7 ; at address 8000? | |
85 return ; no, return | |
86 | |
87 clrf eeprom_address+0 ; Clear eeprom address | |
88 clrf eeprom_address+1 | |
89 bsf eeprom_overflow ; and set overflow bit | |
90 return | |
83
3e351e25f5d1
adding anti-aliased fonts frame and merging some patches from Jeando
heinrichsweikamp
parents:
81
diff
changeset
|
91 |
0 | 92 write_external_eeprom_block: ; Writes a block of 64Byte (one page in external EEPROM without stop condition |
81
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
93 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
94 ; When Simulating with MPLabSIM, there is no way to emulate external EEPROM... |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
95 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
96 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
97 |
0 | 98 btfsc eeprom_blockwrite ; Blockwrite continue? |
99 rcall I2CWRITE_BLOCK2 | |
100 btfss eeprom_blockwrite ; Blockwrite start? | |
101 rcall I2CWRITE_BLOCK | |
102 bsf eeprom_blockwrite ; After the start, do blockwriting for the next 63Bytes! | |
103 | |
104 movlw d'0' ; increase address | |
105 incf eeprom_address+0,F | |
106 addwfc eeprom_address+1,F | |
107 bcf eeprom_overflow | |
108 | |
109 btfss eeprom_address+1,7 ; at address 8000 | |
110 return ; no, return | |
111 | |
112 clrf eeprom_address+0 ; Clear eeprom address | |
113 clrf eeprom_address+1 | |
114 bsf eeprom_overflow ; and set overflow bit | |
115 return | |
116 I2CWRITE_BLOCK: | |
117 movwf ext_ee_temp1 ; Data byte in WREG | |
118 bsf SSPCON2,SEN ; Start condition | |
119 rcall WaitMSSP | |
120 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
121 movwf SSPBUF ; control byte | |
122 rcall WaitMSSP | |
123 rcall I2C_WaitforACK | |
124 movff eeprom_address+1,SSPBUF ; High Address byte | |
125 rcall WaitMSSP | |
126 rcall I2C_WaitforACK | |
127 movff eeprom_address+0,SSPBUF ; Low Address byte | |
128 rcall WaitMSSP | |
129 rcall I2C_WaitforACK | |
130 I2CWRITE_BLOCK2: | |
131 movff ext_ee_temp1, SSPBUF ; Data Byte | |
132 rcall WaitMSSP | |
133 rcall I2C_WaitforACK | |
134 return | |
135 | |
136 | |
137 get_free_EEPROM_location: ; Searches 0xFD, 0xFD, 0xFE and sets Pointer to 0xFE | |
81
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
138 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
139 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
140 ; In testing mode, find 0x100 (internal EEPROM) as the first free location... |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
141 clrf eeprom_address+0 ; Not found in entire EEPROM, set to address 0 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
142 movlw 0x1 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
143 movwf eeprom_address+1 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
144 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
145 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
146 |
0 | 147 clrf ext_ee_temp1 ; low address counter |
148 clrf ext_ee_temp2 ; high address counter | |
149 bcf second_FD ; clear flags | |
150 bcf first_FD | |
151 get_free_EEPROM_location3: | |
152 bsf SSPCON2, PEN ; Stop condition | |
153 rcall WaitMSSP | |
154 bsf SSPCON2,SEN ; Start condition | |
155 rcall WaitMSSP | |
156 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
157 movwf SSPBUF ; control byte | |
158 rcall WaitMSSP | |
159 btfsc SSPCON2,ACKSTAT | |
160 bra get_free_EEPROM_location3 ; EEPROM NOT acknowledged, retry! | |
161 | |
162 movff ext_ee_temp2,SSPBUF ; High Address byte | |
163 rcall WaitMSSP | |
164 rcall I2C_WaitforACK | |
165 movff ext_ee_temp1,SSPBUF ; Low Address byte | |
166 rcall WaitMSSP | |
167 rcall I2C_WaitforACK | |
168 | |
169 bsf SSPCON2,RSEN ; Start condition | |
170 rcall WaitMSSP | |
171 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ | |
172 movwf SSPBUF ; control byte | |
173 rcall WaitMSSP | |
174 rcall I2C_WaitforACK | |
175 | |
176 get_free_EEPROM_location2: | |
177 bsf SSPCON2, RCEN ; Enable recieve mode | |
178 rcall WaitMSSP | |
179 btfsc first_FD | |
180 bra test_2nd_FD | |
181 bsf first_FD ; found first 0xFD? | |
182 movlw 0xFD | |
183 cpfseq SSPBUF | |
184 bcf first_FD ; No | |
185 bra get_free_EEPROM_location2c | |
186 | |
187 test_2nd_FD: | |
188 btfsc second_FD | |
189 bra test_FE | |
190 bsf second_FD ; found second 0xFD? | |
191 movlw 0xFD | |
192 cpfseq SSPBUF | |
193 bra get_free_EEPROM_location2b ;No, clear both flags | |
194 bra get_free_EEPROM_location2c | |
195 test_FE: | |
196 movlw 0xFE ; found the final 0xFE? | |
197 cpfseq SSPBUF | |
198 bra get_free_EEPROM_location2b ;No, clear both flags | |
199 movff ext_ee_temp1,eeprom_address+0 ;Yes, copy ext_ee_temp1->eeprom_address+0 and | |
200 movff ext_ee_temp2,eeprom_address+1 ;ext_ee_temp2->eeprom_address+1 | |
201 bra get_free_EEPROM_location4 ; Done. | |
202 | |
203 get_free_EEPROM_location2b: | |
204 bcf second_FD ; clear both flags! | |
205 bcf first_FD | |
206 get_free_EEPROM_location2c: | |
207 movlw d'1' ; and increase search address | |
208 addwf ext_ee_temp1,F | |
209 movlw d'0' | |
210 addwfc ext_ee_temp2,F | |
211 | |
42 | 212 btfsc ext_ee_temp2,7 ; 0x8000 reached? |
0 | 213 bra get_free_EEPROM_location3b ; yes |
214 | |
215 bsf SSPCON2, ACKEN ; no, send Ack | |
216 rcall WaitMSSP | |
217 bra get_free_EEPROM_location2 ; and continue search | |
218 get_free_EEPROM_location3b: | |
219 clrf eeprom_address+0 ; Not found in entire EEPROM, set to address 0 | |
220 clrf eeprom_address+1 | |
221 get_free_EEPROM_location4: | |
222 bsf SSPCON2, PEN ; Stop | |
223 rcall WaitMSSP | |
224 | |
225 bcf second_FD ; clear flags | |
226 bcf first_FD | |
227 return ; return | |
228 | |
229 | |
230 I2CREAD: | |
231 bsf SSPCON2, PEN ; Stop | |
232 rcall WaitMSSP | |
233 bsf SSPCON2,SEN ; Start condition | |
234 rcall WaitMSSP | |
235 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
236 movwf SSPBUF ; control byte | |
237 rcall WaitMSSP | |
238 btfsc SSPCON2,ACKSTAT | |
239 bra I2CREAD ; EEPROM NOT acknowledged, retry! | |
240 movff eeprom_address+1,SSPBUF ; High Address byte | |
241 rcall WaitMSSP | |
242 rcall I2C_WaitforACK | |
243 movff eeprom_address+0,SSPBUF ; Low Address byte | |
244 rcall WaitMSSP | |
245 rcall I2C_WaitforACK | |
246 | |
247 bsf SSPCON2,RSEN ; Start condition | |
248 rcall WaitMSSP | |
249 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ | |
250 movwf SSPBUF ; control byte | |
251 rcall WaitMSSP | |
252 rcall I2C_WaitforACK | |
253 | |
254 bsf SSPCON2, RCEN ; Enable recieve mode | |
255 rcall WaitMSSP | |
256 movf SSPBUF,W ; copy read byte into WREG | |
257 bsf SSPCON2, PEN ; Stop | |
258 rcall WaitMSSP | |
259 return | |
260 | |
261 I2CREAD2: ; same as I2CREAD but with automatic address increase | |
262 rcall I2CREAD | |
263 movlw d'1' | |
264 addwf eeprom_address+0,F | |
265 movlw d'0' | |
266 addwfc eeprom_address+1,F | |
267 bcf eeprom_overflow | |
268 btfss eeprom_address+1,7 ; at 0x8000? | |
269 return ; no, return | |
270 | |
271 clrf eeprom_address+0 ; Yes, clear address | |
272 clrf eeprom_address+1 | |
273 bsf eeprom_overflow ; and set overflow bit | |
274 return | |
275 | |
276 I2CWRITE: | |
277 movwf ext_ee_temp1 ; Data byte | |
278 bsf SSPCON2,SEN ; Start condition | |
279 rcall WaitMSSP | |
280 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
281 movwf SSPBUF ; control byte | |
282 rcall WaitMSSP | |
283 rcall I2C_WaitforACK | |
284 movff eeprom_address+1,SSPBUF ; High Address byte | |
285 rcall WaitMSSP | |
286 rcall I2C_WaitforACK | |
287 movff eeprom_address+0,SSPBUF ; Low Address byte | |
288 rcall WaitMSSP | |
289 rcall I2C_WaitforACK | |
290 movff ext_ee_temp1, SSPBUF ; Data Byte | |
291 rcall WaitMSSP | |
292 rcall I2C_WaitforACK | |
293 bsf SSPCON2,PEN ; Stop condition | |
294 rcall WaitMSSP | |
295 WAITMS d'6' ; Write delay | |
296 return | |
297 | |
298 I2C_WaitforACK: | |
299 btfsc SSPCON2,ACKSTAT ; checks for ACK bit from slave | |
300 rcall I2CFail | |
301 return | |
302 | |
303 I2CFail: | |
304 ostc_debug 'M' ; Sends debug-information to screen if debugmode active | |
21 | 305 bsf LED_red |
0 | 306 rcall I2CReset ; I2C Reset |
307 bcf PIR1,SSPIF | |
308 clrf i2c_temp | |
309 return | |
310 | |
311 WaitMSSP: | |
312 decfsz i2c_temp,F ; check for timeout during I2C action | |
313 bra WaitMSSP2 | |
314 bra I2CFail ; timeout occured | |
315 WaitMSSP2: | |
316 btfss PIR1,SSPIF | |
317 bra WaitMSSP | |
318 clrf i2c_temp | |
319 bcf PIR1,SSPIF | |
320 return | |
321 | |
322 I2CReset: ; Something went wrong (Slave holds SDA low?) | |
323 clrf SSPCON1 ; wake-up slave and reset entire module | |
324 ostc_debug 'N' ; Sends debug-information to screen if debugmode active | |
325 clrf SSPCON2 | |
326 clrf SSPSTAT | |
327 bcf TRISC,3 ; SCL OUTPUT | |
328 bsf TRISC,4 ; SDA Input | |
329 bcf PORTC,3 | |
330 movlw d'9' | |
331 movwf i2c_temp ; clock-out 9 clock cycles manually | |
332 I2CReset_1: | |
333 bsf PORTC,3 ; SCL=1 | |
334 nop | |
335 btfsc PORTC,4 ; SDA=1? | |
336 bra I2CReset_2 ; =1, SDA has been released from slave | |
337 bcf PORTC,3 ; SCL=0 | |
338 bcf PORTC,3 | |
339 decfsz i2c_temp,F | |
340 bra I2CReset_1 ; check for nine clock cycles | |
341 I2CReset_2: | |
342 bsf TRISC,3 ; SCL Input | |
343 clrf SSPCON1 ; set I²C Mode | |
344 WAITMS d'10' ; Reset-Timeout for I2C devices | |
345 movlw b'00000000' | |
346 movwf SSPSTAT | |
347 movlw b'00101000' | |
348 movwf SSPCON1 | |
349 movlw b'00000000' | |
350 movwf SSPCON2 | |
351 movlw d'8' ; 400kHz I2C clock @ 16MHz Fcy | |
352 movwf SSPADD | |
21 | 353 bcf LED_red |
0 | 354 ostc_debug 'O' ; Sends debug-information to screen if debugmode active |
355 return | |
53 | 356 |
357 ;I2C_TX: | |
358 ; movwf i2c_temp2 ; Data byte | |
359 ; bsf SSPCON2,SEN ; Start condition | |
360 ; rcall WaitMSSP | |
361 ; movlw b'10010000' ; Bit0=0: WRITE, Bit0=1: READ | |
362 ; movwf SSPBUF ; control byte | |
363 ; rcall WaitMSSP | |
364 ; rcall I2C_WaitforACK | |
365 ; movff i2c_temp2, SSPBUF ; Data Byte | |
366 ; rcall WaitMSSP | |
367 ; rcall I2C_WaitforACK | |
368 ; bsf SSPCON2,PEN ; Stop condition | |
369 ; rcall WaitMSSP | |
370 ; return | |
371 ;I2C_RX: | |
372 ; bcf PIR1,SSPIF | |
373 ; bsf SSPCON2,SEN ; Start condition | |
374 ; rcall WaitMSSP | |
375 ; movlw b'10010001' ; Bit0=0: WRITE, Bit0=1: READ | |
376 ; movwf SSPBUF ; control byte | |
377 ; rcall WaitMSSP | |
378 ; rcall I2C_WaitforACK | |
379 ; bsf SSPCON2, RCEN ; Enable recieve mode | |
380 ; rcall WaitMSSP | |
381 ; movff SSPBUF,i2c_temp2 ; Data Byte | |
382 ; bsf SSPCON2,ACKEN ; Master acknowlegde | |
383 ; rcall WaitMSSP | |
384 ; bsf SSPCON2,PEN ; Stop condition | |
385 ; rcall WaitMSSP | |
386 ; return |