/****************************************************************** * Function: FlashRunTests * * Purpose: Performs a full-test on the Flash specified. The tests * run are: * - alt_write_flash * - alt_read_flash * - alt_erase_flash_block * - alt_write_flash_block * ******************************************************************/ static void FlashRunTests(alt_flash_fd* fd, int block, flash_region* regions) { int ret_code = 0x0; int error = 0x0; int test_offset; /* Calculate the offset of the block */ test_offset = (regions->offset + (block * regions->block_size)); /* Test reading and writing functions */ ret_code = FlashTestReadWrite(block, &error, fd, regions); /* Test the erase function */ if (!ret_code) { ret_code = FlashTestBlockErase(block, &error, fd, regions); } /* Test the block write function */ if (!ret_code) { ret_code = FlashTestBlockWrite(block, &error, fd, regions); } /* Erase the block so we dont fill one up each time we run the test */ printf(" -Returning block %d to its erased state.\n", block); alt_erase_flash_block(fd, test_offset, regions->block_size); printf(" -Flash tests complete.\n"); if(ret_code || error) { printf(" -At least one test failed.\n\n"); } }
/****************************************************************** * Function: FlashTestBlockErase * * Purpose: Tests that the function alt_erase_flash_block is * is working properly. Assumes that the specified * flash block contains some non-0xFFFFFFFF data before * this function is called. * ******************************************************************/ static int FlashTestBlockErase(int block, int *error, alt_flash_fd* fd, flash_region* regions) { int ret_code = 0x0; int test_offset; /* Calculate the offset of the block */ test_offset = (regions->offset + (block * regions->block_size)); printf(" -Testing \"alt_erase_flash_block\"."); ret_code = alt_erase_flash_block(fd, test_offset, regions->block_size); /* Check that the erase was successful. */ if (!ret_code) { if(FlashCheckIfBlockErased(fd->base_addr, block, regions)) printf(" passed.\n"); else { printf(" FAILED\n"); *error++; } } return ret_code; }
bool Flash_Erase(FLASH_HANDLE Handle, alt_u16 block_index){ FLASH_INFO *pFlash = (FLASH_INFO *)Handle; if (!pFlash->fd_flash) return FALSE; alt_u32 offset; alt_u32 length; int error_code; bool bSuccess = FALSE; if (Flash_GetBlockInfo(Handle, block_index, &offset, &length)){ error_code = alt_erase_flash_block(pFlash->fd_flash, offset, length); //DEBUG_FLASH("Erase block[%d], offset=%Xh, lenght=%Xh", block_no, offset, length); if (error_code == 0) bSuccess = TRUE; } return bSuccess; }
/****************************************************************** * Function: ProgFlash * * Purpose: This function asks for the name of a flash device, * a pointer to a data buffer, and the size of the data buffer. * ******************************************************************/ int ProgFlash(struct flash_inf_struct *flash_info, int target_addr, char* data, int data_len) { // Flash device variables alt_flash_fd* fd; int number_of_regions; flash_region* regions; alt_u8 flashname[40]; /* Be conservative on the size of the string. */ alt_u8* flashname_ptr = (alt_u8*) &flashname; int new_flash_block = -1; // General purpose variables unsigned int sw_offset = 0; int ret_code = 0x0; /* * flash_info->device contains the flash device name * from the multipart form. * - If you want your flash name to be an option, you must change the * upload_image form in index.html to include your flash device's name * in the pick list. */ flashname_ptr += sprintf( flashname_ptr, "/dev/%s", flash_info->device ); *(flashname_ptr+1) = '\0'; fd = alt_flash_open_dev(flashname); if (fd) { /* Get some useful info about the flash */ ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions); /* To which flash block is the SREC data destined? */ /* new_flash_block = target_addr / regions->block_size; */ /* Ahhh, but what happens if a line spans the end of one block and the * beginning of another? * - Better to handle this case well...as well! */ new_flash_block = (target_addr + data_len) / regions->block_size; if( current_flash_block == -1 ) { /* Output various flash information when programming the first line. */ printf( "\nFlash Name is %s.\nBlock size is %d bytes.\n\nProgramming Flash...\n", flashname, regions->block_size ); } /* if it's a new block, we need to erase it first. */ if(new_flash_block != current_flash_block) { printf("\nFlash Block %d", new_flash_block); /* Blindly erase the new flash block */ alt_erase_flash_block(fd, (new_flash_block * regions->block_size), regions->block_size); current_flash_block = new_flash_block; } alt_write_flash_block(fd, (current_flash_block * regions->block_size), target_addr, data, data_len); /* This just gives us some zippy dots so we know hard work is being done */ if ((target_addr - sw_offset) % (regions->block_size / 8) < data_len) { printf("\n 0x%8.8X: ", (target_addr & 0xFFFFFF00)); } /*if ((target_addr - sw_offset) % (regions->block_size / 256) < data_len) { printf("."); }*/ } else { printf("Error Opening flash device. Exiting."); } alt_flash_close_dev(fd); return (ret_code); }
/****************************************************************** * 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: FlashErase * * Purpose: Erases 1 or all blocks in the specified flash device. * ******************************************************************/ static void FlashErase(void) { alt_flash_fd* fd; int test_offset; int ret_code; flash_region* regions; int number_of_regions; alt_u8 entry[3]; alt_u8 flashname[30]; unsigned int block; /* Get the name of the flash we are erasing */ ret_code = GetFlashName(flashname); fd = alt_flash_open_dev(flashname); if (fd) { /* Find out some useful stuff about the flash */ ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions); if (!ret_code) { printf(" -Region has %d blocks.\n", regions->number_of_blocks); printf(" -Which block would you like to erase?\n"); printf(" -> "); fgets(entry, sizeof(entry), stdin); if(entry[0] == 'a') { printf(" -Erase ALL blocks? (y/n) "); fgets(entry, sizeof(entry), stdin); if(entry[0] == 'y') { /* Erase all blocks */ printf(" -Erasing %d blocks. Please Wait.\n", (regions->number_of_blocks)); for(block = 0; block < regions->number_of_blocks; block++) { /* Dont erase it if it's already erased silly. */ if ((FlashCheckIfBlockErased(fd->base_addr, block, regions)) == 0) { test_offset = (regions->offset + (block * regions->block_size)); alt_erase_flash_block(fd, test_offset, regions->block_size); } /* Just a simple progress meter so we dont get bored waiting for the flash to erase. */ printf("."); if(((block + 1) % 80) == 0) { printf("\n"); } } printf("\n -All Blocks Erased.\n"); } else { printf("Erased zero blocks.\n"); } } /* Just erase one block */ if(sscanf(entry, "%d\n", &block)) { if ((block >= 0) && (block <= (regions->number_of_blocks - 1))) { test_offset = (regions->offset + (block * regions->block_size)); alt_erase_flash_block(fd, test_offset, regions->block_size); printf(" -Block %d erased.\n", block); } else { printf(" -Block number entered is %d\n", block); printf(" -Block number must be between 0 and %d.\n", (regions->number_of_blocks - 1)); } } } printf(" -Closing flash \"%s\".\n", flashname); alt_flash_close_dev(fd); } }