{"id":1935,"date":"2019-06-28T04:18:41","date_gmt":"2019-06-28T04:18:41","guid":{"rendered":"http:\/\/fastbitlab.com\/?p=1935"},"modified":"2023-09-08T09:24:06","modified_gmt":"2023-09-08T03:54:06","slug":"free-rtos","status":"publish","type":"post","link":"https:\/\/fastbitlab.com\/blog\/free-rtos\/","title":{"rendered":"How PendSV is used for Context Switching in FreeRTOS ??"},"content":{"rendered":"<div class=\"tmpl-text-5 boldgrid-section dynamic-gridblock\">\n<div class=\"container-fluid\">\n<div class=\"row background-fixed\" style=\"padding-top: 50px; padding-bottom: 50px; background-image: linear-gradient(to left, rgba(238, 238, 238, 0.8), rgba(238, 238, 238, 0.8)), url('https:\/\/source.unsplash.com\/WB3ujiKLJwQ\/1920x1080'); background-size: cover; background-position: 50% 50%;\" data-image-url=\"https:\/\/source.unsplash.com\/WB3ujiKLJwQ\/1920x1080\" data-bg-overlaycolor=\"rgba(238,238,238,0.8)\">\n<div class=\"col-md-1 col-sm-12 col-xs-12 col-lg-1\">\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"col-md-10 col-sm-12 col-xs-12 align-column-center col-lg-9\">\n<p class=\"mod-reset\">&nbsp;<\/p>\n<h2 class=\"entry-title\" data-fontsize=\"20\" data-lineheight=\"27\"><span style=\"color: #000000;\">How PendSV is used for Context Switching in FreeRTOS ??<\/span><\/h2>\n<div class=\"row bg-editor-hr-wrap\">\n<div class=\"col-md-12 col-sm-12 col-xs-12 col-lg-12\">\n<hr class=\"bg-hr\">\n<\/div>\n<\/div>\n<h3 class=\"color2-color\"><strong>Free RTOS<\/strong><\/h3>\n<p class=\"mod-reset\"><img decoding=\"async\" class=\"aligncenter wp-image-431\" style=\"outline-color: transparent !important;\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/free1.jpg\" alt=\"Free RTOS\" width=\"293\" height=\"111\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1.jpg 330w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/1_0_300_114.329268293_free1.jpg 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1-120x45.jpg 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1-200x76.jpg 200w\" sizes=\"(max-width: 293px) 100vw, 293px\" \/><\/p>\n<p class=\"\">In this article let me explain, line by line, the<\/p>\n<p class=\"\">&nbsp;PendSV handler which carry out the Context switch form one task to another task. That is nothing but saving the context of current task (Context is nothing but few processor registers, that\u2019s it) and retrieving the context of the next potential task.<\/p>\n<p class=\"\">So, here is the handler code, I copied from the&nbsp;<strong>port.c<\/strong>&nbsp;file of&nbsp;<strong>ARM Cortex M3.<\/strong><\/p>\n<p class=\"\">If you want to locate this file then, you have to go to,<\/p>\n<p class=\"\"><span style=\"color: #ff0000;\">&lt;FreeRTOs installation path&gt;\\FreeRTOS\\FreeRTOSv9.0.0\\FreeRTOS\\Source\\portable\\GCC\\ARM_CM3\\<\/span><\/p>\n<p class=\"\">you can download and install the FreeRTOS from here:<\/p>\n<p class=\"\"><strong>&nbsp;<a href=\"https:\/\/sourceforge.net\/projects\/freertos\/\">https:\/\/sourceforge.net\/projects\/freertos\/<\/a><\/strong><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12 color4-background-color color-4-text-contrast bg-background-color\" style=\"border-width: 0px;\">\n<p class=\"\"><span style=\"font-weight: 400;\">void xPortPendSVHandler( void )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">\/* This is a naked function. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">__asm volatile<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">(<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">mrs r0, psp<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">isb<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldr<\/span> <span style=\"font-weight: 400;\">r3, pxCurrentTCBConst<\/span> <span style=\"font-weight: 400;\">\\n&#8221; <span style=\"color: #ff0000;\">\/* Get the location of the current TCB. *\/<\/span><\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldr<\/span> <span style=\"font-weight: 400;\">r2, [r3]<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">stmdb r0!, {r4-r11}<\/span> <span style=\"font-weight: 400;\">\\n&#8221; <span style=\"color: #ff0000;\">\/* Save the remaining registers. *\/<\/span><\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">str r0, [r2]<\/span> <span style=\"font-weight: 400;\">\\n&#8221; <span style=\"color: #ff0000;\">\/* Save the new top of stack into the first member of the TCB. *\/<\/span><\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">stmdb sp!, {r3, r14}<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">mov r0, %0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">msr basepri, r0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">bl vTaskSwitchContext<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">mov r0, #0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">msr basepri, r0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldmia sp!, {r3, r14}<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/* Restore the context, including the critical nesting count. *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldr r1, [r3]<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldr r0, [r1]<\/span> <span style=\"font-weight: 400;\">\\n&#8221; <span style=\"color: #ff0000;\">\/* The first item in pxCurrentTCB is the task top of stack. *\/<\/span><\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldmia r0!, {r4-r11}<\/span> <span style=\"font-weight: 400;\">\\n&#8221; <span style=\"color: #ff0000;\">\/* Pop the registers. *\/<\/span><\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">msr psp, r0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">isb<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">bx r14<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">.align 4<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;pxCurrentTCBConst: .word pxCurrentTCB<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">::&#8221;i&#8221;(configMAX_SYSCALL_INTERRUPT_PRIORITY)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">);<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">}<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">Now, let\u2019s explore this line by line.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">First, i will write the code and below that i will give the explanation !<\/span><\/p>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8221; mrs R0, PSP \\n&#8221;<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">Here, they are just saving the PSP value in to R0. Why? Let\u2019s see.<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-415 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/1.png\" alt=\"\" width=\"593\" height=\"295\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/1.png 593w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/1-300x149.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/1-120x60.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/1-500x249.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/1-200x99.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/1-400x199.png 400w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-9 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8221; ldr R3, pxCurrentTCBConst \\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now, in this line, the address of the global variable \u201cpxCurrentTCB\u201d is stored in R3 register.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">\u201cpxCurrentTCB\u201d is a global variable defined in tasks.c (go ahead and check tasks.c) which holds the address of the Currently executing task\u2019s TCB.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now, let\u2019s just assume that the address of the pxCurrentTCB is 0x20000000. And it is holding the address 0x20001108 as shown below.<\/span><\/p>\n<\/div>\n<div class=\"col-lg-2 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-9 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img decoding=\"async\" class=\"aligncenter wp-image-420 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/2.png\" alt=\"\" width=\"286\" height=\"74\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/2.png 286w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/2-120x31.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/2-200x52.png 200w\" sizes=\"(max-width: 286px) 100vw, 286px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-2 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p><span style=\"font-weight: 400;\">So, now <\/span><b>R3<\/b><span style=\"font-weight: 400;\"> = 0x20000000<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; background-color: #f7f6f6;\"><span style=\"font-weight: 400;\">&#8221; ldr R2, [R3] \\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">(De-reference 0x20000000 and store the value into R2)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So, R2 = 0x20001108 (Address of the Current TCB)<\/span><\/p>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8221; stmdb R0!, {R4-R11} \\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">Now , R0 has PSP value right ??&nbsp; So, using that PSP, we are saving the registers contents R4 to R11 on to the tasks stack memory. So, currently R4 to R11 are safe.<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-421 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/3.png\" alt=\"\" width=\"403\" height=\"356\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/3.png 403w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/3-300x265.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/3-120x106.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/3-200x177.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/3-400x353.png 400w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8221; str R0, [R2] \\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">(De-reference 0x20001108 and store the value in to R0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">What is 0x20001108? It\u2019s the address of the current TCB stored in pxCurrentTCB pointer.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">That is, pxCurrentTCB is nothing but a pointer of type \u201ctskTCB\u201d.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">tskTCB is the TCB structure which is defined in tasks.c. Go Go Go check tasks.c !!! . You will find the below structure.<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12 color4-background-color color-4-text-contrast bg-background-color\" style=\"border-width: 0px;\">\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">\/*<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">&nbsp;* Task control block.&nbsp; A task control block (TCB) is allocated for each task,<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">&nbsp;* and stores task state information, including a pointer to the task&#8217;s context<\/span><\/p>\n<p><span style=\"font-weight: 400; color: #ff0000;\">&nbsp;* (the task&#8217;s run time environment, including register values)<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">&nbsp;*\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">typedef struct tskTaskControlBlock<\/span><\/p>\n<p><span style=\"font-weight: 400;\">{<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">volatile StackType_t<\/span> <span style=\"font-weight: 400;\">*pxTopOfStack;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Points to the location of the last item placed on the tasks stack.&nbsp; THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#if ( portUSING_MPU_WRAPPERS == 1 )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">xMPU_SETTINGS<\/span> <span style=\"font-weight: 400;\">xMPUSettings;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; The MPU settings are defined as part of the port layer.&nbsp; THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">ListItem_t<\/span> <span style=\"font-weight: 400;\">xStateListItem;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">ListItem_t<\/span> <span style=\"font-weight: 400;\">xEventListItem;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Used to reference a task from an event list. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">UBaseType_t<\/span> <span style=\"font-weight: 400;\">uxPriority;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; The priority of the task.&nbsp; 0 is the lowest priority. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">StackType_t<\/span> <span style=\"font-weight: 400;\">*pxStack;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Points to the start of the stack. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">char<\/span> <span style=\"font-weight: 400;\">pcTaskName[ configMAX_TASK_NAME_LEN ];<span style=\"color: #ff0000;\">\/*&lt; Descriptive name given to the task when created.&nbsp; Facilitates debugging only. *\/ \/*lint !e971 Unqualified char types are allowed for strings and single characters only. *\/<\/span><\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#if ( portSTACK_GROWTH &gt; 0 )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">StackType_t<\/span> <span style=\"font-weight: 400;\">*pxEndOfStack;<\/span><span style=\"font-weight: 400; color: #ff0000;\"> \/*&lt; Points to the end of the stack on architectures where the stack grows up from low memory. *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if ( portCRITICAL_NESTING_IN_TCB == 1 )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">UBaseType_t<\/span> <span style=\"font-weight: 400;\">uxCriticalNesting;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#if ( configUSE_TRACE_FACILITY == 1 )<\/span><\/p>\n<p><span style=\"font-weight: 400;\">UBaseType_t<\/span> <span style=\"font-weight: 400;\">uxTCBNumber;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Stores a number that increments each time a TCB is created.&nbsp; It allows debuggers to determine when a task has been deleted and then recreated. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">UBaseType_t<\/span> <span style=\"font-weight: 400;\">uxTaskNumber;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Stores a number specifically for use by third party trace code. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if ( configUSE_MUTEXES == 1 )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">UBaseType_t<\/span> <span style=\"font-weight: 400;\">uxBasePriority;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; The priority last assigned to the task &#8211; used by the priority inheritance mechanism. *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">UBaseType_t<\/span> <span style=\"font-weight: 400;\">uxMutexesHeld;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if ( configUSE_APPLICATION_TASK_TAG == 1 )<\/span><\/p>\n<p><span style=\"font-weight: 400;\">TaskHookFunction_t pxTaskTag;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if( configNUM_THREAD_LOCAL_STORAGE_POINTERS &gt; 0 )<\/span><\/p>\n<p><span style=\"font-weight: 400;\">void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if( configGENERATE_RUN_TIME_STATS == 1 )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">uint32_t<\/span> <span style=\"font-weight: 400;\">ulRunTimeCounter;<\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Stores the amount of time the task has spent in the Running state. *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if ( configUSE_NEWLIB_REENTRANT == 1 )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">\/* Allocate a Newlib reent structure that is specific to this task.<\/span><\/p>\n<p><span style=\"font-weight: 400; color: #ff0000;\">Note Newlib support has been included by popular demand, but is not<\/span><\/p>\n<p><span style=\"font-weight: 400; color: #ff0000;\">used by the FreeRTOS maintainers themselves.&nbsp; FreeRTOS is not<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">responsible for resulting newlib operation.&nbsp; User must be familiar with<\/span><\/p>\n<p><span style=\"font-weight: 400; color: #ff0000;\">newlib and must provide system-wide implementations of the necessary<\/span><\/p>\n<p><span style=\"font-weight: 400; color: #ff0000;\">stubs. Be warned that (at the time of writing) the current newlib design<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">implements a system-wide malloc() that must be provided with locks. *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">struct<\/span> <span style=\"font-weight: 400;\">_reent xNewLib_reent;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if( configUSE_TASK_NOTIFICATIONS == 1 )<\/span><\/p>\n<p><span style=\"font-weight: 400;\">volatile uint32_t ulNotifiedValue;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">volatile uint8_t ucNotifyState;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400; color: #ff0000;\">\/* See the comments above the definition of<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400; color: #ff0000;\">tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">uint8_t<\/span> <span style=\"font-weight: 400;\">ucStaticallyAllocated; <\/span> <span style=\"font-weight: 400; color: #ff0000;\">\/*&lt; Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. *\/<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">#if( INCLUDE_xTaskAbortDelay == 1 )<\/span><\/p>\n<p><span style=\"font-weight: 400;\">uint8_t ucDelayAborted;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">#endif<\/span><\/p>\n<p><span style=\"font-weight: 400;\">} tskTCB;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">In this structure take look in to the first member. What\u2019s that??<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It\u2019s another pointer isn\u2019t? Or you can say a place holder, which holds address of the stack where the last item is placed by this task and remember that every task will have its own stack space.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So, the first member just holds the address of the last item present in the stack.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">After \u201c<\/span><b>stmdb R0!, {R4-R11} , R0\u2033<\/b><span style=\"font-weight: 400;\"> will be pointing to the new top of the stack isn\u2019t it ??<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">So, save that new top of the stack in to the first member of the TCB.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now, the first member of the TCB is updated with new top of the stack. i.e. value in the register R0.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This value will be used to restore the stack pointer, when this task switches in later point in time.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ok. let\u2019s move ahead.<\/span><\/p>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">stmdb sp!, {R3, R14}<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">We are going to branch out after this, that\u2019s why, save R3 and R14 for a time being.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">R0 is already saved, no worries!<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">R1 is not required because we didn\u2019t used it so far,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">R2 was just a place holder, now it\u2019s not required, so only save R3 and R14.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Note that here SP being used, since we are in the handler mode of the processor, SP is nothing but MSP (Main Stack Pointer).<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">So, we just pushed to kernels stack memory.<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-422 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/4.png\" alt=\"\" width=\"421\" height=\"293\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/4.png 421w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/4-300x209.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/4-120x84.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/4-200x139.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/4-400x278.png 400w\" sizes=\"(max-width: 421px) 100vw, 421px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12 color4-background-color color-4-text-contrast bg-background-color\" style=\"border-width: 0px;\">\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">mov R0, %0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">msr basepri, R0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">\/* disable the interrupts, *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">bl vTaskSwitchContext<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">branch out to <\/span><b>vTaskSwitchContext.<\/b><\/p>\n<p class=\"\"><b>vTaskSwitchContext<\/b><span style=\"font-weight: 400;\"> is defined in tasks.c , which selects the next potential task based on priority .<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">So, the below figure explains what happens when call is made to <\/span><b>vTaskSwitchContext<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-423 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/5.png\" alt=\"\" width=\"648\" height=\"315\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/5.png 648w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/5-300x146.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/5-600x292.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/5-120x58.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/5-500x243.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/5-200x97.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/5-400x194.png 400w\" sizes=\"(max-width: 648px) 100vw, 648px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<ol class=\"\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Initially the global pointer <\/span><b>pxCurrentTCB<\/b><span style=\"font-weight: 400;\"> was holding the address of the Current TCB .i.e <\/span><b>0x20001108<\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">After that call is made to <\/span><b>vTaskSwitchContext<\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Inside <\/span><b>vTaskSwitchContext<\/b><span style=\"font-weight: 400;\"> , there is a call to <\/span><b>taskSELECT_HIGHEST_PRIORITY_TASK()<\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\">Which changes the <b>pxCurrentTCB<\/b> value to the address of the next potential TCB<\/li>\n<\/ol>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">mov R0, #0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\">&#8221; msr basepri, R0 &nbsp; &nbsp; &nbsp; &nbsp; \\n&#8221;<\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">Disables the interrupt again, who knows what happened to basepri when we branched out above *\/<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12 color4-background-color color-4-text-contrast bg-background-color\" style=\"border-width: 0px;\">\n<p class=\"\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldmia sp!, {R3, R14}<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">POP R3 , R14.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">Remember that R3 = 0X20000000<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-425 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/6.png\" alt=\"\" width=\"235\" height=\"276\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/6.png 235w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/6-120x141.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/6-200x235.png 200w\" sizes=\"(max-width: 235px) 100vw, 235px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8221; ldr R1, [R3] \\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Dereference R3, and store the value in to R1.<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-426\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/7.png\" alt=\"\" width=\"288\" height=\"87\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/7.png 288w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/7-120x36.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/7-200x60.png 200w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldr R0, [R1]<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Dereference 0x20000580 and store the value in to R0 .<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">That means, store the value of the first member of the new TCB.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As you already know, first member is nothing but top of the stack.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">So, R0 = Top of the stack of new Task, which is selected for switching in.<\/span><\/p>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">ldmia R0!, {R4-R11}<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div><\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p><span style=\"font-weight: 400;\">So, we know the top of the stack;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">Let\u2019s use that to retrieve the context of new Task.<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">This is the Current Stack state of the New Task which is about to switch in!<\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-427 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/8.png\" alt=\"\" width=\"374\" height=\"361\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/8.png 374w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/8-300x290.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/8-120x116.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/8-200x193.png 200w\" sizes=\"(max-width: 374px) 100vw, 374px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\" style=\"text-align: left;\"><span style=\"color: #993366;\"><b>This is the Current Stack state of the New Task after \u201cldmia R0!, {R4-R11}\u201d<\/b><\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-428 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/9.png\" alt=\"\" width=\"472\" height=\"299\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/9.png 472w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/9-300x190.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/9-120x76.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/9-320x202.png 320w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/9-200x127.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/9-400x253.png 400w\" sizes=\"(max-width: 472px) 100vw, 472px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"font-weight: 400;\">So, context of the new task is retrieved to register <\/span><b>R4<\/b><span style=\"font-weight: 400;\"> to <\/span><b>R11<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">msr PSP, R0<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now, let\u2019s initialize the PSP with the R0 value.<\/span><\/p>\n<p class=\"color-4-text-contrast color4-background-color\" style=\"border-width: 0px;\"><span style=\"font-weight: 400;\">&#8220;<\/span> <span style=\"font-weight: 400;\">bx R14<\/span> <span style=\"font-weight: 400;\">\\n&#8221;<\/span><\/p>\n<p class=\"\"><span style=\"font-weight: 400;\">Return from the handler!<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px;\"><span style=\"color: #993366;\"><b>Before executing \u201cbx R14\u201d<\/b><\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-429 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/10.png\" alt=\"\" width=\"511\" height=\"290\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/10.png 511w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/10-300x170.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/10-120x68.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/10-500x284.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/10-200x114.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/10-400x227.png 400w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-11 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-11 col-md-12 col-xs-12 col-sm-12\"><span class=\"\" style=\"color: #993366;\"><b>After executing bx R14<\/b><\/span><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"mod-reset\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-430 size-full\" src=\"https:\/\/fastbitlab.com\/wp-content\/uploads\/2017\/03\/11.png\" alt=\"\" width=\"567\" height=\"323\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/11.png 567w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/11-300x171.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/11-120x68.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/11-500x285.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/11-200x114.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/11-400x228.png 400w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<div class=\"col-lg-10 col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><span style=\"color: #993366;\"><b>So, the task will resume to the instruction during which it was switched out of the running state.<\/b><\/span><\/p>\n<p><span style=\"color: #993366;\"><b>Note: I have omitted explaining of \u201cISB\u201d instruction. Please, read from here<\/b><\/span><\/p>\n<p class=\"\"><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"http:\/\/infocenter.arm.com\/help\/index.jsp?topic=\/com.arm.doc.dui0552a\/CHDEBIEG.html\"><b>http:\/\/infocenter.arm.com\/help\/index.jsp?topic=\/com.arm.doc.dui0552a\/CHDEBIEG.html<\/b><\/a><\/span><\/p>\n<p class=\"\"><span style=\"color: #993366;\"><b>Get the Full Course on FreeRTOS Porting and programming&nbsp; <\/b><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/www.udemy.com\/mastering-rtos-hands-on-with-freertos-arduino-and-stm32fx\/?couponCode=RTOSSUPER15\"><b>Here.<\/b><\/a><\/span><\/span><\/p>\n<p class=\"\"><span style=\"color: #993366;\"><b>If you like this article, please consider sharing it in linkedin by clicking below.<\/b><\/span><\/p>\n<p class=\"\"><span style=\"color: #993366;\"><b>Thanks.<\/b><\/span><\/p>\n<\/div>\n<div class=\"col-lg-1 col-md-12 col-sm-12 col-xs-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<div class=\"row\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; How PendSV is used for Context Switching in FreeRTOS ?? Free RTOS In this article let me explain, line by line, the &nbsp;PendSV handler which carry out the Context switch form one task to another task. That is nothing but saving the context of current task (Context is nothing but few processor registers, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":431,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1935","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","entry","has-media"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How PendSV is used for Context Switching in FreeRTOS ??<\/title>\n<meta name=\"description\" content=\"Context switching in Free RTOS. In this article let me explain, line by line, the PendSV handler which carry out the Context switch form one task to another\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fastbitlab.com\/blog\/free-rtos\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How PendSV is used for Context Switching in FreeRTOS ??\" \/>\n<meta property=\"og:description\" content=\"Context switching in Free RTOS. In this article let me explain, line by line, the PendSV handler which carry out the Context switch form one task to another\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fastbitlab.com\/blog\/free-rtos\/\" \/>\n<meta property=\"og:site_name\" content=\"FastBit EBA\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/fastbiteba\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-28T04:18:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-08T03:54:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"330\" \/>\n\t<meta property=\"og:image:height\" content=\"125\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"FastBitLab\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:site\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"FastBitLab\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/\"},\"author\":{\"name\":\"FastBitLab\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\"},\"headline\":\"How PendSV is used for Context Switching in FreeRTOS ??\",\"datePublished\":\"2019-06-28T04:18:41+00:00\",\"dateModified\":\"2023-09-08T03:54:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/\"},\"wordCount\":1650,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/03\\\/free1.jpg\",\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/\",\"name\":\"How PendSV is used for Context Switching in FreeRTOS ??\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/03\\\/free1.jpg\",\"datePublished\":\"2019-06-28T04:18:41+00:00\",\"dateModified\":\"2023-09-08T03:54:06+00:00\",\"description\":\"Context switching in Free RTOS. In this article let me explain, line by line, the PendSV handler which carry out the Context switch form one task to another\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#primaryimage\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/03\\\/free1.jpg\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/03\\\/free1.jpg\",\"width\":330,\"height\":125},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/free-rtos\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How PendSV is used for Context Switching in FreeRTOS ??\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"name\":\"FastBit EBA\",\"description\":\"Your Online Academy of Embedded Systems\",\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\",\"name\":\"FastBit EBA\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"width\":640,\"height\":640,\"caption\":\"FastBit EBA\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/fastbiteba\\\/\",\"https:\\\/\\\/x.com\\\/fastbiteba\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/fastbit-embedded-brain-academy-b3167b124\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCa1REBV9hyrzGp2mjJCagBg\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\",\"name\":\"FastBitLab\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"caption\":\"FastBitLab\"},\"description\":\"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How PendSV is used for Context Switching in FreeRTOS ??","description":"Context switching in Free RTOS. In this article let me explain, line by line, the PendSV handler which carry out the Context switch form one task to another","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fastbitlab.com\/blog\/free-rtos\/","og_locale":"en_US","og_type":"article","og_title":"How PendSV is used for Context Switching in FreeRTOS ??","og_description":"Context switching in Free RTOS. In this article let me explain, line by line, the PendSV handler which carry out the Context switch form one task to another","og_url":"https:\/\/fastbitlab.com\/blog\/free-rtos\/","og_site_name":"FastBit EBA","article_publisher":"https:\/\/www.facebook.com\/fastbiteba\/","article_published_time":"2019-06-28T04:18:41+00:00","article_modified_time":"2023-09-08T03:54:06+00:00","og_image":[{"width":330,"height":125,"url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1.jpg","type":"image\/jpeg"}],"author":"FastBitLab","twitter_card":"summary_large_image","twitter_creator":"@fastbiteba","twitter_site":"@fastbiteba","twitter_misc":{"Written by":"FastBitLab","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/#article","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/"},"author":{"name":"FastBitLab","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d"},"headline":"How PendSV is used for Context Switching in FreeRTOS ??","datePublished":"2019-06-28T04:18:41+00:00","dateModified":"2023-09-08T03:54:06+00:00","mainEntityOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/"},"wordCount":1650,"commentCount":7,"publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1.jpg","articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fastbitlab.com\/blog\/free-rtos\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/","url":"https:\/\/fastbitlab.com\/blog\/free-rtos\/","name":"How PendSV is used for Context Switching in FreeRTOS ??","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/#primaryimage"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1.jpg","datePublished":"2019-06-28T04:18:41+00:00","dateModified":"2023-09-08T03:54:06+00:00","description":"Context switching in Free RTOS. In this article let me explain, line by line, the PendSV handler which carry out the Context switch form one task to another","breadcrumb":{"@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fastbitlab.com\/blog\/free-rtos\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/#primaryimage","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1.jpg","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2017\/03\/free1.jpg","width":330,"height":125},{"@type":"BreadcrumbList","@id":"https:\/\/fastbitlab.com\/blog\/free-rtos\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fastbitlab.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How PendSV is used for Context Switching in FreeRTOS ??"}]},{"@type":"WebSite","@id":"https:\/\/fastbitlab.com\/blog\/#website","url":"https:\/\/fastbitlab.com\/blog\/","name":"FastBit EBA","description":"Your Online Academy of Embedded Systems","publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fastbitlab.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fastbitlab.com\/blog\/#organization","name":"FastBit EBA","url":"https:\/\/fastbitlab.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","width":640,"height":640,"caption":"FastBit EBA"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/fastbiteba\/","https:\/\/x.com\/fastbiteba","https:\/\/www.linkedin.com\/in\/fastbit-embedded-brain-academy-b3167b124\/","https:\/\/www.youtube.com\/channel\/UCa1REBV9hyrzGp2mjJCagBg"]},{"@type":"Person","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d","name":"FastBitLab","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","caption":"FastBitLab"},"description":"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries."}]}},"_links":{"self":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/1935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/comments?post=1935"}],"version-history":[{"count":5,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/1935\/revisions"}],"predecessor-version":[{"id":15861,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/1935\/revisions\/15861"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media\/431"}],"wp:attachment":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media?parent=1935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/categories?post=1935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/tags?post=1935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}