From cd6039933ed4de8421c8c9b452660354f2deb410 Mon Sep 17 00:00:00 2001
From: Gregory Nutt <gnutt@nuttx.org>
Date: Fri, 11 Apr 2014 15:57:35 -0600
Subject: [PATCH] SAMA5: The PIO used for the red LED is also used as the
 camera module reset line:  Added a configuration option to suppress use of
 the red LED if the PIO is used for another purpose.  Reported by David
 Sidrane.

---
 ChangeLog                              |  6 ++++-
 configs/sama5d3-xplained/README.txt    |  9 ++++----
 configs/sama5d3x-ek/Kconfig            | 12 ++++++++++
 configs/sama5d3x-ek/README.txt         | 32 +++++++++++++++++++++-----
 configs/sama5d3x-ek/include/board.h    | 24 +++++++++++++------
 configs/sama5d3x-ek/src/sam_autoleds.c | 14 ++++++++++-
 configs/sama5d3x-ek/src/sam_userleds.c |  8 ++++++-
 configs/sama5d3x-ek/src/sama5d3x-ek.h  | 11 +++++++--
 8 files changed, 94 insertions(+), 22 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7a9745ba20..6258c1f86b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7165,4 +7165,8 @@
 	* arch/arm/src/sama5/sam_twi.c: TWI data sending is fails to increment
 	  the number of byes transferred on first byte sent.  from David Sidrane
 	  (2014-4-10).
-
+	* configs/sama5d3x-ek/src: The red LED is controlled by PE24 which is
+	  also the camera/ISI interface reset line.  So if the a camera is
+	  installed, then we must avoid controlling the red LED or we inadvertently
+	  also reset the camera module.  Noted by David Sidrane (2014-4-11).
+ 
diff --git a/configs/sama5d3-xplained/README.txt b/configs/sama5d3-xplained/README.txt
index 5deb0c4ac7..d17bcdf88e 100644
--- a/configs/sama5d3-xplained/README.txt
+++ b/configs/sama5d3-xplained/README.txt
@@ -667,7 +667,8 @@ Buttons and LEDs
 
     PE24.  The red LED is also pulled high but is driven by a transistor so
     that it is illuminated when power is applied even if PE24 is not
-    configured as an output.
+    configured as an output.  If PE24 is configured as an output, then the
+    LED is illuminated by a high output.
 
   These LEDs are not used by the board port unless CONFIG_ARCH_LEDS is
   defined.  In that case, the usage by the board port is defined in
@@ -688,7 +689,7 @@ Buttons and LEDs
     LED_IDLE             MCU is is sleep mode         Not used
 
   Thus if the blue LED is statically on, NuttX has successfully booted and
-  is, apparently, running normally.  If the red is flashing at
+  is, apparently, running normally.  If the red LED is flashing at
   approximately 2Hz, then a fatal error has been detected and the system
   has halted.
 
@@ -919,7 +920,7 @@ AT25 Serial FLASH
      6    SCK      PD12  SPI0_SPCK
      1    /CS      PD13  if jumper JP6 is closed.
 
-  NOTE:  The MN8 is not populated on my SAMAD3 Xplained board.  So, as a 
+  NOTE:  The MN8 is not populated on my SAMAD3 Xplained board.  So, as a
   result, these instructions would only apply if you were to have an AT25
   Serial DataFlash installed in MN8.
 
@@ -1484,7 +1485,7 @@ SDRAM Support
 
   SDRAM Data Configuration
   ------------------------
- 
+
   In these configurations, .data and .bss are retained in ISRAM by default.
   .data and .bss can also be retained in SDRAM using these slightly
   different configuration settings.  In this configuration, ISRAM is
diff --git a/configs/sama5d3x-ek/Kconfig b/configs/sama5d3x-ek/Kconfig
index 1ef0998aa2..b5840dc36c 100644
--- a/configs/sama5d3x-ek/Kconfig
+++ b/configs/sama5d3x-ek/Kconfig
@@ -37,6 +37,17 @@ config SAMA5D3xEK_MT47H64M16HR
 
 endchoice # SAMA5D3x-EK DRAM Type
 
+config SAMA5D3xEK_NOREDLED
+	bool "Red LED not available"
+	default n
+	---help---
+		By default, both the blue LED and the red LED are used by the
+		SAMA5D3x-EK board logic.  However, the PIO associated with the red
+		LED (PE24) has other board functions.  For example, PE24 is also the
+		ISI_RST reset signal to the camera interface.  So when PE24 is not
+		available to control the red LED, this configuration variable should
+		be set so that the LED logic will only used the blue LED.
+
 config SAMA5D3xEK_NOR_MAIN
 	bool "Build nor_main"
 	default n
@@ -231,6 +242,7 @@ if VIDEO_OV2640
 config SAMA5D3xEK_OV2640_DEMO
 	bool "SAMA5 OV2640 camera demo"
 	default n
+	select SAMA5D3xEK_NOREDLED
 	---help---
 		Enable the SAMA5 OV2640 camera demo
 
diff --git a/configs/sama5d3x-ek/README.txt b/configs/sama5d3x-ek/README.txt
index 78c3fa893b..b1f9e24af8 100644
--- a/configs/sama5d3x-ek/README.txt
+++ b/configs/sama5d3x-ek/README.txt
@@ -787,12 +787,16 @@ Buttons and LEDs
     PE24.  The red LED is also pulled high but is driven by a transistor so
     that it is illuminated when power is applied even if PE24 is not
     configured as an output.  If PE24 is configured as an output, then the
-    LCD is illuminated by a high output.
+    LED is illuminated by a high output.
+
+      N.B. PE24 Drives the RED Led on the CM (SODIMM200), but unfortunately
+      it is also connected to ISI_RST on the MB (Main Board) and controlling
+      it will reset a Camera connected to the ISI
 
   These LEDs are not used by the board port unless CONFIG_ARCH_LEDS is
   defined.  In that case, the usage by the board port is defined in
   include/board.h and src/sam_leds.c. The LEDs are used to encode OS-related
-  events as follows:
+  events as follows when the red LED (PE24) is available:
 
     SYMBOL                Meaning                     LED state
                                                     Blue     Red
@@ -807,9 +811,25 @@ Buttons and LEDs
     LED_PANIC            The system has crashed     OFF      Blinking
     LED_IDLE             MCU is is sleep mode         Not used
 
+  If CONFIG_SAMA5D3xEK_NOREDLED=y, then the red LED is not used by the
+  system and the controls are as follows:
+
+    SYMBOL                Meaning                     LED state
+                                                    Blue     Red
+    -------------------  -----------------------  -------- ----------
+    LED_STARTED          NuttX has been started     OFF      Not used
+    LED_HEAPALLOCATE     Heap has been allocated    OFF      " " "  "
+    LED_IRQSENABLED      Interrupts enabled         OFF      " " "  "
+    LED_STACKCREATED     Idle stack created         ON       " " "  "
+    LED_INIRQ            In an interrupt              No change
+    LED_SIGNAL           In a signal handler          No change
+    LED_ASSERTION        An assertion failed          No change
+    LED_PANIC            The system has crashed     Blinking " " "  "
+    LED_IDLE             MCU is is sleep mode         Not used
+
   Thus if the blue LED is statically on, NuttX has successfully booted and
-  is, apparently, running normally.  If the red is flashing at
-  approximately 2Hz, then a fatal error has been detected and the system
+  is, apparently, running normally.  If the red (or blue) LED is flashing
+  at approximately 2Hz, then a fatal error has been detected and the system
   has halted.
 
 Serial Consoles
@@ -832,7 +852,7 @@ Serial Consoles
     PB28 RXD1       PIO_USART1_RXD
     PB26 CTS1       PIO_USART1_CTS
 
-    NOTE: Debug TX (DTXD) and RX (DRXD) pins also are routed to the 
+    NOTE: Debug TX (DTXD) and RX (DRXD) pins also are routed to the
     ADM3312EARU via non populated 0 Ohm resistors. Thus allowing one
     skilled with a soldering iron to choose which UART is level
     translated by the ADM3312EARU
@@ -1650,7 +1670,7 @@ SDRAM Support
 
   SDRAM Data Configuration
   ------------------------
- 
+
   In these configurations, .data and .bss are retained in ISRAM by default.
   .data and .bss can also be retained in SDRAM using these slightly
   different configuration settings.  In this configuration, ISRAM is
diff --git a/configs/sama5d3x-ek/include/board.h b/configs/sama5d3x-ek/include/board.h
index 5f3ad98f13..8c64ed09b1 100644
--- a/configs/sama5d3x-ek/include/board.h
+++ b/configs/sama5d3x-ek/include/board.h
@@ -132,18 +132,24 @@
 /* LED index values for use with sam_setled() */
 
 #define BOARD_BLUE        0
-#define BOARD_RED         1
-#define BOARD_NLEDS       2
+#ifdef CONFIG_SAMA5D3xEK_NOREDLED
+#  define BOARD_NLEDS     1
+#else
+#  define BOARD_RED       1
+#  define BOARD_NLEDS     2
+#endif
 
 /* LED bits for use with sam_setleds() */
 
 #define BOARD_BLUE_BIT    (1 << BOARD_BLUE)
-#define BOARD_RED_BIT     (1 << BOARD_RED)
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
+#  define BOARD_RED_BIT   (1 << BOARD_RED)
+#endif
 
 /* These LEDs are not used by the board port unless CONFIG_ARCH_LEDS is
  * defined.  In that case, the usage by the board port is defined in
  * include/board.h and src/sam_leds.c. The LEDs are used to encode OS-related
- * events as follows:
+ * events as follows when the red LED (PE24) is available:
  *
  *      SYMBOL            Val    Meaning                     LED state
  *                                                         Blue     Red
@@ -158,9 +164,13 @@
 #define LED_PANIC         3  /* The system has crashed     OFF      Blinking */
 #undef  LED_IDLE             /* MCU is is sleep mode         Not used        */
 
-/* Thus if the blue LED is statically on, NuttX has successfully booted and
- * is, apparently, running normmally.  If the red is flashing at
- * approximately 2Hz, then a fatal error has been detected and the system
+/* If CONFIG_SAMA5D3xEK_NOREDLED=y, then the red LED is not used by the
+ * system.  The only difference from the above is that it is the blue, not
+ * the red LED that blinks in the event of an PANIC.
+ *
+ * Thus if the blue LED is statically on, NuttX has successfully booted and
+ * is, apparently, running normally.  If the red (or blue) LED is flashing
+ * at approximately 2Hz, then a fatal error has been detected and the system
  * has halted.
  */
 
diff --git a/configs/sama5d3x-ek/src/sam_autoleds.c b/configs/sama5d3x-ek/src/sam_autoleds.c
index ef255f4d3f..3157511b5c 100644
--- a/configs/sama5d3x-ek/src/sam_autoleds.c
+++ b/configs/sama5d3x-ek/src/sam_autoleds.c
@@ -1,7 +1,7 @@
 /****************************************************************************
  * configs/sama5d3x-ek/src/sam_autoleds.c
  *
- *   Copyright (C) 2013 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2013-2014 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <gnutt@nuttx.org>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -123,7 +123,9 @@ void board_led_initialize(void)
   /* Configure LED PIOs for output */
 
   sam_configpio(PIO_BLUE);
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
   sam_configpio(PIO_RED);
+#endif
 }
 
 /****************************************************************************
@@ -133,7 +135,9 @@ void board_led_initialize(void)
 void board_led_on(int led)
 {
   bool blueoff = true;  /* Low illuminates */
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
   bool redon   = false; /* High illuminates */
+#endif
 
   switch (led)
     {
@@ -149,12 +153,18 @@ void board_led_on(int led)
         return;
 
       case 3:  /* LED_PANIC */
+#ifdef CONFIG_SAMA5D3xEK_NOREDLED
+        blueoff = false;
+#else
         redon = true;
+#endif
         break;
     }
 
   sam_piowrite(PIO_BLUE, blueoff);
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
   sam_piowrite(PIO_RED, redon);
+#endif
 }
 
 /****************************************************************************
@@ -166,7 +176,9 @@ void board_led_off(int led)
   if (led != 2)
     {
       sam_piowrite(PIO_BLUE, true);  /* Low illuminates */
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
       sam_piowrite(PIO_RED, false);  /* High illuminates */
+#endif
     }
 }
 
diff --git a/configs/sama5d3x-ek/src/sam_userleds.c b/configs/sama5d3x-ek/src/sam_userleds.c
index 8adbd6ee31..690d109909 100644
--- a/configs/sama5d3x-ek/src/sam_userleds.c
+++ b/configs/sama5d3x-ek/src/sam_userleds.c
@@ -1,7 +1,7 @@
 /****************************************************************************
  * configs/sama5d3x-ek/src/sam_userleds.c
  *
- *   Copyright (C) 2013 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2013-2014 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <gnutt@nuttx.org>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -108,7 +108,9 @@ void sam_ledinit(void)
   /* Configure LED PIOs for output */
 
   sam_configpio(PIO_BLUE);
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
   sam_configpio(PIO_RED);
+#endif
 }
 
 /****************************************************************************
@@ -126,12 +128,14 @@ void sam_setled(int led, bool ledon)
       ledcfg = PIO_BLUE;
       ledon  = !ledon;
     }
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
   else if (led == BOARD_RED)
     {
       /* High illuminates */
 
       ledcfg = PIO_RED;
     }
+#endif
   else
     {
       return;
@@ -153,10 +157,12 @@ void sam_setleds(uint8_t ledset)
   ledon = ((ledset & BOARD_BLUE_BIT) == 0);
   sam_piowrite(PIO_BLUE, ledon);
 
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
   /* High illuminates */
 
   ledon = ((ledset & BOARD_RED_BIT) != 0);
   sam_piowrite(PIO_RED, ledon);
+#endif
 }
 
 #endif /* !CONFIG_ARCH_LEDS */
diff --git a/configs/sama5d3x-ek/src/sama5d3x-ek.h b/configs/sama5d3x-ek/src/sama5d3x-ek.h
index a5611f7ede..d41e169d97 100644
--- a/configs/sama5d3x-ek/src/sama5d3x-ek.h
+++ b/configs/sama5d3x-ek/src/sama5d3x-ek.h
@@ -1,7 +1,7 @@
 /************************************************************************************
  * configs/sama5d3x-ek/src/sama5d3x-ek.h
  *
- *   Copyright (C) 2013 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2013-2014 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <gnutt@nuttx.org>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -349,12 +349,19 @@
  *   that it is illuminated when power is applied even if PE24 is not
  *   configured as an output.  If PE24 is configured as an output, then the
  *   LCD is illuminated by a high output.
+ *
+ *     N.B. PE24 Drives the RED Led on the CM (SODIMM200), but unfortunately
+ *     it is also connected to ISI_RST on the MB (Main Board) and controlling
+ *     it will reset a Camera connected to the ISI
  */
 
 #define PIO_BLUE     (PIO_OUTPUT | PIO_CFG_PULLUP | PIO_OUTPUT_SET | \
                       PIO_PORT_PIOE | PIO_PIN25)
-#define PIO_RED      (PIO_OUTPUT | PIO_CFG_PULLUP | PIO_OUTPUT_CLEAR | \
+
+#ifndef CONFIG_SAMA5D3xEK_NOREDLED
+#  define PIO_RED    (PIO_OUTPUT | PIO_CFG_PULLUP | PIO_OUTPUT_CLEAR | \
                       PIO_PORT_PIOE | PIO_PIN24)
+#endif
 
 /* Buttons **************************************************************************/
 /* There are five push button switches on the SAMA5D3X-EK base board:
-- 
GitLab