gpio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Module for interfacing with GPIO
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "lmem.h"
  5. #include "platform.h"
  6. #include "user_interface.h"
  7. #include "c_types.h"
  8. #include <string.h>
  9. #include "gpio.h"
  10. #undef INTERRUPT
  11. #define PULLUP PLATFORM_GPIO_PULLUP
  12. #define FLOAT PLATFORM_GPIO_FLOAT
  13. #define OUTPUT PLATFORM_GPIO_OUTPUT
  14. #define OPENDRAIN PLATFORM_GPIO_OPENDRAIN
  15. #define INPUT PLATFORM_GPIO_INPUT
  16. #define INTERRUPT PLATFORM_GPIO_INT
  17. #define HIGH PLATFORM_GPIO_HIGH
  18. #define LOW PLATFORM_GPIO_LOW
  19. #ifdef GPIO_INTERRUPT_ENABLE
  20. #if defined(__ESP8266__)
  21. // We also know that the non-level interrupt types are < LOLEVEL, and that
  22. // HILEVEL is > LOLEVEL. Since this is burned into the hardware it is not
  23. // going to change.
  24. # define INTERRUPT_TYPE_IS_LEVEL(x) ((x) >= GPIO_PIN_INTR_LOLEVEL)
  25. #elif defined(__ESP32__)
  26. // There appears to be no level interrupt support on the ESP32?
  27. # define INTERRUPT_TYPE_IS_LEVEL(x) 0
  28. #endif
  29. static int gpio_cb_ref[GPIO_PIN_NUM];
  30. // This task is scheduled by the ISR and is used
  31. // to initiate the Lua-land gpio.trig() callback function
  32. // It also re-enables the pin interrupt, so that we get another callback queued
  33. static void gpio_intr_callback_task (task_param_t param, task_prio_t priority)
  34. {
  35. unsigned pin = param >> 1;
  36. unsigned level = param & 1;
  37. UNUSED(priority);
  38. NODE_DBG("pin:%d, level:%d \n", pin, level);
  39. if(gpio_cb_ref[pin] != LUA_NOREF) {
  40. // GPIO callbacks are run in L0 and inlcude the level as a parameter
  41. lua_State *L = lua_getstate();
  42. NODE_DBG("Calling: %08x\n", gpio_cb_ref[pin]);
  43. //
  44. if (!INTERRUPT_TYPE_IS_LEVEL(pin_int_type[pin])) {
  45. // Edge triggered -- re-enable the interrupt
  46. platform_gpio_intr_init(pin, pin_int_type[pin]);
  47. }
  48. // Do the actual callback
  49. lua_rawgeti(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]);
  50. lua_pushinteger(L, level);
  51. lua_call(L, 1, 0);
  52. if (INTERRUPT_TYPE_IS_LEVEL(pin_int_type[pin])) {
  53. // Level triggered -- re-enable the callback
  54. platform_gpio_intr_init(pin, pin_int_type[pin]);
  55. }
  56. }
  57. }
  58. // Lua: trig( pin, type, function )
  59. static int lgpio_trig( lua_State* L )
  60. {
  61. unsigned pin = luaL_checkinteger( L, 1 );
  62. static const char * const opts[] = {"none", "up", "down", "both",
  63. #ifdef __ESP8266__
  64. "low", "high",
  65. #endif
  66. NULL};
  67. static const int opts_type[] = {
  68. GPIO_PIN_INTR_DISABLE, GPIO_PIN_INTR_POSEDGE, GPIO_PIN_INTR_NEGEDGE,
  69. GPIO_PIN_INTR_ANYEDGE
  70. #ifdef __ESP8266__
  71. ,GPIO_PIN_INTR_LOLEVEL, GPIO_PIN_INTR_HILEVEL
  72. #endif
  73. };
  74. luaL_argcheck(L, platform_gpio_exists(pin) && pin>0, 1, "Invalid interrupt pin");
  75. int old_pin_ref = gpio_cb_ref[pin];
  76. int type = opts_type[luaL_checkoption(L, 2, "none", opts)];
  77. if (type == GPIO_PIN_INTR_DISABLE) {
  78. // "none" clears the callback
  79. gpio_cb_ref[pin] = LUA_NOREF;
  80. } else if (lua_gettop(L)==2 && old_pin_ref != LUA_NOREF) {
  81. // keep the old one if no callback
  82. old_pin_ref = LUA_NOREF;
  83. } else if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
  84. // set up the new callback if present
  85. lua_pushvalue(L, 3);
  86. gpio_cb_ref[pin] = luaL_ref(L, LUA_REGISTRYINDEX);
  87. } else {
  88. // invalid combination, so clear down any old callback and throw an error
  89. if(old_pin_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, old_pin_ref);
  90. luaL_argcheck(L, 0, 3, "invalid callback type");
  91. }
  92. // unreference any overwritten callback
  93. if(old_pin_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, old_pin_ref);
  94. NODE_DBG("Pin data: %d %d %08x, %d %d %d, %08x\n",
  95. pin, type, pin_mux[pin], pin_num[pin], pin_func[pin], pin_int_type[pin], gpio_cb_ref[pin]);
  96. platform_gpio_intr_init(pin, type);
  97. return 0;
  98. }
  99. #endif
  100. // Lua: mode( pin, mode, pullup )
  101. static int lgpio_mode( lua_State* L )
  102. {
  103. unsigned pin = luaL_checkinteger( L, 1 );
  104. unsigned mode = luaL_checkinteger( L, 2 );
  105. unsigned pullup = luaL_optinteger( L, 3, FLOAT );
  106. luaL_argcheck(L, platform_gpio_exists(pin) && (mode!=INTERRUPT || pin>0), 1, "Invalid pin");
  107. luaL_argcheck(L, mode==OUTPUT || mode==OPENDRAIN || mode==INPUT
  108. #ifdef GPIO_INTERRUPT_ENABLE
  109. || mode==INTERRUPT
  110. #endif
  111. , 2, "wrong arg type" );
  112. if(pullup!=FLOAT) pullup = PULLUP;
  113. NODE_DBG("pin,mode,pullup= %d %d %d\n",pin,mode,pullup);
  114. NODE_DBG("Pin data at mode: %d %08x, %d %d %d, %08x\n",
  115. pin, pin_mux[pin], pin_num[pin], pin_func[pin], pin_int_type[pin], gpio_cb_ref[pin]);
  116. #ifdef GPIO_INTERRUPT_ENABLE
  117. if (mode != INTERRUPT){ // disable interrupt
  118. if(gpio_cb_ref[pin] != LUA_NOREF){
  119. luaL_unref(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]);
  120. gpio_cb_ref[pin] = LUA_NOREF;
  121. }
  122. }
  123. #endif
  124. if( platform_gpio_mode( pin, mode, pullup ) < 0 )
  125. return luaL_error( L, "wrong pin num." );
  126. return 0;
  127. }
  128. // Lua: read( pin )
  129. static int lgpio_read( lua_State* L )
  130. {
  131. unsigned pin = luaL_checkinteger( L, 1 );
  132. MOD_CHECK_ID( gpio, pin );
  133. lua_pushinteger( L, platform_gpio_read( pin ) );
  134. return 1;
  135. }
  136. // Lua: write( pin, level )
  137. static int lgpio_write( lua_State* L )
  138. {
  139. unsigned pin = luaL_checkinteger( L, 1 );
  140. unsigned level = luaL_checkinteger( L, 2 );
  141. MOD_CHECK_ID( gpio, pin );
  142. luaL_argcheck(L, level==HIGH || level==LOW, 2, "wrong level type" );
  143. platform_gpio_write(pin, level);
  144. return 0;
  145. }
  146. #define DELAY_TABLE_MAX_LEN 256
  147. #define delayMicroseconds os_delay_us
  148. // Lua: serout( pin, firstLevel, delay_table, [repeatNum] )
  149. // -- serout( pin, firstLevel, delay_table, [repeatNum] )
  150. // gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  151. // gpio.serout(1,1,{30,30,60,60,30,30}) -- serial one byte, b10110010
  152. // gpio.serout(1,1,{30,70},8) -- serial 30% pwm 10k, lasts 8 cycles
  153. // gpio.serout(1,1,{3,7},8) -- serial 30% pwm 100k, lasts 8 cycles
  154. // gpio.serout(1,1,{0,0},8) -- serial 50% pwm as fast as possible, lasts 8 cycles
  155. // gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  156. // gpio.serout(1,0,{20,10,10,20,10,10,10,100}) -- sim uart one byte 0x5A at about 100kbps
  157. // gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
  158. static int lgpio_serout( lua_State* L )
  159. {
  160. unsigned clocks_per_us = system_get_cpu_freq();
  161. unsigned pin = luaL_checkinteger( L, 1 );
  162. unsigned level = luaL_checkinteger( L, 2 );
  163. unsigned repeats = luaL_optint( L, 4, 1 );
  164. unsigned table_len, i, j;
  165. luaL_argcheck(L, platform_gpio_exists(pin), 1, "Invalid pin");
  166. luaL_argcheck(L, level==HIGH || level==LOW, 2, "Wrong arg type" );
  167. luaL_argcheck(L, lua_istable( L, 3 ) &&
  168. ((table_len = lua_objlen( L, 3 )<DELAY_TABLE_MAX_LEN)), 3, "Invalid table" );
  169. luaL_argcheck(L, repeats<256, 4, "repeats >= 256" );
  170. uint32 *delay_table = luaM_newvector(L, table_len*repeats, uint32);
  171. for( i = 1; i <= table_len; i++ ) {
  172. lua_rawgeti( L, 3, i + 1 );
  173. unsigned delay = (unsigned) luaL_checkinteger( L, -1 );
  174. if (delay > 1000000) return luaL_error( L, "delay %u must be < 1,000,000 us", i );
  175. delay_table[i-1] = delay;
  176. lua_pop( L, 1 );
  177. }
  178. for( i = 0; i <= repeats; i++ ) {
  179. if (!i) // skip the first loop (presumably this is some form of icache priming??).
  180. continue;
  181. for( j = 0;j < table_len; j++ ){
  182. /* Direct Write is a ROM function which already disables interrupts for the atomic bit */
  183. GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), level);
  184. delayMicroseconds(delay_table[j]);
  185. level = level==LOW ? HIGH : LOW;
  186. }
  187. }
  188. luaM_freearray(L, delay_table, table_len, uint32);
  189. return 0;
  190. }
  191. #undef DELAY_TABLE_MAX_LEN
  192. // Module function map
  193. static const LUA_REG_TYPE gpio_map[] = {
  194. { LSTRKEY( "mode" ), LFUNCVAL( lgpio_mode ) },
  195. { LSTRKEY( "read" ), LFUNCVAL( lgpio_read ) },
  196. { LSTRKEY( "write" ), LFUNCVAL( lgpio_write ) },
  197. { LSTRKEY( "serout" ), LFUNCVAL( lgpio_serout ) },
  198. #ifdef GPIO_INTERRUPT_ENABLE
  199. { LSTRKEY( "trig" ), LFUNCVAL( lgpio_trig ) },
  200. { LSTRKEY( "INT" ), LNUMVAL( INTERRUPT ) },
  201. #endif
  202. { LSTRKEY( "OUTPUT" ), LNUMVAL( OUTPUT ) },
  203. { LSTRKEY( "OPENDRAIN" ), LNUMVAL( OPENDRAIN ) },
  204. { LSTRKEY( "INPUT" ), LNUMVAL( INPUT ) },
  205. { LSTRKEY( "HIGH" ), LNUMVAL( HIGH ) },
  206. { LSTRKEY( "LOW" ), LNUMVAL( LOW ) },
  207. { LSTRKEY( "FLOAT" ), LNUMVAL( FLOAT ) },
  208. { LSTRKEY( "PULLUP" ), LNUMVAL( PULLUP ) },
  209. { LNILKEY, LNILVAL }
  210. };
  211. int luaopen_gpio( lua_State *L ) {
  212. #ifdef GPIO_INTERRUPT_ENABLE
  213. int i;
  214. for(i=0;i<GPIO_PIN_NUM;i++){
  215. gpio_cb_ref[i] = LUA_NOREF;
  216. }
  217. platform_gpio_init(task_get_id(gpio_intr_callback_task));
  218. #endif
  219. return 0;
  220. }
  221. NODEMCU_MODULE(GPIO, "gpio", gpio_map, luaopen_gpio);