Forum Discussion
Altera_Forum
Honored Contributor
10 years agoThese are my different functions used for the serial flash :
1) Open the deviceint epcs_open(void)
{
int ret_code;
int i;
fd = alt_flash_open_dev("/dev/epcq_controller_avl_mem");
if (fd== NULL)
{
printf("** Can't open flash device **\n\n");
}else
{
printf("\n<----> Running Flash Tests <---->\n\r");
printf("-Testing flash info retrieval...");
ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);
//ret_code =alt_epcq_controller_get_info(fd, ®ions, &number_of_regions);
if (ret_code)
{
printf( "\nERROR: function alt_get_flash_info failed. ret_code %d\n",ret_code);
goto finished;
}
printf("\n\rThis is NIOSII Board Designed by flash Logic\n\r");
printf("Flash name %s\n\r",fd->name);
printf("This flash has %d erase regions\n\r", number_of_regions);
for (i=0;i<number_of_regions;i++)
{
printf("Start 0x%08x End 0x%08x Number of Blocks %3d Block Size 0x%08x\n\r",(regions+i)->offset,
(regions+i)->region_size+(regions+i)->offset,
(regions+i)->number_of_blocks,
(regions+i)->block_size);
}
}
printf("passed.\n\r");
printf("Exiting Flash Tests\n\r");
finished:
alt_flash_close_dev(fd);
return ret_code;
} 2) Write into device int Write_flash( alt_flash_fd* fd, alt_u8 *Ks_par,int test_offset)
{
int i;
int ret_code = 0;
int test_length =16;
// before writing in a sector, we should erase it before
ret_code=alt_erase_flash_block(fd, test_offset,test_length);
if(!ret_code){
for(i=0;i<test_length;i++){
printf( "data_written= 0x%08x\n\r",i,Ks_par);
}
ret_code=alt_write_flash_block(fd,test_offset,test_offset,Ks_par,test_length);
}
if (ret_code)
{
printf( "\nERROR: function alt_write_flash failed. ret_code %d\n",ret_code);
return ret_code;
}
return ret_code;
} 3) Reading from the device float Read_flash( alt_flash_fd* fd,int test_offset)
{
int test_length = 4;
alt_u8 data_read;
int i=0;
union conv2float read_flash;
float Ks_para=0.0;
int ret_code = 0;
if (!ret_code)
{
ret_code=alt_read_flash(fd, test_offset, data_read, test_length);
if(!ret_code)
{
for(i=0;i<test_length;i++){
read_flash.V_In=data_read;
printf( "Flash+%d= 0x%08x\n\r",i,read_flash.V_In);
}
Ks_para=(float)read_flash.V_Fl;
}
}
return Ks_para;
} Best regards,