3 #include <generated/mem.h>
7 #define FLASH_BLOCK_SIZE (128*1024)
8 #define FLASH_OFFSET_CONFIG (FLASH_BOOT_ADDRESS + FLASH_BLOCK_SIZE)
10 static volatile unsigned short *flash_config
= (unsigned short *)(0x80000000 | FLASH_OFFSET_CONFIG
);
12 static void wait_program(void)
14 while(!(*flash_config
& 0x0080)); /* Read status register */
15 *flash_config
= 0x0050; /* Clear status register */
16 *flash_config
= 0x00ff; /* Go to Read Array mode */
19 static void config_erase_block(void)
21 *flash_config
= 0x0020; /* Setup Erase */
22 *flash_config
= 0x00d0; /* Confirm Erase */
26 static void config_write(int offset
, unsigned short data
)
28 flash_config
[offset
] = 0x0040; /* Word Program */
29 flash_config
[offset
] = data
;
33 static const unsigned char config_defaults
[CONFIG_KEY_COUNT
] = CONFIG_DEFAULTS
;
34 static int config_record_count
;
35 static unsigned char config_values
[CONFIG_KEY_COUNT
];
37 static int config_process_record(unsigned char key
, unsigned char value
)
39 if(key
>= CONFIG_KEY_COUNT
)
41 config_record_count
++;
42 config_values
[key
] = value
;
46 void config_init(void)
48 volatile unsigned int *flash_config32
= (unsigned int *)flash_config
;
50 unsigned int flash_word
;
52 memcpy(config_values
, config_defaults
, CONFIG_KEY_COUNT
);
54 for(i
=0;i
<FLASH_BLOCK_SIZE
/4;i
++) {
55 flash_word
= flash_config32
[i
];
56 if(!config_process_record((flash_word
>> 24) & 0xff, (flash_word
>> 16) & 0xff))
58 if(!config_process_record((flash_word
>> 8) & 0xff, flash_word
& 0xff))
63 void config_write_all(void)
68 config_record_count
= 0;
69 for(i
=0;i
<CONFIG_KEY_COUNT
;i
++) {
70 if(config_values
[i
] != config_defaults
[i
]) {
71 config_write(config_record_count
, (i
<< 8) | config_values
[i
]);
72 config_record_count
++;
77 unsigned char config_get(unsigned char key
)
79 return config_values
[key
];
82 void config_set(unsigned char key
, unsigned char value
)
84 if(config_values
[key
] == value
)
86 config_values
[key
] = value
;
87 if(config_record_count
< FLASH_BLOCK_SIZE
/2)
88 config_write(config_record_count
++, (key
<< 8) | value
);