Mercurial > public > hwos_code
comparison src/eeprom_rs232.asm @ 634:4050675965ea
3.10 stable release
author | heinrichsweikamp |
---|---|
date | Tue, 28 Apr 2020 17:34:31 +0200 |
parents | 185ba2f91f59 |
children | 8c1f1f334275 |
comparison
equal
deleted
inserted
replaced
633:690c48db7b5b | 634:4050675965ea |
---|---|
1 ;============================================================================= | 1 ;============================================================================= |
2 ; | 2 ; |
3 ; File eeprom_rs232.asm combined next generation V3.08.8 | 3 ; File eeprom_rs232.asm * combined next generation V3.09.4n |
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 ;============================================================================= |
19 #include "eeprom_rs232.inc" | 19 #include "eeprom_rs232.inc" |
20 | 20 |
21 | 21 |
22 extern lt2942_charge_done | 22 extern lt2942_charge_done |
23 | 23 |
24 ;----------------------------------------------------------------------------- | 24 |
25 ; | 25 ;============================================================================= |
26 ; for EEPROM Macros and Memory Map, see eeprom_rs232.inc | 26 eeprom CODE |
27 ; | 27 ;============================================================================= |
28 ;----------------------------------------------------------------------------- | 28 |
29 | 29 ;----------------------------------------------------------------------------- |
30 ee_rs232 CODE | 30 ; |
31 | 31 ; EEPROM Functions - for EEPROM Macros and Memory Map, see eeprom_rs232.inc |
32 ;============================================================================= | 32 ; |
33 ; EEPROM Functions | 33 ;----------------------------------------------------------------------------- |
34 ;============================================================================= | 34 |
35 | 35 |
36 ;----------------------------------------------------------------------------- | 36 ;----------------------------------------------------------------------------- |
37 ; read from internal EEPROM | 37 ; Read from internal EEPROM |
38 ; | 38 ; |
39 ; Input: EEADRH:EEADR = EEPROM address | 39 ; Input: EEADRH:EEADR = EEPROM address |
40 ; Output: EEDATA | 40 ; Output: EEDATA |
41 ; Trashed: NONE | 41 ; Trashed: NONE |
42 ; | 42 ; |
43 global read_eeprom | 43 global read_eeprom |
44 read_eeprom: | 44 read_eeprom: |
45 bcf EECON1,EEPGD ; | 45 bcf EECON1,EEPGD ; access data EEPROM |
46 bcf EECON1,CFGS ; | 46 bcf EECON1,CFGS ; ... |
47 bsf EECON1,RD ; | 47 bsf EECON1,RD ; initiate reading |
48 return | 48 return ; done |
49 | 49 |
50 ;----------------------------------------------------------------------------- | 50 |
51 ; write into internal EEPROM | 51 ;----------------------------------------------------------------------------- |
52 ; Write into internal EEPROM | |
52 ; | 53 ; |
53 ; Input: EEADRH:EEADR = EEPROM address | 54 ; Input: EEADRH:EEADR = EEPROM address |
54 ; EEDATA = byte to write | 55 ; EEDATA = byte to write |
55 ; Trashed: WREG | 56 ; Trashed: WREG |
56 ; | 57 ; |
57 global write_eeprom | 58 global write_eeprom |
58 write_eeprom: | 59 write_eeprom: |
59 bcf EECON1,EEPGD ; | 60 bcf EECON1,EEPGD ; access data EEPROM |
60 bcf EECON1,CFGS ; | 61 bcf EECON1,CFGS ; ... |
61 bsf EECON1,WREN ; | 62 bsf EECON1,WREN ; enable writing |
62 bcf INTCON,GIE ; disable interrupts for the next 5 instructions | 63 bcf INTCON,GIE ; disable interrupts |
63 movlw 0x55 ; unlock sequence | 64 movlw 0x55 ; unlock sequence |
64 movwf EECON2 ; ... | 65 movwf EECON2 ; ... |
65 movlw 0xAA ; ... | 66 movlw 0xAA ; ... |
66 movwf EECON2 ; ... | 67 movwf EECON2 ; ... |
67 bsf EECON1,WR ; start write operation | 68 bsf EECON1,WR ; start write operation |
68 write_eeprom_loop: | 69 write_eeprom_loop: |
69 btfsc EECON1,WR ; write completed? | 70 btfsc EECON1,WR ; write completed? |
70 bra write_eeprom_loop ; NO - loop waiting | 71 bra write_eeprom_loop ; NO - loop waiting |
71 bcf EECON1,WREN ; | 72 bcf EECON1,WREN ; YES - disable writing |
72 bsf INTCON,GIE ; ...but the flag for the ISR routines were still set, so they will interrupt now! | 73 bsf INTCON,GIE ; - re-enable interrupts |
73 return | 74 return ; - done |
74 | 75 |
75 ;----------------------------------------------------------------------------- | 76 |
76 ; these 2 functions are meant to be used through the macros, see eeprom_rs232! | 77 ;----------------------------------------------------------------------------- |
78 ; EEPROM read and write Functions to be used via Macros | |
77 ; | 79 ; |
78 global eeprom_read_common | 80 global eeprom_read_common |
79 eeprom_read_common: | 81 eeprom_read_common: |
80 movwf eeprom_loop ; initialize loop counter | 82 movwf eeprom_loop ; initialize loop counter |
81 eeprom_read_common_loop: | 83 eeprom_read_common_loop: |
95 incf EEADR,F ; advance to next EEPROM cell | 97 incf EEADR,F ; advance to next EEPROM cell |
96 decfsz eeprom_loop,F ; decrement loop counter, all done? | 98 decfsz eeprom_loop,F ; decrement loop counter, all done? |
97 bra eeprom_write_common_loop ; NO - loop | 99 bra eeprom_write_common_loop ; NO - loop |
98 return ; YES - done | 100 return ; YES - done |
99 | 101 |
100 ;----------------------------------------------------------------------------- | 102 |
101 ; REad OSTC serial number | 103 ;----------------------------------------------------------------------------- |
104 ; Read OSTC Serial Number | |
102 ; | 105 ; |
103 global eeprom_serial_number_read | 106 global eeprom_serial_number_read |
104 eeprom_serial_number_read: | 107 eeprom_serial_number_read: |
105 EEPROM_II_READ eeprom_ostc_serial,mpr | 108 EEPROM_II_READ eeprom_ostc_serial,mpr ; read serial number |
106 return | 109 return ; done |
107 | 110 |
108 ;----------------------------------------------------------------------------- | 111 |
109 ; Read and write dive number offset | 112 ;----------------------------------------------------------------------------- |
113 ; Read and Write Dive Number Offset | |
110 ; | 114 ; |
111 global eeprom_log_offset_read | 115 global eeprom_log_offset_read |
112 eeprom_log_offset_read: | 116 eeprom_log_offset_read: |
113 EEPROM_II_READ eeprom_log_offset,mpr | 117 EEPROM_II_READ eeprom_log_offset,mpr ; read log offset |
114 return | 118 return ; done |
115 | 119 |
116 global eeprom_log_offset_write | 120 global eeprom_log_offset_write |
117 eeprom_log_offset_write: | 121 eeprom_log_offset_write: |
118 EEPROM_II_WRITE mpr,eeprom_log_offset | 122 EEPROM_II_WRITE mpr,eeprom_log_offset ; write log-offset |
119 return | 123 return ; done |
120 | 124 |
121 | 125 |
122 ;----------------------------------------------------------------------------- | 126 ;----------------------------------------------------------------------------- |
123 ; Read and write total number of dives | 127 ; Read and Write total Number of Dives |
124 ; | 128 ; |
125 global eeprom_total_dives_read | 129 global eeprom_total_dives_read |
126 eeprom_total_dives_read: | 130 eeprom_total_dives_read: |
127 EEPROM_II_READ eeprom_num_dives,mpr | 131 EEPROM_II_READ eeprom_num_dives,mpr ; read total dives |
128 return | 132 return ; done |
129 | 133 |
130 global eeprom_total_dives_write | 134 global eeprom_total_dives_write |
131 eeprom_total_dives_write: | 135 eeprom_total_dives_write: |
132 EEPROM_II_WRITE mpr,eeprom_num_dives | 136 EEPROM_II_WRITE mpr,eeprom_num_dives ; write total dives |
133 return | 137 return ; done |
134 | 138 |
135 | 139 |
136 ;----------------------------------------------------------------------------- | 140 ;----------------------------------------------------------------------------- |
137 ; Read and write the battery gauge and type | 141 ; Read and Write the Battery Type and Gauge Reading |
138 ; | 142 ; |
139 global eeprom_battery_gauge_read | 143 global eeprom_battery_gauge_read |
140 eeprom_battery_gauge_read: | 144 eeprom_battery_gauge_read: |
141 ; retrieve battery gauge from EEPROM 0x07-0x0C | 145 ; retrieve battery gauge from EEPROM 0x07-0x0C |
142 bsf block_battery_gauge ; suspend ISR from accessing the battery gauge | 146 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 | 147 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 | 148 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 | 149 bcf block_battery_gauge ; allow ISR to access the battery gauge again |
146 return | 150 return ; done |
147 | 151 |
148 global eeprom_battery_gauge_write | 152 global eeprom_battery_gauge_write |
149 eeprom_battery_gauge_write: | 153 eeprom_battery_gauge_write: |
150 bsf block_battery_gauge ; suspend ISR from accessing the battery gauge | 154 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 | 155 EEPROM_CC_WRITE battery_type, eeprom_battery_type ; 1 byte write to EEPROM |
152 update_battery_gauge: | 156 update_battery_gauge: |
153 EEPROM_RR_WRITE battery_gauge,eeprom_battery_gauge,.6 ; 6 byte write to EEPROM | 157 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 | 158 bcf block_battery_gauge ; allow ISR to access the battery gauge again |
155 return | 159 return ; done |
156 | 160 |
157 | 161 |
158 ;----------------------------------------------------------------------------- | 162 ;----------------------------------------------------------------------------- |
159 ; Read and write the deco status | 163 ; Memorize the Checksum of the Firmware in the update Storage |
164 ; | |
165 global eeprom_memorize_fw_checksum | |
166 eeprom_memorize_fw_checksum: | |
167 EXT_FLASH_ADDR 0x3E000D ; address firmware ID at 0x3E000D | |
168 FLASH_CW_READ_0x40 ; read firmware ID to WREG | |
169 movff WREG,buffer+.5 ; append firmware ID to checksum | |
170 EEPROM_RR_WRITE buffer,eeprom_fw_chksum_current,.6 ; do a 6 byte write to EEPROM | |
171 return ; done | |
172 | |
173 | |
174 ;----------------------------------------------------------------------------- | |
175 ; Read and Write the Deco Status | |
160 ; | 176 ; |
161 global eeprom_deco_data_read | 177 global eeprom_deco_data_read |
162 eeprom_deco_data_read: | 178 eeprom_deco_data_read: |
163 | 179 |
164 btfsc RCON,POR ; was there a power outage ? | 180 btfsc RCON,POR ; was there a power outage? |
165 bra eeprom_deco_data_read_1 ; NO - RTC is up-to-date | 181 bra eeprom_deco_data_read_1 ; NO - RTC is up-to-date |
166 | 182 |
167 EEPROM_RR_READ eeprom_deco_data_timestamp,rtc_latched_year,.6 ; 6 byte read from EEPROM | 183 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 | 184 call rtc_set_rtc ; recover RTC to last known time & date |
169 | 185 |
218 EEPROM_CC_WRITE WREG,eeprom_deco_data_validity ; 1 byte write to EEPROM | 234 EEPROM_CC_WRITE WREG,eeprom_deco_data_validity ; 1 byte write to EEPROM |
219 | 235 |
220 return ; done | 236 return ; done |
221 | 237 |
222 | 238 |
223 | 239 ;============================================================================= |
224 ;============================================================================= | 240 rs232 CODE |
225 ; RS232 Functions | 241 ;============================================================================= |
226 ;============================================================================= | 242 |
227 | 243 ;----------------------------------------------------------------------------- |
228 global disable_ir_s8 | 244 ; |
229 disable_ir_s8: | 245 ; RS232 Functions |
230 banksel TXSTA2 ; select bank for IO register access | 246 ; |
231 clrf TXSTA2 | 247 ;----------------------------------------------------------------------------- |
232 clrf RCSTA2 | 248 |
249 | |
250 ;----------------------------------------------------------------------------- | |
251 ; Switch-On the IR/S8 Port | |
252 ; | |
253 global enable_ir_s8_analog | |
254 enable_ir_s8_analog: | |
255 ;initialize serial port2 (TRISG2) | |
256 btfsc ext_input_s8_ana ; do we have an S8/analog input? | |
257 bra enable_s8_analog ; YES - enable S8/analog input | |
258 ;bra enable_ir ; NO - enable IR digital input | |
259 | |
260 enable_ir: | |
261 banksel BAUDCON2 ; select bank for IO register access | |
262 movlw b'00100000' ; speed generator configuration: BRG16=0, inverted for IR | |
263 movwf BAUDCON2 ; ... | |
264 movlw b'00100000' ; TX configuration: BRGH=0, SYNC=0 | |
265 movwf TXSTA2 ; ... | |
266 movlw .102 ; speed configuration: SPBRGH:SPBRG = .102 : 2403 BAUD @ 16 MHz | |
267 movwf SPBRG2 ; ... | |
268 clrf SPBRGH2 ; ... | |
269 movlw b'10010000' ; RX configuration | |
270 movwf RCSTA2 ; ... | |
233 banksel common ; back to bank common | 271 banksel common ; back to bank common |
234 bcf PIE3,RC2IE ; disable RC2 INT | 272 bsf ir_power ; power-up IR |
235 bcf ir_power ; IR off | 273 btfss ir_power ; power-up confirmed? |
236 bcf mcp_power ; power-down instrumentation amp | 274 bra $-6 ; NO - loop and wait |
237 bsf s8_npower ; power-down S8 digital interface | 275 bsf PIE3,RC2IE ; enable RC2 INT |
238 bcf s8_digital_avail ; digital S8 interface not available | 276 return ; done |
239 return | 277 |
240 | 278 enable_s8_analog: |
241 | |
242 global enable_ir_s8 | |
243 enable_ir_s8: | |
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 | |
250 movwf BAUDCON2 | |
251 movlw b'00100000' ; - BRGH=0, SYNC=0 | |
252 movwf TXSTA2 | |
253 movlw .102 ; - SPBRGH:SPBRG = .102 : 2403 BAUD @ 16 MHz | |
254 movwf SPBRG2 | |
255 clrf SPBRGH2 | |
256 movlw b'10010000' | |
257 movwf RCSTA2 | |
258 banksel common ; - back to bank common | |
259 bsf ir_power ; - power-up IR | |
260 btfss ir_power ; - power-up confirmed? | |
261 bra $-6 ; NO - loop and wait | |
262 bsf PIE3,RC2IE ; - enable RC2 INT | |
263 return ; - done | |
264 | |
265 enable_s8: | |
266 banksel TXSTA2 ; select bank for IO register access | 279 banksel TXSTA2 ; select bank for IO register access |
267 clrf TXSTA2 ; reset UART 2 TX function | 280 clrf TXSTA2 ; reset UART 2 TX function |
268 clrf RCSTA2 ; reset UART 2 RX function | 281 clrf RCSTA2 ; reset UART 2 RX function |
269 banksel common ; back to bank common | 282 banksel common ; back to bank common |
270 | 283 |
271 bsf mcp_power ; power-up instrumentation amp (for analog AND digital) | 284 bsf mcp_power ; power-up instrumentation amp (used by S8 and analog input) |
272 btfss mcp_power ; power-up completed? | 285 btfss mcp_power ; power-up completed? |
273 bra $-4 ; NO - loop | 286 bra $-4 ; NO - loop |
274 | 287 |
275 ; toggle for digital/analog | 288 ; branch according to S8 / analog selection |
276 TSTOSS opt_s8_mode ; =0: analog, =1: digital RS232 | 289 TSTOSS opt_s8_mode ; =0: analog, =1: digital RS232 |
277 bra enable_s8_analog ; -> analog | 290 bra enable_analog ; -> analog |
278 | 291 |
279 ; configure S8 digital interface | 292 ; configure S8 digital interface |
280 bcf s8_npower ; power S8 HUD (inverted via P-MOS transistor) | 293 bcf s8_npower ; power S8 HUD (inverted via P-MOS transistor) |
281 WAITMS d'30' ; NO - wait 30 ms | 294 WAITMS d'30' ; NO - wait 30 ms |
282 banksel BAUDCON2 ; select bank for IO register access | 295 banksel BAUDCON2 ; select bank for IO register access |
283 movlw b'00000000' ; BRG16=0, normal for S8 | 296 movlw b'00000000' ; speed generator configuration: BRG16=0, normal for S8 |
284 movwf BAUDCON2 | 297 movwf BAUDCON2 ; ... |
285 movlw b'00100000' ; BRGH=0, SYNC=0 | 298 movlw b'00100000' ; TX configuration: BRGH=0, SYNC=0 |
286 movwf TXSTA2 | 299 movwf TXSTA2 ; ... |
287 movlw .25 ; SPBRGH:SPBRG = .25 : 9615 BAUD @ 16 MHz | 300 movlw .25 ; speed configuration: SPBRGH:SPBRG = .25 : 9615 BAUD @ 16 MHz |
288 movwf SPBRG2 | 301 movwf SPBRG2 ; ... |
289 movlw b'10010000' | 302 movlw b'10010000' ; RX configuration |
290 movwf RCSTA2 | 303 movwf RCSTA2 ; ... |
291 banksel common ; back to bank common | 304 banksel common ; back to bank common |
292 bsf PIE3,RC2IE ; enable RC2 INT | 305 bsf PIE3,RC2IE ; enable RC2 INT |
293 bsf s8_digital_avail ; digital S8 interface available | |
294 return | 306 return |
295 | 307 |
296 enable_s8_analog: | 308 enable_analog: |
297 ; S8 analog interface | 309 ; S8 analog interface |
298 bcf PIE3,RC2IE ; disable RC2 INT | 310 bcf PIE3,RC2IE ; disable RC2 INT |
299 bsf s8_npower ; power-down S8 HUD | 311 bsf s8_npower ; power-down S8 digital interface |
300 bcf s8_digital_avail ; digital S8 interface not available | 312 return ; done |
301 return | 313 |
302 | 314 |
303 | 315 ;----------------------------------------------------------------------------- |
304 global ir_s8_wait_tx | 316 ; Shut-Down the IR/S8 Port |
305 ir_s8_wait_tx: | 317 ; |
318 global disable_ir_s8_analog | |
319 disable_ir_s8_analog: | |
306 banksel TXSTA2 ; select bank for IO register access | 320 banksel TXSTA2 ; select bank for IO register access |
307 rs232_wait_tx2_loop: | 321 clrf TXSTA2 ; shut down TX function |
308 btfss TXSTA2,TRMT ; RS232 busy? | 322 clrf RCSTA2 ; shut down RX function |
309 bra rs232_wait_tx2_loop ; YES - wait... | 323 banksel common ; back to bank common |
310 banksel common ; NO - back to bank common | 324 bcf PIE3,RC2IE ; disable RC2 INT |
325 bcf ir_power ; power down IR receiver | |
326 bcf mcp_power ; power-down instrumentation amp | |
327 bsf s8_npower ; power-down S8 digital interface | |
328 return ; done | |
329 | |
330 | |
331 ;----------------------------------------------------------------------------- | |
332 ; Send Byte in WREG via the IR/S8 Port | |
333 ; | |
334 global ir_s8_tx_single | |
335 ir_s8_tx_single: | |
336 banksel TXSTA2 ; UART 2 is outside of the access RAM | |
337 movwf TXREG2 ; transmit byte | |
338 ir_s8_tx_single_loop: | |
339 btfss TXSTA2,TRMT ; TX completed? | |
340 bra ir_s8_tx_single_loop ; NO - wait... | |
341 banksel common ; YES - back to bank common | |
311 return ; - done | 342 return ; - done |
312 | 343 |
313 ;----------------------------------------------------------------------------- | 344 |
314 | 345 ;----------------------------------------------------------------------------- |
346 ; Switch-On USB/BT Port | |
347 ; | |
315 global enable_rs232 | 348 global enable_rs232 |
316 enable_rs232: | 349 enable_rs232: |
317 call request_speed_normal ; request CPU speed change to normal speed | 350 call request_speed_normal ; request CPU speed change to normal speed |
318 enable_rs232_1: | 351 enable_rs232_1: |
319 btfss speed_is_normal ; speed = normal? | 352 btfss speed_is_normal ; speed = normal? |
322 bsf PORTJ,2 ; - /Reset (required for very old OSTC sport) | 355 bsf PORTJ,2 ; - /Reset (required for very old OSTC sport) |
323 movlw b'00100100' ; - TX configuration: TX enabled, async, high speed | 356 movlw b'00100100' ; - TX configuration: TX enabled, async, high speed |
324 movwf TXSTA1 ; - ... | 357 movwf TXSTA1 ; - ... |
325 movlw b'10010000' ; - RX configuration: port enabled, RX enabled | 358 movlw b'10010000' ; - RX configuration: port enabled, RX enabled |
326 movwf RCSTA1 ; - ... | 359 movwf RCSTA1 ; - ... |
327 movlw HIGH(.65536-rx_timeout*.32) ; - define TMR5H initialization value for RX timeout | 360 IFNDEF _comm_debug |
328 movwf rx_timoeut_tmr5h_load ; - store for later use | 361 movlw HIGH(.65536-rx_timeout*.32) ; - define TMR5H initialization value for RX timeout (rx_timeout defined in hwos.inc) |
362 ELSE | |
363 include "math.inc" | |
364 movff opt_comm_timeout,xA+0 ; - get timeout setting in multiples of 10 ms (opt_comm_timeout: 10 .. 200 x 10 ms) | |
365 clrf xA+1 ; - ... | |
366 MOVLI .320,xB ; - multiply with 10 to get timeout in ms and 32 because tmr5 ticks 32x per ms | |
367 call mult16x16 ; - xC = xA * xB = timer ticks to go until timeout | |
368 MOVII xC,sub_b ; - multiplication result is max. 64000 | |
369 MOVLI .65535,sub_a ; - timer wraps around after 65535 | |
370 call subU16 ; - sub_c = sub_a - sub_b = start value for timer | |
371 | |
372 movlw .244 ; safety maximum value for rx_timeout_tmr5h_load (minimum timeout interval) | |
373 cpfslt sub_c+1 ; result > safety value? | |
374 movwf sub_c+1 ; YES - revert to safety value | |
375 | |
376 movf sub_c+1,W ; - keep only the upper byte as TMR5H initialization value for RX timeout | |
377 ENDIF | |
378 movwf rx_timeout_tmr5h_load ; - store for later use | |
329 return ; - done | 379 return ; - done |
330 | 380 |
331 | 381 |
382 ;----------------------------------------------------------------------------- | |
383 ; Shut-Down USB/BT Port | |
384 ; | |
332 global disable_rs232 | 385 global disable_rs232 |
333 disable_rs232: | 386 disable_rs232: |
334 clrf RCSTA1 ; disable RX | 387 clrf RCSTA1 ; disable RX |
335 clrf TXSTA1 ; disable TX | 388 clrf TXSTA1 ; disable TX |
336 bcf PORTC,6 ; switch TX pin hard to GND | 389 bcf PORTC,6 ; switch TX pin hard to GND |
337 bsf PORTE,0 ; stop comm | 390 bsf PORTE,0 ; power down BT chip |
338 bcf PORTJ,2 ; /Reset (required for very old OSTC sport) | 391 bcf PORTJ,2 ; /Reset (required for very old OSTC sport) |
339 return | 392 return |
340 | 393 |
341 | 394 |
395 ;----------------------------------------------------------------------------- | |
396 ; Wait for last Byte to be sent out of USB/BT Port | |
397 ; | |
342 global rs232_wait_tx ; ++++ do not touch WREG here! ++++ | 398 global rs232_wait_tx ; ++++ do not touch WREG here! ++++ |
343 rs232_wait_tx: | 399 rs232_wait_tx: |
344 btfss TXSTA1,TRMT ; last byte completely shifted out on TX pin? | 400 btfss TXSTA1,TRMT ; last byte completely shifted out on TX pin? |
345 bra rs232_wait_tx ; NO - wait... | 401 bra rs232_wait_tx ; NO - wait... |
346 btfss ble_available ; YES - OSTC running with Bluetooth? | 402 btfss ble_available ; YES - OSTC running with Bluetooth? |
348 btfsc NRTS ; YES - Bluetooth module also completed TX? | 404 btfsc NRTS ; YES - Bluetooth module also completed TX? |
349 bra rs232_wait_tx ; NO - wait... | 405 bra rs232_wait_tx ; NO - wait... |
350 return ; YES - done | 406 return ; YES - done |
351 | 407 |
352 | 408 |
353 ; ++++ make this code as fast as possible! ++++ | 409 ;----------------------------------------------------------------------------- |
354 global rs232_get_byte ; ++++ do not touch WREG here! ++++ | 410 ; Receive one Byte via the USB/BT Port |
355 rs232_get_byte: | 411 ; |
412 ; ++++ make this code as fast as possible! ++++ | |
413 ; ++++ do not touch WREG here! ++++ | |
414 ; | |
415 global serial_rx_single | |
416 serial_rx_single: | |
356 bcf rs232_rx_timeout ; clear timeout flag | 417 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) | 418 btfsc PIR1,RCIF ; received a data byte? (bit is set on RX completion and reset on reading RCREG1) |
358 return ; YES - done, received a byte (fast path) | 419 return ; YES - done (fast path) |
359 movff rx_timoeut_tmr5h_load,TMR5H ; - load TMR5 high with timeout value | 420 movff rx_timeout_tmr5h_load,TMR5H ; NO - load TMR5 high with timeout value |
360 clrf TMR5L ; - load TMR5 low with a zero, writing low starts the timer | 421 clrf TMR5L ; - load TMR5 low with a zero, writing low starts the timer |
361 bcf PIR5,TMR5IF ; - clear timer overflow flag | 422 bcf PIR5,TMR5IF ; - clear timer overflow flag |
362 rs232_get_byte_loop: | 423 serial_rx_single_loop: |
363 btfsc PIR1,RCIF ; received a data byte? | 424 btfsc PIR1,RCIF ; received a data byte? |
364 return ; YES - done, received a byte | 425 return ; YES - done |
365 btfss PIR5,TMR5IF ; NO - timer overflow (timeout)? | 426 btfss PIR5,TMR5IF ; NO - timer overflow (timeout)? |
366 bra rs232_get_byte_loop ; NO - continue looping | 427 bra serial_rx_single_loop ; NO - continue waiting |
367 ;bra rs232_rx_get_timeout ; YES - give up | 428 ;bra serial_rx_timeout ; YES - timeout |
368 | 429 |
369 rs232_rx_get_timeout: | 430 |
431 ;----------------------------------------------------------------------------- | |
432 ; Helper Function: Timeout in serial_rx_single / serial_tx_steam | |
433 ; | |
434 serial_rx_timeout: | |
370 bsf rs232_rx_timeout ; set timeout flag | 435 bsf rs232_rx_timeout ; set timeout flag |
371 bcf RCSTA1,CREN ; clear receiver status by toggling CREN | 436 bcf RCSTA1,CREN ; clear receiver status: disable RX, |
372 bsf RCSTA1,CREN ; ... | 437 bsf RCSTA1,CREN ; ... enable again RX |
373 return ; done, given up | 438 return ; done |
374 | 439 |
375 | 440 |
376 ;----------------------------------------------------------------------------- | 441 ;----------------------------------------------------------------------------- |
377 ; Send and Receive functions to be used through the macros | 442 ; Send and Receive Functions to be used via Macros |
378 | 443 ;----------------------------------------------------------------------------- |
379 ; send a range of 1-256 bytes from memory to the RS232 interface | 444 |
380 ; | 445 ;----------------------------------------------------------------------------- |
381 global serial_tx_ram | 446 ; Send a Range of 1-256 Bytes from Memory via the USB/BT Port |
382 serial_tx_ram: | 447 ; |
448 global serial_tx_steam | |
449 serial_tx_steam: | |
383 movwf eeprom_loop ; initialize loop counter (eeprom variable used here) | 450 movwf eeprom_loop ; initialize loop counter (eeprom variable used here) |
384 serial_tx_ram_loop: | 451 serial_tx_ram_loop: |
385 rcall rs232_wait_tx ; wait for completion of last transmit | 452 rcall rs232_wait_tx ; wait for completion of last transmit |
386 movff POSTINC2,TXREG1 ; send a byte from memory to serial | 453 movff POSTINC2,TXREG1 ; send a byte from memory to serial |
387 decfsz eeprom_loop,F ; decrement loop counter, became zero? | 454 decfsz eeprom_loop,F ; decrement loop counter, became zero? |
388 bra serial_tx_ram_loop ; NO - loop | 455 bra serial_tx_ram_loop ; NO - loop |
389 return ; YES - done | 456 return ; YES - done |
390 | 457 |
391 | 458 |
392 ; receive a range of 1-256 byte from the RS232 interface and write them to memory | 459 ;----------------------------------------------------------------------------- |
393 ; | 460 ; Receive a Range of 1-256 Byte via the USB/BT Port and write them to Memory |
394 global serial_rx_stream_ram ; ++++ make this code as fast as possible! ++++ | 461 ; ++++ make this code as fast as possible! ++++ |
395 serial_rx_stream_ram: | 462 ; |
463 global serial_rx_stream | |
464 serial_rx_stream: | |
396 movwf eeprom_loop ; initialize loop counter (eeprom variable used here) | 465 movwf eeprom_loop ; initialize loop counter (eeprom variable used here) |
397 serial_rx_stream_ram_loop_1: | 466 serial_rx_stream_loop: |
398 btfss PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1) | 467 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 | 468 bra serial_rx_stream_tmr ; NO - enter receive loop with timeout |
400 movff RCREG1,POSTINC2 ; YES - copy received byte to memory | 469 ;bra serial_rx_stream_received ; YES - copy to memory, tick counter, ... |
401 decfsz eeprom_loop,F ; - decrement loop counter, became zero? | 470 |
402 bra serial_rx_stream_ram_loop_1 ; NO - loop | 471 serial_rx_stream_received: |
403 bcf rs232_rx_timeout ; YES - clear timeout flag | 472 movff RCREG1,POSTINC2 ; copy received byte to memory |
404 return ; - all bytes received, done | 473 decfsz eeprom_loop,F ; decrement loop counter, became zero? |
405 serial_rx_stream_ram_tmr: | 474 bra serial_rx_stream_loop ; NO - await next byte |
406 movff rx_timoeut_tmr5h_load,TMR5H ; load TMR5 high with timeout value | 475 bcf rs232_rx_timeout ; YES - clear timeout flag |
407 clrf TMR5L ; load TMR5 low with a zero, writing low starts the timer | 476 return ; - all bytes received, done |
477 | |
478 serial_rx_stream_tmr: | |
479 movff rx_timeout_tmr5h_load,TMR5H ; load TMR5 high with timeout value | |
480 clrf TMR5L ; load TMR5 low with a zero, writing to low starts the timer | |
408 bcf PIR5,TMR5IF ; clear timer overflow flag | 481 bcf PIR5,TMR5IF ; clear timer overflow flag |
409 serial_rx_stream_ram_loop_2a: | 482 serial_rx_stream_tmr_loop: |
410 clrf TMR5L ; restart timer (see above) | 483 btfsc PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1) |
411 serial_rx_stream_ram_loop_2b: | 484 bra serial_rx_stream_received ; YES - copy to memory, tick counter, ... |
412 btfss PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1) | 485 btfss PIR5,TMR5IF ; NO - timer overflow (timeout)? |
413 bra serial_rx_stream_ram_chk ; NO - check timeout | 486 bra serial_rx_stream_tmr_loop ; NO - continue waiting |
414 movff RCREG1,POSTINC2 ; YES - copy received byte to memory | 487 bra serial_rx_timeout ; YES - timeout |
415 decfsz eeprom_loop,F ; - decrement loop counter, became zero? | 488 |
416 bra serial_rx_stream_ram_loop_2a; NO - loop | 489 ;----------------------------------------------------------------------------- |
417 bcf rs232_rx_timeout ; YES - clear timeout flag | 490 |
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 | |
423 | |
424 ;----------------------------------------------------------------------------- | |
425 END | 491 END |