comparison src/eeprom_rs232.asm @ 582:b455b31ce022

work on 2.97 stable
author heinrichsweikamp
date Mon, 26 Feb 2018 16:40:28 +0100
parents 3c55627d5f8f
children ca4556fb60b9
comparison
equal deleted inserted replaced
581:f5de1ff88814 582:b455b31ce022
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File eeprom_rs232.asm 3 ; File eeprom_rs232.asm V2.98
4 ; 4 ;
5 ; Internal EEPROM, RS232 5 ; Internal EEPROM, RS232
6 ; 6 ;
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. 7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
8 ;============================================================================= 8 ;=============================================================================
9 ; HISTORY 9 ; HISTORY
10 ; 2011-08-06 : [mH] moving from OSTC code 10 ; 2011-08-06 : [mH] moving from OSTC code
11 11
12 #include "hwos.inc" 12 #include "hwos.inc"
13 #include "wait.inc" 13 #include "wait.inc"
14 14 #include "shared_definitions.h"
15 ;============================================================================= 15 #include "rtc.inc"
16 eeprom code 0xF00000+0x10 16
17 write_int_eeprom macro eeprom_address
18 movlw eeprom_address
19 call write_int_eeprom_1
20 endm
21
22 read_int_eeprom macro eeprom_address
23 movlw eeprom_address
24 call read_int_eeprom_1
25 endm
26
27 ;=============================================================================
28 eeprom code 0xF00000+0x10
17 ; Skip SERIAL number. Should not be overwritten. 29 ; Skip SERIAL number. Should not be overwritten.
18 global eeprom_serial_save, eeprom_opt_backup 30
19 eeprom_serial_save res 2 31 global eeprom_serial_save
20 eeprom_opt_backup res 0x3E 32 global eeprom_opt_backup
21 33
22 ;============================================================================= 34 eeprom_serial_save res 2
23 basic CODE 35 eeprom_opt_backup res 0x3E
24 36
25 global write_int_eeprom_1 37 ;=============================================================================
38 basic CODE
39
40 global write_int_eeprom_1
26 write_int_eeprom_1: 41 write_int_eeprom_1:
27 movwf EEADR 42 movwf EEADR
28 bra write_eeprom ; writes and "returns" after write 43 ; bra write_eeprom ; writes and "returns" after write
44
45 ;=============================================================================
46 ; writes into internal eeprom
47 ; Input: EEADRH:EEADR = EEPROM address.
48 ; EEDATA = byte to write.
49 ; Trashed: WREG.
50 global write_eeprom
51 write_eeprom:
52 bcf EECON1,EEPGD
53 bcf EECON1,CFGS
54 bsf EECON1,WREN
55
56 bcf INTCON,GIE ; Disable interrupts for the next 5 instructions
57 movlw 0x55
58 movwf EECON2
59 movlw 0xAA
60 movwf EECON2
61 bsf EECON1,WR
62 bsf INTCON,GIE ; ...but the flag for the ISR routines were still set, so they will interrupt now!
63
64 write_eep2:
65 btfsc EECON1,WR
66 bra write_eep2 ; wait about 4ms...
67 bcf EECON1,WREN
68 return
69
70
29 71
30 global read_int_eeprom_1 72 global read_int_eeprom_1
31 read_int_eeprom_1: 73 read_int_eeprom_1:
32 movwf EEADR 74 movwf EEADR
33 bra read_eeprom ; reads and "returns" after write 75 ; bra read_eeprom ; reads and "returns" after write
34 76
35 ;============================================================================= 77 ;=============================================================================
36 ; reads from internal eeprom 78 ; reads from internal eeprom
37 ; Input: EEADRH:EEADR = EEPROM address. 79 ; Input: EEADRH:EEADR = EEPROM address.
38 ; Output: EEDATA. 80 ; Output: EEDATA.
39 ; Trashed: NONE. 81 ; Trashed: NONE.
40 global read_eeprom 82 global read_eeprom
41 read_eeprom: 83 read_eeprom:
42 bcf EECON1,EEPGD 84 bcf EECON1,EEPGD
43 bcf EECON1,CFGS 85 bcf EECON1,CFGS
44 bsf EECON1,RD 86 bsf EECON1,RD
45 return 87 return
46 88
47 ;============================================================================= 89 global disable_ir_s8
48 ; writes into internal eeprom
49 ; Input: EEADRH:EEADR = EEPROM address.
50 ; EEDATA = byte to write.
51 ; Trashed: WREG.
52 global write_eeprom
53 write_eeprom:
54 bcf EECON1,EEPGD
55 bcf EECON1,CFGS
56 bsf EECON1,WREN
57
58 bcf INTCON,GIE ; Disable interrups for the next 5 instructions
59 movlw 0x55
60 movwf EECON2
61 movlw 0xAA
62 movwf EECON2
63 bsf EECON1,WR
64 bsf INTCON,GIE ; ...but the flag for the ISR routines were still set, so they will interrupt now!
65
66 write_eep2:
67 btfsc EECON1,WR
68 bra write_eep2 ; wait about 4ms...
69 bcf EECON1,WREN
70 return
71
72 global disable_ir_s8
73 disable_ir_s8: 90 disable_ir_s8:
74 banksel TXSTA2 91 banksel TXSTA2
75 clrf TXSTA2 92 clrf TXSTA2
76 clrf RCSTA2 93 clrf RCSTA2
77 banksel common 94 banksel common
78 bcf ir_power ; IR off 95 bcf ir_power ; IR off
79 bcf mcp_power ; Power-down intrumentation amp 96 bcf mcp_power ; Power-down instrumentation amp
80 bsf s8_npower ; Power-down S8 HUD 97 bsf s8_npower ; Power-down S8 HUD
81 return 98 return
82 99
83 global enable_ir_s8 100
101 global enable_ir_s8
84 enable_ir_s8: 102 enable_ir_s8:
85 ;init serial port2 (TRISG2) 103 ;init serial port2 (TRISG2)
86 btfsc analog_o2_input 104 btfsc analog_o2_input
87 bra enable_s8 ; Start S8 105 bra enable_s8 ; Start S8
88 106
89 banksel BAUDCON2 107 banksel BAUDCON2
90 movlw b'00100000' ; BRG16=0 ; inverted for IR 108 movlw b'00100000' ; BRG16=0 ; inverted for IR
91 movwf BAUDCON2 109 movwf BAUDCON2
92 movlw b'00100000' ; BRGH=0, SYNC=0 110 movlw b'00100000' ; BRGH=0, SYNC=0
93 movwf TXSTA2 111 movwf TXSTA2
94 movlw .102 ; SPBRGH:SPBRG = .102 : 2403 BAUD @ 16MHz 112 movlw .102 ; SPBRGH:SPBRG = .102 : 2403 BAUD @ 16MHz
95 movwf SPBRG2 113 movwf SPBRG2
96 movlw b'10010000' 114 movlw b'10010000'
97 movwf RCSTA2 115 movwf RCSTA2
98 banksel common 116 banksel common
99 bsf ir_power ; Power-up IR 117 bsf ir_power ; Power-up IR
100 btfss ir_power 118 btfss ir_power
101 bra $-6 119 bra $-6
102 return 120 return
103 121
104 enable_s8: 122 enable_s8:
105 ; Check for Digital/Analog 123 ; Check for Digital/Analog
106 bsf s8_npower ; Power-down S8 HUD 124 bsf s8_npower ; Power-down S8 HUD
107 WAITMS d'1' ; Very short delay 125 WAITMS d'1' ; Very short delay
108 bsf mcp_power ; Power-up intrumentation amp 126 bsf mcp_power ; Power-up instrumentation amp
109 btfss mcp_power 127 btfss mcp_power
110 bra $-6 128 bra $-6
111 banksel TXSTA2 129 banksel TXSTA2
112 clrf TXSTA2 130 clrf TXSTA2
113 clrf RCSTA2 131 clrf RCSTA2
114 banksel common 132 banksel common
115 133
116 ; It may be digital, check for voltage when isolator is powered 134 ; It may be digital, check for voltage when isolator is powered
117 bcf s8_npower ; Power S8 HUD 135 bcf s8_npower ; Power S8 HUD
118 WAITMS d'1' ; Very short delay 136 WAITMS d'1' ; Very short delay
119 137
120 btfsc PORTG,2 ; RX2=1? 138 btfsc PORTG,2 ; RX2=1?
121 bra enable_s8_2 ; Yes, digital 139 bra enable_s8_2 ; Yes, digital
122 WAITMS d'30' 140 WAITMS d'30'
123 btfsc PORTG,2 ; RX2=1? 141 btfsc PORTG,2 ; RX2=1?
124 bra enable_s8_2 ; Yes, digital 142 bra enable_s8_2 ; Yes, digital
125 143
126 ; Not found, set to analog (fail-safe) 144 ; Not found, set to analog (fail-safe)
127 145
128 enable_s8_analog: 146 enable_s8_analog:
129 ; S8 Analog 147 ; S8 Analog
130 bsf s8_npower ; Power-down S8 HUD 148 bsf s8_npower ; Power-down S8 HUD
131 bcf s8_digital ; Clear flag 149 bcf s8_digital ; Clear flag
132 return 150 return
133 151
134 enable_s8_2: ; S8 Digital 152 enable_s8_2: ; S8 Digital
135 banksel BAUDCON2 153 banksel BAUDCON2
136 movlw b'00000000' ; BRG16=0 ; normal for S8 154 movlw b'00000000' ; BRG16=0 ; normal for S8
137 movwf BAUDCON2 155 movwf BAUDCON2
138 movlw b'00100000' ; BRGH=0, SYNC=0 156 movlw b'00100000' ; BRGH=0, SYNC=0
139 movwf TXSTA2 157 movwf TXSTA2
140 movlw .25 ; SPBRGH:SPBRG = .25 : 9615 BAUD @ 16MHz 158 movlw .25 ; SPBRGH:SPBRG = .25 : 9615 BAUD @ 16MHz
141 movwf SPBRG2 159 movwf SPBRG2
142 movlw b'10010000' 160 movlw b'10010000'
143 movwf RCSTA2 161 movwf RCSTA2
144 banksel common 162 banksel common
145 bsf s8_digital ; Set flag 163 bsf s8_digital ; Set flag
146 return 164 return
147 165
148 ;============================================================================= 166 ;=============================================================================
167
149 global enable_rs232 168 global enable_rs232
150 enable_rs232: 169 enable_rs232:
151 call speed_normal ; 16MHz 170 call speed_normal ; 16MHz
152 enable_rs232_2: 171 enable_rs232_2:
153 movlw T2CON_NORMAL 172 movlw T2CON_NORMAL
154 cpfseq T2CON 173 cpfseq T2CON
155 bra enable_rs232_2 ; Wait until speed is normal 174 bra enable_rs232_2 ; Wait until speed is normal
156 bcf PORTE,0 ; Start comms 175 bcf PORTE,0 ; Start comms
157 ;init serial port1 (TRISC6/7) 176 ;init serial port1 (TRISC6/7)
158 movlw b'00100100' ; BRGH=1, SYNC=0 177 movlw b'00100100' ; BRGH=1, SYNC=0
159 movwf TXSTA1 178 movwf TXSTA1
160 movlw b'10010000' 179 movlw b'10010000'
161 movwf RCSTA1 180 movwf RCSTA1
162 return 181 return
182
163 183
164 global disable_rs232 184 global disable_rs232
165 disable_rs232: 185 disable_rs232:
166 clrf RCSTA1 186 clrf RCSTA1
167 clrf TXSTA1 ; UART disable 187 clrf TXSTA1 ; UART disable
168 bcf PORTC,6 ; TX hard to GND 188 bcf PORTC,6 ; TX hard to GND
169 bsf PORTE,0 ; Stop comms 189 bsf PORTE,0 ; Stop comms
170 return 190 return
171 191
172 192
173 global rs232_wait_tx 193 global rs232_wait_tx
174 rs232_wait_tx: 194 rs232_wait_tx:
175 btfss TXSTA1,TRMT ; RS232 Busy? 195 btfss TXSTA1,TRMT ; RS232 Busy?
176 bra rs232_wait_tx ; yes, wait... 196 bra rs232_wait_tx ; yes, wait...
177 197
178 btfss ble_available ; ble available? 198 btfss ble_available ; ble available?
179 return ; No, done. 199 return ; No, done.
180 200
181 btfsc NRTS ; Wait for Bluetooth module 201 btfsc NRTS ; Wait for Bluetooth module
182 bra rs232_wait_tx ; yes, wait... 202 bra rs232_wait_tx ; yes, wait...
183 return ; Done. 203 return ; Done.
184 204
185 global rs232_wait_tx2 205
206 global rs232_wait_tx2
186 rs232_wait_tx2: 207 rs232_wait_tx2:
187 banksel TXSTA2 208 banksel TXSTA2
188 rs232_wait_tx2_1: 209 rs232_wait_tx2_1:
189 btfss TXSTA2,TRMT ; RS232 Busy? 210 btfss TXSTA2,TRMT ; RS232 Busy?
190 bra rs232_wait_tx2_1 ; yes, wait... 211 bra rs232_wait_tx2_1 ; yes, wait...
191 banksel common 212 banksel common
192 return ; Done. 213 return ; Done.
193 214
194 global rs232_get_byte 215 global rs232_get_byte
195 rs232_get_byte: 216 rs232_get_byte:
196 bcf rs232_recieve_overflow ; clear flag 217 bcf rs232_receive_overflow ; clear flag
197 movlw .10 218 movlw .10
198 movwf uart1_temp 219 movwf uart1_temp
199 clrf uart2_temp 220 clrf uart2_temp
200 clrf uart3_temp 221 clrf uart3_temp
201 rs232_get_byte2: 222 rs232_get_byte2:
202 btfsc PIR1,RCIF ; data arrived? 223 btfsc PIR1,RCIF ; data arrived?
203 return ; Yes 224 return ; Yes
204 decfsz uart3_temp,F 225 decfsz uart3_temp,F
205 bra rs232_get_byte2
206 decfsz uart2_temp,F
207 bra rs232_get_byte2 226 bra rs232_get_byte2
208 decfsz uart1_temp,F 227 decfsz uart2_temp,F
209 bra rs232_get_byte2 228 bra rs232_get_byte2
210 ; timeout occoured (about 400ms) 229 decfsz uart1_temp,F
211 bsf rs232_recieve_overflow ; set flag 230 bra rs232_get_byte2
231 ; timeout occurred (about 400ms)
232 bsf rs232_receive_overflow ; set flag
212 rs232_get_byte3: 233 rs232_get_byte3:
213 bcf RCSTA1,CREN ; Clear receiver status 234 bcf RCSTA1,CREN ; Clear receiver status
214 bsf RCSTA1,CREN 235 bsf RCSTA1,CREN
215 return ; and return anyway 236 return ; and return anyway
237
238 ;=============================================================================
239
240 global do_logoffset_common_write
241 do_logoffset_common_write:
242 ; TODO: limit [lo:hi] to 9999 ? may arrive here as >= 10000 because of +10 increment option
243 ; SIMPLER WAY: return without writing to eeprom if hi == 0x27 -> 0x2700 = 9984, this is within
244 ; the save range for a +10 increment, shall do it...
245 ; mH
246
247 movff lo,EEDATA
248 write_int_eeprom 0x0D
249 movff hi,EEDATA
250 write_int_eeprom 0x0E
251 return
252
253 global do_logoffset_common_read
254 do_logoffset_common_read:
255 clrf EEADRH
256 read_int_eeprom 0x0D
257 movff EEDATA,lo
258 read_int_eeprom 0x0E
259 movff EEDATA,hi ; Existing logbook offset into lo:hi
260 return
261
262
263 global update_battery_registers
264 update_battery_registers:
265 ; save battery_gauge:6 into EEPROM 0x07-0x0C
266 clrf EEADRH
267 movff battery_gauge+0,EEDATA
268 write_int_eeprom 0x07
269 movff battery_gauge+1,EEDATA
270 write_int_eeprom 0x08
271 movff battery_gauge+2,EEDATA
272 write_int_eeprom 0x09
273 movff battery_gauge+3,EEDATA
274 write_int_eeprom 0x0A
275 movff battery_gauge+4,EEDATA
276 write_int_eeprom 0x0B
277 movff battery_gauge+5,EEDATA
278 write_int_eeprom 0x0C
279 movff battery_type,EEDATA ; =0:1.5V, =1:3,6V Saft, =2:LiIon 3,7V/0.8Ah, =3:LiIon 3,7V/3.1Ah, =4: LiIon 3,7V/2.3Ah
280 write_int_eeprom 0x0F
281 return
282
283
284 global vault_decodata_into_eeprom
285 vault_decodata_into_eeprom:
286 ; Vault in EEPROM 512...1023
287 ; Write 0xAA at 512 to indicate valid data in vault
288 ; Store last time/date
289 ; Store 0x700 to 0x780 (pres_tissue_N2 and pres_tissue_He)
290 movlw HIGH .512 ; =2
291 movwf EEADRH
292 movlw 0xAA
293 movwf EEDATA
294 write_int_eeprom .0
295 ; Store date/time
296 movff year,EEDATA
297 write_int_eeprom .1
298 movff month,EEDATA
299 write_int_eeprom .2
300 movff day,EEDATA
301 write_int_eeprom .3
302 movff hours,EEDATA
303 write_int_eeprom .4
304 movff mins,EEDATA
305 write_int_eeprom .5
306 movff secs,EEDATA
307 write_int_eeprom .6
308
309 movff int_O_CNS_fraction+0,EEDATA
310 write_int_eeprom .7
311 movff int_O_CNS_fraction+1,EEDATA
312 write_int_eeprom .8
313 movff int_O_desaturation_time+0,EEDATA
314 write_int_eeprom .9
315 movff int_O_desaturation_time+1,EEDATA
316 write_int_eeprom .10
317 movff surface_interval+0,EEDATA
318 write_int_eeprom .11
319 movff surface_interval+1,EEDATA
320 write_int_eeprom .12
321 movff int_O_gradient_factor+0,EEDATA ; value limited to 255, only lower byte in use for value
322 write_int_eeprom .13
323 movff int_O_nofly_time+0,EEDATA
324 write_int_eeprom .14
325 movff int_O_nofly_time+1,EEDATA
326 write_int_eeprom .15
327
328 ; Tissue data from 16 to 144
329 movlw .16
330 movwf EEADR
331 movlw .128
332 movwf lo
333 lfsr FSR1,0x700 ; pres_tissue_N2+0, 32*4Byte Float = 128Bytes
334 vault_decodata_into_eeprom2:
335 movff POSTINC1,EEDATA
336 call write_eeprom ; EEDATA into EEPROM@EEADR
337 incf EEADR,F
338 decfsz lo,F ; All done?
339 bra vault_decodata_into_eeprom2 ; No
340 clrf EEADRH
341 return
342
343 global restore_decodata_from_eeprom
344 restore_decodata_from_eeprom:
345 movlw LOW .512 ; =0
346 movwf EEADR
347 movlw HIGH .512 ; =2
348 movwf EEADRH
349
350 ; Restore date/time
351 read_int_eeprom .1
352 movff EEDATA,year
353 read_int_eeprom .2
354 movff EEDATA,month
355 read_int_eeprom .3
356 movff EEDATA,day
357 read_int_eeprom .4
358 movff EEDATA,hours
359 read_int_eeprom .5
360 movff EEDATA,mins
361 read_int_eeprom .6
362 movff EEDATA,secs
363 call rtc_set_rtc
364
365 read_int_eeprom .7
366 movff EEDATA,int_O_CNS_fraction+0
367 read_int_eeprom .8
368 movff EEDATA,int_O_CNS_fraction+1
369 read_int_eeprom .9
370 movff EEDATA,int_O_desaturation_time+0
371 read_int_eeprom .10
372 movff EEDATA,int_O_desaturation_time+1
373 read_int_eeprom .11
374 movff EEDATA,surface_interval+0
375 read_int_eeprom .12
376 movff EEDATA,surface_interval+1
377 read_int_eeprom .13
378 movff EEDATA,int_O_gradient_factor+0
379 read_int_eeprom .14
380 movff EEDATA,int_O_nofly_time+0
381 read_int_eeprom .15
382 movff EEDATA,int_O_nofly_time+1
383
384 ; Tissue data from 16 to 144
385 movlw .16
386 movwf EEADR
387 movlw .128
388 movwf lo
389 lfsr FSR1,0x700 ; pres_tissue_N2+0, 32*4Byte Float = 128Bytes
390 restore_decodata_from_eeprom2:
391 call read_eeprom ; EEPROM@EEADR into EEDATA
392 movff EEDATA,POSTINC1
393 incf EEADR,F
394 decfsz lo,F ; All done?
395 bra restore_decodata_from_eeprom2 ; No
396 clrf EEADRH
397 return
398
399
400 global reset_battery_internal_only
401 reset_battery_internal_only:
402 clrf EEADRH
403 clrf EEDATA ; Delete to zero
404 write_int_eeprom 0x07
405 write_int_eeprom 0x08
406 write_int_eeprom 0x09
407 write_int_eeprom 0x0A
408 write_int_eeprom 0x0B
409 write_int_eeprom 0x0C
410 banksel battery_gauge+0
411 clrf battery_gauge+0
412 clrf battery_gauge+1
413 clrf battery_gauge+2
414 clrf battery_gauge+3
415 clrf battery_gauge+4
416 clrf battery_gauge+5
417 banksel common
418 movlw .100
419 movwf batt_percent
420 return
421
422
423 global eeprom_reset_logbook_pointers
424 eeprom_reset_logbook_pointers:
425 clrf EEADRH ; Make sure to select EEPROM bank 0
426 clrf EEDATA
427 write_int_eeprom .4
428 write_int_eeprom .5
429 write_int_eeprom .6
430 write_int_eeprom .2 ; Also, delete total dive counter
431 write_int_eeprom .3
432 write_int_eeprom .16
433 write_int_eeprom .17 ; And the backup counter, too
434 return
216 435
217 END 436 END