-
Notifications
You must be signed in to change notification settings - Fork 1
/
25XXXX-Demo.spin2
183 lines (144 loc) · 5.9 KB
/
25XXXX-Demo.spin2
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
{
----------------------------------------------------------------------------------------------------
Filename: 25XXXX-Demo.spin2
Description: Demo of the 25XXXX NOR Flash driver
* Memory hexdump display
Author: Jesse Burt
Started: Nov 9, 2019
Updated: Sep 18, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
CON
_clkfreq = 250_000_000
_xtlfreq = 20_000_000
' -- User-modifiable constants
{ memory size }
PART = 128
MULT = M ' K = kilobits, M = megabits
' --
K = 1024
M = 1048576
MEMSIZE = (PART * MULT) / 8
CYCLES_USEC = _CLKFREQ / 1_000_000 ' calc . cycles in 1 microsec
OBJ
math: "math.int"
ser: "com.serial.terminal.ansi" | SER_BAUD=2_000_000
mem: "memory.flash.25xxxx" | CS=61, SCK=60, MOSI=59, MISO=58, SPI_FREQ=20_000_000
' NOTE: MOSI and MISO must each be +/- 3 of SCK
VAR
word _lastpage
word _pagesize
byte _pg_buff[512] ' reasonable maximum
PUB main() | base_page, offs
setup()
ser.set_attrs(ser.ECHO)
_pagesize := mem.page_size() <# 512
ser.printf(@"Page size: %d\n\r", _pagesize)
bytefill(@_pg_buff, 0, _pagesize) ' clear out buffer
_lastpage := (MEMSIZE / _pagesize)-1
base_page := 0
mem.rd_block_lsbf(@_pg_buff, base_page, _pagesize)
offs := pg2byte_offs(base_page)
ser.pos_xy(0, 4)
ser.strln(@"Keys:")
ser.strln(@"[, ]: go back, forward a page in memory")
ser.strln(@"a: go to specific address (hexdump will round down to page start address)")
ser.strln(@"s, e: go to the first, last page")
ser.strln(@"w: write test: fill current page with random value")
ser.strln(@"x: erase test: fill the current page with the erase value")
ser.strln(@" (varies among memory types)")
ser.fgcolor(ser.RED)
ser.strln(@"Only perform the write or erase test if the data stored on the memory")
ser.strln(@" isn't important!")
ser.fgcolor(ser.GREY)
repeat
ser.pos_xy(0, 13)
{ display a hexdump of the current page of memory, but limit it to
a reasonable 512 bytes at a time }
ser.hexdump(@_pg_buff, offs, 6, _pagesize, 16)
case ser.getchar()
"[": ' go back a page in memory
base_page--
if (base_page < 0)
base_page := 0
"]": ' go forward a page
base_page++
if (base_page > _lastpage)
base_page := _lastpage
"a":
ser.str(@"Enter address (hex): ")
base_page := ser.gethex() / _pagesize
ser.newline()
"e": ' go to the last page
base_page := _lastpage
"s": ' go to the first page
base_page := 0
"w": ' fill page w/test value
cycle_time(write_test(offs))
"x": ' erase the current page
cycle_time(erase_test(offs))
other:
offs := pg2byte_offs(base_page)
cycle_time(read_test(offs))
PUB erase_test(st_addr): etime | stime
' Erase memory page
ser.str(@"Erasing page...")
mem.global_lock(false)
mem.writes_ena(true)
stime := getct()
mem.erase(st_addr, mem.SECTOR)
repeat while mem.busy()
etime := getct()-stime
PUB read_test(st_addr): etime | stime
' Read memory page
bytefill(@_pg_buff, 0, _pagesize)
ser.str(@"Reading page...")
{ read memory page to buffer }
stime := getct()
mem.rd_block_lsbf(@_pg_buff, st_addr, _pagesize)
etime := getct()-stime
PUB write_test(st_addr): etime | stime
' Write a random test value to memory page
ser.str(@"Writing page...")
bytefill(@_pg_buff, math.rndi(255), _pagesize)
mem.global_lock(false)
mem.writes_ena(true)
{ fill page with test character }
stime := getct()
mem.wr_block_lsbf(st_addr, @_pg_buff, _pagesize)
repeat while mem.busy()
etime := getct()-stime
PRI cycle_time(cycles)
' Display elapsed time in cycles and microseconds
ser.printf(@"%d cycles (%dusec)", cycles, (cycles / CYCLES_USEC))
ser.clear_ln()
ser.newline()
PRI pg2byte_offs(page_nr): b
' Get start of page number as a byte offset
return (page_nr * _pagesize)
PUB setup()
ser.start()
ser.clear()
ser.strln(@"Serial terminal started")
if ( mem.start() )
ser.strln(@"25XXXX driver started")
else
ser.strln(@"25XXXX driver failed to start - halting")
repeat
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}