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