Archive

Posts Tagged ‘Flash’

Using Flash and structs on the Altera DE2 board

February 24th, 2011 No comments

For my DMX project on the Altera DE2 board I need to load devices and chasers from the DE2’s Flash memory. I found out that information about this is very hard to find, especially in combination with NIOSII. The, by Altera provided testcode for the DE2 shows a simple routine which reads and writes integer data from and to the Flash memory. This code is listed below.

alt_u32 test_flash( void ) {
	alt_u32 i, errors = 0;
	alt_u32 in_buff[FLASH_MAX_WORDS], out_buff[FLASH_MAX_WORDS];
	alt_flash_fd* flash_handle;

	flash_handle = alt_flash_open_dev( FLASH_NAME );

	/* Create data buffer to write to Flash memory */
	for( i = 0; i < FLASH_MAX_WORDS; i++ ) {
		in_buff[i] = i + 1000000;
	}

	/* Write data to Flash memory */
	alt_write_flash( flash_handle, 0, in_buff, FLASH_MAX_WORDS*4 );

	/* Read data from Flash memory */
	alt_read_flash( flash_handle, 0, out_buff, FLASH_MAX_WORDS*4 );

	/* Check output from Flash memory */
	for( i = 0; i < FLASH_MAX_WORDS; i++ ) {
		if( out_buff[i] != (i+1000000) )
			errors++;
	}
	alt_flash_close_dev( flash_handle );
	return( errors );
}

This simple test routine generates data which will be written to the Flash memory. After writing the routine reads back the data and checks it for errors. Unfortunately, this routine doesn’t show how to deal with structs. For my project I keep track of devices and chasers by storing them into an array of Device and Chaser structs. After some toying around with the built in HAL functions I found out that writing structs to the Flash memory is pretty easy as well. I will show you by a simple example.

First of all we need to define our struct. Note that the alt_u16 and alt_u8 are integer types provided by the HAL.

typedef struct Device {
	char name[MAX_NAME_SIZE];
	alt_u16 unique_id;
	alt_u16 startchannel;
	alt_u8 channels;
} Device;

Now we need to be able to communicate with the Flash memory. Therefore we need to open the device using the built in functions defined in sys/alt_flash.h.

alt_flash_fd* flash_handle;
flash_handle = alt_flash_open_dev( FLASH_NAME );

As you can see in the code above the flash_hanle variable is of the type alt_flash_fd. This is a built in struct and should be used to refer to the flash chip. My SOCP flash object is called flash. To be able to load the corresponding device use FLASH_NAME.

With the device opened we can now communicate with it. With our previously defined struct we create an instance of Device:

Device flash_device;
strcpy(flash_device.name, "FLASH DEVICE");
flash_device.startchannel = 151;
flash_device.channels = 3;

Writing it to Flash is not that hard:

/* Write data to Flash memory */
alt_write_flash( flash_handle, 0, &flash_device, sizeof(flash_device) );

The first argument is the opened flash device handler. The second is the data offset (where to start writing in memory). As third argument we enter the start address of the previously instantiated Device object. The fourth and last argument is the length of data to be written. We enter the size of our Device object instance here.

Of course we want to be able to read back data from Flash as well. The HAL also provides us with a function for that. We need to create a new object to load our data into. I chose to create the object data_out. After reading the data we can print some information in the struct to standard out.

// Output object
Device data_out;

/* Read data from Flash memory */
alt_read_flash( flash_handle, 0, &data_out, sizeof(flash_device) );

printf("Name: %s\n", data_out.name);
printf("Startchannel: %i\n", data_out.startchannel);

The function alt_read_flash takes care of reading the data from the Flash chip. As with the alt_write_flash function the first argument of this function is the Flash device handler. The second is again the data offset. The third argument is the start address of the data object to be filled. We enter the same read length as with the alt_write_flash call. I’m not sure yet about detecting the end of a struct. I’ll maybe update this post with that information later.

The only thing we officially need to do after using the Flash device is closing it.The first and only argument to this function is the Flash device handler.

alt_flash_close_dev( flash_handle );

Well, thats probably it for now. When I find out more useful information about the Flash device I’ll post it here.

Post to Twitter

Categories: Electronica, Technologie Tags: , , , , ,