⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_cpu.c

📁 给出一个简单操作系统在AVR中应用实例
💻 C
字号:
/*
*********************************************************************************************************
*                                      minOS 0.01v
*                                The Real-Time OS Kernel
*                                  All Rights Reserved
* File : OS_CPU.C
* 作者 : 21icbbs网友 LM7556 ,2004年7月   by : LM7556 , China , 2004-2004 
*
*********************************************************************************************************
*/


#define OS_CPU_GLOBALS
#include "OS_Includes.h"

//#pragma interrupt_handler OSTickISR:8  //uses Timer0 for time ticks.

//;CPU specific definitions 
#define C51_CRYSTAL_FREQ   4000000ul
//;Determine the reload values for timer 0 this is automated by the next macro
#define T0_RELOAD  C51_CRYSTAL_FREQ / OS_TICKS_PER_SEC / 1024
#define T0_TCNT0 (256 - (T0_RELOAD & 0xff))
//Initial OS Timer for time ticks --- 初始化时间节拍定时器。
void InitOSTimer(void)
{
  TIMSK |= (1<<1);     // set T0IE0
  TCCR0  = 0x05;       // CTC0=CK/1024
  TCNT0  = T0_TCNT0;   // (256 - (T0_RELOAD & 0xff))
  SREG  |= (1<<7);     // SEI
}

//建立一个任务(Create a Task)。
//Inport :  tFunc --- function address , tStk --- stack bottom , prio --- Priority .
void OSTaskCreate (void (*tFunc)(void) , OS_STK *tStk, INT8U prio) 
{
 	OSTCB[prio].OSTaskStatus = OS_TASK_Rdy;
	OSTCB[prio].OSStkTop = (INT16U)tStk-1 /* Initial value when main was called             */
                          -12             /* reserve for to save R0-R5 , R26-R31 registers  */
                          -sizeof(INT16U) /* The PC value to be loaded                      */
					   ;
	*--tStk	=  (INT16U const)tFunc & 0xff;   /* Save low byte of task function address   */
	*--tStk	=  (INT16U const)tFunc / 0x100;  /* Save high byte of task function address  */
}

static unsigned int SaveSP; //用于保存需恢复的SP,在OSTickISR或用户中断结束做任务切换时,恢复SP.

//context switch interrupt --- 软中断任务切换
void OSCtxSw(void) 
{
//asm("in %0, %1" : "=r" (value) : "I" (PORTD) : );

	asm("PUSH R31");
	asm("PUSH R30");
	asm("PUSH R29");	
	asm("PUSH R28");
	asm("PUSH R27");
	asm("PUSH R26");

	asm("PUSH R0");
	asm("PUSH R1");
	asm("PUSH R2");	
	asm("PUSH R3");
	asm("PUSH R4");
	asm("PUSH R5");	

	asm("IN R31,0x3d");
	asm("STS SaveSP,R31");
	asm("IN R31,0x3e");	
	asm("STS SaveSP+1,R31");	

	OSTCB[OS_TASK_CrtPrio].OSStkTop = SaveSP; //Save the current task top.

	OS_TASK_CrtPrio=OS_TASK_HighPri;

// Load context from the stack
    SaveSP = OSTCB[OS_TASK_CrtPrio].OSStkTop;	//load the high priority task task top.

	asm("CLI");					
	asm("LDS R31,SaveSP"); 
	asm("OUT 0x3d,R31");
	asm("LDS R31,SaveSP+1"); 
	asm("OUT 0x3e,R31");
	asm("SEI");
	
    asm("POP R5");
    asm("POP R4");
    asm("POP R3");
    asm("POP R2");
    asm("POP R1");
    asm("POP R0");
		
	asm("POP R26");
	asm("POP R27");
    asm("POP R28");
    asm("POP R29");
    asm("POP R30");
    asm("POP R31");

}

//OS Time Tick Interrupt,OS 时钟节拍中断.
INTERRUPT(SIG_OVERFLOW0)
{
    TCNT0  = T0_TCNT0;   // (256 - (T0_RELOAD & 0xff))
#if OS_TICK_HOOK_EN>0
//	AppTickHook();     //Hook file for a time tick --- 用户每个时间节拍的钩子函数

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -