/** * Initialize flash library and data storage in flash * We use two blocks in flash for data storage. One block is used for the * default data that will be restored. The default parameters are stored * at address CO_OD_Flash_Default_Param. The data that will be loaded at * startup or saved if user modifies data is store at CO_OD_Flash_Adress. */ void CO_FlashInit(void) { int i = 0; // // Initialize flash library // int Result = cyg_flash_init(0); CHECK_FLASH_RESULT(Result); #ifdef CYGDBG_IO_CANOPEN_DEBUG cyg_flash_set_global_printf(diag_printf); #endif // // Read info about flash device (number of blocks. block size) // Result = cyg_flash_get_info(0, &flash_info); CHECK_FLASH_RESULT(Result); CO_DBG_PRINT("Flash info dev %d: 0x%x - 0x%x, %d blocks\n", 0, flash_info.start, flash_info.end, flash_info.num_block_infos); const cyg_flash_block_info_t* block_info = flash_info.block_info; for (i = 0; i < flash_info.num_block_infos; ++i) { CO_DBG_PRINT("Block %d: block size: %d blocks: %d\n", i, block_info->block_size, block_info->blocks); block_info++; } // // Calculate addresses for flash data and default flash data // block_info = &flash_info.block_info[flash_info.num_block_infos - 1]; CO_DBG_PRINT("Last block - block size: %d blocks: %d\n", block_info->block_size, block_info->blocks); CO_OD_Flash_Adress = flash_info.end + 1 + (CYGNUM_CANOPEN_FLASH_DATA_BLOCK * block_info->block_size); CO_DBG_PRINT("CO_OD_Flash_Adress 0x%8x\n", CO_OD_Flash_Adress); CO_OD_Flash_Default_Param = CO_OD_Flash_Adress - block_info->block_size; CO_DBG_PRINT("CO_OD_Flash_Default_Param 0x%8x\n", CO_OD_Flash_Default_Param); // // Before we can access the data, we need to make sure, that the flash // block are properly initialized. We do this by reading the block into // a local sCO_OD_ROM variable and verifying the FirstWord and LastWord // members // cyg_flashaddr_t ErrorAddress; struct sCO_OD_ROM DefaultObjDicParam; Result = cyg_flash_read(CO_OD_Flash_Default_Param, &DefaultObjDicParam, sizeof(DefaultObjDicParam), &ErrorAddress); CHECK_FLASH_RESULT(Result); // // If the default parameters are not present in flash, then we know that // we need to create them for later restore // if ((DefaultObjDicParam.FirstWord != CO_OD_FIRST_LAST_WORD) ||(DefaultObjDicParam.LastWord != CO_OD_FIRST_LAST_WORD)) { storeParameters(CO_OD_Flash_Adress, OD_H1010_STORE_PARAM_ALL); storeParameters(CO_OD_Flash_Default_Param, OD_H1010_STORE_PARAM_ALL); } else { restoreParameters(CO_OD_Flash_Adress, OD_H1010_STORE_PARAM_ALL); } }
void cyg_user_start(void) { int ret; cyg_flashaddr_t flash_start=0, flash_end=0; cyg_flash_info_t info; cyg_uint32 i=0; cyg_uint32 j; int block_size=0, blocks=0; cyg_flashaddr_t prog_start; unsigned char * ptr; CYG_TEST_INIT(); cyg_flash_set_global_printf((cyg_flash_printf *)&diag_printf); ret=cyg_flash_init(NULL); CYG_TEST_PASS_FAIL((ret == CYG_FLASH_ERR_OK),"flash_init"); do { ret = cyg_flash_get_info(i, &info); if (ret == CYG_FLASH_ERR_OK) { diag_printf("INFO: Nth=%d, start=%p, end=%p\n", i, (void *) info.start, (void *) info.end); if (i == 0) { flash_start = info.start; flash_end = info.end; block_size = info.block_info[0].block_size; blocks = info.block_info[0].blocks; } for (j=0;j < info.num_block_infos; j++) { diag_printf("INFO:\t block_size %zd, blocks %u\n", info.block_info[j].block_size, info.block_info[j].blocks); } } i++; } while (ret != CYG_FLASH_ERR_INVALID); /* Erase the whole flash. Not recommended on real hardware since this will probably erase the bootloader etc!!! */ ret=cyg_flash_erase(flash_start,block_size * blocks,NULL); CYG_TEST_PASS_FAIL((ret == CYG_FLASH_ERR_OK),"flash_erase1"); /* check that its actually been erased, and test the mmap area */ for (ptr=(unsigned char *)flash_start,ret=0; ptr <= (unsigned char *)flash_end; ptr++) { if (*ptr != 0xff) { ret++; } } CYG_TEST_PASS_FAIL((ret == 0),"flash empty check"); ret = cyg_flash_program(flash_start,©right,sizeof(copyright),NULL); CYG_TEST_PASS_FAIL((ret == CYG_FLASH_ERR_OK),"flash_program1"); /* Check the contents made it into the flash */ CYG_TEST_PASS_FAIL(!strncmp((void *)flash_start, copyright,sizeof(copyright)), "flash program contents"); /* .. and check nothing else changed */ for (ptr=(unsigned char *)flash_start+sizeof(copyright),ret=0; ptr < (unsigned char *)flash_end; ptr++) { if (*ptr != 0xff) { ret++; } } CYG_TEST_PASS_FAIL((ret == 0),"flash program overrun check"); /* Program over a block boundary */ prog_start = flash_start + block_size - sizeof(copyright)/2; ret = cyg_flash_program(prog_start,©right,sizeof(copyright),NULL); CYG_TEST_PASS_FAIL((ret == CYG_FLASH_ERR_OK),"flash_program2"); /* Check the first version is still OK */ CYG_TEST_PASS_FAIL(!strncmp((void *)flash_start, copyright, sizeof(copyright)), "Original contents"); CYG_TEST_PASS_FAIL(!strncmp((void *)prog_start, copyright, sizeof(copyright)), "New program contents"); /* Check the bit in between is still erased */ for (ptr=(unsigned char *)flash_start+sizeof(copyright),ret=0; ptr < (unsigned char *)prog_start; ptr++) { if (*ptr != 0xff) { ret++; } } CYG_TEST_PASS_FAIL((ret == 0),"flash erase check1"); /* Erase the second block and make sure the first is not erased */ ret=cyg_flash_erase(flash_start+block_size,block_size,NULL); CYG_TEST_PASS_FAIL((ret == CYG_FLASH_ERR_OK),"flash_erase2"); /* Check the erase worked */ for (ptr=(unsigned char *)flash_start+block_size,ret=0; ptr < (unsigned char *)flash_start+block_size*2; ptr++) { if (*ptr != 0xff) { ret++; } } CYG_TEST_PASS_FAIL((ret == 0), "flash erase check2"); /* Lastly check the first half of the copyright message is still there */ CYG_TEST_PASS_FAIL(!strncmp((void *)prog_start, copyright, sizeof(copyright)/2), "Block 1 OK"); #if 0 /* This test is be fatal! Its not run by default! Check the flash is read only, by trying to write to it. We expect to get an exception */ *(char *)flash_start = 'a'; #endif CYG_TEST_PASS_FINISH("flash2"); }