Mercurial > public > hwos_code
annotate src/eeprom_rs232.asm @ 632:0347acdf6d8e
changelog updates
author | heinrichsweikamp |
---|---|
date | Sat, 29 Feb 2020 16:57:45 +0100 |
parents | 185ba2f91f59 |
children | 4050675965ea |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
631 | 3 ; File eeprom_rs232.asm combined next generation V3.08.8 |
0 | 4 ; |
5 ; Internal EEPROM, RS232 | |
6 ; | |
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | |
8 ;============================================================================= | |
9 ; HISTORY | |
10 ; 2011-08-06 : [mH] moving from OSTC code | |
11 | |
275 | 12 #include "hwos.inc" |
0 | 13 #include "wait.inc" |
582 | 14 #include "shared_definitions.h" |
15 #include "rtc.inc" | |
631 | 16 #include "external_flash.inc" |
17 | |
18 #DEFINE INSIDE_EEPROM_RS232 | |
19 #include "eeprom_rs232.inc" | |
20 | |
582 | 21 |
623 | 22 extern lt2942_charge_done |
23 | |
24 ;----------------------------------------------------------------------------- | |
631 | 25 ; |
26 ; for EEPROM Macros and Memory Map, see eeprom_rs232.inc | |
27 ; | |
28 ;----------------------------------------------------------------------------- | |
623 | 29 |
631 | 30 ee_rs232 CODE |
582 | 31 |
631 | 32 ;============================================================================= |
33 ; EEPROM Functions | |
34 ;============================================================================= | |
582 | 35 |
623 | 36 ;----------------------------------------------------------------------------- |
37 ; read from internal EEPROM | |
38 ; | |
604 | 39 ; Input: EEADRH:EEADR = EEPROM address |
40 ; Output: EEDATA | |
41 ; Trashed: NONE | |
623 | 42 ; |
0 | 43 global read_eeprom |
582 | 44 read_eeprom: |
631 | 45 bcf EECON1,EEPGD ; |
46 bcf EECON1,CFGS ; | |
47 bsf EECON1,RD ; | |
0 | 48 return |
49 | |
631 | 50 ;----------------------------------------------------------------------------- |
623 | 51 ; write into internal EEPROM |
52 ; | |
604 | 53 ; Input: EEADRH:EEADR = EEPROM address |
54 ; EEDATA = byte to write | |
55 ; Trashed: WREG | |
623 | 56 ; |
604 | 57 global write_eeprom |
58 write_eeprom: | |
631 | 59 bcf EECON1,EEPGD ; |
60 bcf EECON1,CFGS ; | |
61 bsf EECON1,WREN ; | |
62 bcf INTCON,GIE ; disable interrupts for the next 5 instructions | |
63 movlw 0x55 ; unlock sequence | |
64 movwf EECON2 ; ... | |
65 movlw 0xAA ; ... | |
66 movwf EECON2 ; ... | |
67 bsf EECON1,WR ; start write operation | |
68 write_eeprom_loop: | |
69 btfsc EECON1,WR ; write completed? | |
70 bra write_eeprom_loop ; NO - loop waiting | |
71 bcf EECON1,WREN ; | |
72 bsf INTCON,GIE ; ...but the flag for the ISR routines were still set, so they will interrupt now! | |
73 return | |
74 | |
75 ;----------------------------------------------------------------------------- | |
76 ; these 2 functions are meant to be used through the macros, see eeprom_rs232! | |
77 ; | |
78 global eeprom_read_common | |
79 eeprom_read_common: | |
80 movwf eeprom_loop ; initialize loop counter | |
81 eeprom_read_common_loop: | |
82 rcall read_eeprom ; execute read | |
83 movff EEDATA,POSTINC1 ; copy byte from EEPROM data register to memory | |
84 incf EEADR,F ; advance to next EEPROM cell | |
85 decfsz eeprom_loop,F ; decrement loop counter, all done? | |
86 bra eeprom_read_common_loop ; NO - loop | |
87 return ; YES - done | |
604 | 88 |
631 | 89 global eeprom_write_common |
90 eeprom_write_common: | |
91 movwf eeprom_loop ; initialize loop counter | |
92 eeprom_write_common_loop: | |
93 movff POSTINC1,EEDATA ; copy byte from memory to EEPROM data register | |
94 rcall write_eeprom ; execute write | |
95 incf EEADR,F ; advance to next EEPROM cell | |
96 decfsz eeprom_loop,F ; decrement loop counter, all done? | |
97 bra eeprom_write_common_loop ; NO - loop | |
98 return ; YES - done | |
99 | |
100 ;----------------------------------------------------------------------------- | |
101 ; REad OSTC serial number | |
102 ; | |
103 global eeprom_serial_number_read | |
104 eeprom_serial_number_read: | |
105 EEPROM_II_READ eeprom_ostc_serial,mpr | |
106 return | |
604 | 107 |
631 | 108 ;----------------------------------------------------------------------------- |
109 ; Read and write dive number offset | |
110 ; | |
111 global eeprom_log_offset_read | |
112 eeprom_log_offset_read: | |
113 EEPROM_II_READ eeprom_log_offset,mpr | |
114 return | |
115 | |
116 global eeprom_log_offset_write | |
117 eeprom_log_offset_write: | |
118 EEPROM_II_WRITE mpr,eeprom_log_offset | |
119 return | |
120 | |
121 | |
122 ;----------------------------------------------------------------------------- | |
123 ; Read and write total number of dives | |
124 ; | |
125 global eeprom_total_dives_read | |
126 eeprom_total_dives_read: | |
127 EEPROM_II_READ eeprom_num_dives,mpr | |
128 return | |
129 | |
130 global eeprom_total_dives_write | |
131 eeprom_total_dives_write: | |
132 EEPROM_II_WRITE mpr,eeprom_num_dives | |
604 | 133 return |
134 | |
631 | 135 |
136 ;----------------------------------------------------------------------------- | |
137 ; Read and write the battery gauge and type | |
138 ; | |
139 global eeprom_battery_gauge_read | |
140 eeprom_battery_gauge_read: | |
141 ; retrieve battery gauge from EEPROM 0x07-0x0C | |
142 bsf block_battery_gauge ; suspend ISR from accessing the battery gauge | |
143 EEPROM_CC_READ eeprom_battery_type, battery_type ; 1 byte read from EEPROM | |
144 EEPROM_RR_READ eeprom_battery_gauge,battery_gauge,.6 ; 6 byte read from EEPROM | |
145 bcf block_battery_gauge ; allow ISR to access the battery gauge again | |
146 return | |
147 | |
148 global eeprom_battery_gauge_write | |
149 eeprom_battery_gauge_write: | |
150 bsf block_battery_gauge ; suspend ISR from accessing the battery gauge | |
151 EEPROM_CC_WRITE battery_type, eeprom_battery_type ; 1 byte write to EEPROM | |
152 update_battery_gauge: | |
153 EEPROM_RR_WRITE battery_gauge,eeprom_battery_gauge,.6 ; 6 byte write to EEPROM | |
154 bcf block_battery_gauge ; allow ISR to access the battery gauge again | |
155 return | |
156 | |
157 | |
158 ;----------------------------------------------------------------------------- | |
159 ; Read and write the deco status | |
160 ; | |
161 global eeprom_deco_data_read | |
162 eeprom_deco_data_read: | |
163 | |
164 btfsc RCON,POR ; was there a power outage ? | |
165 bra eeprom_deco_data_read_1 ; NO - RTC is up-to-date | |
166 | |
167 EEPROM_RR_READ eeprom_deco_data_timestamp,rtc_latched_year,.6 ; 6 byte read from EEPROM | |
168 call rtc_set_rtc ; recover RTC to last known time & date | |
169 | |
170 eeprom_deco_data_read_1: | |
171 | |
172 ; restore surface interval | |
173 EEPROM_II_READ eeprom_deco_data_surfinterval,mpr ; 2 byte read from EEPROM | |
174 SMOVII mpr,surface_interval_mins ; ISR-safe copy of surface interval | |
175 | |
176 ; bank 3: restore desaturation status | |
177 EEPROM_RR_READ eeprom_deco_data_bank3,0x300,.9 ; 9 byte read from EEPROM | |
178 | |
179 ; bank 5: restore CNS | |
180 EEPROM_RR_READ eeprom_deco_data_bank5,0x500,.4 ; 4 byte read from EEPROM | |
181 | |
182 ; bank 7: restore tissue pressures | |
183 EEPROM_RR_READ eeprom_deco_data_bank7,0x700,.128 ; 128 byte read from EEPROM | |
184 | |
185 return ; done | |
186 | |
187 | |
188 global eeprom_deco_data_write | |
189 eeprom_deco_data_write: | |
190 | |
191 ; invalidate current data in vault | |
192 movlw DECO_DATA_INVALID_TOKEN ; deco data invalid token | |
193 EEPROM_CC_WRITE WREG,eeprom_deco_data_validity ; 1 byte write to EEPROM | |
194 | |
195 ; store vault version | |
196 movlw eeprom_vault_version ; deco data format version | |
197 EEPROM_CC_WRITE WREG,eeprom_deco_data_version ; 1 byte write to EEPROM | |
198 | |
199 ; store date/time | |
200 SMOVSS rtc_year,rtc_latched_year ; ISR-safe 6 byte copy of date and time | |
201 EEPROM_RR_WRITE rtc_latched_year,eeprom_deco_data_timestamp,.6 ; 6 byte write to EEPROM | |
202 | |
203 ; store surface interval | |
204 SMOVII surface_interval_mins,mpr ; ISR-safe copy of surface interval | |
205 EEPROM_II_WRITE mpr,eeprom_deco_data_surfinterval ; 2 byte write to EEPROM | |
206 | |
207 ; bank 3: store desaturation status | |
208 EEPROM_RR_WRITE 0x300,eeprom_deco_data_bank3,.9 ; 9 byte write to EEPROM | |
209 | |
210 ; bank 5: store CNS | |
211 EEPROM_RR_WRITE 0x500,eeprom_deco_data_bank5,.4 ; 4 byte write to EEPROM | |
212 | |
213 ; bank 7: store tissue pressures | |
214 EEPROM_RR_WRITE 0x700,eeprom_deco_data_bank7,.128 ; 128 byte write to EEPROM | |
215 | |
216 ; indicate new valid data in vault | |
217 movlw DECO_DATA_VALID_TOKEN ; deco data valid token | |
218 EEPROM_CC_WRITE WREG,eeprom_deco_data_validity ; 1 byte write to EEPROM | |
219 | |
220 return ; done | |
221 | |
222 | |
223 | |
224 ;============================================================================= | |
225 ; RS232 Functions | |
623 | 226 ;============================================================================= |
227 | |
582 | 228 global disable_ir_s8 |
229 disable_ir_s8: | |
623 | 230 banksel TXSTA2 ; select bank for IO register access |
582 | 231 clrf TXSTA2 |
232 clrf RCSTA2 | |
623 | 233 banksel common ; back to bank common |
629 | 234 bcf PIE3,RC2IE ; disable RC2 INT |
235 bcf ir_power ; IR off | |
236 bcf mcp_power ; power-down instrumentation amp | |
237 bsf s8_npower ; power-down S8 digital interface | |
238 bcf s8_digital_avail ; digital S8 interface not available | |
0 | 239 return |
240 | |
241 | |
582 | 242 global enable_ir_s8 |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
151
diff
changeset
|
243 enable_ir_s8: |
623 | 244 ;initialize serial port2 (TRISG2) |
245 btfsc analog_o2_input ; do we have an analog input? | |
246 bra enable_s8 ; YES - search for S8 digital input | |
247 ; NO - start IR digital input | |
248 banksel BAUDCON2 ; - select bank for IO register access | |
249 movlw b'00100000' ; - BRG16=0, inverted for IR | |
113 | 250 movwf BAUDCON2 |
623 | 251 movlw b'00100000' ; - BRGH=0, SYNC=0 |
582 | 252 movwf TXSTA2 |
623 | 253 movlw .102 ; - SPBRGH:SPBRG = .102 : 2403 BAUD @ 16 MHz |
582 | 254 movwf SPBRG2 |
623 | 255 clrf SPBRGH2 |
582 | 256 movlw b'10010000' |
257 movwf RCSTA2 | |
623 | 258 banksel common ; - back to bank common |
259 bsf ir_power ; - power-up IR | |
629 | 260 btfss ir_power ; - power-up confirmed? |
261 bra $-6 ; NO - loop and wait | |
262 bsf PIE3,RC2IE ; - enable RC2 INT | |
263 return ; - done | |
0 | 264 |
113 | 265 enable_s8: |
629 | 266 banksel TXSTA2 ; select bank for IO register access |
631 | 267 clrf TXSTA2 ; reset UART 2 TX function |
268 clrf RCSTA2 ; reset UART 2 RX function | |
623 | 269 banksel common ; back to bank common |
631 | 270 |
271 bsf mcp_power ; power-up instrumentation amp (for analog AND digital) | |
272 btfss mcp_power ; power-up completed? | |
273 bra $-4 ; NO - loop | |
113 | 274 |
631 | 275 ; toggle for digital/analog |
276 TSTOSS opt_s8_mode ; =0: analog, =1: digital RS232 | |
277 bra enable_s8_analog ; -> analog | |
278 | |
279 ; configure S8 digital interface | |
280 bcf s8_npower ; power S8 HUD (inverted via P-MOS transistor) | |
623 | 281 WAITMS d'30' ; NO - wait 30 ms |
631 | 282 banksel BAUDCON2 ; select bank for IO register access |
623 | 283 movlw b'00000000' ; BRG16=0, normal for S8 |
113 | 284 movwf BAUDCON2 |
582 | 285 movlw b'00100000' ; BRGH=0, SYNC=0 |
286 movwf TXSTA2 | |
604 | 287 movlw .25 ; SPBRGH:SPBRG = .25 : 9615 BAUD @ 16 MHz |
582 | 288 movwf SPBRG2 |
289 movlw b'10010000' | |
290 movwf RCSTA2 | |
623 | 291 banksel common ; back to bank common |
631 | 292 bsf PIE3,RC2IE ; enable RC2 INT |
623 | 293 bsf s8_digital_avail ; digital S8 interface available |
582 | 294 return |
113 | 295 |
631 | 296 enable_s8_analog: |
297 ; S8 analog interface | |
298 bcf PIE3,RC2IE ; disable RC2 INT | |
299 bsf s8_npower ; power-down S8 HUD | |
300 bcf s8_digital_avail ; digital S8 interface not available | |
301 return | |
302 | |
303 | |
304 global ir_s8_wait_tx | |
305 ir_s8_wait_tx: | |
306 banksel TXSTA2 ; select bank for IO register access | |
307 rs232_wait_tx2_loop: | |
308 btfss TXSTA2,TRMT ; RS232 busy? | |
309 bra rs232_wait_tx2_loop ; YES - wait... | |
310 banksel common ; NO - back to bank common | |
311 return ; - done | |
312 | |
313 ;----------------------------------------------------------------------------- | |
582 | 314 |
0 | 315 global enable_rs232 |
316 enable_rs232: | |
623 | 317 call request_speed_normal ; request CPU speed change to normal speed |
318 enable_rs232_1: | |
319 btfss speed_is_normal ; speed = normal? | |
631 | 320 bra enable_rs232_1 ; NO - loop waiting for ISR to have adjusted the speed |
321 bcf PORTE,0 ; YES - switch port to comm | |
322 bsf PORTJ,2 ; - /Reset (required for very old OSTC sport) | |
323 movlw b'00100100' ; - TX configuration: TX enabled, async, high speed | |
324 movwf TXSTA1 ; - ... | |
325 movlw b'10010000' ; - RX configuration: port enabled, RX enabled | |
326 movwf RCSTA1 ; - ... | |
327 movlw HIGH(.65536-rx_timeout*.32) ; - define TMR5H initialization value for RX timeout | |
328 movwf rx_timoeut_tmr5h_load ; - store for later use | |
329 return ; - done | |
0 | 330 |
582 | 331 |
0 | 332 global disable_rs232 |
333 disable_rs232: | |
631 | 334 clrf RCSTA1 ; disable RX |
335 clrf TXSTA1 ; disable TX | |
336 bcf PORTC,6 ; switch TX pin hard to GND | |
623 | 337 bsf PORTE,0 ; stop comm |
631 | 338 bcf PORTJ,2 ; /Reset (required for very old OSTC sport) |
582 | 339 return |
340 | |
341 | |
631 | 342 global rs232_wait_tx ; ++++ do not touch WREG here! ++++ |
343 rs232_wait_tx: | |
344 btfss TXSTA1,TRMT ; last byte completely shifted out on TX pin? | |
345 bra rs232_wait_tx ; NO - wait... | |
346 btfss ble_available ; YES - OSTC running with Bluetooth? | |
347 return ; NO - done | |
348 btfsc NRTS ; YES - Bluetooth module also completed TX? | |
349 bra rs232_wait_tx ; NO - wait... | |
350 return ; YES - done | |
623 | 351 |
352 | |
631 | 353 ; ++++ make this code as fast as possible! ++++ |
354 global rs232_get_byte ; ++++ do not touch WREG here! ++++ | |
355 rs232_get_byte: | |
356 bcf rs232_rx_timeout ; clear timeout flag | |
357 btfsc PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1) | |
358 return ; YES - done, received a byte (fast path) | |
359 movff rx_timoeut_tmr5h_load,TMR5H ; - load TMR5 high with timeout value | |
360 clrf TMR5L ; - load TMR5 low with a zero, writing low starts the timer | |
361 bcf PIR5,TMR5IF ; - clear timer overflow flag | |
362 rs232_get_byte_loop: | |
363 btfsc PIR1,RCIF ; received a data byte? | |
364 return ; YES - done, received a byte | |
365 btfss PIR5,TMR5IF ; NO - timer overflow (timeout)? | |
366 bra rs232_get_byte_loop ; NO - continue looping | |
367 ;bra rs232_rx_get_timeout ; YES - give up | |
623 | 368 |
631 | 369 rs232_rx_get_timeout: |
370 bsf rs232_rx_timeout ; set timeout flag | |
371 bcf RCSTA1,CREN ; clear receiver status by toggling CREN | |
372 bsf RCSTA1,CREN ; ... | |
373 return ; done, given up | |
623 | 374 |
582 | 375 |
631 | 376 ;----------------------------------------------------------------------------- |
377 ; Send and Receive functions to be used through the macros | |
623 | 378 |
631 | 379 ; send a range of 1-256 bytes from memory to the RS232 interface |
380 ; | |
381 global serial_tx_ram | |
382 serial_tx_ram: | |
383 movwf eeprom_loop ; initialize loop counter (eeprom variable used here) | |
384 serial_tx_ram_loop: | |
385 rcall rs232_wait_tx ; wait for completion of last transmit | |
386 movff POSTINC2,TXREG1 ; send a byte from memory to serial | |
387 decfsz eeprom_loop,F ; decrement loop counter, became zero? | |
388 bra serial_tx_ram_loop ; NO - loop | |
389 return ; YES - done | |
582 | 390 |
623 | 391 |
631 | 392 ; receive a range of 1-256 byte from the RS232 interface and write them to memory |
393 ; | |
394 global serial_rx_stream_ram ; ++++ make this code as fast as possible! ++++ | |
395 serial_rx_stream_ram: | |
396 movwf eeprom_loop ; initialize loop counter (eeprom variable used here) | |
397 serial_rx_stream_ram_loop_1: | |
398 btfss PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1) | |
399 bra serial_rx_stream_ram_tmr ; NO - enter loop with timeout | |
400 movff RCREG1,POSTINC2 ; YES - copy received byte to memory | |
401 decfsz eeprom_loop,F ; - decrement loop counter, became zero? | |
402 bra serial_rx_stream_ram_loop_1 ; NO - loop | |
403 bcf rs232_rx_timeout ; YES - clear timeout flag | |
404 return ; - all bytes received, done | |
405 serial_rx_stream_ram_tmr: | |
406 movff rx_timoeut_tmr5h_load,TMR5H ; load TMR5 high with timeout value | |
407 clrf TMR5L ; load TMR5 low with a zero, writing low starts the timer | |
408 bcf PIR5,TMR5IF ; clear timer overflow flag | |
409 serial_rx_stream_ram_loop_2a: | |
410 clrf TMR5L ; restart timer (see above) | |
411 serial_rx_stream_ram_loop_2b: | |
412 btfss PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1) | |
413 bra serial_rx_stream_ram_chk ; NO - check timeout | |
414 movff RCREG1,POSTINC2 ; YES - copy received byte to memory | |
415 decfsz eeprom_loop,F ; - decrement loop counter, became zero? | |
416 bra serial_rx_stream_ram_loop_2a; NO - loop | |
417 bcf rs232_rx_timeout ; YES - clear timeout flag | |
418 return ; - all bytes received, done | |
419 serial_rx_stream_ram_chk: | |
420 btfss PIR5,TMR5IF ; timer overflow (timeout)? | |
421 bra serial_rx_stream_ram_loop_2b; NO - continue looping | |
422 bra rs232_rx_get_timeout ; YES - give up | |
623 | 423 |
631 | 424 ;----------------------------------------------------------------------------- |
582 | 425 END |