{"id":185,"date":"2023-01-04T20:35:27","date_gmt":"2023-01-04T12:35:27","guid":{"rendered":"https:\/\/wordpress.rslly.top\/?p=185"},"modified":"2023-01-04T20:35:27","modified_gmt":"2023-01-04T12:35:27","slug":"%e7%89%a9%e8%81%94%e7%bd%91%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e7%9a%84%e4%bb%bb%e5%8a%a1%e8%b0%83%e5%ba%a6%e5%88%9d%e6%ad%a5%e8%ae%a4%e8%af%86","status":"publish","type":"post","link":"https:\/\/wordpress.rslly.top\/index.php\/2023\/01\/04\/%e7%89%a9%e8%81%94%e7%bd%91%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e7%9a%84%e4%bb%bb%e5%8a%a1%e8%b0%83%e5%ba%a6%e5%88%9d%e6%ad%a5%e8%ae%a4%e8%af%86\/","title":{"rendered":"\u7269\u8054\u7f51\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u521d\u6b65\u8ba4\u8bc6"},"content":{"rendered":"\n<p>\u968f\u98ce<\/p>\n\n\n\n<p>\u4efb\u52a1\u8c03\u5ea6\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u57fa\u672c\u529f\u80fd\u3002\u4efb\u52a1\u8c03\u5ea6\u4f7f\u5f97\u7528\u6237\u7a0b\u5e8f\u611f\u89c9\u81ea\u5df1\u5728\u72ec\u5360cpu\u3002\u4f46\u662fcpu\u662f\u6709\u9650\u7684\uff0c\u4efb\u52a1\u53c8\u90a3\u4e48\u591a\uff0c\u64cd\u4f5c\u7cfb\u7edf\u662f\u5982\u4f55\u505a\u5230\u5408\u7406\u5206\u914dcpu\u65f6\u95f4\u7684\u5462\u3002\u5176\u5b9ertos\u57fa\u672c\u4e0a\u662f\u6839\u636e\u4f18\u5148\u7ea7\u62a2\u5360\u5f0f\u8c03\u5ea6\uff0c\u7136\u540e\u5982\u679c\u4efb\u52a1\u4f18\u5148\u7ea7\u76f8\u540c\u5c31\u6309\u7167\u65f6\u95f4\u8f6e\u8f6c\u7247\u8c03\u5ea6\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u8f6e\u8f6c\u7247\u7b97\u6cd5\u5e76\u4e0d\u96be\uff0c\u7136\u800c\u6211\u9605\u8bfb\u4e86\u5927\u91cf\u7684\u65f6\u95f4\u8f6e\u8f6c\u7247\u4ecb\u7ecd\u7684\u535a\u5ba2\u59cb\u7ec8\u6ca1\u6709\u5177\u4f53\u7684\u786c\u4ef6\u5b9e\u73b0\u4ecb\u7ecd\u3002\u5927\u90e8\u5206\u535a\u5ba2\u90fd\u662f\u4eff\u771f\u5b9e\u73b0\uff0c\u8fd9\u5176\u5b9e\u548c\u771f\u5b9e\u7684\u5185\u6838\u5b9e\u73b0\u6709\u7740\u5f88\u5927\u7684\u5dee\u8ddd\u3002\u56e0\u4e3a\u4eff\u771f\u5207\u6362\u4f60\u6839\u672c\u6ca1\u6cd5\u5b9e\u73b0\u786c\u4ef6\u4e2d\u65ad\u673a\u5236\uff0c\u800c\u771f\u5b9e\u7684\u5185\u6838\u662f\u5229\u7528\u4e2d\u65ad\u673a\u5236\u6765\u5b9e\u73b0\u4efb\u52a1\u8c03\u5ea6\u7684\u3002<\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u5185\u6838\u8981\u4f7f\u7528\u4e2d\u65ad\u673a\u5236\u800c\u4e0d\u662f\u76f4\u63a5\u50cf\u666e\u901a\u7a0b\u5e8f\u90a3\u6837\u7f16\u5199\u3002\u8fd9\u5176\u5b9e\u975e\u5e38\u663e\u7136\uff0c\u5185\u6838\u8c03\u5ea6\u80af\u5b9a\u5360\u7528\u7684\u8d44\u6e90\u8d8a\u4f4e\u8d8a\u597d\uff0c\u5b9e\u65f6\u6027\u8d8a\u9ad8\u8d8a\u597d\u3002\u800c\u4e2d\u65ad\u673a\u5236\u5c31\u80fd\u63d0\u4f9b\u8fd9\u6837\u7684\u5c5e\u6027\u3002\u4e2d\u65ad\u673a\u5236\u53ef\u4ee5\u8bf4\u662f\u8ba1\u7b97\u673a\u6700\u5177\u9769\u547d\u6027\u7684\u4e00\u4e2a\u8bbe\u8ba1\uff0c\u5b83\u5927\u5927\u63d0\u9ad8\u4e86\u8ba1\u7b97\u673a\u7cfb\u7edf\u7684\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n\n\n\n<p>\u5f53\u770b\u5230\u5229\u7528\u4e2d\u65ad\u673a\u5236\u7684\u4ecb\u7ecd\u65f6\u5019\u6211\u8111\u6d77\u91cc\u5df2\u7ecf\u6d6e\u73b0\u4e86\u4e00\u79cd\u5185\u6838\u8c03\u5ea6\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u5229\u7528systick\u4e2d\u65ad\uff0csystick\u4e2d\u65ad\u5b9e\u73b0\u6d41\u6c34\u706f\u76f8\u4fe1\u5927\u5bb6\u90fd\u66fe\u7ecf\u7f16\u5199\u8fc7\uff0c\u56e0\u6b64\u60f3\u5230\u5229\u7528systick\u6765\u8fdb\u884c\u4efb\u52a1\u5207\u6362\u662f\u4e00\u79cd\u975e\u5e38\u81ea\u7136\u7684\u60f3\u6cd5\uff0c\u53ea\u8981\u8bbe\u7f6e\u597d\u4e2d\u65ad\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u90a3\u4e48\u5c31\u5b9e\u73b0\u4e86\u65f6\u95f4\u7247\u7684\u8bbe\u8ba1\u3002\u4e8b\u5b9e\u4e0atos\u7cfb\u7edf\u540c\u6837\u5229\u7528\u4e86systick\u4e2d\u65ad\u3002\u4e0b\u9762\u662ftos\u7cfb\u7edf\u65f6\u57fa\u7684\u5165\u53e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void SysTick_Handler(void)\r\n{\r\n\tif (tos_knl_is_running())\r\n  {\r\n    tos_knl_irq_enter();\r\n    tos_tick_handler();\r\n    tos_knl_irq_leave();\r\n  }\r\n}<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u4ec5\u4ec5\u5229\u7528systick\u4e2d\u65ad\u80af\u5b9a\u662f\u8981\u51fa\u95ee\u9898\u7684\u3002\u56e0\u4e3a\u5355\u7247\u673a\u8fd8\u8981\u5904\u7406\u5176\u5b83\u4e2d\u65ad\uff0c\u5728Cortex-M3\u4e2d\uff0c\u5982\u679cOS\u5728\u67d0\u4e2a\u4e2d\u65ad\u6d3b\u8dc3\u65f6\uff0c\u62a2\u5360\u4e86\u8be5\u4e2d\u65ad\uff0c\u800c\u4e14\u53c8\u53d1\u751f\u4e86\u4efb\u52a1\u8c03\u5ea6\uff0c\u5e76\u6267\u884c\u4e86\u4efb\u52a1\uff0c\u5207\u6362\u5230\u4e86\u7ebf\u7a0b\u8fd0\u884c\u6a21\u5f0f\uff0c\u5c06\u76f4\u63a5\u89e6\u53d1Fault\u5f02\u5e38\u3002\u8fd9\u80af\u5b9a\u662f\u4e0d\u884c\u7684\uff0c\u4f46\u4f60\u80af\u5b9a\u4f1a\u8bf4\u4e0d\u5982\u76f4\u63a5\u628asystick\u8bbe\u7f6e\u4e3a\u6700\u4f4e\u4f18\u5148\u7ea7\u4e0d\u5c31\u884c\u4e86\u5417\uff1f\u5f53\u7136\u53ef\u4ee5\uff0c\u5f02\u5e38\u786e\u5b9e\u4e0d\u4f1a\u53d1\u751f\u4e86\uff0c\u7136\u800c\u4f60\u4ed4\u7ec6\u60f3\u60f3\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p>\u4ed4\u7ec6\u60f3\u597d\u50cf\u4e5f\u6ca1\u95ee\u9898\u554a\u3002\u7684\u786e\u5f88\u96be\u60f3\u5230\uff0c\u4f46\u662f\u6211\u4eec\u5904\u7406\u4e2d\u65ad\u7684\u6d41\u7a0b\u662f\u600e\u4e48\u6837\u7684\uff1f\u7b2c\u4e00\u6b65\u662f\u5173\u4e2d\u65ad\uff0c\u7136\u540e\u4fdd\u5b58\u73b0\u573a\u518d\u5f00\u4e2d\u65ad\uff0c\u6267\u884c\u7a0b\u5e8f\uff0c\u6700\u540e\u518d\u5173\u4e2d\u65ad\uff0c\u6062\u590d\u73b0\u573a\u7136\u540e\u5f00\u4e2d\u65ad\u3002\u8fd9\u6837\u505a\u5c31\u662f\u4e3a\u4e86\u73b0\u573a\u4e0d\u88ab\u7834\u574f\u540c\u65f6\u4fdd\u8bc1\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u80fd\u591f\u6b63\u5e38\u88ab\u54cd\u5e94\u3002\u4e00\u822cOS\u5728\u8c03\u5ea6\u4efb\u52a1\u65f6\uff0c\u4e5f\u662f\u8981\u5173\u95ed\u4e2d\u65ad\uff0c\u4e5f\u5c31\u662f\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u800cOS\u4efb\u52a1\u8c03\u5ea6\u662f\u8981\u8017\u65f6\u7684\uff0c\u8fd9\u5c31\u4f1a\u51fa\u73b0\u4e00\u79cd\u60c5\u51b5\uff1a<br>\u5728\u4efb\u52a1\u8c03\u5ea6\u671f\u95f4\uff0c\u5982\u679c\u65b0\u7684\u5916\u90e8\u4e2d\u65ad\u53d1\u751f\uff0cCPU\u5c06\u4e0d\u80fd\u591f\u5feb\u901f\u54cd\u5e94\u5904\u7406\u3002\u8fd9\u5bf9\u4e8e\u667a\u80fd\u9a7e\u9a76\u8fd9\u6837\u7684\u573a\u666f\u4f60\u80fd\u5fcd\u53d7\u5417\uff1f<\/p>\n\n\n\n<p>\u5230\u4e86\u8fd9\u91cc\u4f60\u4f3c\u4e4e\u8d70\u5230\u4e86\u7a77\u9014\u672b\u8def\u3002\u7684\u786e\u53ea\u5229\u7528systick\u5728\u8f6f\u4ef6\u5c42\u9762\u5df2\u7ecf\u6ca1\u6709\u66f4\u597d\u7684\u529e\u6cd5\u4e86\u3002\u4f46\u662f\u72e1\u733e\u7684\u5185\u6838\u5927\u4f6c\u5229\u7528\u4e86\u4e00\u4e2a\u53eb\u505apendsv\u7684\u4e2d\u65ad\uff0c\u8fd9\u4e2a\u4e2d\u65ad\u5f88\u5947\u602a\uff0c\u5b83\u80fd\u591f\u50cf\u666e\u901a\u5916\u8bbe\u4e2d\u65ad\u4e00\u6837\u60ac\u8d77\u3002\u5b83\u662f\u7cfb\u7edf\u7ea7\u522b\u7684\u5f02\u5e38\uff0c\u4f46\u5b83\u53c8\u5929\u751f\u652f\u6301\u7f13\u671f\u6267\u884c\u3002\u6709\u4e86\u5b83\u6211\u4eec\u5c31\u80fd\u8f7b\u677e\u907f\u514d\u4e86\u4e0d\u80fd\u5feb\u901f\u5904\u7406\u7684\u95ee\u9898\uff0cpendsv\u4f1a\u81ea\u52a8\u7b49\u5f85\u6240\u6709\u7684\u4e2d\u65ad\u5904\u7406\u5b8c\u6bd5\u518d\u8fdb\u884c\u4efb\u52a1\u5207\u6362\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u5230\u4e86\u8fd9\u91cc\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u4e86\u4e00\u79cd\u8f83\u4e3a\u6210\u719f\u7684os\u8c03\u5ea6\u65b9\u6848\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u6ef4\u7b54\u5b9a\u65f6\u5668\u4e2d\u65ad\uff0c\u5236\u4f5c\u4e1a\u52a1\u8c03\u5ea6\u524d\u7684\u5224\u65ad\u5de5\u4f5c\uff0c\u4e0d\u505a\u4efb\u52a1\u5207\u6362\u3002<\/li><li>\u89e6\u53d1PendSV\uff0cPendSV\u5e76\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u56e0\u4e3aPendSV\u7684\u4f18\u5148\u7ea7\u6700\u4f4e\uff0c\u5982\u679c\u6b64\u65f6\u6b63\u597d\u6709\u4e2d\u65ad\u8bf7\u6c42\uff0c\u90a3\u4e48\u5148\u54cd\u5e94\u4e2d\u65ad\uff0c\u6700\u540e\u7b49\u5230\u6240\u6709\u4f18\u5148\u7ea7\u9ad8\u4e8ePendSV\u7684\u4e2d\u65ad\u90fd\u6267\u884c\u5b8c\u6bd5\uff0c\u518d\u6267\u884cPendSV\uff0c\u8fdb\u884c\u4efb\u52a1\u8c03\u5ea6\u3002<\/li><\/ol>\n\n\n\n<p>\u90a3\u4e48tos\u7cfb\u7edf\u4e5f\u662f\u8fd9\u6837\u5b9e\u73b0\u7684\u5417\uff1f\u6211\u4eec\u770b\u770b\u6e90\u7801\u3002\u90a3\u4e48\u6211\u4eec\u8981\u542f\u52a8tos\u7cfb\u7edf\uff0c\u5165\u53e3\u80af\u5b9a\u8981\u8c03\u7528\u542f\u52a8\u7684api\uff0c\u6e90\u4ee3\u7801api\u5934\u6587\u4ef6\u6ce8\u91ca\u662fget the kernel start to run, which means start the multitask scheduling.\u4e2d\u6587\u610f\u601d\u5c31\u662f\u542f\u52a8\u5185\u6838\uff0c\u610f\u5473\u7740\u542f\u52a8\u591a\u4efb\u52a1\u8c03\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>__API__ k_err_t tos_knl_start(void)\r\n{\r\n    if (tos_knl_is_running()) {\r\n        return K_ERR_KNL_RUNNING;\r\n    }\r\n\r\n    k_next_task = readyqueue_highest_ready_task_get();\r\n    k_curr_task = k_next_task;\r\n    k_knl_state = KNL_STATE_RUNNING;\r\n    cpu_sched_start();\r\n\r\n    return K_ERR_NONE;\r\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u524d\u51e0\u884c\u90fd\u662f\u68c0\u6d4b\u548c\u83b7\u53d6\u4efb\u52a1\u7684\u4e00\u4e9b\u4fe1\u606f\uff0c\u91cd\u70b9\u5728\u4e8e cpu_sched_start() \uff0c\u8fd9\u4e2a\u662f\u7528\u6765\u542f\u52a8cpu\u4efb\u52a1\u8c03\u5ea6\u7684\u3002\u521a\u624d\u90a3\u4e2a\u52a0\u4e86\u5b8f_api_\u8868\u660e\u5b83\u8fd8\u662f\u5c5e\u4e8e\u7528\u6237api\uff0c\u4e0b\u9762\u6b63\u5f0f\u8fdb\u5165\u5185\u6838\u4e16\u754c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-2-1024x576.png\" alt=\"\" class=\"wp-image-187\" srcset=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-2-1024x576.png 1024w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-2-300x169.png 300w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-2-768x432.png 768w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-2-1536x864.png 1536w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-2.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u53ef\u5947\u602a\u7684\u662f\u5b83\u7684\u5b9e\u73b0c\u6587\u4ef6\u5f88\u77ed\uff0c\u5c31\u53c8\u8c03\u7528\u4e86\u4e00\u4e2a\u51fd\u6570 port_sched_start \u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>__KERNEL__ void cpu_sched_start(void)\r\n{\r\n    port_sched_start();\r\n}<\/code><\/pre>\n\n\n\n<p>\u7ee7\u7eed\u524d\u8fdb\u5df2\u7ecf\u6ca1\u6709c\u6587\u4ef6\u4e86\uff0c\u8fce\u9762\u800c\u6765\u7684\u662f\u6c47\u7f16\u8bed\u8a00\u3002\u6211\u719f\u60898051\u6c47\u7f16\uff0c\u4f46\u662f\u5bf9\u4e8earm\u7684\u6c47\u7f16\uff0c\u6211\u5e76\u4e0d\u662f\u5f88\u719f\u6089\uff0c\u4f46\u662f\u53c8\u4e0d\u662f\u8003\u8bd5\uff0c\u8fd8\u662f\u53ef\u4ee5\u67e5\u624b\u518c\u7684\u3002\u7c97\u7565\u770b\u4e86\u770b\uff0c\u5927\u6982\u77e5\u9053\u8fd9\u662f\u4e00\u6bb5\u5f88\u91cd\u8981\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-3-1024x576.png\" alt=\"\" class=\"wp-image-188\" srcset=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-3-1024x576.png 1024w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-3-300x169.png 300w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-3-768x432.png 768w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-3-1536x864.png 1536w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-3.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>NVIC_INT_CTRL   EQU     0xE000ED04                              ; Interrupt control state register.\r\nNVIC_SYSPRI14   EQU     0xE000ED20                              ; System priority register (priority 14).\r\nNVIC_PENDSV_PRI EQU     0x00FF0000                              ; PendSV priority value (lowest).\r\nNVIC_PENDSVSET  EQU     0x10000000                              ; Value to trigger PendSV exception.<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u770b\u4e00\u4e0b\u6ce8\u91ca\uff0c\u8fd9\u4e0d\u5c31\u662f\u8bbe\u7f6ependsv\u4e3a\u8bbe\u7f6e\u4e3a\u6700\u4f4e\u4f18\u5148\u7ea7\u5417\uff0c\u5230\u8fd9\u91cc\u6211\u4eec\u8fd8\u4e0d\u6e05\u695a\u5b83\u4e0esystick\u7684\u5173\u7cfb\uff0c\u4f46\u81f3\u5c11\u5df2\u7ecf\u8bc1\u5b9e\u5b83\u80af\u5b9a\u5229\u7528\u4e86pendsv\u3002\u5e78\u8fd0\u7684\u662f\u6211\u6eda\u52a8\u4e00\u4e0b\u9f20\u6807\u5c31\u53d1\u73b0\u7684<\/p>\n\n\n\n<p>pendsv\u7684\u4e2d\u65ad\u5904\u7406\u5b50\u7a0b\u5e8f\uff08\u6c47\u7f16\u6211\u4eec\u8fd8\u662f\u4e0d\u8981\u53eb\u51fd\u6570\u5427\uff09<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-4-1024x576.png\" alt=\"\" class=\"wp-image-189\" srcset=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-4-1024x576.png 1024w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-4-300x169.png 300w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-4-768x432.png 768w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-4-1536x864.png 1536w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-4.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5f88\u590d\u6742\uff0c\u4f46\u662f\u53ea\u8981\u6709\u70b951\u5355\u7247\u673a\u7684\u529f\u5e95\uff0c\u8fd8\u662f\u731c\u5f97\u51fa\u5b83\u5728\u5e72\u4ec0\u4e48\uff0c\u5176\u5b9e\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u6700\u91cd\u8981\u7684\u5de5\u4f5c\uff0c\u4efb\u52a1\u5806\u6808\u7684\u5207\u6362\u3002\u5b83\u501f\u7528\u4e86\u5bc4\u5b58\u5668\u5b9e\u73b0\u7684\u73b0\u573a\u7684\u4fdd\u62a4\u3002\u81f3\u6b64\u6211\u4eec\u5b8c\u5168\u8bc1\u660etos\u7cfb\u7edf\u5229\u7528pendsv\u8fdb\u884c\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u8fd8\u770b\u5230\u8fd9\u6837\u4e00\u6bb5\u6ce8\u91ca<\/p>\n\n\n\n<p>@ set pendsv priority lowest<br>@ otherwise trigger pendsv in port_irq_context_switch will cause a context swich in irq<br>@ that would be a disaster<\/p>\n\n\n\n<p>\u7ffb\u8bd1\u8fc7\u6765\u8ddf\u6211\u4eec\u4e0a\u9762\u63cf\u8ff0\u7684\u5b8c\u5168\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u518d\u53bb\u627esystick\uff0c\u5f97\u5230\u4e86\u8fd9\u4e2a\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>__API__ void tos_tick_handler(void)\r\n{\r\n    if (unlikely(!tos_knl_is_running())) {\r\n        return;\r\n    }\r\n\r\n    tick_update((k_tick_t)1u);\r\n\r\n#if TOS_CFG_TIMER_EN > 0u &amp;&amp; TOS_CFG_TIMER_AS_PROC > 0u\r\n    timer_update();\r\n#endif\r\n\r\n#if TOS_CFG_ROUND_ROBIN_EN > 0u\r\n    robin_sched(k_curr_task->prio);\r\n#endif\r\n}<\/code><\/pre>\n\n\n\n<p>unlikely\u8fd9\u662f\u5185\u6838\u7684\u4e00\u79cd\u4f18\u5316\u51fd\u6570\uff0c\u6682\u65f6\u4e0d\u7528\u7ba1\u5b83\uff0c\u6211\u4eec\u5168\u529b\u770b robin_sched ()\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>__KERNEL__ void robin_sched(k_prio_t prio)\r\n{\r\n    TOS_CPU_CPSR_ALLOC();\r\n    k_task_t *task;\r\n\r\n    if (k_robin_state != TOS_ROBIN_STATE_ENABLED) {\r\n        return;\r\n    }\r\n\r\n    TOS_CPU_INT_DISABLE();\r\n\r\n    task = readyqueue_first_task_get(prio);\r\n    if (!task || knl_is_idle(task)) {\r\n        TOS_CPU_INT_ENABLE();\r\n        return;\r\n    }\r\n\r\n    if (readyqueue_is_prio_onlyone(prio)) {\r\n        TOS_CPU_INT_ENABLE();\r\n        return;\r\n    }\r\n\r\n    if (knl_is_sched_locked()) {\r\n        TOS_CPU_INT_ENABLE();\r\n        return;\r\n    }\r\n\r\n    if (task->timeslice > (k_timeslice_t)0u) {\r\n        --task->timeslice;\r\n    }\r\n\r\n    if (task->timeslice > (k_timeslice_t)0u) {\r\n        TOS_CPU_INT_ENABLE();\r\n        return;\r\n    }\r\n\r\n    readyqueue_move_head_to_tail(k_curr_task->prio);\r\n\r\n    task = readyqueue_first_task_get(prio);\r\n    if (task->timeslice_reload == (k_timeslice_t)0u) {\r\n        task->timeslice = k_robin_default_timeslice;\r\n    } else {\r\n        task->timeslice = task->timeslice_reload;\r\n    }\r\n\r\n    TOS_CPU_INT_ENABLE();\r\n    knl_sched();\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>\u4ece\u6e90\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u65f6\u95f4\u7247\u8c03\u5ea6\u7b97\u6cd5\u7684\u5b9e\u73b0\u975e\u5e38\u7b80\u5355\uff0c\u5f53\u65f6\u949f\u8282\u62cd\u6765\u4e34\u7684\u65f6\u5019\uff0c\u5c06\u5c31\u7eea\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u4efb\u52a1\u63a7\u5236\u5757\u7684\u65f6\u95f4\u7247\u503c\u9012\u51cf\uff0c\u5982\u679c\u9012\u51cf\u52300\uff0c\u5219\u79fb\u5230\u5c31\u7eea\u5217\u8868\u7684\u961f\u5c3e\u53bb\uff0c\u8ba9\u51fa\u6b64\u6b21\u6267\u884c\u673a\u4f1a\uff0c\u5185\u6838\u53d1\u751f\u8c03\u5ea6\u3002\u6211\u4eec\u91cd\u70b9\u770b knl_sched()\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>__KERNEL__ void knl_sched(void)\r\n{\r\n    TOS_CPU_CPSR_ALLOC();\r\n\r\n    if (knl_is_inirq()) {\r\n        return;\r\n    }\r\n\r\n    if (knl_is_sched_locked()) {\r\n        return;\r\n    }\r\n\r\n    TOS_CPU_INT_DISABLE();\r\n    k_next_task = readyqueue_highest_ready_task_get();\r\n    if (knl_is_self(k_next_task)) {\r\n        TOS_CPU_INT_ENABLE();\r\n        return;\r\n    }\r\n\r\n    cpu_context_switch();\r\n    TOS_CPU_INT_ENABLE();\r\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u5c31\u662f\u68c0\u67e5\u4e00\u4e0b\u5f53\u524d\u662f\u5426\u6709\u4e2d\u65ad\u53d1\u751f\uff0c\u8fd8\u6709\u68c0\u67e5\u4e00\u4e0b\u9501\u662f\u5426\u91ca\u653e\uff0c\u7136\u540e\u8c03\u7528\u51fd\u6570  TOS_CPU_INT_DISABLE()\u5173\u95ed\u4e2d\u65ad\uff0c\u8c03\u7528 cpu_context_switch() \u8fdb\u884c\u4e0a\u4e0b\u6587\u5207\u6362\u3002\u4e0d\u51fa\u610f\u5916\uff0c\u7ec8\u70b9\u4e00\u5b9a\u662f\u6c47\u7f16\u5b9e\u73b0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-5-1024x576.png\" alt=\"\" class=\"wp-image-190\" srcset=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-5-1024x576.png 1024w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-5-300x169.png 300w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-5-768x432.png 768w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-5-1536x864.png 1536w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2023\/01\/image-5.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u679c\u7136\u6211\u4eec\u5df2\u7ecf\u79bb\u5f00\u4e86c\u8bed\u8a00\u4e16\u754c\uff0c\u8fdb\u5165\u6c47\u7f16\u4e16\u754c\u3002\u770b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>port_context_switch\r\n    LDR     R0, =NVIC_INT_CTRL\r\n    LDR     R1, =NVIC_PENDSVSET\r\n    STR     R1, &#91;R0]\r\n    BX      LR<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4ec0\u4e48\uff1f\u89e6\u53d1pendsv\u5f02\u5e38\uff0c\u5230\u4e86\u8fd9\u91cc\u6211\u4eec\u5df2\u7ecf\u7834\u89e3\u4e86tos\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u5668\u7684\u57fa\u672c\u539f\u7406\u3002\u8fd9\u8868\u660etos\u7cfb\u7edf\u548cfreertos\u7684\u5b9e\u73b0\u673a\u5236\u662f\u5927\u5dee\u4e0d\u5dee\u7684\u3002<\/p>\n\n\n\n<p>\u5185\u6838\u662f\u975e\u5e38\u590d\u6742\u7684\u4e1c\u897f\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u5145\u6ee1\u656c\u754f\uff0c\u4e0d\u8981\u968f\u610f\u53bb\u4fee\u6539\uff0c\u4f46\u662f\u4e86\u89e3\u5185\u6838\u8fd8\u662f\u5f88\u6709\u5fc5\u8981\u7684\uff0c\u5426\u5219\u5e94\u7528\u5c42\u7684\u5f88\u591a\u4e1c\u897f\u6211\u4eec\u7406\u89e3\u53ef\u80fd\u90fd\u662f\u9519\u8bef\u7684\u3002\u5728\u6b64\uff0c\u771f\u7684\u592a\u4f69\u670d\u592a\u4f69\u670d\u5185\u6838\u5927\u4f6c\u4e86\uff0c\u77ac\u95f4\u89c9\u5f97\u5e94\u7528\u5c42\u5f00\u53d1\u90a3\u70b9\u56f0\u96be\u4e0d\u7b97\u4ec0\u4e48\u4e86\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u6765\u4e2a\u4f20\u9001\u95e8<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"344\" height=\"344\" src=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2020\/10\/\u5fae\u4fe1\u56fe\u7247_20201007185559.jpg\" alt=\"\" class=\"wp-image-37\" srcset=\"https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2020\/10\/\u5fae\u4fe1\u56fe\u7247_20201007185559.jpg 344w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2020\/10\/\u5fae\u4fe1\u56fe\u7247_20201007185559-300x300.jpg 300w, https:\/\/wordpress.rslly.top\/wp-content\/uploads\/2020\/10\/\u5fae\u4fe1\u56fe\u7247_20201007185559-150x150.jpg 150w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u98ce \u4efb\u52a1\u8c03\u5ea6\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u57fa\u672c\u529f\u80fd\u3002\u4efb\u52a1\u8c03\u5ea6\u4f7f\u5f97\u7528\u6237\u7a0b\u5e8f\u611f\u89c9\u81ea\u5df1\u5728\u72ec\u5360cpu\u3002\u4f46\u662fcpu\u662f\u6709\u9650\u7684\uff0c\u4efb\u52a1\u53c8\u90a3\u4e48\u591a &hellip; <a href=\"https:\/\/wordpress.rslly.top\/index.php\/2023\/01\/04\/%e7%89%a9%e8%81%94%e7%bd%91%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e7%9a%84%e4%bb%bb%e5%8a%a1%e8%b0%83%e5%ba%a6%e5%88%9d%e6%ad%a5%e8%ae%a4%e8%af%86\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u7269\u8054\u7f51\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u521d\u6b65\u8ba4\u8bc6<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"spay_email":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":2,"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":191,"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/posts\/185\/revisions\/191"}],"wp:attachment":[{"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.rslly.top\/index.php\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}