Chapter 2
Flash Device Driver
13
Example Write Program
contains the Write Action on the flash device.
CODE
EXAMPLE
2-4
Write Action on Flash Device
/*
* uflash_write.c
* An example that shows how to write flash
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <uflash_if.h>
char *uflash0 = "/dev/uflash0";
int ufd0;
uflash_if_t ufif0;
char *buf0;
char *module;
static int
uflash_init() {
char *buf0 = malloc(ufif0.info.blk_size);
if (!buf0) {
printf("%s: cannot allocate memory\n", module);
return(-1);
}
/* open device */
if ((ufd0 = open(uflash0, O_RDWR)) == -1 ) {
perror("uflash0: ");
exit(1);
}
/* get uflash sizes */
if (ioctl(ufd0, UIOCIBLK, &ufif0) == -1 ) {
perror("ioctl(ufd0, UIOCIBLK): ");
exit(1);
}
if (ufd0) {
printf("%s: \n", uflash0);
printf("manfacturer id = 0x%p\n", ufif0.info.mfr_id);
printf("device id = 0x%p\n", ufif0.info.dev_id);
printf("number of blocks = 0x%p", ufif0.info.blk_num);
printf("block size = 0x%p" ufif0.info.blk_size);
}
}