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

📄 sys.c

📁 AT91所有开发板的资料 AT91所有开发板的资料
💻 C
📖 第 1 页 / 共 3 页
字号:
{    unsigned int msglen;    unsigned int nbytes;    p_Buffer pbuff, rbuff;    unsigned int bufp=0;    word status;    int expectedReason;    unsigned int lenToSend;#define SYS_PROTOCOL_OVERHEAD (4*4)#define MAX_DATA_IN_WRITE  (Angel_ChanBuffSize - SYS_PROTOCOL_OVERHEAD - 3*4)#define MAX_DATA_IN_WRITEX (Angel_ChanBuffSize - SYS_PROTOCOL_OVERHEAD - 4)    pbuff = angel_ChannelAllocBuffer(Angel_ChanBuffSize);    if (pbuff!=NULL) {        lenToSend = len > MAX_DATA_IN_WRITE ? MAX_DATA_IN_WRITE : len;        expectedReason =  CL_Write | HtoT;        msglen = msgbuild(BUFFERDATA(pbuff), "%w%w%w%w%w%w%w", CL_Write |TtoH,                          0, ADP_HandleUnknown, ADP_HandleUnknown, fh, len,                          lenToSend);        LogInfo(LOG_SYS, ("sys_write: total %d this %d\n", len, lenToSend));        do {            MEMCPY_FROM_TARGET(BUFFERDATA(pbuff)+msglen, &buf[bufp], lenToSend);            bufp += lenToSend;            len  -= lenToSend;            if (_sys_do_transaction(pbuff, msglen+lenToSend,                                    &rbuff, expectedReason, &status) )                return -1;            nbytes = GET32LE(BUFFERDATA(rbuff)+20);            angel_ChannelReleaseBuffer(rbuff);            if (status)                return status;            if (len > 0) {                pbuff = angel_ChannelAllocBuffer(Angel_ChanBuffSize);                if (pbuff == NULL)                    return -1;                lenToSend = len > MAX_DATA_IN_WRITEX ? MAX_DATA_IN_WRITEX : len;                expectedReason =  CL_WriteX | HtoT;                msglen = msgbuild(BUFFERDATA(pbuff), "%w%w%w%w%w",                                  CL_WriteX |TtoH, 0, ADP_HandleUnknown,                                  ADP_HandleUnknown,  lenToSend);                LogInfo(LOG_SYS, ("sys_write: CL_WriteX: %d of %d\n", lenToSend, len));            }        } while (len > 0);        return nbytes;    }    else return -1;}/* ReadC reads a byte from the debugger console */static int _sys_readc(void){    int status;    p_Buffer rbuff;    int ret_code;    if (_sys_build_and_transact(&rbuff, CL_ReadC|HtoT, (word *)&status,                                "%w%w%w%w", CL_ReadC|TtoH,                                0, ADP_HandleUnknown, ADP_HandleUnknown))    {        LogInfo(LOG_SYS, ("sys_read: Comms failure\n"));        return -1;    }    else    {        if (status)            ret_code = status;        else            ret_code = GET8(BUFFERDATA(rbuff)+20);        LogInfo(LOG_SYS, ("sys_readc: status %d\n", status));        angel_ChannelReleaseBuffer(rbuff);        return ret_code;    }}/* Read 'len' characters of 'buf' from file associated with 'fh' in 'mode'.   Returns the number of characters NOT read, i.e. 0==NoError.              *//* Note: Assumes that buf is large enough to hold the number of bytes read */static int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len,                     int mode){    unsigned int nread , nbtotal,nbmore, nbytes, status, notread;    p_Buffer rbuff;    IGNORE(mode);    if (_sys_build_and_transact(&rbuff, CL_Read|HtoT, &status,                                "%w%w%w%w%w%w", CL_Read|TtoH,                                0, ADP_HandleUnknown, ADP_HandleUnknown,                                fh, len))    {        LogInfo(LOG_SYS, ("sys_read: Comms failure\n"));        return -1;    }    else {        unpack_message(BUFFERDATA(rbuff)+20, "%w%w", &nbytes, &nbmore);        LogInfo(LOG_SYS, ("sys_read: CL_Read: status %d, nbytes %d. nbmore %d\n",                          status, nbytes, nbmore));        nbtotal = nbytes+nbmore;        notread = len - nbytes;        nread = nbytes;        if (status == 0)            MEMCPY_TO_TARGET(buf, BUFFERDATA(rbuff)+28, nbytes);        angel_ChannelReleaseBuffer(rbuff);        if (status)            return (notread|0x80000000);        while(nread<nbtotal)        {            LogInfo(LOG_SYS, ("sys_read: CL_ReadX (%d / %d)\n", nread, nbtotal));            if (_sys_build_and_transact(&rbuff, CL_ReadX|HtoT, &status,                                        "%w%w%w%w", CL_ReadX|TtoH,                                        0, ADP_HandleUnknown, ADP_HandleUnknown))            {                LogInfo(LOG_SYS, ("sys_read: Comms failure\n"));                return -1;            }            else {                unpack_message(BUFFERDATA(rbuff)+20, "%w%w",  &nbytes, &nbmore);                notread = nbmore;                if (status == 0)                    MEMCPY_TO_TARGET(buf+nread, BUFFERDATA(rbuff)+28, nbytes);                nread += nbytes;                angel_ChannelReleaseBuffer(rbuff);                if (status)                    return (notread|0x80000000);            }        }        LogInfo(LOG_SYS, ("sys_read: returning notread: %d\n", notread));        /* Return number of bytes unread */        return notread;    }}/* Return TRUE if status value indicates an error. */static int _sys_iserror(int status){    LogInfo(LOG_SYS, ("sys_iserror: status: %d\n", status));    if (status == -1)        return TRUE;    else        return FALSE;}/* Returns non-zero if the file is connected to an interactive device. */static int _sys_istty(FILEHANDLE fh){    int status;    if (fh == -1)    {        LogInfo(LOG_SYS, ("sys_istty: not a handle: %d\n", fh));        return -1;    }        if (_sys_build_and_transact(NULL, CL_IsTTY|HtoT, (word *)&status,                                "%w%w%w%w%w", CL_IsTTY|TtoH,                                0, ADP_HandleUnknown, ADP_HandleUnknown, fh))    {        LogInfo(LOG_SYS, ("sys_istty: Comms failure\n"));        return -1;    }    else    {        LogInfo(LOG_SYS, ("sys_istty: status: %d\n", status));        return status;    }}/* Seeks to position 'pos' in the file associated with 'fh'.  Returns a   negative value if there is an error, otherwise >=0.                  */static int _sys_seek(FILEHANDLE fh, long pos){    int status;    if (_sys_build_and_transact(NULL, CL_Seek|HtoT, (word *)&status,                                "%w%w%w%w%w%w", CL_Seek |TtoH,                                0, ADP_HandleUnknown, ADP_HandleUnknown,                                fh, pos))    {        LogInfo(LOG_SYS, ("sys_istty: Comms failure\n"));        return -1;    }    else    {        LogInfo(LOG_SYS, ("sys_istty: status: %d\n", status));        return status;    }}/* Flushes any buffers associated with fh and ensures that the file is   up to date on the backing store medium.  The result is >=0 if OK,   negative for an error. */static int _sys_ensure(FILEHANDLE fh){    IGNORE(fh);    LogInfo(LOG_SYS, ("sys_ensure: not implemented, returning -1\n"));    return -1;}/* There is no CL_ packet for this call. returning -1 is the "old" behaviour. * The call should return the value of "errno" for the last SYS_ call which * was made. Sadly this is broken, as the numbers in question are host-specific. */static int _sys_errno(){    LogInfo(LOG_SYS, ("sys_errno: not implemented, returning -1\n"));    return -1;}/* Returns length of the file fh ( or a negative error indicator). */static long _sys_flen(FILEHANDLE fh){    long length;    if (_sys_build_and_transact(NULL, CL_Flen|HtoT, (word *)&length,                                "%w%w%w%w%w", CL_Flen |TtoH,                                0, ADP_HandleUnknown, ADP_HandleUnknown, fh))    {        LogInfo(LOG_SYS, ("sys_flen: Comms failure\n"));        return -1;    }    else    {        LogInfo(LOG_SYS, ("sys_flen: len: %d\n", length));        return length;    }}/* Returns the name for a temporary file number fileno in the buffer name *//* NOTE: header unclear leave atm */static int _sys_tmpnam(char *name, int sig, unsigned maxlen){    p_Buffer rbuff;    int status, namlen;    if (_sys_build_and_transact(&rbuff, CL_TmpNam|HtoT, (word *)&status,                                "%w%w%w%w%w%w", CL_TmpNam |TtoH,                                0, ADP_HandleUnknown, ADP_HandleUnknown,                                maxlen, sig))    {        LogInfo(LOG_SYS, ("sys_tmpnam: Comms failure\n"));        return -1;    }        if (status == 0) {        namlen = PREAD(LE,(word *)(BUFFERDATA(rbuff)+20));        MEMCPY_TO_TARGET(name, BUFFERDATA(rbuff)+24, namlen);    }    angel_ChannelReleaseBuffer(rbuff);    LogInfo(LOG_SYS, ("sys_tmpnam: status: %d, name %s\n", status, BUFFERDATA(rbuff)+24 ));    return status;}static int _sys_clock(void){#if TIMER_SUPPORTED        int clks = (int)(Timer_CurrentTime() / 10L);#else        p_Buffer rbuff;    int status, clks;    if (_sys_build_and_transact(&rbuff, CL_Clock|HtoT, (word *)&status,                                "%w%w%w%w", CL_Clock |TtoH,                                0, ADP_HandleUnknown, ADP_HandleUnknown))    {        LogInfo(LOG_SYS, ("sys_clock: Comms failure\n"));        return -1;    }    if (status)        clks = -1;    else        clks = PREAD(LE,(word *)(BUFFERDATA(rbuff)+20));    angel_ChannelReleaseBuffer(rbuff);#endif    LogInfo(LOG_SYS, ("sys_clock: returning %d\n", clks));    return clks;}#if SYS_USES_TIMER && TIMER_SUPPORTEDstatic time_t currenttime = (time_t)-1;static bool set_time = FALSE;static int h_timer;static void sys_time_tick(int x, unsigned y){    IGNORE(x);    IGNORE(y);        LogInfo(LOG_SYS, ("sys_time_tick\n"));    if (set_time)        currenttime++;}#endifstatic time_t _sys_time(void){    p_Buffer rbuff;    int status;    #if SYS_USES_TIMER && TIMER_SUPPORTED    if (!set_time)    {        currenttime = -1; /* so we return -1 on fail */                /* first time round, ask host for real time-of-day */        if (_sys_build_and_transact(&rbuff, CL_Time|HtoT, (word *)&status,                                    "%w%w%w%w", CL_Time |TtoH,                                    0, ADP_HandleUnknown, ADP_HandleUnknown))        {            LogInfo(LOG_SYS, ("sys_time: Comms failure\n"));            return -1;        }                if (!status)        {            currenttime = PREAD(LE,(word *)(BUFFERDATA(rbuff)+20));            set_time = TRUE;

⌨️ 快捷键说明

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