Presentation is loading. Please wait.

Presentation is loading. Please wait.

February 24, 2015 Jacob Beningo, CSDP

Similar presentations


Presentation on theme: "February 24, 2015 Jacob Beningo, CSDP"— Presentation transcript:

1 February 24, 2015 Jacob Beningo, CSDP
Baremetal C Programming for Embedded Systems Class 2: Baremetal Scheduling Techniques February 24, 2015 Jacob Beningo, CSDP

2 Course Overview C Concepts for Embedded Systems
Baremetal Scheduling Techniques Driver Design Techniques Design Patterns for Firmware Writing Portable Code

3 Session Overview Real-time Scheduling Theory vs Practice
Round Robin Scheduling Cooperative Scheduling Real-Time Operating Systems

4 Real-Time Scheduling What is meant by Real-Time?
What is the difference between Hard Real-time? Soft Real-time? Where is real-time scheduling required?

5 Rate Monotonic Scheduling
Task 4 Task 3 Task 2 Task 1 2 ms 4 ms 6 ms 8 ms 10 ms 12 ms 14 ms 16 ms

6 Round Robin Scheduling
void main (void) { System_Init(); while(1) // Run the first task Task1(); Task2(); Task3(); } return 1; Initialization Task 1 Task 2 Task 3

7 Round Robin w/ Interrupts
Initialization ISR Entry Task 1 Clear Flags Execute Code Task 2 ISR Exit Task 3

8 Cooperative Scheduler Operation
Run Task 1? Read System Time Task 1 Yes No Run Task 2? Task 2 No Yes

9 Cooperative Scheduler Flowchart
No Task < NumTasks? Read System Time Calculate Elapsed Time Yes Etime >= Task Interval ? No Yes Run Task

10 Cooperative Scheduler
int main(void) { static uint32_t tick = 0; // System tick static TaskType *Task_ptr; // Task pointer static uint8_t TaskIndex = 0; // Task index const uint8_t NumTasks = Task_GetNumTasks(); // Number of tasks /**************************************************************************** * System Initialization *****************************************************************************/ Task_ptr = Task_GetConfig(); // Get a pointer to the task configuration Sys_Init(); // Initializes the system and all peripherals }

11 Cooperative Scheduler
/** * Struct TaskType * TaskType structure is used to define the parameters required in order to * configure a task. */ typedef struct { uint16_t Interval; /**< Defines how often a task will run */ uint32_t LastTick; /**< Stores the last tick task was ran */ void (*Func)(void); /**< Function pointer to the task */ }TaskType;

12 Cooperative Scheduler
/** * Task configuration table. Holds the task interval, last time executed, and * the function to be executed. A continuous task is defined as a task with * an interval of 0. Last time executed is set to 0. */ static TaskType Tasks[] = { { INTERVAL_00MS, 0, Task }, { INTERVAL_10MS, 0, Task_10ms }, { INTERVAL_100MS, 0, Task_100ms }, }; * Defines the number of tasks that are currently scheduled to run. uint8_t Task_Number = sizeof(Tasks) / sizeof(*Tasks);

13 Cooperative Scheduler
TaskType *Task_GetConfig(void) { return Tasks; } uint8_t Task_GetNumTasks(void) return sizeof(Tasks) / sizeof(*Tasks); void Task (void) // Continuous Task Code void Task_10ms (void) // Code executed every 10ms

14 Cooperative Scheduler
// The main while loop. This while loop will run the program forever while(1) { tick = Tmr_GetSystemTick();// Get current system tick // Loop through all tasks. First, run all continuous tasks. Then, // if the number of ticks since the last time the task was run is // greater than or equal to the task interval, execute the task. for(TaskIndex = 0; TaskIndex < NumTasks; TaskIndex++) else if((tick - Task_ptr[TaskIndex].LastTick) >= Task_ptr[TaskIndex].Interval) (*Task_ptr[TaskIndex].Func)(); // Execute Task Task_ptr[TaskIndex].LastTick = tick; // Save last tick the task was ran. } }// end for }// end while(1)

15 RTOS Task execution controlled by priorities Pre-emption
Tasks have state behavior (Blocked, Running, Ready) Tasks have their own registers and stacks Pre-emption Priority Inversion Functions should be reentrant Semaphores Inner Task Communication Message Queues, Mailboxes and Pipes

16 Scheduling Summary Scheduler Complexity Response Time Change Stability
Prioritization Round-Robin 1 All Code Not Good None Round-Robin w/ Interrupts 3 All Code + ISR’s Not Good except for ISR’s ISR Driven Cooperative Scheduler 5 Longest Function + Scheduler Overhead + ISRs Good RTOS 8 Immediate + ISR’s Excellent Scheduler Driven w/ pre-emption

17 Additional Resources Download Course Material for
Updated C Doxygen Templates (Feb 2015) Example drivers source code Microcontroller API Standard EDN Embedded Basics Articles Embedded Bytes Newsletter From under - Blog and Articles > Software Techniques > CEC Baremetal C Programming

18 Jacob Beningo Newsletters P.O. Box 400 Embedded Bytes
Linden, Michigan 48451 Newsletters Embedded Bytes Training MicroPython Bootloaders Low Power Design Real-time Software C/C++ Embedded : : : Jacob_Beningo : Beningo Engineering : JacobBeningo : Embedded Basics Jacob Beningo Principal Consultant 18


Download ppt "February 24, 2015 Jacob Beningo, CSDP"

Similar presentations


Ads by Google