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

📄 serial_read.c

📁 zilog z80f91串口通用函数
💻 C
字号:
 /*
 * Copyright 2001, Metro Link, Inc.
 * All Rights Reserved
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Metro Link, Inc. and may
 * contain proprietary, confidential and trade secret information of
 * Metro Link, Inc. and/or its partners.
 *  
 * The contents of this file may not be disclosed to third parties, copied or
 * duplicated in any form, in whole or in part, without the prior written
 * permission of Metro Link, Inc.
 *
 */
/*
 * File			:	serial_read.c
 *
 * Description	:	Contains implementation of serial read function.
 *					
 * Copyright 2003, ZiLOG Inc.
 * All Rights Reserved
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ZiLOG Inc., and might
 * contain proprietary, confidential and trade secret information of
 * ZiLOG, our partners and parties from which this code has been licensed.
 * 
 * The contents of this file may not be disclosed to third parties, copied or
 * duplicated in any form, in whole or in part, without the prior written
 * permission of ZiLOG Inc.
 */ 

#ifdef _IAR_CODE

#ifdef _EZ80F91
#include <ioez80f91.h>
#endif

#ifdef _EZ80F92
#include <ioez80f92.h>
#endif

#ifdef _EZ80F93
#include <ioez80f92.h>
#endif

#ifdef _EZ80L92
#include <ioez80l92.h>
#endif

#ifdef _EZ80190
#include <ioez80190.h>
#endif

#endif

#include <stdio.h>
#include "serial.h"
#include "_ez80.h"
#include "ZTypes.h"
#include "ZSysgen.h"
#include "ZDevice.h"
#include "ZThread.h"
#include "ZSemaphore.h"
#include "ZInterrupt.h"



DDF_STATUS_t
UARTRead
(
	RZK_DEVICE_CB_t *				pDev,
	INT8 *							pBuf,
	RZK_DEV_BYTES_t					Len
)
{
	UART_DVIOBLK *					pDevBlk   = (UART_DVIOBLK *) pDev->dvinputoutput;
	UINT8							uart_base = pDevBlk->UARTbase;
//	UINT8							do_sync   = pDevBlk->serparam->settings & SERSET_SYNC;  // Commented during IAR Porting
	RZK_SEMAPHOREHANDLE_t			RxSem     = pDevBlk->RxSem;
	UINT8 *							pRxBuf    = pDevBlk->pRxBuf;
	UINT16							BytesRead = 0;
	UINT16 							ReadCount = Len;
	UINT16							Avail;
	UINT8 *							pData;
	UINT8 *							pPastBuf;
	UINTRMASK						IntMask ;


	if( pDevBlk->State != SERIAL_STATE_OPENED )
	{
		return UARTDEV_ERR_INVALID_OPERATION ;
	}

	RZKThreadLockForDelete();
	pPastBuf = pRxBuf + pDevBlk->RxBufSize;
	pData = pDevBlk->pReadBuf;

	 RZKAcquireSemaphore(RxSem, INFINITE_SUSPEND) ;

	while( Len )
	{
		if( pDevBlk->State != SERIAL_STATE_OPENED )
		{			
			RZKReleaseSemaphore(pDevBlk->RxSem) ;
			RZKThreadUnLockForDelete();
			return UARTDEV_ERR_INVALID_OPERATION ;
		}

		IntMask = RZKDisableInterrupts();
		Avail = pDevBlk->RxAvail;
		RZKEnableInterrupts(IntMask);
		
		if( Avail )
		{
			BytesRead = 0;
			if( Avail > Len )
			{
				Avail = Len;
			}
			while( Avail-- )
			{
				*pBuf++ = *pData++;
				if( pData == pPastBuf )
				{
					pData = pRxBuf;
				}
				BytesRead++;
			}
			Len -= BytesRead;
			IntMask = RZKDisableInterrupts();
			pDevBlk->RxAvail -= BytesRead;
			pDevBlk->pReadBuf = pData;
			RZKEnableInterrupts(IntMask);
		}
		else
		{
			IntMask = RZKDisableInterrupts();
			pDevBlk->pRxTask = RZKGetCurrentThread() ;
			
//			RZKSuspendThread(RZKGetCurrentThread(), INFINITE_SUSPEND);
			RZKSuspendInterruptThread();
			RZKEnableInterrupts(IntMask);
			if(pDevBlk->State == SERIAL_STATE_CLOSED)
			{
				RZKReleaseSemaphore( pDevBlk->RxSem );
				RZKThreadUnLockForDelete();
				return (DDFERR_DEVICE_CLOSED);
			}
		}
	}

	/*
	 * Flow back on once the receive buffer is close to empty.
	 */
	if( pDevBlk->serparam->settings & SERSET_RTSCTS )
	{
		IntMask = RZKDisableInterrupts();
		Avail = pDevBlk->RxAvail;
		RZKEnableInterrupts(IntMask);
		if( Avail <= FLOW_ON_LEVEL )
		{
			UART_MCTL(uart_base) |= MCTL_RTS;
		}
	}

	RZKReleaseSemaphore( RxSem );
	RZKThreadUnLockForDelete();
	return( ReadCount );
}



⌨️ 快捷键说明

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