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