gpio.c 12 KB

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