NetBurner 3.1
nbrtos.h
Go to the documentation of this file.
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
16 #ifndef _NBRTOS_H
17 #define _NBRTOS_H
18 
19 // NB Definitions
20 #include <predef.h>
21 
22 // NB Constants
23 #include <constants.h>
24 
25 // NB Libs
26 #include <basictypes.h>
27 #include <nbrtoscpu.h>
28 #include <predef.h>
29 
30 /***********************************************************
31  * NBRTOS.H
32  * SYSTEM DECLARATIONS
33  ***********************************************************
34  */
35 #define OS_LO_PRIO 63 /*IDLE task priority */
36 
42 #define OS_STAT_RDY 0x00
43 #define OS_STAT_MBOX 0x01
44 #define OS_STAT_SEM 0x02
45 #define OS_STAT_Q 0x04
46 #define OS_STAT_FIFO 0x08
47 #define OS_STAT_CRIT 0x10
48 #define OS_STAT_DELAY 0x20
49 #define OS_STAT_RES4 0x40
50 #define OS_STAT_RES5 0x80
51 
58 #define OS_NO_ERR 0
59 #define OS_TIMEOUT 10
60 #define OS_MBOX_FULL 20
61 #define OS_Q_FULL 30
62 #define OS_Q_EXISTS 31
63 #define OS_PRIO_EXIST 40
64 #define OS_PRIO_INVALID 41
65 #define OS_SEM_ERR 50
66 #define OS_SEM_OVF 51
67 #define OS_CRIT_ERR 60
68 #define OS_NO_MORE_TCB 70 // No TCBs free to create task
69 
71 #define WAIT_FOREVER 0
73 typedef volatile uint32_t tick_t;
74 
76 // GLOBAL VARIABLES
77 //
78 extern vuint32_t Secs; // Number of seconds since system start
79 extern volatile tick_t TimeTick; // Number of time ticks since system start
80 
81 class TickTimeout;
82 struct RawTickTimeout_t
83 {
84  tick_t expiration;
85 
86  inline bool expired() const { return expiration ? (((int)(expiration - TimeTick)) <= 0) : false; }
87  inline bool expired() volatile { return expiration ? (((int)(expiration - TimeTick)) <= 0) : false; }
88  inline operator bool() const { return !expired(); }
89  const RawTickTimeout_t &operator=(const TickTimeout &rhs);
90  volatile RawTickTimeout_t &operator=(const TickTimeout &rhs) volatile;
91 
92  inline tick_t operator-(const tick_t &tick) { return expiration - tick; }
93  inline tick_t operator-(const tick_t &tick) const { return expiration - tick; }
94  inline tick_t operator-(const tick_t &tick) volatile { return expiration - tick; }
95 };
96 
97 inline bool operator==(const RawTickTimeout_t &lhs, const int &rhs)
98 {
99  return lhs.expiration == (tick_t)rhs;
100 }
101 inline bool operator==(const volatile RawTickTimeout_t &lhs, const int &rhs)
102 {
103  return lhs.expiration == (tick_t)rhs;
104 }
105 
113 {
114  RawTickTimeout_t raw;
115 
116  void set(uint32_t timeout)
117  {
118  if (!timeout) { raw.expiration = 0; }
119  else
120  {
121  raw.expiration = TimeTick + (timeout & 0x7FFFFFFF);
122  // A 1 tick delay extension is introduced on TimeTick overflow
123  // in order to allow for infinite delays to be indicated with an
124  // expiration of zero
125  if (timeout && !raw.expiration) { raw.expiration = 1; }
126  }
127  }
128 
129  public:
130  class uint32_nonboolean_t
131  {
132  uint32_t value;
133 
134  public:
135  uint32_nonboolean_t(uint32_t val) : value(val) {}
136  inline explicit operator bool() { return (bool)value; }
137  inline operator uint32_t() { return value; }
138  };
139  explicit TickTimeout() { set(0); }
145  explicit TickTimeout(uint32_t timeout) { set(timeout); }
146 
153  inline uint32_t val() const
154  {
155  if (!raw.expiration) { return raw.expiration; }
156  int ret = raw.expiration - TimeTick;
157  // Prevent passing an infinite or otherwise bogus timeout in a tick race
158  return (ret > 0) ? ret : 1;
159  }
160 
167  inline bool expired() const { return raw.expired(); }
168 
179  inline operator bool() const { return !expired(); }
180 
181  inline operator uint32_t() const { return val(); }
182  inline operator uint16_t() const
183  {
184  uint32_t ret = val();
185  return ret > 0xFFFF ? 0xFFFE : ret;
186  }
187 
188  inline TickTimeout &operator=(const TickTimeout &rhs)
189  {
190  raw.expiration = rhs.raw.expiration;
191  return *this;
192  }
193  inline TickTimeout &operator=(uint32_t val)
194  {
195  set(val);
196  return *this;
197  }
198 
199  friend void OSTimeWaitUntil(uint32_t systemTickValue);
200  friend class RawTickTimeout_t;
201 };
202 
203 inline const RawTickTimeout_t &RawTickTimeout_t::operator=(const TickTimeout &rhs)
204 {
205  expiration = rhs.raw.expiration;
206  return *this;
207 }
208 inline volatile RawTickTimeout_t &RawTickTimeout_t::operator=(const TickTimeout &rhs) volatile
209 {
210  expiration = rhs.raw.expiration;
211  return *this;
212 }
213 
214 // The following class holds a list of tasks...
215 
216 class task_bit_list
217 {
218  public:
219  volatile uint32_t OSTbl[TASK_TABLE_SIZE];
220 
221  // task_bit_list();
222  void Init() volatile;
223  void Copy(volatile task_bit_list &rhs)
224  {
225  for (int i = 0; i < TASK_TABLE_SIZE; i++)
226  {
227  OSTbl[i] = rhs.OSTbl[i];
228  }
229  }
230 
231  // The following functions are all guaranteed to be atomic
232  void set(int set_num) volatile;
233  void clr(int clr_num) volatile;
234 
235  // The following functions return 0 if no bits are set.
236  uint32_t gethigh() volatile;
237  uint32_t get_high_and_clear() volatile;
238 
239  inline bool isSet(int num) volatile const { return (OSTbl[num / 32] & (0x80000000 >> (num % 32))); }
240 };
241 
242 class OS_TCB;
243 
244 // The common element of all the task objects...
245 class OS_TASK_DLY_OBJ
246 {
247  task_bit_list tasks_waiting;
248 
249  public:
250  OS_TASK_DLY_OBJ();
251  void Init();
252 
253  // Returns true if woken up, returns false if timed out
254  bool Wait_when(uint8_t StatReason, TickTimeout &timeout);
255 
256  inline bool Wait(uint8_t StatReason, uint32_t to_count)
257  {
258  TickTimeout to_when(to_count);
259  return Wait_when(StatReason, to_when);
260  }
261 
262  // This releases the highest priority task waiting on this object.
263  void MakeHighTaskWaitingReady(uint8_t StatReason);
264  friend class OS_TCB;
265 } __attribute__((packed));
266 
267 // class OS_TCB;//forward
268 
269 class OS_TCB : public cpu_tcb
270 {
271  public:
272  uint8_t OSTCBStat; // Holds the current status of the TCB
273  uint8_t OSTCBResult; // Basically holds the timeout or not flag when woken.
274  uint8_t OSTCBPrio; // Index to prio table.... not sure if we need to keep this.
275  uint8_t pad; // Make 32 bit aligned.
276 
277  RawTickTimeout_t OSTCBDly_when; // When to release timeout the task, 0= never.
278 
279  const char *pOSTCBName;
280  OS_TCB *OSTCBNext;
281  OS_TCB *OSTCBPrev;
282 
283 #ifdef NBRTOS_PRIO_PROMOTION
284  // These pointers are for handling Priority Promotion when OS_CRITs create
285  // an inversion situation
286  volatile OS_TCB *pPromotedTo;
287  volatile OS_TCB *pDisplacedBy;
288  OS_TASK_DLY_OBJ *pWaiting;
289  uint32_t displacedByOrigPrio;
290 
291  void PromoteToCurPrio() volatile;
292  void Demote() volatile;
293 #endif
294 
295  static volatile OS_TCB *GetCur();
296 
297 #ifdef NBRTOS_TIME
298  unsigned long switchTimeTick;
299  unsigned long switchTimeFraction;
300  unsigned long runningTime;
301  unsigned long runningTimeFraction;
302 #endif
303  // OS_TCB(); //Zero everything
304  void Init();
305 
306  // Set up TCB assuming that the STACK has already been setup and properly initalized.
307  bool Init(uint8_t prio, void *pActualTop, void *pstk, long *pbot, const char *name);
308 };
309 
310 /* Forward Declaration */
311 struct OS_CRIT;
312 
318 struct OS_SEM : public OS_TASK_DLY_OBJ
319 {
320  volatile uint32_t OSSemCnt;
321  volatile uint32_t OSSemUsed;
322 
323  private:
324  bool Claim();
325 
326  public:
332  inline OS_SEM(int32_t cnt = 0) : OS_TASK_DLY_OBJ() { Init(cnt); }
333 
345  uint8_t Init(int32_t cnt = 0);
346 
356  uint8_t Post();
357 
371  uint8_t Pend(uint32_t timeoutTicks = WAIT_FOREVER);
372 
384  uint8_t PendNoWait();
385 } __attribute__((packed));
386 
391 struct OS_MBOX : public OS_TASK_DLY_OBJ
392 {
393  void *OSMboxMsg;
394  uint32_t OSMboxDataAvail;
395 
396  bool Claim(void *&Result);
397 
398  public:
402  inline OS_MBOX() : OS_TASK_DLY_OBJ() {}
403 
409  inline OS_MBOX(void *msg) : OS_TASK_DLY_OBJ() { Init(msg); }
410 
420  uint8_t Init(void *msg = NULL);
421 
432  uint8_t Post(void *msg);
433 
450  void *Pend(uint32_t timeoutTicks, uint8_t &result);
451 
466  void *PendNoWait(uint8_t &result);
467 
482  inline void *Pend(uint32_t timeoutTicks = WAIT_FOREVER)
483  {
484  uint8_t unused;
485  return Pend(timeoutTicks, unused);
486  };
487 
500  inline void *PendNoWait()
501  {
502  uint8_t unused;
503  return PendNoWait(unused);
504  };
505 };
506 
507 template<typename T>
508 class TEMPL_MBOX
509 {
510  protected:
511  OS_MBOX m_mbox;
512 
513  public:
514  TEMPL_MBOX(const T *msg) : m_mbox((void *)msg) {}
515  inline uint8_t Init(const T *msg) { return m_mbox.Init((void *)msg); }
516  inline uint8_t Post(const T *msg) { return m_mbox.Post((void *)msg); }
517 
518  inline T *Pend(uint32_t timeoutTicks, uint8_t &result) { return static_cast<T *>(m_mbox.Pend(timeoutTicks, result)); };
519 
520  inline T *PendNoWait(uint8_t &result) { return static_cast<T *>(m_mbox.PendNoWait(result)); };
521 
522  inline T *Pend(uint32_t timeoutTicks = WAIT_FOREVER) { return static_cast<T *>(m_mbox.Pend(timeoutTicks)); };
523 
524  inline T *PendNoWait() { return static_cast<T *>(m_mbox.PendNoWait()); };
525 };
526 
531 struct OS_Q : public OS_TASK_DLY_OBJ
532 {
533  void **OSQStart;
534  void **OSQEnd;
535  void **OSQIn;
536  void **OSQOut;
537  uint8_t OSQSize;
538  uint8_t OSQEntries;
539 
540  bool Claim(void *&Result);
541 
542  public:
546  inline OS_Q() : OS_TASK_DLY_OBJ() {}
547 
554  inline OS_Q(void **pQueueStorage, uint8_t size) : OS_TASK_DLY_OBJ() { Init(pQueueStorage, size); }
555 
566  uint8_t Init(void **pQueueStorage, uint8_t size);
567 
581  uint8_t Post(void *pItem);
582 
596  uint8_t PostFirst(void *pItem);
597 
612  uint8_t PostUnique(void *pItem);
613 
629  uint8_t PostUniqueFirst(void *msg);
630 
645  void *Pend(uint32_t timeoutTicks, uint8_t &result);
646 
660  void *PendNoWait(uint8_t &result);
661 
675  inline void *Pend(uint32_t timeoutTicks = WAIT_FOREVER)
676  {
677  uint8_t unused;
678  return Pend(timeoutTicks, unused);
679  };
680 
691  inline void *PendNoWait()
692  {
693  uint8_t unused;
694  return PendNoWait(unused);
695  };
696 };
697 
698 template<typename T>
699 struct TEMPL_Q
700 {
701  protected:
702  OS_Q m_q;
703 
704  public:
705  TEMPL_Q() {}
706  TEMPL_Q(T **pQueueStorage, uint8_t size) { m_q.Init((void **)pQueueStorage, size); }
707  uint8_t Init(T **pQueueStorage, uint8_t size) { return m_q.Init((void **)pQueueStorage, size); }
708  uint8_t Post(T *item) { return m_q.Post((void *)item); }
709  uint8_t PostFirst(T *item) { return m_q.PostFirst((void *)item); };
710  uint8_t PostUnique(T *item) { return m_q.PostUnique((void *)item); };
711  uint8_t PostUniqueFirst(T *item) { return m_q.PostUniqueFirst((void *)item); };
712 
713  inline T *Pend(uint32_t timeoutTicks, uint8_t &result) { return static_cast<T *>(m_q.Pend(timeoutTicks, result)); };
714 
715  inline T *Pend(uint32_t timeoutTicks = WAIT_FOREVER) { return static_cast<T *>(m_q.Pend(timeoutTicks)); };
716 
717  inline T *PendNoWait() { return static_cast<T *>(m_q.PendNoWait()); };
718 
719  inline T *PendNoWait(uint8_t &result) { return static_cast<T *>(m_q.PendNoWait(result)); };
720 };
721 
722 typedef struct os_fifo_el
723 {
724  union {
725  struct os_fifo_el *pNextFifo_El;
726  puint8_t pAsBytePtr;
727  };
728 } OS_FIFO_EL;
729 
736 struct OS_FIFO : public OS_TASK_DLY_OBJ
737 {
738  OS_FIFO_EL *pHead;
739  OS_FIFO_EL *pTail;
740 
741  bool Claim(volatile OS_FIFO_EL *&pToRet);
742 
743  public:
747  inline OS_FIFO() : OS_TASK_DLY_OBJ() { Init(); }
748 
756  uint8_t Init();
757 
769  uint8_t Post(OS_FIFO_EL *pToPost);
770 
786  uint8_t PostFirst(OS_FIFO_EL *pToPost);
787 
802  OS_FIFO_EL *Pend(uint32_t timeoutTicks, uint8_t &result);
803 
817  OS_FIFO_EL *PendNoWait(uint8_t &result);
818 
831  inline OS_FIFO_EL *Pend(uint32_t timeoutTicks = WAIT_FOREVER)
832  {
833  uint8_t unused;
834  return Pend(timeoutTicks, unused);
835  };
836 
847  inline OS_FIFO_EL *PendNoWait()
848  {
849  uint8_t unused;
850  return PendNoWait(unused);
851  };
852 };
853 
854 template<typename T>
855 struct TEMPL_FIFO
856 {
857  protected:
858  OS_FIFO m_fifo;
859 
860  public:
861  TEMPL_FIFO() { m_fifo.Init(); }
862  uint8_t Init() { return m_fifo.Init(); }
863  uint8_t Post(T *item) { return m_fifo.Post((OS_FIFO_EL *)item); }
864  uint8_t PostFirst(T *item) { return m_fifo.PostFirst((OS_FIFO_EL *)item); };
865 
866  inline T *Pend(uint32_t timeoutTicks, uint8_t &result) { return static_cast<T *>(m_fifo.Pend(timeoutTicks, result)); };
867 
868  inline T *Pend(uint32_t timeoutTicks = WAIT_FOREVER) { return static_cast<T *>(m_fifo.Pend(timeoutTicks)); };
869 
870  inline T *PendNoWait() { return static_cast<T *>(m_fifo.PendNoWait()); };
871 
872  inline T *PendNoWait(uint8_t &result) { return static_cast<T *>(m_fifo.PendNoWait(result)); };
873 };
874 
875 /*
876  ***********************************************************
877  * Critical Section DATA STRUCTURES
878  *
879  * Added by PTB 5/09/99
880  * Modified by DEC 3/21/16
881  ***********************************************************
882  */
883 
884 class BufferCriticalLock;
885 class fifo_buffer_storage;
886 
893 struct OS_CRIT : public OS_TASK_DLY_OBJ
894 {
895  OS_TCB *OSCritOwnerTCB;
896  uint32_t OSCritDepthCount;
897  bool Claim();
898 
899  public:
903  OS_CRIT() { Init(); }
904 
912  uint8_t Init();
913 
931  uint8_t LockAndEnter(uint32_t timeoutTicks = WAIT_FOREVER);
932 
949  uint8_t Enter(uint32_t timeoutTicks = WAIT_FOREVER);
950 
965  uint8_t EnterNoWait();
966 
980  uint8_t Leave();
981 
993  uint8_t LeaveAndUnlock();
994  friend class BufferCriticalLock;
995  friend class fifo_buffer_storage;
996 
1000  friend void ForceReboot(bool fromIRQ);
1001 } __attribute__((packed));
1002 
1003 struct OS_FLAGS_WAIT;
1004 
1009 struct OS_FLAGS
1010 {
1011  protected:
1012  vuint32_t m_current_flags;
1013  void *m_pWaitinglist;
1014  void TestFlags();
1015  void AddOFW(OS_FLAGS_WAIT *ofw);
1016  void RemoveOFW(OS_FLAGS_WAIT *ofw);
1017 
1018  public:
1022  OS_FLAGS();
1023 
1027  void Init();
1028 
1037  void Set(uint32_t bits_to_set);
1038 
1047  void Clear(uint32_t bits_to_clr);
1048 
1062  uint8_t PendAny(uint32_t bit_mask, uint16_t timeout);
1063 
1076  uint8_t PendAnyNoWait(uint32_t bit_mask);
1077 
1091  uint8_t PendAll(uint32_t bit_mask, uint16_t timeout);
1092 
1105  uint8_t PendAllNoWait(uint32_t bit_mask);
1106 
1116  uint32_t State();
1117 };
1118 
1119 /* Create and initialize an OS flags object
1120 This function must be called before you use an OS_FLAGS object.
1121 */
1122 [[deprecated]] inline void OSFlagCreate(OS_FLAGS *pf)
1123 {
1124  pf->Init();
1125 };
1126 
1137 [[deprecated]] inline void OSFlagSet(OS_FLAGS *flags, uint32_t bits_to_set)
1138 {
1139  flags->Set(bits_to_set);
1140 };
1141 
1152 [[deprecated]] inline void OSFlagClear(OS_FLAGS *flags, uint32_t bits_to_clr)
1153 {
1154  flags->Clear(bits_to_clr);
1155 };
1156 
1171 [[deprecated]] inline uint8_t OSFlagPendAny(OS_FLAGS *flags, uint32_t bit_mask, uint16_t timeout)
1172 {
1173  return flags->PendAny(bit_mask, timeout);
1174 };
1175 
1189 [[deprecated]] inline uint8_t OSFlagPendAnyNoWait(OS_FLAGS *flags, uint32_t bit_mask)
1190 {
1191  return flags->PendAnyNoWait(bit_mask);
1192 };
1193 
1208 [[deprecated]] inline uint8_t OSFlagPendAll(OS_FLAGS *flags, uint32_t bit_mask, uint16_t timeout)
1209 {
1210  return flags->PendAll(bit_mask, timeout);
1211 };
1212 
1226 [[deprecated]] inline uint8_t OSFlagPendAllNoWait(OS_FLAGS *flags, uint32_t bit_mask)
1227 {
1228  return flags->PendAllNoWait(bit_mask);
1229 };
1230 
1242 [[deprecated]] inline uint32_t OSFlagState(OS_FLAGS *flags)
1243 {
1244  return flags->State();
1245 };
1246 
1247 /*
1248  ***********************************************************
1249  * NBRTOS GLOBAL VARIABLES
1250  ***********************************************************
1251  */
1252 // Pointers to each of the OS_TCB structure sorted by priority
1253 extern volatile OS_TCB *OSTCBPrioTbl[OS_MAX_PRIOS];
1254 
1255 // taks bits for what tasks are ready to run
1256 extern volatile task_bit_list OSTaskReadyList;
1257 #ifdef NBRTOS_PRIO_PROMOTION
1258 extern volatile task_bit_list OSInUsePrioList FAST_SYS_VAR;
1259 extern volatile task_bit_list OSActivePrioList FAST_SYS_VAR;
1260 #endif
1261 
1262 extern OS_TCB OSTCBTbl[OS_MAX_TASKS]; // All the possible TCB's
1263 
1264 extern OS_TCB *pOSActiveTCBList; // Linked list of activ TCB's
1265 
1266 // One of the following two variables wsill go away....
1267 extern volatile uint32_t nPrioOfCurTask; // Current task priority number
1268 extern volatile uint32_t nPrioOfHighReady; // highest task ready to run
1269 extern volatile OS_TCB *OSTCBCur;
1270 extern volatile OS_TCB *OSTCBHighRdy;
1271 
1272 extern OS_TCB *pOSTCBFreeList; // List of unused TCB's
1273 
1274 extern volatile uint32_t OSIntNesting;
1275 extern volatile uint32_t OSLockNesting;
1276 extern volatile uint32_t OSISRLevel32;
1277 
1278 extern volatile bool OSRunning;
1279 
1280 // So non-recompilable files can reference the right struct size
1281 extern unsigned long OSTcbStructSize;
1282 
1283 #ifdef NBRTOS_TASK_LOG
1284 extern void (*pTaskLogger)(uint8_t nextPrio);
1285 #endif
1286 /*
1287 Removed
1288  extern volatile BOOLEAN OSShowTasksOnLeds;
1289 */
1290 
1291 /*
1292 ***********************************************************
1293 * NBRTOS FUNCTION PROTOTYPES
1294 ***********************************************************
1295 */
1296 void OSInit(uint8_t maxtasks);
1297 void OSStart(void);
1298 void OSCreateIdleTask();
1299 
1326 uint8_t OSTaskCreatewName(void (*task)(void *dptr), // Function to call
1327  void *data, // Data to pass as a parameter
1328  void *pstktop, // Stack top
1329  void *pstkbot, // Stack bottom
1330  uint8_t prio, // current priority
1331  const char *name // task name
1332 );
1333 
1346 #define OSSimpleTaskCreatewName(x, p, n) \
1347  { \
1348  static uint32_t func_##x_Stk[USER_TASK_STK_SIZE] __attribute__((aligned(4))); \
1349  OSTaskCreatewName(x, NULL, (void *)&func_##x_Stk[USER_TASK_STK_SIZE], (void *)func_##x_Stk, p, n); \
1350  }
1351 #define OSSimpleTaskCreatewNameSRAM(x, p, n) \
1352  { \
1353  static uint32_t func_##x_Stk[USER_TASK_STK_SIZE] __attribute__((aligned(4))) FAST_USER_STK; \
1354  OSTaskCreatewName(x, NULL, (void *)&func_##x_Stk[USER_TASK_STK_SIZE], (void *)func_##x_Stk, p, n); \
1355  }
1356 
1357 void OSTimeWaitUntil(uint32_t to_when);
1358 
1368 inline void OSTimeDly(uint32_t to_count)
1369 {
1370  uint32_t to_when = to_count + TimeTick;
1371  if (!to_when) to_when++;
1372  OSTimeWaitUntil(to_when);
1373 };
1374 
1375 // void OSIntEnter( void );
1376 
1377 extern "C"
1378 {
1379  void OSIntExit(void);
1380  void OSCtxSw(void);
1381  void OSTickISR(void);
1382  void OSStartHighRdy(void);
1383  void OSSetupVBR(void);
1384  void OSSched(void);
1385  void OSTimeTick(void);
1386 
1398  void OSTaskDelete(void);
1399 }
1400 
1401 OS_TCB *OSTCBGetFree(void);
1402 
1422 uint8_t OSChangePrio(uint32_t newp);
1423 
1424 void OSSetName(const char *cp);
1425 
1439 void OSLock(void);
1440 
1449 void OSUnlock(void);
1450 
1466 [[deprecated]] inline uint8_t OSSemInit(OS_SEM *psem, long value)
1467 {
1468  return (psem != nullptr) ? psem->Init(value) : OS_CRIT_ERR;
1469 }
1470 
1485 [[deprecated]] inline uint8_t OSSemPost(OS_SEM *psem)
1486 {
1487  return psem->Post();
1488 }
1489 
1505 [[deprecated]] inline uint8_t OSSemPend(OS_SEM *psem, uint16_t timeout)
1506 {
1507  return psem->Pend(timeout);
1508 }
1509 
1523 [[deprecated]] inline uint8_t OSSemPendNoWait(OS_SEM *psem)
1524 {
1525  return psem->PendNoWait();
1526 }
1527 
1542 [[deprecated]] inline uint8_t OSMboxInit(OS_MBOX *pmbox, void *msg)
1543 {
1544  return (pmbox != nullptr) ? pmbox->Init(msg) : OS_CRIT_ERR;
1545 }
1546 
1561 [[deprecated]] inline uint8_t OSMboxPost(OS_MBOX *pmbox, void *msg)
1562 {
1563  return pmbox->Post(msg);
1564 }
1565 
1578 [[deprecated]] inline void *OSMboxPend(OS_MBOX *pmbox, uint16_t timeout, uint8_t *err)
1579 {
1580  return pmbox->Pend(timeout, *err);
1581 }
1582 
1594 [[deprecated]] inline void *OSMboxPendNoWait(OS_MBOX *pmbox, uint8_t *err)
1595 {
1596  return pmbox->PendNoWait(*err);
1597 }
1598 
1614 [[deprecated]] inline uint8_t OSQInit(OS_Q *pq, void **start, uint8_t size)
1615 {
1616  return (pq != nullptr) ? pq->Init(start, size) : OS_CRIT_ERR;
1617 }
1618 
1633 [[deprecated]] inline uint8_t OSQPost(OS_Q *pq, void *msg)
1634 {
1635  return pq->Post(msg);
1636 }
1637 
1652 [[deprecated]] inline uint8_t OSQPostFirst(OS_Q *pq, void *msg)
1653 {
1654  return pq->PostFirst(msg);
1655 }
1656 
1673 [[deprecated]] inline uint8_t OSQPostUnique(OS_Q *pq, void *msg)
1674 {
1675  return pq->PostUnique(msg);
1676 }
1677 
1694 [[deprecated]] inline uint8_t OSQPostUniqueFirst(OS_Q *pq, void *msg)
1695 {
1696  return pq->PostUniqueFirst(msg);
1697 }
1698 
1711 [[deprecated]] inline void *OSQPend(OS_Q *pq, uint16_t timeout, uint8_t *err)
1712 {
1713  return pq->Pend(timeout, *err);
1714 }
1715 
1727 [[deprecated]] inline void *OSQPendNoWait(OS_Q *pq, uint8_t *err)
1728 {
1729  return pq->PendNoWait(*err);
1730 }
1731 
1745 [[deprecated]] inline uint8_t OSFifoInit(OS_FIFO *pFifo)
1746 {
1747  return (pFifo != nullptr) ? pFifo->Init() : OS_CRIT_ERR;
1748 }
1749 
1763 [[deprecated]] inline uint8_t OSFifoPost(OS_FIFO *pFifo, OS_FIFO_EL *pToPost)
1764 {
1765  return pFifo->Post(pToPost);
1766 }
1780 [[deprecated]] inline uint8_t OSFifoPostFirst(OS_FIFO *pFifo, OS_FIFO_EL *pToPost)
1781 {
1782  return pFifo->PostFirst(pToPost);
1783 };
1784 
1796 [[deprecated]] inline OS_FIFO_EL *OSFifoPend(OS_FIFO *pFifo, uint16_t timeout)
1797 {
1798  return pFifo->Pend(timeout);
1799 };
1800 
1811 [[deprecated]] inline OS_FIFO_EL *OSFifoPendNoWait(OS_FIFO *pFifo)
1812 {
1813  return pFifo->PendNoWait();
1814  ;
1815 }
1816 
1829 [[deprecated]] inline uint8_t OSCritInit(OS_CRIT *pCrit)
1830 {
1831  return pCrit->Init();
1832 }
1833 [[deprecated]] inline uint8_t OSCritLockAndEnter(OS_CRIT *pCrit, uint16_t timeout)
1834 {
1835  return pCrit->LockAndEnter(timeout);
1836 }
1837 
1852 [[deprecated]] inline uint8_t OSCritEnter(OS_CRIT *pCrit, uint16_t timeout)
1853 {
1854  return pCrit->Enter(timeout);
1855 }
1856 
1870 [[deprecated]] inline uint8_t OSCritEnterNoWait(OS_CRIT *pCrit)
1871 {
1872  return pCrit->EnterNoWait();
1873 }
1874 
1888 [[deprecated]] inline uint8_t OSCritLeave(OS_CRIT *pCrit)
1889 {
1890  return pCrit->Leave();
1891 }
1892 [[deprecated]] inline uint8_t OSCritLeaveAndUnlock(OS_CRIT *pCrit)
1893 {
1894  return pCrit->LeaveAndUnlock();
1895 }
1896 
1897 /*
1898  This function returns the current tasks priority
1899 */
1900 uint8_t OSTaskID(void);
1901 
1902 /*
1903  This function returns the current tasks name
1904 */
1905 const char *OSTaskName();
1906 
1907 void OSChangeTaskWhen(uint16_t task_prio, uint32_t to_when);
1908 
1921 inline void OSChangeTaskDly(uint16_t task_prio, uint32_t to_count)
1922 {
1923  uint32_t to_when = to_count + TimeTick;
1924  if (!to_when) to_when++;
1925  OSChangeTaskWhen(task_prio, to_when);
1926 }
1927 
1928 void OSDumpStack(void);
1929 
1930 #if (defined NBRTOS_STACKOVERFLOW) || (defined NBRTOS_STACKUNDERFLOW)
1931 void EnableOSStackProtector();
1932 extern "C" void OSStackProtectCtxSw(); // ASM task switcher
1933 extern "C" void OSStackProtector(); // extern because must be called from ASM
1934 #endif
1935 
1936 #ifdef NBRTOS_STACKCHECK
1937 
1943 void OSDumpTCBStacks(void);
1944 
1951 void OSDumpTasks(void);
1952 #endif
1953 
1954 #ifdef NBRTOS_TASKLIST
1955 
1960 void ShowTaskList(void);
1961 #endif
1962 
1963 #ifdef NBRTOS_TIME
1964 uint32_t GetCurrentTaskTime(uint32_t *const TotalTicks);
1965 void ShowTaskTimes(void);
1966 void ClearTaskTimes(void);
1967 #endif
1968 
1982 {
1983  public:
1987  OSLockObj() { OSLock(); };
1988 
1993 } __attribute__((packed));
1994 
2007 {
2008  OS_CRIT *pcrit;
2009 
2010  public:
2018  {
2019  pcrit = &ocrit;
2020  ocrit.Enter(0);
2021  };
2022 
2028 } __attribute__((packed));
2029 
2043 {
2044  OS_CRIT *pcrit;
2045 
2046  public:
2053  OSLockAndCritObj(OS_CRIT &ocrit) : pcrit(&ocrit) { pcrit->LockAndEnter(0); }
2054 
2062 };
2063 
2078 {
2079  OS_CRIT *pcrit;
2080 
2081  public:
2088  OSSpinCrit(OS_CRIT &ocrit) : pcrit(&ocrit)
2089  {
2090  while (pcrit->EnterNoWait() == OS_TIMEOUT) {}
2091  }
2092 
2099  ~OSSpinCrit() { pcrit->Leave(); }
2100 };
2101 
2102 class USERCritObj
2103 {
2104  public:
2105  USERCritObj() { USER_ENTER_CRITICAL(); }
2106  ~USERCritObj() { USER_EXIT_CRITICAL(); }
2107 };
2108 
2109 #endif
2110 
uint8_t LockAndEnter(uint32_t timeoutTicks=WAIT_FOREVER)
Locks the current task to prevent task switching, and claims a critical section.
Definition: nbrtos.cpp:1388
OS_MBOX(void *msg)
Create and initialize a mailbox object with a given message.
Definition: nbrtos.h:409
#define OS_TIMEOUT
Timeout.
Definition: nbrtos.h:59
A FIFO is used to pass structures from one task to another. Note: Structures to be passed must have a...
Definition: nbrtos.h:736
uint8_t Init(int32_t cnt=0)
This function is used to initialize a semaphore structure. Note: This must be done before using a sem...
Definition: nbrtos.cpp:805
OS_FIFO_EL * PendNoWait(uint8_t &result)
Checks to see if a structure has been posted to a FIFO, but does not wait.
Definition: nbrtos.cpp:1294
uint8_t OSFlagPendAny(OS_FLAGS *flags, uint32_t bit_mask, uint16_t timeout)
This function waits a number of time ticks specified by timeout until any of the flags indicated by b...
Definition: nbrtos.h:1171
uint8_t PendAll(uint32_t bit_mask, uint16_t timeout)
This function waits a number of time ticks specified by timeout until all the flags indicated by bit_...
Definition: nbrtos/source/nbrtosflags.cpp:157
uint8_t OSQPostFirst(OS_Q *pq, void *msg)
This function posts a message like OSQPost, but posts the message at the head of the queue...
Definition: nbrtos.h:1652
uint8_t Leave()
This function releases the critical section.
Definition: nbrtos.cpp:1459
void OSTaskDelete(void)
This function deletes the current calling task, but we do not recommend the use of this function beca...
Definition: nbrtos.cpp:772
uint8_t PostUniqueFirst(void *msg)
This function checks to see if a message already exists in a queue, and if it doesn&#39;t, it then posts it at the head of the queue. Note: Any higher priority task waiting on this queue will be started.
Definition: nbrtos.cpp:1173
uint8_t OSQPostUnique(OS_Q *pq, void *msg)
This function posts a message like OSQPost, but only if the message isn&#39;t already in the queue The fu...
Definition: nbrtos.h:1673
A simple wrapper class that uses an OS_CRIT object to try and claim a critical section, and will continue the attempt until it is able to do so.
Definition: nbrtos.h:2077
uint8_t PendAllNoWait(uint32_t bit_mask)
This function immediately checks to see if all the flag bits indicated by bit_mask are set; it does n...
Definition: nbrtos/source/nbrtosflags.cpp:179
OS_CRIT()
Create and initialize an OS_CRIT object.
Definition: nbrtos.h:903
Semaphores are used to control access to shared resource critical section, or to communicate between ...
Definition: nbrtos.h:318
uint8_t Init(void **pQueueStorage, uint8_t size)
Sets the queue object to its initial state.
Definition: nbrtos.cpp:1031
TickTimeouts are used to facilitate sequential function calls with timeout parameters that need to in...
Definition: nbrtos.h:112
void * Pend(uint32_t timeoutTicks, uint8_t &result)
Wait timeout ticks for another task to post to the queue. Note: A timeout value of 0 (zero) waits for...
Definition: nbrtos.cpp:1070
A queue functions as a fixed size FIFO for communication between tasks.
Definition: nbrtos.h:531
uint8_t Pend(uint32_t timeoutTicks=WAIT_FOREVER)
Wait timeout ticks for the value of the semaphore to be non zero. Note: A timeout value of 0 (zero) w...
Definition: nbrtos.cpp:845
void OSFlagSet(OS_FLAGS *flags, uint32_t bits_to_set)
This function sets the corresponding bits asserted in bits_to_set of an OS_FLAGS object pointed to by...
Definition: nbrtos.h:1137
#define WAIT_FOREVER
Definition: nbrtos.h:71
A simple wrapper class that helps use OS locks effectively.
Definition: nbrtos.h:1981
OS_FLAGS()
Create and initialize an OS_FLAG object.
Definition: nbrtos/source/nbrtosflags.cpp:87
void ShowTaskList(void)
This functions dumps the current RTOS task states to stdio.
Definition: nbrtos.cpp:74
OS_FIFO_EL * PendNoWait()
Checks to see if a structure has been posted to a FIFO, but does not wait. This is the same as PendNo...
Definition: nbrtos.h:847
uint8_t Init(void *msg=NULL)
Sets the mailbox object to its initial state.
Definition: nbrtos.cpp:915
OS_FIFO_EL * Pend(uint32_t timeoutTicks, uint8_t &result)
This function pends on a FIFO for a specified number of ticks.
Definition: nbrtos.cpp:1254
uint8_t PendNoWait()
Pend on a semaphore with no waiting period.
Definition: nbrtos.cpp:883
uint8_t OSMboxInit(OS_MBOX *pmbox, void *msg)
This function is used to initialize an OS_MBOX structure.
Definition: nbrtos.h:1542
void * PendNoWait()
Checks to see if a message has been posted to a mailbox, but does not wait. This is the same as PendN...
Definition: nbrtos.h:500
void OSLock(void)
Calling the OSLock function will prevent the OS from changing tasks.
Definition: nbrtos.cpp:509
TickTimeout(uint32_t timeout)
Create and initialize the Timeout.
Definition: nbrtos.h:145
uint8_t EnterNoWait()
This function tries to enter or claim the critical section. However, this function does not wait if i...
Definition: nbrtos.cpp:1439
void OSFlagClear(OS_FLAGS *flags, uint32_t bits_to_clr)
This function clears the bits asserted in bits_to_clr of an OS_FLAGS object pointed to by *flags...
Definition: nbrtos.h:1152
uint8_t Post(void *pItem)
This function posts a message to a Queue. Note: Any higher priority task waiting on this queue will b...
Definition: nbrtos.cpp:1135
uint8_t OSTaskCreatewName(void(*task)(void *dptr), void *data, void *pstktop, void *pstkbot, uint8_t prio, const char *name)
uint8_t LeaveAndUnlock()
This function unlocks the task and releases the critical section.
Definition: nbrtos.cpp:1452
uint8_t OSCritLeave(OS_CRIT *pCrit)
This function releases the critical section.
Definition: nbrtos.h:1888
OS_MBOX()
Create and initialize a mailbox object.
Definition: nbrtos.h:402
~OSSpinCrit()
Initialize the OSSpinCrit object, and then call Leave() on the OS_CRIT object that is passed in...
Definition: nbrtos.h:2099
uint8_t Post(OS_FIFO_EL *pToPost)
This function posts to a FIFO object.
Definition: nbrtos.cpp:1318
void OSChangeTaskDly(uint16_t task_prio, uint32_t to_count)
This function allows the User to modify the timeout delay for a task that is waiting.
Definition: nbrtos.h:1921
uint8_t OSQPostUniqueFirst(OS_Q *pq, void *msg)
This function posts a message like OSQPostFirst, but only if the message isn&#39;t already in the queue T...
Definition: nbrtos.h:1694
friend void ForceReboot(bool fromIRQ)
Forces the system hardware to perform a soft reset.
uint8_t OSQInit(OS_Q *pq, void **start, uint8_t size)
A queue functions as a fixed size FIFO for communication between tasks. This function initializes an ...
Definition: nbrtos.h:1614
uint8_t OSFifoPost(OS_FIFO *pFifo, OS_FIFO_EL *pToPost)
This function posts to a FIFO.
Definition: nbrtos.h:1763
OS_FIFO_EL * OSFifoPend(OS_FIFO *pFifo, uint16_t timeout)
This function pends on a FIFO.
Definition: nbrtos.h:1796
uint8_t OSFlagPendAll(OS_FLAGS *flags, uint32_t bit_mask, uint16_t timeout)
This function waits a number of time ticks specified by timeout until all the flags indicated by bit_...
Definition: nbrtos.h:1208
~OSLockObj()
Destructs the OSLockObj and calls OSUnlock().
Definition: nbrtos.h:1992
uint32_t val() const
Get the timeout duration to be passed to a function utilizing timeout ticks.
Definition: nbrtos.h:153
OSLockAndCritObj(OS_CRIT &ocrit)
Initialize the OSCriticalSectionObj object, and then call LockAndEnter() on the OS_CRIT object that i...
Definition: nbrtos.h:2053
void * OSMboxPendNoWait(OS_MBOX *pmbox, uint8_t *err)
OSMboxPendNoWait() is identical to OSMboxPend(), but it does not wait.
Definition: nbrtos.h:1594
uint8_t OSFifoPostFirst(OS_FIFO *pFifo, OS_FIFO_EL *pToPost)
This function is identical to OSFifoPost(), but the element posted is put at the beginning of the FIF...
Definition: nbrtos.h:1780
uint8_t OSQPost(OS_Q *pq, void *msg)
This function posts a message to a Queue.
Definition: nbrtos.h:1633
void Set(uint32_t bits_to_set)
This function sets the corresponding bits asserted in bits_to_set.
Definition: nbrtos/source/nbrtosflags.cpp:104
uint8_t OSFifoInit(OS_FIFO *pFifo)
Initialize a FIFO, which is used to pass structures from one task to another.
Definition: nbrtos.h:1745
bool expired() const
Determine whether the timeout duration has elapsed.
Definition: nbrtos.h:167
uint8_t OSFlagPendAllNoWait(OS_FLAGS *flags, uint32_t bit_mask)
This function immediately checks to see if all the flag bits indicated by bit_mask are set; it does n...
Definition: nbrtos.h:1226
uint8_t PostFirst(OS_FIFO_EL *pToPost)
This function is identical to Post(), but the element posted is put on the beginning of the FIFO list...
Definition: nbrtos.cpp:1341
uint8_t OSSemPendNoWait(OS_SEM *psem)
OSSemPendNoWait() is identical to OSSemPend(), but it does not wait.
Definition: nbrtos.h:1523
uint8_t Init()
Initialize an OS_CRIT object to its default state.
Definition: nbrtos.cpp:1378
A simple wrapper class that helps utilize OS_CRIT objects to lock tasks and enter critical sections m...
Definition: nbrtos.h:2042
uint8_t Enter(uint32_t timeoutTicks=WAIT_FOREVER)
This function tries to enter or claim the critical section.
Definition: nbrtos.cpp:1399
uint8_t OSSemPost(OS_SEM *psem)
Increases the value of the semaphore by one. Note: If any higher priority tasks were waiting on the s...
Definition: nbrtos.h:1485
uint8_t OSChangePrio(uint32_t newp)
This function changes the priority of the calling task.
Definition: nbrtos.cpp:731
void * PendNoWait(uint8_t &result)
Checks to see if a message has been posted to a queue, but does not wait. An err holds the error code...
Definition: nbrtos.cpp:1111
uint32_t OSFlagState(OS_FLAGS *flags)
This function returns the current values of the flags stored in the OS_FLAGS object structure...
Definition: nbrtos.h:1242
uint8_t PostFirst(void *pItem)
This function posts a message like OSQPost, but posts the message at the head of the queue...
Definition: nbrtos.cpp:1198
uint8_t OSSemPend(OS_SEM *psem, uint16_t timeout)
Wait timeout ticks for the value of the semaphore to be non zero. Note: A timeout value of 0 (zero) w...
Definition: nbrtos.h:1505
void * PendNoWait()
Checks to see if a message has been posted to a queue, but does not wait. This is the same as PendNoW...
Definition: nbrtos.h:691
void Init()
Initialize an OS_FLAG object to its default value.
Definition: nbrtos/source/nbrtosflags.cpp:95
uint8_t PendAnyNoWait(uint32_t bit_mask)
This function immediately checks to see if any of the flag bits indicated by bit_mask are set; it doe...
Definition: nbrtos/source/nbrtosflags.cpp:145
uint8_t PendAny(uint32_t bit_mask, uint16_t timeout)
This function waits a number of time ticks specified by timeout until any of the flags indicated by b...
Definition: nbrtos/source/nbrtosflags.cpp:126
OS_FIFO()
Create and initialize a FIFO object.
Definition: nbrtos.h:747
void * PendNoWait(uint8_t &result)
Checks to see if a message has been posted to a mailbox, but does not wait.
Definition: nbrtos.cpp:983
uint8_t OSCritEnterNoWait(OS_CRIT *pCrit)
This function tries to enter or claim the critical section.
Definition: nbrtos.h:1870
~OSCriticalSectionObj()
Destructs the OSCriticalSectionObj object, and call Leave() on the OS_CRIT object that was passed int...
Definition: nbrtos.h:2027
OS_FIFO_EL * Pend(uint32_t timeoutTicks=WAIT_FOREVER)
This function pends on a FIFO for a specified number of ticks. This is the same as Pend(uint32_t time...
Definition: nbrtos.h:831
uint8_t Post(void *msg)
This function posts a message to the mailbox.
Definition: nbrtos.cpp:1008
An OS_CRIT object is used to establish critical sections of code that can only be run by one task at ...
Definition: nbrtos.h:893
#define NULL
Definition: nm_bsp.h:76
uint8_t Post()
This function increases the value of the semaphore by one. Note: If any higher priority tasks were wa...
Definition: nbrtos.cpp:903
OS_Q(void **pQueueStorage, uint8_t size)
Create and initialize a queue object.
Definition: nbrtos.h:554
void Clear(uint32_t bits_to_clr)
This function clears the bits asserted in bits_to_clr.
Definition: nbrtos/source/nbrtosflags.cpp:112
void * OSMboxPend(OS_MBOX *pmbox, uint16_t timeout, uint8_t *err)
Wait timeout ticks for some other task to post to the Mailbox.
Definition: nbrtos.h:1578
A simple wrapper class that helps utilize OS_CRIT objects more effectively.
Definition: nbrtos.h:2006
void * OSQPendNoWait(OS_Q *pq, uint8_t *err)
OSQPendNoWait() is identical to the OSQPend() function but it does not wait.
Definition: nbrtos.h:1727
void OSUnlock(void)
This function unlocks the OS.
Definition: nbrtos.cpp:524
OSSpinCrit(OS_CRIT &ocrit)
Initialize the OSSpinCrit object, and then call EnterNoWait() repeatedly on the OS_CRIT object that i...
Definition: nbrtos.h:2088
uint32_t State()
This function returns the current values of the flags stored in the OS_FLAGS object structure...
Definition: nbrtos/source/nbrtosflags.cpp:187
void OSDumpTasks(void)
This function dumps the state and call stack for every task to stdout. This function is useful for de...
Mailboxes are used to communicate between tasks.
Definition: nbrtos.h:391
OS_FIFO_EL * OSFifoPendNoWait(OS_FIFO *pFifo)
This function is identical to the OSFifoPen() function, but it does not wait.
Definition: nbrtos.h:1811
OSCriticalSectionObj(OS_CRIT &ocrit)
Initialize the OSCriticalSectionObj object, and then call Enter() on the OS_CRIT object that is passe...
Definition: nbrtos.h:2017
An OS_FLAGS object is used to set, clear, and pend on a set of flags that is held and maintained by t...
Definition: nbrtos.h:1009
void * Pend(uint32_t timeoutTicks, uint8_t &result)
Wait timeout ticks for some other task to post to the mailbox. Note: Pend will wait forever if 0 is p...
Definition: nbrtos.cpp:943
void OSTimeDly(uint32_t to_count)
Delay the task until the specified value of the system timer ticks. The number of system ticks per se...
Definition: nbrtos.h:1368
~OSLockAndCritObj()
Initialize the OSCriticalSectionObj object, and then call LeaveAndUnlock() on the OS_CRIT object that...
Definition: nbrtos.h:2061
uint8_t OSCritInit(OS_CRIT *pCrit)
This function initializes the critical section.
Definition: nbrtos.h:1829
uint8_t OSMboxPost(OS_MBOX *pmbox, void *msg)
This function posts a message to a Mail box.
Definition: nbrtos.h:1561
void OSDumpTCBStacks(void)
This function dumps information about the UCOS stacks and tasks to stdout. This function is useful fo...
uint8_t OSFlagPendAnyNoWait(OS_FLAGS *flags, uint32_t bit_mask)
This function immediately checks to see if any of the flag bits indicated by bit_mask are set; it doe...
Definition: nbrtos.h:1189
OSLockObj()
Initialize the OSLockObj and calls OSLock().
Definition: nbrtos.h:1987
void * Pend(uint32_t timeoutTicks=WAIT_FOREVER)
Wait timeout ticks for some other task to post to the mailbox. This is the same as Pend(uint32_t time...
Definition: nbrtos.h:482
void * OSQPend(OS_Q *pq, uint16_t timeout, uint8_t *err)
Wait timeout ticks for another task to post to the queue.
Definition: nbrtos.h:1711
uint8_t OSSemInit(OS_SEM *psem, long value)
Initializes a semaphore.
Definition: nbrtos.h:1466
uint8_t PostUnique(void *pItem)
This function checks to see if a message already exists in a queue, and if it doesn&#39;t, it then posts it. Note: Any higher priority task waiting on this queue will be started.
Definition: nbrtos.cpp:1154
OS_Q()
Create and initialize a queue object.
Definition: nbrtos.h:546
void * Pend(uint32_t timeoutTicks=WAIT_FOREVER)
Wait timeout ticks for another task to post to the queue. This is the same as Pend(uint32_t timeoutTi...
Definition: nbrtos.h:675
uint8_t OSCritEnter(OS_CRIT *pCrit, uint16_t timeout)
This function tries to enter or claim the critical section.
Definition: nbrtos.h:1852
uint8_t Init()
Sets the FIFO object to its initial state.
Definition: nbrtos.cpp:1238
OS_SEM(int32_t cnt=0)
Create and initialize a semaphore.
Definition: nbrtos.h:332