bool Flash_Read(FLASH_HANDLE Handle, alt_u32 offset, alt_u8 *szBuf, alt_u32 size){ FLASH_INFO *pFlash = (FLASH_INFO *)Handle; int error_code; if (!pFlash->fd_flash) return FALSE; error_code = alt_read_flash(pFlash->fd_flash, offset, szBuf, size); if (error_code == 0) return TRUE; return FALSE; }
/****************************************************************** * Function: FlashTestBlockWrite * * Purpose: Tests that the function alt_write_flash_block is * is working properly. * ******************************************************************/ static int FlashTestBlockWrite(int block, int *error, alt_flash_fd* fd, flash_region* regions) { int i; int ret_code = 0x0; int test_offset; alt_u8 *data_written; alt_u8 *data_read; /* Get a couple buffers for the test */ data_written = malloc(100); data_read = malloc(100); test_offset = (regions->offset + (block * regions->block_size)); /* Fill write buffer with 100 values (incremented by 3) */ for(i=0; i < 100; i++) *(data_written + i) = (i * 3); /* Write the buffer to flash starting 0x40 bytes from the beginning of the block. */ printf(" -Testing \"alt_write_flash_block\"."); ret_code = alt_write_flash_block(fd, test_offset, (test_offset + 0x40), data_written, 100); if (!ret_code) { /* Now read it back into the read_buffer */ ret_code = alt_read_flash(fd, (test_offset + 0x40), data_read, 100); if(!ret_code) { /* See if they match */ if (memcmp(data_written, data_read, 100)) { printf(" FAILED.\n"); *error++; } else printf(" passed.\n"); } } /* Test unaligned writes */ if(!ret_code) { /* Erase the block */ ret_code = alt_erase_flash_block(fd, test_offset, regions->block_size); /* Write the buffer to flash on an unaligned address. */ printf(" -Testing unaligned writes."); ret_code = alt_write_flash_block(fd, test_offset, (test_offset + 0x43), data_written, 100); if (!ret_code) { /* Now read it back into the read_buffer */ ret_code = alt_read_flash(fd, (test_offset + 0x43), data_read, 100); if(!ret_code) { /* See if they match */ if (memcmp(data_written, data_read, 100)) { printf(" FAILED.\n"); *error++; } else printf(" passed.\n"); } } } /* Free up the buffers we allocated. */ free(data_written); free(data_read); return ret_code; }
/****************************************************************** * Function: FlashTestReadWrite * * Purpose: Tests that the functions alt_write_flash and * alt_read_flash are working properly, as well as tests * that every bit in the specified block can store both * a '1' and '0'. * ******************************************************************/ static int FlashTestReadWrite(int block, int *error, alt_flash_fd* fd, flash_region* regions) { int i; int ret_code = 0x0; int test_offset; alt_u8 *data_written; alt_u8 *data_read; /* Get a couple buffers for the tests */ data_written = malloc(regions->block_size); data_read = malloc(regions->block_size); /* Calculate the offset at which the block lives */ test_offset = (regions->offset + (block * regions->block_size)); printf("\n -Starting Flash Test.\n"); printf(" -Testing \"alt_write_flash\" and \"alt_read_flash\".\n"); /* Fill buffer with incrementing values */ for(i=0; i < regions->block_size; i++) *(data_written + i) = i; /* Write the buffer to flash block */ ret_code = alt_write_flash(fd, test_offset, data_written,regions->block_size); if (!ret_code) { /* Read flash block into read buffer */ ret_code = alt_read_flash(fd, test_offset, data_read, regions->block_size); if(!ret_code) { /* See if they match */ if (memcmp(data_written, data_read, regions->block_size)) { printf(" pass 1 - FAILED.\n"); *error++; } else printf(" pass 1 - passed.\n"); } /* Now fill the buffer with decrementing values (invert the incrementing ones) */ for(i=0; i < regions->block_size; i++) *(data_written + i) = ~((alt_u8)(i)); /* Write the buffer to flash block */ ret_code = alt_write_flash(fd, test_offset, data_written, regions->block_size); if (!ret_code) { /* Read flash block into read buffer */ ret_code = alt_read_flash(fd, test_offset, data_read, regions->block_size); if(!ret_code) { /* See if they match */ if (memcmp(data_written, data_read, regions->block_size)) { printf(" pass 2 - FAILED.\n"); *error++; } else printf(" pass 2 - passed.\n"); } } if (*error) ret_code = 1; } /* Free up the buffers we allocated */ free(data_written); free(data_read); return ret_code; }
static void ExecuteCmd(const char const *input, const u32 base) { SendStr("\r\n", base); // Tokenize the command #define MAX_CMD_WORDS 4 char *token[MAX_CMD_WORDS]; char *cmd = (char *)input; u8 numTokens = 0; while (1) { // Skip leading whitespace. while ((*cmd) && isspace(*cmd)) cmd++; // If we get here and we are at the end of the string, then the last // token must have had trailing white spaces. Let's ignore them if (!(*cmd)) break; // If we have exceeded the maximum number of allowable tokens, then // return as error if (numTokens >= MAX_CMD_WORDS) { SendStr(NO_ANSWER, base); return; } // Store the token. token[numTokens] = cmd; numTokens++; // Everything that isn't a whitespace is part of the token. Let's make // sure it is in UPPER CASE while ((*cmd) && (!isspace(*cmd))) { *cmd = toupper(*cmd); cmd++; } // When we get here, we are just past the current token, either because // it ended on a whitespace or because it is the end of the user input. // If the former, then let's force a null termination for that token. If // the latter, then we are done tokenizing. if (!(*cmd)) break; *cmd = '\0'; cmd++; } if (0 == numTokens) { SendStr(NO_ANSWER, base); return; } // Process the command switch (token[0][0]) { case 'R': { if (2 != numTokens) SendStr(NO_ANSWER, base); else { u32 regAddr; u32 regValue; if (StrToU32(token[1], ®Addr) && RegRead(regAddr, ®Value)) { SendStr("Y ", base); char regValStr[9]; U32ToStr(regValue, regValStr); SendStr(regValStr, base); SendStr("\r\n", base); } else SendStr(NO_ANSWER, base); } break; } case 'W': { if (3 != numTokens) SendStr(NO_ANSWER, base); else { u32 regAddr; u32 regValue; if (StrToU32(token[1], ®Addr) && StrToU32(token[2], ®Value) && RegWrite(regAddr, regValue)) SendStr(YES_ANSWER, base); else SendStr(NO_ANSWER, base); } break; } case 'V': { SendStr("FPGA=0x", base); char versionStr[9]; FpgaRegisters * FPGARegs = (FpgaRegisters *)(REGISTER_BASE | BYPASS_DCACHE_MASK); U32ToStr(FPGARegs->fpgaVersion, versionStr); SendStr(versionStr, base); SendStr(" NIOS=0x", base); U32ToStr(NIOS_VERSION, versionStr); SendStr(versionStr, base); SendStr("\r\n", base); break; } case 'F': { if (4 != numTokens) SendStr(NO_ANSWER, base); else { u32 startAddr; u32 length; u32 checksum; StrToU32(token[1], &startAddr); StrToU32(token[2], &length); StrToU32(token[3], &checksum); // Transfer two chunks to get a full sector worth #define FLASH_SECTOR_SIZE (64*1024) #define TRANSFER_SIZE (4*1024) u8 buffer[FLASH_SECTOR_SIZE]; // Validate the requested transfer size if (length != TRANSFER_SIZE) SendStr(NO_ANSWER, base); else { u32 bufferIndex = startAddr % FLASH_SECTOR_SIZE; u32 runningSum = 0; u32 numBytesReceived = 0; // Clear the input buffer FlushRx(base); // Acknowledge that the command is good. This will tell the // sender to actually send the specified number of bytes SendStr(YES_ANSWER, base); // We must receive the correct number of bytes while (true) { while (IORD_FIFOED_AVALON_UART_STATUS(base) & FIFOED_AVALON_UART_CONTROL_RRDY_MSK) { // Read the Uart u8 rx = IORD_FIFOED_AVALON_UART_RXDATA(base); runningSum += rx; buffer[bufferIndex++] = rx; numBytesReceived++; if (numBytesReceived >= length) break; } if (numBytesReceived >= length) break; } // check the checksum if (runningSum != checksum) SendStr(NO_ANSWER, base); else { // If we don't have a full sector worth of data, then ACK and wait for more if (bufferIndex != FLASH_SECTOR_SIZE) SendStr(YES_ANSWER, base); else { u32 totalWriteBufferChecksum = 0; int i; for (i=0; i<sizeof(buffer); i++) { totalWriteBufferChecksum += buffer[i]; } alt_flash_fd* fd = alt_flash_open_dev(SERIAL_FLASH_NAME); if (NULL == fd) SendStr(NO_ANSWER, base); else { u32 sectorStartAddr = (startAddr / FLASH_SECTOR_SIZE) * FLASH_SECTOR_SIZE; if (0 == alt_write_flash(fd, sectorStartAddr, buffer, length)) { memset(buffer, 0x99, sizeof(buffer)); if (0 == alt_read_flash(fd, sectorStartAddr, buffer, sizeof(buffer))) { u32 totalReadBufferChecksum = 0; for (i=0; i<sizeof(buffer); i++) { totalReadBufferChecksum += buffer[i]; } if (totalReadBufferChecksum == totalWriteBufferChecksum) SendStr(YES_ANSWER, base); else SendStr(NO_ANSWER, base); } else SendStr(NO_ANSWER, base); } else SendStr(NO_ANSWER, base); alt_flash_close_dev(fd); } } } } } break; } default: SendStr(NO_ANSWER, base); break; } return; }