{"id":1940,"date":"2015-01-17T01:52:58","date_gmt":"2015-01-17T01:52:58","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1940"},"modified":"2015-03-06T04:40:28","modified_gmt":"2015-03-06T04:40:28","slug":"esp8266-run-loop-messaging","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/esp8266-run-loop-messaging\/","title":{"rendered":"The esp8266 run loop and messaging"},"content":{"rendered":"<p>The basic esp8266 examples have the following basic format:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/Main code function\r\nstatic void ICACHE_FLASH_ATTR loop(os_event_t *events) {\r\n\r\n  \/\/ DO STUFF\r\n\r\n  os_delay_us(100);\r\n  system_os_post(user_procTaskPrio, 0, 0 );\r\n}\r\n\r\n\/\/Init function\r\nvoid ICACHE_FLASH_ATTR user_init() {\r\n\r\n  \/\/ init system\r\n\r\n  \/\/Start os task\r\n  system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);\r\n  system_os_post(user_procTaskPrio, 0, 0 );\r\n}\r\n<\/pre>\n<p>The support libraries appear to manage a message queuing system, system_os_task adds a new task type, with a given priority and sets it&#8217;s callback function. Tasks are then fired by sending them messages with system_os_post. I would guess that they have scheduler which waits for messages to be posted and schedules them appropriately. The basic examples provided create a single task, with the call back of &#8220;loop&#8221;. &#8220;loop&#8221; then sends itself a message at exit, insuring it is queued to be called again at some point in the future.<\/p>\n<p>I would guess they also use the same system to schedule various internal processes, and that if &#8220;loop&#8221; never returns your going to have a bad time.<\/p>\n<p>You can also use the messaging system for your own purposes, for example in my previous uart example, rather than busy waiting for input you can post a message for the uart ISR directly i.e.:<\/p>\n<pre>\r\nuser_main.c:\r\n\/\/Main code function\r\nstatic void ICACHE_FLASH_ATTR loop(os_event_t *events) {\r\n\r\n  int c = uart0_rx_one_char();\r\n\r\n  if(c != -1) {\r\n   uart_tx_one_char(c);\r\n  }\r\n}\r\n\r\nuart.c:\r\nLOCAL void uart0_rx_intr_handler(void *para) {\r\n\r\n...\r\n\r\n   system_os_post(user_procTaskPrio, 0, 0 );\r\n}\r\n<\/pre>\n<p>And loop will still regularly get called when there&#8217;s new data. I&#8217;d be slightly careful here, as the documentation is incomplete and there may be issues I&#8217;m unaware of. A modified version of that UART example <a href=\"http:\/\/41j.com\/blog\/2015\/01\/esp8266-serial-uart0-txrx\/\">I previously posted<\/a> using modified to use this messaging method can be downloaded here:<a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/uart_test_messaging.tar.gz\">uart_test_messaging.tar<\/a><br \/>\n<\/p>\n<h4>This is a continuation of my notes on the esp8266 microcontroller, you can find a complete list of my esp8266 posts <a href=\"http:\/\/41j.com\/blog\/2015\/01\/esp8266-notes-summary\/\">here<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>The basic esp8266 examples have the following basic format: \/\/Main code function static void ICACHE_FLASH_ATTR loop(os_event_t *events) { \/\/ DO STUFF os_delay_us(100); system_os_post(user_procTaskPrio, 0, 0 ); } \/\/Init function void ICACHE_FLASH_ATTR user_init() { \/\/ init system \/\/Start os task system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0 ); } The support libraries appear to manage a message [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1940","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-vi","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1940"}],"version-history":[{"count":6,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1940\/revisions"}],"predecessor-version":[{"id":2207,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1940\/revisions\/2207"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}