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