Skip to content

Commit edcfc92

Browse files
author
Nicolas Benezan
committed
Spi_RdByte() timing fixed, FlashDemo added (by VonSzarvas)
1 parent 1c17215 commit edcfc92

File tree

5 files changed

+693
-64
lines changed

5 files changed

+693
-64
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
' Flash Demo
2+
' Run on P2 EVAL board with FLASH Dip Switch set ON
3+
' Run this code, then within 2 seconds open Parallax Serial Terminal (or equivalent) at 115200 baud
4+
5+
CON
6+
7+
CLK_FREQ = 180_000_000 ' system freq as a constant
8+
_clkfreq = CLK_FREQ
9+
10+
BR_TERM = 230_400 ' terminal baud rate
11+
12+
FLASH_ADR = $60000
13+
14+
MAXCHK = 255
15+
16+
17+
OBJ
18+
19+
com : "jm_serial.spin2"
20+
spi : "SpiFlash.spin2"
21+
22+
23+
VAR
24+
25+
byte buffer[4096]
26+
27+
28+
PUB main() | i
29+
30+
com.start(BR_TERM)
31+
32+
waitms(2000) ' 2 second pause; allow user to open serial debug terminal
33+
34+
com.str (string(13, 10, "erase...", 13, 10))
35+
spi.Erase (FLASH_ADR, $2000)
36+
37+
38+
com.str (string("read before: "))
39+
40+
' clear buffer
41+
repeat i from 0 to 255
42+
buffer[i]:= -2
43+
44+
45+
spi.Read (@buffer, FLASH_ADR, 256)
46+
47+
repeat i from 0 to MAXCHK
48+
com.dec (buffer[i])
49+
com.tx (" ")
50+
com.tx(10)
51+
com.tx(13)
52+
53+
repeat i from 0 to 255
54+
buffer[i]:= i
55+
56+
spi.WritePage (@buffer, FLASH_ADR)
57+
58+
59+
60+
61+
com.str (string("read after write: "))
62+
63+
' clear buffer
64+
repeat i from 0 to 255
65+
buffer[i]:= -2
66+
67+
68+
spi.Read (@buffer, FLASH_ADR, 256)
69+
70+
repeat i from 0 to MAXCHK
71+
com.dec (buffer[i])
72+
com.tx (" ")
73+
com.tx(10)
74+
com.tx(13)

libraries/community/p2/All/Simple_SpiFlash/SpiFlash.spin2

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
' author: Nicolas Benezan ([email protected], forum: ManAtWork)
1414

1515
' bene 07-May-2020 Spi_WrLong() fixed (little endian)
16+
' bene 09-Jul-2020 Spi_RdByte() fixed, waitx
1617

1718
CON spi_cs = 61
1819
spi_ck = 60
@@ -26,28 +27,28 @@ CON spi_cs = 61
2627
Read_Data = $03
2728
Read_Status = $05
2829

29-
PUB start ()' Dummy to avoid compiling as top
30+
PUB start() ' Dummy to avoid compiling as top
3031
repeat
3132

32-
PUB Erase (flashAdr, size) | cmd
33+
PUB Erase(flashAdr, size) | cmd
3334
' size can be 4kB or 64kB
3435
cmd:= Erase_4k
3536
if size > $1000
3637
cmd:= Erase_64k
37-
Spi_Init ()
38+
Spi_Init()
3839
Spi_Cmd8 (Write_Enable)
3940
Spi_Cmd32 (cmd, flashAdr)
40-
Spi_Wait ()
41+
Spi_Wait()
4142

42-
PUB WritePage (hubAdr, flashAdr)
43+
PUB WritePage(hubAdr, flashAdr)
4344
' writes a page of 256 bytes
44-
Spi_Init ()
45-
Spi_Cmd8 (Write_Enable)
46-
Spi_Cmd32 (Write_Page, flashAdr)
45+
Spi_Init()
46+
Spi_Cmd8(Write_Enable)
47+
Spi_Cmd32(Write_Page, flashAdr)
4748
repeat 64
4849
Spi_WrLong (long[hubAdr])
4950
hubAdr+= 4
50-
Spi_Wait ()
51+
Spi_Wait()
5152

5253
PUB Read (hubAdr, flashAdr, size)
5354
' read any number of bytes
@@ -58,19 +59,22 @@ PUB Read (hubAdr, flashAdr, size)
5859
ORG
5960
drvh #spi_cs
6061
END
61-
62-
PUB Verify (hubAdr, flashAdr, size) : ok
62+
63+
PUB Verify(hubAdr, flashAdr, size) : ok
6364
' compare HUBRAM to flash contents, true means match
6465
ok:= true
65-
Spi_Init ()
66-
Spi_Cmd32 (Read_Data, flashAdr)
66+
Spi_Init()
67+
Spi_Cmd32(Read_Data, flashAdr)
6768
repeat size
68-
ok&= byte[hubAdr++] == Spi_RdByte ()
69+
ok&= byte[hubAdr++] == Spi_RdByte()
70+
6971
ORG
7072
drvh #spi_cs
7173
END
7274

73-
PRI Spi_Init ()
75+
return ok
76+
77+
PRI Spi_Init()
7478
ORG
7579
drvh #spi_cs 'spi_cs high
7680
fltl #spi_ck 'reset smart pin spi_ck
@@ -80,19 +84,21 @@ PRI Spi_Init ()
8084
drvl #spi_di
8185
END
8286

83-
PRI Spi_Cmd8 (cmd)
87+
PRI Spi_Cmd8(cmd)
8488
' outputs 8 bits command, MSB first
8589
ORG
8690
drvh #spi_cs
8791
shl cmd,#24 'shift command up
8892
drvl #spi_cs
8993
END
90-
Spi_WrByte (cmd)
94+
95+
Spi_WrByte(cmd)
96+
9197
ORG
9298
drvl #spi_di
9399
END
94100

95-
PRI Spi_WrByte (cmd) : r
101+
PRI Spi_WrByte(cmd) : result
96102
ORG
97103
shl cmd,#1 wc
98104
outc #spi_di
@@ -114,77 +120,84 @@ PRI Spi_WrByte (cmd) : r
114120
END
115121
return cmd
116122

117-
PRI Spi_Cmd32 (cmd, adr)
123+
PRI Spi_Cmd32(cmd, adr)
118124
' outputs 4 bytes: 8 bit command + 24 bits adr
119125
ORG
120126
drvh #spi_cs
121127
shl cmd,#24 'shift command up
122128
or cmd,adr 'or in address
123129
drvl #spi_cs
124130
END
131+
125132
repeat 4
126-
cmd:= Spi_WrByte (cmd)
133+
cmd:= Spi_WrByte(cmd)
134+
127135
ORG
128136
drvl #spi_di
129137
END
130138

131-
PRI Spi_WrLong (l)
139+
PRI Spi_WrLong(l)
132140
' outputs 32 bits while spi_cs stays low
133-
Spi_WrByte (l<<24)
134-
Spi_WrByte (l<<16)
135-
Spi_WrByte (l<<8)
136-
Spi_WrByte (l)
141+
Spi_WrByte(l<<24)
142+
Spi_WrByte(l<<16)
143+
Spi_WrByte(l<<8)
144+
Spi_WrByte(l)
137145

138146
PRI Spi_RdByte (): b
139147
' read 8 bits
140148
ORG
141-
wypin #16,#spi_ck 'start 16 clock transitions
142-
mov b,#0
143-
nop
144-
nop 'read later to compensate input/output delay
145-
nop
146-
nop
147-
testp #spi_do wc
148-
rcl b,#1
149-
testp #spi_do wc
150-
rcl b,#1
151-
testp #spi_do wc
152-
rcl b,#1
153-
testp #spi_do wc
154-
rcl b,#1
155-
testp #spi_do wc
156-
rcl b,#1
157-
testp #spi_do wc
158-
rcl b,#1
159-
testp #spi_do wc
160-
rcl b,#1
161-
testp #spi_do wc
162-
rcl b,#1
149+
wypin #16,#spi_ck 'start 16 clock transitions
150+
mov b,#0
151+
waitx #4 ' 3..6 works for me
152+
'nop 'read later to compensate input/output delay
153+
'nop ' 2 or 3 nops works, 1+4 nops don't
154+
'nop
155+
testp #spi_do wc
156+
rcl b,#1
157+
testp #spi_do wc
158+
rcl b,#1
159+
testp #spi_do wc
160+
rcl b,#1
161+
testp #spi_do wc
162+
rcl b,#1
163+
testp #spi_do wc
164+
rcl b,#1
165+
testp #spi_do wc
166+
rcl b,#1
167+
testp #spi_do wc
168+
rcl b,#1
169+
testp #spi_do wc
170+
rcl b,#1
163171
END
172+
return b
173+
174+
175+
PRI Spi_Wait() | st
164176

165-
PRI Spi_Wait ()| st
166177
' waits until busy flag == 0, drives spi_cs high when done
167178
repeat
168-
Spi_Cmd8 (Read_Status)
169-
st:= Spi_RdByte ()
179+
Spi_Cmd8(Read_Status)
180+
st:= Spi_RdByte()
170181
until st & $01 == 0
182+
171183
ORG
172184
drvh #spi_cs
173185
END
174186

187+
{
175188
+------------------------------------------------------------------------------------------------------------------------------+
176-
TERMS OF USE: MIT License
177-
+------------------------------------------------------------------------------------------------------------------------------
178-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
179-
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
180-
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
181-
is furnished to do so, subject to the following conditions:
182-
183-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
184-
185-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
186-
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
187-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
188-
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
189+
¦ TERMS OF USE: MIT License ¦
190+
+------------------------------------------------------------------------------------------------------------------------------¦
191+
¦Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation ¦
192+
¦files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, ¦
193+
¦modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software¦
194+
¦is furnished to do so, subject to the following conditions: ¦
195+
¦ ¦
196+
¦The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.¦
197+
¦ ¦
198+
¦THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE ¦
199+
¦WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR ¦
200+
¦COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ¦
201+
¦ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ¦
189202
+------------------------------------------------------------------------------------------------------------------------------+
190-
203+
}
1.03 KB
Binary file not shown.

libraries/community/p2/All/Simple_SpiFlash/jm_nstr.spin2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'' ================================================================================================='''' File....... jm_nstr.spin2'' Purpose.... Convert numbers to strings'' Authors.... Jon McPhalen'' -- Copyright (c) 2020 Jon McPhalen'' -- see below for terms of use'' E-mail..... [email protected]'' Started....'' Updated.... 28 JUN 2020'''' =================================================================================================con NBUF_SIZE = 33 PBUF_SIZE = 81var byte nbuf[NBUF_SIZE] ' number conversions byte pbuf[PBUF_SIZE] ' padded stringspub null()'' This is not a top level objectpub fmt_number(value, radix, digits, width, pad) : p_str'' Return pointer to string of value converted to number in padded field'' -- value is converted using radix'' -- digits is max number of digits to use'' -- width is width of final fields (max)'' -- pad is character used to pad final field (if needed) case_fast radix 02 : p_str := padstr(itoa(value, 2, digits), width, pad) 04 : p_str := padstr(itoa(value, 4, digits), width, pad) 08 : p_str := padstr(itoa(value, 8, digits), width, pad) 10 : p_str := padstr(itoa(value, 10, digits), width, pad) 16 : p_str := padstr(itoa(value, 16, digits), width, pad) 99 : p_str := padstr(dpdec(value, digits), width, pad) ' special case other : p_str := string("?")pub dec(value, digits) : p_str | sign, len'' Convert decimal value to string'' -- digits is 0 (auto size) to 10 p_str := itoa(value, 10, digits)pub dpdec(value, dp) : p_str | len, byte scratch[12]'' Convert value to string with decimal point'' -- dp is digits after decimal point'' -- returns pointer to updated fp string'' -- modifies original string'' -- return pointer to converted string p_str := itoa(value, 10, 0) if (dp <= 0) ' abort if no decimal point return p_str len := strsize(p_str) ' digits bytefill(@scratch, 0, 12) ' clear scratch buffer if (value < 0) ' ignore "-" if present ++p_str --len if (len < (dp+1)) ' insert 0s? bytemove(@scratch, p_str, len) ' move digits to scratch buffer bytefill(p_str, "0", dp+2-len) ' pad string with 0s bytemove(p_str+dp+2-len, @scratch, len+1) ' move digits back byte[p_str+1] := "." ' insert dpoint else bytemove(@scratch, p_str+len-dp, dp) ' move decimal part to buffer byte[p_str+len-dp] := "." ' insert dpoint bytemove(p_str+len-dp+1, @scratch, dp+1) ' move decimal part back if (value < 0) ' fix pointer for negative #s --p_strpub itoa(value, radix, digits) : p_str | sign, len, d'' Convert integer to string'' -- supports radix 10, 2, 4, 8, and 16'' -- digits is 0 (auto size) to limit for long using radix bytefill(@nbuf, 0, NBUF_SIZE) ' clear buffer p_str := @nbuf ' point to it case radix ' fix digits 02 : digits := 0 #> digits <# 32 04 : digits := 0 #> digits <# 16 08 : digits := 0 #> digits <# 11 10 : digits := 0 #> digits <# 10 16 : digits := 0 #> digits <# 8 other : byte[p_str] := 0 return if ((radix == 10) && (value < 0)) ' deal with negative decimals if (value == negx) sign := 2 value := posx else sign := 1 value := -value else sign := 0 len := 0 repeat d := value +// radix ' get digit (1s column) byte[p_str++] := (d < 10) ? d + "0" : d - 10 + "A" ' convert to ASCII value +/= radix ' remove digit if (digits) ' length limited? if (++len == digits) ' check size quit else if (value == 0) ' done? quit if (sign) byte[p_str++] := "-" ' add sign if needed if (sign == 2) nbuf[0] := "8" ' fix negx if needed byte[p_str++] := 0 ' terminate string return revstr(@nbuf) ' fix order (reverse)pub revstr(p_str) : result | first, len, last'' Reverse the order of characters in a string. result := first := p_str ' start len := strsize(p_str) ' length last := first + len - 1 ' end repeat (len >> 1) ' reverse them byte[first++], byte[last--] := byte[last], byte[first]pub padstr(p_str, width, padchar) : p_pad | len'' Pad string with padchar character'' -- positive width uses left pad, negative field width uses right pad'' -- truncate if string len > width'' -- input string is not modified'' -- returns pointer to padded string bytefill(@pbuf, 0, PBUF_SIZE) ' clear padded buffer len := strsize(p_str) ' get length of input width := -PBUF_SIZE+1 #> width <# PBUF_SIZE-1 ' constrain to buffer size if (width > 0) ' right-justify in padded field if (width > len) bytefill(@pbuf, padchar, width-len) bytemove(@pbuf+width-len, p_str, len) p_pad := @pbuf else bytemove(@pbuf, p_str+len-width, width) ' truncate to right-most characters p_pad := @pbuf elseif (width < 0) ' left-justify in padded field width := -width if (width > len) bytemove(@pbuf, p_str, len) bytefill(@pbuf+len, padchar, width-len) p_pad := @pbuf else bytemove(@pbuf, p_str, width) ' truncate to leftmost characters p_pad := @pbuf else p_pad := p_strcon { license }{{ Terms of Use: MIT License 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.}}

0 commit comments

Comments
 (0)