Backing up an unused program in case of future need

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Thu Dec 06 11:40:19 2018 +0000
Parent:
7:024ace6d943c
Commit message:
Backing up

Changed in this revision

1-wirenew.cpp Show annotated file Show diff for this revision Revisions of this file
1-wirenew.h Show annotated file Show diff for this revision Revisions of this file
extrtc.cpp Show annotated file Show diff for this revision Revisions of this file
extrtc.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
server.cpp Show annotated file Show diff for this revision Revisions of this file
time.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/1-wirenew.h	Thu Dec 06 11:40:19 2018 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "io.h"
+ 
+static void timer0_IRQHandler(void)
+{
+    //Handle match channel 0
+    if (LPC_TIM0->IR & 1)
+    {
+        LPC_TIM0->IR = 1; //Writing a logic one to the corresponding IR bit will reset the interrupt. Writing a zero has no effect. See 21.6.1.
+    }
+    //Handle match channel 1
+    if (LPC_TIM0->IR & 2)
+    {
+        LPC_TIM0->IR = 2;
+    }
+    //Handle match channel 2
+    if (LPC_TIM0->IR & 4)
+    {
+        LPC_TIM0->IR = 4;
+    }
+    //Handle match channel 3
+    if (LPC_TIM0->IR & 8)
+    {
+        LPC_TIM0->IR = 8;
+    }
+    Led1 = !Led1
+}
+ 
+void OneWireNewSetup(int us0, int us1, int us2, int us3)
+{
+    LPC_TIM0->MR0  = us0;  // 21.6.7 Match Register 0       - Match count
+    LPC_TIM0->MR1  = us1;  // 21.6.7 Match Register 1       - Match count
+    LPC_TIM0->MR2  = us2;  // 21.6.7 Match Register 2       - Match count
+    LPC_TIM0->MR3  = us3;  // 21.6.7 Match Register 3       - Match count
+    LPC_TIM0->TCR  = 1;    // 21.6.2 Timer Control Register - Enable TC and PC
+}
+
+int OneWireNewInit()
+{
+    NVIC_SetVector  (TIMER0_IRQn,(uint32_t)&timer0_IRQHandler);
+    NVIC_SetPriority(TIMER0_IRQn, 1);
+    LPC_SC->PCONP |= 2;        //  4.8.9 Power Control for Peripherals register - Timer0 Power On
+    LPC_TIM0->TCR  = 2;        // 21.6.2 Timer Control Register - Reset TC and PC
+    LPC_TIM0->CTCR = 0;        // 21.6.3 Count Control Register - Timer mode
+    LPC_TIM0->PR   = 95;       // 21.6.5 Prescale register      - Prescale 96MHz clock to 1Mhz. When PC is equal to this value, the next clock increments the TC and clears the PC
+    LPC_TIM0->MCR  = 07111;    // 21.6.8 Match Control Register - Interrupt on match 2, 1, and 0; stop, reset and interrupt on match 3
+    NVIC_EnableIRQ(TIMER0_IRQn); 
+    return 0;
+}
+int OneWireNewMain() 
+{
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extrtc.cpp	Thu Dec 06 11:40:19 2018 +0000
@@ -0,0 +1,81 @@
+#include "mbed.h"
+
+static SPI device(p5, p6, p7); // mosi, miso, sclk
+static DigitalOut cs(p8, 1);   // ssel
+
+/*
+| BIT 7 | BIT 6 | BIT 5 | BIT 4 | BIT 3 | BIT 2 | BIT 1 | BIT 0 |
++-------+-------+-------+-------+-------+-------+-------+-------+
+|   -   |          10 Seconds   |             Seconds           | 00–59
++-------+-------+-------+-------+-------+-------+-------+-------+
+|   -   |          10 Minutes   |             Minutes           | 00–59
++-------+-------+-------+-------+-------+-------+-------+-------+
+|   -   |   -   |  10 Hours     |             Hours             | 00-23
++-------+-------+-------+-------+-------+-------+-------+-------+
+|   -   |   -   |   -   |   -   |   -   |     Weekday           |  1-7
++-------+-------+-------+-------+-------+-------+-------+-------+
+|   -   |   -   |  10 Days      |             Days              | 01-31
++-------+-------+-------+-------+-------+-------+-------+-------+
+|Century|   -   |   -   | 10 Mo |             Months            | 01-12
++-------+-------+-------+-------+-------+-------+-------+-------+
+|                  10 Years     |             Years             | 00-99
++-------+-------+-------+-------+-------+-------+-------+-------+
+*/
+static char bcdToInt(char bcd)
+{
+    return 0;
+}
+static char intToBcd(char i)
+{
+    return 0;
+}
+void ExtRtcSet(struct tm *ptm)
+{
+    char bcd;
+    cs = 0;
+    device.write(0x80); //Send address 00h
+    bcd = intToBcd(ptm->tm_sec ); device.write(bcd);
+    bcd = intToBcd(ptm->tm_min ); device.write(bcd);
+    bcd = intToBcd(ptm->tm_hour); device.write(bcd);
+    bcd =          ptm->tm_wday;  device.write(bcd);
+    bcd = intToBcd(ptm->tm_mday); device.write(bcd);
+    int year = ptm->tm_year;
+    bcd = intToBcd(ptm->tm_mon);
+    if (year > 99) bcd |= 0x80;
+    device.write(bcd);
+    if (year > 99) year -=100;
+    bcd = intToBcd(year);
+    device.write(bcd);
+    cs = 1;
+}
+void ExtRtcGet(struct tm *ptm)
+{
+    char bcd;
+    cs = 0;
+    device.write(0x00); //Send address 00h
+    bcd = device.write(0xFF); ptm->tm_sec  = bcdToInt(bcd);
+    bcd = device.write(0xFF); ptm->tm_min  = bcdToInt(bcd);
+    bcd = device.write(0xFF); ptm->tm_hour = bcdToInt(bcd);
+    bcd = device.write(0xFF); ptm->tm_wday = bcd;
+    bcd = device.write(0xFF); ptm->tm_mday = bcdToInt(bcd);
+    bcd = device.write(0xFF);
+    int century = bcd & 0x80;
+    bcd &= 0x7F;
+    ptm->tm_mon  = bcdToInt(bcd);
+    bcd = device.write(0xFF);
+    ptm->tm_year = bcdToInt(bcd);
+    if (century) ptm->tm_year += 100;
+    cs = 1;
+}
+
+int ExtRtcMain()
+{
+    return 0;
+}
+int ExtRtcInit()
+{
+    cs = 1;
+    device.format(8, 1);
+    device.frequency(1000000);
+    return 0;
+}
\ No newline at end of file
--- a/mbed.bld	Thu Jun 02 09:26:28 2016 +0000
+++ b/mbed.bld	Thu Dec 06 11:40:19 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/7c328cabac7e
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/794e51388b66
\ No newline at end of file
--- a/server.cpp	Thu Jun 02 09:26:28 2016 +0000
+++ b/server.cpp	Thu Dec 06 11:40:19 2018 +0000
@@ -164,13 +164,7 @@
             len = 0;
             struct tm tm;
             TimeGetTm(&tm);
-            len = addChunkF(len, "Time (UTC): %d-%02d-%02d %02d:%02d:%02d ",
-                tm.tm_year + 1900,
-                tm.tm_mon + 1,
-                tm.tm_mday,
-                tm.tm_hour,
-                tm.tm_min,
-                tm.tm_sec);
+            len = addChunkF(len, "Time: %d-%02d-%02d ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
             switch(tm.tm_wday)
             {
                 case  0: len = addChunk(len, "Sun"); break;
@@ -182,6 +176,7 @@
                 case  6: len = addChunk(len, "Sat"); break;
                 default: len = addChunk(len, "???"); break;
             }
+            len = addChunkF(len, " %02d:%02d:%02d UTC", tm.tm_hour, tm.tm_min, tm.tm_sec);
             len = addChunk (len, "<br/><br/>\r\n");
             len = addChunkF(len, "Scan &micro;s: %d<br/><br/>\r\n", TimeScanUs);
             lineToSendToId[id] += 1;
--- a/time.cpp	Thu Jun 02 09:26:28 2016 +0000
+++ b/time.cpp	Thu Dec 06 11:40:19 2018 +0000
@@ -33,18 +33,14 @@
     //Add a year at a time while there is more than a year of days left
     int year      = 1970; //Unix epoch is 1970
     int dayOfWeek = 4;    //1 Jan 1970 is a Thursday
-    int dayOfYear;
     int leapYear;
     while(1)
     {
         //See if it is a leap year
-        int year4   = !(year %   4);
-        int year100 = !(year % 100);
-        int year400 = !(year % 400);
-                     leapYear = false;
-        if (year4)   leapYear =  true;
-        if (year100) leapYear = false;
-        if (year400) leapYear =  true;
+                             leapYear = false;
+        if (year %   4 == 0) leapYear =  true;
+        if (year % 100 == 0) leapYear = false;
+        if (year % 400 == 0) leapYear =  true;
         
         //Find the number of days in this year
         int daysInYear = leapYear ? 366 : 365;
@@ -61,30 +57,18 @@
         year++;
     }
     
-    dayOfYear = daysLeft;
+    //Days left contains the days left from the start (1 Jan) of the current year
+    int dayOfYear = daysLeft + 1;
     dayOfWeek += daysLeft;
     dayOfWeek %= 7;
 
     //Add a month at a time while there is more than a month of days left
-    int month = 0;
+    static char monthlengths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+    int month = 1;
     while(1)
     {
-        int daysInMonth;
-        switch (month + 1)
-        {
-            case  1: daysInMonth = 31; break;
-            case  2: daysInMonth = leapYear ? 29 : 28; break;
-            case  3: daysInMonth = 31; break;
-            case  4: daysInMonth = 30; break;
-            case  5: daysInMonth = 31; break;
-            case  6: daysInMonth = 30; break;
-            case  7: daysInMonth = 31; break;
-            case  8: daysInMonth = 31; break;
-            case  9: daysInMonth = 30; break;
-            case 10: daysInMonth = 31; break;
-            case 11: daysInMonth = 30; break;
-            case 12: daysInMonth = 31; break;
-        }
+        int daysInMonth = monthlengths[month-1];
+        if (month == 2 && leapYear) daysInMonth++;
         
         //Stop if this is the last month
         if (daysLeft < daysInMonth) break;
@@ -93,15 +77,49 @@
         daysLeft -= daysInMonth;
         month++;
     }
-
-  ptm->tm_sec  = seconds;
-  ptm->tm_min  = minutes;
-  ptm->tm_hour = hours;
-  ptm->tm_mday = daysLeft + 1;
-  ptm->tm_mon  = month;
-  ptm->tm_year = year - 1900;
-  ptm->tm_wday = dayOfWeek;
-  ptm->tm_yday = dayOfYear;
+    
+    //Days left contains the days left from the start (1st) of the current month
+    int dayOfMonth = daysLeft + 1;
+      
+    //Find the last Sunday in the month
+    int lastSunday = monthlengths[month-1];
+    while (lastSunday > 0)
+    {
+        int weekday = (dayOfWeek + lastSunday - dayOfMonth) % 7;
+        if (weekday == 0) break; //Stop when weekday is Sunday
+        lastSunday--;
+    }
+    
+    //Work out if Daylight Saving Time applies
+    int dst;
+    if (month <= 2) dst = false;              //Jan, Feb
+    if (month == 3)                           //Mar - DST true after 1am UTC on the last Sunday in March
+    {
+        if (dayOfMonth <  lastSunday) dst = false;
+        if (dayOfMonth == lastSunday) dst = hours >= 1;
+        if (dayOfMonth >  lastSunday) dst = true;
+    }
+    if (month >= 4 && month <= 9) dst = true; //Apr, May, Jun, Jul, Aug, Sep
+    if (month == 10)                          //Oct - DST false after 1am UTC on the last Sunday in October
+    {
+        if (dayOfMonth <  lastSunday) dst = true;
+        if (dayOfMonth == lastSunday) dst = hours < 1;
+        if (dayOfMonth >  lastSunday) dst = false;
+    }
+    if (month >= 11) dst = false;             //Nov, Dec
+    
+    
+    //Set up the broken time TM structure
+    ptm->tm_sec   = seconds;       // 00 --> 59
+    ptm->tm_min   = minutes;       // 00 --> 59
+    ptm->tm_hour  = hours;         // 00 --> 23
+    ptm->tm_mday  = dayOfMonth;    // 01 --> 31
+    ptm->tm_mon   = month - 1;     // 00 --> 11
+    ptm->tm_year  = year - 1900;   // Years since 1900
+    ptm->tm_wday  = dayOfWeek;     // 0 --> 6 where 0 == Sunday
+    ptm->tm_yday  = dayOfYear - 1; // 0 --> 365
+    ptm->tm_isdst = dst;           // +ve if DST, 0 if not DSTime, -ve if the information is not available. Note that 'true' evaluates to +1.
+    
 }
 void TimeGetTm(struct tm* ptm)
 {