comparison src/eeprom_rs232.asm @ 0:11d4fc797f74

init
author heinrichsweikamp
date Wed, 24 Apr 2013 19:22:45 +0200
parents
children f3062a611eef
comparison
equal deleted inserted replaced
-1:000000000000 0:11d4fc797f74
1 ;=============================================================================
2 ;
3 ; File eeprom_rs232.asm
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
12 #include "ostc3.inc"
13 #include "start.inc"
14 #include "tft.inc"
15 #include "wait.inc"
16 #include "strings.inc"
17 #include "convert.inc"
18
19 ;=============================================================================
20 eeprom code 0xF00000+0x10
21 ; Skip SERIAL number. Should not be overwritten.
22 global eeprom_serial_save, eeprom_opt_backup
23 eeprom_serial_save res 2
24 eeprom_opt_backup res 0x3E
25
26 ;=============================================================================
27 basic CODE
28
29 global write_int_eeprom_1
30 write_int_eeprom_1:
31 movwf EEADR
32 bra write_eeprom ; writes and "returns" after write
33
34 global read_int_eeprom_1
35 read_int_eeprom_1:
36 movwf EEADR
37 bra read_eeprom ; reads and "returns" after write
38
39 ;=============================================================================
40 ; reads from internal eeprom
41 ; Input: EEADRH:EEADR = EEPROM address.
42 ; Output: EEDATA.
43 ; Trashed: NONE.
44 global read_eeprom
45 read_eeprom:
46 bcf EECON1,EEPGD
47 bcf EECON1,CFGS
48 bsf EECON1,RD
49 return
50
51 ;=============================================================================
52 ; writes into internal eeprom
53 ; Input: EEADRH:EEADR = EEPROM address.
54 ; EEDATA = byte to write.
55 ; Trashed: WREG.
56 global write_eeprom
57 write_eeprom:
58 bcf EECON1,EEPGD
59 bcf EECON1,CFGS
60 bsf EECON1,WREN
61
62 bcf INTCON,GIE ; even the RTC will be delayed for the next 5 instructions...
63 movlw 0x55
64 movwf EECON2
65 movlw 0xAA
66 movwf EECON2
67 bsf EECON1,WR
68 bsf INTCON,GIE ; ...but the flag for the ISR routines were still set, so they will interrupt now!
69
70 write_eep2:
71 btfsc EECON1,WR
72 bra write_eep2 ; wait about 4ms...
73 bcf EECON1,WREN
74 return
75
76 global disable_ir
77 disable_ir:
78 banksel TXSTA2
79 clrf TXSTA2
80 clrf RCSTA2
81 banksel common
82 bcf ir_power ; IR off
83 return
84
85 global enable_ir
86 enable_ir:
87 ;init serial port2 (TRISG2)
88 banksel TXSTA2
89 movlw b'00100000' ; BRGH=0, SYNC=0
90 movwf TXSTA2
91 movlw .102 ; SPBRGH:SPBRG = .102 : 2403 BAUD @ 16MHz
92 movwf SPBRG2
93 clrf SPBRGH2
94 movlw b'10010000'
95 movwf RCSTA2
96 banksel common
97 bsf ir_power ; Power-up IR
98 btfss ir_power
99 bra $-6
100 return
101
102 ;=============================================================================
103 global enable_rs232
104 enable_rs232:
105 bcf TRISC,6 ; Output
106 bsf TRISC,7 ; Input
107 call speed_normal ; 16MHz
108 enable_rs232_2:
109 movlw T2CON_NORMAL
110 cpfseq T2CON
111 bra enable_rs232_2 ; Wait until speed is normal
112 ;init serial port1 (TRISC6/7)
113 clrf RCSTA1
114 clrf TXSTA1
115 movlw b'00001000' ; BRG16=1
116 movwf BAUDCON1
117 movlw b'00100100' ; BRGH=1, SYNC=0
118 movwf TXSTA1
119 movlw .34 ; SPBRGH:SPBRG = .34 : 114285 BAUD @ 16MHz (+0,79% Error to 115200 BAUD)
120 movwf SPBRG1
121 clrf SPBRGH1
122 movlw b'10010000'
123 movwf RCSTA1
124 return
125
126 global disable_rs232
127 disable_rs232:
128 clrf RCSTA1
129 clrf TXSTA1 ; UART disable
130 bsf TRISC,6 ; Input
131 bsf TRISC,7 ; Input
132 return
133
134 global rs232_wait_tx
135 rs232_wait_tx:
136 ; btfss RCSTA1,SPEN ; Transmitter active?
137 ; return ; No, return!
138
139 btfsc TXSTA1,TRMT ; Transmit Shift Register empty?
140 return ; Yes, return!
141
142 btfss TXSTA,TRMT ; RS232 Busy?
143 bra rs232_wait_tx ; yes, wait...
144 return ; Done.
145
146 global rs232_get_byte
147 rs232_get_byte:
148 bcf PIR1,RCIF ; clear flag
149 bcf rs232_recieve_overflow ; clear flag
150 clrf uart1_temp
151 clrf uart2_temp
152 rs232_get_byte2:
153 btfsc PIR1,RCIF ; data arrived?
154 return
155 ; bra rs232_get_byte3
156
157 decfsz uart2_temp,F
158 bra rs232_get_byte2
159 decfsz uart1_temp,F
160 bra rs232_get_byte2
161 ; timeout occoured (about 20ms)
162 bsf rs232_recieve_overflow ; set flag
163 ;rs232_get_byte3:
164 bcf RCSTA1,CREN ; Clear receiver status
165 bsf RCSTA1,CREN
166 return ; and return anyway
167
168 END