Returning a 256 Byte Array from a User Module

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Returning a 256 Byte Array from a User Module

Post by ohararp » Thu Jan 15, 2009 6:39 pm

When declaring functions I understand you cannot return a byte array. However, I am trying to create a user module for the M25P128 (128Mbit memory), and need to return the values from a page read. Right now I am addressing a global byte array to get the values after the page write.

Is there somehow a way to return this byte array from either the function I created or via a module such the code could be called

Code: Select all

PageData = M25PReadPage(0)
Instead of the way I access the data now

Code: Select all

M25PReadPage(0)
PageData = M25PData
Below is the function I have below and M25PData is the global variable in which the data is written.

Code: Select all

Function M25PReadPage(pM25PDataAddress as byte) As Byte
    M25PReadStatus()                                            ' Check its not in the middle of a current write
    While WIP = 1
        M25PReReadStatus() 
    Wend 
    M25PDisable()                                               ' Disable to ensure the read status is stopped
    DelayUS(4)
    M25PEnable()                                                ' Enable the chip again   
    Shift.Out(MSB_FIRST,M25P_Read,8)                            ' Sent the data read instruction
    Shift.Out(MSB_FIRST,pM25PDataAddress.BYTE2,8)                ' Send the address
    Shift.Out(MSB_FIRST,pM25PDataAddress.BYTE1,8)
    Shift.Out(MSB_FIRST,pM25PDataAddress.BYTE0,8)

    For M25PIndex = 0 To 255                                    
        M25PData(M25PIndex) = Shift.In(MSB_POST,8)
    Next 
    M25PDisable()                                                 
End Function
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Thu Jan 15, 2009 6:50 pm

Could you pass the array by reference? I'll have to check this.

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Thu Jan 15, 2009 7:23 pm

I can pass the array in as a reference, but I am not sure if I can do it out. Below is the routine to write up to 256 bytes

Code: Select all

Sub M25PWriteBytes(pM25PDataAddress,ByRef pDATA() As Byte)
    M25PReadStatus()                                            ' Check its not in the middle of a current write
    While WIP = 1
        M25PReReadStatus()
    Wend
        M25PDisable()                                           ' Disable to ensure the read status is stopped
    DelayUS(4)
    M25PEnable()                                                ' Enable the chip again
    M25PWriteEnable()                                           ' Write enable the device
    M25PEnable()                                                ' Enable the device
    Shift.Out(MSB_FIRST,M25P_PP,8)                              ' Sent the Page Write instruction
    Shift.Out(MSB_FIRST,pM25PDataAddress.BYTE2,8)                ' Send the address
    Shift.Out(MSB_FIRST,pM25PDataAddress.BYTE1,8)
    Shift.Out(MSB_FIRST,pM25PDataAddress.BYTE0,8)
  
    For M25PIndex = 0 To 255' Bound(pDATA)                       ' Via FRS0 send all the data requested
        Shift.Out(MSB_FIRST,M25PData(M25PIndex),8)
    Next 
    M25PDisable()                                               ' Finish the write by bringing the CS line high
End Sub
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

nigle
Registered User
Registered User
Posts: 36
Joined: Tue Aug 12, 2008 3:13 pm
Location: West London

Post by nigle » Fri Jan 16, 2009 1:27 pm

ohararp wrote:I can pass the array in as a reference.
Not quite right - you are passing a reference to the array in to the function. Subtle but important difference!

The function can then read or write to that array as required, which will acheive what you need.

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Fri Jan 16, 2009 2:55 pm

nigle,

Nice catch. I really needed to have a phone conversation with someone on the Byref and Byval abilities of functions. Thanks for the tidbit on the Byref.

Using your comments and taking another look at the help section I found this:

This is in contrast to passing a variable by reference (byref). Passing by reference means that a subroutine or function receiving the variable can modify the contents of the variable being passed. This is sometimes referred to as a variable parameter.
Thanks for the help and clarification.
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

Post Reply