Răsfoiți Sursa

WIP - Working on EEprom controller

Jordy Sipkema 9 ani în urmă
părinte
comite
f5e929231d
6 a modificat fișierele cu 198 adăugiri și 36 ștergeri
  1. 75 0
      eeprom.c
  2. 59 0
      eeprom.h
  3. 13 11
      main.c
  4. 48 24
      ntp.c
  5. 1 1
      ntp.h
  6. 2 0
      typedefs.h

+ 75 - 0
eeprom.c

@@ -0,0 +1,75 @@
+//
+// Created by Jordy Sipkema on 04/03/16.
+//
+
+#include "eeprom.h"
+
+
+
+void EepromRead(int addr, void* settings, size_t size){
+
+}
+
+void EepromWrite(int addr, void* settings, size_t size){
+
+}
+
+bool EepromGetAll(TSettings *settings){
+    NutNvMemLoad(EEPROM_BASE, settings, sizeof(*settings));
+
+
+    if (settings->Checksum != sizeof(*settings)){
+        // Size mismatch: There is no valid configuration present.
+        puts("EepromGetAll(): Size mismatch");
+        return false;
+    }
+
+    return true;
+}
+
+void EepromSetAll(TSettings *settings){
+    int success = NutNvMemSave(EEPROM_BASE, settings, sizeof(*settings));
+    if (success == 0){ puts("EepromSetAll: SettingsSetAll successful."); }
+    else { puts("EepromSetAll: SettingsSetAll successful."); }
+
+    NutDelay(100);
+}
+
+void EepromWriteDefaults(void){
+    puts("EepromWriteDefaults()");
+
+    // Declare TSettings:
+    TSettings settings;
+    settings.Checksum = sizeof(settings);
+
+    settings.Cache = (const struct TCache){ 0 };
+    settings.System = (const struct TSettingsSystem){ 0 };
+
+    EepromSetAll(&settings);
+}
+
+
+//bool EepromGetSystemSettings(TSettingsSystem *SystemSettings){
+//
+//}
+//
+
+bool EepromGetCache(TCache *cache){
+    TSettings *settings;
+    if (EepromGetAll(settings) == false){
+        return false;
+    }
+
+    *cache = settings->Cache;
+    return true;
+}
+
+void EepromSetCache(TCache *cache){
+    TSettings settings;
+    settings.Checksum = sizeof(settings);
+
+    settings.System = (const struct TSettingsSystem){ 0 };
+    settings.Cache = *cache;
+
+    EepromSetAll(&settings);
+}

+ 59 - 0
eeprom.h

@@ -0,0 +1,59 @@
+/*
+ *  Copyright Jordy Sipkema, 2016.
+ *
+ *  Project             : 20152016-TI2.3a6-Internet Radio
+ *  Module              : Eeprom
+ *  File name           : Eeprom.h
+ *  Revision            : 0.1
+ *  Creation Date       : 2016/03/04
+ *
+ *  Description         : This module stores the non-volatile settings
+ *                        for the radio in the atmega128's internal eeprom
+ */
+
+#ifndef MUTLI_OS_BUILD_EEPROM_H
+#define MUTLI_OS_BUILD_EEPROM_H
+
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include "typedefs.h"
+
+#define EEPROM_BASE            0x0100 //The address where we'll start writing values.
+
+#define SETTINGS_POINTER            ((TSettings *)EEPROM_USER_BASE)
+
+/*!\brief System settings */
+typedef struct _TSettingsSystem
+{
+    u_long  lSerialnumber;         // size = 4
+} TSettingsSystem;                 // totalsize = 4
+
+/*!\brief System Cache */
+typedef struct _TCache
+{
+    tm last_sync;                  // TODO: figure out the size
+} TCache;
+
+/*!\brief Settings to write on Eeprom */
+typedef struct _TSettings
+{
+    size_t              Checksum;  // Checksum for validation TSettings-struct
+    TSettingsSystem     System;    // System settings
+    TCache              Cache;     // Cache
+} TSettings;
+
+
+extern void EepromRead(int, void*, size_t);
+extern void EepromWrite(int, void*, size_t);
+
+extern bool EepromGetAll(TSettings*);
+extern void EepromSetAll(TSettings *settings);
+
+extern bool EepromGetSystemSettings(TSettingsSystem*);
+
+extern bool EepromGetCache(TCache*);
+extern void EepromSetCache(TCache*);
+
+#endif //MUTLI_OS_BUILD_EEPROM_H

+ 13 - 11
main.c

@@ -20,31 +20,33 @@
 /*--------------------------------------------------------------------------*/
 #include <stdio.h>
 #include <string.h>
+#include <time.h>
 
 #include <sys/thread.h>
 #include <sys/timer.h>
 #include <sys/version.h>
 #include <dev/irqreg.h>
 
-#include "displayHandler.h"
-#include "system.h"
-#include "portio.h"
+// Jordy: Please keep this in alphabetical order!
 #include "display.h"
-#include "remcon.h"
+#include "displayHandler.h"
+#include "eeprom.h"
+#include "flash.h"
 #include "keyboard.h"
 #include "led.h"
 #include "log.h"
-#include "uart0driver.h"
 #include "mmc.h"
-#include "watchdog.h"
-#include "flash.h"
-#include "spidrv.h"
 #include "network.h"
+#include "ntp.h"
+#include "portio.h"
+#include "remcon.h"
+#include "rtc.h"
+#include "spidrv.h"
+#include "system.h"
+#include "uart0driver.h"
+#include "watchdog.h"
 
 
-#include <time.h>
-#include "rtc.h"
-#include "ntp.h"
 
 
 /*-------------------------------------------------------------------------*/

+ 48 - 24
ntp.c

@@ -20,15 +20,12 @@
 
 #include "log.h"
 #include "ntp.h"
+#include "eeprom.h"
+#include "typedefs.h"
 
 #define TIME_ZONE 1
 #define LOG_MODULE  LOG_NTP_MODULE
 
-typedef struct _Eeprom_tm {
-    size_t len;
-    tm tm_struct;
-} Eeprom_tm;
-
 bool isSyncing;
 bool validTime = false;
 time_t ntp_time = 0;
@@ -47,32 +44,54 @@ bool NtpIsSyncing(void){
 }
 
 void NtpCheckValidTime(void){
+    TCache *cache;
 
-    Eeprom_tm eeprom_tm_struct;
-
-    NutNvMemLoad(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
-
-    if (eeprom_tm_struct.len != sizeof(eeprom_tm_struct)){
-        // Size mismatch: There is no valid configuration present.
-        puts("NtpCheckValidTime(): Size mismatch");
+    if (EepromGetCache(cache) == false){
+        puts("NtpCheckValidTime(): No cache available");
         validTime = false;
         return;
     }
 
-    // Valid configuration available.
-    puts("NtpCheckValidTime(): Valid config available");
-    tm stored_tm = eeprom_tm_struct.tm_struct;
+    // Cache is present
+    puts("NtpCheckValidTime(): Cache is available");
 
-    // Check time is valid;
+    // Check if time is valid;
     tm current_tm;
     X12RtcGetClock(&current_tm);
 
-    validTime = NtpCompareTime(current_tm, stored_tm);
+    validTime = NtpCompareTime(current_tm, cache->last_sync);
+
     if (validTime){
         puts("NtpCheckValidTime(): Time was valid");
     }else {
         puts("NtpCheckValidTime(): Invalid time!");
     }
+
+//    Eeprom_tm eeprom_tm_struct;
+//
+//    NutNvMemLoad(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
+//
+//    if (eeprom_tm_struct.len != sizeof(eeprom_tm_struct)){
+//        // Size mismatch: There is no valid configuration present.
+//        puts("NtpCheckValidTime(): Size mismatch");
+//        validTime = false;
+//        return;
+//    }
+//
+//    // Valid configuration available.
+//    puts("NtpCheckValidTime(): Valid config available");
+//    tm stored_tm = eeprom_tm_struct.tm_struct;
+//
+//    // Check time is valid;
+//    tm current_tm;
+//    X12RtcGetClock(&current_tm);
+//
+//    validTime = NtpCompareTime(current_tm, stored_tm);
+//    if (validTime){
+//        puts("NtpCheckValidTime(): Time was valid");
+//    }else {
+//        puts("NtpCheckValidTime(): Invalid time!");
+//    }
 }
 
 //Tests if t1 is after t2.
@@ -145,15 +164,20 @@ void NtpSync(void){
 }
 
 void NtpWriteTimeToEeprom(tm time_struct){
-    Eeprom_tm eeprom_tm_struct;
-
-    eeprom_tm_struct.len = sizeof(eeprom_tm_struct);
-    eeprom_tm_struct.tm_struct = time_struct;
+    TCache cache;
 
-    int success = NutNvMemSave(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
-    if (success == 0){ puts("NtpWriteTimeToEeprom: Time succesfully written to eeprom"); }
+    cache.last_sync = time_struct;
+    EepromSetCache(&cache);
 
-    NutDelay(100);
+//    Eeprom_tm eeprom_tm_struct;
+//
+//    eeprom_tm_struct.len = sizeof(eeprom_tm_struct);
+//    eeprom_tm_struct.tm_struct = time_struct;
+//
+//    int success = NutNvMemSave(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
+//    if (success == 0){ puts("NtpWriteTimeToEeprom: Time succesfully written to eeprom"); }
+//
+//    NutDelay(100);
 }
 
 //unsigned long TmStructToEpoch(tm tm_struct){

+ 1 - 1
ntp.h

@@ -5,7 +5,7 @@
 #ifndef _Ntp_H
 #define _Ntp_H
 
-typedef enum {false, true} bool;
+#include "typedefs.h"
 
 extern bool NtpIsSyncing(void);
 extern void NtpInit(void);

+ 2 - 0
typedefs.h

@@ -25,6 +25,8 @@
 /*--------------------------------------------------------------------------*/
 /*  Type declarations                                                       */
 /*--------------------------------------------------------------------------*/
+typedef enum {false, true} bool;
+
 /* RL: this 'table' has now a mirror table in 'Display.c' (LcdErrorStrings) */
 /*     Make sure that any modification made to this table are reflected by  */
 /*     the LcdErrorStirngs table! (an error is bad but showing the wrong    */