Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide



Rockbox mail archive

Subject: PATCH: Configuration storage in RTC RAM
From: Heikki Hannikainen (hessu_at_hes.iki.fi)
Date: 2002-07-14


  Hi,

  Attached is a patch (against the current CVS version) which adds
configuration saving/loading in the 44 bytes of user RAM space on the
real-time clock chip of the Recorder. Tested to work on the Recorder 20.
The patch also includes the RTC RAM viewer (pressing F2 resets the config
memory) and an 'Info' menu item. I hope the project maintainers find the
code useful.

  The configuration is loaded during the boot-up. If the header is not
detected, or the checksum does not match, the configuration block is not
used and 'factory defaults' are used. The configuration block is saved on
the RTC in the init code after the old block has been interpreted and the
boot counter has been increased, and when leaving one of the configuration
menus.

  I admit not putting too much thought in the config block format, but it
should be somewhat usable until someone comes up with better ideas. The
config block format is described in rtc-conf.txt which is shown before the
patch, maybe it should be included somewhere in the source code
distribution too as README.rtc-config-format or something.

  Once FAT writing is enabled, it should be straightforward to write the
same config block on the disk of RTC-less models, unless a more extendable
and less compressed format is preferred.

  Comments, please!

  - Hessu

------------------------- rtc-conf.txt ----------

Config block as saved on the battery-packed RTC user RAM memory block
of 44 bytes, starting at offset 0x14 of the RTC memory space.

offset abs
0x00 0x14 "Roc" header signature: 0x52 0x6f 0x63
0x03 0x17 <version byte: 0x0>
0x04 0x18 <volume byte>
0x05 0x19 <balance byte>
0x06 0x1a <bass byte>
0x07 0x1b <treble byte>
0x08 0x1c <loudness byte>
0x09 0x1d <bass boost byte>
0x0a 0x1e <contrast byte>
0x0b 0x1f <backlight byte>
0x0c 0x20 <poweroff timer byte>
0x0d 0x21 <resume settings byte>
0x0e 0x22 <shuffle mode & directory filter byte>
0x0f 0x23 <scroll speed & WPS display byte>
0x10 0x24 <playlist options byte>

        <all unused space filled with 0xff>

  the geeky but useless statistics part:
0x24 <total uptime in seconds: 32 bits uint, actually unused for now>
0x28 <boots: 16 bits uint>

0x2a <checksum 2 bytes: xor of 0x0-0x29>

Config memory is reset to 0xff and initialized with 'factory defaults' if
a valid header & checksum is not found. Config version number is only
increased when information is _relocated_ or space is _reused_ so that old
versions can read and modify configuration changed by new versions. New
versions should check for the value of '0xff' in each config memory
location used, and reset the setting in question with a factory default if
needed. Memory locations not used by a given version should not be
modified unless the header & checksum test fails.

------------

  Patch follows:

diff -urN orig/firmware/debug.c firmware/debug.c
--- orig/firmware/debug.c Thu Jul 4 19:09:53 2002
+++ firmware/debug.c Sun Jul 14 21:36:15 2002
@@ -36,6 +36,8 @@
 #include "mas.h"
 #include "power.h"
 
+#include "rtc.h"
+
 void debug_init(void)
 {
     /* Clear it all! */
@@ -386,6 +388,59 @@
                 break;
         }
     }
+}
+#endif
+
+#ifdef HAVE_RTC
+/* Read RTC RAM contents and display them */
+void dbg_rtc(void)
+{
+ char buf[32];
+ unsigned char addr = 0, r, c;
+ int i;
+ int button;
+
+ lcd_clear_display();
+ lcd_puts(0, 0, "RTC read:");
+
+ while(1)
+ {
+ for (r = 0; r <= 3; r++) {
+ snprintf(buf, 10, "0x%02x: ", addr + r*4);
+ for (c = 0; c <= 3; c++) {
+ i = rtc_read(addr + r*4 + c);
+ snprintf(buf + 6 + c*2, 3, "%02x", i);
+ }
+ lcd_puts(1, r+1, buf);
+ }
+
+ lcd_update();
+ sleep(HZ/2);
+
+ button = button_get(false);
+
+ switch(button)
+ {
+ case BUTTON_DOWN:
+ if (addr < 63-16) { addr += 16; }
+ break;
+ case BUTTON_UP:
+ if (addr) { addr -= 16; }
+ break;
+ case BUTTON_F2:
+ /* clear the user RAM space */
+ for (c = 0; c <= 43; c++)
+ rtc_write(0x18 + c, 0);
+ break;
+ case BUTTON_OFF:
+ return;
+ }
+ }
+}
+#else
+void dbg_rtc(void)
+{
+ return;
 }
 #endif
 
diff -urN orig/firmware/debug.h firmware/debug.h
--- orig/firmware/debug.h Tue Jul 2 18:59:15 2002
+++ firmware/debug.h Sun Jul 14 20:42:57 2002
@@ -23,6 +23,9 @@
 extern void debugf(char* fmt,...);
 #ifndef SIMULATOR
 extern void dbg_ports(void);
+#ifdef HAVE_RTC
+extern void dbg_rtc(void);
+#endif
 #endif
 
 #ifdef __GNUC__
diff -urN orig/apps/debug_menu.c apps/debug_menu.c
--- orig/apps/debug_menu.c Thu Jan 1 02:00:00 1970
+++ apps/debug_menu.c Sun Jul 14 22:54:00 2002
@@ -0,0 +1,52 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2002 Robert Hak
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include "config.h"
+#ifndef SIMULATOR
+#include <stdio.h>
+#include <stdbool.h>
+#include "lcd.h"
+#include "menu.h"
+#include "debug_menu.h"
+#include "button.h"
+#include "kernel.h"
+#include "sprintf.h"
+#include "debug.h"
+
+void debug_menu(void)
+{
+ int m;
+
+ struct menu_items items[] = {
+#ifdef HAVE_LCD_BITMAP
+ { "Debug ports", dbg_ports },
+#ifdef HAVE_RTC
+ { "Debug RTC", dbg_rtc },
+#endif /* HAVE_RTC */
+#endif /* HAVE_LCD_BITMAP */
+ };
+
+ m=menu_init( items, sizeof items / sizeof(struct menu_items) );
+ menu_run(m);
+ menu_exit(m);
+}
+
+#endif /* SIMULATOR */
+
+
diff -urN orig/apps/debug_menu.h apps/debug_menu.h
--- orig/apps/debug_menu.h Thu Jan 1 02:00:00 1970
+++ apps/debug_menu.h Sun Jul 14 20:42:53 2002
@@ -0,0 +1,24 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2002 Robert Hak
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#ifndef _DEBUG_MENU_H
+#define _DEBUG_MENU_H
+
+void debug_menu(void);
+
+#endif
diff -urN orig/apps/main.c apps/main.c
--- orig/apps/main.c Wed Jul 3 00:40:12 2002
+++ apps/main.c Sun Jul 14 21:34:37 2002
@@ -119,7 +119,7 @@
 #endif
         panicf("ata: %d", rc);
     }
-
+
     pinfo = disk_init();
     if (!pinfo)
         panicf("disk: NULL");
@@ -128,6 +128,10 @@
     if(rc)
         panicf("mount: %d",rc);
     
+ reload_all_settings(&global_settings);
+ global_settings.total_boots++;
+ persist_all_settings();
+
     mpeg_init( global_settings.volume,
                global_settings.bass,
                global_settings.treble );
diff -urN orig/apps/main_menu.c apps/main_menu.c
--- orig/apps/main_menu.c Thu Jul 4 16:38:49 2002
+++ apps/main_menu.c Sun Jul 14 22:05:07 2002
@@ -25,7 +25,7 @@
 #include "kernel.h"
 #include "main_menu.h"
 #include "version.h"
-#include "debug.h"
+#include "debug_menu.h"
 #include "sprintf.h"
 #include <string.h>
 #include "playlist.h"
@@ -116,11 +116,25 @@
         sleep((HZ*2)/10);
 
         if (button_get(false))
- return;
+ return;
     }
     roll_credits();
 }
 
+void show_info(void)
+{
+ char s[32];
+
+ lcd_clear_display();
+ lcd_puts(0, 0, "Rockbox info:");
+ /* TODO: add disk size/usage info, battery charge etc here? */
+ snprintf(s, sizeof(s), "Booted: %d times", global_settings.total_boots);
+ lcd_puts(0, 2, s);
+ lcd_update();
+
+ button_get(true);
+}
+
 void main_menu(void)
 {
     int m;
@@ -133,9 +147,10 @@
         { "Games", games_menu },
         { "Screensavers", screensavers_menu },
 #endif
+ { "Info", show_info },
         { "Version", show_credits },
 #ifndef SIMULATOR
- { "Debug (keep out!)", dbg_ports },
+ { "Debug (keep out!)", debug_menu },
 #endif
     };
 
diff -urN orig/apps/settings.c apps/settings.c
--- orig/apps/settings.c Fri Jun 28 14:54:18 2002
+++ apps/settings.c Sun Jul 14 22:57:15 2002
@@ -8,6 +8,7 @@
  * $Id: settings.c,v 1.4 2002/06/28 11:54:18 linusnielsen Exp $
  *
  * Copyright (C) 2002 by wavey_at_wavey.org
+ * RTC config saving code (C) 2002 by hessu_at_hes.iki.fi
  *
  * All files in this archive are subject to the GNU General Public License.
  * See the file COPYING in the source tree root for full license agreement.
@@ -26,14 +27,147 @@
 #include "button.h"
 #include "lcd.h"
 #include "mpeg.h"
+#include "string.h"
 
 struct user_settings global_settings;
 
+#ifdef HAVE_RTC
+#include "rtc.h"
+static unsigned char rtc_config_block[44];
+
+/*
+ * Calculates the checksum for the config block and places it in the given 2-byte buffer
+ */
+
+void calculate_config_checksum(unsigned char *cksum)
+{
+ unsigned char *p;
+ memset(cksum, 0x0, 2);
+
+ 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;
+ }
+}
+
+/*
+ * initialize the config block buffer
+ */
+void init_config_buffer( void )
+{
+ DEBUGF( "init_config_buffer()\n" );
+
+ /* reset to 0xff - all unused */
+ memset(rtc_config_block, 0xff, sizeof(rtc_config_block));
+ /* insert header */
+ rtc_config_block[0] = 'R';
+ rtc_config_block[1] = 'o';
+ rtc_config_block[2] = 'c';
+ rtc_config_block[3] = 0x0; /* config block version number */
+}
+
 /*
- * persist all runtime user settings to disk
+ * save the config block buffer on the RTC RAM
+ */
+int save_config_buffer( void )
+{
+ unsigned char addr = 0x14;
+ int r;
+ unsigned char *p;
+
+ 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);
+
+ /* 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++) {
+ if ((r = rtc_write(addr, *p))) {
+ DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n", addr, r );
+ return r;
+ }
+ addr++;
+ }
+
+ return 0;
+}
+
+/*
+ * load the config block buffer from the RTC RAM
+ */
+int load_config_buffer( void )
+{
+ unsigned char addr = 0x14;
+ unsigned char cksum[2];
+ unsigned char *p;
+
+ DEBUGF( "load_config_buffer()\n" );
+
+ /* 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);
+ 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" );
+ 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" );
+ init_config_buffer();
+ return 1;
+}
+
+#endif /* HAVE_RTC */
+
+/*
+ * persist all runtime user settings to disk or RTC RAM
  */
 int persist_all_settings( void )
 {
+ DEBUGF( "persist_all_settings()\n" );
+
+#ifdef HAVE_RTC
+ /* update the config block buffer with current settings and save the block in the RTC */
+ rtc_config_block[0x4] = (unsigned char)global_settings.volume;
+ rtc_config_block[0x5] = (unsigned char)global_settings.balance;
+ rtc_config_block[0x6] = (unsigned char)global_settings.bass;
+ rtc_config_block[0x7] = (unsigned char)global_settings.treble;
+ rtc_config_block[0x8] = (unsigned char)global_settings.loudness;
+ rtc_config_block[0x9] = (unsigned char)global_settings.bass_boost;
+
+ rtc_config_block[0xa] = (unsigned char)global_settings.contrast;
+ rtc_config_block[0xb] = (unsigned char)global_settings.backlight;
+ rtc_config_block[0xc] = (unsigned char)global_settings.poweroff;
+ rtc_config_block[0xd] = (unsigned char)global_settings.resume;
+
+ rtc_config_block[0xe] = (
+ ((unsigned char)global_settings.playlist_shuffle & 1)
+ | (((unsigned char)global_settings.mp3filter & 1) << 1)
+ );
+
+ rtc_config_block[0xf] = ((unsigned char)global_settings.scroll_speed << 3)
+ | ((unsigned char)global_settings.wps_display & 7);
+
+ memcpy((void *)&rtc_config_block[0x24], (void *)&global_settings.total_uptime, 4);
+ memcpy((void *)&rtc_config_block[0x28], (void *)&global_settings.total_boots, 2);
+
+ save_config_buffer();
+#endif /* HAVE_RTC */
+
     return 1;
 }
 
@@ -46,17 +180,61 @@
 }
 
 /*
- * load settings from disk
+ * load settings from disk or RTC RAM
  */
 void reload_all_settings( struct user_settings *settings )
 {
+#ifdef HAVE_RTC
+ unsigned char c;
+#endif
+
     DEBUGF( "reload_all_settings()\n" );
 
- /* this is a TEMP stub version */
-
     /* populate settings with default values */
-
     reset_settings( settings );
+
+#ifdef HAVE_RTC
+ /* load the buffer from the RTC (resets it to all-unused if the block
+ is invalid) and decode the settings which are set in the block */
+ load_config_buffer();
+
+ if (rtc_config_block[0x4] != 0xFF)
+ settings->volume = rtc_config_block[0x4];
+ if (rtc_config_block[0x5] != 0xFF)
+ settings->balance = rtc_config_block[0x5];
+ if (rtc_config_block[0x6] != 0xFF)
+ settings->bass = rtc_config_block[0x6];
+ if (rtc_config_block[0x7] != 0xFF)
+ settings->treble = rtc_config_block[0x7];
+ if (rtc_config_block[0x8] != 0xFF)
+ settings->loudness = rtc_config_block[0x8];
+ if (rtc_config_block[0x9] != 0xFF)
+ settings->bass_boost = rtc_config_block[0x9];
+
+ if (rtc_config_block[0xa] != 0xFF)
+ settings->contrast = rtc_config_block[0xa];
+ if (rtc_config_block[0xb] != 0xFF)
+ settings->backlight = rtc_config_block[0xb];
+ if (rtc_config_block[0xc] != 0xFF)
+ settings->poweroff = rtc_config_block[0xc];
+ if (rtc_config_block[0xd] != 0xFF)
+ settings->resume = rtc_config_block[0xd];
+ if (rtc_config_block[0xe] != 0xFF) {
+ settings->playlist_shuffle = rtc_config_block[0xe] & 1;
+ settings->mp3filter = (rtc_config_block[0xe] >> 1) & 1;
+ }
+
+ c = rtc_config_block[0xf] >> 3;
+ if (c != 31) settings->scroll_speed = c;
+ c = rtc_config_block[0xf] & 7;
+ if (c != 7) settings->wps_display = c;
+
+ if (rtc_config_block[0x24] != 0xFF)
+ memcpy((void *)&global_settings.total_uptime, (void *)&rtc_config_block[0x24], 4);
+ if (rtc_config_block[0x28] != 0xFF)
+ memcpy((void *)&global_settings.total_boots, (void *)&rtc_config_block[0x28], 2);
+
+#endif /* HAVE_RTC */
 }
 
 /*
@@ -78,6 +256,8 @@
     settings->wps_display = DEFAULT_WPS_DISPLAY;
     settings->mp3filter = true;
     settings->playlist_shuffle = false;
+ settings->total_boots = 0;
+ settings->total_uptime = 0;
 }
 
 
diff -urN orig/apps/settings.h apps/settings.h
--- orig/apps/settings.h Fri Jun 28 14:48:53 2002
+++ apps/settings.h Sun Jul 14 22:38:55 2002
@@ -54,12 +54,15 @@
 
     int loop_playlist; /* do we return to top of playlist at end? */
     bool mp3filter;
- int scroll_speed;
+ int scroll_speed; /* long texts scrolling speed: 1-20 */
     bool playlist_shuffle;
 
     /* while playing screen settings */
- int wps_display;
-
+ int wps_display; /* 0=id3, 1=file, 2=parse */
+
+ /* geeky persistent statistics */
+ unsigned short total_boots; /* how many times the device has been booted */
+ unsigned int total_uptime; /* total uptime since rockbox was first booted */
 };
 
 /* prototypes */
diff -urN orig/apps/settings_menu.c apps/settings_menu.c
--- orig/apps/settings_menu.c Thu Jun 27 04:08:11 2002
+++ apps/settings_menu.c Sun Jul 14 20:42:52 2002
@@ -76,4 +76,5 @@
     m=menu_init( items, sizeof items / sizeof(struct menu_items) );
     menu_run(m);
     menu_exit(m);
+ persist_all_settings();
 }
diff -urN orig/apps/sound_menu.c apps/sound_menu.c
--- orig/apps/sound_menu.c Fri Jun 28 15:40:32 2002
+++ apps/sound_menu.c Sun Jul 14 20:42:52 2002
@@ -130,4 +130,5 @@
     m=menu_init( items, sizeof items / sizeof(struct menu_items) );
     menu_run(m);
     menu_exit(m);
+ persist_all_settings();
 }



Page was last modified "Jan 10 2012" The Rockbox Crew
aaa