Pārlūkot izejas kodu

Merge pull request #27 from jancoow/eeprom-controller

Eeprom controller
Jordy Sipkema 9 gadi atpakaļ
vecāks
revīzija
6a3af6f907
12 mainītis faili ar 245 papildinājumiem un 153 dzēšanām
  1. 8 28
      alarm.c
  2. 16 20
      alarm.h
  3. 3 2
      displayHandler.c
  4. 75 0
      eeprom.c
  5. 67 0
      eeprom.h
  6. 3 3
      httpstream.c
  7. 1 1
      httpstream.h
  8. 40 60
      main.c
  9. 1 1
      network.c
  10. 28 37
      ntp.c
  11. 1 1
      ntp.h
  12. 2 0
      typedefs.h

+ 8 - 28
alarm.c

@@ -5,9 +5,11 @@
 #include <time.h>
 #include <assert.h>
 
+#include "alarm.h"
 #include "log.h"
+#include "ntp.h"
+#include "typedefs.h"
 #include "rtc.h"
-#include "alarm.h"
 
 #define n 2
 
@@ -64,18 +66,18 @@ void setState(int idx){
 	struct _tm ct;
 	X12RtcGetClock(&ct);
 	
-	if (compareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != 0){
+	if (NtpCompareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != false){
 		alarm[idx].state = 1;
 	} else {
 		alarm[idx].state = 0;
 	}
 	
-	/*if (compareTime(alarm[idx].time,snooze[idx].snoozeStart)){
+	/*if (NtpCompareTime(alarm[idx].time,snooze[idx].snoozeStart)){
 		alarm[idx].state = 2;
 	}
 	
 	if (alarm[idx].state == 2){
-		if (compareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
+		if (NtpCompareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
 			snooze[idx].snoozeStart.tm_min += alarm[idx].snooze + 1;
 			snooze[idx].snoozeEnd.tm_min += alarm[idx].snooze + 1;
 		}
@@ -89,11 +91,11 @@ void setState(int idx){
 	}
 }*/
 
-void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
+void setAlarm(struct _tm time, char* name, u_long ip, u_short port, char* url, char snooze, int id, int idx){
 	alarm[idx].time = time;
 	
 	strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
-	strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
+	alarm[idx].ip = ip;
 	alarm[idx].port = port;
 	strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
 
@@ -123,26 +125,4 @@ void handleAlarm(int idx){
 	alarm[idx].state = 0;
 }
 
-int compareTime(tm t1,tm t2){
-    if (t1.tm_year > t2.tm_year){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
-        return 1;
-	}
-    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
-        return 1;
-	}
-
-    return 0;
-}
 

+ 16 - 20
alarm.h

@@ -1,30 +1,26 @@
-/* Alarm get/set status values */
-#define ALARM_1 	5
-#define ALARM_2		6
-
-#define AFLGS		0b11111111
 
 #ifndef _ALARM_DEFINED
-struct _alarm
+typedef struct _alarm
 {
-	struct _tm time;
-	char ip[24];
-	u_short port;
-	char url[24];
-	char name[16];
-	int snooze;
-	int id;
-	int state;
-};
-#define _ALARM_DEFINED
-#endif
+    struct _tm time;   // Alarm time                          ?
+    u_long ip;         // IP of the stream to be played       4
+	u_short port;      // Port of the stream to be played     2
+	char url[24];      // Get url                            24
+	char name[16];     // Name of the alarm                  16
+	char snooze;       // Snooze time in minutes              1
+	int id;            // Database identifier                 2
+	signed char state; // Enum: [-1 ... 2]                    1
+} AAlarm;              // Total size:                        50 + ?
+
 
 void handleAlarm(int idx);
 int checkAlarms(void);
-void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx);
+void setAlarm(struct _tm time, char* name, u_long ip, u_short port, char* url, char snooze, int id, int idx); // TODO: Linked list?
 int alarmExist(int id);
 void deleteAlarm(int idx);
-int compareTime(tm t1, tm t2);
 void setState(int idx);
 int getState(int idx);
-struct _alarm getAlarm(int idx);
+struct _alarm getAlarm(int idx);
+
+#define _ALARM_DEFINED
+#endif

+ 3 - 2
displayHandler.c

@@ -85,7 +85,7 @@ void displayVolume(int pos)
 {
     ClearLcd();
     int i;
-    LcdArrayLineOne("     Volume     ", 16);
+    (*write_display_ptr[0])("     Volume     ", 16);
 
     char characters[17];
 
@@ -93,7 +93,8 @@ void displayVolume(int pos)
     {
         characters[i] = 0xFF;
     }
-        LcdArrayLineTwo(characters,pos);
+
+    (*write_display_ptr[1])(characters, pos);
 }
 
 

+ 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);
+}

+ 67 - 0
eeprom.h

@@ -0,0 +1,67 @@
+/*
+ *  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;
+
+typedef struct _TAlarm // Max 5
+{
+    tm alarm_time;
+    char name[16];
+    char snooze_time;                 // size = ?, desc: snooze_time in minutes.
+} TAlarm;
+
+/*!\brief Settings to write on Eeprom */
+typedef struct _TSettings
+{
+    size_t              Checksum;      // Checksum for validation TSettings-struct
+    TSettingsSystem     System;        // System settings
+    TAlarm              Alarm_general; // Alarms 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

+ 3 - 3
httpstream.c

@@ -48,10 +48,10 @@ THREAD(Stream, args)
     NutThreadExit();
 }
 
-void playStream(char *ipaddr, u_short port, char *radiourl){
+void playStream(u_long ipaddr, u_short port, char *radiourl){
     if(isStreaming != true){
         isStreaming = true;
-        ConnectStation(sock, inet_addr(ipaddr), port, radiourl, &metaint);
+        ConnectStation(sock, ipaddr, port, radiourl, &metaint);
         NutThreadCreate("Stream", Stream, NULL, 1024);
     }
 }
@@ -282,4 +282,4 @@ void PlayMp3Stream(FILE *stream, u_long metaint)
             break;
         }
     }
-}
+}

+ 1 - 1
httpstream.h

@@ -6,7 +6,7 @@
 #define _Httpstream_H
 
 extern bool HttpIsStreaming();
-extern void playStream(char *ipaddr, u_short port, char *radiourl);
+extern void playStream(u_long ipaddr, u_short port, char *radiourl);
 extern void stopStream();
 
 

+ 40 - 60
main.c

@@ -20,34 +20,34 @@
 /*--------------------------------------------------------------------------*/
 #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 "alarm.h"
 #include "display.h"
-#include "remcon.h"
+#include "displayHandler.h"
+#include "eeprom.h"
+#include "flash.h"
+#include "httpstream.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 "typedefs.h"
-
-
-#include <time.h>
-#include "rtc.h"
-#include "alarm.h"
 #include "ntp.h"
-#include "httpstream.h"
+#include "portio.h"
+#include "remcon.h"
+#include "rtc.h"
+#include "spidrv.h"
+#include "system.h"
+#include "uart0driver.h"
+#include "watchdog.h"
+
 
 /*-------------------------------------------------------------------------*/
 /* local routines (prototyping)                                            */
@@ -181,8 +181,9 @@ static void SysControlMainBeat(u_char OnOff)
 /*-------------------------------------------------------------------------*/
 /* global variable definitions                                             */
 /*-------------------------------------------------------------------------*/
-int isAlarmSyncing;
-int initialized;
+bool isAlarmSyncing;
+bool initialized = false;
+unsigned char VS_volume = 7; //[0-15];
 
 
 /*-------------------------------------------------------------------------*/
@@ -198,37 +199,27 @@ THREAD(StartupInit, arg)
 
     NtpSync();
 
-    initialized = 1;
+    initialized = true;
     NutThreadExit();
 }
 
-THREAD(NTPSync, arg)
+THREAD(Sync, arg)
 {
     for(;;)
     {
-        if(initialized && (hasNetworkConnection() == true))
+        if((initialized == true) && (hasNetworkConnection() == true))
         {
-            while(isAlarmSyncing)
-            {
-                NutSleep(2000);
+            // NTP
+            if (0){ // TODO: Do a normal check
+                NtpSync();
             }
-            NtpSync();
-        }
-        NutSleep(86400000);
-    }
-}
 
-THREAD(AlarmSync, arg)
-{
-    for(;;)
-    {
-        if(initialized && (hasNetworkConnection() == true))
-        {
-            isAlarmSyncing = 1;
+            // Regular alarms
+            isAlarmSyncing = true;
             char* content = httpGet("/getAlarmen.php?radioid=DE370");
             parseAlarmJson(content);
             free(content);
-            isAlarmSyncing = 0;
+            isAlarmSyncing = false;
         }
         NutSleep(3000);
     }
@@ -257,7 +248,6 @@ int checkOffPressed(){
 
 int main(void)
 {
-	initialized = 0;
     int VOL2;
     time_t start;
 	int idx = 0;
@@ -290,8 +280,7 @@ int main(void)
     NtpInit();
 
     NutThreadCreate("BackgroundThread", StartupInit, NULL, 1024);
-    NutThreadCreate("BackgroundThread", AlarmSync, NULL, 2500);
-    NutThreadCreate("BackgroundThread", NTPSync, NULL, 700);
+    NutThreadCreate("BackgroundThread", Sync, NULL, 2500);
     /** Quick fix for turning off the display after 10 seconds boot */
 
     RcInit();
@@ -308,18 +297,7 @@ int main(void)
 	/* Enable global interrupts */
 	sei();
 
-   /* struct _tm tm;
-	tm = GetRTCTime();
-	tm.tm_sec += 10;
-    setAlarm(tm,"    test1234      ", "0.0.0.0", 8001,1,0,0);
-	tm.tm_sec +=20;
-	setAlarm(tm,"    test5678      ", "0.0.0.0", 8001,1,0,1);*/
-
-/*    if(hasNetworkConnection() == true){
-        playStream("145.58.53.152", 80, "/3fm-bb-mp3");
-    }*/
     start = time(0) - 10;
-    unsigned char VOL = 64;
 
     running = 1;
 
@@ -340,25 +318,28 @@ int main(void)
 			}
 		}
 
-        VOL = VOL2;
+        if (KbGetKey() == KEY_01){
+            playStream("145.58.53.152", 80, "/3fm-bb-mp3");
+        }
+
         if(KbGetKey() == KEY_DOWN)
         {
             NutSleep(150);
             start = time(0);
-            if(VOL > 8){
-                VOL -= 8;
-                VsSetVolume (128-VOL, 128-VOL);
-                displayVolume(VOL/8);
+            if (VS_volume >= 1){
+                VS_volume = (--VS_volume) % 17;
+                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
+                displayVolume(VS_volume);
             }
         }
         else if(KbGetKey() == KEY_UP)
         {
             NutSleep(150);
             start = time(0);
-            if(VOL < 128) {
-                VOL += 8;
-                VsSetVolume(128-VOL, 128-VOL);
-                displayVolume(VOL/8);
+            if (VS_volume <= 15){
+                VS_volume = (++VS_volume) % 17;
+                VsSetVolume (128-(VS_volume*8), 128-(VS_volume*8));
+                displayVolume(VS_volume);
             }
         }
         else if(timer(start) >= 5 && checkAlarms() == 1)
@@ -381,7 +362,6 @@ int main(void)
             displayDate(1);
 		}
 
-        VOL2 = VOL;
         WatchDogRestart();
     }
     return(0);

+ 1 - 1
network.c

@@ -190,7 +190,7 @@ void parseAlarmJson(char* content){
             //zoek naar een vrije plaats in de alarm array
             for(j = 0; j < maxAlarms(); j++){
                 if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
-                    setAlarm(time, name, ip, port, url, 5, id, j);
+                    setAlarm(time, name, inet_addr(ip), port, url, 5, id, j);
                     usedAlarms[j] = 1;
                     j = 10;
                 }

+ 28 - 37
ntp.c

@@ -20,20 +20,16 @@
 
 #include "log.h"
 #include "ntp.h"
+#include "eeprom.h"
+#include "typedefs.h"
 
-int TIME_ZONE = 1;
 #define LOG_MODULE  LOG_NTP_MODULE
 
-typedef struct _Eeprom_tm {
-    size_t len;
-    tm tm_struct;
-} Eeprom_tm;
-
-bool isSyncing;
+bool isSyncing = false;
 bool validTime = false;
 time_t ntp_time = 0;
 tm *ntp_datetime;
-uint32_t timeserver = 0;
+int TIME_ZONE = 1;
 
 void NtpInit(void) {
     puts("Func: NtpInit(void)");
@@ -47,26 +43,23 @@ 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 \n");
+    if (EepromGetCache(cache) == false){
+        puts("NtpCheckValidTime(): No cache available");
         validTime = false;
         return;
     }
 
-    // Valid configuration available.
-    puts("NtpCheckValidTime(): Valid config available \n");
-    tm stored_tm = eeprom_tm_struct.tm_struct;
+    // Cache is present
+    puts("NtpCheckValidTime(): Cache is available");
 
     // Check time is valid;
     tm current_tm;
     X12RtcGetClock(&current_tm);
 
+    tm stored_tm = cache->last_sync;
+
     validTime = NtpCompareTime(current_tm, stored_tm);
     if (validTime){
         puts("NtpCheckValidTime(): Time was valid \n");
@@ -95,20 +88,25 @@ bool NtpCompareTime(tm t1, tm t2){
     );
     puts(debug);
 
-    if (t1.tm_year > t2.tm_year)
+    if (t1.tm_year > t2.tm_year){
         return true;
-    if (t1.tm_mon > t2.tm_mon)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
         return true;
-    if (t1.tm_mday > t2.tm_mday)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
         return true;
-    if (t1.tm_hour > t2.tm_hour)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
         return true;
-    if (t1.tm_min > t2.tm_min)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
         return true;
-    if (t1.tm_sec > t2.tm_sec)
+    }
+    if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
         return true;
+    }
 
-    //else
     return false;
 }
 
@@ -120,10 +118,11 @@ void NtpSync(void){
     /* Ophalen van pool.ntp.org */
     isSyncing = true;
     _timezone = -getTimeZone() * 3600;
+    puts("NtpSync(): Timezone fetched. ");
     printf(TIME_ZONE);
     NutDelay(100);
     //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
-    timeserver = inet_addr("213.154.229.24");
+    uint32_t timeserver = inet_addr("213.154.229.24");
 
     for (;;) {
         if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
@@ -148,17 +147,9 @@ void NtpSync(void){
 }
 
 void NtpWriteTimeToEeprom(tm time_struct){
-    Eeprom_tm eeprom_tm_struct;
+    TCache cache;
 
-    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 \n"); }
-
-    NutDelay(100);
+    cache.last_sync = time_struct;
+    EepromSetCache(&cache);
 }
 
-//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    */