Download presentation
Presentation is loading. Please wait.
1
CET360 Microprocessor Engineering J. Sumey
5: Task Management CET360 Microprocessor Engineering J. Sumey
2
Smartcar Chaos by now, we've seen how complex the Smartcar project has become: line position data acquisition and processing steering control speed sensing throttle control battery monitoring UI: LCD, status LEDs, "go" button data comms plus, various tasks need to be done at different rates! CET360: Task Management
3
Observations modern embedded systems have become correspondingly more complex than in the "early years" why? with more capable MCUs at decreasing cost points comes the desire to perform more functions this trend does not appear to be letting up any time soon! CET360: Task Management
4
Solution look at multi-processing / multi-threading models from modern Operating Systems architecture CET360: Task Management
5
Variable Rate Tasks processing of various tasks at different rates
in order to optimize CPU utilization ex: it does not make sense to update steering servo output pulse width at any rate above servo update rate (i.e. 50 Hz) task organization & design documentation of all tasks to be performed, execution frequency/period, relative priority, constraints/conditions CET360: Task Management
6
Smartcar Task Design Task Frequency Priority Constraints
Line position processing 50 Hz Real-time Limited by steering servo update rate Steering control Speed sensing Variable Handled by ISR Throttle control Hz Limited by mechanical lag Battery monitoring 5 Hz Low Result determines LED color Data comms 3 Hz Medium Limited by comm link bit rate Status LED / LCD updates Suitable for human consumption Note: because of common goal and execution rate, line position processing can be absorbed into steering control task. CET360: Task Management
7
Implementation management of multiple tasks requires some form of processor scheduler either among multiple processes or within a single process (Smartcar project model) core component of modern Operating System (OS) design many embedded systems tend to not be complicated enough to warrant a full-blown OS but still need minimal task management capability CET360: Task Management
8
Embedded System Solution
enter: a task scheduler specifically: a variable rate, dynamic priority, cooperative, low overhead task scheduler variable rate: various tasks are executed at specified & differing periods dynamic priority: simultaneous tasks are ranked by assigned priority and executed accordingly cooperative: tasks are aware of and respect other tasks (thus: no CPU hogging!) low overhead: Coldfire instance requires approx. 2KB code for scheduler and 60 bytes RAM per managed task CET360: Task Management
9
Scheduler Components scheduler.c – actual code for scheduler ("implementation") scheduler.h – "interface" to scheduler functionality #define constants enumeration types (task priorities, time units) function prototypes of the form scheduler_xxx() replacement usDelay() and msDelay() functions CET360: Task Management
10
Scheduler Functions Function name† Description scheduler_init() Initializes scheduler for execution, must be called first scheduler_restart() Resets start time of scheduler scheduler_getStartTime() Returns time of scheduler start/restart scheduler_getUpTime() Returns amount of time scheduler has been "up" scheduler_getSystemLoad() Returns current percent of CPU utilization scheduler_createTask() Creates a new managed task scheduler_deleteTask() Deletes an existing task scheduler_getTaskId() Returns id of task given task name scheduler_getTaskPeriod() Returns a task's period in us. scheduler_setTaskPeriod() Sets a new period for a task in us. scheduler_getLatestPeriod() Returns actual period of latest execution of a task scheduler_setTaskEnabled() Enables/disables a task scheduler_run() Runs one iteration of the scheduler, must be called as often as possible † see scheduler.h for parameters & details, minimum needed functions in bold CET360: Task Management
11
Support Functions Function name Description usDelay()
Delays specified number of microseconds msDelay() Delays specified number of milliseconds CET360: Task Management
12
Typical Use CET360: Task Management task name name of check function
#include "scheduler.h" // add this to include section // initialization code: scheduler_init(); scheduler_createTask("taskname", NULL, taskHandler, 10000, TASK_PRIORITY_MEDIUM); task name name of check function name of task function desired execution period (us) relative task priority (see TaskPriority_e) // main loop: for (;;) { scheduler_run(); // can have other stuff here, but no long delays // no direct calls to taskHandler functions! } CET360: Task Management
13
Example Use† † taken from RGBschedulerTest.c CET360: Task Management
void task1() { TOGGLE(FB_RGB_RED); } void task2() { TOGGLE(FB_RGB_GREEN); void task3() { TOGGLE(FB_RGB_BLUE); void main() { // TPM1 initialization TPM1SC = TPM2SC_TOIE_MASK // enable interrupt on timer overflow + TPM1SC_CLKSA_MASK // select bus clock for timer clock + 4; // divby-16 prescaler (1MHz) EnableInterrupts; // enable interrupts into CPU scheduler_init(); scheduler_createTask("task1", NULL, task1, , TASK_PRIORITY_LOW); scheduler_createTask("task2", NULL, task2, , TASK_PRIORITY_HIGH); scheduler_createTask("task3", NULL, task3, , TASK_PRIORITY_MEDIUM); for (;;) scheduler_run(); } CET360: Task Management † taken from RGBschedulerTest.c
14
Coldfire Project Specifics
requires use of a free-running timer (i.e. no set modulo value) clocked at 1 MHz chosen timer number is set in scheduler.h look for "#define TIMING_TPMSELECT" ISR for timer overflow event is supplied in scheduler.c but requires user to enable timer overflow interrupts! (TOIE bit of TPMSC register) CET360: Task Management
15
Coldfire Project Application
to integrate scheduler into a Coldfire application developed in CodeWarrior: copy scheduler.h & scheduler.c into project Sources folder edit scheduler.h to select TPM to be used also verify value of MAXTASKS follow "note" comment at begin of scheduler.h add #include "scheduler.h" to end of #include section delete any existing usDelay() and msDelay() definitions use scheduler-provided version instead make sure selected timer overflow interrupt is enabled (TOIE) add scheduler_init() and "createTask" calls to initialization code task handler functions must be "void(void)" revise main program loop to call scheduler_run() very often CET360: Task Management
16
Smartcar Task Suggestions
taskSteering() – at 50 Hz taskSpeed() – at 100 Hz taskPeriodics() – at 5 Hz acquire new Pot a/d with smoothing Vbatt monitoring (with smoothing) updateLCD() taskDataComms() – at 3 Hz send 1 status packet CET360: Task Management
17
Troubleshooting No parameters into/out of task handler functions
but ok to call other functions from handlers with/without parameters Change usDelay, msDelay prototypes in LCD.h from unsigned short to plain int may need to remove scheduler_run() from inside of replacement WAITFOR() macro in scheduler.h? (credits to CET360 class of SP16) CET360: Task Management
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.