Need help passing array back from function

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

I have a calling routine (read32reg) that passes a readBuffer to routine (readfromspi). In the readfrom spi routine I do receive correct data (4 bytes) from the device. However, when I pass the array back to read32reg (which have correct values 0x01 0x30 0xCA 0xDE), "buffer" ends up with values 0x01 0x01 0x01 0x01

But, if I set up a local buffer in the spi routine I get correct data in that buffer 0x 01 CA DE

Not sure if I am not handling the pointers properly or maybe something is being optimized out. (Though the debug window does not indicate the buffer elements as optimized out.

Originally I was putting spi values directly into "readBuffer" but I could not access the elements in debug window so I created a local array "rxBuf" that holds received values so I could verify they were correct.

Thanks, code below

//Calling C routine

 

uint32 read32reg(int regFileID,int regOffset)

{

uint32  regval = 0 ;

int     j ;

uint8   buffer[4] ;

dwt_readfromdevice(regFileID,regOffset,4,buffer); // Read 4 bytes (32-bits) register into buffer

    for (j = 3 ; j >= 0 ; j --)

    {

regval = (regval << 😎 + buffer ;

    }

    return regval ;

}

//Declaration in ".h" file

extern int writetospi(uint16 headerLength, const uint8 *headerBuffer, uint32 bodylength, const uint8 *bodyBuffer);

//Routine in ".c" file

int readfromspi(uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer)

{

int i=0;

   volatile unsigned char rxbuf[4];

   

    CyGlobalIntDisable;

CS_Write(0);

    for(i=0; i<headerLength; i++)

    {

  SPIM_WriteTxData(headerBuffer);  //write command to device

while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE)){};  //wait till sent

readBuffer = SPIM_ReadRxData() ; //read any received data

    }

    for(i=0; i<(int)readlength; i++)

    {

SPIM_WriteTxData(0x00);  //dummy write for each byte to read

while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE)){};  //wait till each byte done

//readBuffer = SPIM_ReadRxData() ; //read any received data

rxbuf = SPIM_ReadRxData();

readBuffer = rxbuf[1];

   }

CS_Write(1);

CyGlobalIntEnable;

    return 0;

}

rxbuf[4] 0x20007F14 (All)         volatile unsigned char [4]

0 0x30 '0'           0x20007F14 (All)         volatile unsigned char

1 0x01 '\001'      0x20007F15 (All)         volatile unsigned char

2 0xCA '\312'      0x20007F16 (All)         volatile unsigned char

3 0xDE '\336'      0x20007F17 (All)         volatile unsigned char

buffer [4]        0x20007F5C (All)         uint8 [4]

0 0x01 '\001'      0x20007F5C (All)         unsigned char

1 0x01 '\001'      0x20007F5D (All)        unsigned char

2 0x01 '\001'      0x20007F5E (All) unsigned char

  3 0x01 '\001'      0x20007F5F (All)         unsigned char

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

> readBuffer = rxbuf[1];

You are writing same rxbuf[1] to readBuffer[0] ~ readBuffer[3]

I think that it's probably rxbuf[1] is typo of rxbuf .

moto

View solution in original post

0 Likes
2 Replies