summaryrefslogtreecommitdiff
path: root/software/pic.bootloader/generic-mainloop.inc
blob: 1cecf3ab01b530e571de5386151e13f6dd23087a (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
  ;;
  ;;  spreadspace pic utils
  ;;
  ;;
  ;;  Copyright (C) 2011-2013 Christian Pointner <equinox@spreadspace.org>
  ;;
  ;;  This file is part of spreadspace pic utils.
  ;;
  ;;  spreadspace pic utils 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.
  ;;
  ;;  spreadspace pic utils 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 spreadspace pic utils. If not, see <http://www.gnu.org/licenses/>.
  ;;

wait_new_cmd
  movlw     combuff
  movwf     FSR
  clrf      combuff
  clrf      current_cmdlen
  clrf      flags

wait_cmd
  call      com_rx_byte
  movwf     INDF          ; process received byte
  incf      FSR,f
  btfss     F_CMD_STARTED
  goto      wait_cmd_len
  movf      current_cmdlen,f
  btfsc     STATUS,Z
  goto      new_cmd
  decfsz    current_cmdlen,f
  goto      wait_cmd
  goto      exec_cmd

wait_cmd_len              ; command code received, now wait for length
  bsf       F_CMD_STARTED
  goto      wait_cmd

new_cmd                   ; got new command code and length -> check it
  movf      combuff,w
  sublw     CMD_MAX
  btfss     STATUS,C
  goto      invalid_cmd
  movlw     CMD_MIN_LEN         ; check for minimum command len
  subwf     combuff + .1,w
  btfss     STATUS,C
  goto      invalid_cmd
  movlw     .2                  ; 2 bytes already received
  subwf     combuff + .1,w
  movwf     current_cmdlen
  goto      wait_cmd

exec_cmd                        ; command is complete -> check csum
  movlw     combuff
  movwf     FSR
  movf      combuff + .1,w
  movwf     cnt
  clrf      csum
exec_cmd_check_csum
  movf      INDF,w
  xorwf     csum,f
  incf      FSR,f
  decfsz    cnt,f
  goto      exec_cmd_check_csum
  movf      csum,f
  btfss     STATUS,Z
  goto      csum_error

  movf      combuff,w           ; command is correct and complete -> dispatch
  addwf     PCL,f
  goto      wait_new_cmd
  goto      cmd_identify        ; identify
  goto      cmd_boot            ; boot
  goto      HOOK_CMD_RESET      ; reset
  goto      HOOK_CMD_R_FLASH    ; read flash segment
  goto      HOOK_CMD_W_FLASH    ; write flash segment
  goto      HOOK_CMD_R_EEPROM   ; read eeprom
  goto      HOOK_CMD_W_EEPROM   ; write eeprom
  goto      HOOK_CMD_R_CONFIG   ; read config
  goto      HOOK_CMD_R_CONFIG   ; write config

  ;; ** generic error messages *******
invalid_cmd               ; received command code is not known or len is bogus
  movlw     E_INV_CMD
  call      ack_cmd
  goto      wait_new_cmd

csum_error                 ; received command has an invalid check sum
  movlw     E_BAD_CSUM
  call      ack_cmd
  goto      wait_new_cmd

address_invalid            ; received command uses invalid address (i.e. not aligned to 16bytes)
  movlw     E_ADDR_INVALID
  call      ack_cmd
  goto      wait_new_cmd

address_prohibited         ; received command uses prohibited address (i.e. FLASH < 0x100)
  movlw     E_ADDR_PROHIB
  call      ack_cmd
  goto      wait_new_cmd

  ;; ** generic command handlers ************
  ;; ** identify *******
cmd_identify
  clrf      csum
  movf      combuff,w
  call      com_tx_byte

  movlw     .20
  call      com_tx_byte

  movlw     E_OK
  call      com_tx_byte

  movlw     VERSION_MIN
  call      com_tx_byte
  movlw     VERSION_MAJ
  call      com_tx_byte

  movlw     NAME_0
  call      com_tx_byte
  movlw     NAME_1
  call      com_tx_byte
  movlw     NAME_2
  call      com_tx_byte

  movlw     DEVID_L
  call      com_tx_byte
  movlw     DEVID_H
  call      com_tx_byte

  movlw     FLASH_SIZE_L
  call      com_tx_byte
  movlw     FLASH_SIZE_H
  call      com_tx_byte
  movlw     FSS
  call      com_tx_byte

  movlw     EEPROM_SIZE_L
  call      com_tx_byte
  movlw     EEPROM_SIZE_H
  call      com_tx_byte
  movlw     MESS
  call      com_tx_byte

  movlw     CFG
  call      com_tx_byte

  movlw     SUPPORTED_L
  call      com_tx_byte
  movlw     SUPPORTED_H
  call      com_tx_byte

  movf      csum,w
  call      com_tx_byte
  goto      wait_new_cmd

  ;; ** boot *******
cmd_boot
  movlw     E_OK
  call      ack_cmd
  goto      USERVECT

  ;; ** not implemented commands *******
cmd_not_impl
  movlw     E_NOT_IMPL
  call      ack_cmd
  goto      wait_new_cmd