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

📄 port_m8.c

📁 TinyWorks操作系统。 每个任务占6~10个字节的RAM空间
💻 C
字号:
#include <avr/io.h>
#include <avr/interrupt.h>
#include "..\..\TinyWorks.h"

void TimerHook( void );

#define SAVE_CONTEXT()									\
	asm volatile (	"push	r0						\n\t"	\
					"in		r0, __SREG__			\n\t"	\
					"cli							\n\t"	\
					"push	r0						\n\t"	\
					"push	r1						\n\t"	\
					"clr	r1						\n\t"	\
					"push	r2						\n\t"	\
					"push	r3						\n\t"	\
					"push	r4						\n\t"	\
					"push	r5						\n\t"	\
					"push	r6						\n\t"	\
					"push	r7						\n\t"	\
					"push	r8						\n\t"	\
					"push	r9						\n\t"	\
					"push	r10						\n\t"	\
					"push	r11						\n\t"	\
					"push	r12						\n\t"	\
					"push	r13						\n\t"	\
					"push	r14						\n\t"	\
					"push	r15						\n\t"	\
					"push	r16						\n\t"	\
					"push	r17						\n\t"	\
					"push	r18						\n\t"	\
					"push	r19						\n\t"	\
					"push	r20						\n\t"	\
					"push	r21						\n\t"	\
					"push	r22						\n\t"	\
					"push	r23						\n\t"	\
					"push	r24						\n\t"	\
					"push	r25						\n\t"	\
					"push	r26						\n\t"	\
					"push	r27						\n\t"	\
					"push	r28						\n\t"	\
					"push	r29						\n\t"	\
					"push	r30						\n\t"	\
					"push	r31						\n\t"	\
					"lds	r26, pCurrent		\n\t"	\
					"lds	r27, pCurrent + 1	\n\t"	\
					"in		r0, 0x3d				\n\t"	\
					"st		x+, r0					\n\t"	\
					"in		r0, 0x3e				\n\t"	\
					"st		x+, r0					\n\t"	\
				);


#define RESTORE_CONTEXT()								\
	asm volatile (	"lds	r26, pNew		\n\t"	\
					"lds	r27, pNew + 1	\n\t"	\
					"ld		r28, x+					\n\t"	\
					"out	__SP_L__, r28			\n\t"	\
					"ld		r29, x+					\n\t"	\
					"out	__SP_H__, r29			\n\t"	\
					"pop	r31						\n\t"	\
					"pop	r30						\n\t"	\
					"pop	r29						\n\t"	\
					"pop	r28						\n\t"	\
					"pop	r27						\n\t"	\
					"pop	r26						\n\t"	\
					"pop	r25						\n\t"	\
					"pop	r24						\n\t"	\
					"pop	r23						\n\t"	\
					"pop	r22						\n\t"	\
					"pop	r21						\n\t"	\
					"pop	r20						\n\t"	\
					"pop	r19						\n\t"	\
					"pop	r18						\n\t"	\
					"pop	r17						\n\t"	\
					"pop	r16						\n\t"	\
					"pop	r15						\n\t"	\
					"pop	r14						\n\t"	\
					"pop	r13						\n\t"	\
					"pop	r12						\n\t"	\
					"pop	r11						\n\t"	\
					"pop	r10						\n\t"	\
					"pop	r9						\n\t"	\
					"pop	r8						\n\t"	\
					"pop	r7						\n\t"	\
					"pop	r6						\n\t"	\
					"pop	r5						\n\t"	\
					"pop	r4						\n\t"	\
					"pop	r3						\n\t"	\
					"pop	r2						\n\t"	\
					"pop	r1						\n\t"	\
					"pop	r0						\n\t"	\
					"out	__SREG__, r0			\n\t"	\
					"pop	r0						\n\t"	\
				);

void Stk_Init(TW_TCB *Tcb, CFUNC pTask, Stk_t *StkTop )
{
    Tcb->BreakPoint = StkTop;
    *Tcb->BreakPoint = (Stk_t)(((u16)pTask)&0xff);
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = (Stk_t)(((u16)pTask)>>8); //  任务起始地址
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x11;     //  R0
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x80;     //  SREG
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x00;     //  R1
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x22;     //  R2
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x33;     //  R3
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x44;     //  R4
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x55;     //  R5
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x66;     //  R6
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x77;     //  R7
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x88;     //  R8
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x99;     //  R9
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xaa;     //  R10
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xbb;     //  R11
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xcc;     //  R12
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xdd;     //  R13
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xee;     //  R14
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xff;     //  R15
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x11;     //  R16
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x22;     //  R17
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x33;     //  R18
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x44;     //  R19
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x55;     //  R20
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x66;     //  R21
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x77;     //  R22
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x88;     //  R23
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x99;     //  R24
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xaa;     //  R25
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xbb;     //  R26
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xcc;     //  R27
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xdd;     //  R28
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xee;     //  R29
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0xff;     //  R30
    Tcb->BreakPoint --;
    *Tcb->BreakPoint = 0x00;     //  R31
    Tcb->BreakPoint --;
}

void TWContext( void )
{
    SAVE_CONTEXT();
    pCurrent = pNew;
    RESTORE_CONTEXT();
}

void TWStart( void )
{
    TWSched();
    pCurrent = pNew;
    RESTORE_CONTEXT();
}

void timer0_init( void )
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0x06; //set count
 TCCR0 = 0x03; //start timer
}

ISR(TIMER0_OVF_vect)
{
    TCNT0 = 0x06; //reload counter value
    TimerHook();
    TWTimerTick();
    TWSched();
    TWContext();
}

⌨️ 快捷键说明

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