void Flash_Write(uint8_t addr, uint32_t value) { if (addr < USED_SIZE / 4) { /* 缓存Flash中其他位置的数据 */ uint32_t dist = 0; Flash_Read(0, &dist); Using_Values[addr] = value; FMC_Erase(NEW_BLOCK_ADDR); Block_Write(NEW_BLOCK_ADDR, Using_Values); FMC_Erase(BAK_BLOCK_ADDR); Block_Write(BAK_BLOCK_ADDR, Using_Values); } }
void State_Machine(void) { switch (M_State) { case ST_RX_SETUP: Receive_Setup(); // Receive and decode host Setup Message break; case ST_RX_FILE: Receive_File(); // Receive File data from host break; case ST_TX_ACK: M_State = ST_RX_FILE; // Ack Transmit complete, continue RX data break; case ST_TX_FILE: // Send file data to host WriteStageLength = ((BytesToWrite - BytesWrote) > MAX_BLOCK_SIZE_WRITE)? MAX_BLOCK_SIZE_WRITE:(BytesToWrite - BytesWrote); BytesWrote += Block_Write((BYTE*)(ReadIndex), WriteStageLength); ReadIndex += WriteStageLength; if ((BlocksWrote%8) == 0) Led2 = ~Led2; if (BytesWrote == NumBytes) Led2 = 0; break; default: break; } }
// sends all accumulated events. if a xmit fifo is empty then this call returns in about 14us. // it takes about 860us if we send 225 events. void sendEvents(){ // EA=0; //USB_Int_Disable(); // disable USB interrupt while writing buffer to FIFO EIE1 &= ~0x02; // disable USB interrupt Block_Write((BYTE*)AEBuffer, AECounter*4); EIE1 |= 0x02; // enable USB interrupt AECounter=0; // reset counter and pointer for event buffering AEPtr=AEBuffer; TH1=0; TL1=0; // reset timer1 because we only want advance xfer if we haven't sent events when timer1 wraps from 0 // EA=1; //USB_Int_Enable(); }
// sends all accumulated events. if a xmit fifo is empty then this call returns in about 14us. // it takes about 860us if we send 225 events. void sendEvents(){ // EA=0; //USB_Int_Disable(); // disable USB interrupt while writing buffer to FIFO LedBlueOn(); // toggle LED on each packet EIE1 &= ~0x02; Block_Write((BYTE*)AEBuffer, AECounter*4); EIE1 |= 0x02; AECounter=0; // reset counter and pointer for event buffering AEPtr=AEBuffer; LedBlueOff(); // toggle LED on each packet // EA=1; //USB_Int_Enable(); }
/* * Write a full GOSFS block to disk * Return 0 on success, exits on error * Note: blocknum is the GOSFS block number */ int GOSFS_Block_Write(struct Block_Device *dev, int blockNum, void *buf) { int rc = 0; int index = 0; for (index = 0; index < DISK_BLOCKS_PER_GOSFS; index++) { rc = Block_Write(dev, blockNum * DISK_BLOCKS_PER_GOSFS + index, buf + (index * SECTOR_SIZE)); if (rc != 0) Exit(rc); } return rc; }
/* Writes an entire GOSFSBlock (4KB) using the src */ static int writeGOSFSBlock(struct Block_Device *dev, int gosfsBlock, void *src, size_t numBytes) { char buffer[GOSFS_BLOCK_SIZE], *bufferPtr = buffer; int blockIndex = gosfsBlock * (GOSFS_BLOCK_SIZE / SECTOR_SIZE); int i, rc; memcpy(buffer, src, numBytes); for (i = 0; i < (GOSFS_BLOCK_SIZE / SECTOR_SIZE); i++) { rc = Block_Write(dev, blockIndex + i, bufferPtr); if (rc < 0) goto fail; bufferPtr += SECTOR_SIZE; } return 0; fail: return rc; }
//write character string into newly created file //returns number of bytes written int Write_File(int inode_number, int offset, char* to_write, int bytes) { //make sure inode is valid file to read from Inode curNode = Inode_Read(inode_number); if ((curNode.Inode_Number == -1) || (curNode.Flag != 1)) { printf("Error, Inode is invalid\n"); return -1; } //inode is not a valid file/is not open //make sure we're adding onto the end of the file if (offset != curNode.File_Size) { printf("Error, writing to an invalid offset\n"); return -1; } //trying to write to an invalid offset //make sure we have enough space to write the string int endBlock = curNode.End_Block; if (endBlock == -1) { Super_block curSuper = Superblock_Read(); endBlock = curSuper.next_free_block; } int totalBytes = bytes; //total number of bytes if ((curNode.File_Size % 512) != 0) //look at existing block space { totalBytes += curNode.File_Size % 512; totalBytes -= 512; } endBlock += totalBytes / 512; //additional full blocks needed if ((totalBytes % 512) != 0) {endBlock++;} //overflow into next block if (endBlock > 8191) { printf("Error, not enough memory for file\n"); return -1; } //we don't have enough blocks //if this is a new file, set up inode with next free block int curBlock = curNode.End_Block; int bytesWritten = 0; totalBytes = bytes; Super_block curSuper = Superblock_Read(); if (curBlock == -1) { curBlock = curSuper.next_free_block++; Superblock_Write(curSuper); curNode.Start_Block = curBlock; Inode_Write(inode_number, curNode); } //fill up last file block if it is not full yet if ((curNode.File_Size % 512) != 0) { int curLength = curNode.File_Size % 512; int fillLength = 512 - curLength; if (bytes < fillLength) {fillLength = bytes;} int totalLength = curLength + fillLength; char curString[totalLength]; if (Block_Read(curBlock, curLength, curString) != curLength) { printf("Error, could not read block\n"); return -1; } //error filling up block if (concatString(curString, to_write, curLength, fillLength) != fillLength) { printf("Error, could not copy string portion\n"); return -1; } //error filling up block if (Block_Write(curBlock, totalLength, curString) != totalLength) { printf("Error, could not write to block\n"); return -1; } //error writing to block to_write += fillLength; bytesWritten += fillLength; curNode.File_Size += bytesWritten; if (bytesWritten != bytes) { curBlock = curSuper.next_free_block++; Superblock_Write(curSuper); } } int bytesLeft = bytes - bytesWritten; int totalBytesWritten = bytesWritten; while(bytesLeft) { bytesWritten = Block_Write(curBlock, (bytesLeft > 512 ? 512:bytesLeft), to_write); if(bytesWritten == 0) { printf("Error, could not write to block\n"); return -1; } bytesLeft -= bytesWritten; to_write += bytesWritten; totalBytesWritten += bytesWritten; curNode.File_Size += bytesWritten; if (bytesLeft) { curBlock = curSuper.next_free_block++; Superblock_Write(curSuper); } } curNode.End_Block = curBlock; Inode_Write(inode_number, curNode); return totalBytesWritten; }