Example #1
0
unsigned write_flash(unsigned * dst, char * src, unsigned no_of_bytes)
{
  unsigned enabled_irqs;
  
  enabled_irqs = NVIC->ISER[0];

  if (flash_address == (unsigned *)UPDATE_REQD)
	{
	  /* Store flash start address */
	  flash_address = (unsigned *)dst;
	}
	memcpy(&flash_buf[byte_ctr],src,no_of_bytes);
  byte_ctr = byte_ctr + no_of_bytes;

	if( byte_ctr == FLASH_BUF_SIZE)
	{
	  /* We have accumulated enough bytes to trigger a flash write */
    NVIC->ICER[0] = enabled_irqs; 
	  find_erase_prepare_sector(cclk, (unsigned)flash_address);
      if(result_table[0] != CMD_SUCCESS)
      {
        while(1); /* No way to recover. Just let Windows report a write failure */
      }
      write_data(cclk,(unsigned)flash_address,(unsigned *)flash_buf,FLASH_BUF_SIZE);
      if(result_table[0] != CMD_SUCCESS)
      {
        while(1); /* No way to recover. Just let Windows report a write failure */
      }
	  /* Reset byte counter and flash address */
	  byte_ctr = 0;
	  flash_address = (unsigned *)UPDATE_REQD;
	}
  NVIC->ISER[0] = enabled_irqs;
  return(CMD_SUCCESS);
}
Example #2
0
unsigned write_flash(unsigned * dst, char * src, unsigned no_of_bytes)
{
  unsigned i;

  for(i = 0;i<no_of_bytes;i++)
  {
    flash_buf[(byte_ctr+i)] = *(src+i);
  }
  byte_ctr = byte_ctr + no_of_bytes;

  if(byte_ctr == FLASH_BUF_SIZE)
  {
    /* We have accumulated enough bytes to trigger a flash write */
    find_erase_prepare_sector(SystemCoreClock/1000, (unsigned)dst);
    if(result_table[0] != CMD_SUCCESS)
    {
      DBG("Error: prepare sectors\n");
      while(1); /* No way to recover. Just let OS report a write failure */
    }
    write_data( SystemCoreClock/1000,
                (unsigned)dst,
                (void *)flash_buf,
                FLASH_BUF_SIZE);
    if(result_table[0] != CMD_SUCCESS)
    {
      DBG("Error: writing data\n");
      while(1); /* No way to recover. Just let OS report a write failure */
    }

    compare_data( SystemCoreClock/1000,
                (unsigned)dst,
                (void *)flash_buf,
                FLASH_BUF_SIZE);
    if(result_table[0] != CMD_SUCCESS)
    {
      DBG("Error: verifying data\n");
      while(1); /* No way to recover. Just let OS report a write failure */
    }

    /* Reset byte counter and flash address */
    byte_ctr = 0;
    dst = 0;
  }
  return(CMD_SUCCESS);
}