\doxysection{kernel.\+hpp} \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}} \textbf{ [詳解]} \begin{DoxyCode}{0} \DoxyCodeLine{00001\ \textcolor{comment}{//\ tinykernel:\ minimal\ preemptive\ task\ scheduler\ for\ RP2350\ (Pico\ 2,\ Cortex-\/M33)}} \DoxyCodeLine{00002\ \textcolor{preprocessor}{\#pragma\ once}} \DoxyCodeLine{00003\ } \DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ }} \DoxyCodeLine{00005\ \textcolor{preprocessor}{\#include\ }} \DoxyCodeLine{00006\ } \DoxyCodeLine{00007\ \textcolor{preprocessor}{\#include\ "{}pico/runtime.h"{}}\ \ \textcolor{comment}{//\ PICO\_RUNTIME\_INIT\_FUNC\ (static\ task\ registration)}} \DoxyCodeLine{00008\ } \DoxyCodeLine{00010\ \textcolor{preprocessor}{\#ifndef\ TK\_TICKS\_PER\_SEC}} \DoxyCodeLine{00011\ \textcolor{preprocessor}{\#define\ TK\_TICKS\_PER\_SEC\ 1000u}} \DoxyCodeLine{00012\ \textcolor{preprocessor}{\#endif}} \DoxyCodeLine{00013\ } \DoxyCodeLine{00015\ \textcolor{preprocessor}{\#ifndef\ TK\_TIME\_SLICE\_MS}} \DoxyCodeLine{00016\ \textcolor{preprocessor}{\#define\ TK\_TIME\_SLICE\_MS\ 10u}} \DoxyCodeLine{00017\ \textcolor{preprocessor}{\#endif}} \DoxyCodeLine{00018\ } \DoxyCodeLine{00020\ \textcolor{preprocessor}{\#ifndef\ TK\_MAX\_TASKS}} \DoxyCodeLine{00021\ \textcolor{preprocessor}{\#define\ TK\_MAX\_TASKS\ 32u}} \DoxyCodeLine{00022\ \textcolor{preprocessor}{\#endif}} \DoxyCodeLine{00023\ } \DoxyCodeLine{00025\ \textcolor{preprocessor}{\#ifndef\ TK\_DEFAULT\_STACK\_SIZE}} \DoxyCodeLine{00026\ \textcolor{preprocessor}{\#define\ TK\_DEFAULT\_STACK\_SIZE\ (4\ *\ 1024)}} \DoxyCodeLine{00027\ \textcolor{preprocessor}{\#endif}} \DoxyCodeLine{00028\ } \DoxyCodeLine{00029\ \textcolor{keyword}{namespace\ }tk\ \{} \DoxyCodeLine{00030\ } \DoxyCodeLine{00032\ \textcolor{keyword}{using\ }TaskFn\ =\ void\ (*)(\textcolor{keywordtype}{void}*\ arg);} \DoxyCodeLine{00033\ } \DoxyCodeLine{00039\ \textcolor{keyword}{enum}\ Prio\ :\ uint32\_t\ \{} \DoxyCodeLine{00040\ \ \ \ \ PRIO\_REALTIME\ =\ 1,\ \ \ } \DoxyCodeLine{00041\ \ \ \ \ PRIO\_HIGH\ \ \ \ \ =\ 5,\ \ \ } \DoxyCodeLine{00042\ \ \ \ \ PRIO\_NORMAL\ \ \ =\ 9,\ \ \ } \DoxyCodeLine{00043\ \ \ \ \ PRIO\_LOW\ \ \ \ \ \ =\ 13,\ \ } \DoxyCodeLine{00044\ \};} \DoxyCodeLine{00045\ } \DoxyCodeLine{00049\ \textcolor{keyword}{struct\ }TaskHandle\ \{} \DoxyCodeLine{00050\ \ \ \ \ uint8\_t\ slot\{0xFF\};\ \ } \DoxyCodeLine{00051\ } \DoxyCodeLine{00053\ \ \ \ \ \textcolor{keywordtype}{bool}\ is\_valid()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ slot\ !=\ 0xFF;\ \}} \DoxyCodeLine{00054\ \};} \DoxyCodeLine{00055\ } \DoxyCodeLine{00066\ TaskHandle\ create(TaskFn\ fn,\ \textcolor{keywordtype}{void}*\ arg\ =\ \textcolor{keyword}{nullptr},\ uint32\_t\ prio\ =\ PRIO\_NORMAL);} \DoxyCodeLine{00067\ } \DoxyCodeLine{00074\ \textcolor{keywordtype}{void}\ start();} \DoxyCodeLine{00075\ } \DoxyCodeLine{00081\ \textcolor{keywordtype}{void}\ yield();} \DoxyCodeLine{00082\ } \DoxyCodeLine{00091\ \textcolor{keywordtype}{void}\ sleep\_ms(uint32\_t\ ms);} \DoxyCodeLine{00092\ } \DoxyCodeLine{00099\ \textcolor{keywordtype}{void}\ exit(\textcolor{keywordtype}{int}\ code\ =\ 0);} \DoxyCodeLine{00100\ } \DoxyCodeLine{00106\ uint64\_t\ now\_ms();} \DoxyCodeLine{00107\ } \DoxyCodeLine{00115\ \textcolor{keywordtype}{bool}\ lock();} \DoxyCodeLine{00116\ } \DoxyCodeLine{00122\ \textcolor{keywordtype}{void}\ unlock(\textcolor{keywordtype}{bool}\ was\_enabled);} \DoxyCodeLine{00123\ } \DoxyCodeLine{00132\ \textcolor{keywordtype}{void}\ preempt\_disable();} \DoxyCodeLine{00133\ } \DoxyCodeLine{00141\ \textcolor{keywordtype}{void}\ preempt\_enable();} \DoxyCodeLine{00142\ } \DoxyCodeLine{00156\ \textcolor{keyword}{class\ }PreemptGuard\ \{} \DoxyCodeLine{00157\ \textcolor{keyword}{public}:} \DoxyCodeLine{00158\ \ \ \ \ PreemptGuard()\ \{\ preempt\_disable();\ \}} \DoxyCodeLine{00159\ \ \ \ \ \string~PreemptGuard()\ \{\ preempt\_enable();\ \}} \DoxyCodeLine{00160\ } \DoxyCodeLine{00161\ \ \ \ \ PreemptGuard(\textcolor{keyword}{const}\ PreemptGuard\&)\ =\ \textcolor{keyword}{delete};} \DoxyCodeLine{00162\ \ \ \ \ PreemptGuard\&\ operator=(\textcolor{keyword}{const}\ PreemptGuard\&)\ =\ \textcolor{keyword}{delete};} \DoxyCodeLine{00163\ \};} \DoxyCodeLine{00164\ } \DoxyCodeLine{00165\ \textcolor{keyword}{using\ }task\_mask\_t\ =\ uint32\_t;} \DoxyCodeLine{00166\ } \DoxyCodeLine{00167\ \textcolor{keyword}{namespace\ }detail\ \{} \DoxyCodeLine{00168\ \textcolor{keywordtype}{void}\ static\_register(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}*\ name,\ TaskFn\ fn,\ uint32\_t\ prio);} \DoxyCodeLine{00169\ uint32\_t\ sched\_lock();} \DoxyCodeLine{00170\ \textcolor{keywordtype}{void}\ sched\_unlock(uint32\_t\ save);} \DoxyCodeLine{00171\ \textcolor{keywordtype}{void}\ block\_current\_task\_locked();} \DoxyCodeLine{00172\ \textcolor{keywordtype}{void}\ wake\_task\_locked(TaskHandle\ handle);} \DoxyCodeLine{00173\ uint8\_t\ current\_task\_slot();} \DoxyCodeLine{00174\ uint8\_t\ find\_highest\_prio\_waiter(task\_mask\_t\ wait\_mask);} \DoxyCodeLine{00175\ \}\ \ \textcolor{comment}{//\ namespace\ detail}} \DoxyCodeLine{00176\ } \DoxyCodeLine{00177\ \}\ \ \textcolor{comment}{//\ namespace\ tk}} \DoxyCodeLine{00178\ } \DoxyCodeLine{00187\ \textcolor{preprocessor}{\#define\ TK\_TASK(fn,\ prio)\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}} \DoxyCodeLine{00188\ \textcolor{preprocessor}{\ \ \ \ static\ void\ tk\_register\_\#\#fn(void)\ \{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}} \DoxyCodeLine{00189\ \textcolor{preprocessor}{\ \ \ \ \ \ \ \ ::tk::detail::static\_register(\#fn,\ fn,\ (prio));\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}} \DoxyCodeLine{00190\ \textcolor{preprocessor}{\ \ \ \ \}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(\backslash\)}} \DoxyCodeLine{00191\ \textcolor{preprocessor}{\ \ \ \ PICO\_RUNTIME\_INIT\_FUNC(tk\_register\_\#\#fn,\ "{}ZZ900"{});}} \end{DoxyCode}