Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions libraries/community/p2/All/Simple_SpiFlash/FlashDemo.spin2
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
' Flash Demo
' Run on P2 EVAL board with FLASH Dip Switch set ON
' Run this code, then within 2 seconds open Parallax Serial Terminal (or equivalent) at 115200 baud

CON

CLK_FREQ = 180_000_000 ' system freq as a constant
_clkfreq = CLK_FREQ

BR_TERM = 230_400 ' terminal baud rate

FLASH_ADR = $60000

MAXCHK = 255


OBJ

com : "jm_serial.spin2"
spi : "SpiFlash.spin2"


VAR

byte buffer[4096]


PUB main() | i

com.start(BR_TERM)

waitms(2000) ' 2 second pause; allow user to open serial debug terminal

com.str (string(13, 10, "erase...", 13, 10))
spi.Erase (FLASH_ADR, $2000)


com.str (string("read before: "))

' clear buffer
repeat i from 0 to 255
buffer[i]:= -2


spi.Read (@buffer, FLASH_ADR, 256)

repeat i from 0 to MAXCHK
com.dec (buffer[i])
com.tx (" ")
com.tx(10)
com.tx(13)

repeat i from 0 to 255
buffer[i]:= i

spi.WritePage (@buffer, FLASH_ADR)




com.str (string("read after write: "))

' clear buffer
repeat i from 0 to 255
buffer[i]:= -2


spi.Read (@buffer, FLASH_ADR, 256)

repeat i from 0 to MAXCHK
com.dec (buffer[i])
com.tx (" ")
com.tx(10)
com.tx(13)
141 changes: 77 additions & 64 deletions libraries/community/p2/All/Simple_SpiFlash/SpiFlash.spin2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
' author: Nicolas Benezan (nicolas@benezan.de, forum: ManAtWork)

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

CON spi_cs = 61
spi_ck = 60
Expand All @@ -26,28 +27,28 @@ CON spi_cs = 61
Read_Data = $03
Read_Status = $05

PUB start ()' Dummy to avoid compiling as top
PUB start() ' Dummy to avoid compiling as top
repeat

PUB Erase (flashAdr, size) | cmd
PUB Erase(flashAdr, size) | cmd
' size can be 4kB or 64kB
cmd:= Erase_4k
if size > $1000
cmd:= Erase_64k
Spi_Init ()
Spi_Init()
Spi_Cmd8 (Write_Enable)
Spi_Cmd32 (cmd, flashAdr)
Spi_Wait ()
Spi_Wait()

PUB WritePage (hubAdr, flashAdr)
PUB WritePage(hubAdr, flashAdr)
' writes a page of 256 bytes
Spi_Init ()
Spi_Cmd8 (Write_Enable)
Spi_Cmd32 (Write_Page, flashAdr)
Spi_Init()
Spi_Cmd8(Write_Enable)
Spi_Cmd32(Write_Page, flashAdr)
repeat 64
Spi_WrLong (long[hubAdr])
hubAdr+= 4
Spi_Wait ()
Spi_Wait()

PUB Read (hubAdr, flashAdr, size)
' read any number of bytes
Expand All @@ -58,19 +59,22 @@ PUB Read (hubAdr, flashAdr, size)
ORG
drvh #spi_cs
END

PUB Verify (hubAdr, flashAdr, size) : ok
PUB Verify(hubAdr, flashAdr, size) : ok
' compare HUBRAM to flash contents, true means match
ok:= true
Spi_Init ()
Spi_Cmd32 (Read_Data, flashAdr)
Spi_Init()
Spi_Cmd32(Read_Data, flashAdr)
repeat size
ok&= byte[hubAdr++] == Spi_RdByte ()
ok&= byte[hubAdr++] == Spi_RdByte()

ORG
drvh #spi_cs
END

PRI Spi_Init ()
return ok

PRI Spi_Init()
ORG
drvh #spi_cs 'spi_cs high
fltl #spi_ck 'reset smart pin spi_ck
Expand All @@ -80,19 +84,21 @@ PRI Spi_Init ()
drvl #spi_di
END

PRI Spi_Cmd8 (cmd)
PRI Spi_Cmd8(cmd)
' outputs 8 bits command, MSB first
ORG
drvh #spi_cs
shl cmd,#24 'shift command up
drvl #spi_cs
END
Spi_WrByte (cmd)

Spi_WrByte(cmd)

ORG
drvl #spi_di
END

PRI Spi_WrByte (cmd) : r
PRI Spi_WrByte(cmd) : result
ORG
shl cmd,#1 wc
outc #spi_di
Expand All @@ -114,77 +120,84 @@ PRI Spi_WrByte (cmd) : r
END
return cmd

PRI Spi_Cmd32 (cmd, adr)
PRI Spi_Cmd32(cmd, adr)
' outputs 4 bytes: 8 bit command + 24 bits adr
ORG
drvh #spi_cs
shl cmd,#24 'shift command up
or cmd,adr 'or in address
drvl #spi_cs
END

repeat 4
cmd:= Spi_WrByte (cmd)
cmd:= Spi_WrByte(cmd)

ORG
drvl #spi_di
END

PRI Spi_WrLong (l)
PRI Spi_WrLong(l)
' outputs 32 bits while spi_cs stays low
Spi_WrByte (l<<24)
Spi_WrByte (l<<16)
Spi_WrByte (l<<8)
Spi_WrByte (l)
Spi_WrByte(l<<24)
Spi_WrByte(l<<16)
Spi_WrByte(l<<8)
Spi_WrByte(l)

PRI Spi_RdByte (): b
' read 8 bits
ORG
wypin #16,#spi_ck 'start 16 clock transitions
mov b,#0
nop
nop 'read later to compensate input/output delay
nop
nop
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
wypin #16,#spi_ck 'start 16 clock transitions
mov b,#0
waitx #4 ' 3..6 works for me
'nop 'read later to compensate input/output delay
'nop ' 2 or 3 nops works, 1+4 nops don't
'nop
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
testp #spi_do wc
rcl b,#1
END
return b


PRI Spi_Wait() | st

PRI Spi_Wait ()| st
' waits until busy flag == 0, drives spi_cs high when done
repeat
Spi_Cmd8 (Read_Status)
st:= Spi_RdByte ()
Spi_Cmd8(Read_Status)
st:= Spi_RdByte()
until st & $01 == 0

ORG
drvh #spi_cs
END

{
+------------------------------------------------------------------------------------------------------------------------------+
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.
¦ 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. ¦
+------------------------------------------------------------------------------------------------------------------------------+

}
Binary file not shown.
1 change: 1 addition & 0 deletions libraries/community/p2/All/Simple_SpiFlash/jm_nstr.spin2
Original file line number Diff line number Diff line change
@@ -0,0 +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..... jon.mcphalen@gmail.com'' 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.}}
Expand Down
Loading