TInt DMediaDriverFlashWin32::DoRead() { if (iWriteReq) return 1; // write in progress so defer read __KTRACE_OPT(KLOCDRV,Kern::Printf("Flash:DoRead")); if (iState==EErasePending) { iTimer.Cancel(); iState = ESuspended; } if (iState==EIdle || iState==ESuspended) { // can do the read now TInt pos=(TInt)iReadReq->Pos(); TInt len=(TInt)iReadReq->Length(); TPtrC8 des(iBase+pos,len); TInt r=iReadReq->WriteRemote(&des,0); Complete(EReqRead,r); if (iState==ESuspended) StartErase(); } else if (iState==EErase) { // erase in progress - suspend it SuspendErase(); } else if (iState==EEraseNoSuspend) iState=ESuspendPending; // wait for suspend to complete return KErrNone; }
void DMediaDriverFlashWin32::CompleteWrite() // // Do the actual write in the completion // Transfer data in blocks from the client and AND it to the 'flash' // { __KTRACE_OPT(KLOCDRV,Kern::Printf("Flash:WriteComplete")); TInt r = KErrNone; TUint8* flash = iBase + (TInt)iWriteReq->Pos(); TInt len = (TInt)iWriteReq->Length(); TInt offset = 0; while (len > 0) { TInt size = Min(len, KDataBufSize); TPtr8 des(iData,size); r = iWriteReq->ReadRemote(&des, offset); if (r!=KErrNone) break; len -= size; offset += size; const TUint8* ptr = iData; do { *flash++ &= *ptr++; } while (--size > 0); } if (iState == EWriting) iState = EIdle; Complete(EReqWrite,r); if (iState == ESuspended) StartErase(); }
TInt DMediaDriverFlashWin32::DoErase() { if (iReadReq || iWriteReq) return 1; // read or write in progress so defer this request TInt pos=(TUint32)iEraseReq->Pos(); TInt len=(TUint32)iEraseReq->Length(); __KTRACE_OPT(KLOCDRV,Kern::Printf("Flash:DoErase %d@%08x",len,pos)); if (len!=iEraseBlockSize) return KErrArgument; // only allow single-block erase if (pos & (iEraseBlockSize-1)) return KErrArgument; // start position must be on erase block boundary __ASSERT_ALWAYS(iState==EIdle,FLASH_FAULT()); iErasePos=pos; StartErase(); return KErrNone; }
void AP_Logger_SITL::Init() { if (flash_fd == 0) { flash_fd = open("dataflash.bin", O_RDWR|O_CLOEXEC, 0777); if (flash_fd == -1) { flash_fd = open("dataflash.bin", O_RDWR | O_CREAT | O_CLOEXEC, 0777); StartErase(); erase_started_ms = 0; } if (ftruncate(flash_fd, DF_PAGE_SIZE*uint32_t(DF_NUM_PAGES)) != 0) { AP_HAL::panic("Failed to create dataflash.bin"); } } df_PageSize = DF_PAGE_SIZE; df_PagePerSector = DF_PAGE_PER_SECTOR; df_NumPages = DF_NUM_PAGES; AP_Logger_Block::Init(); }