{"id":299,"date":"2024-09-22T16:43:47","date_gmt":"2024-09-22T08:43:47","guid":{"rendered":"https:\/\/chaixiangyu.cn\/?p=299"},"modified":"2024-09-22T17:25:41","modified_gmt":"2024-09-22T09:25:41","slug":"uc-os-iii","status":"publish","type":"post","link":"https:\/\/chaixiangyu.cn\/?p=299","title":{"rendered":"uC\/OS-III"},"content":{"rendered":"\n<p>Compared to evaluating whether one has the ability to develop projects using uC\/OS-III, a more fundamental question is whether uC\/OS-III is suitable for the current project. In this article, I will first compare the application scenarios of uC\/OS-III and bare-metal development, focusing on MCU resource requirements and development time. After that, I will delve into key concepts related to uC\/OS-III, which may involve interpreting uC\/OS-III kernel files. Initially, the goal is not to systematically and comprehensively grasp the knowledge, but rather to focus on absorbing and understanding the key concepts. This point-by-point approach will help gradually develop a comprehensive understanding of uC\/OS-III. The background of this article is that the author has already conducted a first demo of uC\/OS-III on the STM32F103C8T6 MCU.<\/p>\n\n\n\n<p>In uC\/OS-III (uCOS3), tasks are divided based on the priority system, where each task has a unique priority. Task division in a real-time operating system (RTOS) like uCOS3 depends on the requirements of the project, such as timing constraints, task functionality, and system complexity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Task Division in uCOS-III:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Priority-based Scheduling<\/strong>: Each task is assigned a unique priority, and the OS schedules tasks based on priority. Higher-priority tasks preempt lower-priority tasks.<\/li>\n\n\n\n<li><strong>Task Functions<\/strong>: Tasks are typically divided based on their functions, such as:\n<ul class=\"wp-block-list\">\n<li><strong>Control Tasks<\/strong>: Responsible for managing critical system operations, like motor control or sensor data processing.<\/li>\n\n\n\n<li><strong>Communication Tasks<\/strong>: Handle communication protocols like UART, SPI, or I2C for data exchange.<\/li>\n\n\n\n<li><strong>UI\/Display Tasks<\/strong>: Manage user interface updates, such as an LCD screen.<\/li>\n\n\n\n<li><strong>Background Tasks<\/strong>: Handle lower-priority activities like logging data, housekeeping, or monitoring battery status.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Preemptive Multitasking<\/strong>: uCOS3 supports preemptive multitasking, meaning high-priority tasks can interrupt lower-priority tasks, ensuring critical operations are handled on time.<\/li>\n\n\n\n<li><strong>Task Stacks<\/strong>: Each task has its own stack, and the system switches between them during context switching.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Task Division in a Real-World Project:<\/h3>\n\n\n\n<p><strong>Project Example<\/strong>: Let&#8217;s consider an <strong>industrial motor control system<\/strong> using uCOS-III.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Task 1: Motor Control (High Priority)<\/strong><br>This task is responsible for real-time control of the motor, ensuring precise speed and position control.<\/li>\n\n\n\n<li><strong>Task 2: Sensor Data Acquisition (Medium Priority)<\/strong><br>Reads sensor data like temperature, voltage, and current from the motor. It updates every few milliseconds to ensure data is available for control algorithms.<\/li>\n\n\n\n<li><strong>Task 3: Communication (Medium-Low Priority)<\/strong><br>Manages communication over UART or CAN bus with a central system to send status updates and receive commands.<\/li>\n\n\n\n<li><strong>Task 4: User Interface (Low Priority)<\/strong><br>Updates the LCD or other interfaces to show motor status, logs, and other non-critical information.<\/li>\n\n\n\n<li><strong>Task 5: System Monitoring (Lowest Priority)<\/strong><br>Background task to log data, check battery levels, and perform system diagnostics.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use uCOS-III:<\/h3>\n\n\n\n<p>uCOS-III is ideal for <strong>time-critical<\/strong> and <strong>embedded systems<\/strong> that require predictable task scheduling and management. Some typical use cases include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Industrial Automation<\/strong>: Motor control systems, PLCs, and robots where precise timing is essential.<\/li>\n\n\n\n<li><strong>Medical Devices<\/strong>: For example, ventilators or infusion pumps where system reliability and response time are critical.<\/li>\n\n\n\n<li><strong>Automotive Systems<\/strong>: Managing various in-car systems like ADAS (Advanced Driver Assistance Systems), real-time sensor data processing, and communication between systems.<\/li>\n\n\n\n<li><strong>IoT Devices<\/strong>: Real-time data acquisition, communication with servers, and local control for sensors and actuators in smart devices.<\/li>\n<\/ol>\n\n\n\n<p><strong>Example<\/strong>:<br>A <strong>smart thermostat<\/strong> system with uCOS-III might have tasks such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Temperature Control Task<\/strong> (high priority) to regulate the HVAC system.<\/li>\n\n\n\n<li><strong>Sensor Monitoring Task<\/strong> to track temperature, humidity, and air quality.<\/li>\n\n\n\n<li><strong>Wi-Fi Communication Task<\/strong> to send data to the cloud.<\/li>\n\n\n\n<li><strong>User Interface Task<\/strong> for updating the display.<\/li>\n<\/ul>\n\n\n\n<p>In such systems, the ability of uCOS-III to handle real-time constraints, multitasking, and inter-task communication makes it an excellent choice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4e92\u65a5\u91cf\u548c\u4e8c\u503c\u4fe1\u53f7\u91cf\u7684\u5f02\u540c<\/h2>\n\n\n\n<p>\u5728uC\/OS-III\u4e2d\uff0c<strong>\u4e92\u65a5\u91cf\uff08Mutex\uff09<\/strong> \u548c <strong>\u4e8c\u503c\u4fe1\u53f7\u91cf\uff08Binary Semaphore\uff09<\/strong> \u90fd\u662f\u7528\u4e8e\u4efb\u52a1\u95f4\u540c\u6b65\u548c\u8d44\u6e90\u4fdd\u62a4\u7684\u673a\u5236\uff0c\u4f46\u5b83\u4eec\u7684\u8bbe\u8ba1\u76ee\u6807\u548c\u4f7f\u7528\u573a\u666f\u6709\u6240\u4e0d\u540c\u3002\u4e0b\u9762\u662f\u5b83\u4eec\u5728uC\/OS-III\u4e2d\u7684\u5f02\u540c\u70b9\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f8\u540c\u70b9\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u540c\u6b65\u4e0e\u8d44\u6e90\u4fdd\u62a4<\/strong>\uff1a\u4e24\u8005\u90fd\u53ef\u4ee5\u7528\u4e8e\u4efb\u52a1\u4e4b\u95f4\u7684\u540c\u6b65\uff0c\u9632\u6b62\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u907f\u514d\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u7ade\u6001\u6761\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u4e8c\u503c\u72b6\u6001<\/strong>\uff1a\u4e92\u65a5\u91cf\u548c\u4e8c\u503c\u4fe1\u53f7\u91cf\u90fd\u53ea\u6709\u4e24\u79cd\u72b6\u6001\uff0c\u7c7b\u4f3c\u4e8e\u201c\u9501\u5b9a\u201d\u548c\u201c\u89e3\u9501\u201d\u3002\n<ul class=\"wp-block-list\">\n<li>\u4e92\u65a5\u91cf\uff1a\u9501\u5b9a\u6216\u672a\u9501\u5b9a\u3002<\/li>\n\n\n\n<li>\u4e8c\u503c\u4fe1\u53f7\u91cf\uff1a0\u62161\uff0c\u8868\u793a\u4fe1\u53f7\u53ef\u7528\u6216\u4e0d\u53ef\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Pend\u548cPost\u64cd\u4f5c<\/strong>\uff1a\u4e24\u8005\u90fd\u901a\u8fc7\u7c7b\u4f3c\u7684\u673a\u5236\u6765\u64cd\u4f5c\uff1a\n<ul class=\"wp-block-list\">\n<li><code>Pend<\/code>\uff1a\u7b49\u5f85\u83b7\u53d6\u4e92\u65a5\u91cf\u6216\u4fe1\u53f7\u91cf\u3002<\/li>\n\n\n\n<li><code>Post<\/code>\uff1a\u91ca\u653e\u4e92\u65a5\u91cf\u6216\u53d1\u9001\u4fe1\u53f7\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e0d\u540c\u70b9\uff1a<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u5f81<\/th><th>\u4e92\u65a5\u91cf\uff08Mutex\uff09<\/th><th>\u4e8c\u503c\u4fe1\u53f7\u91cf\uff08Binary Semaphore\uff09<\/th><\/tr><\/thead><tbody><tr><td><strong>\u8bbe\u8ba1\u76ee\u6807<\/strong><\/td><td>\u4e13\u4e3a<strong>\u4efb\u52a1\u95f4\u4e92\u65a5<\/strong>\u800c\u8bbe\u8ba1\uff0c\u4e3b\u8981\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u3002<\/td><td>\u7528\u4e8e<strong>\u4fe1\u53f7\u4f20\u9012<\/strong>\u548c\u7b80\u5355\u540c\u6b65\uff0c\u53ef\u7528\u4e8e\u4efb\u52a1\u95f4\u6216\u4e2d\u65ad\u4e0e\u4efb\u52a1\u95f4\u540c\u6b65\u3002<\/td><\/tr><tr><td><strong>\u4f18\u5148\u7ea7\u7ee7\u627f<\/strong><\/td><td>\u652f\u6301<strong>\u4f18\u5148\u7ea7\u7ee7\u627f<\/strong>\u673a\u5236\uff0c\u9632\u6b62\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898\u3002<\/td><td>\u4e0d\u652f\u6301\u4f18\u5148\u7ea7\u7ee7\u627f\uff0c\u53ef\u80fd\u5bfc\u81f4\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898\u3002<\/td><\/tr><tr><td><strong>\u62e5\u6709\u8005\u6982\u5ff5<\/strong><\/td><td>\u4e92\u65a5\u91cf\u6709\u4efb\u52a1<strong>\u62e5\u6709\u8005<\/strong>\u7684\u6982\u5ff5\uff0c\u53ea\u6709\u83b7\u5f97\u4e92\u65a5\u91cf\u7684\u4efb\u52a1\u53ef\u4ee5\u91ca\u653e\u5b83\u3002<\/td><td>\u6ca1\u6709\u62e5\u6709\u8005\u6982\u5ff5\uff0c\u4efb\u4f55\u4efb\u52a1\u6216\u4e2d\u65ad\u90fd\u53ef\u4ee5\u91ca\u653e\u4fe1\u53f7\u91cf\u3002<\/td><\/tr><tr><td><strong>\u5178\u578b\u4f7f\u7528\u573a\u666f<\/strong><\/td><td>\u4e3b\u8981\u7528\u4e8e<strong>\u9632\u6b62\u7ade\u6001\u6761\u4ef6<\/strong>\uff0c\u4f8b\u5982\u4fdd\u62a4\u5171\u4eab\u6570\u636e\u6216\u786c\u4ef6\u8d44\u6e90\u3002<\/td><td>\u7528\u4e8e<strong>\u4efb\u52a1\u95f4\u4fe1\u53f7\u4f20\u9012<\/strong>\u6216\u7b80\u5355\u7684\u540c\u6b65\uff0c\u9002\u5408\u4e2d\u65ad\u4e0e\u4efb\u52a1\u4e4b\u95f4\u901a\u4fe1\u3002<\/td><\/tr><tr><td><strong>\u9012\u5f52\u4f7f\u7528<\/strong><\/td><td>\u5141\u8bb8\u4efb\u52a1<strong>\u9012\u5f52\u5730<\/strong>\u83b7\u53d6\u540c\u4e00\u4e2a\u4e92\u65a5\u91cf\uff08\u4efb\u52a1\u53ef\u4ee5\u591a\u6b21\u83b7\u53d6\u5b83\uff09\u3002<\/td><td>\u4e0d\u5141\u8bb8\u9012\u5f52\u83b7\u53d6\uff0c\u4efb\u52a1\u4e0d\u80fd\u591a\u6b21\u83b7\u53d6\u540c\u4e00\u4e2a\u4fe1\u53f7\u91cf\u3002<\/td><\/tr><tr><td><strong>\u4e2d\u65ad\u5b89\u5168\u6027<\/strong><\/td><td>\u4e0d\u80fd\u5728<strong>\u4e2d\u65ad\u4e0a\u4e0b\u6587<\/strong>\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4e0e\u4efb\u52a1\u8c03\u5ea6\u6709\u5173\u3002<\/td><td>\u53ef\u4ee5\u5728\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\uff0c\u9002\u5408\u4e2d\u65ad\u4e0e\u4efb\u52a1\u7684\u540c\u6b65\u3002<\/td><\/tr><tr><td><strong>\u6027\u80fd\u5f00\u9500<\/strong><\/td><td>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4e92\u65a5\u91cf\u7684\u5f00\u9500\u8f83\u9ad8\uff0c\u5c24\u5176\u662f\u4f18\u5148\u7ea7\u7ee7\u627f\u673a\u5236\u3002<\/td><td>\u4e8c\u503c\u4fe1\u53f7\u91cf\u7684\u5f00\u9500\u8f83\u5c0f\uff0c\u9002\u5408\u5feb\u901f\u540c\u6b65\u64cd\u4f5c\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e92\u65a5\u91cf\uff08Mutex\uff09\u7684\u7279\u6027\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f18\u5148\u7ea7\u7ee7\u627f<\/strong>\uff1a\u8fd9\u662f\u4e92\u65a5\u91cf\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u3002\u5982\u679c\u4e00\u4e2a\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u7b49\u5f85\u4e00\u4e2a\u88ab\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u5360\u7528\u7684\u4e92\u65a5\u91cf\uff0c\u7cfb\u7edf\u4f1a\u6682\u65f6\u5c06\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u63d0\u5347\u5230\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u7684\u7ea7\u522b\uff0c\u4ee5\u9632\u6b62\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u957f\u65f6\u95f4\u88ab\u963b\u585e\u3002\u8fd9\u6709\u6548\u89e3\u51b3\u4e86<strong>\u4f18\u5148\u7ea7\u53cd\u8f6c<\/strong>\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u6240\u6709\u8005\u6982\u5ff5<\/strong>\uff1a\u53ea\u6709\u5360\u6709\u4e92\u65a5\u91cf\u7684\u4efb\u52a1\u53ef\u4ee5\u91ca\u653e\u5b83\u3002\u8fd9\u4f7f\u5f97\u5b83\u7279\u522b\u9002\u5408\u5728\u591a\u4efb\u52a1\u4e2d\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u503c\u4fe1\u53f7\u91cf\uff08Binary Semaphore\uff09\u7684\u7279\u6027\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u65e0\u4f18\u5148\u7ea7\u7ee7\u627f<\/strong>\uff1a\u4e8c\u503c\u4fe1\u53f7\u91cf\u4e0d\u63d0\u4f9b\u4f18\u5148\u7ea7\u7ee7\u627f\uff0c\u56e0\u6b64\u53ef\u80fd\u51fa\u73b0\u4f18\u5148\u7ea7\u53cd\u8f6c\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u7b80\u5355\u7684\u540c\u6b65<\/strong>\uff1a\u4e8c\u503c\u4fe1\u53f7\u91cf\u6ca1\u6709\u201c\u62e5\u6709\u8005\u201d\u6982\u5ff5\uff0c\u56e0\u6b64\u4efb\u4f55\u4efb\u52a1\u6216\u4e2d\u65ad\u90fd\u53ef\u4ee5\u91ca\u653e\u5b83\u3002\u9002\u5408\u4e8e\u4efb\u52a1\u4e4b\u95f4\u7684\u7b80\u5355\u540c\u6b65\u6216\u4fe1\u53f7\u4f20\u9012\uff0c\u5c24\u5176\u662f\u4e2d\u65ad\u4e0e\u4efb\u52a1\u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f\u5bf9\u6bd4\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e92\u65a5\u91cf<\/strong>\uff1a\u5f53\u591a\u4e2a\u4efb\u52a1\u9700\u8981\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff08\u5982\u5168\u5c40\u53d8\u91cf\u3001\u786c\u4ef6\u8d44\u6e90\uff09\u65f6\uff0c\u5e94\u4f7f\u7528\u4e92\u65a5\u91cf\u3002\u5c24\u5176\u662f\u9700\u8981\u9632\u6b62\u7ade\u6001\u6761\u4ef6\u548c\u4f18\u5148\u7ea7\u53cd\u8f6c\u65f6\uff0c\u4e92\u65a5\u91cf\u66f4\u5408\u9002\u3002<\/li>\n\n\n\n<li><strong>\u4e8c\u503c\u4fe1\u53f7\u91cf<\/strong>\uff1a\u9002\u7528\u4e8e\u4efb\u52a1\u4e4b\u95f4\u7684\u540c\u6b65\u6216\u4e2d\u65ad\u4e0e\u4efb\u52a1\u7684\u4fe1\u53f7\u4f20\u9012\u3002\u4f8b\u5982\uff0c\u67d0\u4e2a\u4efb\u52a1\u9700\u8981\u7b49\u5f85\u4e00\u4e2a\u4e2d\u65ad\u4e8b\u4ef6\u7684\u53d1\u751f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e92\u65a5\u91cf\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>c\u590d\u5236\u4ee3\u7801<code>OS_MUTEX MyMutex;\n\nvoid Task1(void *p_arg) {\n    OS_ERR err;\n\n    while (1) {\n        \/\/ \u83b7\u53d6\u4e92\u65a5\u91cf\n        OSMutexPend(&amp;MyMutex, 0, OS_OPT_PEND_BLOCKING, NULL, &amp;err);\n        \n        \/\/ \u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\n        \/\/ ... \n        \n        \/\/ \u91ca\u653e\u4e92\u65a5\u91cf\n        OSMutexPost(&amp;MyMutex, OS_OPT_POST_NONE, &amp;err);\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e8c\u503c\u4fe1\u53f7\u91cf\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>c\u590d\u5236\u4ee3\u7801<code>OS_SEM MySem;\n\nvoid Task1(void *p_arg) {\n    OS_ERR err;\n\n    while (1) {\n        \/\/ \u7b49\u5f85\u4fe1\u53f7\u91cf\n        OSSemPend(&amp;MySem, 0, OS_OPT_PEND_BLOCKING, NULL, &amp;err);\n        \n        \/\/ \u6536\u5230\u4fe1\u53f7\u540e\u6267\u884c\u64cd\u4f5c\n        \/\/ ... \n    }\n}\n\nvoid ISR_Handler(void) {\n    OS_ERR err;\n\n    \/\/ \u4e2d\u65ad\u670d\u52a1\u4f8b\u7a0b\u4e2d\u53d1\u9001\u4fe1\u53f7\u91cf\n    OSSemPost(&amp;MySem, OS_OPT_POST_1, &amp;err);\n}\n<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e92\u65a5\u91cf<\/strong>\u9002\u7528\u4e8e\u591a\u4efb\u52a1\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u5c24\u5176\u662f\u5728\u907f\u514d\u4f18\u5148\u7ea7\u53cd\u8f6c\u7684\u573a\u666f\u4e0b\u3002<\/li>\n\n\n\n<li><strong>\u4e8c\u503c\u4fe1\u53f7\u91cf<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u4efb\u52a1\u95f4\u540c\u6b65\u6216\u4e2d\u65ad\u548c\u4efb\u52a1\u95f4\u7684\u901a\u4fe1\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4e92\u65a5\u91cf\u7684\u4f18\u5148\u7ea7\u7ee7\u627f\u673a\u5236<\/h2>\n\n\n\n<p>\u5728uC\/OS-III\u4e2d\uff0c\u4e92\u65a5\u91cf\u652f\u6301<strong>\u4f18\u5148\u7ea7\u7ee7\u627f<\/strong>\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u9632\u6b62<strong>\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898<\/strong>\u3002\u4f18\u5148\u7ea7\u53cd\u8f6c\u662f\u6307\u4e00\u4e2a\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u56e0\u4e3a\u7b49\u5f85\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u91ca\u653e\u8d44\u6e90\u800c\u88ab\u963b\u585e\uff0c\u800c\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u5374\u53ef\u80fd\u88ab\u5176\u4ed6\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u62a2\u5360\uff0c\u5bfc\u81f4\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u957f\u65f6\u95f4\u65e0\u6cd5\u8fd0\u884c\u3002<\/p>\n\n\n\n<p><strong>\u4f18\u5148\u7ea7\u7ee7\u627f<\/strong>\u673a\u5236\u901a\u8fc7\u5728\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u6301\u6709\u4e92\u65a5\u91cf\u65f6\uff0c\u5c06\u8be5\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u6682\u65f6\u63d0\u5347\u5230\u4e0e\u7b49\u5f85\u8be5\u4e92\u65a5\u91cf\u7684\u6700\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u76f8\u540c\u7684\u7ea7\u522b\u3002\u8fd9\u6837\uff0c\u6301\u6709\u4e92\u65a5\u91cf\u7684\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u80fd\u591f\u5c3d\u5feb\u5b8c\u6210\u5e76\u91ca\u653e\u8d44\u6e90\uff0c\u51cf\u5c11\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u7684\u7b49\u5f85\u65f6\u95f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u5148\u7ea7\u7ee7\u627f\u7684\u5b9e\u73b0<\/h3>\n\n\n\n<p>\u5728uC\/OS-III\u4e2d\uff0c\u5f53\u4e00\u4e2a\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u6301\u6709\u4e92\u65a5\u91cf\u65f6\uff0c\u4e14\u4e00\u4e2a\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u7b49\u5f85\u8be5\u4e92\u65a5\u91cf\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u63d0\u5347\u6301\u6709\u4e92\u65a5\u91cf\u7684\u4efb\u52a1\u4f18\u5148\u7ea7<\/strong>\uff1auC\/OS-III\u4f1a\u4e34\u65f6\u5c06\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u63d0\u5347\u5230\u4e0e\u7b49\u5f85\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u76f8\u540c\u3002<\/li>\n\n\n\n<li><strong>\u4efb\u52a1\u6267\u884c\u5e76\u91ca\u653e\u4e92\u65a5\u91cf<\/strong>\uff1a\u5f53\u6301\u6709\u4e92\u65a5\u91cf\u7684\u4efb\u52a1\u5b8c\u6210\u5176\u5de5\u4f5c\u5e76\u91ca\u653e\u4e92\u65a5\u91cf\u540e\uff0c\u7cfb\u7edf\u5c06\u5176\u4f18\u5148\u7ea7\u6062\u590d\u5230\u539f\u6765\u7684\u7ea7\u522b\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u7ee7\u7eed\u6267\u884c<\/strong>\uff1a\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u63a5\u7ba1\u4e92\u65a5\u91cf\uff0c\u7ee7\u7eed\u6267\u884c\u5b83\u7684\u5de5\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001uC\/OS-III\u64cd\u4f5c\u7cfb\u7edf\u5728\u540e\u53f0\u81ea\u52a8\u5904\u7406\u548c\u7ba1\u7406\u4f18\u5148\u7ea7\u7ee7\u627f\u673a\u5236\u7684\u4ee3\u7801<\/h2>\n\n\n\n<p>uC\/OS-III\u7684<code>os_mutex.c<\/code>\u6587\u4ef6\u4e2d\u5305\u542b\u4e86\u4e92\u65a5\u91cf\u7684\u76f8\u5173\u5b9e\u73b0\u4ee3\u7801\uff0c\u5305\u62ec\u521b\u5efa\u3001\u83b7\u53d6\uff08<code>OSMutexPend()<\/code>\uff09\u3001\u91ca\u653e\uff08<code>OSMutexPost()<\/code>\uff09\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u5728uC\/OS-III\u4e2d\uff0c\u4f18\u5148\u7ea7\u7ee7\u627f\u673a\u5236\u662f\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u81ea\u52a8\u5904\u7406\u7684\uff0c\u5b83\u5e76\u4e0d\u9700\u8981\u7528\u6237\u5728\u5e94\u7528\u4ee3\u7801\u4e2d\u624b\u52a8\u7f16\u5199\u3002\u4f18\u5148\u7ea7\u7ee7\u627f\u7684\u903b\u8f91\u5b9e\u9645\u4e0a\u5df2\u7ecf\u5728uC\/OS-III\u7684\u4e92\u65a5\u91cf\u76f8\u5173\u7684\u5185\u6838\u4ee3\u7801\u4e2d\u5b9e\u73b0\u4e86\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u51fd\u6570\u4e2d\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6838\u5fc3\u673a\u5236\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>OSMutexPend()<\/strong>\uff1a\u4efb\u52a1\u5c1d\u8bd5\u83b7\u53d6\u4e92\u65a5\u91cf\u7684\u51fd\u6570\u3002\u5982\u679c\u4e92\u65a5\u91cf\u5df2\u7ecf\u88ab\u5176\u4ed6\u4efb\u52a1\u5360\u7528\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u51b3\u5b9a\u662f\u5426\u8981\u63d0\u5347\u5360\u7528\u4e92\u65a5\u91cf\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\uff08\u5982\u679c\u7b49\u5f85\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u66f4\u9ad8\uff09\u3002<\/li>\n\n\n\n<li><strong>OSMutexPost()<\/strong>\uff1a\u5f53\u4efb\u52a1\u91ca\u653e\u4e92\u65a5\u91cf\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u6062\u590d\u8be5\u4efb\u52a1\u7684\u539f\u59cb\u4f18\u5148\u7ea7\uff0c\u5e76\u8ba9\u7b49\u5f85\u7684\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u63a5\u7ba1\u4e92\u65a5\u91cf\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5177\u4f53\u5b9e\u73b0\u601d\u8def<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <code>OSMutexPend()<\/code>\uff1a\u7b49\u5f85\u4e92\u65a5\u91cf\u7684\u4efb\u52a1<\/h4>\n\n\n\n<p>\u5f53\u4efb\u52a1\u8c03\u7528<code>OSMutexPend()<\/code>\u65f6\uff0c\u5982\u679c\u4e92\u65a5\u91cf\u5df2\u7ecf\u88ab\u5176\u4ed6\u4efb\u52a1\u5360\u7528\uff0c\u7cfb\u7edf\u4f1a\u8fdb\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u68c0\u67e5\u5360\u7528\u4e92\u65a5\u91cf\u4efb\u52a1\u7684\u4f18\u5148\u7ea7<\/strong>\uff1a\u5982\u679c\u5360\u7528\u4e92\u65a5\u91cf\u7684\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u5f53\u524d\u7b49\u5f85\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\uff0c\u7cfb\u7edf\u4f1a\u4e34\u65f6\u63d0\u5347\u5360\u7528\u4e92\u65a5\u91cf\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u5230\u4e0e\u7b49\u5f85\u4efb\u52a1\u76f8\u540c\u7684\u4f18\u5148\u7ea7\uff08\u5373<strong>\u4f18\u5148\u7ea7\u7ee7\u627f<\/strong>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4efb\u52a1\u8fdb\u5165\u7b49\u5f85\u72b6\u6001<\/strong>\uff1a\u7b49\u5f85\u4efb\u52a1\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u76f4\u5230\u4e92\u65a5\u91cf\u88ab\u91ca\u653e\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u76f8\u5173\u4ee3\u7801\u7684\u6838\u5fc3\u903b\u8f91\u4f4d\u4e8e<code>OSMutexPend()<\/code>\u51fd\u6570\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>void OSMutexPend(OS_MUTEX *p_mutex, OS_TICK timeout, OS_OPT opt, OS_TICK *p_ts, OS_ERR *p_err) {\n    ...\n    if (p_mutex-&gt;OwnerTCB != (OS_TCB *)0) {  \/\/ \u4e92\u65a5\u91cf\u5df2\u88ab\u5360\u7528\n        if (p_mutex-&gt;OwnerTCB-&gt;Prio &gt; p_tcb-&gt;Prio) {  \/\/ \u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u6bd4\u6301\u6709\u4e92\u65a5\u91cf\u7684\u4efb\u52a1\u9ad8\n            p_mutex-&gt;OwnerTCB-&gt;Prio = p_tcb-&gt;Prio;  \/\/ \u63d0\u5347\u6301\u6709\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\n        }\n        ...\n        \/\/ \u5f53\u524d\u4efb\u52a1\u8fdb\u5165\u7b49\u5f85\u961f\u5217\uff0c\u7b49\u5f85\u4e92\u65a5\u91cf\u88ab\u91ca\u653e\n        OS_Pend(&amp;p_mutex-&gt;PendList, (OS_TICK)timeout, (OS_OPT)opt, (OS_TICK *)p_ts, (OS_ERR *)&amp;err);\n    }\n    ...\n}\n<\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. <code>OSMutexPost()<\/code>\uff1a\u91ca\u653e\u4e92\u65a5\u91cf\u7684\u4efb\u52a1<\/h4>\n\n\n\n<p>\u5f53\u4efb\u52a1\u5b8c\u6210\u4e86\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u5e76\u8c03\u7528<code>OSMutexPost()<\/code>\u91ca\u653e\u4e92\u65a5\u91cf\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8fdb\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6062\u590d\u4efb\u52a1\u539f\u59cb\u4f18\u5148\u7ea7<\/strong>\uff1a\u5982\u679c\u8be5\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u66fe\u7ecf\u56e0\u4e3a\u4f18\u5148\u7ea7\u7ee7\u627f\u800c\u88ab\u63d0\u5347\uff0c\u7cfb\u7edf\u4f1a\u5c06\u5b83\u7684\u4f18\u5148\u7ea7\u6062\u590d\u4e3a\u539f\u59cb\u4f18\u5148\u7ea7\u3002<\/li>\n\n\n\n<li><strong>\u5524\u9192\u7b49\u5f85\u7684\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1<\/strong>\uff1a\u5982\u679c\u6709\u4efb\u52a1\u5728\u7b49\u5f85\u4e92\u65a5\u91cf\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u4e92\u65a5\u91cf\u5206\u914d\u7ed9\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u4efb\u52a1\uff0c\u5e76\u8ba9\u5b83\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u76f8\u5173\u4ee3\u7801\u7684\u6838\u5fc3\u903b\u8f91\u4f4d\u4e8e<code>OSMutexPost()<\/code>\u51fd\u6570\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c\u590d\u5236\u4ee3\u7801<code>void OSMutexPost(OS_MUTEX *p_mutex, OS_OPT opt, OS_ERR *p_err) {\n    ...\n    if (p_mutex-&gt;OwnerTCB != (OS_TCB *)0) {  \/\/ \u68c0\u67e5\u4e92\u65a5\u91cf\u6301\u6709\u8005\n        if (p_mutex-&gt;OwnerTCB-&gt;Prio != p_mutex-&gt;OwnerOriginalPrio) {  \/\/ \u68c0\u67e5\u662f\u5426\u6709\u4f18\u5148\u7ea7\u7ee7\u627f\n            p_mutex-&gt;OwnerTCB-&gt;Prio = p_mutex-&gt;OwnerOriginalPrio;  \/\/ \u6062\u590d\u4efb\u52a1\u7684\u539f\u59cb\u4f18\u5148\u7ea7\n        }\n        ...\n        \/\/ \u5524\u9192\u7b49\u5f85\u4efb\u52a1\n        OS_PendListPost(&amp;p_mutex-&gt;PendList, (void *)p_mutex, (OS_OPT)opt, (OS_ERR *)&amp;err);\n    }\n    ...\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f18\u5148\u7ea7\u7ee7\u627f\u7684\u4e3b\u8981\u903b\u8f91<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u5360\u7528\u4e92\u65a5\u91cf\uff0c\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u8bd5\u56fe\u83b7\u53d6\u65f6\uff0c\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u88ab\u4e34\u65f6\u63d0\u5347\u5230\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u7684\u7ea7\u522b\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u7531<code>OSMutexPend()<\/code>\u5185\u90e8\u81ea\u52a8\u5b8c\u6210\u3002<\/li>\n\n\n\n<li>\u5f53\u4f4e\u4f18\u5148\u7ea7\u4efb\u52a1\u91ca\u653e\u4e92\u65a5\u91cf\u65f6\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u4f1a\u88ab\u6062\u590d\u5230\u521d\u59cb\u503c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5728<code>OSMutexPost()<\/code>\u4e2d\u5b8c\u6210\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u00b5C\/OS-III\u00ae to FreeRTOS Migration Guide Introduction<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u7f51\u5740\u4ecb\u7ecd\u4e86\u5982\u4f55\u5c06 \u00b5C\/OS&nbsp;-III \u8fc1\u79fb\u5230 FreeRTOS\uff0c\u9002\u7528\u4e8e\u5e0c\u671b\u5c06\u57fa\u4e8e \u00b5C\/OS&nbsp;-III \u7684\u5e94\u7528\u7a0b\u5e8f\u8fc1\u79fb\u5230 FreeRTOS \u7684\u5ba2\u6237\u3002\u60a8\u4f1a\u53d1\u73b0\u5c06 \u00b5C\/OS&nbsp;-III \u5e94\u7528\u7a0b\u5e8f\u8fc1\u79fb\u5230 FreeRTOS \u975e\u5e38\u7b80\u5355\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u53ea\u9700\u51e0\u4e2a\u5c0f\u65f6\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/wiki.analog.com\/resources\/tools-software\/freertos\/migration-guide\/ucos-to-freertos-migration-guide#ucos-iii_to_freertos_migration_guide\">https:\/\/wiki.analog.com\/resources\/tools-software\/freertos\/migration-guide\/ucos-to-freertos-migration-guide#ucos-iii_to_freertos_migration_guide<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compared to evaluating whether one has the ability to d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":302,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=299"}],"version-history":[{"count":9,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":310,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions\/310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/media\/302"}],"wp:attachment":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}