Mercurial > public > mk2
annotate code_part1/OSTC_code_asm_part1/i2c_eeprom.asm @ 82:bc3092c41335
Cleaning up old wordprocessor to make it OLEDSim compatible.
author | JeanDo |
---|---|
date | Tue, 07 Dec 2010 13:22:17 +0100 |
parents | 31fa973a70fd |
children | 3e351e25f5d1 |
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 | |
91 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
|
92 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
93 ; 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
|
94 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
95 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
96 |
0 | 97 btfsc eeprom_blockwrite ; Blockwrite continue? |
98 rcall I2CWRITE_BLOCK2 | |
99 btfss eeprom_blockwrite ; Blockwrite start? | |
100 rcall I2CWRITE_BLOCK | |
101 bsf eeprom_blockwrite ; After the start, do blockwriting for the next 63Bytes! | |
102 | |
103 movlw d'0' ; increase address | |
104 incf eeprom_address+0,F | |
105 addwfc eeprom_address+1,F | |
106 bcf eeprom_overflow | |
107 | |
108 btfss eeprom_address+1,7 ; at address 8000 | |
109 return ; no, return | |
110 | |
111 clrf eeprom_address+0 ; Clear eeprom address | |
112 clrf eeprom_address+1 | |
113 bsf eeprom_overflow ; and set overflow bit | |
114 return | |
115 I2CWRITE_BLOCK: | |
116 movwf ext_ee_temp1 ; Data byte in WREG | |
117 bsf SSPCON2,SEN ; Start condition | |
118 rcall WaitMSSP | |
119 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
120 movwf SSPBUF ; control byte | |
121 rcall WaitMSSP | |
122 rcall I2C_WaitforACK | |
123 movff eeprom_address+1,SSPBUF ; High Address byte | |
124 rcall WaitMSSP | |
125 rcall I2C_WaitforACK | |
126 movff eeprom_address+0,SSPBUF ; Low Address byte | |
127 rcall WaitMSSP | |
128 rcall I2C_WaitforACK | |
129 I2CWRITE_BLOCK2: | |
130 movff ext_ee_temp1, SSPBUF ; Data Byte | |
131 rcall WaitMSSP | |
132 rcall I2C_WaitforACK | |
133 return | |
134 | |
135 | |
136 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
|
137 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
138 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
139 ; 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
|
140 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
|
141 movlw 0x1 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
142 movwf eeprom_address+1 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
143 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
144 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
145 |
0 | 146 clrf ext_ee_temp1 ; low address counter |
147 clrf ext_ee_temp2 ; high address counter | |
148 bcf second_FD ; clear flags | |
149 bcf first_FD | |
150 get_free_EEPROM_location3: | |
151 bsf SSPCON2, PEN ; Stop condition | |
152 rcall WaitMSSP | |
153 bsf SSPCON2,SEN ; Start condition | |
154 rcall WaitMSSP | |
155 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
156 movwf SSPBUF ; control byte | |
157 rcall WaitMSSP | |
158 btfsc SSPCON2,ACKSTAT | |
159 bra get_free_EEPROM_location3 ; EEPROM NOT acknowledged, retry! | |
160 | |
161 movff ext_ee_temp2,SSPBUF ; High Address byte | |
162 rcall WaitMSSP | |
163 rcall I2C_WaitforACK | |
164 movff ext_ee_temp1,SSPBUF ; Low Address byte | |
165 rcall WaitMSSP | |
166 rcall I2C_WaitforACK | |
167 | |
168 bsf SSPCON2,RSEN ; Start condition | |
169 rcall WaitMSSP | |
170 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ | |
171 movwf SSPBUF ; control byte | |
172 rcall WaitMSSP | |
173 rcall I2C_WaitforACK | |
174 | |
175 get_free_EEPROM_location2: | |
176 bsf SSPCON2, RCEN ; Enable recieve mode | |
177 rcall WaitMSSP | |
178 btfsc first_FD | |
179 bra test_2nd_FD | |
180 bsf first_FD ; found first 0xFD? | |
181 movlw 0xFD | |
182 cpfseq SSPBUF | |
183 bcf first_FD ; No | |
184 bra get_free_EEPROM_location2c | |
185 | |
186 test_2nd_FD: | |
187 btfsc second_FD | |
188 bra test_FE | |
189 bsf second_FD ; found second 0xFD? | |
190 movlw 0xFD | |
191 cpfseq SSPBUF | |
192 bra get_free_EEPROM_location2b ;No, clear both flags | |
193 bra get_free_EEPROM_location2c | |
194 test_FE: | |
195 movlw 0xFE ; found the final 0xFE? | |
196 cpfseq SSPBUF | |
197 bra get_free_EEPROM_location2b ;No, clear both flags | |
198 movff ext_ee_temp1,eeprom_address+0 ;Yes, copy ext_ee_temp1->eeprom_address+0 and | |
199 movff ext_ee_temp2,eeprom_address+1 ;ext_ee_temp2->eeprom_address+1 | |
200 bra get_free_EEPROM_location4 ; Done. | |
201 | |
202 get_free_EEPROM_location2b: | |
203 bcf second_FD ; clear both flags! | |
204 bcf first_FD | |
205 get_free_EEPROM_location2c: | |
206 movlw d'1' ; and increase search address | |
207 addwf ext_ee_temp1,F | |
208 movlw d'0' | |
209 addwfc ext_ee_temp2,F | |
210 | |
42 | 211 btfsc ext_ee_temp2,7 ; 0x8000 reached? |
0 | 212 bra get_free_EEPROM_location3b ; yes |
213 | |
214 bsf SSPCON2, ACKEN ; no, send Ack | |
215 rcall WaitMSSP | |
216 bra get_free_EEPROM_location2 ; and continue search | |
217 get_free_EEPROM_location3b: | |
218 clrf eeprom_address+0 ; Not found in entire EEPROM, set to address 0 | |
219 clrf eeprom_address+1 | |
220 get_free_EEPROM_location4: | |
221 bsf SSPCON2, PEN ; Stop | |
222 rcall WaitMSSP | |
223 | |
224 bcf second_FD ; clear flags | |
225 bcf first_FD | |
226 return ; return | |
227 | |
228 | |
229 I2CREAD: | |
230 bsf SSPCON2, PEN ; Stop | |
231 rcall WaitMSSP | |
232 bsf SSPCON2,SEN ; Start condition | |
233 rcall WaitMSSP | |
234 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
235 movwf SSPBUF ; control byte | |
236 rcall WaitMSSP | |
237 btfsc SSPCON2,ACKSTAT | |
238 bra I2CREAD ; EEPROM NOT acknowledged, retry! | |
239 movff eeprom_address+1,SSPBUF ; High Address byte | |
240 rcall WaitMSSP | |
241 rcall I2C_WaitforACK | |
242 movff eeprom_address+0,SSPBUF ; Low Address byte | |
243 rcall WaitMSSP | |
244 rcall I2C_WaitforACK | |
245 | |
246 bsf SSPCON2,RSEN ; Start condition | |
247 rcall WaitMSSP | |
248 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ | |
249 movwf SSPBUF ; control byte | |
250 rcall WaitMSSP | |
251 rcall I2C_WaitforACK | |
252 | |
253 bsf SSPCON2, RCEN ; Enable recieve mode | |
254 rcall WaitMSSP | |
255 movf SSPBUF,W ; copy read byte into WREG | |
256 bsf SSPCON2, PEN ; Stop | |
257 rcall WaitMSSP | |
258 return | |
259 | |
260 I2CREAD2: ; same as I2CREAD but with automatic address increase | |
261 rcall I2CREAD | |
262 movlw d'1' | |
263 addwf eeprom_address+0,F | |
264 movlw d'0' | |
265 addwfc eeprom_address+1,F | |
266 bcf eeprom_overflow | |
267 btfss eeprom_address+1,7 ; at 0x8000? | |
268 return ; no, return | |
269 | |
270 clrf eeprom_address+0 ; Yes, clear address | |
271 clrf eeprom_address+1 | |
272 bsf eeprom_overflow ; and set overflow bit | |
273 return | |
274 | |
275 I2CWRITE: | |
276 movwf ext_ee_temp1 ; Data byte | |
277 bsf SSPCON2,SEN ; Start condition | |
278 rcall WaitMSSP | |
279 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
280 movwf SSPBUF ; control byte | |
281 rcall WaitMSSP | |
282 rcall I2C_WaitforACK | |
283 movff eeprom_address+1,SSPBUF ; High Address byte | |
284 rcall WaitMSSP | |
285 rcall I2C_WaitforACK | |
286 movff eeprom_address+0,SSPBUF ; Low Address byte | |
287 rcall WaitMSSP | |
288 rcall I2C_WaitforACK | |
289 movff ext_ee_temp1, SSPBUF ; Data Byte | |
290 rcall WaitMSSP | |
291 rcall I2C_WaitforACK | |
292 bsf SSPCON2,PEN ; Stop condition | |
293 rcall WaitMSSP | |
294 WAITMS d'6' ; Write delay | |
295 return | |
296 | |
297 I2C_WaitforACK: | |
298 btfsc SSPCON2,ACKSTAT ; checks for ACK bit from slave | |
299 rcall I2CFail | |
300 return | |
301 | |
302 I2CFail: | |
303 ostc_debug 'M' ; Sends debug-information to screen if debugmode active | |
21 | 304 bsf LED_red |
0 | 305 rcall I2CReset ; I2C Reset |
306 bcf PIR1,SSPIF | |
307 clrf i2c_temp | |
308 return | |
309 | |
310 WaitMSSP: | |
311 decfsz i2c_temp,F ; check for timeout during I2C action | |
312 bra WaitMSSP2 | |
313 bra I2CFail ; timeout occured | |
314 WaitMSSP2: | |
315 btfss PIR1,SSPIF | |
316 bra WaitMSSP | |
317 clrf i2c_temp | |
318 bcf PIR1,SSPIF | |
319 return | |
320 | |
321 I2CReset: ; Something went wrong (Slave holds SDA low?) | |
322 clrf SSPCON1 ; wake-up slave and reset entire module | |
323 ostc_debug 'N' ; Sends debug-information to screen if debugmode active | |
324 clrf SSPCON2 | |
325 clrf SSPSTAT | |
326 bcf TRISC,3 ; SCL OUTPUT | |
327 bsf TRISC,4 ; SDA Input | |
328 bcf PORTC,3 | |
329 movlw d'9' | |
330 movwf i2c_temp ; clock-out 9 clock cycles manually | |
331 I2CReset_1: | |
332 bsf PORTC,3 ; SCL=1 | |
333 nop | |
334 btfsc PORTC,4 ; SDA=1? | |
335 bra I2CReset_2 ; =1, SDA has been released from slave | |
336 bcf PORTC,3 ; SCL=0 | |
337 bcf PORTC,3 | |
338 decfsz i2c_temp,F | |
339 bra I2CReset_1 ; check for nine clock cycles | |
340 I2CReset_2: | |
341 bsf TRISC,3 ; SCL Input | |
342 clrf SSPCON1 ; set I²C Mode | |
343 WAITMS d'10' ; Reset-Timeout for I2C devices | |
344 movlw b'00000000' | |
345 movwf SSPSTAT | |
346 movlw b'00101000' | |
347 movwf SSPCON1 | |
348 movlw b'00000000' | |
349 movwf SSPCON2 | |
350 movlw d'8' ; 400kHz I2C clock @ 16MHz Fcy | |
351 movwf SSPADD | |
21 | 352 bcf LED_red |
0 | 353 ostc_debug 'O' ; Sends debug-information to screen if debugmode active |
354 return | |
53 | 355 |
356 ;I2C_TX: | |
357 ; movwf i2c_temp2 ; Data byte | |
358 ; bsf SSPCON2,SEN ; Start condition | |
359 ; rcall WaitMSSP | |
360 ; movlw b'10010000' ; Bit0=0: WRITE, Bit0=1: READ | |
361 ; movwf SSPBUF ; control byte | |
362 ; rcall WaitMSSP | |
363 ; rcall I2C_WaitforACK | |
364 ; movff i2c_temp2, SSPBUF ; Data Byte | |
365 ; rcall WaitMSSP | |
366 ; rcall I2C_WaitforACK | |
367 ; bsf SSPCON2,PEN ; Stop condition | |
368 ; rcall WaitMSSP | |
369 ; return | |
370 ;I2C_RX: | |
371 ; bcf PIR1,SSPIF | |
372 ; bsf SSPCON2,SEN ; Start condition | |
373 ; rcall WaitMSSP | |
374 ; movlw b'10010001' ; Bit0=0: WRITE, Bit0=1: READ | |
375 ; movwf SSPBUF ; control byte | |
376 ; rcall WaitMSSP | |
377 ; rcall I2C_WaitforACK | |
378 ; bsf SSPCON2, RCEN ; Enable recieve mode | |
379 ; rcall WaitMSSP | |
380 ; movff SSPBUF,i2c_temp2 ; Data Byte | |
381 ; bsf SSPCON2,ACKEN ; Master acknowlegde | |
382 ; rcall WaitMSSP | |
383 ; bsf SSPCON2,PEN ; Stop condition | |
384 ; rcall WaitMSSP | |
385 ; return |