{"id":87,"date":"2016-04-02T20:05:13","date_gmt":"2016-04-02T18:05:13","guid":{"rendered":"http:\/\/blog.ilialex.gr\/?p=87"},"modified":"2016-04-02T20:05:13","modified_gmt":"2016-04-02T18:05:13","slug":"funkos-driver-with-mutex","status":"publish","type":"post","link":"http:\/\/blog.ilialex.gr\/?p=87","title":{"rendered":"FunkOS Driver with Mutex"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>I am currently working on the SD\/MMC driver for CoFILOS.<br \/>\nDuring this time I figured out that there are some issues with the Mutex system, whih I will describe here.<\/p>\n<p>FunkOS uses a driver structure that contains everything needed from the driver level. It mainly contains the standard FunkOS Driver entry point functions (Function Pointer to Init\/Read\/Write etc), a mutex entry and then custom data which are defined per each driver.<\/p>\n<p>The Driver level is actually a wrapper of the actual driver functions that are called. There the common functionality required by the driver system is implemented. Then the function pointers call the actual work routines of the driver.<\/p>\n<figure id=\"attachment_93\" aria-describedby=\"caption-attachment-93\" style=\"width: 451px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide2.png\" rel=\"attachment wp-att-93\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-93\" src=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide2-300x225.png\" alt=\"FunkOS Driver and Mutex Architecture\" width=\"451\" height=\"338\" srcset=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide2-300x225.png 300w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide2-768x576.png 768w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide2.png 960w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/a><figcaption id=\"caption-attachment-93\" class=\"wp-caption-text\">What FunkOS Driver does<\/figcaption><\/figure>\n<p>The mutex function is checked on the Driver level. Now as the mutex is stored on the driver structure, the mutex will work for two tasks if both tasks share the same driver structure. For example accessing the serial port with two tasks that share the serial port (not a very common scenario) means that both tasks should share the same structure. This might be fine for such applications with the drawback that both tasks share the driver structure. Who in this case does the initialization? There are some issues to be solved by the application.<\/p>\n<p>However I do not believe this would be the common case of the drivers. Normally a single task access each device, so this is not a big problem.<\/p>\n<p>Let&#8217;s see another example. The SPI interface supports multiple physical devices that may be accessed. The SPI bus itself is a common shared resource. Each SPI device may use different clock speeds and phase settings to work. I would expect in this case that each SPI device has its own Driver structure. Actually this is how the SPI driver is built in CoFILOS.<\/p>\n<figure id=\"attachment_92\" aria-describedby=\"caption-attachment-92\" style=\"width: 451px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide3.png\" rel=\"attachment wp-att-92\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-92\" src=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide3-300x225.png\" alt=\"CoFILOS Driver Requirements\" width=\"451\" height=\"338\" srcset=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide3-300x225.png 300w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide3-768x576.png 768w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide3.png 960w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/a><figcaption id=\"caption-attachment-92\" class=\"wp-caption-text\">How CoFILOS requires to work<\/figcaption><\/figure>\n<p>Now the common mutex scheme employed by FunkOS would not work, as the mutexes are inside each structure; This means a mutex lock will lock the specific driver&#8217;s mutex. Another task will still access the SPI as the mutex of this driver is not locked. This leads to the probably obvious\u00a0\u00a0solution to keep a driver internal common mutex to properly support mutlitasking.<\/p>\n<figure id=\"attachment_91\" aria-describedby=\"caption-attachment-91\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide4.png\" rel=\"attachment wp-att-91\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-91\" src=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide4-300x225.png\" alt=\"SPI driver example with internal mutex\" width=\"450\" height=\"337\" srcset=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide4-300x225.png 300w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide4-768x576.png 768w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide4.png 960w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-91\" class=\"wp-caption-text\">CoFILOS SPI Driver Implementation<\/figcaption><\/figure>\n<p>The new implementation supports two different configuration of access. The first mode is the interleaved mode. There each task may access the SPI interface per transaction (read\/write). So two tasks can execute transactions to two different devices. Each transaction though is locked, so we do not scramble the SPI input\/output data.<\/p>\n<p>However there are cases where we need to lock a complete transaction sequence. In this case the SPI driver through the Control function supports locking of sequences. In this case CS of this device should be manipulated by the task level itself. This could be modified to be done on the driver as well, but I have not decided if there is a need for handling CS from the task level. This scheme is used for the SD\/MMC interfaces where the there is a requirement of series of actions, especially during the initialization phase.<\/p>\n<p><a href=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide5.png\" rel=\"attachment wp-att-90\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-90\" src=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide5-300x225.png\" alt=\"Timeline of two tasks accessing the Driver\" width=\"451\" height=\"338\" srcset=\"http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide5-300x225.png 300w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide5-768x576.png 768w, http:\/\/blog.ilialex.gr\/wp-content\/uploads\/2016\/04\/Slide5.png 960w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/a><\/p>\n<p>TDD cannot completely cover the full testing of this sequence easily, I just created a primitive test to check critical sections and mutex claims, without actually checking the internal sequence. This is a risk I will take in order to reduce the development time, as my main task is the SD\/MMC driver interface implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; I am currently working on the SD\/MMC driver for CoFILOS. During this time I figured out that there are some issues with the Mutex system, whih I will describe here. FunkOS uses a driver structure that contains everything needed from the driver level. It mainly contains the standard FunkOS Driver entry point functions (Function &hellip; <a href=\"http:\/\/blog.ilialex.gr\/?p=87\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">FunkOS Driver with Mutex<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":94,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-firmware"],"_links":{"self":[{"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":5,"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":98,"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/98"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=\/wp\/v2\/media\/94"}],"wp:attachment":[{"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ilialex.gr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}