This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

simple code not always working

Hi
I have a function InitRAM
This way it works:

unsigned char Flash_Data[512];
unsigned int AnalogMaxValues[8];
#define AnalogMaxValues_start 129   //byte number of AnalogMaxValues in flash block

void InitRAM()
{
  unsigned char i;
  unsigned int index,index2,a,b;
  memcpy(&Flash_Data, ConfigStartAddress, sizeof(Flash_Data)) ;

  //save Analog related values to SRAM
  for(i=0;i<8;i++)
  {
        index =AnalogMaxValues_start+2*i;
        index2=AnalogMaxValues_start+2*i+1; //index+1;
        a= Flash_Data[index];
        b= Flash_Data[index2];
        AnalogMaxValues[i]     = a+ (b<<8);  //this is a 2 byte variable
  }

}


And AnalogMaxValues[i] have correct values
But the code below should do the same, while it's not working

unsigned char Flash_Data[512];
unsigned int AnalogMaxValues[8];
#define AnalogMaxValues_start 129   //byte number of AnalogMaxValues in flash block

void InitRAM()
{
  unsigned char i;
  unsigned int index,index2,a,b;
  memcpy(&Flash_Data, ConfigStartAddress, sizeof(Flash_Data)) ;

  //save Analog related values to SRAM
  for(i=0;i<8;i++)
  {
        index =AnalogMaxValues_start+2*i;
        index2=AnalogMaxValues_start+2*i+1; //index+1;
        AnalogMaxValues[i] = (int)Flash_Data[index]+ ((int)Flash_Data[index2]<<8);
  }

}


and Flash_Data[index] and Flash_Data[index2] seem to be the same while they are not
I'm using Keil uVision
Do you think I have a problem in the second code?

  • Sorry
    The first code doesn't work either
    I have to use it this way to work:

    unsigned char Flash_Data[512];
    unsigned int AnalogMaxValues[8];
    #define AnalogMaxValues_start 129   //byte number of AnalogMaxValues in flash block
    
    void InitRAM()
    {
      unsigned char i;
      unsigned int index,index2,a,b;
      memcpy(&Flash_Data, ConfigStartAddress, sizeof(Flash_Data)) ;
    
      //save Analog related values to SRAM
      for(i=0;i<8;i++)
      {
            index =AnalogMaxValues_start+2*i;
            index2=index+1;
            a= Flash_Data[index];
            b= Flash_Data[index2];
            AnalogMaxValues[i]     = a+ (b<<8);  //this is a 2 byte variable
      }
    
    }
    

    not index2=index+1;

  • memcpy(AnalogMaxValues, &Flash_Data[AnalogMaxValues_start], sizeof(AnalogMaxValues));

    Better yet make a structure and access it via a pointer.

  • Thanks Westonsupermare
    I'm not sure if this could cause the problem but I'd be happy to write my code better by using structures
    But I don't know how I could do this for this code
    If it's not too many lines of code, would you mind showing me how exactly I could do it?

  • typedef struct _FLASH_DATA {
    
      unsigned RandomClutter[129]; // Better to have some natural alignment
    
      unsigned int AnalogMaxValues[8];
    
      unsigned long AdditionalClutter[4];
    
      unsigned long Checksum; // For integrity perhaps
    
    } FLASH_DATA;
    
    FLASH_DATA * Flash_Data = (FLASH_DATA *)ConfigStartAddress; // Address of your stuff in FLASH memory, wherever it is
    
    unsigned int AnalogMaxValues[8];
    
    void InitRAM()
    {
      memcpy(&AnalogMaxValues[0], &Flash_Data->AnalogMaxValues[0], sizeof(AnalogMaxValues));
    }
    

  • Should have been unsigned char RandomClutter[129]; damn the lack of edit here.