rtos.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. .. _design_rtos:
  2. RTOS
  3. ====
  4. .. _design_rtos_overview:
  5. Overview
  6. --------
  7. In Nuclei SDK, we have support four most-used RTOSes in the world,
  8. **FreeRTOS**, **UCOSII**, **ThreadX** and **RT-Thread Nano**.
  9. Our RTOS port require Nuclei ECLIC interrupt controller, please make sure
  10. your Nuclei CPU is configured with ECLIC present.
  11. If you want to use RTOS in your application, you can choose one
  12. of the supported RTOSes.
  13. .. note::
  14. - From 0.9.0, we have fixed task stack alignment to match psABI requirement,
  15. please see changes happened in ``RTOS`` folder in git repo.
  16. - When you want to develop RTOS application in Nuclei SDK, please
  17. don't reconfigure ``SysTimer`` and ``SysTimer Software Interrupt``,
  18. since it is already used by RTOS portable code.
  19. .. _design_rtos_freertos:
  20. FreeRTOS
  21. --------
  22. `FreeRTOS`_ is a market-leading real-time operating system (RTOS) for
  23. microcontrollers and small microprocessors.
  24. In our FreeRTOS portable code, we are using ``SysTimer Interrupt`` as RTOS SysTick
  25. Interrupt, and using ``SysTimer Software Interrupt`` to do task switch.
  26. These two interrupts are kept as lowest level, and ``SysTimer Interrupt``
  27. is initialized as non-vector interrupt, and ``SysTimer Software Interrupt``
  28. is initialized as vector interrupt and interrupt handler implemented using asm code.
  29. In our FreeRTOS porting, we also allow FreeRTOS configuration variable
  30. ``configMAX_SYSCALL_INTERRUPT_PRIORITY`` which can be find in https://www.freertos.org/a00110.html.
  31. The ``configMAX_SYSCALL_INTERRUPT_PRIORITY`` should be set to be a
  32. absolute interrupt level range from 1 to (2^lvlbits-1) while ``lvlbits = min(nlbits, CLICINTCTLBITS)``.
  33. If you set ``configMAX_SYSCALL_INTERRUPT_PRIORITY`` to value above the accepted
  34. value range, it will use the max value.
  35. To avoid **DEAD LOCK** in FreeRTOS SMP, we recommended when there is no idle priority(0) application task
  36. created for each CORE, ``configIDLE_SHOULD_YIELD`` must be set to ``0``, and to stay low power for
  37. idle task with nothing to do, it is recommend to execute ``__WFI()`` in background.
  38. If you want to learn about how to use FreeRTOS APIs, you need to go to
  39. its website to learn the FreeRTOS documentation in its website.
  40. In Nuclei SDK, if you want to use **FreeRTOS** in your application, you need
  41. to add ``RTOS = FreeRTOS`` in your application Makefile.
  42. And in your application code, you need to do the following things:
  43. * Add FreeRTOS configuration file -> ``FreeRTOSConfig.h``
  44. * Include FreeRTOS header files
  45. Now we also support FreeRTOS SMP version, about SMP version, please refer to
  46. https://www.freertos.org/symmetric-multiprocessing-introduction.html , and we also
  47. provide freertos smpdemo example in our SDK, you can find it in
  48. ``application\freertos\smpdemo``.
  49. To use FreeRTOS SMP version for 2 Core SMP CPU, you need to add ``SMP = 2`` in your application Makefile.
  50. And also you need to make sure your application code is placed and run on shared memory which can be
  51. accessed by both CPUs. When ``SMP=2`` is specified, it will define extra requried macro called ``configNUMBER_OF_CORES``,
  52. for details, please check ``OS/FreeRTOS/build.mk``.
  53. .. note::
  54. * From 0.9.0, when ``configMAX_SYSCALL_INTERRUPT_PRIORITY >= 255``, the FreeRTOS interrupt masking will
  55. use MSTATUS.MIE to disable/enable interrupts, otherwise it will use ECLIC MTH as before.
  56. * You can check the ``application\freertos\`` for freertos application reference
  57. * From Nuclei SDK 0.6.0, we introduced FreeRTOS SMP support, both Nuclei RV32 and RV64 processors are supported.
  58. * Current version of FreeRTOS used in Nuclei SDK is ``V11.1.0``
  59. * If you want to change the OS ticks per seconds, you can change the ``configTICK_RATE_HZ``
  60. defined in ``FreeRTOSConfig.h``
  61. More information about FreeRTOS get started, please click
  62. https://www.freertos.org/FreeRTOS-quick-start-guide.html
  63. .. _design_rtos_ucosii:
  64. UCOSII
  65. ------
  66. `UCOSII`_ a priority-based preemptive real-time kernel for microprocessors,
  67. written mostly in the programming language C. It is intended for use in embedded systems.
  68. In our UCOSII portable code, we are using ``SysTimer Interrupt`` as RTOS SysTick
  69. Interrupt, and using ``SysTimer Software Interrupt`` to do task switch.
  70. If you want to learn about ``UCOSII``, please click https://www.micrium.com/books/ucosii/
  71. We are using the opensource version of UC-OS2 source code from https://github.com/SiliconLabs/uC-OS2,
  72. with optimized code for Nuclei RISC-V processors.
  73. In Nuclei SDK, if you want to use **UCOSII** in your application, you need
  74. to add ``RTOS = UCOSII`` in your application Makefile.
  75. And in your application code, you need to do the following things:
  76. * Add UCOSII application configuration header file -> ``app_cfg.h`` and ``os_cfg.h``
  77. * Add application hook source file -> ``app_hooks.c``
  78. * Include UCOSII header files
  79. .. note::
  80. * From 0.9.0, we have fixed UCOS-II interrupt masking to use MSTATUS.MIE only,
  81. no longer mess up with ECLIC MTH.
  82. * You can check the ``application\ucosii\`` for ucosii application reference
  83. * The UCOS-II application configuration template files can also be found in
  84. https://github.com/SiliconLabs/uC-OS2/tree/master/Cfg/Template
  85. * Current version of UCOSII used in Nuclei SDK is ``V2.93.00``
  86. * If you want to change the OS ticks per seconds, you can change the ``OS_TICKS_PER_SEC``
  87. defined in ``os_cfg.h``
  88. .. warning::
  89. * For Nuclei SDK release > v0.2.2, the UCOSII source code is replaced using the
  90. version from https://github.com/SiliconLabs/uC-OS2/, and application development
  91. for UCOSII is also changed, the ``app_cfg.h``, ``os_cfg.h`` and ``app_hooks.c`` files
  92. are required in application source code.
  93. .. _design_rtos_rtthread:
  94. RT-Thread
  95. ---------
  96. `RT-Thread`_ was born in 2006, it is an open source, neutral,
  97. and community-based real-time operating system (RTOS).
  98. RT-Thread is mainly written in C language, easy to understand and easy
  99. to port(can be quickly port to a wide range of mainstream MCUs and module chips).
  100. It applies object-oriented programming methods to real-time system design,
  101. making the code elegant, structured, modular, and very tailorable.
  102. In our support for RT-Thread, we get the source code of RT-Thread from a project
  103. called `RT-Thread Nano`_, which only provide kernel code of RT-Thread, which is easy
  104. to be integrated with Nuclei SDK.
  105. In our RT-Thread portable code, we are using ``SysTimer Interrupt`` as RTOS SysTick
  106. Interrupt, and using ``SysTimer Software Interrupt`` to do task switch.
  107. And also the ``rt_hw_board_init`` function is implemented in our portable code.
  108. If you want to learn about ``RT-Thread``, please click:
  109. * For Chinese version, click https://www.rt-thread.org/document/site/
  110. * For English version, click https://github.com/RT-Thread/rt-thread#documentation
  111. In Nuclei SDK, if you want to use **RT-Thread** in your application, you need
  112. to add ``RTOS = RTThread`` in your application Makefile.
  113. And in your application code, you need to do the following things:
  114. * Add RT-Thread application configuration header file -> ``rtconfig.h``
  115. * Include RT-Thread header files
  116. * If you want to enable RT-Thread MSH feature, just add ``RTTHREAD_MSH := 1`` in
  117. your application Makefile.
  118. .. note::
  119. * We also maintained RT-Thread fork repo as described in https://github.com/riscv-mcu/rt-thread/issues/1,
  120. which support RV32 and RV64 and also support SMP feature.
  121. * You can check the ``application\rtthread\`` for rtthread application reference
  122. * In RT-Thread, the ``main`` function is created as a RT-Thread thread,
  123. so you don't need to do any OS initialization work, it is done before ``main``
  124. * We also provide good support directly through RT-Thread official repo,
  125. you can check Nuclei processor support for RT-Thread in `RT-Thread BSP For Nuclei`_.
  126. .. _design_rtos_threadx:
  127. ThreadX
  128. -------
  129. `Eclipse ThreadX`_ offers a vendor-neutral, open source, safety certified OS for real-time applications,
  130. all under a permissive license. It stands alone as the first and only RTOS with this unique blend of
  131. attributes to meet a wide range of needs that will benefit industry adopters, developers and end users alike.
  132. Microsoft has contributed the Azure RTOS technology to the Eclipse Foundation.
  133. With the Eclipse Foundation as its new home, Azure RTOS now becomes Eclipse ThreadX – an advanced embedded
  134. development suite including a small but powerful operating system that provides reliable, ultra-fast performance
  135. for resource-constrained devices.
  136. ThreadX is IEC 61508, IEC 62304, ISO 26262, and EN 50128 conformance certified by SGS-TÜV Saar.
  137. ThreadX has also achieved EAL4+ Common Criteria security certification.
  138. These certifications are a big differentiator, and are unprecedented in the industry.
  139. They are a game changer, as there are currently no open source RTOS's which have them.
  140. In our ThreadX portable code, we are using ``SysTimer Interrupt`` as RTOS SysTick
  141. Interrupt, and using ``SysTimer Software Interrupt`` to do task switch.
  142. If you want to learn about ``Eclipse ThreadX``, please click:
  143. * For introduction of Eclipse ThreadX, click https://eclipse-foundation.blog/2023/11/21/introducing-eclipse-threadx/
  144. * For ThreadX documentation, click https://github.com/eclipse-threadx/rtos-docs/blob/main/rtos-docs/threadx/index.md
  145. In Nuclei SDK, if you want to use **ThreadX** in your application, you need
  146. to add ``RTOS = ThreadX`` in your application Makefile.
  147. And in your application code, you need to do the following things:
  148. * Add ThreadX application configuration header file -> ``tx_user.h``
  149. * Include ThreadX header files
  150. .. note::
  151. * ThreadX itself doesn't have a idle task, see https://github.com/eclipse-threadx/threadx/blob/acf2e57606361f3fa95cc5f9bf8c0370f2c4b898/utility/rtos_compatibility_layers/FreeRTOS/readme.md?plain=1#L113-L114
  152. * From Nuclei SDK 0.9.0, we bring support for ThreadX SMP support, and also idle task is by default emulated in Nuclei RISC-V portable code now.
  153. * You can check the ``application\threadx\`` for threadx application reference
  154. * Currently we only support single core version, the SMP version is not yet supported.
  155. .. _design_rtos_others:
  156. Others
  157. ------
  158. We also support other RTOSes, but not maintained in Nuclei SDK, please see following links:
  159. - **OpenHarmony LiteOS-M**: https://github.com/riscv-mcu/kernel_liteos_m/tree/nuclei/OpenHarmony-3.0-LTS/targets/riscv_nuclei_evalsoc_gcc
  160. - **RT-Thread**: https://github.com/riscv-mcu/rt-thread/issues/1
  161. - **Nuttx variants such Appache NuttX, Xiaomi Vela, LiAuto HaloOS**: https://github.com/riscv-mcu/nuttx/tree/nuclei_trunk/Documentation/platforms/risc-v/nuclei-evalsoc/boards/nuclei-fpga-eval
  162. - **Zephyr RTOS**: https://github.com/riscv-mcu/zephyr/blob/nuclei/4.1-branch/boards/nuclei/fpga_eval/doc/index.rst
  163. - **Little Kernel**: https://github.com/littlekernel/lk/pull/281
  164. .. _FreeRTOS: https://www.freertos.org/
  165. .. _UCOSII: https://www.micrium.com/
  166. .. _RT_Thread: https://www.rt-thread.org/
  167. .. _RT-Thread Nano: https://github.com/RT-Thread/rtthread-nano
  168. .. _Eclipse ThreadX: https://github.com/eclipse-threadx/threadx
  169. .. _RT-Thread BSP For Nuclei: https://github.com/riscv-mcu/rt-thread/blob/nuclei/lts-v4.1.x/bsp/nuclei/nuclei_fpga_eval