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

📄 rxtest.c

📁 AT91所有开发板的资料 AT91所有开发板的资料
💻 C
字号:
/* rough and ready test harness for the rx engine *//* Copyright (c) 1995, Advanced RISC Machines Limited. * All Rights Reserved. * * $Revision: 1.1 $ *   $Author: rivimey $ *     $Date: 1999/03/11 11:53:43 $ */#include "rxtx.h"#include "crc.h"#include <unistd.h>#include <stdio.h>#include <malloc.h>#define PKTLEN 20#define RPTNUM 4#define corrupt_ch 0x50enum rx_state_flag{    RST_STX,    RST_TYP,    RST_LEN,    RST_DAT,    RST_CRC,    RST_ETX,    RST_ESC = (0x1 << 0x3)};#define N_STX           0x0     /* first 2 bits for N_ */#define N_BODY          0x1#define N_ETX           0x2#define N_IDLE          0x3#define N_MASK          0x3     /* mask for the Encapsulator state */#define E_PLAIN         (0x0 << 2)  /* 3rd bit for E_ */#define E_ESC           (0x1 << 2)  /* 3rd bit for E_ */#define E_MASK          (0x1 << 2)  /* mask for the Escaper state */#define F_HEAD          (0x0 << 3)  /* 4th and 5th bits for F_ */#define F_DATA          (0x1 << 3)#define F_CRC           (0x1 << 4)#define F_MASK          (0x3 << 3)  /* mask for the Escaper state */void report(unsigned int run_num, re_status restat, struct data_packet *r_packet, struct re_state *rxstate);unsigned char corrupt(unsigned int run_num, unsigned char rt_ch, struct te_state *txstate);int main(void){    unsigned int c, d;    re_status restat = 0;    te_status testat = 0;    struct re_config *rxconfig;    struct re_state *rxstate;    struct te_state *txstate;    struct data_packet *t_packet, *r_packet;    unsigned char rt_ch;    rxstate = (struct re_state *)malloc(sizeof(struct re_state));    txstate = (struct te_state *)malloc(sizeof(struct te_state));    rxconfig = (struct re_config *)malloc(sizeof(struct re_config));    r_packet = (struct data_packet *)malloc(sizeof(struct data_packet));    t_packet = (struct data_packet *)malloc(sizeof(struct data_packet));    /* set up the rxconfig */    /* TODO: set up for flow control testing */    rxconfig->stx = serial_STX;    rxconfig->etx = serial_ETX;    rxconfig->esc = serial_ESC;    rxconfig->fc_set = 0;    rxconfig->esc_set = (1 << serial_STX) | (1 << serial_ESC) | (1 << serial_ETX);    rxconfig->fc_callback = NULL;    rxconfig->fc_data = NULL;    /* steup the data packet */    t_packet->data = (char *)malloc(512 * sizeof(unsigned char));    r_packet->data = (char *)malloc(512 * sizeof(unsigned char));    /* init the engine */    /* can't do any checking on the init as it returns void */    r_packet->buf_len = 512;    Angel_RxEngineInit(rxconfig, rxstate);    t_packet->buf_len = 512;    t_packet->len = PKTLEN;    Angel_TxEngineInit(rxconfig, t_packet, txstate);    /* lets build up a packet to give top the tx engine */    t_packet->type = DC_APPL;    /* copy the data into the packet */    for (d = 0; d < RPTNUM; d++)    {        for (c = 0; c < PKTLEN; c++)            t_packet->data[c] = (unsigned char)c;        /* must init the tx engine for each packet */        testat = TS_IDLE;        t_packet->buf_len = 512;        t_packet->len = PKTLEN;        Angel_TxEngineInit(rxconfig, t_packet, txstate);        /* if we previously sent out an unexpected stx we ought to          * reinit the rx engine, purely because it will now think its         * part way into a packet.  This *is* correct */        if ((d >= 2) || (d <= 4))        {            rxstate->rx_state = 0;  /* RST_STX */            rxstate->error = RE_OKAY;            restat = RS_WAIT_PKT;        }        while ((testat != TS_DONE_PKT) && (restat != RS_BAD_PKT))        {            testat = Angel_TxEngine(t_packet, txstate, &rt_ch);            rt_ch = corrupt(d, rt_ch, txstate);            restat = Angel_RxEngine(rt_ch, r_packet, rxstate);        }        report(d, restat, r_packet, rxstate);    }    return 0;}/* to simulate some corrution make rt_ch equal something interesting * * for instance serial_STX on the next line */unsigned char corrupt(unsigned int run_num, unsigned char rt_ch, struct te_state *txstate){    /* we need to test for unexpected stx/etx/esc in the data */    unsigned char fudgepkt[3] =    {serial_STX, serial_ETX, serial_ESC};    switch (run_num)    {        case 0:            /* do an ok packet */            return rt_ch;        case 1:        case 2:        case 3:            /* send an unexpected stx/etx/esc */            if (((txstate->tx_state & F_MASK) == F_CRC) && (txstate->field_c == 4))                return fudgepkt[run_num - 1];  /* send on last data char */        case 4:            /* corrupt the crc, if the next state is to send an etx then corrupt */            if ((txstate->tx_state & N_MASK) == N_ETX)                return corrupt_ch;            else                return rt_ch;            /* do a len>buffersize */            /* do a len =0 */            /* do a long packet */            /* do a short packet */        default:            return rt_ch;    }}void report(unsigned int run_num, re_status restat, struct data_packet *r_packet, struct re_state *rxstate){    unsigned int c;    /* work on the keep quiet if its ok philosophy so as not to hide errors */    switch (run_num)    {        case 0:            if (restat == RS_GOOD_PKT)            {                /* an ok run */                for (c = 0; c < (r_packet->len); c++)                    if (r_packet->data[c] != c)                        printf("Data corrupted on an \"ok\" run\n");            }            else                printf("An \"ok\" packet has gone very wrong\n");            break;        case 1:            if ((restat == RS_BAD_PKT) && (rxstate->error != RE_U_STX))                printf("Error code for an unexpected stx is wrong\n");            else if (restat != RS_BAD_PKT)                printf("An unexpected_stx packet has gone very wrong\n");            else                printf("unexp stx ok\n");            break;        case 2:            if ((restat == RS_BAD_PKT) && (rxstate->error != RE_U_ETX))                printf("Error code for an unexpected etx is wrong\n");            else if (restat != RS_BAD_PKT)                printf("An unexpected_etx packet has gone very wrong\n");            else                printf("unexp etx ok\n");            break;        case 3:            /* an unexpected esc should show up as unexpected ETX as there             * will appear to be a shortage of data chars */            if ((restat == RS_BAD_PKT) && (rxstate->error != RE_U_ETX))                printf("Error code for an unexpected esc is wrong\n");            else if (restat != RS_BAD_PKT)            {                printf("An unexpected_esc packet has gone very wrong\n");                printf("restat=0x%x should have been RS_BAD_PKT, rxstate->error=0x%x should have been RE_U_ETX\n", restat, rxstate->error);            }            else                printf("unexp esc ok\n");            break;        case 4:            /* corrupt crc */            if ((restat == RS_BAD_PKT) && (rxstate->error != RE_CRC))                printf("Error code for a bad crc is wrong\n");            else if (restat != RS_BAD_PKT)                printf("A bad packet has gone very wrong\n");            break;    }}

⌨️ 快捷键说明

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