CODE EXAMPLE 3-4 Write Action on User Flash Device (Continued)
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);
}
/* open device */
if ((ufd0 = open(uflash0, O_RDWR)) ==
exit(1);
}
/* get uflash sizes */
if (ioctl(ufd0, UIOCIBLK, &ufif0) ==
}
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);
}
}
static int
uflash_uninit() { if (ufd0)
close(ufd0);
cleanup: if (buf0)
free(buf0);
}
static int uflash_write() {
int i;
/* write some pattern to the buffers */
for (i = 0; i < ufif0.info.blk_size; i += sizeof(int)) *((int *) (buf0 + i)) = 0xDEADBEEF;
/* write block 0 of user flash */
if (pwrite(ufd0, buf0, ufif0.info.blk_size, 0) != ufif0.info.blk_size) perror("uflash0:write");
return(0);