-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsettings.s
161 lines (151 loc) · 3.58 KB
/
settings.s
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
; vim:syntax=z8a:ts=8
;
; msTERM
; settings routines manipulating dataflash
; https://github.com/jcs/mailstation-tools/blob/master/docs/flash-28SF040.pdf
;
; Copyright (c) 2019 joshua stein <[email protected]>
;
; Permission to use, copy, modify, and distribute this software for any
; purpose with or without fee is hereby granted, provided that the above
; copyright notice and this permission notice appear in all copies.
;
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;
.module settings
.include "mailstation.inc"
.equ SDP_LOCK, #SLOT_ADDR + 0x040a
.equ SDP_UNLOCK, #SLOT_ADDR + 0x041a
.equ settings_ident_0, 'j'
.equ settings_ident_1, 'c'
.equ settings_ident_2, 's'
.area _DATA
settings_begin:
settings_ident:
.db #settings_ident_0
.db #settings_ident_1
.db #settings_ident_2
_setting_modem_speed:
.dw #MODEM_DEFAULT_SPEED
_setting_default_source:
.db #SOURCE_WIFI
settings_end:
.area _CODE
; void settings_read(void)
_settings_read::
push bc
push de
push hl
in a, (#SLOT_DEVICE) ; store device and page
ld h, a
in a, (#SLOT_PAGE)
ld l, a
push hl
ld a, #DEVICE_DATAFLASH ; slot 4 device = dataflash
out (#SLOT_DEVICE), a
ld a, #settings_page
out (#SLOT_PAGE), a
ld hl, #SLOT_ADDR + (settings_sector * 256)
push hl
ld a, (hl)
cp #settings_ident_0 ; verify that the first 3 bytes are
jr nz, skip_loading ; our ident characters before loading
inc hl
ld a, (hl)
cp #settings_ident_1
jr nz, skip_loading
inc hl
ld a, (hl)
cp #settings_ident_2
jr nz, skip_loading
inc hl
ld bc, #settings_end - settings_begin
ld de, #settings_begin
pop hl
ldir ; ld (de), (hl), bc-- until bc == 0
push hl
skip_loading:
pop hl
pop hl
ld a, h
out (#SLOT_DEVICE), a
ld a, l
out (#SLOT_PAGE), a
pop hl
pop de
pop bc
ret
sdp:
ld a, (#SLOT_ADDR + 0x1823) ; 28SF040 Software Data Protection
ld a, (#SLOT_ADDR + 0x1820)
ld a, (#SLOT_ADDR + 0x1822)
ld a, (#SLOT_ADDR + 0x0418)
ld a, (#SLOT_ADDR + 0x041b)
ld a, (#SLOT_ADDR + 0x0419)
ret
; caller needs to read final SDP_LOCK or SDP_UNLOCK address
; void settings_write(void)
_settings_write::
push bc
push de
push hl
in a, (#SLOT_DEVICE) ; store device and page
ld h, a
in a, (#SLOT_PAGE)
ld l, a
push hl
ld a, #DEVICE_DATAFLASH ; slot 4 device = dataflash
out (#SLOT_DEVICE), a
xor a
out (#SLOT_PAGE), a ; slot 4 page = 0
call sdp
ld a, (#SDP_UNLOCK)
ld a, #settings_page
out (#SLOT_PAGE), a
ld hl, #SLOT_ADDR + (settings_sector * 256)
sector_erase:
ld (hl), #0x20 ; 28SF040 Sector-Erase Setup
ld (hl), #0xd0 ; 28SF040 Execute
sector_erase_wait:
ld a, (hl) ; wait until End-of-Write
ld b, a
ld a, (hl)
cp b
jr nz, sector_erase_wait
dump_setting_bytes:
ld de, #settings_begin
ld b, #settings_end - settings_begin
byte_program_loop:
ld a, (de)
ld c, a
ld (hl), #0x10 ; 28SF040 Byte-Program Setup
ld (hl), a ; 28SF040 Execute
byte_program:
ld a, (hl)
ld c, a
ld a, (hl) ; End-of-Write by reading it
cp c
jr nz, byte_program ; read until writing succeeds
inc hl
inc de
djnz byte_program_loop
flash_lock:
xor a ; slot page = 0
out (#SLOT_PAGE), a
call sdp
ld a, (#SDP_LOCK)
pop hl
ld a, h
out (#SLOT_DEVICE), a
ld a, l
out (#SLOT_PAGE), a
pop hl
pop de
pop bc
ret