54 #include "BRTOSConfig.h"
69 #define BRTOS_VERSION "BRTOS Ver. 2.00"
84 #ifndef READY_LIST_VAR
85 #define READY_LIST_VAR
88 #define BRTOS_BIG_ENDIAN (0)
89 #define BRTOS_LITTLE_ENDIAN (1)
92 #define BRTOS_TH OS_CPU_TYPE
95 #if (!defined(ostick_t) && !defined(osdtick_t))
96 #define ostick_t uint16_t
97 #define osdtick_t uint32_t
99 #define sizeof_ostick_t sizeof(ostick_t)
103 #define READY (uint8_t)0
104 #define SUSPENDED (uint8_t)1
105 #define BLOCKED (uint8_t)2
106 #define MUTEX_PRIO (uint8_t)0xFE
107 #define EMPTY_PRIO (uint8_t)0xFF
111 #if (ostick_t == uint64_t)
112 #define MAX_TIMER 0xffffffffffffffffULL
113 #elif (ostick_t == uint32_t)
114 #define MAX_TIMER 0xffffffff
116 #define MAX_TIMER 0xffff
118 #define NO_TIMEOUT (ostick_t)(MAX_TIMER - 1)
119 #define EXIT_BY_TIMEOUT (ostick_t)(MAX_TIMER - 2)
120 #define TICK_COUNT_OVERFLOW (ostick_t)(MAX_TIMER - 3)
121 #define TickCountOverFlow TICK_COUNT_OVERFLOW
123 #define OK (uint8_t)0
125 #define NO_MEMORY (uint8_t)1
126 #define STACK_SIZE_TOO_SMALL (uint8_t)2
127 #define END_OF_AVAILABLE_PRIORITIES (uint8_t)3
128 #define BUSY_PRIORITY (uint8_t)4
129 #define INVALID_TIME (uint8_t)5
130 #define TIMEOUT (uint8_t)6
131 #define CANNOT_ASSIGN_IDLE_TASK_PRIO (uint8_t)7
132 #define NOT_VALID_TASK (uint8_t)8
133 #define NO_TASK_DELAY (uint8_t)9
134 #define END_OF_AVAILABLE_TCB (uint8_t)10
135 #define EXIT_BY_NO_ENTRY_AVAILABLE (uint8_t)11
136 #define TASK_WAITING_EVENT (uint8_t)12
137 #define CANNOT_UNINSTALL_IDLE_TASK (uint8_t)13
138 #define EXIT_BY_NO_RESOURCE_AVAILABLE (uint8_t)14
148 #define ALLOC_EVENT_OK (uint8_t)0
149 #define NO_AVAILABLE_EVENT (uint8_t)1
150 #define NO_AVAILABLE_MEMORY (uint8_t)2
151 #define INVALID_PARAMETERS (uint8_t)3
152 #define IRQ_PEND_ERR (uint8_t)4
153 #define ERR_SEM_OVF (uint8_t)5
154 #define ERR_MUTEX_OVF (uint8_t)6
155 #define ERR_EVENT_NO_CREATED (uint8_t)7
156 #define NULL_EVENT_POINTER (uint8_t)8
157 #define ERR_EVENT_OWNER (uint8_t)9
158 #define DELETE_EVENT_OK (uint8_t)10
159 #define AVAILABLE_RESOURCE (uint8_t)11
160 #define BUSY_RESOURCE (uint8_t)12
161 #define AVAILABLE_MESSAGE (uint8_t)13
162 #define NO_MESSAGE (uint8_t)14
178 #define READ_BUFFER_OK 0
179 #define WRITE_BUFFER_OK 0
180 #define BUFFER_UNDERRUN 1
181 #define CLEAN_BUFFER_OK 2
182 #define NO_ENTRY_AVAILABLE 3
202 #if (NUMBER_OF_PRIORITIES > 16)
203 #define configMAX_TASK_INSTALL 32
204 #define configMAX_TASK_PRIORITY 31
205 typedef uint32_t PriorityType;
207 #if (NUMBER_OF_PRIORITIES > 8)
208 #define configMAX_TASK_INSTALL 16
209 #define configMAX_TASK_PRIORITY 15
210 typedef uint16_t PriorityType;
212 #define configMAX_TASK_INSTALL 8
213 #define configMAX_TASK_PRIORITY 7
214 typedef uint8_t PriorityType;
240 const CHAR8 * TaskName;
248 #if (BRTOS_DYNAMIC_TASKS_ENABLED == 1)
251 #if (COMPUTES_TASK_LOAD == 1)
258 uint8_t SuspendedType;
261 struct Context *Next;
262 struct Context *Previous;
290 #if (BRTOS_BINARY_SEM_EN == 1)
523 #if (TASK_WITH_PARAMETERS == 1)
524 uint8_t OSInstallTask(
void(*FctPtr)(
void*),
const CHAR8 *TaskName, uint16_t USER_STACKED_BYTES,uint8_t iPriority,
void *parameters, OS_CPU_TYPE *TaskHandle);
525 #define InstallTask OSInstallTask
527 uint8_t OSInstallTask(
void(*FctPtr)(
void),
const CHAR8 *TaskName, uint16_t USER_STACKED_BYTES,uint8_t iPriority, OS_CPU_TYPE *TaskHandle);
528 #define InstallTask OSInstallTask
539 uint8_t OSUninstallTask(BRTOS_TH TaskHandle, OS_CPU_TYPE safety_off);
540 #define UninstallTask OSUninstallTask
547 #if (TASK_WITH_PARAMETERS == 1)
548 void Idle(
void *parameters);
559 #if (TIMER_HOOK_EN == 1)
560 void BRTOS_TimerHook(
void);
568 #if (IDLE_HOOK_EN == 1)
613 #define DelayTask OSDelayTask
627 uint8_t
OSDelayTaskHMSM(uint8_t hours, uint8_t minutes, uint8_t seconds, uint16_t miliseconds);
628 #define DelayTaskHMSM OSDelayTaskHMSM
676 #define BlockPriority OSBlockPriority
689 #define UnBlockPriority OSUnBlockPriority
697 uint8_t OSBlockTask(BRTOS_TH iTaskNumber);
698 #define BlockTask OSBlockTask
707 uint8_t OSUnBlockTask(BRTOS_TH iTaskNumber);
708 #define UnBlockTask OSUnBlockTask
719 #define BlockMultipleTask OSBlockMultipleTask
730 #define UnBlockMultipleTask OSUnBlockMultipleTask
737 #define BRTOS_Init BRTOSInit
761 #if (BRTOS_SEM_EN == 1)
762 extern BRTOS_Sem BRTOS_Sem_Table[BRTOS_MAX_SEM];
766 #if (BRTOS_MUTEX_EN == 1)
767 extern BRTOS_Mutex BRTOS_Mutex_Table[BRTOS_MAX_MUTEX];
771 #if (BRTOS_MBOX_EN == 1)
772 extern BRTOS_Mbox BRTOS_Mbox_Table[BRTOS_MAX_MBOX];
776 #if (BRTOS_QUEUE_EN == 1)
777 extern BRTOS_Queue BRTOS_Queue_Table[BRTOS_MAX_QUEUE];
779 extern OS_QUEUE BRTOS_OS_QUEUE_Table[BRTOS_MAX_QUEUE];
791 #if (BRTOS_SEM_EN == 1)
802 uint8_t OSSemCreate (uint8_t cnt,
BRTOS_Sem **event);
804 #if (BRTOS_BINARY_SEM_EN == 1)
814 uint8_t OSSemBinaryCreate(uint8_t bit,
BRTOS_Sem **event);
838 uint8_t OSSemPend (
BRTOS_Sem *pont_event, ostick_t timeout);
848 uint8_t OSSemPost(
BRTOS_Sem *pont_event);
851 #if (BRTOS_MUTEX_EN == 1)
861 uint8_t OSMutexCreate (
BRTOS_Mutex **event, uint8_t HigherPriority);
883 uint8_t OSMutexAcquire(
BRTOS_Mutex *pont_event, ostick_t time_wait);
898 #if (BRTOS_MBOX_EN == 1)
909 uint8_t OSMboxCreate (
BRTOS_Mbox **event,
void *message);
933 uint8_t OSMboxPend (
BRTOS_Mbox *pont_event,
void **Mail, ostick_t timeout);
945 uint8_t OSMboxPost(
BRTOS_Mbox *pont_event,
void *message);
963 #if (BRTOS_QUEUE_EN == 1)
974 uint8_t OSQueueCreate(uint16_t size,
BRTOS_Queue **event);
984 uint8_t OSWQueue(
OS_QUEUE *cqueue,uint8_t data);
994 uint8_t OSRQueue(
OS_QUEUE *cqueue, uint8_t* pdata);
1013 uint8_t OSQueuePend (
BRTOS_Queue *pont_event, uint8_t* pdata, ostick_t timeout);
1025 uint8_t OSQueuePost(
BRTOS_Queue *pont_event, uint8_t data);
1042 #if (BRTOS_QUEUE_16_EN == 1)
1053 uint8_t OSQueue16Create(OS_QUEUE_16 *cqueue, uint16_t size);
1063 uint8_t OSWQueue16(OS_QUEUE_16 *cqueue,uint16_t data);
1072 uint8_t OSRQueue16(OS_QUEUE_16 *cqueue, uint16_t *pdata);
1080 uint8_t OSCleanQueue16(OS_QUEUE_16 *cqueue);
1098 #if (BRTOS_QUEUE_32_EN == 1)
1109 uint8_t OSQueue32Create(OS_QUEUE_32 *cqueue, uint16_t size);
1119 uint8_t OSWQueue32(OS_QUEUE_32 *cqueue,uint32_t data);
1128 uint8_t OSRQueue32(OS_QUEUE_32 *cqueue, uint32_t *pdata);
1136 uint8_t OSCleanQueue32(OS_QUEUE_32 *cqueue);
1149 #if (BRTOS_DYNAMIC_QUEUE_ENABLED == 1)
1163 uint8_t OSDQueueCreate(uint16_t queue_lenght, OS_CPU_TYPE type_size,
BRTOS_Queue **event);
1193 uint8_t OSDQueuePend (
BRTOS_Queue *pont_event,
void *pdata, ostick_t time_wait);
1204 uint8_t OSDQueuePost(
BRTOS_Queue *pont_event,
void *pdata);
1222 extern PriorityType OSReadyList;
1223 extern PriorityType OSBlockedList;
1224 extern const PriorityType PriorityMask[configMAX_TASK_PRIORITY+1];
1231 extern volatile uint8_t SelectedTask;
1235 extern volatile uint32_t
OSDuty;
1239 #ifdef TICK_TIMER_32BITS
1246 #if (!BRTOS_DYNAMIC_TASKS_ENABLED)
1247 extern OS_CPU_TYPE STACK[(HEAP_SIZE /
sizeof(OS_CPU_TYPE))];
1249 extern OS_CPU_TYPE QUEUE_STACK[(QUEUE_HEAP_SIZE /
sizeof(OS_CPU_TYPE))];
1251 #error("You must define the OS_CPU_TYPE !!!")
1257 #if (PROCESSOR == ATMEGA)
1258 #if (!defined __GNUC__)
1263 extern PGM_P CONST BRTOSStringTable[]
PROGMEM;
1265 #if (PROCESSOR == PIC18)
1266 extern const rom CHAR8 *version;
1268 extern const CHAR8 *version;
1272 #if ((PROCESSOR == ATMEGA) || (PROCESSOR == PIC18))
1273 extern CHAR8 BufferText[32];
1285 #if (defined ISR_DEDICATED_STACK && ISR_DEDICATED_STACK == 1)
1288 #define OS_INT_ENTER() if (!iNesting){OS_SAVE_SP(); OS_RESTORE_ISR_SP(); }; iNesting++;
1290 #define OS_INT_EXIT() \
1291 CriticalDecNesting(); \
1295 SelectedTask = OSSchedule(); \
1296 if (currentTask != SelectedTask){ \
1297 OS_SAVE_CONTEXT(); \
1299 ContextTask[currentTask].StackPoint = SPvalue; \
1300 currentTask = SelectedTask; \
1301 SPvalue = ContextTask[currentTask].StackPoint; \
1303 OS_RESTORE_CONTEXT(); \
1310 #define OS_INT_ENTER() iNesting++;
1313 #if (COMPUTES_TASK_LOAD == 1)
1314 extern void COMPUTE_TASK_LOAD(
void);
1316 #define OS_INT_EXIT() \
1317 CriticalDecNesting(); \
1320 SelectedTask = OSSchedule(); \
1321 if (currentTask != SelectedTask){ \
1322 COMPUTE_TASK_LOAD(); \
1323 OS_SAVE_CONTEXT(); \
1325 ContextTask[currentTask].StackPoint = SPvalue; \
1326 currentTask = SelectedTask; \
1327 SPvalue = ContextTask[currentTask].StackPoint; \
1329 OS_RESTORE_CONTEXT(); \
1334 #define OS_INT_EXIT() \
1335 CriticalDecNesting(); \
1338 SelectedTask = OSSchedule(); \
1339 if (currentTask != SelectedTask){ \
1340 OS_SAVE_CONTEXT(); \
1342 ContextTask[currentTask].StackPoint = SPvalue; \
1343 currentTask = SelectedTask; \
1344 SPvalue = ContextTask[currentTask].StackPoint; \
1346 OS_RESTORE_CONTEXT(); \
1359 #define RemoveFromDelayList() \
1369 Head = Task->Next; \
1370 Head->Previous = NULL; \
1377 Tail = Task->Previous; \
1378 Tail->Next = NULL; \
1382 Task->Next->Previous = Task->Previous; \
1383 Task->Previous->Next = Task->Next; \
1388 #define IncludeTaskIntoDelayList() \
1392 Tail->Next = Task; \
1393 Task->Previous = Tail; \
1395 Tail->Next = NULL; \
1401 Task->Next = NULL; \
1402 Task->Previous = NULL; \
uint8_t OSEventWait
Counter of waiting Tasks.
Definition: BRTOS.h:321
void OS_TICK_HANDLER(void)
Tick timer interrupt handler routine (Internal kernel function).
Definition: BRTOS.c:563
PGM_P CONST BRTOSStringTable[] PROGMEM
Informs BRTOS version.
Definition: BRTOS.c:77
uint8_t OSEventOwner
Defines mutex owner.
Definition: BRTOS.h:318
uint8_t OSBlockPriority(uint8_t iPriority)
Blocks a specific priority Blocks the task that is associated with the specified priority. The user must be careful when using this function in together with mutexes. This can lead to undesired results due the "cealing priority" property used in the mutex.
Definition: BRTOS.c:722
volatile uint32_t OSDutyTmp
Used to compute the CPU load.
Definition: BRTOS.c:133
#define configMAX_TASK_INSTALL
Task Defines.
Definition: BRTOS.h:212
uint8_t OSEventAllocated
Indicate if the event is allocated or not.
Definition: BRTOS.h:316
uint8_t NumberOfInstalledTasks
Number of Installed tasks at the moment.
Definition: BRTOS.c:114
uint8_t OSUnBlockMultipleTask(uint8_t TaskStart, uint8_t TaskNumber)
UnBlocks a set of tasks.
Definition: BRTOS.c:993
PriorityType OSEventWaitList
Task wait list for event to occur.
Definition: BRTOS.h:293
uint8_t OSEventWait
Counter of waiting Tasks.
Definition: BRTOS.h:289
uint16_t OSQSize
Size of the queue - Defined in the create queue function.
Definition: BRTOS.h:403
uint8_t OSEventAllocated
Indicate if the event is allocated or not.
Definition: BRTOS.h:287
volatile uint8_t currentTask
Current task being executed.
Definition: BRTOS.c:115
uint16_t OSQEntries
Size of data inside the queue.
Definition: BRTOS.h:404
uint8_t OSOriginalPriority
Save original priority of Mutex owner task - used to the priority ceiling implementation.
Definition: BRTOS.h:320
uint8_t OSEventState
Mailbox state - Defines if the message is available or not.
Definition: BRTOS.h:347
stack_pointer_t StackAddress
Virtual stack pointer.
Definition: BRTOS.c:108
System Time managment struct declarations and functions prototypes.
uint8_t PriorityVector[configMAX_TASK_INSTALL]
Allocate task priorities.
Definition: BRTOS.c:101
uint8_t * OSQIn
Pointer to the next queue entry.
Definition: BRTOS.h:401
void Idle(void)
Idle Task. May be used to implement low power commands.
Definition: BRTOS.c:1059
volatile uint16_t LastOSDuty
Last CPU load computed.
Definition: BRTOS.c:138
ostick_t OSGetCount(void)
Return current tick count. Internal BRTOS function.
Definition: BRTOS.c:357
ContextType ContextTask[NUMBER_OF_TASKS+1]
Definition: BRTOS.c:232
uint8_t OSEventCount
Queue Event Count - This value is increased with a post and decremented with a pend.
Definition: BRTOS.h:373
volatile uint32_t OSDuty
Used to compute the CPU load.
Definition: BRTOS.c:132
uint8_t BRTOSStart(void)
Start the Operating System Scheduler The user must call this function to start the tasks execution...
Definition: BRTOS.c:640
uint8_t OSDelayTaskHMSM(uint8_t hours, uint8_t minutes, uint8_t seconds, uint16_t miliseconds)
Wait for a specified period (in hours, minutes, seconds and miliseconds). A task that calling this fu...
Definition: BRTOS.c:509
uint8_t OSDelayTask(ostick_t time_wait)
Wait for a specified period. A task that calling this function will be suspended for a certain time...
Definition: BRTOS.c:424
void initEvents(void)
Initialize event control blocks.
Definition: BRTOS.c:1548
ostick_t OSGetTickCount(void)
Return current tick count. The user must call this function in order to receive the current tick coun...
Definition: BRTOS.c:333
uint16_t iQueueAddress
Queue heap control.
Definition: BRTOS.c:105
PriorityType OSEventWaitList
Task wait list for event to occur.
Definition: BRTOS.h:376
uint8_t * OSQStart
Pointer to the queue start.
Definition: BRTOS.h:399
uint8_t * OSQEnd
Pointer to the queue end.
Definition: BRTOS.h:400
BRTOS_TH OSGetCurrentTaskHandle(void)
Return the handle of the current task. The user must call this function in order to receive the curre...
Definition: BRTOS.c:285
uint8_t OSEventAllocated
Indicate if the event is allocated or not.
Definition: BRTOS.h:345
void * OSEventPointer
Pointer to queue structure.
Definition: BRTOS.h:375
void PreInstallTasks(void)
Function that initialize the kernel main variables. This function resets the kernel main variables...
Definition: BRTOS.c:675
void BRTOSInit(void)
Initialize BRTOS control blocks and tick timer (Internal kernel function).
Definition: BRTOS.c:1520
PriorityType OSEventWaitList
Task wait list for event to occur.
Definition: BRTOS.h:322
uint8_t OSEventState
Mutex state - Defines if the resource is available or not.
Definition: BRTOS.h:317
PriorityType OSEventWaitList
Task wait list for event to occur.
Definition: BRTOS.h:348
BRTOS_TH OSGetTaskPriority(BRTOS_TH task_handle)
Return the priority of the specified task. The user must call this function in order to receive the p...
Definition: BRTOS.c:309
uint8_t OSEventWait
Counter of waiting Tasks.
Definition: BRTOS.h:346
void OSIncCounter(void)
Update the tick counter.
Definition: BRTOS.c:402
uint8_t * OSQOut
Pointer to the next data in the queue output.
Definition: BRTOS.h:402
uint8_t OSEventAllocated
Indicate if the event is allocated or not.
Definition: BRTOS.h:372
uint32_t TaskAlloc
Used to search a empty task control block.
Definition: BRTOS.c:142
uint8_t OSEventCount
Semaphore Count - This value is increased with a post and decremented with a pend.
Definition: BRTOS.h:288
uint8_t SAScheduler(PriorityType ReadyList)
Sucessive Aproximation Scheduler (Internal kernel function).
Definition: BRTOS.c:1595
void * OSEventPointer
Pointer to the message structure / type.
Definition: BRTOS.h:349
uint8_t iNesting
Used to inform if the current code position is an interrupt handler code.
Definition: BRTOS.c:143
uint16_t iStackAddress
Virtual stack counter - Informs the stack occupation in bytes.
Definition: BRTOS.c:102
uint8_t OSUnBlockPriority(uint8_t iPriority)
UnBlock a specific priority UnBlocks the task that is associated with the specified priority...
Definition: BRTOS.c:772
uint8_t OSEventWait
Counter of waiting Tasks.
Definition: BRTOS.h:374
uint8_t OSSchedule(void)
Priority Preemptive Scheduler (Internal kernel function).
Definition: BRTOS.c:261
uint8_t OSMaxPriority
Defines max priority accessing resource.
Definition: BRTOS.h:319
uint8_t OSBlockMultipleTask(uint8_t TaskStart, uint8_t TaskNumber)
Blocks a set of tasks.
Definition: BRTOS.c:941