Skip to content

Commit

Permalink
Implemented task scheduler. Did not implement priority queue.
Browse files Browse the repository at this point in the history
Could not test. TIVA on hand was defective.
  • Loading branch information
jackerzhaques committed May 17, 2019
1 parent 424c32e commit 85531ee
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 49 deletions.
74 changes: 37 additions & 37 deletions .cproject

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions .launches/TIVA-Task-Scheduler.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="com.ti.ccstudio.debug.launchType.device.debugging">
<stringAttribute key="com.ti.ccstudio.debug.debugModel.ATTR_LOAD_PROGRAM.P:\CCS\TIVA-Task-Scheduler\targetConfigs\Tiva TM4C1294NCPDT.ccxml.Stellaris In-Circuit Debug Interface/CORTEX_M4_0" value="ALL"/>
<stringAttribute key="com.ti.ccstudio.debug.debugModel.ATTR_PROGRAM.P:\CCS\TIVA-Task-Scheduler\targetConfigs\Tiva TM4C1294NCPDT.ccxml.Stellaris In-Circuit Debug Interface/CORTEX_M4_0" value="${build_artifact:TIVA-Task-Scheduler}"/>
<stringAttribute key="com.ti.ccstudio.debug.debugModel.ATTR_PROJECT.P:\CCS\TIVA-Task-Scheduler\targetConfigs\Tiva TM4C1294NCPDT.ccxml.Stellaris In-Circuit Debug Interface/CORTEX_M4_0" value="TIVA-Task-Scheduler"/>
<stringAttribute key="com.ti.ccstudio.debug.debugModel.ATTR_TARGET_CONFIG" value="${target_config_active_default:TIVA-Task-Scheduler}"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/TIVA-Task-Scheduler"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="com.ti.ccstudio.debug.sourceLocator"/>
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;sourceLookupDirector&gt;&#13;&#10;&lt;sourceContainers duplicates=&quot;false&quot;&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;default/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.debug.core.containerType.default&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;cpuSpecificContainer cpuName=&amp;quot;Stellaris In-Circuit Debug Interface/CORTEX_M4_0&amp;quot;&amp;gt;&amp;#13;&amp;#10;&amp;lt;childContainerEntry childMemento=&amp;quot;&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot; standalone=&amp;amp;quot;no&amp;amp;quot;?&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;amp;lt;project name=&amp;amp;quot;TIVA-Task-Scheduler&amp;amp;quot; referencedProjects=&amp;amp;quot;true&amp;amp;quot;/&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;quot; childType=&amp;quot;org.eclipse.debug.core.containerType.project&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&amp;lt;childContainerEntry childMemento=&amp;quot;&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot; standalone=&amp;amp;quot;no&amp;amp;quot;?&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;amp;lt;default/&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;quot; childType=&amp;quot;org.eclipse.debug.core.containerType.default&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&amp;lt;childContainerEntry childMemento=&amp;quot;&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot; standalone=&amp;amp;quot;no&amp;amp;quot;?&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;amp;lt;productsSource/&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;quot; childType=&amp;quot;com.ti.ccstudio.debug.containerType.products.source&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&amp;lt;childContainerEntry childMemento=&amp;quot;&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot; standalone=&amp;amp;quot;no&amp;amp;quot;?&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;amp;lt;deviceLibrarySource/&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;quot; childType=&amp;quot;com.ti.ccstudio.debug.containerType.device.library.source&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&amp;lt;childContainerEntry childMemento=&amp;quot;&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot; standalone=&amp;amp;quot;no&amp;amp;quot;?&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;amp;lt;librarySource/&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;quot; childType=&amp;quot;com.ti.ccstudio.debug.containerType.library.source&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&amp;lt;childContainerEntry childMemento=&amp;quot;&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot; standalone=&amp;amp;quot;no&amp;amp;quot;?&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;amp;lt;project name=&amp;amp;quot;PUMA260&amp;amp;quot; referencedProjects=&amp;amp;quot;true&amp;amp;quot;/&amp;amp;gt;&amp;amp;#13;&amp;amp;#10;&amp;quot; childType=&amp;quot;org.eclipse.debug.core.containerType.project&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&amp;lt;/cpuSpecificContainer&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;com.ti.ccstudio.debug.containerType.cpu.specific&quot;/&gt;&#13;&#10;&lt;/sourceContainers&gt;&#13;&#10;&lt;/sourceLookupDirector&gt;&#13;&#10;"/>
</launchConfiguration>
6 changes: 0 additions & 6 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
eclipse.preferences.version=1
encoding//Debug/ControlLoop/subdir_rules.mk=UTF-8
encoding//Debug/ControlLoop/subdir_vars.mk=UTF-8
encoding//Debug/EIB/subdir_rules.mk=UTF-8
encoding//Debug/EIB/subdir_vars.mk=UTF-8
encoding//Debug/MotorDriver/subdir_rules.mk=UTF-8
encoding//Debug/MotorDriver/subdir_vars.mk=UTF-8
encoding//Debug/TaskScheduler/subdir_rules.mk=UTF-8
encoding//Debug/TaskScheduler/subdir_vars.mk=UTF-8
encoding//Debug/makefile=UTF-8
Expand Down
101 changes: 99 additions & 2 deletions TaskScheduler/TaskScheduler.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,113 @@
//Project includes
#include "TaskScheduler.h"

//Tivaware includes
#include <driverlib/sysctl.h>
#include <driverlib/timer.h>
#include <inc/hw_ints.h>
#include <driverlib/interrupt.h>

#ifndef NULL
#define NULL 0
#endif

static volatile TaskScheduler scheduler;

void InitializeTaskScheduler(uint32_t timerBase){
/*
* Timer ISR for the task scheduler
*
* Updates each timer's task
* If the task is ready to be fired, then it is added to a queue and processed
*
* This timer must be given a low priority ISR because it is possible to have
* a heavy load to process
*/
void TaskSchedulerTimer_ISR(void){
//Loop through every task
Task *pTask = scheduler.pTaskListRoot;
while(pTask){
if(pTask->enabled){
pTask->ticks++;

if(pTask->ticks >= pTask->maxTicks){
pTask->ticks = 0;

//Add task to priority queue
//TODO: Implement priority queue
pTask->pCallback();
}
}
else{
//Do nothing
}

//Move to next task
pTask = pTask->pNextTask;
}

TimerIntClear(scheduler.timerBase, TIMER_TIMA_TIMEOUT);
}

void InitializeTaskScheduler(uint32_t timerBase, uint16_t sysClkFreq, uint32_t timerIntBase){
//Initialize the task scheduler
scheduler.timerBase = timerBase;
scheduler.pTaskListRoot = NULL;

//Initialize the timer
SysCtlPeripheralEnable(timerBase);

//Wait for the clock to stabilize
SysCtlDelay(10);

//Configure the timer to be a periodic 100us timer
TimerConfigure(timerBase, TIMER_CFG_PERIODIC);
TimerLoadSet(timerBase, TIMER_A, sysClkFreq * TASK_SCHEDULER_TIMER_PERIOD / 1000000);

//Configure the ISR
TimerIntRegister(timerBase, TIMER_BOTH, TaskSchedulerTimer_ISR);
IntEnable(timerIntBase);
TimerIntEnable(timerBase, TIMER_TIMA_TIMEOUT);

//Enable the timer and interrupts
TimerEnable(timerBase, TIMER_A);
IntMasterEnable();
}

void AddTask(Task *pTask){
//Reset the task and calculate the max ticks
pTask->ticks = 0;
pTask->maxTicks = pTask->period * TASK_SCHEDULER_TICKS_IN_ONE_SECOND;

//Add the task to the list
pTask->pNextTask = scheduler.pTaskListRoot;
scheduler.pTaskListRoot = pTask;
}

//Only remove the task if you must
//It is recommended to just disable the task
void RemoveTask(Task *pTask){
Task *pTaskRoot = scheduler.pTaskListRoot;

//Loop until we are pointing to the task before the one to be removed
//Or no task was found
bool taskFound = false;
while(pTaskRoot->pNextTask){
if(pTaskRoot->pNextTask == pTask){
taskFound = true;
break;
}
else{
//Keep searching
pTaskRoot = pTaskRoot->pNextTask;
}
}

if(taskFound){
//Remove the task from the list
pTaskRoot->pNextTask = pTask->pNextTask;
}
}
void DisableTask(Task *pTask){

void DisableTask(Task *pTask){
pTask->enabled = false;
}
16 changes: 12 additions & 4 deletions TaskScheduler/TaskScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
#include <stdint.h>
#include <stdbool.h>

//For the most accurate timer, choose a frequency that is an integer
#define TASK_SCHEDULER_TIMER_PERIOD 100 //us
#define TASK_SCHEDULER_TICKS_IN_ONE_SECOND 1/TASK_SCHEDULER_TIMER_PERIOD

struct Task_tag;
typedef struct Task_tag Task;

struct Task_tag {
uint32_t taskTimerTicks;
float taskPeriod;
uint32_t ticks;
uint32_t maxTicks;
float period;

uint8_t priority;
bool enabled;

void (*pCallback)(void);
bool taskEnabled;
Task *pNextTask;
};

Expand All @@ -20,7 +28,7 @@ typedef struct TaskScheduler_tag{
Task *pTaskListRoot;
} TaskScheduler;

void InitializeTaskScheduler(uint32_t timerBase);
void InitializeTaskScheduler(uint32_t timerBase, uint16_t sysClkFreq, uint32_t timerIntBase);

void AddTask(Task *pTask);
void RemoveTask(Task *pTask);
Expand Down
11 changes: 11 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//Project includes
#include "TaskScheduler/TaskScheduler.h"

//Standard includes
#include <stdbool.h>
#include <stdint.h>
Expand All @@ -6,6 +9,7 @@

//Tivaware includes
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
Expand Down Expand Up @@ -49,6 +53,13 @@ void EnableClock(void){
*/
void EnablePeripherals(void){
InitConsole();

InitializeTaskScheduler(TIMER0_BASE, SYS_CLK, INT_TIMER0A);

while(1){
UARTprintf("Hello world!");
SysCtlDelay(SYS_CLK / 3);
}
}

//Initializes UART0 to be used as a console.
Expand Down

0 comments on commit 85531ee

Please sign in to comment.