uint32_t osAtomicInc32(uint32_t *n) { uint32_t m; //Enter critical section osSuspendAllTasks(); //Increment the specified 32-bit integer m = ++(*n); //Leave critical section osResumeAllTasks(); //Return the incremented value return m; }
void osFreeMem(void *p) { //Make sure the pointer is valid if(p != NULL) { //Debug message TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p); //Enter critical section osSuspendAllTasks(); //Free memory block free(p); //Leave critical section osResumeAllTasks(); } }
void *osAllocMem(size_t size) { void *p; //Enter critical section osSuspendAllTasks(); //Allocate a memory block p = malloc(size); //Leave critical section osResumeAllTasks(); //Debug message TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n", size, (uintptr_t) p); //Return a pointer to the newly allocated memory block return p; }
OsTask *osCreateTask(const char_t *name, OsTaskCode taskCode, void *params, size_t stackSize, int_t priority) { uint_t i; OS_TASK *task; void *stack; //Enter critical section osSuspendAllTasks(); //Loop through TCB table for(i = 0; i < OS_PORT_MAX_TASKS; i++) { //Check whether the current entry is free if(tcbTable[i] == NULL) break; } //Any entry available in the table? if(i < OS_PORT_MAX_TASKS) { //Allocate a memory block to hold the task's control block task = osAllocMem(sizeof(OS_TASK)); //Successful memory allocation? if(task != NULL) { //Allocate a memory block to hold the task's stack stack = osAllocMem(stackSize * sizeof(uint_t)); //Successful memory allocation? if(stack != NULL) { //Create a new task OS_CreateTaskEx(task, name, priority, taskCode, stack, stackSize * sizeof(uint_t), 1, params); //Save TCB base address tcbTable[i] = task; //Save stack base address stkTable[i] = stack; } else { osFreeMem(task); //Memory allocation failed task = NULL; } } } else { //Memory allocation failed task = NULL; } //Leave critical section osResumeAllTasks(); //Return task pointer return task; }
OsTask *osCreateTask(const char_t *name, OsTaskCode taskCode, void *params, size_t stackSize, int_t priority) { OS_ERR err; CPU_INT32U i; CPU_STK stackLimit; OS_TCB *task; CPU_STK *stack; //The watermark limit is used to monitor and ensure that the stack does not overflow stackLimit = stackSize / 10; //Enter critical section osSuspendAllTasks(); //Loop through TCB table for(i = 0; i < OS_PORT_MAX_TASKS; i++) { //Check whether the current entry is free if(tcbTable[i] == NULL) break; } //Any entry available in the table? if(i < OS_PORT_MAX_TASKS) { //Allocate a memory block to hold the task's control block task = osAllocMem(sizeof(OS_TCB)); //Successful memory allocation? if(task != NULL) { //Allocate a memory block to hold the task's stack stack = osAllocMem(stackSize * sizeof(CPU_STK)); //Successful memory allocation? if(stack != NULL) { //Create a new task OSTaskCreate(task, (CPU_CHAR *) name, taskCode, params, priority, stack, stackLimit, stackSize, 0, 1, NULL, OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR, &err); //Check the return value if(err == OS_ERR_NONE) { //Save TCB base address tcbTable[i] = task; //Save stack base address stkTable[i] = stack; } else { //Clean up side effects osFreeMem(task); osFreeMem(stack); } } else { //Memory allocation failed err = OS_ERR_MEM_FULL; //Clean up side effects osFreeMem(task); } } else { //Memory allocation failed err = OS_ERR_MEM_FULL; } } else { //No entry available in the table err = OS_ERR_MEM_FULL; } //Leave critical section osResumeAllTasks(); //Check whether the task was successfully created if(err == OS_ERR_NONE) return task; else return NULL; }