Mercurial > public > mk2
annotate code_part1/OSTC_code_asm_part1/i2c_eeprom.asm @ 539:a587f491693d
clock in I2C "bus failed" routine
author | heinrichsweikamp |
---|---|
date | Sun, 08 Jan 2012 18:52:19 +0100 |
parents | 3091628b2742 |
children | 77c8ff191cd7 |
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 | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
21 incf_eeprom_address macro ext_ee_temp1 ; Will increase eeprom_address:2 with the 8Bit value "ext_ee_temp1" |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
22 movlw ext_ee_temp1 |
148 | 23 call incf_eeprom_address0 |
0 | 24 endm |
25 | |
26 incf_eeprom_address0: | |
148 | 27 addwf eeprom_address+0,F ; increase address |
28 movlw d'0' | |
29 addwfc eeprom_address+1,F | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
30 return |
425 | 31 |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
32 ;============================================================================= |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
33 ; Will decrease eeprom_address:2 with the 8Bit value "ext_ee_temp1" |
0 | 34 |
148 | 35 |
36 decf_eeprom_address macro ext_ee_temp1 | |
37 movlw ext_ee_temp1 | |
38 call decf_eeprom_address0 | |
39 endm | |
0 | 40 |
41 decf_eeprom_address0: | |
148 | 42 subwf eeprom_address+0,F ; decrease address: do a 16-8bits substract. |
43 movlw d'0' | |
44 subwfb eeprom_address+1,F | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
45 return |
148 | 46 |
47 ;============================================================================= | |
0 | 48 |
49 write_external_eeprom: ; data in WREG | |
50 ; increase address eeprom_address+0:eeprom_address+1 after write | |
51 ; with banking after 7FFF | |
81
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
52 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
53 ; 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
|
54 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
55 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
56 |
0 | 57 rcall I2CWRITE ; writes WREG into EEPROM@eeprom_address |
58 movlw d'1' ; increase address | |
59 addwf eeprom_address+0,F | |
60 movlw d'0' | |
61 addwfc eeprom_address+1,F | |
62 return | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
63 ; btfss eeprom_address+1,7 ; at address 8000? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
64 ; return ; no, return |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
65 ; |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
66 ; clrf eeprom_address+0 ; Clear eeprom address |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
67 ; clrf eeprom_address+1 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
68 ; return |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
69 ; |
0 | 70 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
|
71 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
72 ; 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
|
73 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
74 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
75 |
0 | 76 btfsc eeprom_blockwrite ; Blockwrite continue? |
77 rcall I2CWRITE_BLOCK2 | |
78 btfss eeprom_blockwrite ; Blockwrite start? | |
79 rcall I2CWRITE_BLOCK | |
80 bsf eeprom_blockwrite ; After the start, do blockwriting for the next 63Bytes! | |
81 | |
82 movlw d'0' ; increase address | |
83 incf eeprom_address+0,F | |
84 addwfc eeprom_address+1,F | |
85 return | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
86 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
87 ; btfss eeprom_address+1,7 ; at address 8000 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
88 ; return ; no, return |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
89 ; |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
90 ; clrf eeprom_address+0 ; Clear eeprom address |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
91 ; clrf eeprom_address+1 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
92 ; return |
0 | 93 I2CWRITE_BLOCK: |
94 movwf ext_ee_temp1 ; Data byte in WREG | |
95 bsf SSPCON2,SEN ; Start condition | |
96 rcall WaitMSSP | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
97 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
98 btfss eeprom_address+1,7 ; Access Block2? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
99 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1 |
0 | 100 movwf SSPBUF ; control byte |
101 rcall WaitMSSP | |
102 rcall I2C_WaitforACK | |
103 movff eeprom_address+1,SSPBUF ; High Address byte | |
104 rcall WaitMSSP | |
105 rcall I2C_WaitforACK | |
106 movff eeprom_address+0,SSPBUF ; Low Address byte | |
107 rcall WaitMSSP | |
108 rcall I2C_WaitforACK | |
109 I2CWRITE_BLOCK2: | |
110 movff ext_ee_temp1, SSPBUF ; Data Byte | |
111 rcall WaitMSSP | |
112 rcall I2C_WaitforACK | |
113 return | |
114 | |
115 | |
116 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
|
117 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
118 #ifdef TESTING |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
119 ; 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
|
120 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
|
121 movlw 0x1 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
122 movwf eeprom_address+1 |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
123 return |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
124 #endif |
31fa973a70fd
Kludges to emulate inexisting devices when debugged with the MPLAB software SIMulator.
JeanDo
parents:
53
diff
changeset
|
125 |
0 | 126 clrf ext_ee_temp1 ; low address counter |
127 clrf ext_ee_temp2 ; high address counter | |
128 bcf second_FD ; clear flags | |
129 bcf first_FD | |
455
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
130 bcf eeprom_switched_b1 |
0 | 131 get_free_EEPROM_location3: |
132 bsf SSPCON2, PEN ; Stop condition | |
133 rcall WaitMSSP | |
134 bsf SSPCON2,SEN ; Start condition | |
135 rcall WaitMSSP | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
136 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
137 btfss ext_ee_temp2,7 ; Access Block2? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
138 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1 |
0 | 139 movwf SSPBUF ; control byte |
140 rcall WaitMSSP | |
141 btfsc SSPCON2,ACKSTAT | |
142 bra get_free_EEPROM_location3 ; EEPROM NOT acknowledged, retry! | |
143 | |
144 movff ext_ee_temp2,SSPBUF ; High Address byte | |
145 rcall WaitMSSP | |
146 rcall I2C_WaitforACK | |
147 movff ext_ee_temp1,SSPBUF ; Low Address byte | |
148 rcall WaitMSSP | |
149 rcall I2C_WaitforACK | |
150 | |
151 bsf SSPCON2,RSEN ; Start condition | |
152 rcall WaitMSSP | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
153 movlw b'10101111' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
154 btfss ext_ee_temp2,7 ; Access Block2? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
155 movlw b'10100111' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
156 movwf SSPBUF ; control byte |
0 | 157 rcall WaitMSSP |
158 rcall I2C_WaitforACK | |
159 | |
160 get_free_EEPROM_location2: | |
455
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
161 btfsc eeprom_switched_b1 ; Switched to Block1 already? |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
162 bra get_free_EEPROM_location2_x ; Yes, skip this check! |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
163 |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
164 btfsc ext_ee_temp2,7 ; Access Block1? |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
165 bsf eeprom_switched_b1 ; Yes, and set the flag so this check is skipped in the rest of this routine |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
166 btfsc ext_ee_temp2,7 ; Access Block1? |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
167 bra get_free_EEPROM_location3 ; Yes, initiate new read sequence in block1 |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
168 |
c512a868937c
last work for 2.0: Fixing 32kB boundary read for logbook
heinrichsweikamp
parents:
425
diff
changeset
|
169 get_free_EEPROM_location2_x: |
0 | 170 bsf SSPCON2, RCEN ; Enable recieve mode |
171 rcall WaitMSSP | |
172 btfsc first_FD | |
173 bra test_2nd_FD | |
174 bsf first_FD ; found first 0xFD? | |
175 movlw 0xFD | |
176 cpfseq SSPBUF | |
177 bcf first_FD ; No | |
178 bra get_free_EEPROM_location2c | |
179 | |
180 test_2nd_FD: | |
181 btfsc second_FD | |
182 bra test_FE | |
183 bsf second_FD ; found second 0xFD? | |
184 movlw 0xFD | |
185 cpfseq SSPBUF | |
186 bra get_free_EEPROM_location2b ;No, clear both flags | |
187 bra get_free_EEPROM_location2c | |
188 test_FE: | |
189 movlw 0xFE ; found the final 0xFE? | |
190 cpfseq SSPBUF | |
191 bra get_free_EEPROM_location2b ;No, clear both flags | |
192 movff ext_ee_temp1,eeprom_address+0 ;Yes, copy ext_ee_temp1->eeprom_address+0 and | |
193 movff ext_ee_temp2,eeprom_address+1 ;ext_ee_temp2->eeprom_address+1 | |
194 bra get_free_EEPROM_location4 ; Done. | |
195 | |
196 get_free_EEPROM_location2b: | |
197 bcf second_FD ; clear both flags! | |
198 bcf first_FD | |
199 get_free_EEPROM_location2c: | |
200 movlw d'1' ; and increase search address | |
201 addwf ext_ee_temp1,F | |
202 movlw d'0' | |
203 addwfc ext_ee_temp2,F | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
204 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
205 movlw 0xFF |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
206 cpfseq ext_ee_temp2 ; =0xFFFF |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
207 bra get_free_EEPROM_location2d ; No |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
208 cpfseq ext_ee_temp1 ; =0xFFFF |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
209 bra get_free_EEPROM_location2d ; No |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
210 |
0 | 211 bra get_free_EEPROM_location3b ; yes |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
212 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
213 get_free_EEPROM_location2d: |
0 | 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: | |
245
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
230 rcall I2CREAD_COMMON |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
231 bsf SSPCON2, PEN ; Stop |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
232 rcall WaitMSSP |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
233 return |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
234 |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
235 I2CREAD2: ; same as I2CREAD but with automatic address increase |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
236 rcall I2CREAD |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
237 I2CREAD2_2: |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
238 movlw d'1' |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
239 addwf eeprom_address+0,F |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
240 movlw d'0' |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
241 addwfc eeprom_address+1,F |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
242 return |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
243 ; btfss eeprom_address+1,7 ; at 0x8000? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
244 ; return ; no, return |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
245 ; |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
246 ; clrf eeprom_address+0 ; Yes, clear address |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
247 ; clrf eeprom_address+1 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
248 ; return |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
249 ; |
245
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
250 I2CREAD3: ; block read start with automatic address increase |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
251 rcall I2CREAD_COMMON |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
252 ; no Stop condition here |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
253 bra I2CREAD2_2 |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
254 |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
255 I2CREAD4: ; block read with automatic address increase |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
256 bsf SSPCON2,ACKEN ; Master acknowlegde |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
257 rcall WaitMSSP |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
258 bsf SSPCON2, RCEN ; Enable recieve mode |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
259 rcall WaitMSSP |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
260 movf SSPBUF,W ; copy read byte into WREG |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
261 ; no Stop condition here |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
262 bra I2CREAD2_2 |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
263 |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
264 |
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
265 I2CREAD_COMMON: |
0 | 266 bsf SSPCON2, PEN ; Stop |
267 rcall WaitMSSP | |
268 bsf SSPCON2,SEN ; Start condition | |
269 rcall WaitMSSP | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
270 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
271 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
272 btfss eeprom_address+1,7 ; Access Block2? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
273 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
274 movwf SSPBUF ; control byte |
0 | 275 rcall WaitMSSP |
276 btfsc SSPCON2,ACKSTAT | |
277 bra I2CREAD ; EEPROM NOT acknowledged, retry! | |
278 movff eeprom_address+1,SSPBUF ; High Address byte | |
279 rcall WaitMSSP | |
280 rcall I2C_WaitforACK | |
281 movff eeprom_address+0,SSPBUF ; Low Address byte | |
282 rcall WaitMSSP | |
283 rcall I2C_WaitforACK | |
284 | |
285 bsf SSPCON2,RSEN ; Start condition | |
286 rcall WaitMSSP | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
287 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
288 movlw b'10101111' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
289 btfss eeprom_address+1,7 ; Access Block2? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
290 movlw b'10100111' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
291 movwf SSPBUF ; control byte |
0 | 292 rcall WaitMSSP |
293 rcall I2C_WaitforACK | |
294 | |
295 bsf SSPCON2, RCEN ; Enable recieve mode | |
296 rcall WaitMSSP | |
297 movf SSPBUF,W ; copy read byte into WREG | |
298 return | |
299 | |
245
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
300 |
0 | 301 I2CWRITE: |
302 movwf ext_ee_temp1 ; Data byte | |
303 bsf SSPCON2,SEN ; Start condition | |
304 rcall WaitMSSP | |
350
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
305 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
306 btfss eeprom_address+1,7 ; Access Block2? |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
307 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1 |
7250ca7c8d24
64kByte logbook (Still incompatible to existing PC software)
heinrichsweikamp
parents:
245
diff
changeset
|
308 movwf SSPBUF ; control byte |
0 | 309 rcall WaitMSSP |
310 rcall I2C_WaitforACK | |
311 movff eeprom_address+1,SSPBUF ; High Address byte | |
312 rcall WaitMSSP | |
313 rcall I2C_WaitforACK | |
314 movff eeprom_address+0,SSPBUF ; Low Address byte | |
315 rcall WaitMSSP | |
316 rcall I2C_WaitforACK | |
317 movff ext_ee_temp1, SSPBUF ; Data Byte | |
318 rcall WaitMSSP | |
319 rcall I2C_WaitforACK | |
320 bsf SSPCON2,PEN ; Stop condition | |
321 rcall WaitMSSP | |
245
dee88c962653
Some clean up and minor I2C speed improvements
heinrichsweikamp
parents:
148
diff
changeset
|
322 WAITMS d'5' ; Write delay |
0 | 323 return |
324 | |
325 I2C_WaitforACK: | |
326 btfsc SSPCON2,ACKSTAT ; checks for ACK bit from slave | |
327 rcall I2CFail | |
328 return | |
329 | |
330 I2CFail: | |
331 ostc_debug 'M' ; Sends debug-information to screen if debugmode active | |
21 | 332 bsf LED_red |
0 | 333 rcall I2CReset ; I2C Reset |
334 bcf PIR1,SSPIF | |
335 clrf i2c_temp | |
336 return | |
337 | |
338 WaitMSSP: | |
339 decfsz i2c_temp,F ; check for timeout during I2C action | |
340 bra WaitMSSP2 | |
341 bra I2CFail ; timeout occured | |
342 WaitMSSP2: | |
343 btfss PIR1,SSPIF | |
344 bra WaitMSSP | |
345 clrf i2c_temp | |
346 bcf PIR1,SSPIF | |
347 return | |
348 | |
349 I2CReset: ; Something went wrong (Slave holds SDA low?) | |
350 clrf SSPCON1 ; wake-up slave and reset entire module | |
351 ostc_debug 'N' ; Sends debug-information to screen if debugmode active | |
352 clrf SSPCON2 | |
353 clrf SSPSTAT | |
354 bcf TRISC,3 ; SCL OUTPUT | |
355 bsf TRISC,4 ; SDA Input | |
356 bcf PORTC,3 | |
357 movlw d'9' | |
358 movwf i2c_temp ; clock-out 9 clock cycles manually | |
359 I2CReset_1: | |
360 bsf PORTC,3 ; SCL=1 | |
361 nop | |
539 | 362 nop |
363 nop | |
364 nop | |
0 | 365 btfsc PORTC,4 ; SDA=1? |
539 | 366 bra I2CReset_2 ; =1, SDA has been released from slave |
0 | 367 bcf PORTC,3 ; SCL=0 |
539 | 368 nop |
369 nop | |
0 | 370 bcf PORTC,3 |
539 | 371 nop |
372 nop | |
0 | 373 decfsz i2c_temp,F |
374 bra I2CReset_1 ; check for nine clock cycles | |
375 I2CReset_2: | |
376 bsf TRISC,3 ; SCL Input | |
377 clrf SSPCON1 ; set I²C Mode | |
378 WAITMS d'10' ; Reset-Timeout for I2C devices | |
537 | 379 movlw SSPSTAT_VALUE |
0 | 380 movwf SSPSTAT |
381 movlw b'00101000' | |
382 movwf SSPCON1 | |
383 movlw b'00000000' | |
384 movwf SSPCON2 | |
537 | 385 movlw SSPADD_VALUE |
0 | 386 movwf SSPADD |
21 | 387 bcf LED_red |
0 | 388 ostc_debug 'O' ; Sends debug-information to screen if debugmode active |
389 return | |
53 | 390 |
391 ;I2C_TX: | |
392 ; movwf i2c_temp2 ; Data byte | |
393 ; bsf SSPCON2,SEN ; Start condition | |
394 ; rcall WaitMSSP | |
395 ; movlw b'10010000' ; Bit0=0: WRITE, Bit0=1: READ | |
396 ; movwf SSPBUF ; control byte | |
397 ; rcall WaitMSSP | |
398 ; rcall I2C_WaitforACK | |
399 ; movff i2c_temp2, SSPBUF ; Data Byte | |
400 ; rcall WaitMSSP | |
401 ; rcall I2C_WaitforACK | |
402 ; bsf SSPCON2,PEN ; Stop condition | |
403 ; rcall WaitMSSP | |
404 ; return | |
405 ;I2C_RX: | |
406 ; bcf PIR1,SSPIF | |
407 ; bsf SSPCON2,SEN ; Start condition | |
408 ; rcall WaitMSSP | |
409 ; movlw b'10010001' ; Bit0=0: WRITE, Bit0=1: READ | |
410 ; movwf SSPBUF ; control byte | |
411 ; rcall WaitMSSP | |
412 ; rcall I2C_WaitforACK | |
413 ; bsf SSPCON2, RCEN ; Enable recieve mode | |
414 ; rcall WaitMSSP | |
415 ; movff SSPBUF,i2c_temp2 ; Data Byte | |
416 ; bsf SSPCON2,ACKEN ; Master acknowlegde | |
417 ; rcall WaitMSSP | |
418 ; bsf SSPCON2,PEN ; Stop condition | |
419 ; rcall WaitMSSP | |
420 ; return |