예제 #1
0
파일: WRProtect.c 프로젝트: meterhu/stm32F4
/*******************************************************************************************************
 *
 *         函数名称: 
 *
 *         函数功能: 
 *
 *         入口参数: 
 *
 *         出口参数: 无
 *
 *             说明:
 *
 *******************************************************************************************************/
void FlashWRDPUnlock(void)
{
    uint32_t rt;
    FlagStatus   RDP_state;
    FLASH_Status Flash_state;
    uint32_t     optcr_byte;
    //如果匹配,进行Flash的读写解锁
    FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); 
    FLASH_Unlock();
    FLASH_OB_Unlock();
    FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); 

    rt = FLASH_OB_GetWRP();
    
    do
    {
        //FlashWRPLock(WRITE, WRP_Sector_All);
        FLASH_OB_WRPConfig(WRP_Sector_All, DISABLE); //开启写保护
    }while(FLASH_OB_Launch() != FLASH_COMPLETE);
    
    rt = FLASH_OB_GetWRP();
    
    RDP_state = FLASH_OB_GetRDP();
    do
    {
        FLASH_OB_RDPConfig(OB_RDP_Level_0);//关闭读保护
    }while(FLASH_OB_Launch() != FLASH_COMPLETE);
    
    RDP_state = FLASH_OB_GetRDP();    
    FLASH_OB_Lock();
    FLASH_Lock();
    
}
예제 #2
0
bool writeEEPROM(void)
{
    // Generate compile time error if the config does not fit in the reserved area of flash.
    BUILD_BUG_ON(sizeof(master_t) > FLASH_TO_RESERVE_FOR_CONFIG);

    FLASH_Status status = 0;
    uint32_t wordOffset;
    int8_t attemptsRemaining = 3;
    bool changed;

    // prepare checksum/version constants
    masterConfig.version = EEPROM_CONF_VERSION;
    masterConfig.size = sizeof(master_t);
    masterConfig.magic_be = 0xBE;
    masterConfig.magic_ef = 0xEF;
    masterConfig.chk = 0; // erase checksum before recalculating
    masterConfig.chk = calculateChecksum((const uint8_t *) &masterConfig, sizeof(master_t));

    changed = (memcmp((const master_t *)CONFIG_START_FLASH_ADDRESS, &masterConfig, sizeof(master_t)) != 0);

    // write it
    if(changed) {
      FLASH_Unlock();
      while (attemptsRemaining--) {
#ifdef STM32F303
        FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
#endif
#ifdef STM32F10X
          FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
#endif
          for (wordOffset = 0; wordOffset < sizeof(master_t); wordOffset += 4) {
              if (wordOffset % FLASH_PAGE_SIZE == 0) {
                  status = FLASH_ErasePage(CONFIG_START_FLASH_ADDRESS + wordOffset);
                  if (status != FLASH_COMPLETE) {
                      break;
                  }
              }

              status = FLASH_ProgramWord(CONFIG_START_FLASH_ADDRESS + wordOffset,
                      *(uint32_t *) ((char *) &masterConfig + wordOffset));
              if (status != FLASH_COMPLETE) {
                  break;
              }
          }
          if (status == FLASH_COMPLETE) {
              break;
          }
      }
      FLASH_Lock();

      // Flash write failed - just die now
      if (status != FLASH_COMPLETE || !isEEPROMContentValid()) {
          failureMode(10);
      }
    }

    return changed;
}
예제 #3
0
// Initialise EEPROM hardware
void eeprom_init_hw(void)
{
  // unlock flash
  FLASH_Unlock();
  // Clear Flags
  FLASH_ClearFlag(FLASH_FLAG_PGERR |FLASH_FLAG_WRPERR|FLASH_FLAG_EOP);
  
  uint8_t mirror;
  uint16_t page;
  uint32_t addr;
  
  // Read pages status
  eFEE_STATUS pages_status[FEE_MIRROR_NUMBER];
  eFEE_STATUS status;
  
  for(page = 0; page < FEE_SECTORS; page++)
  {
    fee_pages_map[page] = 0xFF;
    for(mirror = 0; mirror < FEE_MIRROR_NUMBER; mirror++)
    {
      addr = FEE_BASE_ADDRESS + FEE_EEPROM_SIZE * mirror + FEE_SECTOR_SIZE * page;
      status = *(uint16_t *)(addr);
      pages_status[mirror] = status;
      
      if((status == eFEE_PAGE_VALID) && (fee_pages_map[page] == 0xFF))
      {
        fee_pages_map[page] = mirror;
      }
      else if(status != eFEE_PAGE_ERASED)
      {
        if(FLASH_ErasePage(addr) == FLASH_COMPLETE)
          pages_status[mirror] = eFEE_PAGE_ERASED;
        else
        {
          FLASH_ClearFlag(FLASH_FLAG_PGERR |FLASH_FLAG_WRPERR|FLASH_FLAG_EOP);
          pages_status[mirror] = eFEE_PAGE_INVALID;
        }
      }
    }

    if(fee_pages_map[page] == 0xFF) //  No Valid mirror
      for(mirror = 0; mirror < FEE_MIRROR_NUMBER; mirror++)
      {
        if(pages_status[mirror] == eFEE_PAGE_ERASED)
        {
          addr = FEE_BASE_ADDRESS + FEE_EEPROM_SIZE * mirror + FEE_SECTOR_SIZE * page;
          if(FLASH_ProgramHalfWord(addr, eFEE_PAGE_VALID) == FLASH_COMPLETE)
          {
            fee_pages_map[page] = mirror;
            break;
          }
          else
            FLASH_ClearFlag(FLASH_FLAG_PGERR |FLASH_FLAG_WRPERR|FLASH_FLAG_EOP);
        }
      }
  }
}
예제 #4
0
/*************************** 
Function : Erase the internal Flash data 
Input :- start address	
- Number of page 
Output: return the FLASH status 

***************************/ 
u16 Flash_EEPROM_ErasePage( u32 Start_Address, u16 No_of_Size) 
{ 
  FLASH_Unlock(); 
  FLASHStatus=FLASH_COMPLETE; 
  FLASH_EEPROM_ProgramStatus= PASSED; 

  Flash_EEPROM_Nb_Of_Page= No_of_Size; // 
  /*Clear all the flash */ 
  FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR | FLASH_FLAG_OPTERR); 

  /*erase the page*/ 
  for(EraseCounter=0; EraseCounter<Flash_EEPROM_Nb_Of_Page; EraseCounter++) 
  { 
    FLASHStatus = FLASH_ErasePage(Start_Address +(FL_EEPROM_PageSize* EraseCounter)); 

    if (FLASHStatus != FLASH_COMPLETE) 
    { 
      return FLASHStatus; 
    } 
  } 

  FLASH_Lock(); 

  return FLASHStatus; 

} 
예제 #5
0
int write_whole_poweroff_info_tbl(struct poweroff_info_st *data)
{
	u32 *ps, *pd;
	int i;
	FLASH_Status status;

	if (NULL == data)
		return -RT_ERROR;

	FLASH_Unlock();
	/* Clear pending flags (if any) */
	FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPTERR | FLASH_FLAG_WRPRTERR |
					FLASH_FLAG_PGERR | FLASH_FLAG_BSY);

	status = FLASH_ErasePage((rt_uint32_t)POWEROFF_INFO_TBL_BASE_OF_FLASH); /* wait for complete in FLASH_ErasePage() */
	if (FLASH_COMPLETE != status)
		while(1); //return status;

	ps = (u32 *)data;
	pd = (u32 *)POWEROFF_INFO_TBL_BASE_OF_FLASH;
	for (i=0; i < RT_ALIGN(POWEROFF_INFO_TBL_SIZE_OF_FLASH, 4)/4; i++) {
		status = FLASH_ProgramWord((u32)pd, *ps); /* wait for complete in FLASH_ProgramWord()  */
		if (FLASH_COMPLETE != status)
			while(1); //return status;
		++pd;
		++ps;
	}
	FLASH_Lock();

	return FLASH_COMPLETE;
}
예제 #6
0
u8* bsp_Firmware_Write(u8* writebuffer,u32 writeaddress,u16 writelength)
{	
	
	u8 status=0;
	u16 index=0;
	SleepTime=0;
  FLASH_Unlock();									//解锁 
  FLASH_DataCacheCmd(DISABLE);//FLASH擦除期间,必须禁止数据缓存
 	FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR|  FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
	
   __set_PRIMASK(1);  
		  for(index=0;index<writelength;index++)
		{
			status=FLASH_ProgramByte(writeaddress+index,writebuffer[index]);  
		}
   __set_PRIMASK(0);  

	
  FLASH_DataCacheCmd(ENABLE);	//FLASH擦除结束,开启数据缓存
	FLASH_Lock();//上锁	
	if(status==FLASH_COMPLETE)
	{
		MCU_WriteStatus=0;
	}
	else
	{
		MCU_WriteStatus=status;
	}
	return &MCU_WriteStatus;
		
}
예제 #7
0
int write_flash(uint32_t addr, uint8_t *data, uint32_t data_len)
{
	FLASH_Status FLASHStatus = FLASH_COMPLETE;
	int rep = 0, i;
	uint32_t tmp;

	__disable_irq();

	rep = (data_len / 4);
	if(data_len % 4)
		rep++;

	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);	

	for(i = 0 ; i < rep ; i++) {
		tmp = *((uint32_t *)data + i);
		FLASHStatus = FLASH_ProgramWord(addr, tmp);
		if(FLASHStatus != FLASH_COMPLETE) {
			myprintf("FLASH_ProgramWord Error\r\n");
			return -1;
		}

		addr += 4;
	}

	FLASH_Lock();

	__enable_irq();

	return data_len;
}
예제 #8
0
/**
 * Erase data on flash.
 * @note This operation is irreversible.
 * @note This operation's units is different which on many chips.
 *
 * @param addr flash address
 * @param size erase bytes size
 *
 * @return result
 */
EfErrCode ef_port_erase(uint32_t addr, size_t size) {
    EfErrCode result = EF_NO_ERR;
    FLASH_Status flash_status;
    size_t erase_pages, i;
    
    /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
    EF_ASSERT(addr % ERASE_MIN_SIZE == 0);
    
    /* calculate pages */
    erase_pages = size / PAGE_SIZE;
    if (size % PAGE_SIZE != 0) {
        erase_pages++;
    }

    /* start erase */
    FLASH_Unlock();
    FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
    for (i = 0; i < erase_pages; i++) {
        flash_status = FLASH_ErasePage(addr + (PAGE_SIZE * i));
        if (flash_status != FLASH_COMPLETE) {
            result = EF_ERASE_ERR;
            break;
        }
    }
    FLASH_Lock();

    return result;
}
예제 #9
0
void writeFlash(u8 address_num,u32 data) //address_num should start from 0 up to 255
 {

const u8 data_count=255;
	 
	 u32 flash_data[data_count];
	 
	 for (u8 i = 0; i < data_count; ++i) {
			flash_data[i] = readFlash(i);
		}
		
		flash_data[address_num] = data;
	 
	 
 FLASH_Unlock();// you need to unlcok flash first
 /* Clear All pending flags */
 FLASH_ClearFlag( FLASH_FLAG_EOP |  FLASH_FLAG_WRPERR |  FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

//you need to erase entire sector before write anything
 FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);
 //VoltageRange_3        ((uint8_t)0x02)  /*!<Device operating range: 2.7V to 3.6V */

		for (u8 i = 0; i < data_count; ++i) {   //write back the whole sector memory
	
			FLASH_ProgramWord((u32)(startAddress + i * 4), flash_data[i]);
		}

 FLASH_Lock();//lock flash at the end

}
예제 #10
0
파일: flash_helper.c 프로젝트: vedderb/bldc
uint16_t flash_helper_write_new_app_data(uint32_t offset, uint8_t *data, uint32_t len) {
	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
			FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

	mc_interface_unlock();
	mc_interface_release_motor();
	utils_sys_lock_cnt();
	timeout_configure_IWDT_slowest();

	for (uint32_t i = 0;i < len;i++) {
		uint16_t res = FLASH_ProgramByte(flash_addr[NEW_APP_BASE] + offset + i, data[i]);
		if (res != FLASH_COMPLETE) {
			FLASH_Lock();
			return res;
		}
	}
	FLASH_Lock();

	timeout_configure_IWDT();

	utils_sys_unlock_cnt();

	return FLASH_COMPLETE;
}
예제 #11
0
uint16_t flash_helper_erase_new_app(uint32_t new_app_size) {
	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
			FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

	new_app_size += flash_addr[NEW_APP_BASE];

	mcpwm_unlock();
	mcpwm_release_motor();
	utils_sys_lock_cnt();
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, DISABLE);

	for (int i = 0;i < NEW_APP_SECTORS;i++) {
		if (new_app_size > flash_addr[NEW_APP_BASE + i]) {
			uint16_t res = FLASH_EraseSector(flash_sector[NEW_APP_BASE + i], VoltageRange_3);
			if (res != FLASH_COMPLETE) {
				return res;
			}
		} else {
			break;
		}
	}

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
	utils_sys_unlock_cnt();

	return FLASH_COMPLETE;
}
예제 #12
0
void flash_inside_write(void* address,void* data,long length) {
    char old_data[2]={0};
    int  old_data_int=0; 
    long the_last=length-1;

    if (!is_valid_address(address)) return;
    
    FLASH_Unlock();
    if ((long)address%2!=0) {
        flash_inside_read((void*)((long)address-1),old_data,2);
        old_data[1]=*(char*)data;
        memcpy(&old_data_int,old_data,sizeof(int));
        FLASH_ProgramHalfWord((uint32_t)(FLASH_INSIDE_ADDRESS_START+(long)address-1),old_data_int);
        --length;
    }
    for (;length>1;length-=2)
        FLASH_ProgramHalfWord((uint32_t)(FLASH_INSIDE_ADDRESS_START+(long)address+length-2),*(uint16_t*)((long)data+length-2));
    if (length) {
        flash_inside_read((void*)((long)address+the_last),old_data,2);
        old_data[0]=*(char*)((long)data+the_last);
        memcpy(&old_data_int,old_data,sizeof(int));
        FLASH_ProgramHalfWord((uint32_t)(FLASH_INSIDE_ADDRESS_START+(long)address+the_last),old_data_int);
    }
    FLASH_Lock();
    FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);
}
예제 #13
0
int flashAddress(uint32_t startAddr, uint32_t *data, uint32_t len) {
    FLASH_Status status;
    int retries;
    int ret = 1;
    int i;

    FLASH_Unlock();

    // Clear pending flags (if any)
    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

    for (i = 0; i < len; i++) {
        retries = 0;
        do {
            status = FLASH_ProgramWord(startAddr + i*4, *(data + i));
            retries++;
        } while (status != FLASH_COMPLETE && retries < FLASH_RETRIES);

        if (retries == FLASH_RETRIES) {
            ret = 0;
            break;
        }
    }

    FLASH_Lock();

    return ret;
}
예제 #14
0
mal_error_e mal_flash_write_uint32_values(unsigned int start_address, uint32_t *values, uint32_t count) {
	FLASH_Status result = FLASH_BUSY;
	// Unlock flash registers
	FLASH_Unlock();
	// Clear pending flags
	FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
	// Write values
	uint32_t i;
	uint32_t address = start_address;
	for (i = 0; i < count; i++) {
		// Write data
		result = FLASH_ProgramWord(address, values[i]);
		if (FLASH_COMPLETE != result) {
			// We stop at first sign of error
			break;
		}
		// Increment address
		address += sizeof(uint32_t);
	}
	// Lock flash registers
	FLASH_Lock();
	// Return result
	if (FLASH_COMPLETE != result) {
		return MAL_ERROR_MEMORY;
	}
	return MAL_ERROR_OK;
}
예제 #15
0
// write data to FLASH, only within 1 page.
static bool fee_write_page(uint32_t fee_addr, uint32_t len, uint8_t *pData)
{
  uint16_t din, dout;

  for(; len > 0; len--)
  {
    din = (uint16_t)(*(pData++)) | 0xFF00;
    dout = *(uint16_t *)(fee_addr);

    if(din != dout)
    {
      if(dout != 0xFFFF)
        return false;

      if(FLASH_ProgramHalfWord(fee_addr, din) != FLASH_COMPLETE)
      {
        FLASH_ClearFlag(FLASH_FLAG_PGERR |FLASH_FLAG_WRPERR|FLASH_FLAG_EOP);
        return false;
      }
    }
    fee_addr += 2;
  }

  return true;
}
예제 #16
0
void writeParams(void)
{
    FLASH_Status status;
    uint32_t i;
    uint8_t chk = 0;
    const uint8_t *p;
    
    cfg.version = EEPROM_CONF_VERSION;
    cfg.size = sizeof(config_t);
    cfg.magic_be = 0xBE;
    cfg.magic_ef = 0xEF;
    cfg.chk = 0;
    // recalculate checksum before writing
    for (p = (const uint8_t *)&cfg; p < ((const uint8_t *)&cfg + sizeof(config_t)); ++p)
        chk ^= *p;
    cfg.chk = chk;
    
    // write it
    FLASH_Unlock();
    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

    if (FLASH_ErasePage(FLASH_WRITE_ADDR) == FLASH_COMPLETE) {
        for (i = 0; i < sizeof(config_t); i += 4) {
            status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *)((char *)&cfg + i));
            if (status != FLASH_COMPLETE)
                while(1);//break; // TODO: fail
        }
    }

    FLASH_Lock();

    readEEPROM();
}
예제 #17
0
// write to a specified page in flash
// page_addr: the page address that write to
void write_embedded_flash_with_addr_len(uint32_t page_addr, uint8_t *buf, uint16_t len) {
	uint32_t saddr = page_addr;			// start address for the page to write
	uint32_t eaddr = saddr+len;	// end addrees for that page
	uint32_t *pbuf = (uint32_t*)buf;

	// Unlock the Flash Program Erase controller
	FLASH_Unlock();

	// Clear All pending flags 
	FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR);

	// Erase the FLASH pages 
	FLASHStatus = FLASH_ErasePage(saddr);
	
	// write buf to that page
	while((saddr < eaddr) && (FLASHStatus == FLASH_COMPLETE)) {
		FLASHStatus = FLASH_FastProgramWord(saddr, *(pbuf));	//problem
		saddr = saddr + 4;
		pbuf++;
	}

	if (FLASHStatus != FLASH_COMPLETE) {
		HardFault_Handler();//enter_unexpected();  do something here;
	}


	// Lock flash
	FLASH_Lock();
}
예제 #18
0
파일: eeprom.c 프로젝트: htol/ultramicron
//**************************************************************************
void eeprom_erase(void)
{
  DATA_EEPROM_Unlock(); // разблокировка перед записью
  
  /* Clear all pending flags */      
  FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR
                  | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_OPTVERRUSR);	
  
  /*  Data EEPROM Fast Word program of FAST_DATA_32 at addresses defined by 
  DATA_EEPROM_START_ADDR and DATA_EEPROM_END_ADDR */  
  Address = DATA_EEPROM_START_ADDR;
  
  NbrOfPage = ((DATA_EEPROM_END_ADDR - Address) + 1 ) >> 2; 
  
  /* Erase the Data EEPROM Memory pages by Word (32-bit) */
  for(j = 0; j < NbrOfPage; j++)
  {
    FLASHStatus = DATA_EEPROM_EraseWord(Address + (4 * j));
  }
  DATA_EEPROM_Lock();
  
  /* Check the correctness of written data */
  while(Address < DATA_EEPROM_END_ADDR)
  {
    if(*(__IO uint32_t*)Address != 0x0)
    {
      DataMemoryProgramStatus = FAILED;
      //ОШИБКА стирания !!!!
      return;
    }
    Address = Address + 4;
  }
  
}
예제 #19
0
파일: flash_helper.c 프로젝트: vedderb/bldc
uint16_t flash_helper_erase_new_app(uint32_t new_app_size) {
	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
			FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

	new_app_size += flash_addr[NEW_APP_BASE];

	mc_interface_unlock();
	mc_interface_release_motor();
	utils_sys_lock_cnt();
	timeout_configure_IWDT_slowest();

	for (int i = 0;i < NEW_APP_SECTORS;i++) {
		if (new_app_size > flash_addr[NEW_APP_BASE + i]) {
			uint16_t res = FLASH_EraseSector(flash_sector[NEW_APP_BASE + i], VoltageRange_3);
			if (res != FLASH_COMPLETE) {
				FLASH_Lock();
				return res;
			}
		} else {
			break;
		}
	}
	FLASH_Lock();

	timeout_configure_IWDT();
	utils_sys_unlock_cnt();

	return FLASH_COMPLETE;
}
예제 #20
0
WORD EE_WriteVariable(WORD offset, WORD newValue)
{
    FLASH_Status stat = FLASH_COMPLETE;

    volatile WORD *pCurrentValue =   (__IO uint16_t*) (PAGE0_BASE_ADDRESS + 2 + (offset * 2));
    WORD PageStatus0             = *((__IO uint16_t*)  PAGE0_BASE_ADDRESS);
    WORD currentValue            = *pCurrentValue;

    // first check is page is valid, and if the value did change
    if(PageStatus0 == VALID_PAGE && (currentValue == newValue)) {   // page is valid, value didn't change? Good, quit
        return FLASH_COMPLETE;
    }

    // if we got here, then either page is not valid, or the value changed
    FLASH_Unlock();
    FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); 

    stat = FLASH_ErasePage(PAGE0_BASE_ADDRESS);                         // erase page
    if(stat != FLASH_COMPLETE) {
        return stat;
    }
    
    stat = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);       // write VALID_PAGE marker
    if(stat != FLASH_COMPLETE) {
        return stat;
    }

    stat = FLASH_ProgramHalfWord((uint32_t) pCurrentValue, newValue);   // wite new vale
    if(stat != FLASH_COMPLETE) {
        return stat;
    }

    FLASH_Lock();
    return stat;
}
예제 #21
0
void writeParams(uint8_t b)
{
    FLASH_Status status;
    uint32_t i;
    uint8_t chk = 0;
    const uint8_t *p;

    cfg.version    = EEPROM_CONF_VERSION;
    cfg.size       = sizeof(config_t);
    cfg.magic_be   = 0xBE;
    cfg.magic_ef   = 0xEF;
    cfg.chk        = 0;
    for (p = (const uint8_t *)&cfg; p < ((const uint8_t *)&cfg + sizeof(config_t)); p++) chk ^= *p; // recalculate checksum before writing
    cfg.chk = chk;
    FLASH_Unlock();                                                                       // write it
    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
  
    for (i = 0; i < FLASH_PAGES_FORCONFIG; i++)                                           // Erase the pages here
    {
        while (FLASH_ErasePage(FLASH_WRITE_ADDR + (i * FLASH_PAGE_SIZE)) != FLASH_COMPLETE);
    }

    for (i = 0; i < sizeof(config_t); i += 4)                                             // Write that config now.
    {
        status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *) ((char *) &cfg + i));
        if (status != FLASH_COMPLETE) break;                                              // TODO: fail
    }
    FLASH_Lock();
    readEEPROM();
    if (b) blinkLED(15, 20, 1);
}
예제 #22
0
/***********************************************************
 * 函数名:Write_GPS_Info
 * 描述  :把GPS信息从CPU写入Flash
 * 输入  : 无
 * 输出  : 无
 ***********************************************************/
void Write_Flash_GPS_Info(void)
{
	FLASHStatus = FLASH_COMPLETE;
	MemoryProgramStatus = PASSED;
	FLASH_Unlock();	//FLASH解锁
	FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);//清标志位
	
	NbrOfPage = (EndAddr[10] - StartAddr[10]) / FLASH_PAGE_SIZE; //页面擦除子程序
	FLASHStatus = FLASH_ErasePage(StartAddr[10] + (FLASH_PAGE_SIZE * NbrOfPage));

	FlashAddress = StartAddr[10];

	FLASHStatus = FLASH_ProgramWord(FlashAddress, jingdu);
	FlashAddress = FlashAddress + 4;
	FLASHStatus = FLASH_ProgramWord(FlashAddress, weidu);
	FlashAddress = FlashAddress + 4;
	FLASHStatus = FLASH_ProgramWord(FlashAddress, sog);
	FlashAddress = FlashAddress + 4;
	FLASHStatus = FLASH_ProgramWord(FlashAddress, direction);
	
	BKP_WriteBackupRegister(BKP_DR4,(u16)(jingdu >> 16));
	BKP_WriteBackupRegister(BKP_DR5,(u16)(weidu >> 16));
	BKP_WriteBackupRegister(BKP_DR7,(u16)jingdu);
	BKP_WriteBackupRegister(BKP_DR8,(u16)weidu);
	BKP_WriteBackupRegister(BKP_DR9,sog);
	BKP_WriteBackupRegister(BKP_DR10,direction);
}
예제 #23
0
//External Functions
void arch_IntfInit()
{

	FLASH_Unlock();
    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | 
                  FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
}
예제 #24
0
bool HardwareEEPROM::put(char * src, size_t size)
{
    FLASH_Status status = (FLASH_Status)0;

    FLASH_Unlock();

    for (unsigned int tries = 3; tries; tries--) {

        FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

        status = FLASH_COMPLETE;

        for (size_t i=0; i<size && status==FLASH_COMPLETE; i+=2) {

            uint16_t datum = 0;

            memcpy(&datum, &src[i], (i+2>size)?1:2); // handle odd-length data

            status = FLASH_ProgramHalfWord(FLASH_WRITE_ADDR+i, datum);
        }

        if (status == FLASH_COMPLETE)
            break;
    }

    FLASH_Lock();

    return status == FLASH_COMPLETE;
}
예제 #25
0
/***********************************************************
 * 函数名:WriteflashMPeriod
 * 描述  :把interval_s从CPU写入Flash
 * 输入  : 无
 * 输出  : 无
 ***********************************************************/
void WriteflashMPeriod(void)
{
	FLASHStatus = FLASH_COMPLETE;
	MemoryProgramStatus = PASSED; 

	FLASH_Unlock();	//FLASH解锁
	FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);//清标志位
	NbrOfPage = (EndAddr[4] - StartAddr[4]) / FLASH_PAGE_SIZE;//页面擦除子程序
	FLASHStatus = FLASH_ErasePage(StartAddr[4] + (FLASH_PAGE_SIZE * NbrOfPage)); 
	
	FlashAddress = StartAddr[4]; 
	FLASHStatus = FLASH_ProgramWord(FlashAddress, gps_invalid);
	
	FlashAddress = FlashAddress + 4;
	FLASHStatus = FLASH_ProgramWord(FlashAddress, intervalA);
	
	FlashAddress = FlashAddress + 4;
	FLASHStatus = FLASH_ProgramWord(FlashAddress, intervalB);
	
	FlashAddress = FlashAddress + 4;
	FLASHStatus = FLASH_ProgramWord(FlashAddress, interval_num);
	
	FlashAddress = FlashAddress + 4;
//	FLASHStatus = FLASH_ProgramWord(FlashAddress, time_o);
	
	FlashAddress = FlashAddress + 4;
	FLASHStatus = FLASH_ProgramHalfWord(FlashAddress, interval_s);

	BKP_WriteBackupRegister(BKP_DR1,interval_num);
	//BKP_WriteBackupRegister(BKP_DR2,time_o);
	BKP_WriteBackupRegister(BKP_DR3,interval_s);
}
예제 #26
0
//void SD_TEST(void)
//{
//	u8 res;
//	UINT bw,index;
//	res=f_mount( &fatfs_SDCARD,"0:/",1 );
//	res=f_open(&file_sdif, "0:/sdif.dat",FA_OPEN_EXISTING|FA_WRITE|FA_READ);
//	res=f_write(&file_sdif,SDTEST_WriteBuffer,512,&bw);
//	res=f_close(&file_sdif);
//	res=f_open(&file_sdif, "0:/sdif.dat",FA_OPEN_EXISTING |FA_WRITE);
//	res=f_write(&file_sdif,SDTEST_WriteBuffer,512,&bw);
//	res=f_close(&file_sdif);
//	res=f_open(&file_sdif, "0:/sdif.dat",FA_OPEN_EXISTING | FA_READ);
//	index=0;
//	while(SDTEST_ReadBuffer[31]!=0x04)
//	{
//		index++;
//		res=f_read(&file_sdif,SDTEST_ReadBuffer,512,&bw);
//	}
//	f_close(&file_sdif);      
//	
//	
//}
u8 bsp_ChangeAPPtoRun(u8 App_Select)
{
  u8 status=0;
	u8 Flash_status=0;
	FLASH_Unlock();									//解锁 
  FLASH_DataCacheCmd(DISABLE);//FLASH擦除期间,必须禁止数据缓存
 	FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR|  FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
	status=FLASH_EraseSector(FLASH_Sector_3 ,VoltageRange_3);//VCC=2.7~3.6V之间!!

	if(status==FLASH_COMPLETE)
	{
   __set_PRIMASK(1);  
	status=FLASH_ProgramByte(App_ToRun_Flag,App_Select);  //SysID
   __set_PRIMASK(0);  
	}
  FLASH_DataCacheCmd(ENABLE);	//FLASH擦除结束,开启数据缓存
	FLASH_Lock();//上锁	
	if(status==FLASH_COMPLETE)
	{
		Flash_status=0;
	}
	else
	{
		Flash_status=status;
	}
	return Flash_status;
		
}
예제 #27
0
Std_ReturnType Fls_Erase(Fls_AddressType TargetAddress, Fls_LengthType Length)
{
	Fls_AddressType pageStart = 0;
	Fls_AddressType erased = 0;
	u32 page = 0;
	u32 pageIndex;

	/* Unlock the Flash Program Erase controller */
	FLASH_Unlock();
	/* Clear All pending flags */
	FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

	/* Find first sector. */
	page = (TargetAddress - flsConfigPtr->FlsSectorList[0].FlsSectorStartaddress) / flsConfigPtr->FlsSectorList[0].FlsPageSize;
	pageStart = flsConfigPtr->FlsSectorList[0].FlsSectorStartaddress + page * flsConfigPtr->FlsSectorList[0].FlsPageSize;
	//page = Length / flsConfigPtr->FlsSectorList[0].FlsPageSize;

	/* Erase the pages... */
	for (pageIndex = page; erased < Length; pageIndex++)
	{
		volatile FLASH_Status status = FLASH_BUSY;
		while(status != FLASH_COMPLETE){
			status = FLASH_ErasePage((uint32_t)pageStart);
		}
		erased += flsConfigPtr->FlsSectorList[0].FlsPageSize;
		pageStart += flsConfigPtr->FlsSectorList[0].FlsPageSize;
	}

	return E_OK;
}
예제 #28
0
unsigned char flash_write_operate_nocheckcomplete(u32 Address,u16 * buff,u32 len)
{
  volatile FLASH_Status FLASHStatus;
  u32 Temp_Addr;
  FLASHStatus = FLASH_COMPLETE;
  if(len%2) return WR_LEN_ERR;
  if(Address%2) return WR_ADDRESS_ERR;
  if( Address<StartAddr || Address+len>EndAddr ) return ADDRESS_LEN_ERR;
  
  //Unlock the Flash Program Erase controller 
  FLASH_Unlock();
  //Clear All pending flags
  FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR);
  Temp_Addr=Address+len;
  
  while((Address < Temp_Addr) )
  {
    FLASHStatus = FLASH_ProgramHalfWord(Address, *buff);
    Address = Address + 2;
    buff++;
  }
  
  FLASH_Lock();
  
  if( FLASHStatus==FLASH_COMPLETE ) return FLASH_OP_OK;
  else
    return FLASHStatus;
}
예제 #29
0
void Internal_EE_Write8(uint16_t position, uint8_t data){
	DATA_EEPROM_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR
                  | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_OPTVERRUSR);	
	DATA_EEPROM_ProgramByte(INTERNAL_8BIT_START_ADDRESS+position, data);
	DATA_EEPROM_Lock();
}
예제 #30
0
/*
 * NOTE:write flash is a slow process
 */
static int write_whole_syscfgdata_tbl(struct syscfgdata_tbl *data)
{
	unsigned char *page_addr;
	unsigned char *data_page;
	u32 all_data_len;

	if (NULL == data)
		return -RT_ERROR;

	FLASH_Unlock();
	/* Clear pending flags (if any) */
	FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPTERR | FLASH_FLAG_WRPRTERR | FLASH_FLAG_PGERR | FLASH_FLAG_BSY);

	page_addr	= (unsigned char *)SYSCFGDATA_TBL_BASE_OF_FLASH;
	data_page	= (unsigned char *)data;
	all_data_len	= SYSCFGDATA_TBL_SIZE_OF_FLASH;
	while (all_data_len > 0) {
		if (all_data_len > SIZE_PER_FLASH_PAGE) {
			do_write_flash_page(page_addr, data_page, SIZE_PER_FLASH_PAGE);
			page_addr += SIZE_PER_FLASH_PAGE;
			data_page += SIZE_PER_FLASH_PAGE;
			all_data_len -= SIZE_PER_FLASH_PAGE;
		} else {
			do_write_flash_page(page_addr, data, RT_ALIGN(all_data_len, 4));
			all_data_len = 0;
			break;
		}
	}

	FLASH_Lock();

	return FLASH_COMPLETE;
}