gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Module for interfacing with GPIO
  2. //#define NODE_DEBUG
  3. #include "module.h"
  4. #include "lauxlib.h"
  5. #include "lmem.h"
  6. #include "platform.h"
  7. #include "user_interface.h"
  8. #include "c_types.h"
  9. #include "c_string.h"
  10. #include "gpio.h"
  11. #include "hw_timer.h"
  12. #define PULLUP PLATFORM_GPIO_PULLUP
  13. #define FLOAT PLATFORM_GPIO_FLOAT
  14. #define OUTPUT PLATFORM_GPIO_OUTPUT
  15. #define OPENDRAIN PLATFORM_GPIO_OPENDRAIN
  16. #define INPUT PLATFORM_GPIO_INPUT
  17. #define INTERRUPT PLATFORM_GPIO_INT
  18. #define HIGH PLATFORM_GPIO_HIGH
  19. #define LOW PLATFORM_GPIO_LOW
  20. #ifdef GPIO_INTERRUPT_ENABLE
  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. static int gpio_cb_ref[GPIO_PIN_NUM];
  26. // This task is scheduled by the ISR and is used
  27. // to initiate the Lua-land gpio.trig() callback function
  28. // It also re-enables the pin interrupt, so that we get another callback queued
  29. static void gpio_intr_callback_task (task_param_t param, uint8 priority)
  30. {
  31. uint32_t then = (param >> 8) & 0xffffff;
  32. uint32_t pin = (param >> 1) & 127;
  33. uint32_t level = param & 1;
  34. UNUSED(priority);
  35. uint32_t now = system_get_time();
  36. // Now must be >= then . Add the missing bits
  37. if (then > (now & 0xffffff)) {
  38. // Now must have rolled over since the interrupt -- back it down
  39. now -= 0x1000000;
  40. }
  41. then = (then + (now & 0x7f000000)) & 0x7fffffff;
  42. NODE_DBG("pin:%d, level:%d \n", pin, level);
  43. if(gpio_cb_ref[pin] != LUA_NOREF) {
  44. // GPIO callbacks are run in L0 and include the level as a parameter
  45. lua_State *L = lua_getstate();
  46. NODE_DBG("Calling: %08x\n", gpio_cb_ref[pin]);
  47. //
  48. if (!INTERRUPT_TYPE_IS_LEVEL(pin_int_type[pin])) {
  49. // Edge triggered -- re-enable the interrupt
  50. platform_gpio_intr_init(pin, pin_int_type[pin]);
  51. }
  52. // Do the actual callback
  53. lua_rawgeti(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]);
  54. lua_pushinteger(L, level);
  55. lua_pushinteger(L, then);
  56. lua_call(L, 2, 0);
  57. if (INTERRUPT_TYPE_IS_LEVEL(pin_int_type[pin])) {
  58. // Level triggered -- re-enable the callback
  59. platform_gpio_intr_init(pin, pin_int_type[pin]);
  60. }
  61. }
  62. }
  63. // Lua: trig( pin, type, function )
  64. static int lgpio_trig( lua_State* L )
  65. {
  66. unsigned pin = luaL_checkinteger( L, 1 );
  67. static const char * const opts[] = {"none", "up", "down", "both", "low", "high", NULL};
  68. static const int opts_type[] = {
  69. GPIO_PIN_INTR_DISABLE, GPIO_PIN_INTR_POSEDGE, GPIO_PIN_INTR_NEGEDGE,
  70. GPIO_PIN_INTR_ANYEDGE, GPIO_PIN_INTR_LOLEVEL, GPIO_PIN_INTR_HILEVEL
  71. };
  72. luaL_argcheck(L, platform_gpio_exists(pin) && pin>0, 1, "Invalid interrupt pin");
  73. int old_pin_ref = gpio_cb_ref[pin];
  74. int type = opts_type[luaL_checkoption(L, 2, "none", opts)];
  75. if (type == GPIO_PIN_INTR_DISABLE) {
  76. // "none" clears the callback
  77. gpio_cb_ref[pin] = LUA_NOREF;
  78. } else if (lua_gettop(L)==2 && old_pin_ref != LUA_NOREF) {
  79. // keep the old one if no callback
  80. old_pin_ref = LUA_NOREF;
  81. } else if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
  82. // set up the new callback if present
  83. lua_pushvalue(L, 3);
  84. gpio_cb_ref[pin] = luaL_ref(L, LUA_REGISTRYINDEX);
  85. } else {
  86. // invalid combination, so clear down any old callback and throw an error
  87. if(old_pin_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, old_pin_ref);
  88. luaL_argcheck(L, 0, 3, "invalid callback type");
  89. }
  90. // unreference any overwritten callback
  91. if(old_pin_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, old_pin_ref);
  92. NODE_DBG("Pin data: %d %d %08x, %d %d %d, %08x\n",
  93. pin, type, pin_mux[pin], pin_num[pin], pin_func[pin], pin_int_type[pin], gpio_cb_ref[pin]);
  94. platform_gpio_intr_init(pin, type);
  95. return 0;
  96. }
  97. #endif
  98. // Lua: mode( pin, mode, pullup )
  99. static int lgpio_mode( lua_State* L )
  100. {
  101. unsigned pin = luaL_checkinteger( L, 1 );
  102. unsigned mode = luaL_checkinteger( L, 2 );
  103. unsigned pullup = luaL_optinteger( L, 3, FLOAT );
  104. luaL_argcheck(L, platform_gpio_exists(pin) && (mode!=INTERRUPT || pin>0), 1, "Invalid pin");
  105. luaL_argcheck(L, mode==OUTPUT || mode==OPENDRAIN || mode==INPUT
  106. #ifdef GPIO_INTERRUPT_ENABLE
  107. || mode==INTERRUPT
  108. #endif
  109. , 2, "wrong arg type" );
  110. if(pullup!=FLOAT) pullup = PULLUP;
  111. NODE_DBG("pin,mode,pullup= %d %d %d\n",pin,mode,pullup);
  112. NODE_DBG("Pin data at mode: %d %08x, %d %d %d, %08x\n",
  113. pin, pin_mux[pin], pin_num[pin], pin_func[pin], pin_int_type[pin], gpio_cb_ref[pin]);
  114. #ifdef GPIO_INTERRUPT_ENABLE
  115. if (mode != INTERRUPT){ // disable interrupt
  116. if(gpio_cb_ref[pin] != LUA_NOREF){
  117. luaL_unref(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]);
  118. gpio_cb_ref[pin] = LUA_NOREF;
  119. }
  120. }
  121. #endif
  122. if( platform_gpio_mode( pin, mode, pullup ) < 0 )
  123. return luaL_error( L, "wrong pin num." );
  124. return 0;
  125. }
  126. // Lua: read( pin )
  127. static int lgpio_read( lua_State* L )
  128. {
  129. unsigned pin = luaL_checkinteger( L, 1 );
  130. MOD_CHECK_ID( gpio, pin );
  131. lua_pushinteger( L, platform_gpio_read( pin ) );
  132. return 1;
  133. }
  134. // Lua: write( pin, level )
  135. static int lgpio_write( lua_State* L )
  136. {
  137. unsigned pin = luaL_checkinteger( L, 1 );
  138. unsigned level = luaL_checkinteger( L, 2 );
  139. MOD_CHECK_ID( gpio, pin );
  140. luaL_argcheck(L, level==HIGH || level==LOW, 2, "wrong level type" );
  141. platform_gpio_write(pin, level);
  142. return 0;
  143. }
  144. #define DELAY_TABLE_MAX_LEN 256
  145. #define delayMicroseconds os_delay_us
  146. // Lua: serout( pin, firstLevel, delay_table[, repeat_num[, callback]])
  147. // gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  148. // gpio.serout(1,1,{30,30,60,60,30,30}) -- serial one byte, b10110010
  149. // gpio.serout(1,1,{30,70},8) -- serial 30% pwm 10k, lasts 8 cycles
  150. // gpio.serout(1,1,{3,7},8) -- serial 30% pwm 100k, lasts 8 cycles
  151. // gpio.serout(1,1,{0,0},8) -- serial 50% pwm as fast as possible, lasts 8 cycles
  152. // gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  153. // gpio.serout(1,0,{20,10,10,20,10,10,10,100}) -- sim uart one byte 0x5A at about 100kbps
  154. // gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
  155. typedef struct
  156. {
  157. unsigned pin;
  158. unsigned level;
  159. uint32 index;
  160. uint32 repeats;
  161. uint32 *delay_table;
  162. uint32 tablelen;
  163. task_handle_t done_taskid;
  164. int lua_done_ref; // callback when transmission is done
  165. } serout_t;
  166. static serout_t serout;
  167. static const os_param_t TIMER_OWNER = 0x6770696f; // "gpio"
  168. static void seroutasync_done (task_param_t arg)
  169. {
  170. lua_State *L = lua_getstate();
  171. luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
  172. serout.delay_table = NULL;
  173. if (serout.lua_done_ref != LUA_NOREF) {
  174. lua_rawgeti (L, LUA_REGISTRYINDEX, serout.lua_done_ref);
  175. luaL_unref (L, LUA_REGISTRYINDEX, serout.lua_done_ref);
  176. serout.lua_done_ref = LUA_NOREF;
  177. if (lua_pcall(L, 0, 0, 0)) {
  178. // Uncaught Error. Print instead of sudden reset
  179. luaL_error(L, "error: %s", lua_tostring(L, -1));
  180. }
  181. }
  182. }
  183. static void ICACHE_RAM_ATTR seroutasync_cb(os_param_t p) {
  184. (void) p;
  185. if (serout.index < serout.tablelen) {
  186. GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[serout.pin]), serout.level);
  187. serout.level = serout.level==LOW ? HIGH : LOW;
  188. platform_hw_timer_arm_us(TIMER_OWNER, serout.delay_table[serout.index]);
  189. serout.index++;
  190. if (serout.repeats && serout.index>=serout.tablelen) {serout.index=0; serout.repeats--;}
  191. } else {
  192. platform_hw_timer_close(TIMER_OWNER);
  193. task_post_low (serout.done_taskid, (task_param_t)0);
  194. }
  195. }
  196. static int lgpio_serout( lua_State* L )
  197. {
  198. serout.pin = luaL_checkinteger( L, 1 );
  199. serout.level = luaL_checkinteger( L, 2 );
  200. serout.repeats = luaL_optint( L, 4, 1 )-1;
  201. luaL_unref (L, LUA_REGISTRYINDEX, serout.lua_done_ref);
  202. uint8_t is_async = FALSE;
  203. if (!lua_isnoneornil(L, 5)) {
  204. if (lua_isnumber(L, 5)) {
  205. serout.lua_done_ref = LUA_NOREF;
  206. } else {
  207. lua_pushvalue(L, 5);
  208. serout.lua_done_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  209. }
  210. is_async = TRUE;
  211. } else {
  212. serout.lua_done_ref = LUA_NOREF;
  213. }
  214. if (serout.delay_table) {
  215. luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
  216. serout.delay_table = NULL;
  217. }
  218. luaL_argcheck(L, platform_gpio_exists(serout.pin), 1, "Invalid pin");
  219. luaL_argcheck(L, serout.level==HIGH || serout.level==LOW, 2, "Wrong level type" );
  220. luaL_argcheck(L, lua_istable( L, 3 ) &&
  221. ((serout.tablelen = lua_objlen( L, 3 )) < DELAY_TABLE_MAX_LEN), 3, "Invalid delay_times" );
  222. serout.delay_table = luaM_newvector(L, serout.tablelen, uint32);
  223. for(unsigned i = 0; i < serout.tablelen; i++ ) {
  224. lua_rawgeti( L, 3, i + 1 );
  225. unsigned delay = (unsigned) luaL_checkinteger( L, -1 );
  226. serout.delay_table[i] = delay;
  227. lua_pop( L, 1 );
  228. }
  229. if (is_async) { // async version for duration above 15 mSec
  230. if (!platform_hw_timer_init(TIMER_OWNER, FRC1_SOURCE, TRUE)) {
  231. // Failed to init the timer
  232. luaL_error(L, "Unable to initialize timer");
  233. }
  234. platform_hw_timer_set_func(TIMER_OWNER, seroutasync_cb, 0);
  235. serout.index = 0;
  236. seroutasync_cb(0);
  237. } else { // sync version for sub-50 µs resolution & total duration < 15 mSec
  238. do {
  239. for( serout.index = 0;serout.index < serout.tablelen; serout.index++ ){
  240. NODE_DBG("%d\t%d\t%d\t%d\t%d\t%d\t%d\n", serout.repeats, serout.index, serout.level, serout.pin, serout.tablelen, serout.delay_table[serout.index], system_get_time()); // timings is delayed for short timings when debug output is enabled
  241. GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[serout.pin]), serout.level);
  242. serout.level = serout.level==LOW ? HIGH : LOW;
  243. delayMicroseconds(serout.delay_table[serout.index]);
  244. }
  245. } while (serout.repeats--);
  246. luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
  247. }
  248. return 0;
  249. }
  250. #undef DELAY_TABLE_MAX_LEN
  251. // Module function map
  252. static const LUA_REG_TYPE gpio_map[] = {
  253. { LSTRKEY( "mode" ), LFUNCVAL( lgpio_mode ) },
  254. { LSTRKEY( "read" ), LFUNCVAL( lgpio_read ) },
  255. { LSTRKEY( "write" ), LFUNCVAL( lgpio_write ) },
  256. { LSTRKEY( "serout" ), LFUNCVAL( lgpio_serout ) },
  257. #ifdef GPIO_INTERRUPT_ENABLE
  258. { LSTRKEY( "trig" ), LFUNCVAL( lgpio_trig ) },
  259. { LSTRKEY( "INT" ), LNUMVAL( INTERRUPT ) },
  260. #endif
  261. { LSTRKEY( "OUTPUT" ), LNUMVAL( OUTPUT ) },
  262. { LSTRKEY( "OPENDRAIN" ), LNUMVAL( OPENDRAIN ) },
  263. { LSTRKEY( "INPUT" ), LNUMVAL( INPUT ) },
  264. { LSTRKEY( "HIGH" ), LNUMVAL( HIGH ) },
  265. { LSTRKEY( "LOW" ), LNUMVAL( LOW ) },
  266. { LSTRKEY( "FLOAT" ), LNUMVAL( FLOAT ) },
  267. { LSTRKEY( "PULLUP" ), LNUMVAL( PULLUP ) },
  268. { LNILKEY, LNILVAL }
  269. };
  270. int luaopen_gpio( lua_State *L ) {
  271. #ifdef GPIO_INTERRUPT_ENABLE
  272. int i;
  273. for(i=0;i<GPIO_PIN_NUM;i++){
  274. gpio_cb_ref[i] = LUA_NOREF;
  275. }
  276. platform_gpio_init(task_get_id(gpio_intr_callback_task));
  277. #endif
  278. serout.done_taskid = task_get_id((task_callback_t) seroutasync_done);
  279. serout.lua_done_ref = LUA_NOREF;
  280. return 0;
  281. }
  282. NODEMCU_MODULE(GPIO, "gpio", gpio_map, luaopen_gpio);