main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <stdio.h>
  2. #include "hardware/gpio.h"
  3. #include "pico/stdlib.h"
  4. #include "tinykernel/kernel.hpp"
  5. // Atomic printf helper: stdio is shared, so wrap each call with the kernel lock.
  6. #define P(...) \
  7. do { \
  8. bool _lk = tk::lock(); \
  9. printf(__VA_ARGS__); \
  10. tk::unlock(_lk); \
  11. } while (0)
  12. // ---------------------------------------------------------------------------
  13. // Static task (registered at link time via TK_TASK): toggles the onboard LED.
  14. void task_led(void* arg) {
  15. (void)arg;
  16. for (;;) {
  17. gpio_put(25, !gpio_get(25));
  18. tk::sleep_ms(200);
  19. }
  20. }
  21. TK_TASK(task_led, tk::PRIO_NORMAL)
  22. // ---------------------------------------------------------------------------
  23. // Static task: FPU self-check. Runs the same deterministic float/double sequence
  24. // twice; any corruption of FPU state across context switches shows up as a
  25. // mismatch or a drifting result.
  26. static float fpu_run_f(float v) {
  27. for (int i = 0; i < 10000; ++i) v = v * 1.000001f + 0.5f;
  28. return v;
  29. }
  30. static double fpu_run_d(double v) {
  31. for (long i = 0; i < 10000; ++i) v = v * 1.0000000002 + 0.5;
  32. return v;
  33. }
  34. void task_fpu(void* arg) {
  35. (void)arg;
  36. for (;;) {
  37. float fa = fpu_run_f(1.5f);
  38. float fb = fpu_run_f(1.5f);
  39. double da = fpu_run_d(2.5);
  40. double db = fpu_run_d(2.5);
  41. if (fa != fb || da != db) {
  42. P("FPU FAIL f: %8.6f vs %8.6f d: %.9f vs %.9f\n", (double)fa, (double)fb, da, db);
  43. } else {
  44. P("FPU ok f: %8.6f d: %.9f\n", (double)fa, da);
  45. }
  46. tk::sleep_ms(300);
  47. }
  48. }
  49. TK_TASK(task_fpu, tk::PRIO_NORMAL)
  50. // ---------------------------------------------------------------------------
  51. // Dynamic task (higher priority): busy-spins for a few ms to demonstrate that it
  52. // preempts the lower-priority tasks immediately.
  53. static void task_hi(void* arg) {
  54. (void)arg;
  55. for (;;) {
  56. volatile uint32_t n = 0;
  57. while (n < 500000u) ++n;
  58. P("HI prio=%u busy-spin done, now=%llu ms\n", (unsigned)tk::PRIO_HIGH, (unsigned long long)tk::now_ms());
  59. tk::sleep_ms(700);
  60. }
  61. }
  62. // ---------------------------------------------------------------------------
  63. // Dynamic task: prints a few times and then exits.
  64. static void task_dying(void* arg) {
  65. (void)arg;
  66. for (int i = 1; i <= 3; ++i) {
  67. P("DYING %d (now=%llu ms)\n", i, (unsigned long long)tk::now_ms());
  68. tk::sleep_ms(250);
  69. }
  70. P("DYING calling tk::exit()\n");
  71. tk::exit(0);
  72. }
  73. // ---------------------------------------------------------------------------
  74. // Dynamic task: plain periodic printer at normal priority.
  75. static void task_print(void* arg) {
  76. (void)arg;
  77. uint32_t count = 0;
  78. for (;;) {
  79. P("A #%lu now=%llu ms\n", (unsigned long)count++, (unsigned long long)tk::now_ms());
  80. tk::sleep_ms(150);
  81. }
  82. }
  83. int main() {
  84. stdio_init_all();
  85. gpio_init(25);
  86. gpio_set_dir(25, GPIO_OUT);
  87. printf("tinykernel: registering dynamic tasks...\n");
  88. auto h_hi = tk::create(task_hi, nullptr, tk::PRIO_HIGH);
  89. auto h_dy = tk::create(task_dying, nullptr, tk::PRIO_LOW);
  90. auto h_pr = tk::create(task_print, nullptr, tk::PRIO_NORMAL);
  91. if (h_hi.slot == 0xFF || h_dy.slot == 0xFF || h_pr.slot == 0xFF) {
  92. printf("tinykernel: task creation FAILED\n");
  93. for (;;) {
  94. }
  95. }
  96. printf("tinykernel: starting scheduler (preemptive, core M33 only)\n");
  97. tk::start(); // never returns
  98. }