forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemdemo.common.spinh
158 lines (136 loc) · 5.46 KB
/
memdemo.common.spinh
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
{
--------------------------------------------
Filename: memdemo.common.spinh
Author: Jesse Burt
Description: Routines common to all memory drivers
Copyright (c) 2023
Started Jul 30, 2022
Updated Apr 8, 2023
See end of file for terms of use.
--------------------------------------------
}
VAR
word _lastpage
word _pagesize
byte _pg_buff[512] ' reasonable maximum
OBJ
math : "math.int"
CON
CLK_FREQ = (_clkmode >> 6) * _xinfreq ' extract P1 clock freq
CYCLES_USEC = CLK_FREQ / 1_000_000 ' calc # cycles in 1 microsec
PUB demo{} | base_page, offs
ser.set_attrs(ser.ECHO)
_pagesize := mem.page_size{} <# 512
ser.printf1(string("Page size: %d\n\r"), _pagesize)
bytefill(@_pg_buff, 0, _pagesize) ' clear out buffer
_lastpage := (MEMSIZE / _pagesize)-1
base_page := 0
math.rndseed(cnt)
mem.rd_block_lsbf(@_pg_buff, base_page, _pagesize)
offs := pg2byte_offs(base_page)
ser.pos_xy(0, 4)
ser.strln(string("Keys:"))
ser.strln(string("[, ]: go back, forward a page in memory"))
ser.strln(string("a: go to specific address (hexdump will round down to page start address)"))
ser.strln(string("s, e: go to the first, last page"))
ser.strln(string("w: write test: fill current page with random value"))
ser.strln(string("x: erase test: fill the current page with the erase value"))
ser.strln(string(" (varies among memory types)"))
ser.fgcolor(ser#RED)
ser.strln(string("Only perform the write or erase test if the data stored on the memory"))
ser.strln(string(" 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(string("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(string("Erasing page..."))
#ifdef IS_FLASH
mem.global_lock(false)
mem.writes_enabled(true)
stime := cnt
mem.erase(st_addr, mem.SECTOR)
repeat while mem.busy()
etime := cnt-stime
#else
{ fill working buffer with erase character }
bytefill(@_pg_buff, mem#ERASE_CELL, _pagesize)
{ write the buffer to memory, and track how long it takes }
stime := cnt
mem.wr_block_lsbf(st_addr, @_pg_buff, _pagesize)
etime := cnt-stime
#endif
PUB read_test(st_addr): etime | stime
' Read memory page
bytefill(@_pg_buff, 0, _pagesize)
ser.str(string("Reading page..."))
{ read memory page to buffer }
stime := cnt
mem.rd_block_lsbf(@_pg_buff, st_addr, _pagesize)
etime := cnt-stime
PUB write_test(st_addr): etime | stime
' Write a random test value to memory page
ser.str(string("Writing page..."))
bytefill(@_pg_buff, math.rndi(255), _pagesize)
#ifdef IS_FLASH
mem.global_lock(false)
mem.writes_enabled(true)
#endif
{ fill page with test character }
stime := cnt
mem.wr_block_lsbf(st_addr, @_pg_buff, _pagesize)
#ifdef IS_FLASH
repeat while mem.busy()
#endif
etime := cnt-stime
PRI cycle_time(cycles)
' Display elapsed time in cycles and microseconds
ser.printf2(string("%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)
DAT
{
Copyright 2023 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.
}