CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PCD: Problem reading data from program_memory

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
krogers



Joined: 10 Apr 2009
Posts: 4

View user's profile Send private message

PCD: Problem reading data from program_memory
PostPosted: Tue May 05, 2009 10:07 am     Reply with quote

I'm trying to use the program memory in a PIC24FJ128GA006 as non-volatile storage, but I'm not getting all the bytes that I write to it back from it.

Using PCD v 4.091.


Code:

void main(void) {
  char flash_read[512] = {0};
  int16 flash_size = 0;
  int1 read_from_flash = FALSE;
 
  // Initialize PIC configuration (set clock and pins)
  Init();
 
  set_timer1(0);
  enable_interrupts(int_rda);
  enable_interrupts(int_rda2);
  enable_interrupts(int_rtc);
  enable_interrupts(INTR_GLOBAL);
  while(TRUE)
  {
     if(last_rcvd == '!')
     {
       fprintf(payload, "writing %s to flash\r\n", sensor_buffer);
       flash_size = writeToFlash(sensor_buffer, sb_index);
       //write_rom_memory(FLASH_START, sensor_buffer, sb_index);
       sb_index = 0;
       sensor_buffer[0] = 0;
       read_from_flash = TRUE;
       last_rcvd = 0;
     }
     if(read_from_flash)
     {
        int size_read = 0;
        //fprintf(payload, "flash_size = %d\r\n", flash_size);
        size_read = readFromFlash(flash_read, flash_size);
        //read_rom_memory(FLASH_START, flash_read, flash_size);
        flash_read[size_read] = 0;
        fprintf(payload, "Read %d bytes from flash: %s\r\n", size_read, flash_read);
        flash_size = 0;
        read_from_flash = FALSE;
     }
  }
}
/*******************************************************************************
 * Writes buffer to flash for storage.  The flash on the PIC24 was designed to
 * hold 24bit instructions, and therefore converts every 4th byte to NULL
 * when being written to.  This function compensates for that to prevent data
 * loss/corruption.
 * @param buffer char pointer to the data to be written
 * @param count  int16 number of bytes to write to flash.
 * @returns number of bytes written to flash
 ******************************************************************************/
int writeToFlash(char* buffer, int16 count)
{
   int16 write_count = 0;
   char write_buffer[640] = {0};
   int16 i = 0;
   
   erase_program_memory(FLASH_START);
   for(i = 0; i < count; i++)
   {
      /* skip every 4th byte starting with buffer[3] */
      if(write_count != 0 && ((write_count + 1) % 4 == 0))
      {
         write_count++;
      }
      write_buffer[write_count++] = buffer[i];
      //fprintf(payload, "write_buffer[%d] = %d, buffer[%d] = %d\r\n", (write_count - 1), write_buffer[write_count-1], i, buffer[i]);
   }
   write_count++;
   fprintf(payload, "writing %d bytes to flash\r\n", write_count);
   write_program_memory(FLASH_START, write_buffer, write_count);
   return write_count;
}

/*******************************************************************************
 * Reads from flash storage, skips every 4th byte to remove the NULL byte
 * written by the hardware when writing to the flash memory.
 * @param buffer char pointer for the data to be written into
 * @param count  int16 number of bytes to read from the flash.
 *               NOTE: This number is the number of bytes written to the flash
 *               INCLUDING the NULLs, not the number of bytes of data given
 *               to the writeToFlash call.
 * @returns number of bytes read without the NULL bytes.
 ******************************************************************************/
int readFromFlash(char* buffer, int16 count)
{
   char read_buffer[640] = {0};
   int16 i = 0, j = 0;
   
   //fprintf(payload, "reading %d bytes from FLASH_START\r\n", count);
   read_program_memory(FLASH_START, read_buffer, count);
   for(i = 0; i < count; i++)
   {
      /* skip every 4th byte starting with 3 */
      if(i != 0 && ((i + 1) % 4 == 0))
      {
         continue;
      }
      buffer[j++] = read_buffer[i];
      //fprintf(payload, "buffer[%d] = %d, read_buffer[%d] = %d\r\n", (j - 1), buffer[j-1], i, read_buffer[i]);
   }
   //free(read_buffer);
   return j;
}

#int_rda2
void serial_isr2()
{
   if(sb_index > 510)
      sb_index = 0;
   sensor_buffer[sb_index] = fgetc(sensor);
   last_rcvd = sensor_buffer[sb_index];
   sb_index +=1;
   return;
}


Here's the output I'm getting when I run this.

Compiled on 05-May-09 at 11:39:54
writing 01234! to flash
writing 8 bytes to flash
Read 6 bytes from flash: 01234!
writing 012345! to flash
writing 10 bytes to flash
Read 8 bytes from flash: 012345
writing 0123456! to flash
writing 11 bytes to flash
Read 9 bytes from flash: 012345
writing 01234567! to flash
writing 12 bytes to flash
Read 9 bytes from flash: 01234567!
writing 012345678! to flash
writing 14 bytes to flash
Read 11 bytes from flash: 012345678

the bytes that don't show up in the read are showing up as -1 if printed as a signed decimal, which is what the flash should be erased to, as I understand it.

I'm suspecting there's some sort of word boundary issue at work here, but I can't find anything that tells me this is actually so.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue May 05, 2009 2:29 pm     Reply with quote

Apart from some minor inconsistencies in your code, e.g. no \0 delimiter set to sensor_buffer, the main problem is, that the byte count in write_program_memory() must be apparently rounded up to a multiple of four to write the data in the last word.

You can't count on the compiler doc in this regard.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group