/** * @brief Main program * @param None * @retval None */ int main(void) { RCC_Configuration(); GPIO_Configuration(); USART_Configuration(); SysTick_Config(SystemCoreClock/10); // Enable the LSI OSC RCC_LSICmd(ENABLE); // Wait till LSI is ready while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {}; IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); // IWDG counter clock: LSI/256 IWDG_SetPrescaler(IWDG_Prescaler_256); IWDG_SetReload(0x0FFF); // Reload IWDG counter IWDG_ReloadCounter(); // Enable IWDG (the LSI oscillator will be enabled by hardware) IWDG_Enable(); // Write memmory FLASH_UnlockBank1(); FLASH_ErasePage(FLAG_ADDR); FLASH_ProgramWord(FLAG_ADDR,(u32)FLAG_UPDATED); FLASH_LockBank1(); updateFW_control(); }
//===================================== void saveFont(u32 addr,u8* fontData,u16 length) { u32 i = 0; u32 addr_temp; u32 data_temp = 0; FLASH_Status status = FLASH_COMPLETE; addr_temp = addr; // Unlock flash to rewrite to flash FLASH_UnlockBank1(); // Erase pages used to store font for(i = 0;i<3;i++) { status = FLASH_ErasePage(addr_temp); addr_temp += FLASH_PAGE_SIZE; } // Write font data to flash for(i = 0;i<length;i+=4) { memcpy(&data_temp,&fontData[i],((length-i)>4?4:(length-i))); status = FLASH_ProgramWord(addr,data_temp); addr += sizeof(u32); } FLASH_LockBank1(); }
/** * @brief Programs a half word at a specified Option Byte Data address. * @note This function can be used for all STM32F10x devices. * @param Address: specifies the address to be programmed. * @param buf: specifies the data to be programmed. * @param iNbrToWrite: the number to write into flash * @retval if success return the number to write, -1 if error * */ int Flash_Write(uint32_t iAddress, uint8_t *buf, uint32_t iNbrToWrite) { /* Unlock the Flash Bank1 Program Erase controller */ uint32_t secpos; uint32_t iNumByteToWrite = iNbrToWrite; uint16_t secoff; uint16_t secremain; uint16_t i = 0; uint8_t tmp[FLASH_PAGE_SIZE]; FLASH_UnlockBank1(); secpos=iAddress & (~(FLASH_PAGE_SIZE -1 )) ;//扇区地址 secoff=iAddress & (FLASH_PAGE_SIZE -1); //在扇区内的偏移 secremain=FLASH_PAGE_SIZE-secoff; //扇区剩余空间大小 volatile FLASH_Status FLASHStatus = FLASH_COMPLETE; if(iNumByteToWrite<=secremain) secremain = iNumByteToWrite;//不大于4096个字节 while( 1 ) { Flash_Read(secpos, tmp, FLASH_PAGE_SIZE); //读出整个扇区 for(i=0;i<secremain;i++) { //校验数据 if(tmp[secoff+i]!=0XFF)break; //需要擦除 } if(i<secremain) { //需要擦除 FLASHStatus = FLASH_ErasePage(secpos); //擦除这个扇区 if(FLASHStatus != FLASH_COMPLETE) return -1; for(i=0;i<secremain;i++) //复制 { tmp[i+secoff]=buf[i]; } Flash_Write_Without_check(secpos ,tmp ,FLASH_PAGE_SIZE);//写入整个扇区 } else { Flash_Write_Without_check(iAddress,buf,secremain);//写已经擦除了的,直接写入扇区剩余区间. } if(iNumByteToWrite==secremain) //写入结束了 { break; } else { secpos += FLASH_PAGE_SIZE; secoff = 0;//偏移位置为0 buf += secremain; //指针偏移 iAddress += secremain;//写地址偏移 iNumByteToWrite -= secremain; //字节数递减 if(iNumByteToWrite>FLASH_PAGE_SIZE) secremain=FLASH_PAGE_SIZE;//下一个扇区还是写不完 else secremain = iNumByteToWrite; //下一个扇区可以写完了 } } FLASH_LockBank1(); return iNbrToWrite; }
//================================================ static void write_mem(void) { u32 addr = 0; u16 i = 0; u8 temp[4]; u16 length = 0; u32 data = 0; u8 crc = 0; FLASH_Status status = FLASH_COMPLETE; // Receive start address for(i=0;i<4;i++) temp[i] = USART_GetC(); crc = USART_GetC(); if(crc == (temp[0]^temp[1]^temp[2]^temp[3])) { mem_cpy(&addr,temp,4); USART_PutC(ACK); length = USART_GetC(); length = (length<<8) | USART_GetC(); for(i = 0;i<length;i++) fwBuff[i] = USART_GetC(); crc = USART_GetC(); // Write memmory FLASH_UnlockBank1(); if(!fUpdating) { // Disable systick SysTick->CTRL &=~( SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk); status = FLASH_ErasePage(FLAG_ADDR); if(status == FLASH_COMPLETE) status = FLASH_ProgramWord(FLAG_ADDR,(u32)FLAG_UPDATING); fUpdating = true; } if(status == FLASH_COMPLETE) status = FLASH_ErasePage(addr); for(i = 0;i<length;i+=4) { mem_cpy(&data,&fwBuff[i],((length-i)>4?4:(length-i))); if(status != FLASH_COMPLETE) break; status = FLASH_ProgramWord(addr,data); addr += 4; } FLASH_LockBank1(); if(status == FLASH_COMPLETE) USART_PutC(ACK); else USART_PutC(NACK); } else USART_PutC(NACK); }
//----------------------------------- void Store_Channels_Data(void) // { unsigned char i=0,j=0; FLASH_UnlockBank1(); /* Define the number of page to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } /* Program Flash Bank1 */ Address = BANK1_WRITE_START_ADDR; for(i=0;i<CHANNEL_NUMBER;i++) { channels[i].calibrate.cal.brightness=tab.indicators[i].brightness&0xF; for(j=0;j<4;j++) { FLASHStatus = FLASH_ProgramWord(Address, channels[i].calibrate.serialize[j]); Address = Address + 4; } } FLASH_LockBank1(); // /* Check the corectness of written data */ // Address = BANK1_WRITE_START_ADDR; // // while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED)) // { // if((*(__IO uint32_t*) Address) != Data) // { // MemoryProgramStatus = FAILED; // } // Address += 4; // } // // if( MemoryProgramStatus == FAILED) // { // // while(1); // //FlashError=1; // } return; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* Porgram FLASH Bank1 ********************************************************/ /* Unlock the Flash Bank1 Program Erase controller */ FLASH_UnlockBank1(); /* Define the number of page to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } /* Program Flash Bank1 */ Address = BANK1_WRITE_START_ADDR; while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE)) { FLASHStatus = FLASH_ProgramWord(Address, Data); Address = Address + 4; } FLASH_LockBank1(); /* Check the corectness of written data */ Address = BANK1_WRITE_START_ADDR; while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED)) { if((*(__IO uint32_t*) Address) != Data) { MemoryProgramStatus = FAILED; } Address += 4; } while (1) { } }
void FLASH_Write(uint32_t *data, uint16_t len) { /* Porgram FLASH Bank1 ********************************************************/ /* Unlock the Flash Bank1 Program Erase controller */ FLASH_UnlockBank1(); /* Define the number of page to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } /* Program Flash Bank1 */ Address = BANK1_WRITE_START_ADDR; while((Address < (BANK1_WRITE_START_ADDR+len)) && (FLASHStatus == FLASH_COMPLETE)) { FLASHStatus = FLASH_ProgramWord(Address, data); data++; Address = Address + 4; } FLASH_LockBank1(); /* Check the corectness of written data */ Address = BANK1_WRITE_START_ADDR; while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED)) { if((*(__IO uint32_t*) Address) != Data) { MemoryProgramStatus = FAILED; } Address += 4; } if( MemoryProgramStatus == FAILED) { // while(1); FlashError=1; } }
int FLASH_write(char* argv){ /* Porgram FLASH Bank1 ********************************************************/ /* Unlock the Flash Bank1 Program Erase controller */ char *pData; int need_erase = 0; char buffer[20]; int Data=0x87654321; int Address,FLASHStatus; Address = atoi(argv); pData = strchr(argv,SEP_FIELD); pData++; Data = atoi(pData); sprintf(buffer,"\naddr:%x\n",Address); printf_(buffer); sprintf(buffer,"\ndata:%x\n",Data); printf_(buffer); if ( *(__IO uint32_t*)Address != 0xffffffff) need_erase =1; FLASH_UnlockBank1(); if ( need_erase!=0 ){ printf_("\r\nNeeds erasing page first!erase now...\r\n"); } /* Define the number of page to be erased */ if ( (Address&0x3) !=0){ printf_("\r\nError:Address must align by four bytes!"); return 3; //3, 命令参数无效 } FLASH_ProgramWord(Address, Data); FLASH_LockBank1(); return 0; /* Check the correctness of written data */ //~ Address = BANK1_WRITE_START_ADDR; //~ while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED)) //~ { //~ if((*(__IO uint32_t*) Address) != Data) //~ { //~ MemoryProgramStatus = FAILED; //~ } //~ Address += 4; //~ } }
void ML_FLASH_EraseFlashPages(void) { uint32_t NbrOfPage = 0x00; uint32_t EraseCounter = 0x00; volatile FLASH_Status FLASHStatus = FLASH_COMPLETE; FLASH_UnlockBank1(); NbrOfPage = (WRITE_END_ADDR - WRITE_START_ADDR) / FLASH_PAGE_SIZE; FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } FLASH_LockBank1(); }
int flash_program_my(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ u32 data; data = 0x12340000 + (u32)iris_motor_mode; /* Porgram FLASH Bank1 ********************************************************/ /* Unlock the Flash Bank1 Program Erase controller */ FLASH_UnlockBank1(); /* Define the number of page to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } /* Program Flash Bank1 */ Address = BANK1_WRITE_START_ADDR; FLASHStatus = FLASH_ProgramWord(Address, (u32)data); FLASHStatus = FLASH_ProgramWord(Address+4, (u32)beep_enable); }
//================================================ void updateFW_control(void) { u8 cmd = 0; u8 crc = 0; u32* flagAddr; u32 flagValue = 0; FLASH_Status status = FLASH_COMPLETE; while(1) { // Reload IWDG counter IWDG_ReloadCounter(); cmd = USART_GetC(); switch(cmd) { case CMD_WRITE: crc = USART_GetC(); if(crc == (CMD_WRITE^0xFF)) { // Ack to host USART_PutC(ACK); write_mem(); } else USART_PutC(NACK); break; case CMD_REBOOT: crc = USART_GetC(); if(crc == (CMD_REBOOT^0xFF)) { // Ack to host USART_PutC(ACK); // Reboot uC NVIC_SystemReset(); } else USART_PutC(NACK); break; case CMD_UPDATE_COMPLETE: if(fUpdating) { // Write memmory FLASH_UnlockBank1(); status = FLASH_ErasePage(FLAG_ADDR); if(status == FLASH_COMPLETE) status = FLASH_ProgramWord(FLAG_ADDR,(u32)FLAG_UPDATED); fUpdating = false; FLASH_LockBank1(); if(status == FLASH_COMPLETE) USART_PutC(ACK); else USART_PutC(NACK); } else USART_PutC(NACK); break; case CMD_RUN_APP: // Read out flag value flagAddr = (u32*)FLAG_ADDR; flagValue = (u32)(*flagAddr); if(flagValue == FLAG_UPDATED) { USART_PutC(ACK); // Disable systick SysTick->CTRL &=~( SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk); // Jump to user application JumpAddress = *(__IO u32*) (MAIN_APP_ADDR+ 4); Jump_To_Application = (pFunction) JumpAddress; // Initialize user application's Stack Pointer __set_MSP(*(__IO u32*) MAIN_APP_ADDR); Jump_To_Application(); } else { USART_PutC(NACK); } break; default: break; } timingCount = 0; } }
int main(void) { initHardware(); int flashy = 0; BootloaderState state = BLS_UNDEFINED; char currentCommand = 0; while (1) { if (!jshIsUSBSERIALConnected()) { jshPinOutput(LED2_PININDEX, 0); // reset, led off } else { int f = (flashy>>9) & 0x7F; if (f&0x40) f=128-f; jshPinOutput(LED3_PININDEX, ((flashy++)&0xFF)<f); // flash led int d = getc(); if (d>=0) { // if we have data if (state==BLS_EXPECT_DATA) { } else if (state==BLS_INITED) { currentCommand = d; state = BLS_COMMAND_FIRST_BYTE; } else if (state==BLS_COMMAND_FIRST_BYTE) { if (currentCommand == d^0xFF) { unsigned int addr,i; char chksum, buffer[256]; unsigned int nBytesMinusOne, nPages; // confirmed switch (currentCommand) { case CMD_GET: // get bootloader info putc(ACK); putc(5); // 6 bytes // now report what we support putc(BOOTLOADER_MAJOR_VERSION<<4 | BOOTLOADER_MINOR_VERSION); // Bootloader version // list supported commands putc(CMD_GET); putc(CMD_GET_ID); putc(CMD_READ); putc(CMD_WRITE); putc(CMD_EXTERASE); // erase putc(ACK); // last byte break; case CMD_GET_ID: // get chip ID putc(ACK); putc(1); // 2 bytes // now report what we support putc(0x04); // 0x30 F1 XL density // 0x14 F1 high density putc(0x30); // TODO: really? putc(ACK); // last byte break; case CMD_READ: // read memory putc(ACK); addr = getc_blocking() << 24; addr |= getc_blocking() << 16; addr |= getc_blocking() << 8; addr |= getc_blocking(); chksum = getc_blocking(); // TODO: check checksum putc(ACK); setLEDs(2); // green = wait for data nBytesMinusOne = getc_blocking(); chksum = getc_blocking(); // TODO: check checksum putc(ACK); for (i=0;i<=nBytesMinusOne;i++) putc(((unsigned char*)addr)[i]); setLEDs(0); // off break; case CMD_WRITE: // write memory putc(ACK); addr = getc_blocking() << 24; addr |= getc_blocking() << 16; addr |= getc_blocking() << 8; addr |= getc_blocking(); chksum = getc_blocking(); // TODO: check checksum and address&3==0 putc(ACK); setLEDs(2); // green = wait for data nBytesMinusOne = getc_blocking(); for (i=0;i<=nBytesMinusOne;i++) buffer[i] = getc_blocking(); chksum = getc_blocking(); setLEDs(1); // red = write // TODO: check checksum and (nBytesMinusOne+1)&3==0 FLASH_UnlockBank1(); for (i=0;i<=nBytesMinusOne;i+=4) { unsigned int realaddr = addr+i; if (realaddr >= (FLASH_START+BOOTLOADER_SIZE)) // protect bootloader FLASH_ProgramWord(realaddr, *(unsigned int*)&buffer[i]); } FLASH_LockBank1(); setLEDs(0); // off putc(ACK); // TODO - could speed up writes by ACKing beforehand if we have space break; case CMD_EXTERASE: // erase memory putc(ACK); nPages = getc_blocking() << 8; nPages |= getc_blocking(); chksum = getc_blocking(); // TODO: check checksum if (nPages == 0xFFFF) { // all pages (except us!) setLEDs(1); // red = write FLASH_UnlockBank1(); for (i=BOOTLOADER_SIZE;i<FLASH_TOTAL;i+=FLASH_PAGE_SIZE) FLASH_ErasePage((uint32_t)(FLASH_START + i)); FLASH_LockBank1(); setLEDs(0); // off putc(ACK); } else { putc(NACK); // not implemented } break; default: // unknown command putc(NACK); break; } } else { // not correct putc(NACK); } state = BLS_INITED; } else { switch (d) { case 0x7F: // initialisation byte putc(state == BLS_UNDEFINED ? ACK : NACK); state = BLS_INITED; break; } } } } } }
/************************************************* Function: hw_flash_init Descroption: Input: None Output: Return: Other: *************************************************/ void hw_flash_init(void) { FLASH_UnlockBank1(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); }
int RCFS_AddFile( unsigned char *data, int length, char *name ) { long *toc = (long *)(baseaddr + VTOC_OFFSET); long addr; long size; short slot; long maxaddr = 0; long nextaddr = 0; volatile FLASH_Status FLASHStatus = FLASH_COMPLETE; flash_file f; // bounds check length if( (length <= 0) || (length > MAX_FLASH_FILE_SIZE)) return(RCFS_ERROR); // more than kMaxNumbofFlashFiles files we have an error for(slot=0;slot<kMaxNumbofFlashFiles;slot++) { // Read next file address addr = *toc; // End of table ? if( addr == (-1) ) { // Start on Word boundary if(nextaddr & 1) nextaddr++; // move us into high menory // V3.51 had crap at 8040000 so we had to push back // to 8030000 if(nextaddr < 0x18000) nextaddr = 0x18000; // Check if there is room for the file // We reserve 4K for user parameter storage if( (nextaddr + length ) > 0x47000 ) return(RCFS_ERROR); // create new file RCFS_FileInit( &f ); // Copy name, max 15 chars strncpy( &f.name[0], name, 15 ); // setup address, data pointer and length for this file f.addr = baseaddr + nextaddr; f.data = data; f.datalength = length; #ifdef FFDEBUG // Debug RCFS_DebugFile(&f); #endif // Unlock the Flash Bank1 Program Erase controller FLASH_UnlockBank1(); // Clear All pending flags FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // write table of contents entry FLASHStatus = FLASH_ProgramWord( (uint32_t)toc++, nextaddr ); FLASHStatus = FLASH_ProgramWord( (uint32_t)toc++, length + FLASH_FILE_HEADER_SIZE ); // Write file RCFS_Write( &f ); // We are done return(RCFS_SUCCESS); } else { // Valid file found toc++; size = *toc++; // Last file in memory ? if( addr > maxaddr ) { // maximum address found maxaddr = addr; // Address after this file nextaddr = addr + size; } } } // No more VTOC space if here return(RCFS_ERROR); }
u8 WriteConfigurationBlock(void) { // load all of the config RAM registers into one big array ConfigData[0] = BOARDCONFIG_ram; ConfigData[1] = MIDI0MODE_ram; ConfigData[2] = MIDI1MODE_ram; ConfigData[3] = MIDI2MODE_ram; ConfigData[4] = MIDI3MODE_ram; ConfigData[5] = MIDI4MODE_ram; ConfigData[6] = MIDI5MODE_ram; ConfigData[7] = MIDI6MODE_ram; ConfigData[8] = MIDI7MODE_ram; ConfigData[9] = MIDI8MODE_ram; ConfigData[10] = MIDI9MODE_ram; ConfigData[11] = MIDI10MODE_ram; ConfigData[12] = MIDI11MODE_ram; ConfigData[13] = MIDI12MODE_ram; ConfigData[14] = MIDI13MODE_ram; ConfigData[15] = MIDI0POL_ram; ConfigData[16] = MIDI1POL_ram; ConfigData[17] = MIDI2POL_ram; ConfigData[18] = MIDI3POL_ram; ConfigData[19] = MIDI4POL_ram; ConfigData[20] = MIDI5POL_ram; ConfigData[21] = MIDI6POL_ram; ConfigData[22] = MIDI7POL_ram; ConfigData[23] = MIDI8POL_ram; ConfigData[24] = MIDI9POL_ram; ConfigData[25] = MIDI10POL_ram; ConfigData[26] = MIDI11POL_ram; ConfigData[27] = MIDI12POL_ram; ConfigData[28] = MIDI13POL_ram; ConfigData[29] = MIDI0SENS_ram; ConfigData[30] = MIDI1SENS_ram; ConfigData[31] = MIDI2SENS_ram; ConfigData[32] = MIDI3SENS_ram; ConfigData[33] = MIDI4SENS_ram; ConfigData[34] = MIDI5SENS_ram; ConfigData[35] = MIDI6SENS_ram; ConfigData[36] = MIDI7SENS_ram; ConfigData[37] = MIDI8SENS_ram; ConfigData[38] = MIDI9SENS_ram; ConfigData[39] = MIDI10SENS_ram; ConfigData[40] = MIDI11SENS_ram; ConfigData[41] = MIDI12SENS_ram; ConfigData[42] = MIDI13SENS_ram; ConfigData[43] = MIDI0CURVE_ram; ConfigData[44] = MIDI1CURVE_ram; ConfigData[45] = MIDI2CURVE_ram; ConfigData[46] = MIDI3CURVE_ram; ConfigData[47] = MIDI4CURVE_ram; ConfigData[48] = MIDI5CURVE_ram; ConfigData[49] = MIDI6CURVE_ram; ConfigData[50] = MIDI7CURVE_ram; ConfigData[51] = MIDI8CURVE_ram; ConfigData[52] = MIDI9CURVE_ram; ConfigData[53] = MIDI10CURVE_ram; ConfigData[54] = MIDI11CURVE_ram; ConfigData[55] = MIDI12CURVE_ram; ConfigData[56] = MIDI13CURVE_ram; ConfigData[57] = MIDI0NOTE_ram; ConfigData[58] = MIDI1NOTE_ram; ConfigData[59] = MIDI2NOTE_ram; ConfigData[60] = MIDI3NOTE_ram; ConfigData[61] = MIDI4NOTE_ram; ConfigData[62] = MIDI5NOTE_ram; ConfigData[63] = MIDI6NOTE_ram; ConfigData[64] = MIDI7NOTE_ram; ConfigData[65] = MIDI8NOTE_ram; ConfigData[66] = MIDI9NOTE_ram; ConfigData[67] = MIDI10NOTE_ram; ConfigData[68] = MIDI11NOTE_ram; ConfigData[69] = MIDI12NOTE_ram; ConfigData[70] = MIDI13NOTE_ram; FLASH_UnlockBank1(); /* Define the number of page to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } // Write the blocks uint32_t flashAddress = 0x08007800; uint8_t *dataaddress = &ConfigData[0]; uint32_t data; dataaddress = &ConfigData[0]; for(u8 i = 0; i<18; i++){ uint8_t j = i*4; // dataaddress = &ConfigData[j]; // data = (uint32_t)*(dataaddress + j); data = ConfigData[j]+(ConfigData[j+1]*0x00000100)+(ConfigData[j+2]*0x00010000)+(ConfigData[j+3]*0x01000000); FLASHStatus = FLASH_ProgramWord(flashAddress + j, data); } FLASH_LockBank1(); // lock the Flash memory again ConfigurePorts(); // reconfigure the Ports any time you update the configuration return OK; }
/******************************************************************************** * FunctionName: Flash_Write * * Description : * * Parameters : * * Returns : * * @brief Programs a half word at a specified Option Byte Data address. * @note This function can be used for all STM32F10x devices. * @param Address: specifies the address to be programmed. * @param buf: specifies the data to be programmed. * @param iNbrToWrite: the number to write into flash * @retval if success return the number to write, -1 if error // iAddress:保存EEPROM数据的起始地址(0~) // iNbrToRead:保存数据长度,要求数据长度小于一页 // *buf:保存数据缓存指针 ********************************************************************************/ int Flash_Write(uint32_t iAddress, uint8_t *buf, uint32_t iNbrToWrite) { volatile FLASH_Status FLASHStatus = FLASH_COMPLETE; /* Unlock the Flash Bank1 Program Erase controller */ uint32_t secpos; uint32_t iNumByteToWrite = iNbrToWrite; uint16_t secoff; uint16_t secremain; uint16_t i = 0; uint8_t tmp[FLASH_PAGE_SIZE]; FLASH_UnlockBank1(); secpos=iAddress & (~(FLASH_PAGE_SIZE -1 )) ;//扇形地址 secoff=iAddress & (FLASH_PAGE_SIZE -1); //在扇区内的偏移 secremain=FLASH_PAGE_SIZE-secoff; //扇区剩余空间大小 /*不大于4096个字节*/ if(iNumByteToWrite<=secremain) secremain = iNumByteToWrite; while( 1 ) { /*读出整个扇区*/ Flash_Read(secpos, tmp, FLASH_PAGE_SIZE); /*校验数据*/ for(i=0;i<secremain;i++) { if(tmp[secoff+i]!=0XFF)//需要擦除 { break; } } /*需要擦除*/ if(i < secremain) { /*擦除这个扇区*/ FLASHStatus = FLASH_ErasePage(secpos); if(FLASHStatus != FLASH_COMPLETE) { return -1; } /*复制*/ for(i=0;i<secremain;i++) { tmp[i+secoff]=buf[i]; } /*写入整个扇区*/ Flash_Write_Without_check(secpos ,tmp ,FLASH_PAGE_SIZE); } /*写已经擦除了的,直接写入扇区剩余区间*/ else { Flash_Write_Without_check(iAddress,buf,secremain); } /*写入结束*/ if(iNumByteToWrite == secremain) { break; } else { secpos += FLASH_PAGE_SIZE; secoff = 0; //偏移位置为0 buf += secremain; //指针偏移 iAddress += secremain; //写地址偏移 iNumByteToWrite -= secremain; //字节数递减 if(iNumByteToWrite > FLASH_PAGE_SIZE) { secremain = FLASH_PAGE_SIZE; /*下一个扇区还是写不完*/ } else { secremain = iNumByteToWrite; /*下一个扇区可以写完了*/ } } } FLASH_LockBank1(); return iNbrToWrite; }