📄 devmng.c
字号:
/*********************************************************
* Copyright (C) Asic Center. 2001
* All Rights Reserved
*
* Filename : systask.c
* Function : System Task
* Revision :
* 2002/3/20 Pessia Modify design of Device Manage
*********************************************************/
#include <kernel\ros33\ros33.h>
#include <sys\devmng.h>
#include <sys\syserr.h>
#include <sys\systsk.h>
#include <sys\sysusr.h>
#ifdef SIM_ON_PC
#include <simdrv\include\uart.h>
#else
#include <hardware\drball\uart.h>
#endif
/*
#define UART0_OWN_SEM 0
#define UART0_WRITE_SEM 0
#define UART0_WBUF_EVENT 0
#define UART0_READ_SEM 0
#define UART0_RBUF_EVENT 0
#define UART1_OWN_SEM 1
#define UART1_WRITE_SEM 1
#define UART1_WBUF_EVENT 1
#define UART1_READ_SEM 1
#define UART1_RBUF_EVENT 1
*/
/* Global Device Use Table */
DEVUSETBL gDevUseTbl[DEVNUM]={
/* name, ownerid, semid_own, semid_write, flgid_wbuf, semid_read, flgid_rbuf data_proc*/
{ "UART0", 0, UART0_OWN_SEM, UART0_WRITE_SEM, UART0_WBUF_EVENT, UART0_READ_SEM, UART0_RBUF_EVENT, NULL},
{ "UART1", 0, UART1_OWN_SEM, UART1_WRITE_SEM, UART1_WBUF_EVENT, UART1_READ_SEM, UART1_RBUF_EVENT, NULL}
};
VOID SysDevTableInit(VOID)
{
INT i;
for(i=0;i<DEVNUM;i++)
gDevUseTbl[i].ownerid=0;
}
STATUS SysOpenDev(ID devid, INT timeout)
{
// ID ownerid;
ID tskid;
// INT err;
DEVUSETBL *dev;
/* Check time value */
if(timeout<-1)
return SYS_PAR;
/* Check device id */
if(devid > DEVNUM || devid ==0)
return SYS_PAR;
get_tid(&tskid);
/* Only user task is allowed to call this function */
if(tskid < SHELL_ID)
return SYS_ID;
dev = &gDevUseTbl[devid-1];
dis_dsp(); // enter critical
if( dev->ownerid != 0 ) // device is in use
{
if( dev->ownerid == tskid) // current task has owned this device
{
ena_dsp(); // leave critical
return SYS_OK;
}
else // device is used by another task
{
ena_dsp(); // leave critical
return SYS_PAR;
}
}
else // device is assigned to the current task
dev->ownerid = tskid;
ena_dsp(); // leave critical
/* If this device is owned by another task, wait for
* semaphore (semid_own). If return value of
* twai_sem is not zero, return SYS_ERR(FAIL TO OPEN).
* If this device is free(ownerid == 0),
* clear semaphore without waiting,
* because all semaphore is initialed to 1.
*/
// err = twai_sem(gDevUseTbl[devid-1].semid_own,
// timeout);
// if(err)
// return SYS_ERR;
//gSysTcbTbl[tskid-1].devices |= devid;
return SYS_OK;
}
STATUS SysCloseDev(ID devid)
{
// ID ownerid;
ID tskid;
DEVUSETBL *dev;
/* Check device id */
if(devid > DEVNUM || devid ==0)
return SYS_PAR;//error device id
get_tid(&tskid);
/* Only user task is allowed to call this function */
if(tskid < SHELL_ID)
return SYS_ID;
dev = &gDevUseTbl[devid-1];
dis_dsp(); // enter critical
if( dev->ownerid != 0 ) // device is in use
{
if( dev->ownerid == tskid ) // current task owned this device
{
dev->ownerid = 0;
// sig_sem(gDevUseTbl[devid-1].semid_own);
ena_dsp(); // leave critical
return SYS_OK;
}
else // device is used by another task
{
ena_dsp(); // leave critical
return SYS_PAR;
}
}
else // device is free
{
ena_dsp(); // leave critical
return SYS_OK;
}
}
INT SysWriteDev(ID devid, UCHAR* pData, INT byteCounts, INT timeout)
{
ID tskid;
INT err;
INT written_count;
ID semid_write;
DEVUSETBL *dev;
/* Check time value */
if(timeout<-1)
return SYS_PAR;
/* Check device id */
if(devid > DEVNUM || devid ==0)
return SYS_PAR;
if(pData == NULL || byteCounts<=0)
return SYS_PAR;
get_tid(&tskid);
dev = &gDevUseTbl[devid-1];
/* If this is user task, check whether the device is belong to it */
dis_dsp(); // enter critical
if( tskid >= SHELL_ID)
{
if( dev->ownerid == 0 ) // device is free
dev->ownerid = tskid;
else // device is in use
if( dev->ownerid != tskid) // device is used by another task
{
ena_dsp(); // leave critical
return SYS_ID;
}
}
ena_dsp(); // leave critical
/* Wait for semaphore to write.
* Because system-level task always have permittion to
* read/write device. There may be more than one task
* which calls this function at the same time. So, we use
* a semapore ( semid_write ) to make synchronization.
*/
semid_write = dev->semid_write;
err = twai_sem( semid_write, timeout );
if( !err )
{
switch( devid )
{
case UART0_ID:
written_count = write_0_SendBuf( pData, byteCounts, timeout );
//written_count = 0;
break;
case UART1_ID:
written_count = write_1_SendBuf( pData, byteCounts, timeout );
//written_count = 0;
break;
default:
break;
}
sig_sem( semid_write );
}else
/* If wait-timeout, return error */
return SYS_ERR;
/* Return counts of data having been written into buffer */
return written_count;
}
INT SysReadDev(ID devid, UCHAR* pData, INT byteCounts, INT timeout)
{
ID tskid;
INT err;
INT read_count;
ID semid_read;
DEVUSETBL *dev;
/* Check time value */
if(timeout<-1)
return SYS_PAR;
/* Check device id */
if(devid > DEVNUM || devid ==0)
return SYS_PAR;
if(pData == NULL || byteCounts<=0)
return SYS_PAR;
get_tid(&tskid);
/* If this is user task, check whether the device is belong to it */
dev = &gDevUseTbl[devid-1];
dis_dsp(); // enter critical
if( tskid >= SHELL_ID)
{
if( dev->ownerid == 0 ) // device is free
dev->ownerid = tskid;
else // device is in use
if( dev->ownerid != tskid) // device is used by another task
{
ena_dsp(); // leave critical
return SYS_ID;
}
}
ena_dsp(); // leave critical
/* Wait for semaphore to read.
* Because system-level task always have permittion to
* read/write device. There may be more than one task
* which calls this function at the same time. So, we use
* a semapore ( semid_write ) to make synchronization.
*/
semid_read = dev->semid_read;
err = twai_sem(semid_read, timeout);
if(!err)
{
switch(devid)
{
case UART0_ID:
read_count = read_0_ReceiveBuf(pData, byteCounts, timeout);
//read_count =0;
break;
case UART1_ID:
read_count = read_1_ReceiveBuf(pData, byteCounts, timeout);
//read_count =0;
break;
default:
break;
}
sig_sem(semid_read);
}else
/* If wait-timeout, return error */
return SYS_ERR;
/* Return counts of data having been read from buffer */
return read_count;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -