{"action":"created","issue":{"url":"https://api.github.com/repos/nodemcu/nodemcu-firmware/issues/845","labels_url":"https://api.github.com/repos/nodemcu/nodemcu-firmware/issues/845/labels{/name}","comments_url":"https://api.github.com/repos/nodemcu/nodemcu-firmware/issues/845/comments","events_url":"https://api.github.com/repos/nodemcu/nodemcu-firmware/issues/845/events","html_url":"https://github.com/nodemcu/nodemcu-firmware/issues/845","id":121888188,"number":845,"title":"gpio.trig doesn't play well with interpreter and can cause a panic","user":{"login":"pjsg","id":1508813,"avatar_url":"https://avatars.githubusercontent.com/u/1508813?v=3","gravatar_id":"","url":"https://api.github.com/users/pjsg","html_url":"https://github.com/pjsg","followers_url":"https://api.github.com/users/pjsg/followers","following_url":"https://api.github.com/users/pjsg/following{/other_user}","gists_url":"https://api.github.com/users/pjsg/gists{/gist_id}","starred_url":"https://api.github.com/users/pjsg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjsg/subscriptions","organizations_url":"https://api.github.com/users/pjsg/orgs","repos_url":"https://api.github.com/users/pjsg/repos","events_url":"https://api.github.com/users/pjsg/events{/privacy}","received_events_url":"https://api.github.com/users/pjsg/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"TerryE","id":690173,"avatar_url":"https://avatars.githubusercontent.com/u/690173?v=3","gravatar_id":"","url":"https://api.github.com/users/TerryE","html_url":"https://github.com/TerryE","followers_url":"https://api.github.com/users/TerryE/followers","following_url":"https://api.github.com/users/TerryE/following{/other_user}","gists_url":"https://api.github.com/users/TerryE/gists{/gist_id}","starred_url":"https://api.github.com/users/TerryE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TerryE/subscriptions","organizations_url":"https://api.github.com/users/TerryE/orgs","repos_url":"https://api.github.com/users/TerryE/repos","events_url":"https://api.github.com/users/TerryE/events{/privacy}","received_events_url":"https://api.github.com/users/TerryE/received_events","type":"User","site_admin":false},"milestone":null,"comments":9,"created_at":"2015-12-12T23:58:40Z","updated_at":"2016-01-24T02:02:48Z","closed_at":null,"body":"The gpio module has support for callbacks which will be triggered when the specified condition happens on the selected pin. The code is called at interrupt level (I think) by the SDK. It can thus interrupt what is going on in the main line code. If you enable the asserts, and then run the following code, you can demonstrate the problem (with a bit of luck):\r\n\r\n```\r\nfunction gp()\r\n    p1 = 0\r\n    du = 0\r\n    gpio.mode(1,gpio.INT)\r\n    function pin1cb(level)\r\n       du = (tmr.now() - p1)\r\n       print(du)\r\n       p1 = tmr.now()\r\n       if level == 1 then gpio.trig(1, \"down\") else gpio.trig(1, \"up\") end\r\n    end\r\n    gpio.trig(1, \"down\",pin1cb)\r\nend\r\n\r\ngpio.mode(0, gpio.OUTPUT)\r\nbit = gpio.HIGH\r\ntmr.alarm(0, 100, 1, function() gpio.write(0, bit) bit = gpio.HIGH + gpio.LOW - bit end)\r\ngp()\r\n```\r\n\r\nOnce running this code, use a screwdriver (or something similar) to make a noisy connection from D0 to D1. Wiggle the screwdriver around, making and breaking the connection. I find that I can trigger assert failures within 10 to 15 seconds, sometimes with panics as here. For example:\r\n\r\n```\r\n2718\r\nlapi.c:607: L->top < L->ci->top\r\nlapi.c:447: L->top < L->ci->top\r\nlapi.c:830: (nresults) == (-1) || (L->ci->top - L->top >= (nresults) - (nargs))\r\nPANIC: unprotected error in call to Lua API (stdin:1: attempt to perform arithmetic on field 'HIGH' (a nil value))\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nodemcu/nodemcu-firmware/issues/comments/174243241","html_url":"https://github.com/nodemcu/nodemcu-firmware/issues/845#issuecomment-174243241","issue_url":"https://api.github.com/repos/nodemcu/nodemcu-firmware/issues/845","id":174243241,"user":{"login":"TerryE","id":690173,"avatar_url":"https://avatars.githubusercontent.com/u/690173?v=3","gravatar_id":"","url":"https://api.github.com/users/TerryE","html_url":"https://github.com/TerryE","followers_url":"https://api.github.com/users/TerryE/followers","following_url":"https://api.github.com/users/TerryE/following{/other_user}","gists_url":"https://api.github.com/users/TerryE/gists{/gist_id}","starred_url":"https://api.github.com/users/TerryE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TerryE/subscriptions","organizations_url":"https://api.github.com/users/TerryE/orgs","repos_url":"https://api.github.com/users/TerryE/repos","events_url":"https://api.github.com/users/TerryE/events{/privacy}","received_events_url":"https://api.github.com/users/TerryE/received_events","type":"User","site_admin":false},"created_at":"2016-01-24T02:02:48Z","updated_at":"2016-01-24T02:02:48Z","body":"OK,  There are three major instabilities with the the GPIO code:\r\n\r\n-  Stuff that shouldn't be executed out of Flash is being so.  I am talking specifically about the ISR and a couple of interrupt-disabled sections that are in `platform_gpio_mode()`. I pull these last two out into separate functions and tagged all three as `ICACHE_RAM_ATTR` .\r\n\r\n-  The ISR can be reentered but is not designed as rentrant\r\n\r\n-  It call Lua Land in a nasty nasty way.\r\n\r\nThe can all be fixed but there still remains an issue of how to handle reentrance.  What I am talking about is say we are triggering on GPIO2 which is bouncing, so the ISR gets a **pin2:close** for which it issues a `task_ post_high()`, but before the task executes pin2 opens and closes again.  If we make the trigger one-shot, the ISR disables interrupts on pin2 and it is left to the Lua cb to reissue the trigger.  If we don't and we have a rapidly bouncing pin, then we will quickly exhaust task Q.  My vote is for one-shot, which is (if I recall correctly) the current intended behaviour).  "}}