// 检查要写的位置是否为空,若不空,则擦除该位置所在扇区. // 注意:在给定地址和要写入的字节数后,须首先检查要写入范围内的每个位置,若某位置已不空,则必须擦除该位置所在扇区。 // 对于地址范围内的每个扇区,须从一开始就检查该扇区要写入的所有位置,不可只检查一部分,写入,而后在检查后面部分时, // 发现该部分不空,从而导致擦除整个扇区,之后又再次从前面再写入的情况。 bool CSerialFlash::Write(u8* pBuf, u32 WriteAddr, u32 NumBytesToWrite, u32 *NumBytesWritten) { if (WriteAddr >= CHIP_SIZE) return false; if (NumBytesWritten) *NumBytesWritten = 0; NumBytesToWrite = min(NumBytesToWrite, CHIP_SIZE - WriteAddr); if (NumBytesToWrite == 0) return true; u32 endAddr = WriteAddr + NumBytesToWrite - 1; // when WriteAddr = NumBytesToWrite = 0, endAddr will be 0xFFFFFFFF, and that's wrong. for (u32 addr = WriteAddr; addr <= endAddr; ) // check and erase { addr = RangeBlank(addr, endAddr); if (addr < CHIP_SIZE) { if (!SectorErase(addr)) return false; addr = (addr / SECTOR_SIZE + 1) * SECTOR_SIZE; } else break; } return WriteWithoutCheck(pBuf, WriteAddr, NumBytesToWrite, NumBytesWritten); }
void AP_Logger_SITL::StartErase() { for (uint32_t i=0; i<DF_NUM_PAGES/DF_PAGE_PER_SECTOR; i++) { SectorErase(i); } erase_started_ms = AP_HAL::millis(); }
int SectorProg(U32 begin, U16 *data, U32 size) { U32 tmp = 0x1000-(begin&0xfff); if(tmp>size) tmp = size; for(; size;) { SectorErase(begin&0xfffff000); //4K Bytes boudary FlashProg(begin, data, tmp/2); size -= tmp; begin += tmp; data += tmp/2; tmp = (size>0x1000)?0x1000:size; } return 0; }
/****************************************************************************** ** INTERNAL FUNCTION DEFINITIONS ******************************************************************************/ int main(void) { volatile unsigned int count = 0x0FFFu; unsigned int retVal = FALSE; unsigned char choice = 0; /* Enable the clocks for McSPI0 module.*/ McSPI0ModuleClkConfig(); /* Perform Pin-Muxing for SPI0 Instance */ McSPIPinMuxSetup(0); /* Perform Pin-Muxing for CS0 of SPI0 Instance */ McSPI0CSPinMuxSetup(chNum); /* Initialize the UART utility functions */ UARTStdioInit(); UARTPuts("Here the McSPI controller on the SoC communicates with", -1); UARTPuts(" the SPI Flash.\r\n\r\n", -1); /* Enable IRQ in CPSR.*/ IntMasterIRQEnable(); /* Map McSPI Interrupts to AINTC */ McSPI0AintcConfigure(); /* Do the necessary set up configurations for McSPI.*/ McSPISetUp(); /* Pass the write enable command to flash.*/ WriteEnable(); /* Wait until write enable command is successfully written to flash.*/ while(FALSE == retVal) { retVal = IsWriteSuccess(); } retVal = FALSE; UARTPuts("Do you want to erase a sector of the flash before ", -1); UARTPuts("writing to it ?.", -1); UARTPuts("\r\nInput y(Y)/n(N) to proceed.\r\n", -1); choice = UARTGetc(); UARTPutc(choice); if(('Y' == choice) || ('y' == choice)) { /* Erase a sector of flash.*/ SectorErase(); } /* Pass the write enable command to flash.*/ WriteEnable(); /* Wait until write enable command is successfully written to flash.*/ while(FALSE == retVal) { retVal = IsWriteSuccess(); } /* Write data of 1 page size to flash.*/ WriteToFlash(); while(count--); count = 0x0FFFu; /* Read data of 1 page size from flash.*/ ReadFromFlash(); while(count--); /* Verify the data written to and read from flash are same or not.*/ VerifyData(); while(1); }
//等待刷卡 void WaitingBrush(void) { unsigned char i,j; do { WDT_CONTR = 0x3E; MIF_Halt(); Request(RF_CMD_REQUEST_STD); AntiColl(); if(SelectCard()==FM1702_OK) return; else if(Brushed ==1) { Brushed = 0; Show(0,0); } else { if(RxD_Buffer == 0x55) //0x55表示指令代码 { RxD_Buffer = 0; SendData(0x00); //在没有刷卡的状态下,发送给主机0x00; } else if((RxD_Buffer>>4) == 6) //RxD_Buffer高4位为6,底4位是通道号,记忆卡的ID号 { if((RxD_Buffer!=0x6f)&&(RxD_Buffer!=0x6e)) Channel = RxD_Buffer&0x0f; else { if(RxD_Buffer==0x6f) { for(i=0;i<5;i++){USER_Card_ID[Channel][i]=gCard_UID[i];}//Last_gCard_UID[i]=0; Write_E2PROM8_CardID(); //每开通某个通道,就写E2PROM一次,记录下该卡号的id Delay_1ms(200); Delay_1ms(200); Delay_1ms(200); Delay_1ms(200); } Channel = 0x0f; } } else if(RxD_Buffer == 0x77) //0x77功能代码表示,刷卡单位时间的发送 { Delay_1ms(100); SendData(0x33); //无效指令 Delay_1ms(100); SK_Timer = RxD_Buffer*100; RxD_Buffer = 0; SendData(0x33); //无效指令 Delay_1ms(100); SK_Timer += RxD_Buffer; RxD_Buffer = 0; SendData(0x33); //无效指令 Delay_1ms(100); //Show(SK_Timer,0); SectorErase(0x600); ByteProgram(0x600,(SK_Timer/100)); ByteProgram(0x601,(SK_Timer%100)); IAP_Disable(); } else if((RxD_Buffer>>4) == 8) { Delay_1ms(100); SendData(0x33); //无效指令 for(i=0;i<5;i++){USER_Card_ID[RxD_Buffer&0x0f][i]=0;} Write_E2PROM8_CardID(); Delay_1ms(200); Delay_1ms(200); Delay_1ms(200); Delay_1ms(200); } else if(RxD_Buffer == 0x99) { for(j=0;j<10;j++){for(i=0;i<5;i++){USER_Card_ID[j][i]=0;}} Write_E2PROM8_CardID(); Delay_1ms(200); Delay_1ms(200); Delay_1ms(200); Delay_1ms(200); } }
void main() { uchar shujv[8]; uchar stop=0; uchar cejv=1; uchar flag=0; uchar helpcejv=0; init_io(); led0=1;led1=1;ledk=1; while(NRF_Check()) { led0=0; delayms(1000); led0=1; delayms(1000); } TX_Mode(); //设置为发送模式 /*************自己加的***********************/ while(CNRF_Check()) //检查nrf是否存在 { led0=0; delayms(1000); led0=1; delayms(1000); } CTX_Mode(); /*************自己加的***********************/ stop=byte_read(0x2000); //读取上一次保留的值 while(1) { wenshi(shujv); //写入前4个数据 // yali(shujv);//写入第5个数据 // shache(shujv);//写入第六个数据 Get_weight(); if (weight_shiwu/1000<=5) shujv[4]='0'; else shujv[4]='1'; if(stop==0) shujv[5]='1'; if(stop==1) shujv[5]='0'; if(out==0) shujv[6]='1'; else shujv[6]='0'; ////////////////////////////// if(NRF24L01_TxPacket(shujv)==TX_OK) { led1=0; delayms(300); led1=1; delayms(10); ///// flag=1; } else flag=0; ////////////////////////////// if(flag==1) { if(CNRF24L01_TxPacket(shujv)==TX_OK) { led3=0; delayms(300); led3=1; delayms(10); cejv=0; helpcejv=0; } else { helpcejv++; delayms(100); if(helpcejv>2) { led2=0; delayms(300); led2=1; delayms(10); cejv=0; helpcejv=0; } } ///////////////////////////////////////////////// if(shache(cejv,stop)==1) { led0=0; delayms(300); led0=1; delayms(10); stop=1; SectorErase(0x2000); byte_write(0x2000,stop); } if(checkbutton(cejv,stop)==0) {stop=1; SectorErase(0x2000); byte_write(0x2000,stop); } } } }
int main( int ArgC, char *ArgV[] ) { int BDMHandle; unsigned int Base; unsigned int Chips; unsigned int Bytes; char *Operation; FlashError_t Error; if (ArgC < 6) Usage( ArgV[0] ); if((BDMHandle = bdm_init(ArgV[1]))<0) { fprintf( stderr, "Problem opening bdm device %s, error code %d.\n", ArgV[1], BDMHandle ); return (EXIT_FAILURE); } Base = (unsigned int) strtoul( ArgV[2], NULL, 0 ); Chips = (unsigned int) strtoul( ArgV[3], NULL, 0 ); Bytes = (unsigned int) strtoul( ArgV[4], NULL, 0 ); Operation = ArgV[5]; Error = BDMFlashConfigSet( BDMHandle, Base, Chips, Bytes ); if (Error != FlashErrorOkay_c) { fprintf( stderr, "Problem configuring flash; error code = %d '%s'.\n", (int) Error, FlashErrorDescriptionEnglish[Error] ); } if (strcasecmp( Operation, "erase" ) == 0) { Erase( ArgC, ArgV ); } else if (strcasecmp( Operation, "SectorErase" ) == 0) { SectorErase( ArgC, ArgV ); } else if (strcasecmp( Operation, "BlockErase" ) == 0) { BlockErase( ArgC, ArgV ); } else if (strcasecmp( Operation, "write" ) == 0) { Write( ArgC, ArgV ); } else if (strcasecmp( Operation, "read" ) == 0) { Read( BDMHandle, ArgC, ArgV ); } else if (strcasecmp( Operation, "probe" ) == 0) { Probe( ArgC, ArgV ); } else if (strcasecmp( Operation, "detect" ) == 0) { Detect( ArgC, ArgV ); } else Usage( ArgV[0] ); bdm_release( BDMHandle ); return (EXIT_SUCCESS); }