?? scheduler.h
字號:
/** * Copyright (c) 2006-2008 iWESUN (ShenZhen) Inf. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the AvrcX MTOS * * Author: Winter Hu <winter.hu@gmail.com> * Create: Nov 27, 2006 */#ifndef __SCHEDULER_H__#define __SCHEDULER_H__#include "common.h"#include "lnklist.h"#include "hardware.h"/* Use Timer0 system tick */#ifndef TICKRATE#define TICKRATE 1000#endif#define TCNT0_INIT (0xFF-(F_CPU/256/TICKRATE)) // 1ms/* __AVR__*/#define MINCONTEXT 35 // 32 registers, return address and SREG#define USER_MODE 0xFF#define MAX_WAIT_TICKS 250 // MUST less than 0xFF/* Process Status */#define TASK_READY 0x00 // Task is running or is ready in RunQueue#define TASK_BLOCK 0x01 // Task is blocked in BlockedQueue of semaphore#define TASK_DELAY 0x02 // Task is delay in TimerQueue#ifndef __ASSEMBLER__/* These only work in C program */typedef Node PID; // PID.pData-->PCBtypedef Node TID; // TID.pData-->TSTimertypedef struct TSTimer { PID* pid; __volatile__ unsigned short ticks;} TSTimer;// Define Process Control Blocktypedef struct ProcessControlBlock { __volatile__ unsigned char status; // Process Status Word __volatile__ unsigned char priority; // Predefined priority __volatile__ unsigned char tmslices; // Predefined time slice __volatile__ unsigned char reserved; void* context; // Process Context Pointer, i.e. Stack Pointer TSTimer timer; TID tid;} PCB;// Define Task Control Block which are stored in FLASH memorytypedef struct TaskControlBlock { void *stack; // Start of stack void (*start) (void); // Entry point of code PCB *pcb; // Pointer to the Process Control Block PID *pid; // Pointer to the Process Node unsigned char priority; // Predefined priority of task (0~255) unsigned char tmslices; // Predefined time slice of task (0~255)} FLASH const TCB;/** * A macro to ease the declaration of tasks definition. * Usage: TASKDEF(myFoo, 10, 16, 10) * * @param start, The name of the task function * @param n_stack, The bytes of this task maybe use * @param priority, The task priority * @param slice, The task time slice */#define TASKDEF(start, n_stack, priority, slice) \ CTASKFUNC(start) \ char start##_stack[n_stack + MINCONTEXT]; \ PCB start##_pcb; \ PID start##_pid; \ TCB start##_tcb = { \ &start##_stack[sizeof(start##_stack) - 1], \ start, \ &start##_pcb, \ &start##_pid, \ priority, \ slice \ }/** * Return the pointer to the task PID * * @param start, The name of the task */#define getPID(start) &start##_pid/** * Return the ponter to the task PCB * * @param start, The name of the task */#define getPCB(start) &start##_pcb/** * Return the pointer to the task TCB * * @param start, The name of the task */#define getTCB(start) &start##_tcb// Define Kernel Datatypedef struct KernelData{ LinkedList RunQueue; // The ready queue LinkedList TimerQueue; // The timer queue LinkedList *BlockedQueue; // The blocked queue PID *Running; // Current running process void *Context; // Kernel context __volatile__ unsigned char TmSlices; // Remaining time ticks of current process __volatile__ unsigned char SysLevel; // Re-entry counter into kernel context // user mode is 0xFF unsigned long SysTicks;} KernelData;extern KernelData kernel;/** * Initizlize Scheduler * This must be invoked before tasks schedulling */INTERFACE void init_scheduler(void);/** * Start scheduler * This method never return */INTERFACE void NAKED start(void);/** * Create the task PID of specified TCB * * @param TCB*, The pointer to the Task Control Block * @return PID*, The pointer to the Task PID * @see "scheduler.asm.S" */INTERFACE PID* create_task(TCB*);/** * Create and resume the task into RunQueue * * @param TCB*, The pointer to the Task Control Block */INTERFACE void run_task(TCB*);/** * Retrieves current running task * * @return PID*, the pointer to the current running task PID */INTERFACE PID* get_running(void);/** * Retreives system ticks * * @return unsigned long, The ticks from power on */INTERFACE unsigned long get_systicks(void);/** * Causes the currently executing task to temporarily pause and allow * other tasks to execute. */INTERFACE void NAKED yield(void);/** * Causes the currently executing task to temporarily pause in TimQueue * until delay expire. * * @param unsigned int, Ticks for sleeping */INTERFACE void NAKED sleep(unsigned int);/** * Swap the first PID from RunQueue to current env */void swapping(void);/** * Insert the PID into RunQueue * * @param PID*, The pointer to the PID */void resume_task(PID*);/** * Insert the PID into TimerQueue * * @param PID*, The pointer to the PID * @param unsigned int, The ticks to delay */void delay_task(PID*, unsigned int);/** * Pushes entire register context onto the current stack * SysLevel >= 0, running on kernel stack * * @see "scheduler.asm.S" */void NAKED prologue(void);/** * Restore previous context (kernel or user) * Ref. "scheduler.asm.S" * * ASSUMES: SysLevel >= 0, running on kernel stack */void NAKED epilogue(void);#endif /* !__ASSEMBLER__ *//* Kernel data structure offsets */#define KERN_RUNQUEUE 0 // Head of the ready queue#define KERN_TIMQUEUE 2 // Head of the timer queue#define KERN_BLKQUEUE 4 // Head of the blocked queue#define KERN_RUNNING 6 // Current running task#define KERN_CONTEXT 8 // Kernel stack SP#define KERN_TMSLICES 10 // Remaining time ticks of current task#define KERN_SYSLEVEL 11 // User mode or Kernel mode#define KERN_SYSTICKS 12 // A long integer of system tick counts/* Process Control Block data structure offsets */#define PCB_STATUS 0#define PCB_PRIORITY 1#define PCB_TMSLICES 2#define PCB_CONTEXT 4#define PCB_TIMER 6#define PCB_TIMER_TICK 8#endif /* __SCHEDULER_H__ */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -