[freenos] r298 committed - Modified the Time server to enhance it's coding style....

  • From: codesite-noreply@xxxxxxxxxx
  • To: freenos@xxxxxxxxxxxxx
  • Date: Fri, 07 Aug 2009 09:32:40 +0000

Revision: 298
Author: nieklinnenbank
Date: Fri Aug  7 02:32:03 2009
Log: Modified the Time server to enhance it's coding style.
These are some small coding style changes to the code, including:

 1) Declare variables at the beginning of the function body.
2) If you don't implement a virtual function, leave it out (e.g. write() here) 3) Don't add spaces between the paratheses of an if(), for() or while() construct.
 4) If using external code, mention it in the Doxygen tags with @see.
5) Always add Doxygen tags to each function, class, struct and preprocessor directive.


http://code.google.com/p/freenos/source/detail?r=298

Modified:
 /trunk/srv/time/Time.cpp
 /trunk/srv/time/Time.h

=======================================
--- /trunk/srv/time/Time.cpp    Wed Aug  5 05:42:39 2009
+++ /trunk/srv/time/Time.cpp    Fri Aug  7 02:32:03 2009
@@ -33,65 +33,58 @@
 {
     ProcessCtl(SELF, AllowIO, RTC_PORT(0));
     ProcessCtl(SELF, AllowIO, RTC_PORT(1));
+
     /* Done! */
     return ESUCCESS;
 }

 Error Time::read(s8 *buffer, Size size, Size offset)
 {
-    /*
-     * NOTICE:
-     * A lot of this code comes from the linux kernel source,
-     * particularly from the file arch/x86/kernel/rtc.c.
-     * As a result, the code (from mainly Time::readCMOS()) may
-     * have to be replaced to something like kernel/X86/RTC.cpp
-    */
-
+    unsigned int year, month, day, hour, min, sec = 0, time;
+
     /* PHONY read */
-    if( offset >= 10 )
+    if(offset >= 10)
     {
         return 0;
     }

-    //unsigned int status;
-    //unsigned int century;
-    unsigned int year, month, day, hour, min, sec = 0;
-
-    /*
-     * This code comes from arch/x86/kernel/rtc.c from the linux source
+    /*
+     * If UIP is clear, then we have >= 244 microseconds before
+     * RTC registers will be updated.  Spec sheet says that this
+     * is the reliable way to read RTC - registers. If UIP is set
+     * then the register access might be invalid.
      */
-    while( (readCMOS(RTC_FREQ_SELECT) & RTC_UIP ))
-    {
-        Time::cpuRelax();
+    while((readCMOS(RTC_STATUS_A) & RTC_UIP))
+    {
+        cpuRelax();
     }

-    sec = bcd2bin(readCMOS(RTC_SECONDS));
-    min = bcd2bin(readCMOS(RTC_MINUTES));
-    hour = bcd2bin(readCMOS(RTC_HOURS));
-    day = bcd2bin(readCMOS(RTC_DAY_OF_MONTH));
+    /* Convert from binary coded decimal (bcd) to machine numbers. */
+    sec   = bcd2bin(readCMOS(RTC_SECONDS));
+    min   = bcd2bin(readCMOS(RTC_MINUTES));
+    hour  = bcd2bin(readCMOS(RTC_HOURS));
+    day   = bcd2bin(readCMOS(RTC_DAY_OF_MONTH));
     month = bcd2bin(readCMOS(RTC_MONTH));
-    year = bcd2bin(readCMOS(RTC_YEAR));
-
-    if( year < 100 )
+    year  = bcd2bin(readCMOS(RTC_YEAR));
+
+    /* Assume that a two-digit year is after 2000 */
+    if(year < 100)
     {
         year += CMOS_YEARS_OFFS;
     }

-    unsigned long time = mktime(year, month, day, hour, min, sec);
-
+    /* Format as an ASCII string. */
+    time = mktime(year, month, day, hour, min, sec);
     snprintf((char*)buffer, size, "%u", time);
+
+    /* All done. */
     return (Error) size;
 }

 unsigned char Time::readCMOS(unsigned char addr)
 {
-    unsigned char val;
-
-    //lock_cmos_prefix(addr);
     outb(RTC_PORT(0), addr);
-    val = inb(RTC_PORT(1));
-    // lock_cmos_suffix(addr);
-    return val;
+    return inb(RTC_PORT(1));
 }

 void Time::cpuRelax()
=======================================
--- /trunk/srv/time/Time.h      Wed Aug  5 05:42:39 2009
+++ /trunk/srv/time/Time.h      Fri Aug  7 02:32:03 2009
@@ -22,28 +22,60 @@
 #include <Types.h>
 #include <Device.h>

+/** @brief The base I/O port of the CMOS. */
 #define RTC_PORT(x) (0x70 + (x))

+/** @brief Offset in the CMOS for the current number of seconds. */
 #define RTC_SECONDS             0
+
+/** @brief Offset in the CMOS for the current number of minutes. */
 #define RTC_MINUTES             2
+
+/** @brief Offset in the CMOS for the current number of hours. */
 #define RTC_HOURS               4
+
+/** @brief Offset in the CMOS for the current day of the week. */
 #define RTC_DAY_OF_WEEK         6
+
+/** @brief Offset in the CMOS for the current day of the month. */
 #define RTC_DAY_OF_MONTH        7
+
+/** @brief Offset in the CMOS for the current month. */
 #define RTC_MONTH               8
+
+/**
+ * @brief Offset in the CMOS for the current year.
+ *
+ * A one digit value means before 2000, and a two-digit value,
+ * i.e. >= 100, is after the year 2000.
+ */
 #define RTC_YEAR                9

-/* Assume that a two-digit year is after 2000 */
+/** @brief Assume that a two-digit year is after 2000 */
 #define CMOS_YEARS_OFFS         2000

-#define RTC_FREQ_SELECT         10
+/** @brief Offset in CMOS for the status A register. */
+#define RTC_STATUS_A           10
+
+/** @brief Update in progress flag. */
 #define RTC_UIP                 0x80

+/**
+ * @brief System Time server.
+ *
+ * This server is responsible for maintaining the global system
+ * time via the CMOS. A lot of this code comes from the linux kernel
+ * source, particularly from the file arch/x86/kernel/rtc.c.
+ *
+ * @see http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.30.y.git;a=blob_plain;f=arch/x86/kernel/rtc.c;hb=HEAD
+ */
+
 class Time : public Device
 {
     public:

        /**
-        * Constructor function.
+        * @brief Constructor function.
         */
         Time();

@@ -54,41 +86,39 @@
        Error initialize();

        /**
-        * Read the time
+        * @brief Read the system time.
         * @param buffer Buffer to save the read bytes.
         * @param size Number of bytes to read.
-        * @param offset Unused.
+        * @param offset Offset in the file to read.
         * @return Number of bytes on success and ZERO on failure.
         */
        Error read(s8 *buffer, Size size, Size offset);

-       /**
-        * Write the time
-        * @param buffer Buffer containing bytes to write.
-        * @param size Number of bytes to write.
-        * @param offset Unused.
-        * @return Number of bytes on success and ZERO on failure.
-        */
-       /* Error write(s8 *buffer, Size size, Size offset); */
-
     private:

         /**
-         * Returns the value stored at the given address
-         * from the CMOS.
-         * I actually almost copied this code from the linux source
-         * from the file arch/x86/kernel/rtc.h so you should also
-         * take a look over there how they do it.
+         * @brief Returns the value stored at the given address
+         *        from the CMOS.
+        *
          * @param addr The address to read from the CMOS
          * @return The value at the given address.
+        *
+        * @note I actually almost copied this code from the linux source
+         *       from the file arch/x86/kernel/rtc.h so you should also
+         *       take a look over there how they do it.
          */
         unsigned char readCMOS(unsigned char addr);

         /**
-         * Requests that the cpu does a nop.
+         * @brief Requests that the cpu does a NOP instruction.
          */
         static inline void cpuRelax();
-
+
+       /**
+        * @brief Convert from binary coded decimal to binary form.
+        * @param val The value to convert.
+        * @return A binary integer.
+        */
         unsigned bcd2bin(unsigned char val);
 };


Other related posts:

  • » [freenos] r298 committed - Modified the Time server to enhance it's coding style.... - codesite-noreply