0
|
1 ;=============================================================================
|
|
2 ;
|
634
|
3 ; File eeprom_rs232.asm * combined next generation V3.09.4n
|
0
|
4 ;
|
|
5 ; Internal EEPROM, RS232
|
|
6 ;
|
654
|
7 ; Copyright (c) 2011, JD Gascuel, heinrichs weikamp gmbh, all right reserved.
|
0
|
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
|
634
|
24
|
|
25 ;=============================================================================
|
|
26 eeprom CODE
|
|
27 ;=============================================================================
|
|
28
|
623
|
29 ;-----------------------------------------------------------------------------
|
631
|
30 ;
|
634
|
31 ; EEPROM Functions - for EEPROM Macros and Memory Map, see eeprom_rs232.inc
|
631
|
32 ;
|
|
33 ;-----------------------------------------------------------------------------
|
623
|
34
|
582
|
35
|
623
|
36 ;-----------------------------------------------------------------------------
|
634
|
37 ; Read from internal EEPROM
|
623
|
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:
|
634
|
45 bcf EECON1,EEPGD ; access data EEPROM
|
|
46 bcf EECON1,CFGS ; ...
|
|
47 bsf EECON1,RD ; initiate reading
|
|
48 return ; done
|
|
49
|
0
|
50
|
631
|
51 ;-----------------------------------------------------------------------------
|
634
|
52 ; Write into internal EEPROM
|
623
|
53 ;
|
604
|
54 ; Input: EEADRH:EEADR = EEPROM address
|
|
55 ; EEDATA = byte to write
|
|
56 ; Trashed: WREG
|
623
|
57 ;
|
604
|
58 global write_eeprom
|
|
59 write_eeprom:
|
634
|
60 bcf EECON1,EEPGD ; access data EEPROM
|
|
61 bcf EECON1,CFGS ; ...
|
|
62 bsf EECON1,WREN ; enable writing
|
|
63 bcf INTCON,GIE ; disable interrupts
|
631
|
64 movlw 0x55 ; unlock sequence
|
|
65 movwf EECON2 ; ...
|
|
66 movlw 0xAA ; ...
|
|
67 movwf EECON2 ; ...
|
|
68 bsf EECON1,WR ; start write operation
|
|
69 write_eeprom_loop:
|
|
70 btfsc EECON1,WR ; write completed?
|
634
|
71 bra write_eeprom_loop ; NO - loop waiting
|
650
|
72 btfsc EECON1,WRERR ; All ok?
|
|
73 rcall write_eeprom_error ; NO, something failed.
|
634
|
74 bcf EECON1,WREN ; YES - disable writing
|
|
75 bsf INTCON,GIE ; - re-enable interrupts
|
|
76 return ; - done
|
650
|
77 write_eeprom_error:
|
|
78 bsf eeprom_write_error_flag ; Set error flag
|
|
79 ; Try again (once)
|
|
80 movlw 0x55 ; unlock sequence
|
|
81 movwf EECON2 ; ...
|
|
82 movlw 0xAA ; ...
|
|
83 movwf EECON2 ; ...
|
|
84 bsf EECON1,WR ; start write operation
|
|
85 write_eeprom_loop2:
|
|
86 btfsc EECON1,WR ; write completed?
|
|
87 bra write_eeprom_loop2 ; NO - loop waiting
|
|
88 return
|
|
89
|
631
|
90
|
|
91 ;-----------------------------------------------------------------------------
|
634
|
92 ; EEPROM read and write Functions to be used via Macros
|
631
|
93 ;
|
|
94 global eeprom_read_common
|
|
95 eeprom_read_common:
|
|
96 movwf eeprom_loop ; initialize loop counter
|
|
97 eeprom_read_common_loop:
|
|
98 rcall read_eeprom ; execute read
|
|
99 movff EEDATA,POSTINC1 ; copy byte from EEPROM data register to memory
|
|
100 incf EEADR,F ; advance to next EEPROM cell
|
|
101 decfsz eeprom_loop,F ; decrement loop counter, all done?
|
|
102 bra eeprom_read_common_loop ; NO - loop
|
|
103 return ; YES - done
|
604
|
104
|
631
|
105 global eeprom_write_common
|
|
106 eeprom_write_common:
|
|
107 movwf eeprom_loop ; initialize loop counter
|
|
108 eeprom_write_common_loop:
|
|
109 movff POSTINC1,EEDATA ; copy byte from memory to EEPROM data register
|
|
110 rcall write_eeprom ; execute write
|
|
111 incf EEADR,F ; advance to next EEPROM cell
|
|
112 decfsz eeprom_loop,F ; decrement loop counter, all done?
|
|
113 bra eeprom_write_common_loop ; NO - loop
|
|
114 return ; YES - done
|
|
115
|
634
|
116
|
631
|
117 ;-----------------------------------------------------------------------------
|
634
|
118 ; Read OSTC Serial Number
|
631
|
119 ;
|
|
120 global eeprom_serial_number_read
|
|
121 eeprom_serial_number_read:
|
634
|
122 EEPROM_II_READ eeprom_ostc_serial,mpr ; read serial number
|
|
123 return ; done
|
|
124
|
604
|
125
|
631
|
126 ;-----------------------------------------------------------------------------
|
634
|
127 ; Read and Write Dive Number Offset
|
631
|
128 ;
|
|
129 global eeprom_log_offset_read
|
|
130 eeprom_log_offset_read:
|
634
|
131 EEPROM_II_READ eeprom_log_offset,mpr ; read log offset
|
|
132 return ; done
|
631
|
133
|
|
134 global eeprom_log_offset_write
|
|
135 eeprom_log_offset_write:
|
634
|
136 EEPROM_II_WRITE mpr,eeprom_log_offset ; write log-offset
|
|
137 return ; done
|
631
|
138
|
|
139
|
|
140 ;-----------------------------------------------------------------------------
|
634
|
141 ; Read and Write total Number of Dives
|
631
|
142 ;
|
|
143 global eeprom_total_dives_read
|
|
144 eeprom_total_dives_read:
|
634
|
145 EEPROM_II_READ eeprom_num_dives,mpr ; read total dives
|
|
146 return ; done
|
631
|
147
|
|
148 global eeprom_total_dives_write
|
|
149 eeprom_total_dives_write:
|
634
|
150 EEPROM_II_WRITE mpr,eeprom_num_dives ; write total dives
|
|
151 return ; done
|
604
|
152
|
631
|
153
|
|
154 ;-----------------------------------------------------------------------------
|
634
|
155 ; Read and Write the Battery Type and Gauge Reading
|
631
|
156 ;
|
|
157 global eeprom_battery_gauge_read
|
|
158 eeprom_battery_gauge_read:
|
|
159 ; retrieve battery gauge from EEPROM 0x07-0x0C
|
|
160 bsf block_battery_gauge ; suspend ISR from accessing the battery gauge
|
|
161 EEPROM_CC_READ eeprom_battery_type, battery_type ; 1 byte read from EEPROM
|
|
162 EEPROM_RR_READ eeprom_battery_gauge,battery_gauge,.6 ; 6 byte read from EEPROM
|
654
|
163 EEPROM_RR_READ eeprom_charge_cycles,charge_cycles,.2 ; 2 byte read from EEPROM
|
631
|
164 bcf block_battery_gauge ; allow ISR to access the battery gauge again
|
634
|
165 return ; done
|
631
|
166
|
|
167 global eeprom_battery_gauge_write
|
|
168 eeprom_battery_gauge_write:
|
|
169 bsf block_battery_gauge ; suspend ISR from accessing the battery gauge
|
|
170 EEPROM_CC_WRITE battery_type, eeprom_battery_type ; 1 byte write to EEPROM
|
|
171 update_battery_gauge:
|
|
172 EEPROM_RR_WRITE battery_gauge,eeprom_battery_gauge,.6 ; 6 byte write to EEPROM
|
654
|
173 EEPROM_RR_WRITE charge_cycles,eeprom_charge_cycles,.2 ; 2 byte write to EEPROM
|
631
|
174 bcf block_battery_gauge ; allow ISR to access the battery gauge again
|
634
|
175 return ; done
|
631
|
176
|
|
177
|
|
178 ;-----------------------------------------------------------------------------
|
634
|
179 ; Memorize the Checksum of the Firmware in the update Storage
|
|
180 ;
|
|
181 global eeprom_memorize_fw_checksum
|
|
182 eeprom_memorize_fw_checksum:
|
|
183 EXT_FLASH_ADDR 0x3E000D ; address firmware ID at 0x3E000D
|
|
184 FLASH_CW_READ_0x40 ; read firmware ID to WREG
|
|
185 movff WREG,buffer+.5 ; append firmware ID to checksum
|
|
186 EEPROM_RR_WRITE buffer,eeprom_fw_chksum_current,.6 ; do a 6 byte write to EEPROM
|
|
187 return ; done
|
|
188
|
|
189
|
|
190 ;-----------------------------------------------------------------------------
|
|
191 ; Read and Write the Deco Status
|
631
|
192 ;
|
|
193 global eeprom_deco_data_read
|
|
194 eeprom_deco_data_read:
|
|
195
|
634
|
196 btfsc RCON,POR ; was there a power outage?
|
631
|
197 bra eeprom_deco_data_read_1 ; NO - RTC is up-to-date
|
|
198
|
|
199 EEPROM_RR_READ eeprom_deco_data_timestamp,rtc_latched_year,.6 ; 6 byte read from EEPROM
|
|
200 call rtc_set_rtc ; recover RTC to last known time & date
|
|
201
|
|
202 eeprom_deco_data_read_1:
|
|
203
|
|
204 ; restore surface interval
|
|
205 EEPROM_II_READ eeprom_deco_data_surfinterval,mpr ; 2 byte read from EEPROM
|
|
206 SMOVII mpr,surface_interval_mins ; ISR-safe copy of surface interval
|
|
207
|
|
208 ; bank 3: restore desaturation status
|
|
209 EEPROM_RR_READ eeprom_deco_data_bank3,0x300,.9 ; 9 byte read from EEPROM
|
|
210
|
|
211 ; bank 5: restore CNS
|
|
212 EEPROM_RR_READ eeprom_deco_data_bank5,0x500,.4 ; 4 byte read from EEPROM
|
|
213
|
|
214 ; bank 7: restore tissue pressures
|
|
215 EEPROM_RR_READ eeprom_deco_data_bank7,0x700,.128 ; 128 byte read from EEPROM
|
|
216
|
|
217 return ; done
|
|
218
|
|
219
|
|
220 global eeprom_deco_data_write
|
|
221 eeprom_deco_data_write:
|
|
222
|
|
223 ; invalidate current data in vault
|
|
224 movlw DECO_DATA_INVALID_TOKEN ; deco data invalid token
|
|
225 EEPROM_CC_WRITE WREG,eeprom_deco_data_validity ; 1 byte write to EEPROM
|
|
226
|
|
227 ; store vault version
|
|
228 movlw eeprom_vault_version ; deco data format version
|
|
229 EEPROM_CC_WRITE WREG,eeprom_deco_data_version ; 1 byte write to EEPROM
|
|
230
|
|
231 ; store date/time
|
|
232 SMOVSS rtc_year,rtc_latched_year ; ISR-safe 6 byte copy of date and time
|
|
233 EEPROM_RR_WRITE rtc_latched_year,eeprom_deco_data_timestamp,.6 ; 6 byte write to EEPROM
|
|
234
|
|
235 ; store surface interval
|
|
236 SMOVII surface_interval_mins,mpr ; ISR-safe copy of surface interval
|
|
237 EEPROM_II_WRITE mpr,eeprom_deco_data_surfinterval ; 2 byte write to EEPROM
|
|
238
|
|
239 ; bank 3: store desaturation status
|
|
240 EEPROM_RR_WRITE 0x300,eeprom_deco_data_bank3,.9 ; 9 byte write to EEPROM
|
|
241
|
|
242 ; bank 5: store CNS
|
|
243 EEPROM_RR_WRITE 0x500,eeprom_deco_data_bank5,.4 ; 4 byte write to EEPROM
|
|
244
|
|
245 ; bank 7: store tissue pressures
|
|
246 EEPROM_RR_WRITE 0x700,eeprom_deco_data_bank7,.128 ; 128 byte write to EEPROM
|
|
247
|
|
248 ; indicate new valid data in vault
|
|
249 movlw DECO_DATA_VALID_TOKEN ; deco data valid token
|
|
250 EEPROM_CC_WRITE WREG,eeprom_deco_data_validity ; 1 byte write to EEPROM
|
|
251
|
|
252 return ; done
|
|
253
|
|
254
|
|
255 ;=============================================================================
|
634
|
256 rs232 CODE
|
623
|
257 ;=============================================================================
|
|
258
|
634
|
259 ;-----------------------------------------------------------------------------
|
|
260 ;
|
|
261 ; RS232 Functions
|
|
262 ;
|
|
263 ;-----------------------------------------------------------------------------
|
0
|
264
|
|
265
|
634
|
266 ;-----------------------------------------------------------------------------
|
|
267 ; Switch-On the IR/S8 Port
|
|
268 ;
|
|
269 global enable_ir_s8_analog
|
|
270 enable_ir_s8_analog:
|
623
|
271 ;initialize serial port2 (TRISG2)
|
634
|
272 btfsc ext_input_s8_ana ; do we have an S8/analog input?
|
|
273 bra enable_s8_analog ; YES - enable S8/analog input
|
|
274 ;bra enable_ir ; NO - enable IR digital input
|
0
|
275
|
634
|
276 enable_ir:
|
|
277 banksel BAUDCON2 ; select bank for IO register access
|
|
278 movlw b'00100000' ; speed generator configuration: BRG16=0, inverted for IR
|
|
279 movwf BAUDCON2 ; ...
|
|
280 movlw b'00100000' ; TX configuration: BRGH=0, SYNC=0
|
|
281 movwf TXSTA2 ; ...
|
|
282 movlw .102 ; speed configuration: SPBRGH:SPBRG = .102 : 2403 BAUD @ 16 MHz
|
|
283 movwf SPBRG2 ; ...
|
|
284 clrf SPBRGH2 ; ...
|
|
285 movlw b'10010000' ; RX configuration
|
|
286 movwf RCSTA2 ; ...
|
|
287 banksel common ; back to bank common
|
|
288 bsf ir_power ; power-up IR
|
|
289 btfss ir_power ; power-up confirmed?
|
|
290 bra $-6 ; NO - loop and wait
|
|
291 bsf PIE3,RC2IE ; enable RC2 INT
|
|
292 return ; done
|
|
293
|
|
294 enable_s8_analog:
|
629
|
295 banksel TXSTA2 ; select bank for IO register access
|
631
|
296 clrf TXSTA2 ; reset UART 2 TX function
|
|
297 clrf RCSTA2 ; reset UART 2 RX function
|
623
|
298 banksel common ; back to bank common
|
631
|
299
|
634
|
300 bsf mcp_power ; power-up instrumentation amp (used by S8 and analog input)
|
631
|
301 btfss mcp_power ; power-up completed?
|
|
302 bra $-4 ; NO - loop
|
113
|
303
|
634
|
304 ; branch according to S8 / analog selection
|
631
|
305 TSTOSS opt_s8_mode ; =0: analog, =1: digital RS232
|
634
|
306 bra enable_analog ; -> analog
|
631
|
307
|
|
308 ; configure S8 digital interface
|
|
309 bcf s8_npower ; power S8 HUD (inverted via P-MOS transistor)
|
623
|
310 WAITMS d'30' ; NO - wait 30 ms
|
631
|
311 banksel BAUDCON2 ; select bank for IO register access
|
634
|
312 movlw b'00000000' ; speed generator configuration: BRG16=0, normal for S8
|
|
313 movwf BAUDCON2 ; ...
|
|
314 movlw b'00100000' ; TX configuration: BRGH=0, SYNC=0
|
|
315 movwf TXSTA2 ; ...
|
|
316 movlw .25 ; speed configuration: SPBRGH:SPBRG = .25 : 9615 BAUD @ 16 MHz
|
|
317 movwf SPBRG2 ; ...
|
|
318 movlw b'10010000' ; RX configuration
|
|
319 movwf RCSTA2 ; ...
|
623
|
320 banksel common ; back to bank common
|
631
|
321 bsf PIE3,RC2IE ; enable RC2 INT
|
582
|
322 return
|
113
|
323
|
634
|
324 enable_analog:
|
631
|
325 ; S8 analog interface
|
|
326 bcf PIE3,RC2IE ; disable RC2 INT
|
634
|
327 bsf s8_npower ; power-down S8 digital interface
|
|
328 return ; done
|
631
|
329
|
|
330
|
634
|
331 ;-----------------------------------------------------------------------------
|
|
332 ; Shut-Down the IR/S8 Port
|
|
333 ;
|
|
334 global disable_ir_s8_analog
|
|
335 disable_ir_s8_analog:
|
631
|
336 banksel TXSTA2 ; select bank for IO register access
|
634
|
337 clrf TXSTA2 ; shut down TX function
|
|
338 clrf RCSTA2 ; shut down RX function
|
|
339 banksel common ; back to bank common
|
|
340 bcf PIE3,RC2IE ; disable RC2 INT
|
|
341 bcf ir_power ; power down IR receiver
|
|
342 bcf mcp_power ; power-down instrumentation amp
|
|
343 bsf s8_npower ; power-down S8 digital interface
|
|
344 return ; done
|
|
345
|
|
346
|
|
347 ;-----------------------------------------------------------------------------
|
|
348 ; Send Byte in WREG via the IR/S8 Port
|
|
349 ;
|
|
350 global ir_s8_tx_single
|
|
351 ir_s8_tx_single:
|
|
352 banksel TXSTA2 ; UART 2 is outside of the access RAM
|
|
353 movwf TXREG2 ; transmit byte
|
|
354 ir_s8_tx_single_loop:
|
|
355 btfss TXSTA2,TRMT ; TX completed?
|
|
356 bra ir_s8_tx_single_loop ; NO - wait...
|
|
357 banksel common ; YES - back to bank common
|
631
|
358 return ; - done
|
|
359
|
634
|
360
|
631
|
361 ;-----------------------------------------------------------------------------
|
634
|
362 ; Switch-On USB/BT Port
|
|
363 ;
|
0
|
364 global enable_rs232
|
|
365 enable_rs232:
|
623
|
366 call request_speed_normal ; request CPU speed change to normal speed
|
|
367 enable_rs232_1:
|
|
368 btfss speed_is_normal ; speed = normal?
|
631
|
369 bra enable_rs232_1 ; NO - loop waiting for ISR to have adjusted the speed
|
640
|
370 bsf TRISC,7
|
654
|
371 bcf ble_npower ; YES - switch port to comm
|
631
|
372 bsf PORTJ,2 ; - /Reset (required for very old OSTC sport)
|
|
373 movlw b'00100100' ; - TX configuration: TX enabled, async, high speed
|
|
374 movwf TXSTA1 ; - ...
|
|
375 movlw b'10010000' ; - RX configuration: port enabled, RX enabled
|
|
376 movwf RCSTA1 ; - ...
|
634
|
377 IFNDEF _comm_debug
|
|
378 movlw HIGH(.65536-rx_timeout*.32) ; - define TMR5H initialization value for RX timeout (rx_timeout defined in hwos.inc)
|
|
379 ELSE
|
|
380 include "math.inc"
|
|
381 movff opt_comm_timeout,xA+0 ; - get timeout setting in multiples of 10 ms (opt_comm_timeout: 10 .. 200 x 10 ms)
|
|
382 clrf xA+1 ; - ...
|
|
383 MOVLI .320,xB ; - multiply with 10 to get timeout in ms and 32 because tmr5 ticks 32x per ms
|
|
384 call mult16x16 ; - xC = xA * xB = timer ticks to go until timeout
|
|
385 MOVII xC,sub_b ; - multiplication result is max. 64000
|
|
386 MOVLI .65535,sub_a ; - timer wraps around after 65535
|
|
387 call subU16 ; - sub_c = sub_a - sub_b = start value for timer
|
|
388
|
|
389 movlw .244 ; safety maximum value for rx_timeout_tmr5h_load (minimum timeout interval)
|
|
390 cpfslt sub_c+1 ; result > safety value?
|
|
391 movwf sub_c+1 ; YES - revert to safety value
|
|
392
|
|
393 movf sub_c+1,W ; - keep only the upper byte as TMR5H initialization value for RX timeout
|
|
394 ENDIF
|
|
395 movwf rx_timeout_tmr5h_load ; - store for later use
|
631
|
396 return ; - done
|
0
|
397
|
582
|
398
|
634
|
399 ;-----------------------------------------------------------------------------
|
|
400 ; Shut-Down USB/BT Port
|
|
401 ;
|
0
|
402 global disable_rs232
|
|
403 disable_rs232:
|
631
|
404 clrf RCSTA1 ; disable RX
|
|
405 clrf TXSTA1 ; disable TX
|
|
406 bcf PORTC,6 ; switch TX pin hard to GND
|
654
|
407 bsf ble_npower ; power down BT chip
|
631
|
408 bcf PORTJ,2 ; /Reset (required for very old OSTC sport)
|
640
|
409 bcf TRISC,7
|
|
410 bcf PORTC,7 ; switch RX pin hard to GND
|
582
|
411 return
|
|
412
|
|
413
|
634
|
414 ;-----------------------------------------------------------------------------
|
|
415 ; Wait for last Byte to be sent out of USB/BT Port
|
|
416 ;
|
631
|
417 global rs232_wait_tx ; ++++ do not touch WREG here! ++++
|
|
418 rs232_wait_tx:
|
|
419 btfss TXSTA1,TRMT ; last byte completely shifted out on TX pin?
|
|
420 bra rs232_wait_tx ; NO - wait...
|
|
421 btfss ble_available ; YES - OSTC running with Bluetooth?
|
|
422 return ; NO - done
|
|
423 btfsc NRTS ; YES - Bluetooth module also completed TX?
|
|
424 bra rs232_wait_tx ; NO - wait...
|
|
425 return ; YES - done
|
623
|
426
|
|
427
|
634
|
428 ;-----------------------------------------------------------------------------
|
|
429 ; Receive one Byte via the USB/BT Port
|
|
430 ;
|
|
431 ; ++++ make this code as fast as possible! ++++
|
|
432 ; ++++ do not touch WREG here! ++++
|
|
433 ;
|
|
434 global serial_rx_single
|
|
435 serial_rx_single:
|
631
|
436 bcf rs232_rx_timeout ; clear timeout flag
|
634
|
437 btfsc PIR1,RCIF ; received a data byte? (bit is set on RX completion and reset on reading RCREG1)
|
|
438 return ; YES - done (fast path)
|
|
439 movff rx_timeout_tmr5h_load,TMR5H ; NO - load TMR5 high with timeout value
|
631
|
440 clrf TMR5L ; - load TMR5 low with a zero, writing low starts the timer
|
|
441 bcf PIR5,TMR5IF ; - clear timer overflow flag
|
634
|
442 serial_rx_single_loop:
|
631
|
443 btfsc PIR1,RCIF ; received a data byte?
|
634
|
444 return ; YES - done
|
631
|
445 btfss PIR5,TMR5IF ; NO - timer overflow (timeout)?
|
634
|
446 bra serial_rx_single_loop ; NO - continue waiting
|
|
447 ;bra serial_rx_timeout ; YES - timeout
|
623
|
448
|
582
|
449
|
631
|
450 ;-----------------------------------------------------------------------------
|
634
|
451 ; Helper Function: Timeout in serial_rx_single / serial_tx_steam
|
|
452 ;
|
|
453 serial_rx_timeout:
|
|
454 bsf rs232_rx_timeout ; set timeout flag
|
|
455 bcf RCSTA1,CREN ; clear receiver status: disable RX,
|
|
456 bsf RCSTA1,CREN ; ... enable again RX
|
|
457 return ; done
|
|
458
|
623
|
459
|
634
|
460 ;-----------------------------------------------------------------------------
|
|
461 ; Send and Receive Functions to be used via Macros
|
|
462 ;-----------------------------------------------------------------------------
|
|
463
|
|
464 ;-----------------------------------------------------------------------------
|
|
465 ; Send a Range of 1-256 Bytes from Memory via the USB/BT Port
|
631
|
466 ;
|
634
|
467 global serial_tx_steam
|
|
468 serial_tx_steam:
|
631
|
469 movwf eeprom_loop ; initialize loop counter (eeprom variable used here)
|
|
470 serial_tx_ram_loop:
|
|
471 rcall rs232_wait_tx ; wait for completion of last transmit
|
|
472 movff POSTINC2,TXREG1 ; send a byte from memory to serial
|
|
473 decfsz eeprom_loop,F ; decrement loop counter, became zero?
|
|
474 bra serial_tx_ram_loop ; NO - loop
|
|
475 return ; YES - done
|
582
|
476
|
623
|
477
|
634
|
478 ;-----------------------------------------------------------------------------
|
|
479 ; Receive a Range of 1-256 Byte via the USB/BT Port and write them to Memory
|
|
480 ; ++++ make this code as fast as possible! ++++
|
631
|
481 ;
|
634
|
482 global serial_rx_stream
|
|
483 serial_rx_stream:
|
631
|
484 movwf eeprom_loop ; initialize loop counter (eeprom variable used here)
|
634
|
485 serial_rx_stream_loop:
|
631
|
486 btfss PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1)
|
634
|
487 bra serial_rx_stream_tmr ; NO - enter receive loop with timeout
|
|
488 ;bra serial_rx_stream_received ; YES - copy to memory, tick counter, ...
|
|
489
|
|
490 serial_rx_stream_received:
|
|
491 movff RCREG1,POSTINC2 ; copy received byte to memory
|
|
492 decfsz eeprom_loop,F ; decrement loop counter, became zero?
|
|
493 bra serial_rx_stream_loop ; NO - await next byte
|
|
494 bcf rs232_rx_timeout ; YES - clear timeout flag
|
|
495 return ; - all bytes received, done
|
|
496
|
|
497 serial_rx_stream_tmr:
|
|
498 movff rx_timeout_tmr5h_load,TMR5H ; load TMR5 high with timeout value
|
|
499 clrf TMR5L ; load TMR5 low with a zero, writing to low starts the timer
|
631
|
500 bcf PIR5,TMR5IF ; clear timer overflow flag
|
634
|
501 serial_rx_stream_tmr_loop:
|
|
502 btfsc PIR1,RCIF ; received a data byte? (bit is set on RX complete and reset on reading RCREG1)
|
|
503 bra serial_rx_stream_received ; YES - copy to memory, tick counter, ...
|
|
504 btfss PIR5,TMR5IF ; NO - timer overflow (timeout)?
|
|
505 bra serial_rx_stream_tmr_loop ; NO - continue waiting
|
|
506 bra serial_rx_timeout ; YES - timeout
|
623
|
507
|
631
|
508 ;-----------------------------------------------------------------------------
|
634
|
509
|
648
|
510 ;-----------------------------------------------------------------------------
|
|
511 ; Add new services and characteristics to new BLE (type 2) module
|
|
512 ;-----------------------------------------------------------------------------
|
|
513 global ble2_configure
|
|
514 ble2_configure:
|
|
515 rcall enable_rs232
|
|
516 bcf NCTS ; Clear to send
|
|
517 call wait_1s
|
|
518 call wait_1s
|
650
|
519 call wait_1s
|
|
520 call wait_1s
|
648
|
521 bcf PORTB,6
|
650
|
522 nop
|
648
|
523 bsf PORTB,6 ; rising edge -> Command mode
|
|
524
|
|
525 ; point to config table
|
|
526 movlw LOW ble_AT1
|
|
527 movwf TBLPTRL
|
|
528 movlw HIGH ble_AT1
|
|
529 movwf TBLPTRH
|
|
530 movlw UPPER ble_AT1
|
|
531 movwf TBLPTRU
|
|
532 rcall ble_init_loop
|
|
533
|
|
534 rcall disable_rs232
|
|
535 bcf PORTB,6 ; keep low for min. current consumption
|
|
536 return ; done.
|
|
537
|
|
538 ble_init_loop:
|
|
539 TBLRD*+
|
|
540 movlw 0xFF ; end
|
|
541 cpfseq TABLAT
|
|
542 bra ble_init_loop1 ; not end
|
|
543 ; quit (return)
|
|
544 return ; done.
|
|
545 ble_init_loop1:
|
|
546 movlw 0xFE ; WAIT 20ms
|
|
547 cpfseq TABLAT
|
|
548 bra ble_init_loop2 ; not wait 20ms
|
|
549 WAITMS d'20'
|
|
550 bra ble_init_loop
|
|
551 ble_init_loop2:
|
|
552 movlw 0xFD ; WAIT 1s
|
|
553 cpfseq TABLAT
|
|
554 bra ble_init_loop3 ; not wait 1s
|
|
555 call wait_1s
|
|
556 bra ble_init_loop
|
|
557 ble_init_loop3:
|
|
558 movf TABLAT,W
|
|
559 SERIAL_CC_SEND WREG
|
|
560 bra ble_init_loop
|
|
561
|
|
562 ble_AT1: ; config table
|
|
563 ; 0xFF at the end
|
|
564 ; 0xFE: 20ms delay
|
|
565 ; 0xFD: 1s delay
|
|
566 ; .13: cr character
|
650
|
567 db 0xFD,0xFD,0xFD,0xFD ; Wait 4 seconds
|
648
|
568 db "AT+UDSC=0,0",.13,0xFE,0xFE ; Disable SPP Server on ID0 (and wait 40ms)
|
|
569 db "AT+UDSC=0,3",.13,0xFE,0xFE ; SPP Server on ID0 (and wait 40ms)
|
|
570 db "AT+UDSC=1,0",.13,0xFE,0xFE ; Disable SPS Server on ID1 (and wait 40ms)
|
|
571 db "AT+UDSC=1,6",.13,0xFE,0xFE ; SPS Server on ID1 (and wait 40ms)
|
|
572 db "AT&W",.13,0xFE ; write settings into eeprom (and wait 20ms)
|
|
573 db "AT+CPWROFF",.13,0xFD,0xFD,0xFF ; save and reboot (and wait 2 seconds)
|
|
574
|
|
575
|
582
|
576 END
|