summaryrefslogtreecommitdiff
path: root/software/ihu/rtc.inc
blob: 79b4bdc6b0e66f3d44a98c4ce784761dbf49cc7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  ;;
  ;;  mur.sat
  ;;
  ;;  Somewhen in the year 2011, mur.at will have a nano satellite launched
  ;;  into a low earth orbit (310 km above the surface of our planet). The
  ;;  satellite itself is a TubeSat personal satellite kit, developed and
  ;;  launched by interorbital systems. mur.sat is a joint venture of mur.at,
  ;;  ESC im Labor and realraum.
  ;;
  ;;  Please visit the project hompage at sat.mur.at for further information.
  ;; 
  ;;
  ;;  Copyright (C) 2011 Christian Pointner <equinox@mur.at>
  ;;                         
  ;;  This file is part of mur.sat.
  ;;
  ;;  mur.sat is free software: you can redistribute it and/or modify
  ;;  it under the terms of the GNU General Public License as published by
  ;;  the Free Software Foundation, either version 3 of the License, or
  ;;  any later version.
  ;;
  ;;  mur.sat is distributed in the hope that it will be useful,
  ;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
  ;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  ;;  GNU General Public License for more details.
  ;;
  ;;  You should have received a copy of the GNU General Public License
  ;;  along with mur.sat. If not, see <http://www.gnu.org/licenses/>.
  ;;
  ;; -------------------------------------
  ;; DEFINES
  ;;   constants
RTC_ADDR_W   EQU b'11010000'
RTC_ADDR_R   EQU b'11010001'
RTC_REG_SEC  EQU H'00'
RTC_REG_MIN  EQU H'01'
RTC_REG_HOUR EQU H'02'
RTC_REG_DOW  EQU H'03'
RTC_REG_DOM  EQU H'04'
RTC_REG_MON  EQU H'05'
RTC_REG_YEAR EQU H'06'
RTC_REG_A1S  EQU H'07'
RTC_REG_A1M  EQU H'08'
RTC_REG_A1H  EQU H'09'
RTC_REG_A1D  EQU H'0A'
RTC_REG_A2M  EQU H'0B'
RTC_REG_A2H  EQU H'0C'
RTC_REG_A2D  EQU H'0D'
RTC_REG_CON  EQU H'0E'
RTC_REG_STAT EQU H'0F'

  ;; -------------------------------------
i2c_start
  bsf       STATUS,RP0
  bcf       TRISB,RTC_SDA
  nop
  bcf       TRISB,RTC_SCL
  bcf       STATUS,RP0
  return
  
  ;; ------------------
i2c_restart
  bsf       STATUS,RP0
  bsf       TRISB,RTC_SDA
  nop
  bsf       TRISB,RTC_SCL
  nop
  nop
  nop
  bcf       TRISB,RTC_SDA
  nop
  bcf       TRISB,RTC_SCL
  bcf       STATUS,RP0
  return
  
  ;; ------------------
i2c_stop
  bsf       STATUS,RP0
  bsf       TRISB,RTC_SCL
  nop
  bsf       TRISB,RTC_SDA
  bcf       STATUS,RP0
  return
  
  ;; ------------------
i2c_send_byte
  movwf     RTC_BYTE
  movlw     .8
  movwf     RTC_BIT_CNT
  bsf       STATUS,RP0
i2c_send_byte_next
  btfss     RTC_BYTE,7
  bcf       TRISB,RTC_SDA
  btfsc     RTC_BYTE,7
  bsf       TRISB,RTC_SDA
  nop
  bsf       TRISB,RTC_SCL
  nop
  nop
  bcf       TRISB,RTC_SCL
  rlf       RTC_BYTE,f
  decfsz    RTC_BIT_CNT,f
  goto      i2c_send_byte_next

  nop
  nop
  bsf       TRISB,RTC_SDA
  bsf       TRISB,RTC_SCL
  bcf       STATUS,RP0
  movf      PORTB,w
  bsf       STATUS,RP0
  bcf       TRISB,RTC_SCL
  bcf       TRISB,RTC_SDA
  bcf       STATUS,RP0
  andlw     b'00010000'
  movwf     RTC_BYTE
  swapf     RTC_BYTE,f
  return

  ;; ------------------
i2c_recv_byte
  clrf      RTC_BYTE
  movlw     .8
  movwf     RTC_BIT_CNT
  bsf       STATUS,RP0
  bsf       TRISB,RTC_SDA
i2c_recv_byte_loop
  bsf       TRISB,RTC_SCL
  bcf       STATUS,RP0
  btfsc     PORTB,RTC_SDA
  bsf       RTC_BYTE,0
  bsf       STATUS,RP0
  bcf       TRISB,RTC_SCL
  decfsz    RTC_BIT_CNT,f
  goto      i2c_recv_byte_next

  bcf       TRISB,RTC_SDA
  bcf       STATUS,RP0
  return

i2c_recv_byte_next
  bcf       STATUS,C
  rlf       RTC_BYTE,f
  goto      i2c_recv_byte_loop

  ;; ------------------
i2c_ack
  bsf       STATUS,RP0
  bcf       TRISB,RTC_SDA
  bsf       TRISB,RTC_SCL
  nop
  nop
  bcf       TRISB,RTC_SCL
  bcf       STATUS,RP0
  return
  
  ;; ------------------
i2c_nack
  bsf       STATUS,RP0
  bsf       TRISB,RTC_SDA
  bsf       TRISB,RTC_SCL
  nop
  nop
  bcf       TRISB,RTC_SCL
  bcf       TRISB,RTC_SDA
  bcf       STATUS,RP0
  return

  ;; -------------------------------------
rtc_init
  call      i2c_start

  movlw     RTC_ADDR_W
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack
  
  movlw     RTC_REG_STAT
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack

  call      i2c_restart
  
  movlw     RTC_ADDR_R
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack

  call      i2c_recv_byte
  call      i2c_nack

  btfsc     RTC_BYTE,7
  goto      rtc_init_after_clock_fail
  
  call      i2c_stop
  return
  
rtc_init_after_clock_fail
  call      i2c_restart

  movlw     RTC_ADDR_W
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack
  
  movlw     RTC_REG_SEC
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack

  movlw     .4
  movwf     RTC_BYTE_CNT
rtc_init_clear1
  clrw
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack
  decfsz    RTC_BYTE_CNT,f
  goto      rtc_init_clear1
  
  movlw     .2
  movwf     RTC_BYTE_CNT
rtc_init_clear2
  movlw     .1
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack
  decfsz    RTC_BYTE_CNT,f
  goto      rtc_init_clear2

  movlw     .10
  movwf     RTC_BYTE_CNT
rtc_init_clear3
  clrw
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack
  decfsz    RTC_BYTE_CNT,f
  goto      rtc_init_clear3

  call      i2c_stop
  return

  ;; ---------------
rtc_get_time
  call      i2c_start

  movlw     RTC_ADDR_W
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack
  
  movlw     RTC_REG_SEC
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack

  call      i2c_restart
  
  movlw     RTC_ADDR_R
  call      i2c_send_byte
  btfsc     RTC_BYTE,0
  goto      rtc_error_ack

  movlw     .7
  movwf     RTC_BYTE_CNT
rtc_get_time_loop
  call      i2c_recv_byte
  movf      RTC_BYTE,w
  movwf     INDF
  decfsz    RTC_BYTE_CNT,f
  goto      rtc_get_time_next

  call      i2c_nack
  call      i2c_stop
  return
  
rtc_get_time_next
  incf      FSR,f
  call      i2c_ack
  goto      rtc_get_time_loop

  ;; ---------------
rtc_error_ack
  call      i2c_stop
  ;; TODO: add error handling
  return

  ;; -------------------------------------