summaryrefslogtreecommitdiff
path: root/software/ctr/ctr.asm
blob: ac514d3878d039da6101ac1967f57da4b31980c4 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  ;;
  ;;  mur.sat
  ;;
  ;;  Somewhen in the year 20xx, 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-2015 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/>.
  ;;
  ;; -------------------------------------
  ;; PREAMBLE

  LIST      p=16F1847
  include   "p16f1847.inc"
  __config  _CONFIG1,  _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
  __config  _CONFIG2,  _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_LO & _LVP_OFF


  ;; -------------------------------------
  ;; DEFINES  
  ;;   constants
DOT_DEF       EQU .256 - .21      ; .29
DASH_DEF      EQU .256 - .63      ; .87
WSPACE_DEF    EQU .256 - .147     ; .203
TUNE_DEF      EQU DOT_DEF
BCNINTL_DEF   EQU .256 - .120
BCNINTH_DEF   EQU .255
START_CHAR    EQU 'N'
ACK_CHAR      EQU 'A'
OSC_LOW       EQU b'00010000'       ; 31,25 kHz
OSC_HIGH      EQU b'01101000'       ; 4 MHz
    

  ;;   I/O Pins
;; PortA
#define       CW_ENABLE     PORTA,2
#define       CW_OUT        PORTA,3
#define       IHU_SS        PORTA,5
#define       IHU_SDO       PORTA,6
#define       IHU_INT       PORTA,7  
;; PortB
#define       CLK_1HZ       PORTB,0
#define       IHU_SDI       PORTB,1
#define       CRX_INT       PORTB,3
#define       IHU_SCK       PORTB,4
#define       I2C_SDA       PORTB,6
#define       I2C_SCL       PORTB,7


  ;;   variables
;; page 0 only
ELEMENT       EQU H'0020'
ELECNT        EQU H'0021'
CHAR          EQU H'0022'
PARITY        EQU H'0023'
CHARCNT       EQU H'0024'
PENTLE0       EQU H'0025'
PENTLE1       EQU H'0026'
PENTLECNT     EQU H'0027'
ACK_DATA      EQU H'0028'
TMP1          EQU H'0029'
TMP2          EQU H'002A'

BUF           EQU H'0040'
BUF_END       EQU H'004F'
  
;; all pages
BEACON_INTL   EQU H'0070'
BEACON_INTH   EQU H'0071'
DOT           EQU H'0072'
ESPACE        EQU DOT
DASH          EQU H'0073'
CSPACE        EQU DASH
WSPACE        EQU H'0074'
TUNE          EQU H'0075'

I2C_BYTE      EQU H'0077'
I2C_BIT_CNT   EQU H'0078'
I2C_BYTE_CNT  EQU H'0079'

NVSTATE       EQU H'007A'
BEACONCNTH    EQU H'007B'
BEACONCNTL    EQU H'007C'
STATE         EQU H'007D'
W_TEMP        EQU H'007E'
STATUS_TEMP   EQU H'007F'

  ;; -------------------------------------
  ;; OFFSET
  org	      0
  goto      init

  ;; -------------------------------------
  ;; Interrupt Vector
  org	      .4
  ;; save context
  movwf     W_TEMP
  swapf     STATUS,W
  movwf     STATUS_TEMP

interrupt_end
  ;; restore context
  swapf     STATUS_TEMP,W
  movwf     STATUS
  swapf     W_TEMP,F
  swapf     W_TEMP,W
  retfie

  ;; -------------------------------------
  ;; TABLES
  include   "tables.inc"
  
  ;; -------------------------------------
  ;; SUBROUTINES

  ;; -------------------------------------
  include   "ttx.inc"

  ;; -------------------------------------
  include   "i2c.inc"


  ;;; TODO remove this
  ;; -------------------------------------
  ;; -------------------------------------
  ;; -------------------------------------
  ;; -------------------------------------  
  ;; 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'
 
  ;; -------------------------------------
rtc_init
  movlb     .1
  movlw     OSC_HIGH
  movwf     OSCCON  
  movlb     .0
  
  call      i2c_start

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

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

  call      i2c_recv_byte
  call      i2c_nack

  btfsc     I2C_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     I2C_BYTE,0
  goto      rtc_error_ack
  
  movlw     RTC_REG_SEC
  call      i2c_send_byte
  btfsc     I2C_BYTE,0
  goto      rtc_error_ack

  movlw     .16
  movwf     I2C_BYTE_CNT
rtc_init_clear
  clrw
  call      i2c_send_byte
  btfsc     I2C_BYTE,0
  goto      rtc_error_ack
  decfsz    I2C_BYTE_CNT,f
  goto      rtc_init_clear

  call      i2c_stop

  movlb     .1
  movlw     OSC_LOW
  movwf     OSCCON  
  movlb     .0

  return

  ;; ---------------
rtc_get_time
  movlb     .1
  movlw     OSC_HIGH
  movwf     OSCCON  
  movlb     .0


  call      i2c_start

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

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

  movlw     .7
  movwf     I2C_BYTE_CNT
rtc_get_time_loop
  call      i2c_recv_byte
  movf      I2C_BYTE,w
  movwf     INDF1
  decfsz    I2C_BYTE_CNT,f
  goto      rtc_get_time_next

  call      i2c_nack
  call      i2c_stop

  movlb     .1
  movlw     OSC_LOW
  movwf     OSCCON  
  movlb     .0
  return
  
rtc_get_time_next
  incf      FSR1L,f
  call      i2c_ack
  goto      rtc_get_time_loop

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

  movlb     .1
  movlw     OSC_LOW
  movwf     OSCCON  
  movlb     .0
  return

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

  
  ;; -------------------------------------
  ;; INIT
init
  ;; BANK 3
  movlb     .4
  clrf      WPUA
  clrf     WPUB

  ;; BANK 3
  movlb     .3
  clrf      ANSELA
  clrf      ANSELB

  ;; BANK 2
  movlb     .2
  movlw     b'01100000'
  movwf     APFCON0
  clrf      APFCON1
  bcf       I2C_SDA
  bcf       I2C_SCL
  
  ;; BANK 1
  movlb     .1
  movlw     b'00110011'
  movwf     TRISA
  movlw     b'11111111'
  movwf     TRISB
  movlw     OSC_LOW
  movwf     OSCCON
  movlw     b'11000011'
  movwf     OPTION_REG

  ;; BANK 0
  movlb     .0
  movlw     b'00000000'
  movwf     INTCON

  
  bcf       CW_OUT
  bcf       CW_ENABLE
  
  clrf      NVSTATE             ; TODO: read State from EEPROM
  movlw     .42
  movwf     BEACONCNTL          ; TODO: read Beaconcnt from EEPROM
  movlw     .23
  movwf     BEACONCNTH
  movlw     BCNINTL_DEF         ; TODO: read Beacon Interval from EEPROM
  movwf     BEACON_INTL
  movlw     BCNINTH_DEF
  movwf     BEACON_INTH

  movlw     DOT_DEF             ; TODO: read cw timings from EEPROM
  movwf     DOT                 ;   - DOT: length of .
  movlw     DASH_DEF            ;   - DASH: length of -
  movwf     DASH                ;   - WSPACE: length word space
  movlw     WSPACE_DEF          ;   - TUNE: tune in time of oscillator
  movwf     WSPACE
  movlw     TUNE_DEF
  movwf     TUNE

  ;; -------------------------------------
  ;; MAINLOOP
main
  movlw     BUF
  movwf     FSR1L
  clrf      FSR1H
  call      rtc_get_time
  
  ;; clrw
  ;; call      dottime

  ;; movlw     b'00001100'
  ;; xorwf     PORTA,f
  
  goto	    main

  ;; -------------------------------------
  ;; END
  end