Posted: 09:47am 11 Jun 2016 |
Copy link to clipboard |
 Print this post |
|
I've playing around with a SPI FRAM module and things are working fairly well. I can write data and read it back without any problems. MMBasic has a great SPI feature to write out a block of string data of known length. But, when reading a block of string data back, it is received as numeric data into an array. It's easy to convert the data in the array to a string using CHR$ however it is relatively slow. So, I tried to create CSub to move the data directly into the string. Unfortunately it is not working correctly.
MMBasic Code:
Dim AS Integer msgIn(65), NumOfChars
DIM STRING msg2$ LENGTH 65
FRAMsize = 8192 'Total # of BYTES on FRAM chip
' pin 3 is SPI Out for MicroMite 170
' pin 25 is SPI Clock for MicroMite 170
' Pin 14 is SPI In for MicroMite 170 and is not connected for this application
FRAM_CS = 23 'CS pin for the FRAM
Setpin FRAM_CS, Dout
Pin(FRAM_CS) = 1 ' Set CS to high to deselect chip
SPI OPEN 10000000, 0, 8 'Open SPI port 10Mhz, mode 0, 8 bits
pause 10
'Set start address for FRAM access
FRAMaddr = 0
msg$ = "1234567890123456789012345678901234567890123456789012345678901234"
Print "String to be written to FRAM:"
Print msg$
Print "Length of data to be written to FRAM: "; Len(msg$)
Print
ReadyWrite
WrtAddress (FRAMaddr)
Spi WRITE 64, msg$
CloseWrite
Pin(FRAM_CS) = 1 ' Set CS to high to deselect chip
Print "Data was written to FRAM. Ready to read data back."
Print
NumOfChars = 64
ReadyRead (FRAMaddr)
Spi READ NumOfChars, msgIn()
Print "Data has been read from FRAM"
PRint
msg3$ = ""
for i = 0 to NumOfChars - 1
msg3$ = msg3$ + chr$(msgIn(i))
next i
Print "Conversion of the returned array using CHR$() on each element:"
print msg3$
Print "Length = "; len(msg3$)
Print
msg2$ = STRING$( NumOfChars, "*" ) 'allocate string space for return string
Print "Result returned from C routine:"
Array2STR(msgIn(), msg2$, NumOfChars) 'Convert/move numeric data into string
Print msg2$
Print "Length = "; len(msg2$)
SPI Close
End
Sub ReadyWrite
Pin(FRAM_CS) = 0 ' activate FRAM
AA=spi(&H06) ' Send Write Enable op-code
Pulse FRAM_CS, .01 ' activate op-code
End Sub
Sub WrtAddress (WrtAddr)
Local HiByte, LoByte, HexAddr$
HexAddr$ = HEX$(WrtAddr ,4)
HiByte = Val("&H"+left$(HexAddr$,2))
LoByte = Val("&H"+RIGHT$(HexAddr$,2))
AA=spi(&H02) ' Send Write Op-Code
AA=Spi(HiByte) 'Send address
AA=Spi(LoByte)
End Sub
'Close FRAM Write Operation
Sub CloseWrite
Pulse FRAM_CS, .01 'Pulse CS to Stop writing
AA=spi(&H04) 'Disable Write
End Sub
Sub ReadyRead (ReadAddr)
Local HiByte, LoByte, HexAddr$
Pin(FRAM_CS) = 0 ' activate FRAM
HexAddr$ = HEX$(ReadAddr ,4)
HiByte = Val("&H"+left$(HexAddr$,2))
LoByte = Val("&H"+RIGHT$(HexAddr$,2))
Pin(FRAM_CS) = 0 ' activate FRAM
AA=spi(&H03) ' Send Write Op-Code
AA=Spi(HiByte) 'Send address
AA=Spi(LoByte)
End Sub
CSub Array2STR(Integer, String, Integer)
00000000
8CC20004 5C400006 00001021 14400014 00000000 8CC20000 10400011 00001021
00A21821 8C870000 A0670000 24420001 00021FC3 8CC70004 0067382A 14E0FFF8
24840008 8CC70004 14E30005 00000000 8CC30000 0043402B 1500FFF2 00A21821
03E00008 00000000
End CSub
C Routine:
void Array2STR(long long thearray[], unsigned char thestring[], long long *strlen)
{
int i;
for(i = 0; i < *strlen; i++)
{
thestring = thearray;
} // Next i
} // End of Array2STR
void main(){}
Sample Output:
RUN
String to be written to FRAM:
1234567890123456789012345678901234567890123456789012345678901234
Length of data to be written to FRAM: 64
Data was written to FRAM. Ready to read data back.
Data has been read from FRAM
Conversion of the returned array using CHR$() on each element:
1234567890123456789012345678901234567890123456789012345678901234
Length = 64
Result returned from C routine:
2345678901234567890123456789012345678901234567890
Length = 49
>
What am I doing wrong?
Thanks,
Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |