Home  |  Contents 

Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: uM2(+): data logging - cheap fast & easy Post ReplyPost New Topic
<< Prev Page of 9 Next >>
Author
Message << Prev Topic | Next Topic >>
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 6306
Posted: 21 May 2016 at 12:21pm | IP Logged Quote Grogster

Calling getpagecount before attempting a write routine causes a processor exception:





I can't close the SPI2 port, as it is not open, but I can't open it cos it is already open.......





I will ask Geoff about that last one, cos that should not be I don't think.

EDIT: Just so you can see what I mean by 'goto start'.....





UPDATE:

I am getting reading and writing now SO LONG as I don't try to write to the same page more then once. If I do, the data is corrupted. If I make sure I use a new page each time, it reads back fine.





EDIT: Yes, I have confirmed this. If you write to the same page that has been written to before, the data is corrupted. If I issue D$=SPACE$(255) at the command prompt, and then writepage (51,D$,21), that completes fine, and you can read it back with readpage, but if I then try to write some data to page 51 again, it remains as blank spaces - you cannot overwrite a page that has previously been written in other words.

I don't think these logging routines are for me. Matherp obviously wrote them for logging purposes, and I get the distinct feeling I am trying to mutate the Cfunctions etc into something they were never written to be in the first place.

I am now reading the device manual. Perhaps I can write some basic code of my own to manage the pages the way I need to be able to. I'll have a go, anyway.

Edited by Grogster on 21 May 2016 at 1:50pm


__________________
Smoke makes things work. When the smoke gets out, it stops!


Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2497
Posted: 21 May 2016 at 1:44pm | IP Logged Quote matherp

I don't understand why you are making things so difficult - put getpagecount or spi open in the program

any of the commented versions below work perfectly

testdata() 'sets up the testdata generator   
'   
'formatlog(chipselectpin) 'format the chip   
'spi2 open 10000000,3,8
i=getpagecount(d$,chipselectpin%)
'   
timer=0   
for i=1 to 1000 'log 1,000 strings  
  s$=int2Text(i) 
  s$=s$+space$(255-len(s$)) 
  writepage(i,s$,chipselectpin%)   
next i   
print "1,000 records constructed and logged in ",timer\1000," seconds"   
pause 3000   
for i=1 to 1000 'log 1,000 strings  
  readpage(i,s$,chipselectpin%)   
  print s$ 
next i   
'   
end
Back to Top View matherp's Profile Search for other posts by matherp
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 6306
Posted: 21 May 2016 at 1:53pm | IP Logged Quote Grogster

matherp wrote:
I don't understand why you are making things so difficult - put getpagecount or spi open in the program

any of the commented versions below work perfectly


I am NOT being difficult - I kinda resent that.

I think the issue is that your code was designed for logging purposes, and I need it for random-access purposes. From what I can see, the code you wrote is designed specifically for logging, whereas I need to be able to write to a page, read it, and then write TO THE SAME PAGE AGAIN several times, as I want to use the pages of the memory to story a text-based database.

spi open does not work, as the memory is on SPI2, and I can't open the port - I get the error that it is already open etc - see above.

I have put getpagecount in there, and that is working OK now, but not for writing to the same page more then once.



Edited by Grogster on 21 May 2016 at 1:55pm


__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2497
Posted: 21 May 2016 at 1:59pm | IP Logged Quote matherp

Quote:
I am NOT being difficult


I mean't you are mixing command line and program. Just put everything in the program and it works. When you run a program Geoff's firmware does various things as initialisation that can affect I/O. With everything in the program you can be certain of the status of the I/O.

The program doesn't know or care whether you are doing random access or not. If you look at my code you will see that the logging commands are reading and writing from multiple pages of the flash to maintain the index and update pages
Back to Top View matherp's Profile Search for other posts by matherp
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 6306
Posted: 21 May 2016 at 2:06pm | IP Logged Quote Grogster

matherp wrote:
I mean't you are mixing command line and program. Just put everything in the program and it works.


Oh, I see.
I've done that before heaps of times(calling from command prompt), and it normally is OK to do that. That was normal basic code though, not anything involving Cfunction or Csub routines. Is that a no-no?

I will make some more changes......


Edited by Grogster on 21 May 2016 at 2:07pm


__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 6306
Posted: 21 May 2016 at 2:33pm | IP Logged Quote Grogster

OK, my test code all off the command line, but I still cannot write to the same page more then once. I can read and write though, so that's progress!


My code:


Option explicit
Option default none
Dim integer i,x,P
Const chipselectpin%=21 'chip select pin
Dim s$,B$,D$
'
'  Example program to demonstrate logging to a serial flash chip mounted on a SSD1963
'  Compatible with 64 and 100 pin MM+ processors
'
'testdata() 'sets up the testdata generator
'
'formatlog(chipselectpin) 'format the chip
'

START:
Input "String: ";D$
If D$="" Then
  Input "Page to read: ";P
  x=getpagecount(B$,21) 'Wake up SPI
  readpage (P,D$,21)
  Print D$
  GoTo START
EndIf
Do
  D$=D$+Chr$(42)
  If Len(D$)=255 Then Exit Do
Loop
Print D$;
Print Str$(Len(D$))
Input "Page: ";P
x=getpagecount(B$,21) 'Wake up SPI
writepage (P,D$,21)
Print "Done."
Pause 500
Print "Now attempting to read page " + Str$(P)
'x=getpagecount(B$,21) 'Wake up SPI
readpage (P,B$,21)
Print B$
Print:Print
GoTo START

End


My results - note how I can write and read page 60 in this example fine the first time around, but if I try to write to page 60 again, it is corrupted:





So, it is working, but I can't make it write anything different to the same page more then once. I can select any page number I like, and the FIRST write to that page reads back fine(as many times as I want), INCLUDING previous tests to other pages, but if I try to write something else to the same page, the readback is corrupted as in the image above. I suspect the writing routine, cos the readback is always the same, even though it is corrupted. I have no idea why that should be.

I can readback any page at will by just pressing ENTER when asked for a string, and that also returns corrupted data IF I have tried to write to that page more then once. It makes no difference if "'x=getpagecount(B$,21) 'Wake up SPI" right below "Now attempting to read page" is commented out or not.

This keeps all my testing inside a running code, no nothing on the command line anymore.

EDIT: Here is another example with page 65 this time:





Edited by Grogster on 21 May 2016 at 2:45pm


__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2497
Posted: 21 May 2016 at 2:47pm | IP Logged Quote matherp

Quote:
but I still cannot write to the same page more then once. I can read and write though, so that's progress!


This is how SPI flash works. It is erased to all &HFF and then writes can change bits to 0. You can only get them back to a 1 by erasing the page. Pages can only be erased in blocks of 4K

To re-write an individual page you would have to read in the requisite 4k block, erase the block and then re-write the block with the new page updated.

I hadn't understood this is what you were wanting to do. Give me an hour or two and I'll write a block erase CFunction
Back to Top View matherp's Profile Search for other posts by matherp
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 6306
Posted: 21 May 2016 at 2:54pm | IP Logged Quote Grogster

Oh - that IS different to I2C which is what I thought it essentially was, but just with an SPI interface instead of I2C....

I'm not sure WHY I thought that, but for some reason I thought you could just overwrite a page with new data no problems.

If it's going to be a pain for you to have to write more code - I don't want to be a pain.......

__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2497
Posted: 22 May 2016 at 7:58am | IP Logged Quote matherp

Grogster

In this demo I've added a new function rewritepage.

This takes the same parameters as write page: the page to write, the data to be written, and the CS pin number. It works by reading the sector (16 pages) containing the page in question into buffer memory (4096 bytes) and then erasing the sector. It then re-writes the sector with the original contents except for the page requested which is replaced with the new data. Of course this takes quite a lot of I/O so it does take some time (approx 60msec on a Winbond 25Q32)

This took very little time to write but ages to debug because....

WARNING

If you are using a flash chip on a TFT display you must ensure the CS pins of the touch controller and the SD card (if inserted) are held high. This is done automatically if touch and SD are configured (OPTION TOUCH etc.). Otherwise it appears electrical noise can cause the touch controller to just very occasionally result in miss-reads from the flash.

NB I've added a simple Basic subroutine to initialise the I/O and optionally erase the chip. This should work whether or not the Touch and SD card are in use as long as their CS lines are held high

option explicit   
option default none   
dim integer i   
const chipselectpin%=21 'chip select pin   
dim s$ ,d$ ,t$

'   
'  Example program to demonstrate page mode access to a serial flash chip mounted on a SSD1963  
'  Compatible with 64 and 100 pin MM+ processors  
'   
testdata() 'sets up the testdata generator   
'   
initchip(1,chipselectpin%) 'iniitialise the chip with full erase
s$=space$(255)  '   
for i=1 to 100 
  writepage(i,s$,chipselectpin%)   'write out a page of spaces
  d$="spoil" 'make sure we can't cheat
  readpage(i,d$,chipselectpin%)   'read in the page
  if d$<>s$ then 'prove the page is all spaces
    print "error in read 1"
    end
  endif
  t$=int2Text(i) 
  t$=t$+space$(255-len(t$)) 
  timer = 0
  rewritepage(i,t$,chipselectpin%) 'rewrite the page
  if i=10 then print timer
  readpage(i,d$,chipselectpin%)   'read in the updated page
  if d$<>t$ then 'prove the page is as written
    print "error in read 2"
    end
  endif
next i  
for i=1 to 100
  readpage(i,d$,chipselectpin%)   'read in the updated page
  print d$  
next i
end   
'   
'****************************************   


sub initchip(eraseit as integer, cspin as integer)
  local integer i=getpagecount(d$,cspin)*256
  if eraseit then
    if not (erasechip(i,chipselectpin%)) then  
      print "erase failed"  
      end  
    endif  
    print "Erase complete" 
  endif
end sub
'
CFunction getpagecount
    00000000
    27BDFFC8 AFB30028 AFBF0034 AFB50030 AFB4002C AFB20024 AFB10020 AFB0001C 
    8CB10000 3C029D00 8C420088 00111880 00621021 8C420000 24030008 10430007 
    00809821 3C029D00 8C420010 02202021 24050008 0040F809 00003021 3C02BF81 
    8C44F220 3C029D00 8C430088 3C020580 7C84D800 3442B053 00821026 24050014 
    24040030 00A2200B 00831021 8C430000 24020065 1062006F 3C02BF81 8C45F220 
    3C030580 3C029D00 8C420010 3463B053 7CA5D800 00A32826 24040005 2403000C 
    0065200A 00003021 0040F809 24050008 3C02BF81 8C45F220 3C030580 3C029D00 
    8C420010 3463B053 7CA5D800 00A32826 24040005 2403000C 0065200A 00003021 
    0040F809 24050064 3C02BF81 8C45F220 3C030580 3C029D00 8C420010 3463B053 
    7CA5D800 00A32826 2404002F 2403000B 0065200A 00003021 0040F809 24050002 
    3C02BF81 8C45F220 3C030580 3C029D00 8C420010 3463B053 7CA5D800 00A32826 
    2404002F 2403000B 0065200A 00003021 0040F809 24050064 3C02BF81 8C45F220 
    3C030580 3C029D00 8C420010 3463B053 7CA5D800 00A32826 24040004 2403000A 
    0065200A 00003021 0040F809 24050008 3C02BF81 8C45F220 3C030580 3C029D00 
    8C420010 3463B053 7CA5D800 00A32826 24040004 2403000A 0065200A 00003021 
    0040F809 24050064 3C02BF81 8C43F220 3C020580 3442B053 7C63D800 1462000C 
    3C03BF81 8C64FA90 24050001 3C02BF81 7CA41804 AC64FA90 8C43FCA0 24040006 
    7C831804 AC43FCA0 1000000B 3C029D00 8C64FA90 24050007 3C02BF81 7CA41804 
    AC64FA90 8C43FC9C 24040006 7C831804 AC43FC9C 3C029D00 8C42001C 02202021 
    0040F809 24050006 3C02BF80 34038060 24040C00 AC435A00 AC445A40 24040001 
    AC445A30 3C03BF80 AC405A20 8C625A10 30420080 1040FFFD 3C029D00 8C42001C 
    3C10BF80 02202021 24050005 8E125A20 0040F809 00000000 2402009F AE025A20 
    3C03BF80 8C625A10 30420080 1040FFFD 3C02BF80 8C435A20 3C03BF80 AC405A20 
    8C625A10 30420080 1040FFFD 3C02BF80 8C505A20 3C03BF80 AC405A20 8C625A10 
    30420080 1040FFFD 3C02BF80 8C555A20 3C03BF80 AC525A20 8C625A10 30420080 
    1040FFFD 3C129D00 8E42001C 3C03BF80 02202021 24050006 8C745A20 0040F809 
    00108200 24020010 AFA20010 02158021 8E420030 00108200 02148021 26640001 
    02003021 0040F809 00103FC3 3C02FF40 3442D9BF 02021021 24030006 2C420002 
    10400027 A2630000 3C029D00 8C42001C 02202021 0040F809 24050005 24030006 
    3C02BF80 AC435A20 3C03BF80 8C625A10 30420080 1040FFFD 3C129D00 8E42001C 
    3C13BF80 02202021 24050006 8E635A20 0040F809 00000000 8E42001C 02202021 
    0040F809 24050005 24020098 AE625A20 3C03BF80 8C625A10 30420080 1040FFFD 
    3C029D00 8C42001C 3C03BF80 02202021 24050006 8C635A20 0040F809 00000000 
    3C0200BF 24422642 24044000 12020012 00002821 3C0200BF 24422602 1202000E 
    3C0200BF 24422641 1202000B 24042000 3C0200BF 24422601 12020006 2694FFF8 
    24020001 0282A004 02802021 10000002 00142FC3 00002821 8FBF0034 00801021 
    00A01821 8FB50030 8FB4002C 8FB30028 8FB20024 8FB10020 8FB0001C 03E00008 
    27BD0038 
End CFunction
'
CFunction erasechip
    00000000
    27BDFFD8 AFB40020 AFB00010 AFBF0024 AFB3001C AFB20018 AFB10014 8CB30000 
    3C029D00 8C42001C 24030001 3C10BF80 AE035A30 0080A021 24050005 0040F809 
    02602021 24020006 AE025A20 3C03BF80 8C625A10 30420080 1040FFFD 3C109D00 
    8E02001C 3C11BF80 02602021 24050006 8E235A20 0040F809 00000000 8E02001C 
    02602021 0040F809 24050005 240200C7 AE225A20 3C03BF80 8C625A10 30420080 
    1040FFFD 3C029D00 8C42001C 02602021 24050006 3C03BF80 8C635A20 0040F809 
    3C119D00 3C10BF80 8E220004 0040F809 3404C350 8E22001C 02602021 0040F809 
    24050005 24020005 AE025A20 8E025A10 30420080 1040FFFD 00000000 8E025A20 
    AE125A20 8E025A10 30420080 1040FFFD 00000000 8E125A20 8E22001C 02602021 
    7C129420 0040F809 24050006 32420001 5440FFE6 8E220004 3C029D00 8C42001C 
    02602021 0040F809 24050005 24030003 3C02BF80 AC435A20 3C03BF80 8C625A10 
    30420080 1040FFFD 3C02BF80 8C435A20 3C03BF80 AC405A20 8C625A10 30420080 
    1040FFFD 3C02BF80 8C435A20 3C03BF80 AC405A20 8C625A10 30420080 1040FFFD 
    3C02BF80 8C435A20 3C03BF80 AC405A20 8C625A10 30420080 1040FFFD 00000000 
    8E820004 3C03BF80 8C635A20 5C400006 3C02BF80 14400014 3C029D00 8E820000 
    10400010 3C02BF80 AC525A20 3C03BF80 8C625A10 30420080 1040FFFD 3C029D00 
    8C42001C 02602021 24050006 3C03BF80 8C635A20 0040F809 00000000 10000007 
    00001021 3C029D00 8C42001C 02602021 0040F809 24050006 24020001 8FBF0024 
    8FB40020 8FB3001C 8FB20018 8FB10014 8FB00010 03E00008 27BD0028 
End CFunction
'
CSUB writepage
    00000000
    27BDFFD8 AFB3001C AFB10014 AFB00010 AFBF0024 AFB40020 AFB20018 8CD30000 
    3C029D00 8C42001C 8C940000 24030001 3C10BF80 AE035A30 00A08821 02602021 
    0040F809 24050005 24020006 0014A200 AE025A20 3C03BF80 8C625A10 30420080 
    1040FFFD 3C109D00 8E02001C 3C12BF80 02602021 24050006 8E435A20 0040F809 
    00000000 8E02001C 02602021 0040F809 24050005 24020002 AE425A20 3C03BF80 
    8C625A10 30420080 1040FFFD 3C02BF80 7E843C00 8C435A20 3C03BF80 AC445A20 
    8C625A10 30420080 1040FFFD 3C02BF80 7E943A00 8C435A20 3C03BF80 AC545A20 
    8C625A10 30420080 1040FFFD 3C02BF80 8C435A20 3C03BF80 AC405A20 8C625A10 
    30420080 1040FFFD 3C02BF80 8C425A20 00008021 3C03BF80 24040100 02301021 
    80420000 AC625A20 8C625A10 30420080 1040FFFD 00000000 26100001 8C625A20 
    1604FFF7 02301021 3C029D00 8C42001C 02602021 0040F809 24050006 3C129D00 
    3C11BF80 8E420004 0040F809 24040064 8E42001C 02602021 0040F809 24050005 
    24020005 AE225A20 8E225A10 30420080 1040FFFD 00000000 8E225A20 AE305A20 
    8E225A10 30420080 1040FFFD 00000000 8E305A20 8E42001C 02602021 0040F809 
    24050006 32020001 5440FFE7 8E420004 8FBF0024 8FB40020 8FB3001C 8FB20018 
    8FB10014 8FB00010 03E00008 27BD0028 
End CSUB
'
CSUB readpage
    00000000
    27BDFFD8 AFB3001C AFB20018 AFB10014 AFBF0024 AFB40020 AFB00010 8CD20000 
    3C029D00 8C42001C 8C940000 24030001 3C13BF80 AE635A30 00A08821 02402021 
    0040F809 24050005 24020003 0014A200 AE625A20 3C03BF80 8C625A10 30420080 
    1040FFFD 3C02BF80 7E843C00 8C435A20 3C03BF80 AC445A20 8C625A10 30420080 
    1040FFFD 3C02BF80 7E943A00 8C435A20 3C03BF80 AC545A20 8C625A10 30420080 
    1040FFFD 3C02BF80 8C435A20 3C03BF80 AC405A20 8C625A10 30420080 1040FFFD 
    3C02BF80 8C425A20 00002021 3C03BF80 24050100 AC705A20 8C625A10 30420080 
    1040FFFD 00000000 8C705A20 02241021 24840001 1485FFF7 A0500000 3C029D00 
    8C42001C 02402021 0040F809 24050006 8FBF0024 8FB40020 8FB3001C 8FB20018 
    8FB10014 8FB00010 03E00008 27BD0028 
End CSUB
'
CSUB rewritepage
    00000000
    27BDFFC0 AFBF003C AFB70034 AFB5002C AFB40028 AFB30024 AFB20020 AFB1001C 
    AFBE0038 3C119D00 AFB60030 AFB00018 8E22003C 00809021 24041000 8CD30000 
    0040F809 00A0A021 8E430000 0040B821 8E22001C 24040001 3C11BF80 3C1500FF 
    00031A00 AE245A30 24050005 02602021 36B5F000 30720FFF 0040F809 0075A824 
    24020003 AE225A20 3C02BF80 8C435A10 30630080 1060FFFD 0015B402 3C02BF80 
    8C435A20 3C03BF80 AC565A20 8C625A10 30420080 1040FFFD 0015F203 3C02BF80 
    33DE00FF 8C435A20 3C03BF80 AC5E5A20 8C625A10 30420080 1040FFFD 3C02BF80 
    32A300FF AFA30010 8FA40010 8C435A20 3C03BF80 AC445A20 8C625A10 30420080 
    1040FFFD 3C02BF80 8C425A20 02E08821 26E51000 02E02021 3C03BF80 AC705A20 
    8C625A10 30420080 1040FFFD 00000000 8C705A20 A0900000 24840001 1485FFF7 
    26420100 0242102A 10400008 26420100 02E21021 02F29021 92830000 A2430000 
    26520001 1642FFFC 26940001 3C129D00 8E42001C 02602021 0040F809 24050006 
    8E42001C 02602021 0040F809 24050005 24030006 3C02BF80 AC435A20 8C435A10 
    30630080 1060FFFD 3C129D00 8E42001C 3C14BF80 02602021 24050006 8E835A20 
    0040F809 00000000 8E42001C 02602021 0040F809 24050005 24020020 AE825A20 
    3C02BF80 8C435A10 30630080 1060FFFD 00000000 3C02BF80 8C435A20 3C03BF80 
    AC565A20 8C625A10 30420080 1040FFFD 3C02BF80 8C435A20 3C03BF80 AC5E5A20 
    8C625A10 30420080 1040FFFD 8FA40010 3C02BF80 8C435A20 3C03BF80 AC445A20 
    8C625A10 30420080 1040FFFD 3C029D00 8C42001C 02602021 24050006 3C03BF80 
    8C635A20 0040F809 3C149D00 3C12BF80 8E820004 0040F809 24040064 8E82001C 
    02602021 0040F809 24050005 24020005 AE425A20 8E425A10 30420080 1040FFFD 
    00000000 8E425A20 AE505A20 8E425A10 30420080 1040FFFD 00000000 8E505A20 
    8E82001C 02602021 0040F809 24050006 32020001 5440FFE7 8E820004 26BE1000 
    02BE102A 1040005E 02A0B021 3C129D00 3C10BF80 24150006 8E42001C 02602021 
    0040F809 24050005 AE155A20 8E025A10 30420080 1040FFFD 02602021 8E42001C 
    02A02821 8E035A20 0040F809 00000000 8E42001C 02602021 0040F809 24050005 
    24020002 AE025A20 8E025A10 30420080 1040FFFD 7EC23C00 8E035A20 AE025A20 
    8E025A10 30420080 1040FFFD 7EC23A00 8E035A20 AE025A20 8E025A10 30420080 
    1040FFFD 32C200FF 8E035A20 AE025A20 8E025A10 30420080 1040FFFD 00000000 
    8E025A20 26230100 92220000 AE025A20 8E025A10 30420080 1040FFFD 00000000 
    26310001 8E025A20 5623FFF8 92220000 8E42001C 02602021 0040F809 02A02821 
    24140100 8E420004 0040F809 24040064 8E42001C 02602021 0040F809 24050005 
    24020005 AE025A20 8E025A10 30420080 1040FFFD 00000000 8E025A20 AE145A20 
    8E025A10 30420080 1040FFFD 00000000 8E145A20 8E42001C 02602021 0040F809 
    02A02821 32820001 5440FFE7 8E420004 26D60100 02DE102A 5440FFA8 8E42001C 
    3C029D00 8C420044 0040F809 02E02021 8FBF003C 8FBE0038 8FB70034 8FB60030 
    8FB5002C 8FB40028 8FB30024 8FB20020 8FB1001C 8FB00018 03E00008 27BD0040 
End CSUB




'


'   
'****************************   
' test data generation   
'   
'small   
sub testdata   
  DATA "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"   
  DATA "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"   
'tens   
  DATA "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"   
'big   
  DATA "thousand", "million", "billion"   
  dim small(19) AS STRING length 10, tens(7) AS STRING length 10, big(2) AS STRING length 10   
  FOR i = 1 TO 19   
    READ small(i)   
  NEXT   
  FOR i = 0 TO 7   
    READ tens(i)   
  NEXT   
  FOR i = 0 TO 2   
    READ big(i)   
  NEXT   
'   
end sub   
.   
FUNCTION int2Text(number AS integer) as string   
    local num AS integer, outP AS STRING length 60, unit AS INTEGER   
    local tmpLng1 AS integer   
    
    IF 0 = number THEN   
        int2Text = "zero"   
        EXIT FUNCTION   
    END IF   
    
    num = ABS(number)   
    
    DO   
        tmpLng1 = num MOD 100   
        SELECT CASE tmpLng1   
            CASE 1 TO 19   
                outP = small(tmpLng1) + " " + outP   
            CASE 20 TO 99   
                SELECT CASE tmpLng1 MOD 10   
                    CASE 0   
                        outP = tens((tmpLng1 \ 10) - 2) + " " + outP   
                    CASE ELSE   
                        outP = tens((tmpLng1 \ 10) - 2) + "-" + small(tmpLng1 MOD 10) + " " + outP   
                END SELECT   
        END SELECT   
    
        tmpLng1 = (num MOD 1000) \ 100   
        IF tmpLng1 THEN   
            outP = small(tmpLng1) + " hundred " + outP   
        END IF   
    
        num = num \ 1000   
        IF num < 1 THEN EXIT DO   
    
        tmpLng1 = num MOD 1000   
        IF tmpLng1 THEN outP = big(unit) + " " + outP   
    
        unit = unit + 1   
    LOOP   
    
    IF number < 0 THEN outP = "negative " + outP   
    Do WHILE ASC(RIGHT$(outp,1))<=32   
       outp = LEFT$(outp,len(outp)-1)    
    loop   
    int2Text = outP   
END FUNCTION   
'*********************************


Edited by matherp on 22 May 2016 at 8:00am
Back to Top View matherp's Profile Search for other posts by matherp
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 6306
Posted: 22 May 2016 at 11:48pm | IP Logged Quote Grogster

Thanks a ton.

I will try this out ASAP.

__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 6306
Posted: 23 May 2016 at 1:02pm | IP Logged Quote Grogster

Oh, matherp, you are a champ - this is working perfectly.

I don't care about the 60mS sector rewrite time - anything less then 500mS is fine with me, as I am only storing text in each page, so speed is not any real concern(for me), only that I can easily store and readback pages any time I want.





...and my test code:(I ran your example code first, before I started tinkering)





...and my memory-module board:








Now I can get to work on writing the rest of the code to use this module and your Cfunction/CSub code to talk to the memory easily.

__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2497
Posted: 23 May 2016 at 2:44pm | IP Logged Quote matherp

Glad it is working for you

If anyone wants a version for the MX170 (SPI or SPI2) let me know.

For those interested here is the C-source of the new function

void rewritepage(long long *address, unsigned char d[], long long *cspin){
    int m,k,add,pin=*cspin,sec;
    unsigned char *p;
    p=GetMemory(4096);
    sec=*address<<8; //convert page number to byte number
    add=sec & 0xFFF; //convert page number to byte position within sector
    sec=sec & 0xFFF000; //convert to 4KB sector address
    SPIBRG=1;
    PinSetBit(pin,LATCLR);
    SPIsend(readdata); 
    SPIsend((sec>>16) & 0xFF);
    SPIsend((sec>>8) & 0xFF);
    SPIsend(sec & 0xFF);
    for(k=0;k<4096;k++){ //read in the sector into local memory
        SPIread(m); 
        p[k]=m;
    }
    for(k=add;k<add+256;k++)p[k]=d[k-add];//set the relevant page to the new data
    PinSetBit(pin,LATSET);
    // now erase the sector
    PinSetBit(pin,LATCLR);
    SPIsend(writeenable);
    PinSetBit(pin,LATSET);
    PinSetBit(pin,LATCLR);
    SPIsend(sectorerase); 
    SPIsend((sec>>16) & 0xFF);
    SPIsend((sec>>8) & 0xFF);
    SPIsend(sec & 0xFF);
    PinSetBit(pin,LATSET);
    do { //wait for the erase to complete
        uSec(100);
        PinSetBit(pin,LATCLR);
        SPIsend(readstatus1)
        SPIread(m);
        PinSetBit(pin,LATSET);
    } while(m & 1);
    k=0;
    for(add=sec;add<sec+4096;add+=256){ //write out the sector a page at a time
        PinSetBit(pin,LATCLR);
        SPIsend(writeenable);
        PinSetBit(pin,LATSET);
        PinSetBit(pin,LATCLR);
        SPIsend(pageprogram); 
        SPIsend((add>>16) & 0xFF);
        SPIsend((add>>8) & 0xFF);
        SPIsend(add & 0xFF);
        for(m=0;m<256;m++) {
            SPIsend(p[k]); 
            k++;
        }
        PinSetBit(pin,LATSET);
        do {
            uSec(100);
            PinSetBit(pin,LATCLR);
            SPIsend(readstatus1)
            SPIread(m);
            PinSetBit(pin,LATSET);
        } while(m & 1);
    }
    FreeMemory(p);
}


Edited by matherp on 23 May 2016 at 2:50pm
Back to Top View matherp's Profile Search for other posts by matherp
 


<< Prev Page of 9 Next >>
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1563 seconds.
Privacy Policy     Process times : 0, 0, 0, 0.16