2 Replies Latest reply on Feb 21, 2019 12:29 PM by DaHu_285096

    Need help passing array back from function


      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 << 8) + buffer[j] ;


          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];





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


        SPIM_WriteTxData(headerBuffer[i]);  //write command to device

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

      readBuffer[i] = 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[i] = SPIM_ReadRxData() ; //read any received data

      rxbuf[i] = SPIM_ReadRxData();

      readBuffer[i] = rxbuf[1];







          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