drivers/nvram_file/nvram_file.c
author frdupin
Mon, 29 Jan 2007 09:42:22 +0100
changeset 75 debc68b124b7
parent 18 2fc8aa46980b
permissions -rw-r--r--
compiler compatibility : accessDictionaryError contains nothing out of debogue mode
18
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     1
#include <stdio.h>
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     2
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     3
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     4
/* size of NVRAM in bytes */
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     5
#define NVRAM_MAX_SIZE 262144
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     6
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     7
/* size of block in byte */
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     8
#define NVRAM_BLOCK_SIZE 256
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
     9
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    10
#define NVRAM_FILE_NAME "__nvram__"
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    11
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    12
FILE *nvram_file = NULL;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    13
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    14
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    15
void iat_flash_read_regs();
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    16
void iat_flash_write_regs();
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    17
void iat_flash_write_page(unsigned int);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    18
void iat_flash_read_page(unsigned int);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    19
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    20
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    21
short data_len; /* 0 to NVRAM_BLOCK_SIZE bytes */
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    22
short data_num_pages;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    23
unsigned int *data_page = NULL;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    24
unsigned int data_addr;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    25
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    26
unsigned int *regs_page = NULL;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    27
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    28
int iat_init()
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    29
{
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    30
	int i;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    31
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    32
	nvram_file = fopen(NVRAM_FILE_NAME, "wr");
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    33
	if (nvram_file  == NULL)
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    34
		return -1;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    35
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    36
	int n = NVRAM_BLOCK_SIZE / sizeof(unsigned int);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    37
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    38
	/* some actions to initialise the flash */
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    39
	data_len = 0;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    40
	data_num_pages = 0;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    41
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    42
	data_page = (unsigned int *)malloc(sizeof(unsigned int) * n);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    43
	memset(data_page, 0, sizeof(unsigned int)*n);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    44
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    45
	if (data_page == NULL)
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    46
		return -1;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    47
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    48
	regs_page = (unsigned int *)malloc(sizeof(unsigned int) * n);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    49
	memset(regs_page, 0, sizeof(unsigned int)*n);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    50
	if (regs_page == NULL)
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    51
		return -2;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    52
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    53
	iat_flash_read_regs();
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    54
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    55
	/* start the data at the location specified in the registers */ 
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    56
	if (0) /* for now it is 0, but put here a test to know whether
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    57
                  or not the NVRAM has been written before */
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    58
		data_addr = regs_page[1];
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    59
	else
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    60
		data_addr = NVRAM_BLOCK_SIZE; /* let start at block 1 */
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    61
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    62
	/* create a file the size of the simulated NVRAM */
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    63
	for(i=0; i<NVRAM_MAX_SIZE/NVRAM_BLOCK_SIZE + 1; i++)
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    64
		fwrite(data_page, sizeof(unsigned int), n, nvram_file);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    65
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    66
	return 0;
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    67
}
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    68
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    69
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    70
void iat_end()
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    71
{
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    72
	iat_flash_write_page(data_addr);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    73
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    74
	iat_flash_write_regs();
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    75
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    76
	fclose(nvram_file);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    77
}
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    78
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    79
 
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    80
void iat_flash_read_regs()
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    81
{
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    82
	fseek(nvram_file, 0, SEEK_SET);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    83
	fread(regs_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    84
}
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    85
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    86
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    87
void iat_flash_write_regs()
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    88
{
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    89
	fseek(nvram_file, 0, SEEK_SET);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    90
	fwrite(regs_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    91
}
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    92
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    93
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    94
void iat_flash_read_page(unsigned int addr)
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    95
{
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    96
	fseek(nvram_file, addr*sizeof(unsigned int), SEEK_SET);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    97
	fread(data_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    98
}
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
    99
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   100
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   101
void iat_flash_write_page(unsigned int addr)
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   102
{
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   103
	fseek(nvram_file, addr*sizeof(unsigned int), SEEK_SET);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   104
	fwrite(data_page, sizeof(unsigned int), NVRAM_BLOCK_SIZE, nvram_file);
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   105
}
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   106
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   107
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   108
/*------------------------------------------------------*/
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   109
int main()
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   110
{
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   111
	iat_init();
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   112
	iat_end();
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   113
}
2fc8aa46980b First version of NVRAM implemented on the file system for can_virtual
oremeq
parents: 17
diff changeset
   114
/*------------------------------------------------------*/