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

📄 avos.c

📁 一个uCOS-II下的tcpip协议栈实现
💻 C
字号:
/*****************************************************************************
* avos.c - Accu-Vote Operating System code file.  This extends the uC/OS
*	real-time kernel.
*
* Copyright (c) 1997 by Global Election Systems Inc.
*
* The authors hereby grant permission to use, copy, modify, distribute,
* and license this software and its documentation for any purpose, provided
* that existing copyright notices are retained in all copies and that this
* notice and the following disclaimer are included verbatim in any 
* distributions. No written agreement, license, or royalty fee is required
* for any of the authorized uses.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *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 CONTRIBUTORS 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.
*
******************************************************************************
* REVISION HISTORY
*
* Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
*	97-02-12  Modified from startup.c.
*****************************************************************************/

#include "typedefs.h"
#include <string.h>
#include "v25.h"
#include "avconfig.h"
#include "stdio.h"		// Need sprintf()
#include "netbuf.h"		// required by devio.h
#include "devio.h"		// Required by lcddev.h
#include "lcddev.h"		// used by isrDisplay()
#include "timer.h"
#include "avos.h"


/*****************************/
/*** LOCAL DATA STRUCTURES ***/
/*****************************/
static void (*avShutdown)(void) = NULL;


/**********************************/
/*** LOCAL FUNCTION DEFINITIONS ***/
/**********************************/
/*
 * Interrupt handler for the Accu-Vote's OS clock.  Currently this services
 *	the timer service, the uC/OS time tick, and the pseudo interrupts.
 */
static void interrupt tickISR(void)
{
	OSIntEnter();
	timerCheck();
	OSTimeTick();
	fint();
	OSIntExit();
}

/*
 * Start the Accu-Vote's OS clock timer.  This is known as a 'Jiffy'
 *	timer and the clock ticks are known as 'Jiffies'.
 */
static void startJiffy(void)
{
	OS_ENTER_CRITICAL();
	setvect(INTR_TU2, tickISR);
	MD1 = HZCNTDOWN;
	TMIC2 &= ~0xC0;		/* what does this mean: sould be set? */
	TMC1 = 0xC0;		/* start timer at FCLK/128 */
	OS_EXIT_CRITICAL();
}


/***********************************/
/*** PUBLIC FUNCTION DEFINITIONS ***/
/***********************************/
void avosInit(void (*shutdown)())
{
	avShutdown = shutdown;
	startJiffy();
}


/*
 * Sleep for n seconds;
 */
void sleep(UINT n)
{
	OSTimeDly(n * HZ);
}

/*
 * Sleep ms milliseconds.  Note that this only has a (close to) 1 Jiffy 
 *	resolution.  Use msleep[01] if you need better than this.
 * Note: Since there may me less than a ms left before the next clock
 * 	tick, 1 tick is added to ensure we delay at least ms time.
 */
void msleep(UINT ms)
{
	OSTimeDly((ms / MSPERTICK) + 1);
}

/*
 * Return the milliseconds since power up.  We base this on the number of 
 *	Jiffies that have passed plus the remaining time on the Jiffy timer.
 */
ULONG mtime(void)
{
	UINT count;
	ULONG time;

	OS_ENTER_CRITICAL();
	count = TM1;
	/* Get the current OSTime and check. If an interrupt is pending then add 1
		to the OSTime */
	time = OSTime + ((TMIC2 & 0x80) ? 1UL : 0UL);
	OS_EXIT_CRITICAL();
	return (time * MSPERTICK) + (HZCNTDOWN - (count/(FDIVCLK/1000)));
}

/*
 * Return the difference between t and the current system elapsed
 *	time in milliseconds.
 */
LONG diffTime(ULONG t)
{
	return t - mtime();
}


/*
 * Return the time in Jiffy timer ticks since power up.
 */
ULONG jiffyTime(void)
{
	ULONG time;
	
	OS_ENTER_CRITICAL();
	
	/* Get the current OSTime and if an interrupt is pending then add 1. */
	time = OSTime + ((TMIC2 & 0x80) ? 1UL : 0UL);
	OS_EXIT_CRITICAL();
	
	return time;
}

/*
 * Return the difference between t and the current system elapsed
 *	time in Jiffy timer ticks.  Positive values indicate that t
 *	is in the future, negative that t is in the past.
 */
LONG diffJTime(ULONG t)
{
	return t - jiffyTime();
}


/*
 * Halt the system.  If a shutdown function is registered, it is invoked
 *	to turn off devices before we lock up.
 * Note that this disables task switching but does not disable interrupt
 *	handling (thus the debugger will still function).
 */
void HALT(void)
{
	OSSchedLock();			// Disable task switching.
	if (avShutdown)
		(*avShutdown)();	// Shut down devices.
	for (;;)				// And loop here forever.
		;
	/* Not reached */
}

/* Display a panic message and HALT the system. */
void panic(char *msg)
{
	char errStr[50];
	
	OSSchedLock();
	asm STI;
    
    sprintf(errStr, MSPANIC, msg);
	lcdRawWrite(errStr);
	HALT();
	/* Not reached */
}


/*
 * Display the interrupt service routine number and the address at the time
 *	of interrupt and halt.  This is normally called for an unexpected interrupt.
 */
void isrDisplay(INT isrnum, INT ps, INT pc)
{
	char buffer[LCDBUFSZ + 1];
	
	OSSchedLock();
	sprintf(buffer, "ISR%-2d: %4.4X:%4.4X\n%s", isrnum, ps, pc, MSCALFORSERV);
	lcdRawWrite(buffer);
	HALT();
	/* not reached */	
}



⌨️ 快捷键说明

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