[PATCH] Led.h: Minor code adjustments for clarity, and cleaned up comments

Jeff Mayes Jeff.Mayes at OARcorp.com
Wed Apr 15 17:21:58 UTC 2015


From: Jeff Mayes <jeff.mayes at oarcorp.com>

---
 led/README |  8 ++---
 led/led.h  | 98 +++++++++++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 81 insertions(+), 25 deletions(-)

diff --git a/led/README b/led/README
index 4a2ab42..e78f1fd 100644
--- a/led/README
+++ b/led/README
@@ -1,12 +1,10 @@
 This is a set of example RTEMS programs.  Most of these programs are
-demonstrations various ways to use inter-task communication to 
-turn and LED on and off at a fixed rate.
+demonstrations that show various ways to use inter-task communication 
+to turn a LED on and off at a fixed rate.
 
-You must edit led.h so it knows how to turn an LED on and off on your
-target hardware.
+You must edit the led.h file to set your target hardware.
 
 The example programs form a logical sequence in the following order:
-
 hello_world_c
 posix_hello_world
 ticker
diff --git a/led/led.h b/led/led.h
index 3fee684..eecf100 100644
--- a/led/led.h
+++ b/led/led.h
@@ -38,10 +38,12 @@
 
 #elif defined(MPC8313ERDB)
 
-/* This rotates a single LED off around the back of 8. */
+/* 
+ * This rotates a single LED off around the back of 8.
+ */
 volatile uint8_t *MPC8313_LED_REG;
 uint8_t MPC8313_LED_Values[] =
-    { 0x01, 0x02, 0x04, 0x08, 0x80, 0x40, 0x20, 0x10};
+    {0x01, 0x02, 0x04, 0x08, 0x80, 0x40, 0x20, 0x10};
 uint8_t MPC8313_LED_Count;
 
 #define LED_INIT()  \
@@ -53,7 +55,7 @@ uint8_t MPC8313_LED_Count;
 #define LED_ROTATE()  \
   do { \
     *MPC8313_LED_REG = MPC8313_LED_Values[MPC8313_LED_Count]; \
-    /* printk( "v=0x%02x ", MPC8313_LED_Values[MPC8313_LED_Count] ); */ \
+    /* printk("v=0x%02x ", MPC8313_LED_Values[MPC8313_LED_Count]); */ \
     if ( ++MPC8313_LED_Count == 9 ) \
       MPC8313_LED_Count = 0; \
   } while (0)
@@ -62,23 +64,79 @@ uint8_t MPC8313_LED_Count;
 #define LED_OFF() LED_ROTATE()
 
 #elif defined(BCM2835_GPIO_REGS_BASE)
-// Raspberry Pi
-#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) 
-#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3)) 
-#define GPIO_SET *(gpio+7)   // sets   bits which are 1 ignores bits which are 0 
-#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
-// For GPIO# >= 32 (RPi B+)
-#define GPIO_SET_EXT *(gpio+8)  // sets   bits which are 1 ignores bits which are 0
-#define GPIO_CLR_EXT *(gpio+11) // clears bits which are 1 ignores bits which are 0
-
-// RPi B
-//#define LED_INIT()  do { unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; OUT_GPIO(16);} while(0)
-//#define LED_ON()  do { unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; GPIO_CLR = 1 << 16;} while(0)
-//#define LED_OFF()  do { unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; GPIO_SET = 1 << 16;} while(0)
-// RPi B+ => led 47
-#define LED_INIT()  do { unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; OUT_GPIO(47);} while(0)
-#define LED_ON()  do { unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; GPIO_CLR_EXT = 1 << (47 % 32);} while(0)
-#define LED_OFF()  do { unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; GPIO_SET_EXT = 1 << (47 % 32);} while(0)
+  /*
+   * Raspberry Pi - set the #define to match your model
+   * #define PI_MODEL_B 1
+   * #define PI_MODEL_B_PLUS 1 
+   */
+  #define PI_MODEL_B 1
+
+  #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) 
+  #define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3)) 
+
+  /*
+   *  For the base GPIO pins:
+   *  SET: sets bits which are 1, ignores bits which are 0
+   *  CLR: clears bits which are 1, ignores bits which are 0
+   */
+  #define GPIO_SET *(gpio+7) 
+  #define GPIO_CLR *(gpio+10)
+
+  /* 
+   *  For the extended GPIO# >= 32 (RPi B+ and maybe Pi2?)
+   *  SET_EXT: sets bits which are 1, ignores bits which are 0
+   *  CLR_EXT: clears bits which are 1, ignores bits which are 0
+   */  
+  #define GPIO_SET_EXT *(gpio+8)  
+  #define GPIO_CLR_EXT *(gpio+11) 
+
+  #if PI_MODEL_B
+    /*
+     * For RPi B, GPIO16 drives the ACT status LED on the board, so 
+     * use pin 16.  Alternatively, GPIO17 (pin #11 on the board) can be used
+     * to connect an external LED.
+     */
+    #define PI_LED_PIN 16
+    #define LED_INIT() \
+      do { \
+        unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; \
+        OUT_GPIO(PI_LED_PIN); \
+      } while(0)
+    #define LED_ON() \
+      do { \
+        unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; \
+        GPIO_CLR = 1 << PI_LED_PIN; \
+      } while(0)
+    #define LED_OFF() \
+      do { \
+        unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; \
+        GPIO_SET = 1 << PI_LED_PIN; \
+      } while(0)
+
+  #elif PI_MODEL_BPLUS
+    /*
+     * For RPi B+, use LED mapped to pin 47
+     */
+    #define PI_LED_PIN 47
+    #define LED_INIT() \
+      do { \
+        unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; \
+        OUT_GPIO(PI_LED_PIN); \
+      } while(0)
+    #define LED_ON() \
+      do { \
+        unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; \
+        GPIO_CLR_EXT = 1 << (PI_LED_PIN % 32); \
+      } while(0)
+    #define LED_OFF() \
+      do { \
+        unsigned int *gpio = (unsigned int *)BCM2835_GPIO_REGS_BASE; \
+        GPIO_SET_EXT = 1 << (PI_LED_PIN % 32); \
+      } while(0)
+
+  #else
+    #error "Pi LED: Pick a model"
+  #endif
 
 #else
 /* default case is to print */
-- 
1.9.3



More information about the devel mailing list