{"id":7842,"date":"2016-05-23T10:02:57","date_gmt":"2016-05-23T09:02:57","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=7842"},"modified":"2026-03-26T16:48:56","modified_gmt":"2026-03-26T20:48:56","slug":"simple-task-scheduling-a-question-and-an-answer","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2016\/05\/23\/simple-task-scheduling-a-question-and-an-answer\/","title":{"rendered":"Simple real-time task scheduling &#8211; a question and an answer"},"content":{"rendered":"<p>I recently received an email, from somebody with whom I was not acquainted, with the subject line &#8220;Probably a silly question\u201d. This got my attention, as I think the only silly thing about a question is not asking it. It certainly was not a silly question. The emailer was asking about implementation of a real time system and I gave him some advice, which I hope was helpful.<\/p>\n<p>With his permission, I will share the question and my answer &#8230;<!--more--><\/p>\n<p>As the question was about implementing a real time system, my default response would be advice about using an RTOS. Although, in this case, it might have been a valid response, it is probably not the best answer. Here is what he asked:<\/p>\n<p><em>I am using a bare metal app with a super loop. Using polling inside for 3 tasks at different time periods. So am I better off using a simple for loop or using the timer module present in the Atmel SAM D20 Arm M0+ microcontroller ?<\/em><\/p>\n<p><em>100usec &#8211; action A<\/em><br \/>\n<em>1msec &#8211; action B<\/em><br \/>\n<em>10msec &#8211; action C<\/em><\/p>\n<p><em>Obviously, a for loop\/delay is the easiest way but it keeps the processor working and it may not be easy to put the microcontroller to sleep to conserve power. So I am taking a look at implementing it using timers. Please let me know if I am doing it wrong ?!<\/em><\/p>\n<p>The nature of the three actions is unimportant. Suffice it to say that they are small amounts of processing that needs to be performed at regular intervals.<\/p>\n<p>It would be quite possible to use an RTOS. Each action would be coded as a task something like this:<\/p>\n<pre>void task_A()\n{\n action_A();\n RTOS_delay(100);\n}\n\n<\/pre>\n<pre>void task_B()\n{\n action_B();\n RTOS_delay(1000);\n}\n\n<\/pre>\n<pre>void task_C()\n{\n action_C();\n RTOS_delay(10000);\n}\n\n<\/pre>\n<p>If the application was the starting point for something bigger &#8211; i.e. much more functionality\/complexity might be added later &#8211; utilizing an RTOS may be an excellent idea. This is because an RTOS supports a multi-threaded structure which is very scaleable, so adding new functionality is less likely to adversely affect existing code. Of course, I feel honor bound to recommend our own <a href=\"https:\/\/www.mentor.com\/embedded-software\/nucleus\/\" target=\"_blank\" rel=\"noopener noreferrer\">Nucleus RTOS<\/a>, which is ideal for such an application.<\/p>\n<p>However, in this case, I had the impression that the project was bounded and I suggested setting up a 100usec timer interrupt and coding the ISR thus:<\/p>\n<pre>interrupt void timer_ISR()\n{\n static int timer_B = 0;\n static int timer_C = 0;\n\n<\/pre>\n<pre> action_A();\n\n<\/pre>\n<pre> if (timer_B-- == 0)\n {\n  action_B();\n  timer_B = 10;\n }\n\n<\/pre>\n<pre> if (timer_C-- == 0)\n {\n  action_C();\n  timer_C = 100;\n }\n}\n\n<\/pre>\n<p>Any other suggestions on how to address this problem? Please comment, email or contact me via social media.<\/p>\n<p>I am also very open to queries such as this, so please feel free to ask.<\/p>\n<p><a href=\"http:\/\/www.linkedin.com\/in\/colinwalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6579\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/linkedin.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/twitter.com\/colin_walls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6583\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/twitter.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/www.facebook.com\/colinwalls.author\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6591\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/facebook.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/plus.google.com\/116301748426290440139\/posts?hl=en%3Fhl=en\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6587\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/google.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"http:\/\/www.slideshare.net\/ColinWalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6595\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/slideshare.jpg\" alt=\"\" width=\"41\" height=\"41\" \/><\/a><a href=\"http:\/\/blogs.mentor.com\/colinwalls\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6599\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/wordpress.jpg\" alt=\"\" width=\"44\" height=\"44\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently received an email, from somebody with whom I was not acquainted, with the subject line &#8220;Probably a silly&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[300,638,304,374,357,309,639],"industry":[],"product":[],"coauthors":[],"class_list":["post-7842","post","type-post","status-publish","format-standard","hentry","category-news","tag-embedded-software","tag-interrupts","tag-nucleus","tag-nucleus-os","tag-real-time","tag-rtos","tag-timer-interrupts"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=7842"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7842\/revisions"}],"predecessor-version":[{"id":10474,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/7842\/revisions\/10474"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=7842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=7842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=7842"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=7842"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=7842"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=7842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}