summaryrefslogtreecommitdiff
path: root/apps/settings.c
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-08-13 17:16:09 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-08-13 17:16:09 +0000
commitcd7691de2261e84d3ee72ec3d585ab66680d04e2 (patch)
treeb9aea6c27b4f68342a9ec87d77fddb682bff5385 /apps/settings.c
parent9872b66cd100dce18e2e6f4f77ee2beaa50b8075 (diff)
downloadrockbox-cd7691de2261e84d3ee72ec3d585ab66680d04e2.zip
rockbox-cd7691de2261e84d3ee72ec3d585ab66680d04e2.tar.gz
rockbox-cd7691de2261e84d3ee72ec3d585ab66680d04e2.tar.bz2
rockbox-cd7691de2261e84d3ee72ec3d585ab66680d04e2.tar.xz
Enabled saving settings to disk on player
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@1717 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/settings.c')
-rw-r--r--apps/settings.c181
1 files changed, 75 insertions, 106 deletions
diff --git a/apps/settings.c b/apps/settings.c
index e285d26..7ca5715 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -30,12 +30,18 @@
#include "mpeg.h"
#include "string.h"
#include "ata.h"
+#include "fat.h"
#include "power.h"
#include "backlight.h"
#include "powermgmt.h"
struct user_settings global_settings;
+static unsigned short last_checksum = 0;
+
+#define CONFIG_BLOCK_VERSION 0
+#define CONFIG_BLOCK_SIZE 44
+
/********************************************
Config block as saved on the battery-packed RTC user RAM memory block
@@ -78,25 +84,24 @@ modified unless the header & checksum test fails.
*************************************/
#include "rtc.h"
-static unsigned char rtc_config_block[44];
+static unsigned char rtc_config_block[CONFIG_BLOCK_SIZE];
/*
* Calculates the checksum for the config block and places it in the given 2-byte buffer
*/
-static void calculate_config_checksum(unsigned char *cksum)
+static unsigned short calculate_config_checksum(void)
{
- unsigned char *p;
+ unsigned int i;
+ unsigned char cksum[2];
cksum[0] = cksum[1] = 0;
- for (p = rtc_config_block;
- p < rtc_config_block + sizeof(rtc_config_block) - 2;
- p++)
- {
- cksum[0] = cksum[0] ^ *p;
- p++;
- cksum[1] = cksum[1] ^ *p;
+ for (i=0; i < CONFIG_BLOCK_SIZE - 2; i+=2 ) {
+ cksum[0] ^= rtc_config_block[i];
+ cksum[1] ^= rtc_config_block[i+1];
}
+
+ return (cksum[0] << 8) | cksum[1];
}
/*
@@ -107,141 +112,107 @@ static void init_config_buffer( void )
DEBUGF( "init_config_buffer()\n" );
/* reset to 0xff - all unused */
- memset(rtc_config_block, 0xff, sizeof(rtc_config_block));
+ memset(rtc_config_block, 0xff, CONFIG_BLOCK_SIZE);
/* insert header */
rtc_config_block[0] = 'R';
rtc_config_block[1] = 'o';
rtc_config_block[2] = 'c';
- rtc_config_block[3] = 0x1; /* config block version number */
+ rtc_config_block[3] = CONFIG_BLOCK_VERSION;
}
-#ifdef HAVE_RTC
/*
- * save the config block buffer on the RTC RAM
+ * save the config block buffer to disk or RTC RAM
*/
static int save_config_buffer( void )
{
- unsigned char addr = 0x14;
- int r;
- unsigned char *p;
-
+ unsigned short chksum;
+
+#ifdef HAVE_RTC
+ unsigned int i;
+ int addr=0x14;
+#endif
+
DEBUGF( "save_config_buffer()\n" );
/* update the checksum in the end of the block before saving */
- calculate_config_checksum(rtc_config_block + sizeof(rtc_config_block) - 2);
-
+ chksum = calculate_config_checksum();
+ rtc_config_block[ CONFIG_BLOCK_SIZE - 2 ] = chksum >> 8;
+ rtc_config_block[ CONFIG_BLOCK_SIZE - 1 ] = chksum & 0xff;
+
+ /* don't save if no changes were made */
+ if ( chksum == last_checksum )
+ return 0;
+
+#ifdef HAVE_RTC
/* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
that it would write a number of bytes at a time since the RTC chip
supports that, but this will have to do for now 8-) */
- for (p = rtc_config_block;
- p < rtc_config_block + sizeof(rtc_config_block);
- p++)
- {
- r = rtc_write(addr, *p);
+ for (i=0; i < CONFIG_BLOCK_SIZE; i++ ) {
+ int r = rtc_write(14, rtc_config_block[i]);
if (r) {
- DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n", addr, r );
+ DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n", 14, r );
return r;
}
addr++;
}
-
+
+#else
+
+ if(battery_level_safe() && (fat_startsector()!=0))
+ return !ata_write_sectors( 61, 1, rtc_config_block);
+ else
+ return -1;
+
+#endif
+
return 0;
}
/*
- * load the config block buffer from the RTC RAM
+ * load the config block buffer from disk or RTC RAM
*/
static int load_config_buffer( void )
{
+ unsigned short chksum;
+
+#ifdef HAVE_RTC
unsigned char addr = 0x14;
- unsigned char cksum[2];
- unsigned char *p;
+ unsigned int i;
+#endif
DEBUGF( "load_config_buffer()\n" );
-
+
+#ifdef HAVE_RTC
/* FIXME: the same comment applies here as for rtc_write */
- for (p = rtc_config_block;
- p < rtc_config_block + sizeof(rtc_config_block);
- p++)
- {
- *p = rtc_read(addr);
+ for (i=0; i < CONFIG_BLOCK_SIZE; i++ ) {
+ rtc_config_block[i] = rtc_read(addr);
addr++;
}
-
- /* calculate the checksum, check it and the header */
- calculate_config_checksum(cksum);
-
- if (rtc_config_block[0x0] == 'R'
- && rtc_config_block[0x1] == 'o'
- && rtc_config_block[0x2] == 'c'
- && rtc_config_block[0x3] == 0x0
- && cksum[0] == rtc_config_block[0x2a]
- && cksum[1] == rtc_config_block[0x2b]) {
- DEBUGF( "load_config_buffer: header & checksum test ok\n" );
- return 0; /* header and checksum is valid */
- }
-
- /* if checksum is not valid, initialize the config buffer to all-unused */
- DEBUGF( "load_config_buffer: header & checksum test failed\n" );
- init_config_buffer();
- return 1;
-}
-
-#else /* HAVE_RTC */
-/*
- * save the config block buffer on the 61 Sector
- */
-static int save_config_buffer( void )
-{
- DEBUGF( "save_config_buffer()\n" );
-
- /* update the checksum in the end of the block before saving */
- calculate_config_checksum(rtc_config_block + sizeof(rtc_config_block) - 2);
-#ifdef SAVE_TO_DISK
- if(battery_level_safe() && (fat_firstsector()!=0))
- return !ata_write_sectors( 61, 1, rtc_config_block);
- else
- return -1;
#else
- return 0;
+ ata_read_sectors( 61, 1, rtc_config_block);
#endif
-}
-
-/*
- * load the config block buffer from disk
- */
-static int load_config_buffer( void )
-{
-#ifdef SAVE_TO_DISK
- unsigned char cksum[2];
-
- DEBUGF( "load_config_buffer()\n" );
- ata_read_sectors( 61, 1, rtc_config_block);
-
/* calculate the checksum, check it and the header */
- calculate_config_checksum(cksum);
+ chksum = calculate_config_checksum();
+
+ if (rtc_config_block[0] == 'R' &&
+ rtc_config_block[1] == 'o' &&
+ rtc_config_block[2] == 'c' &&
+ rtc_config_block[3] == CONFIG_BLOCK_VERSION &&
+ (chksum >> 8) == rtc_config_block[CONFIG_BLOCK_SIZE - 2] &&
+ (chksum & 0xff) == rtc_config_block[CONFIG_BLOCK_SIZE - 1])
+ {
+ DEBUGF( "load_config_buffer: header & checksum test ok\n" );
+ last_checksum = chksum;
+ return 0; /* header and checksum is valid */
+ }
- if (rtc_config_block[0x0] == 'R'
- && rtc_config_block[0x1] == 'o'
- && rtc_config_block[0x2] == 'c'
- && rtc_config_block[0x3] == 0x0
- && cksum[0] == rtc_config_block[0x2a]
- && cksum[1] == rtc_config_block[0x2b]) {
- DEBUGF( "load_config_buffer: header & checksum test ok\n" );
- return 0; /* header and checksum is valid */
- }
-#endif
/* if checksum is not valid, initialize the config buffer to all-unused */
DEBUGF( "load_config_buffer: header & checksum test failed\n" );
init_config_buffer();
return 1;
}
-
-#endif /* HAVE_RTC */
-
-
/*
* persist all runtime user settings to disk or RTC RAM
*/
@@ -276,8 +247,6 @@ int settings_save( void )
rtc_config_block[0x11] = (unsigned char)global_settings.avc;
- rtc_config_block[0x12] = (unsigned char)global_settings.contrast;
-
memcpy(&rtc_config_block[0x24], &global_settings.total_uptime, 4);
if(save_config_buffer())
@@ -325,8 +294,11 @@ void settings_load(void)
if (rtc_config_block[0x9] != 0xFF)
global_settings.bass_boost = rtc_config_block[0x9];
- if (rtc_config_block[0xa] != 0xFF)
+ if (rtc_config_block[0xa] != 0xFF) {
global_settings.contrast = rtc_config_block[0xa];
+ if ( global_settings.contrast < MIN_CONTRAST_SETTING )
+ global_settings.contrast < DEFAULT_CONTRAST_SETTING;
+ }
if (rtc_config_block[0xb] != 0xFF)
global_settings.backlight = rtc_config_block[0xb];
if (rtc_config_block[0xc] != 0xFF)
@@ -352,9 +324,6 @@ void settings_load(void)
if (rtc_config_block[0x11] != 0xFF)
global_settings.avc = rtc_config_block[0x11];
- if (rtc_config_block[0x12] != 0xff)
- global_settings.contrast = rtc_config_block[0x12];
-
if (rtc_config_block[0x24] != 0xFF)
memcpy(&global_settings.total_uptime, &rtc_config_block[0x24], 4);
}