As the title says... Chat on...

User avatar
By Markus Gritsch
#9572 Hi,

I tried to get PULLDOWN on GPIO pins working, but I am failing so far. I see, that platform.c already contains the necessary code:
Code: Select all    case PLATFORM_GPIO_PULLDOWN:
      PIN_PULLUP_DIS(pin_mux[pin]);
      PIN_PULLDWN_EN(pin_mux[pin]);
      break;

so I made the following changes to gpio.c, to make it available from Lua:
Code: Select all--- gpio.c_orig   2015-02-05 19:04:09.000000000 +0100
+++ gpio.c   2015-02-11 22:42:07.297420800 +0100
@@ -10,8 +10,9 @@
 #include "c_types.h"
 #include "c_string.h"
 
-#define PULLUP PLATFORM_GPIO_PULLUP
 #define FLOAT PLATFORM_GPIO_FLOAT
+#define PULLUP PLATFORM_GPIO_PULLUP
+#define PULLDOWN PLATFORM_GPIO_PULLDOWN
 #define OUTPUT PLATFORM_GPIO_OUTPUT
 #define INPUT PLATFORM_GPIO_INPUT
 #define INTERRUPT PLATFORM_GPIO_INT
@@ -86,10 +87,11 @@
 }
 #endif
 
-// Lua: mode( pin, mode, pullup )
+// Lua: mode( pin, mode, pull )
 static int lgpio_mode( lua_State* L )
 {
-  unsigned mode, pullup = FLOAT;
+  unsigned mode;
+  unsigned pull = FLOAT;
   unsigned pin;
 
   pin = luaL_checkinteger( L, 1 );
@@ -100,9 +102,7 @@
   if(pin==0 && mode==INTERRUPT)
     return luaL_error( L, "no interrupt for D0" );
   if(lua_isnumber(L, 3))
-    pullup = lua_tointeger( L, 3 );
-  if(pullup!=FLOAT)
-    pullup = PULLUP;
+    pull = lua_tointeger( L, 3 );
 #ifdef GPIO_INTERRUPT_ENABLE
   gL = L;   // save to local gL, for callback function
   if (mode!=INTERRUPT){     // disable interrupt
@@ -112,7 +112,7 @@
     gpio_cb_ref[pin] = LUA_NOREF;
   }
 #endif
-  int r = platform_gpio_mode( pin, mode, pullup );
+  int r = platform_gpio_mode( pin, mode, pull );
   if( r<0 )
     return luaL_error( L, "wrong pin num." );
   return 0; 
@@ -167,6 +167,7 @@
   { LSTRKEY( "LOW" ), LNUMVAL( LOW ) },
   { LSTRKEY( "FLOAT" ), LNUMVAL( FLOAT ) },
   { LSTRKEY( "PULLUP" ), LNUMVAL( PULLUP ) },
+  { LSTRKEY( "PULLDOWN" ), LNUMVAL( PULLDOWN ) },
 #endif
   { LNILKEY, LNILVAL }
 };
@@ -195,6 +196,7 @@
   MOD_REG_NUMBER( L, "LOW", LOW );
   MOD_REG_NUMBER( L, "FLOAT", FLOAT );
   MOD_REG_NUMBER( L, "PULLUP", PULLUP );
+  MOD_REG_NUMBER( L, "PULLDOWN", PULLDOWN );
   return 1;
 #endif // #if LUA_OPTIMIZE_MEMORY > 0 
 }

Now when calling
Code: Select allgpio.mode(4, gpio.INPUT, gpio.PULLDOWN)

I am sure the PLATFORM_GPIO_PULLDOWN case in platform.c is executed (added a c_printf() call there to check), but the result on the pin is the same as with PULLUP.

Any hints?