{"id":279,"date":"2013-01-30T17:55:44","date_gmt":"2013-01-31T01:55:44","guid":{"rendered":"http:\/\/micromouseusa.com\/?p=279"},"modified":"2014-09-16T18:30:10","modified_gmt":"2014-09-17T01:30:10","slug":"how-to-config-interrupts-priorities-for-stm32","status":"publish","type":"post","link":"http:\/\/micromouseusa.com\/?p=279","title":{"rendered":"How to config interrupts priorities for STM32"},"content":{"rendered":"<p>I am testing nested interrupt recently on my STM32 dev board in order to ensure the relationships between nested interrupts are clear for me. I&#8217;ve read lots of sample codes online but never tried on my own since I only used systick and timer based encoder interrupt last year.<\/p>\n<p>before set up the priority for interrupts, we need to determine the NVIC priority group first.<\/p>\n<p>NVIC refers to nested vector interrupt controller, is a controller built in cortex arm M3 M4 processors, therefore this feature can also be found at some other brand&#8217;s arm M3 M4 processors other than stm32.<\/p>\n<p>There are 2 different kinds of priorities: <strong>preemption priorities and sub priorities<\/strong><\/p>\n<p>Usually whoever has higher preemption priority can be executed first, this is always happening with nested interrupts. when 2 interrupts have same preemption priority, then the one who has higher sub priority will be execute first. If both interrupts both have same preemption priority and sub priority, the one comes first will be execute first(first come first serve).<\/p>\n<p>Cortex Arm M3 M4 processors use 8 bits to store the priorities for preemption and sub priorities. In the header file core_CM3.h or core_CM4.h, it uses only 4 bits to store those values. If we use all 4 bits to store preemption priority, then there will be no bit left for sub priority, and vice versa,\u00a0 there will be no bit available for preemption priority when all 4bits are used for sub priority. This process is call priority grouping. There are 5 different groups we can set.<\/p>\n<p>group0\u00a0\u00a0 0 bits for preemption, 4 bits for sub priority<\/p>\n<p>group1 \u00a0 1 bits for preemption, 3 bits for sub priority<\/p>\n<p>group2 \u00a0 2 bits for preemption, 2 bits for sub priority<\/p>\n<p>group3 \u00a0 3 bits for preemption, 1 bits for sub priority<\/p>\n<p>group4 \u00a0 4 bits for preemption, 0 bits for sub priority<\/p>\n<p>thus, we can call function NVIC_PriorityGroupConfig to setup priority grouping now.<\/p>\n<p>I choose group 4 since I only want different preemption priority in my micromouse between interrupts, so I call function as follow:<\/p>\n<p><span style=\"color: #ffff99;\">NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);\u00a0\u00a0 \u00a0\/\/4 bits for preemp priority 0 bit for sub priority<\/span><\/p>\n<p>If you don&#8217;t set the priority grouping, the default grouping in ST library is group 2, which is 2 bits for preemption priority and 2 bits for sub priority<\/p>\n<p>now we can go set the priorities for each peripheral interrupts.<\/p>\n<p>For instance, I want to set the priority for TIM4 as 1 (lower number has higher priority), I call the code as follow:<\/p>\n<p><span style=\"color: #ffff99;\">NVIC_InitTypeDef NVIC_InitStructure;<\/span><\/p>\n<p><span style=\"color: #ffff99;\">NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; \/\/TIM4 IRQ Channel<\/span><br \/>\n<span style=\"color: #ffff99;\"> NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;\/\/Preemption Priority<\/span><br \/>\n<span style=\"color: #ffff99;\"> NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; \/\/Sub Priority<\/span><br \/>\n<span style=\"color: #ffff99;\"> NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;<\/span><br \/>\n<span style=\"color: #ffff99;\"> NVIC_Init(&amp;NVIC_InitStructure);<\/span><\/p>\n<p>If you want to setup the priorities for other device, just replace &#8216;TIM4_IRQn&#8217; to the one you want.<\/p>\n<p>However, if you want to setup the priorities for those peripherals whose values for NVIC vector table are smaller than 0, the method we used above will be no long valid. The correct way is to call the function as follow(assume I want systick has highest priority):<br \/>\nNVIC_SetPriority(SysTick_IRQn, 0);\/\/set systick interrupt priority, 0 is the highest for all<\/p>\n<p>you might be confused here since this function didn&#8217;t specify the preemption and sub priories in parameter. In fact, they are both included. Since I mentioned stm32 uses 4 bits to store preemption and sub priorities, the high bits are for preemption and low bits are for sub priority. If the parameter for priority is 0 for the function above(that&#8217;s also what I had in the function), 0 is 0x00 in hex and 0000 in binary, since the default priority grouping for stm32 is group 2, which is 2 bits for preemption priority and 2 bits for sub priority, we get 00 for preemption and 00 for sub. If we change the input from 0 to 7 for the function, the binary form for 7 is 0111, so higher 2 bits is 01 which is 1 in decimal gives you preemption priority as 1, and the lower 2 bits is 11 which is 3 gives you sub priority as 3.<\/p>\n<p>If your priority grouping is same as mine which is group 4, then there is no sub priority, the input in parameter in function NVIC_SetPriority only refers to your preemption priority in this case.<\/p>\n<p>&nbsp;<\/p>\n<p>PS: never set your preemption and sub priorities out of range, it might work, but something unexpected may happen.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am testing nested interrupt recently on my STM32 dev board in order to ensure the relationships between nested interrupts are clear for me. I&#8217;ve read lots of sample codes online but never tried on my own since I only &hellip; <a href=\"http:\/\/micromouseusa.com\/?p=279\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[7],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/micromouseusa.com\/index.php?rest_route=\/wp\/v2\/posts\/279"}],"collection":[{"href":"http:\/\/micromouseusa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/micromouseusa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/micromouseusa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/micromouseusa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=279"}],"version-history":[{"count":18,"href":"http:\/\/micromouseusa.com\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":288,"href":"http:\/\/micromouseusa.com\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions\/288"}],"wp:attachment":[{"href":"http:\/\/micromouseusa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/micromouseusa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/micromouseusa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}