Ejemplo n.º 1
0
bool PersistentStore::access_finish() {
  if (eeprom_dirty) {
    IAP_STATUS_CODE status;
    if (--current_slot < 0) {
      // all slots have been used, erase everything and start again
      __disable_irq();
      status = EraseSector(EEPROM_SECTOR, EEPROM_SECTOR);
      __enable_irq();

      current_slot = EEPROM_SLOTS - 1;
    }

    __disable_irq();
    status = CopyRAM2Flash(SLOT_ADDRESS(EEPROM_SECTOR, current_slot), ram_eeprom, IAP_WRITE_4096);
    __enable_irq();

    if (status != CMD_SUCCESS) return false;
    eeprom_dirty = false;
  }
  return true;
}
/* Function to do the read/write to USB Disk */
bool USB_ReadWriteFile(void)
{
	FRESULT rc;		/* Result code */
	UINT br;
	uint32_t *address = USER_FLASH_START;
	uint16_t i = 0;

	if(f_mount(&fatFS, "0:", 1) != FR_OK)
	{
		PRINTDBG("\nUnable to mount pendrive.");	/* First... Mounting USB Unit. */
		return(false);
	}

	if (f_open(&fileObj, "0:firmware.bin", FA_READ) != FR_OK)
	{
		PRINTDBG("\nUnable to open firmware.bin from USB.");
		return(false);
	}

	/* LED's ON. */
	LEDPORT->FIOPIN &= ~(1 << LEDPORTPIN);

	PRINTDBG("\nReading firmware.bin from USB.");

	for (;;)
	{
		/* Clear Buffer. */
		for(i = 0; i < _MIN_SS; i++) buffer[i] = 0;

		/* Read a chunk of file */
		rc = f_read(&fileObj, &buffer, sizeof(buffer), &br);

		if (rc || !br)
		{
			break;					/* Error or end of file */
		}

		if(address == (uint32_t *)USER_FLASH_START)
		{
			uint32_t checksum = 0;
			uint32_t *tmpptr;

			tmpptr = (uint32_t *)&buffer[0];
			for (i = 0; i < 8; i++)
			{
				checksum += *tmpptr;
				tmpptr++;
			}
			if (checksum != 0)
			{
				rc = 1;
				PRINTDBG("\nChecksum Error.");
				return(false);
			}
			PRINTDBG("\nChecksum Valid.");
		}
		/* */
		__disable_irq();

		for(i = USER_START_SECTOR; i <= MAX_USER_SECTOR; i++)
		{
			if(address < sector_end_map[i])
			{
				if(address == sector_start_map[i])
				{
					EraseSector(i,i);
				}
				break;
			}
		}
		//
		if(CopyRAM2Flash((uint8_t *)address, &buffer[0], FLASH_BUF_SIZE) != CMD_SUCCESS)
		{
			return(false); /* No way to recover. Just let Windows report a write failure */
		}
		//
#if DEBUG
		printf("\n%d bytes writed in %#08x.", br, address);
#endif
		address += (FLASH_BUF_SIZE / sizeof(uint32_t));
		__enable_irq();

	}

	if (f_close(&fileObj) != FR_OK)
	{
		PRINTDBG("\nError to close file.");
		return(false);
	}

	PRINTDBG("\nClose the file.");

	USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0);

	PRINTDBG("\nFlash userdata updated.");

	return(true);
}
/*********************************************************************//**
 * @brief		The entry of the program
 *
 * @param[in]None
 *
 * @return 	None.
 *
 **********************************************************************/
void c_entry (void)
{	    		
  uint32_t result[4];
  uint8_t ver_major, ver_minor;
  uint32_t i;
  uint8_t *ptr;
  uint32_t flash_prog_area_sec_start;
  uint32_t flash_prog_area_sec_end;
  IAP_STATUS_CODE status;

  // Initialize
  debug_frmwrk_init();
  for (i = 0;i < sizeof(buffer);i++)
  {
    buffer[i] = (uint8_t)i;
  }
  flash_prog_area_sec_start = GetSecNum(FLASH_PROG_AREA_START);
  flash_prog_area_sec_end =  GetSecNum(FLASH_PROG_AREA_START + FLASH_PROG_AREA_SIZE);

  _DBG_(menu);

  status = ReadPartID(result);
  if(status != CMD_SUCCESS)
  {
     _DBG("Read Part ID failed with code is ");_DBD(status);_DBG_("");
     while(1);
  }

  _DBG("PartID: ");_DBH32(result[0]);_DBG_("");
  
  status = ReadBootCodeVer(&ver_major, &ver_minor);
  if(status != CMD_SUCCESS)
  {
     _DBG("Read Boot Code Version failed with code is ");_DBD(status);_DBG_("");
     while(1);
  }

  _DBG("Boot Code Version: ");_DBD(ver_major);_DBG(".");_DBD(ver_minor);_DBG_("");

  status = ReadDeviceSerialNum(result);
  if(status != CMD_SUCCESS)
  {
     _DBG("Read UID failed with code is ");_DBD(status);_DBG_("");
     while(1);
  }

  _DBG("UID: ");
  for(i = 0; i < 4; i++)
  {
     _DBD32(result[i]);
	 if(i<3)
	   _DBG("-");
  }
  _DBG_("");

  status = EraseSector(flash_prog_area_sec_start, flash_prog_area_sec_end); 
  if(status != CMD_SUCCESS)
  {
     _DBG("Erase chip failed with code is ");_DBD(status);_DBG_("");
     while(1); 
  }

  status = BlankCheckSector(flash_prog_area_sec_start, flash_prog_area_sec_end,
                                  &result[0], &result[1]);
  if(status != CMD_SUCCESS)
  {
     _DBG("Blank Check failed with code is ");_DBD(status);_DBG_("");
	 if(status == SECTOR_NOT_BLANK)
	 {
	   _DBG(">>>>The first non-blank sector is sector ");
	   _DBD(flash_prog_area_sec_start + result[0]);
	   _DBG_("");
	 }
     while(1); 
  }

  _DBG_("Erase chip: Success");


  /* Be aware that Program and ErasePage take long time to complete!!! If bigger
  RAM is present, allocate big buffer and reduce the number of Program blocks. */

  /* Program flash block by block until the end of the flash. */
  for ( i = 0; i < FLASH_PROG_AREA_SIZE/BUFF_SIZE; i++ )
  {
    ptr = (uint8_t*)(FLASH_PROG_AREA_START + i*BUFF_SIZE);
	status =  CopyRAM2Flash(ptr, buffer,IAP_WRITE_1024);
	if(status != CMD_SUCCESS)
	{
	   _DBG("Program chip failed with code is ");_DBD(status);_DBG_("");
       while(1);
    }
  }
  // Compare
  for ( i = 0; i < FLASH_PROG_AREA_SIZE/BUFF_SIZE; i++ )
  {
    ptr = (uint8_t*)(FLASH_PROG_AREA_START + i*BUFF_SIZE);
	status =  Compare(ptr, buffer,BUFF_SIZE);
	if(status != CMD_SUCCESS)
	{
	   _DBG("Compare memory failed with code is ");_DBD(status);_DBG_("");
       while(1);
	}
  }

   _DBG_("Program chip: Success");

  _DBG_("Demo termination");  
  
  while (1);
}