gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <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. bool needs_callback = 1;
  48. while (needs_callback) {
  49. // Note that the interrupt level only modifies 'seen' and
  50. // the base level only modifies 'reported'.
  51. // Do the actual callback
  52. lua_rawgeti(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]);
  53. lua_pushinteger(L, level);
  54. lua_pushinteger(L, then);
  55. uint16_t seen = pin_counter[pin].seen;
  56. lua_pushinteger(L, 0x7fff & (seen - pin_counter[pin].reported));
  57. pin_counter[pin].reported = seen & 0x7fff; // This will cause the next interrupt to trigger a callback
  58. uint16_t diff = (seen ^ pin_counter[pin].seen);
  59. // Needs another callback if seen changed but not if the top bit is set
  60. needs_callback = diff <= 0x7fff && diff > 0;
  61. if (needs_callback) {
  62. // Fake this for next time (this only happens if another interrupt happens since
  63. // we loaded the 'seen' variable.
  64. then = system_get_time() & 0x7fffffff;
  65. }
  66. lua_call(L, 3, 0);
  67. }
  68. if (INTERRUPT_TYPE_IS_LEVEL(pin_int_type[pin])) {
  69. // Level triggered -- re-enable the callback
  70. platform_gpio_intr_init(pin, pin_int_type[pin]);
  71. }
  72. }
  73. }
  74. // Lua: trig( pin, type, function )
  75. static int lgpio_trig( lua_State* L )
  76. {
  77. unsigned pin = luaL_checkinteger( L, 1 );
  78. static const char * const opts[] = {"none", "up", "down", "both", "low", "high", NULL};
  79. static const int opts_type[] = {
  80. GPIO_PIN_INTR_DISABLE, GPIO_PIN_INTR_POSEDGE, GPIO_PIN_INTR_NEGEDGE,
  81. GPIO_PIN_INTR_ANYEDGE, GPIO_PIN_INTR_LOLEVEL, GPIO_PIN_INTR_HILEVEL
  82. };
  83. luaL_argcheck(L, platform_gpio_exists(pin) && pin>0, 1, "Invalid interrupt pin");
  84. int old_pin_ref = gpio_cb_ref[pin];
  85. int type = opts_type[luaL_checkoption(L, 2, "none", opts)];
  86. if (type == GPIO_PIN_INTR_DISABLE) {
  87. // "none" clears the callback
  88. gpio_cb_ref[pin] = LUA_NOREF;
  89. } else if (lua_gettop(L)==2 && old_pin_ref != LUA_NOREF) {
  90. // keep the old one if no callback
  91. old_pin_ref = LUA_NOREF;
  92. } else if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
  93. // set up the new callback if present
  94. lua_pushvalue(L, 3);
  95. gpio_cb_ref[pin] = luaL_ref(L, LUA_REGISTRYINDEX);
  96. } else {
  97. // invalid combination, so clear down any old callback and throw an error
  98. if(old_pin_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, old_pin_ref);
  99. luaL_argcheck(L, 0, 3, "invalid callback type");
  100. }
  101. // unreference any overwritten callback
  102. if(old_pin_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, old_pin_ref);
  103. uint16_t seen;
  104. // Make sure that we clear out any queued interrupts
  105. do {
  106. seen = pin_counter[pin].seen;
  107. pin_counter[pin].reported = seen & 0x7fff;
  108. } while (seen != pin_counter[pin].seen);
  109. NODE_DBG("Pin data: %d %d %08x, %d %d %d, %08x\n",
  110. pin, type, pin_mux[pin], pin_num[pin], pin_func[pin], pin_int_type[pin], gpio_cb_ref[pin]);
  111. platform_gpio_intr_init(pin, type);
  112. return 0;
  113. }
  114. #endif
  115. // Lua: mode( pin, mode, pullup )
  116. static int lgpio_mode( lua_State* L )
  117. {
  118. unsigned pin = luaL_checkinteger( L, 1 );
  119. unsigned mode = luaL_checkinteger( L, 2 );
  120. unsigned pullup = luaL_optinteger( L, 3, FLOAT );
  121. luaL_argcheck(L, platform_gpio_exists(pin) && (mode!=INTERRUPT || pin>0), 1, "Invalid pin");
  122. luaL_argcheck(L, mode==OUTPUT || mode==OPENDRAIN || mode==INPUT
  123. #ifdef GPIO_INTERRUPT_ENABLE
  124. || mode==INTERRUPT
  125. #endif
  126. , 2, "wrong arg type" );
  127. if(pullup!=FLOAT) pullup = PULLUP;
  128. NODE_DBG("pin,mode,pullup= %d %d %d\n",pin,mode,pullup);
  129. NODE_DBG("Pin data at mode: %d %08x, %d %d %d, %08x\n",
  130. pin, pin_mux[pin], pin_num[pin], pin_func[pin],
  131. #ifdef GPIO_INTERRUPT_ENABLE
  132. pin_int_type[pin], gpio_cb_ref[pin]
  133. #else
  134. 0, 0
  135. #endif
  136. );
  137. #ifdef GPIO_INTERRUPT_ENABLE
  138. if (mode != INTERRUPT){ // disable interrupt
  139. if(gpio_cb_ref[pin] != LUA_NOREF){
  140. luaL_unref(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]);
  141. gpio_cb_ref[pin] = LUA_NOREF;
  142. }
  143. }
  144. #endif
  145. if( platform_gpio_mode( pin, mode, pullup ) < 0 )
  146. return luaL_error( L, "wrong pin num." );
  147. return 0;
  148. }
  149. // Lua: read( pin )
  150. static int lgpio_read( lua_State* L )
  151. {
  152. unsigned pin = luaL_checkinteger( L, 1 );
  153. MOD_CHECK_ID( gpio, pin );
  154. lua_pushinteger( L, platform_gpio_read( pin ) );
  155. return 1;
  156. }
  157. // Lua: write( pin, level )
  158. static int lgpio_write( lua_State* L )
  159. {
  160. unsigned pin = luaL_checkinteger( L, 1 );
  161. unsigned level = luaL_checkinteger( L, 2 );
  162. MOD_CHECK_ID( gpio, pin );
  163. luaL_argcheck(L, level==HIGH || level==LOW, 2, "wrong level type" );
  164. platform_gpio_write(pin, level);
  165. return 0;
  166. }
  167. #define DELAY_TABLE_MAX_LEN 256
  168. #define delayMicroseconds os_delay_us
  169. // Lua: serout( pin, firstLevel, delay_table[, repeat_num[, callback]])
  170. // gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  171. // gpio.serout(1,1,{30,30,60,60,30,30}) -- serial one byte, b10110010
  172. // gpio.serout(1,1,{30,70},8) -- serial 30% pwm 10k, lasts 8 cycles
  173. // gpio.serout(1,1,{3,7},8) -- serial 30% pwm 100k, lasts 8 cycles
  174. // gpio.serout(1,1,{0,0},8) -- serial 50% pwm as fast as possible, lasts 8 cycles
  175. // gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  176. // gpio.serout(1,0,{20,10,10,20,10,10,10,100}) -- sim uart one byte 0x5A at about 100kbps
  177. // gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
  178. typedef struct
  179. {
  180. unsigned pin;
  181. unsigned level;
  182. uint32 index;
  183. uint32 repeats;
  184. uint32 *delay_table;
  185. uint32 tablelen;
  186. task_handle_t done_taskid;
  187. int lua_done_ref; // callback when transmission is done
  188. } serout_t;
  189. static serout_t serout;
  190. static const os_param_t TIMER_OWNER = 0x6770696f; // "gpio"
  191. static void seroutasync_done (task_param_t arg)
  192. {
  193. lua_State *L = lua_getstate();
  194. if (serout.delay_table) {
  195. luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
  196. serout.delay_table = NULL;
  197. }
  198. if (serout.lua_done_ref != LUA_NOREF) {
  199. lua_rawgeti (L, LUA_REGISTRYINDEX, serout.lua_done_ref);
  200. luaL_unref (L, LUA_REGISTRYINDEX, serout.lua_done_ref);
  201. serout.lua_done_ref = LUA_NOREF;
  202. if (lua_pcall(L, 0, 0, 0)) {
  203. // Uncaught Error. Print instead of sudden reset
  204. luaL_error(L, "error: %s", lua_tostring(L, -1));
  205. }
  206. }
  207. }
  208. static void ICACHE_RAM_ATTR seroutasync_cb(os_param_t p) {
  209. (void) p;
  210. if (serout.index < serout.tablelen) {
  211. GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[serout.pin]), serout.level);
  212. serout.level = serout.level==LOW ? HIGH : LOW;
  213. platform_hw_timer_arm_us(TIMER_OWNER, serout.delay_table[serout.index]);
  214. serout.index++;
  215. if (serout.repeats && serout.index>=serout.tablelen) {serout.index=0; serout.repeats--;}
  216. } else {
  217. platform_hw_timer_close(TIMER_OWNER);
  218. task_post_low (serout.done_taskid, (task_param_t)0);
  219. }
  220. }
  221. static int lgpio_serout( lua_State* L )
  222. {
  223. serout.pin = luaL_checkinteger( L, 1 );
  224. serout.level = luaL_checkinteger( L, 2 );
  225. serout.repeats = luaL_optint( L, 4, 1 )-1;
  226. luaL_unref (L, LUA_REGISTRYINDEX, serout.lua_done_ref);
  227. uint8_t is_async = FALSE;
  228. if (!lua_isnoneornil(L, 5)) {
  229. if (lua_isnumber(L, 5)) {
  230. serout.lua_done_ref = LUA_NOREF;
  231. } else {
  232. lua_pushvalue(L, 5);
  233. serout.lua_done_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  234. }
  235. is_async = TRUE;
  236. } else {
  237. serout.lua_done_ref = LUA_NOREF;
  238. }
  239. if (serout.delay_table) {
  240. luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
  241. serout.delay_table = NULL;
  242. }
  243. luaL_argcheck(L, platform_gpio_exists(serout.pin), 1, "Invalid pin");
  244. luaL_argcheck(L, serout.level==HIGH || serout.level==LOW, 2, "Wrong level type" );
  245. luaL_argcheck(L, lua_istable( L, 3 ) &&
  246. ((serout.tablelen = lua_objlen( L, 3 )) < DELAY_TABLE_MAX_LEN), 3, "Invalid delay_times" );
  247. serout.delay_table = luaM_newvector(L, serout.tablelen, uint32);
  248. for(unsigned i = 0; i < serout.tablelen; i++ ) {
  249. lua_rawgeti( L, 3, i + 1 );
  250. unsigned delay = (unsigned) luaL_checkinteger( L, -1 );
  251. serout.delay_table[i] = delay;
  252. lua_pop( L, 1 );
  253. }
  254. if (is_async) { // async version for duration above 15 mSec
  255. if (!platform_hw_timer_init(TIMER_OWNER, FRC1_SOURCE, TRUE)) {
  256. // Failed to init the timer
  257. luaL_error(L, "Unable to initialize timer");
  258. }
  259. platform_hw_timer_set_func(TIMER_OWNER, seroutasync_cb, 0);
  260. serout.index = 0;
  261. seroutasync_cb(0);
  262. } else { // sync version for sub-50 µs resolution & total duration < 15 mSec
  263. do {
  264. for( serout.index = 0;serout.index < serout.tablelen; serout.index++ ){
  265. 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
  266. GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[serout.pin]), serout.level);
  267. serout.level = serout.level==LOW ? HIGH : LOW;
  268. delayMicroseconds(serout.delay_table[serout.index]);
  269. }
  270. } while (serout.repeats--);
  271. luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
  272. serout.delay_table = NULL;
  273. }
  274. return 0;
  275. }
  276. #undef DELAY_TABLE_MAX_LEN
  277. #ifdef LUA_USE_MODULES_GPIO_PULSE
  278. LROT_EXTERN(gpio_pulse);
  279. extern int gpio_pulse_init(lua_State *);
  280. #endif
  281. // Module function map
  282. LROT_BEGIN(gpio)
  283. LROT_FUNCENTRY( mode, lgpio_mode )
  284. LROT_FUNCENTRY( read, lgpio_read )
  285. LROT_FUNCENTRY( write, lgpio_write )
  286. LROT_FUNCENTRY( serout, lgpio_serout )
  287. #ifdef LUA_USE_MODULES_GPIO_PULSE
  288. LROT_TABENTRY( pulse, gpio_pulse )
  289. #endif
  290. #ifdef GPIO_INTERRUPT_ENABLE
  291. LROT_FUNCENTRY( trig, lgpio_trig )
  292. LROT_NUMENTRY( INT, INTERRUPT )
  293. #endif
  294. LROT_NUMENTRY( OUTPUT, OUTPUT )
  295. LROT_NUMENTRY( OPENDRAIN, OPENDRAIN )
  296. LROT_NUMENTRY( INPUT, INPUT )
  297. LROT_NUMENTRY( HIGH, HIGH )
  298. LROT_NUMENTRY( LOW, LOW )
  299. LROT_NUMENTRY( FLOAT, FLOAT )
  300. LROT_NUMENTRY( PULLUP, PULLUP )
  301. LROT_END( gpio, NULL, 0 )
  302. int luaopen_gpio( lua_State *L ) {
  303. #ifdef LUA_USE_MODULES_GPIO_PULSE
  304. gpio_pulse_init(L);
  305. #endif
  306. #ifdef GPIO_INTERRUPT_ENABLE
  307. int i;
  308. for(i=0;i<GPIO_PIN_NUM;i++){
  309. gpio_cb_ref[i] = LUA_NOREF;
  310. }
  311. platform_gpio_init(task_get_id(gpio_intr_callback_task));
  312. #endif
  313. serout.done_taskid = task_get_id((task_callback_t) seroutasync_done);
  314. serout.lua_done_ref = LUA_NOREF;
  315. return 0;
  316. }
  317. NODEMCU_MODULE(GPIO, "gpio", gpio, luaopen_gpio);