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
|
$MOD167
$SEGMENTED
$INCLUDE (REG167.INC)
$INCLUDE (COMMON.INC)
CONST_2TCL EQU (REGBANK1 + 32d) ; premenna
NAME FIRMWARE_ST10F168
ASSUME DPP3: SYSTEM
FW_DATA SECTION DATA WORD AT FW2DATA
; Control array of structures {segment, address, length in words} for read and write operations
FLASH_MAPPING:
DW 0,0,(32*1024)/2
DW 1,8000h,(32*1024)/2
DW 2,0000h,(64*1024)/2
DW 3,0000h,(32*1024)/2
DW 3,8000h,(32*1024)/2
DW 4,0000h,(64*1024)/2 ; Length is not needed
FW_DATA ENDS
FW_ST10F168 SECTION CODE AT FW2BASE
;-------------------------------------------------------------------------------
; Main procedure
;
; Caka na slovo prikazu, odosle ho naspat pre potvrdenie a to iste musi znovu
; prijat.
;-------------------------------------------------------------------------------
MAIN PROC
; Default register bank
MOV CP,#REGBANK0
; Zapneme serial buffer overrun detection
BSET S0CON.7
; Init stack, size = 128 words, 256 bytes
MOV R0,SYSCON
AND R0,#3FFFh
OR R0,#2000h
MOV SYSCON,R0
MOV STKUN, #STACK_TOP
MOV STKOV, #STACK_BOTTOM
MOV SP, #STACK_TOP
NOP
NOP
MOV DPP0,#0
MOV DPP1,#1
MOV DPP2,#2
MOV DPP3,#3
; Signalize successful initialization
MOV R15,#0
CALL SEND
;-----------------------------------------------------------------------
; Command loop
;-----------------------------------------------------------------------
$INCLUDE (SHELL.INC)
MAIN ENDP
;-------------------------------------------------------------------------------
; Receive config information (1 word)
;-------------------------------------------------------------------------------
REC_CONFIG PROC NEAR
; Get 2TCL constant
CALL REC_SAFE
CMP R14,#0
JMPR CC_NE,REC_CONFIG_ERROR
MOV CONST_2TCL,R15
REC_CONFIG_ERROR:
RET
REC_CONFIG ENDP
;-------------------------------------------------------------------------------
; Erase chip
;-------------------------------------------------------------------------------
ERASE_CHIP PROC NEAR
SCXT CP,#REGBANK1
; Receive 2TCL constant for R4 STEAK
CALL REC_CONFIG
CMP R14,#0
JMPR CC_NE,ERASE_CHIP_ERROR
MOV R13,#0Fh ; Erase all blocks
JMP ERASE_BLOCKS_MASK_OK
ERASE_CHIP_ERROR:
POP CP
RET
ERASE_CHIP ENDP
;-------------------------------------------------------------------------------
; Erase blocks
;-------------------------------------------------------------------------------
ERASE_BLOCKS PROC NEAR
SCXT CP,#REGBANK1
; Receive 2TCL constant for R4 STEAK
CALL REC_CONFIG
CMP R14,#0
JMPR CC_NE,ERASE_BLOCKS_ERROR
; Receive mask of blocks going to be erased
CALL REC_SAFE
; Store mask
MOV R13,R15
; Exit if there was an error
CMP R14,#0
JMPR CC_EQ,ERASE_BLOCKS_MASK_OK
ERASE_BLOCKS_ERROR:
POP CP
RET
ERASE_BLOCKS_MASK_OK:
; Erase blocks command
MOV R0,#0EEEEh
MOV R1,#5555h
MOV R2,#0 ; Block counter
; V R13 mame masku
ERASE_BLOCKS_LOOP:
ROR R13,#1
JNB PSW.1,ERASE_BLOCKS_NEXT
MOV R3,R2
CALL UNLOCK_SEQUENCE
; Check return code
CMP R14,#0
JMPR CC_NE,ERASE_BLOCKS_DONE
ERASE_BLOCKS_NEXT:
CMPI1 R2,#2
JMPR CC_UGT,ERASE_BLOCKS_DONE
JMP ERASE_BLOCKS_LOOP
ERASE_BLOCKS_DONE:
MOV R15,R14
CALL SEND
POP CP
RET
ERASE_BLOCKS ENDP
;-------------------------------------------------------------------------------
; Read
;-------------------------------------------------------------------------------
READ PROC NEAR
SCXT CP,#REGBANK1
; Receive 2TCL constant for R4 STEAK
CALL REC_CONFIG
CMP R14,#0
JMPR CC_NE,READ_ERROR
; Prijmeme pocet bajtov pre precitanie
CALL REC_DWORD_SAFE
CMP R14,#0
JMPR CC_NE,READ_ERROR
; Pocitadlo velkosti 1024 bajtoveho bloku
MOV R10,#1024
; Base of the control table
MOV R11,#DPP3:FLASH_MAPPING
READ_START:
MOV R0,[R11] ; Get segment, R0
ADD R11,#2
MOV R1,[R11] ; Get start address, R1
ADD R11,#2
MOV R2,[R11] ; Get data length to read, R2
ADD R11,#2
READ_LOOP:
; Read word
EXTS R0,#1
MOV R15,[R1]
CALL SEND
CALL CHECK_COUNT
CMP R14,#1
JMPR CC_EQ,READ_DONE
ADD R1,#2 ; Set address of the next word
SUB R2,#1 ; Decrement word counter
JMPR CC_NZ,READ_LOOP
JMP READ_START
READ_ERROR:
READ_DONE:
POP CP
RET
READ ENDP
;-------------------------------------------------------------------------------
; Write
;-------------------------------------------------------------------------------
WRITE PROC NEAR
SCXT CP,#REGBANK1
; Receive 2TCL constant for R4 STEAK
CALL REC_CONFIG
CMP R14,#0
JMPR CC_NE,WRITE_ERROR
; Prijmeme pocet bajtov pre zapis
CALL REC_DWORD_SAFE
CMP R14,#0
JMPR CC_NE,WRITE_ERROR
; Pocitadlo velkosti 1024 bajtoveho bloku
MOV R10,#1024
; Base of the control table
MOV R11,#DPP3:FLASH_MAPPING
WRITE_START:
MOV R0,[R11] ; Get segment number, R0
AND R0,#000Fh ; Skonstruujeme prikaz
OR R0,#55A0h
ADD R11,#2
MOV R1,[R11] ; Get start address, R1
ADD R11,#2
MOV R5,[R11] ; Get data length to write, R5
ADD R11,#2
WRITE_LOOP:
CALL REC
CMP R14,#0
JMPR CC_NE,WRITE_SEND_ERROR
MOV R2,R15
; Write word
CALL UNLOCK_SEQUENCE
CMP R14,#0
JMPR CC_NE,WRITE_SEND_ERROR
CALL CHECK_COUNT
CMP R14,#1
JMPR CC_EQ,WRITE_DONE
ADD R1,#2
SUB R5,#1
JMPR CC_NZ,WRITE_LOOP
JMP WRITE_START
WRITE_SEND_ERROR:
MOV R15,R14
CALL SEND
WRITE_ERROR:
WRITE_DONE:
POP CP
RET
WRITE ENDP
;-------------------------------------------------------------------------------
; Helper subroutines
;-------------------------------------------------------------------------------
$INCLUDE (SUBROUTINES.INC)
; Vstup: R0, R1, R2, R3, R4
; Vystup: R14 navratovy kod
; Zachovava: R0, R1, R2, R3, R4
UNLOCK_SEQUENCE PROC NEAR
PUSH R0
PUSH R1
PUSH R2
PUSH R3
PUSH R6
MOV R4,CONST_2TCL ; Perioda hodinoveho signalu v ns
; Unlock sequence
MOV R6,#8000h
EXTS #1,#2
MOV 8000h,R6
MOV [R6],R6
NOP
NOP
MOV R14,R0
POP R6
POP R3
POP R2
POP R1
POP R0
RET
UNLOCK_SEQUENCE ENDP
;-------------------------------------------------------------------------------
; End of helper subroutines
;-------------------------------------------------------------------------------
FW_ST10F168 ENDS
END
|