kernel_8hpp_source.tex 6.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. \doxysection{kernel.\+hpp}
  2. \label{kernel_8hpp_source}\index{C:/Users/yoneda/Documents/Pico\_Projects/tinykernel/tinykernel/include/tinykernel/kernel.hpp@{C:/Users/yoneda/Documents/Pico\_Projects/tinykernel/tinykernel/include/tinykernel/kernel.hpp}}
  3. \textbf{ [詳解]}
  4. \begin{DoxyCode}{0}
  5. \DoxyCodeLine{00001\ \textcolor{comment}{//\ tinykernel:\ minimal\ preemptive\ task\ scheduler\ for\ RP2350\ (Pico\ 2,\ Cortex-\/M33)}}
  6. \DoxyCodeLine{00002\ \textcolor{preprocessor}{\#pragma\ once}}
  7. \DoxyCodeLine{00003\ }
  8. \DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ <stdint.h>}}
  9. \DoxyCodeLine{00005\ \textcolor{preprocessor}{\#include\ <stddef.h>}}
  10. \DoxyCodeLine{00006\ }
  11. \DoxyCodeLine{00007\ \textcolor{preprocessor}{\#include\ "{}pico/runtime.h"{}}\ \ \textcolor{comment}{//\ PICO\_RUNTIME\_INIT\_FUNC\ (static\ task\ registration)}}
  12. \DoxyCodeLine{00008\ }
  13. \DoxyCodeLine{00010\ \textcolor{preprocessor}{\#ifndef\ TK\_TICKS\_PER\_SEC}}
  14. \DoxyCodeLine{00011\ \textcolor{preprocessor}{\#define\ TK\_TICKS\_PER\_SEC\ 1000u}}
  15. \DoxyCodeLine{00012\ \textcolor{preprocessor}{\#endif}}
  16. \DoxyCodeLine{00013\ }
  17. \DoxyCodeLine{00015\ \textcolor{preprocessor}{\#ifndef\ TK\_TIME\_SLICE\_MS}}
  18. \DoxyCodeLine{00016\ \textcolor{preprocessor}{\#define\ TK\_TIME\_SLICE\_MS\ 10u}}
  19. \DoxyCodeLine{00017\ \textcolor{preprocessor}{\#endif}}
  20. \DoxyCodeLine{00018\ }
  21. \DoxyCodeLine{00020\ \textcolor{preprocessor}{\#ifndef\ TK\_MAX\_TASKS}}
  22. \DoxyCodeLine{00021\ \textcolor{preprocessor}{\#define\ TK\_MAX\_TASKS\ 32u}}
  23. \DoxyCodeLine{00022\ \textcolor{preprocessor}{\#endif}}
  24. \DoxyCodeLine{00023\ }
  25. \DoxyCodeLine{00025\ \textcolor{preprocessor}{\#ifndef\ TK\_DEFAULT\_STACK\_SIZE}}
  26. \DoxyCodeLine{00026\ \textcolor{preprocessor}{\#define\ TK\_DEFAULT\_STACK\_SIZE\ (4\ *\ 1024)}}
  27. \DoxyCodeLine{00027\ \textcolor{preprocessor}{\#endif}}
  28. \DoxyCodeLine{00028\ }
  29. \DoxyCodeLine{00029\ \textcolor{keyword}{namespace\ }tk\ \{}
  30. \DoxyCodeLine{00030\ }
  31. \DoxyCodeLine{00032\ \textcolor{keyword}{using\ }TaskFn\ =\ void\ (*)(\textcolor{keywordtype}{void}*\ arg);}
  32. \DoxyCodeLine{00033\ }
  33. \DoxyCodeLine{00039\ \textcolor{keyword}{enum}\ Prio\ :\ uint32\_t\ \{}
  34. \DoxyCodeLine{00040\ \ \ \ \ PRIO\_REALTIME\ =\ 1,\ \ \ }
  35. \DoxyCodeLine{00041\ \ \ \ \ PRIO\_HIGH\ \ \ \ \ =\ 5,\ \ \ }
  36. \DoxyCodeLine{00042\ \ \ \ \ PRIO\_NORMAL\ \ \ =\ 9,\ \ \ }
  37. \DoxyCodeLine{00043\ \ \ \ \ PRIO\_LOW\ \ \ \ \ \ =\ 13,\ \ }
  38. \DoxyCodeLine{00044\ \};}
  39. \DoxyCodeLine{00045\ }
  40. \DoxyCodeLine{00049\ \textcolor{keyword}{struct\ }TaskHandle\ \{}
  41. \DoxyCodeLine{00050\ \ \ \ \ uint8\_t\ slot\{0xFF\};\ \ }
  42. \DoxyCodeLine{00051\ }
  43. \DoxyCodeLine{00053\ \ \ \ \ \textcolor{keywordtype}{bool}\ is\_valid()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ slot\ !=\ 0xFF;\ \}}
  44. \DoxyCodeLine{00054\ \};}
  45. \DoxyCodeLine{00055\ }
  46. \DoxyCodeLine{00066\ TaskHandle\ create(TaskFn\ fn,\ \textcolor{keywordtype}{void}*\ arg\ =\ \textcolor{keyword}{nullptr},\ uint32\_t\ prio\ =\ PRIO\_NORMAL);}
  47. \DoxyCodeLine{00067\ }
  48. \DoxyCodeLine{00074\ \textcolor{keywordtype}{void}\ start();}
  49. \DoxyCodeLine{00075\ }
  50. \DoxyCodeLine{00081\ \textcolor{keywordtype}{void}\ yield();}
  51. \DoxyCodeLine{00082\ }
  52. \DoxyCodeLine{00091\ \textcolor{keywordtype}{void}\ sleep\_ms(uint32\_t\ ms);}
  53. \DoxyCodeLine{00092\ }
  54. \DoxyCodeLine{00099\ \textcolor{keywordtype}{void}\ exit(\textcolor{keywordtype}{int}\ code\ =\ 0);}
  55. \DoxyCodeLine{00100\ }
  56. \DoxyCodeLine{00106\ uint64\_t\ now\_ms();}
  57. \DoxyCodeLine{00107\ }
  58. \DoxyCodeLine{00115\ \textcolor{keywordtype}{bool}\ lock();}
  59. \DoxyCodeLine{00116\ }
  60. \DoxyCodeLine{00122\ \textcolor{keywordtype}{void}\ unlock(\textcolor{keywordtype}{bool}\ was\_enabled);}
  61. \DoxyCodeLine{00123\ }
  62. \DoxyCodeLine{00132\ \textcolor{keywordtype}{void}\ preempt\_disable();}
  63. \DoxyCodeLine{00133\ }
  64. \DoxyCodeLine{00141\ \textcolor{keywordtype}{void}\ preempt\_enable();}
  65. \DoxyCodeLine{00142\ }
  66. \DoxyCodeLine{00156\ \textcolor{keyword}{class\ }PreemptGuard\ \{}
  67. \DoxyCodeLine{00157\ \textcolor{keyword}{public}:}
  68. \DoxyCodeLine{00158\ \ \ \ \ PreemptGuard()\ \{\ preempt\_disable();\ \}}
  69. \DoxyCodeLine{00159\ \ \ \ \ \string~PreemptGuard()\ \{\ preempt\_enable();\ \}}
  70. \DoxyCodeLine{00160\ }
  71. \DoxyCodeLine{00161\ \ \ \ \ PreemptGuard(\textcolor{keyword}{const}\ PreemptGuard\&)\ =\ \textcolor{keyword}{delete};}
  72. \DoxyCodeLine{00162\ \ \ \ \ PreemptGuard\&\ operator=(\textcolor{keyword}{const}\ PreemptGuard\&)\ =\ \textcolor{keyword}{delete};}
  73. \DoxyCodeLine{00163\ \};}
  74. \DoxyCodeLine{00164\ }
  75. \DoxyCodeLine{00165\ \textcolor{keyword}{using\ }task\_mask\_t\ =\ uint32\_t;}
  76. \DoxyCodeLine{00166\ }
  77. \DoxyCodeLine{00167\ \textcolor{keyword}{namespace\ }detail\ \{}
  78. \DoxyCodeLine{00168\ \textcolor{keywordtype}{void}\ static\_register(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}*\ name,\ TaskFn\ fn,\ uint32\_t\ prio);}
  79. \DoxyCodeLine{00169\ uint32\_t\ sched\_lock();}
  80. \DoxyCodeLine{00170\ \textcolor{keywordtype}{void}\ sched\_unlock(uint32\_t\ save);}
  81. \DoxyCodeLine{00171\ \textcolor{keywordtype}{void}\ block\_current\_task\_locked();}
  82. \DoxyCodeLine{00172\ \textcolor{keywordtype}{void}\ wake\_task\_locked(TaskHandle\ handle);}
  83. \DoxyCodeLine{00173\ uint8\_t\ current\_task\_slot();}
  84. \DoxyCodeLine{00174\ uint8\_t\ find\_highest\_prio\_waiter(task\_mask\_t\ wait\_mask);}
  85. \DoxyCodeLine{00175\ \}\ \ \textcolor{comment}{//\ namespace\ detail}}
  86. \DoxyCodeLine{00176\ }
  87. \DoxyCodeLine{00177\ \}\ \ \textcolor{comment}{//\ namespace\ tk}}
  88. \DoxyCodeLine{00178\ }
  89. \DoxyCodeLine{00187\ \textcolor{preprocessor}{\#define\ TK\_TASK(fn,\ prio)\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}}
  90. \DoxyCodeLine{00188\ \textcolor{preprocessor}{\ \ \ \ static\ void\ tk\_register\_\#\#fn(void)\ \{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}}
  91. \DoxyCodeLine{00189\ \textcolor{preprocessor}{\ \ \ \ \ \ \ \ ::tk::detail::static\_register(\#fn,\ fn,\ (prio));\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}}
  92. \DoxyCodeLine{00190\ \textcolor{preprocessor}{\ \ \ \ \}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}}
  93. \DoxyCodeLine{00191\ \textcolor{preprocessor}{\ \ \ \ PICO\_RUNTIME\_INIT\_FUNC(tk\_register\_\#\#fn,\ "{}ZZ900"{});}}
  94. \end{DoxyCode}