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

📄 ahdlcavr.c

📁 含有完整TCP/IP PPP协议的嵌入式操作系统
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (C) 2003-2004 by egnite Software GmbH. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of *    contributors may be used to endorse or promote products derived *    from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND 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 EGNITE * SOFTWARE GMBH OR 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. * * For additional information see http://www.ethernut.de/ * *//* * $Log: ahdlcavr.c,v $ * Revision 1.2  2005/08/02 17:46:45  haraldkipp * Major API documentation update. * * Revision 1.1  2005/07/26 18:02:27  haraldkipp * Moved from dev. * * Revision 1.10  2005/06/26 12:40:59  chaac * Added support for raw mode to AHDLC driver. * * Revision 1.9  2005/05/27 14:43:28  chaac * Fixed bugs on closing AHDLC sessions. Fixed AHDLC ioctl handling. Not all * messages were handled correctly	and fixed possible problem of reading memory * from address zero. * * Revision 1.8  2005/04/05 17:44:56  haraldkipp * Made stack space configurable. * * Revision 1.7  2005/02/10 07:06:17  hwmaier * Changes to incorporate support for AT90CAN128 CPU * * Revision 1.6  2005/01/24 21:11:48  freckle * renamed NutEventPostFromIRQ into NutEventPostFromIrq * * Revision 1.5  2005/01/21 16:49:45  freckle * Seperated calls to NutEventPostAsync between Threads and IRQs * * Revision 1.4  2004/12/16 08:40:35  haraldkipp * Late increment fixes ICCAVR bug. * * Revision 1.3  2004/03/18 14:05:20  haraldkipp * Comments updated * * Revision 1.2  2004/03/16 16:48:27  haraldkipp * Added Jan Dubiec's H8/300 port. * * Revision 1.1  2004/03/08 11:16:54  haraldkipp * Asynchronous HDLC driver added * */#include <cfg/ahdlc.h>#include <string.h>#include <stdlib.h>#include <sys/nutconfig.h>#include <sys/atom.h>#include <sys/heap.h>#include <sys/event.h>#include <sys/timer.h>#include <sys/thread.h>#include <dev/irqreg.h>#include <dev/uartavr.h>#include <fcntl.h>#include <dev/ppp.h>#include <dev/ahdlcavr.h>#include <stdio.h>/*! * \addtogroup xgAhdlcAvr *//*@{*//* * FCS lookup table located in program memory space. */static prog_char fcstab[512] = {    0x00, 0x00, 0x11, 0x89, 0x23, 0x12, 0x32, 0x9b, 0x46, 0x24, 0x57, 0xad, 0x65, 0x36, 0x74, 0xbf,    0x8c, 0x48, 0x9d, 0xc1, 0xaf, 0x5a, 0xbe, 0xd3, 0xca, 0x6c, 0xdb, 0xe5, 0xe9, 0x7e, 0xf8, 0xf7,    0x10, 0x81, 0x01, 0x08, 0x33, 0x93, 0x22, 0x1a, 0x56, 0xa5, 0x47, 0x2c, 0x75, 0xb7, 0x64, 0x3e,    0x9c, 0xc9, 0x8d, 0x40, 0xbf, 0xdb, 0xae, 0x52, 0xda, 0xed, 0xcb, 0x64, 0xf9, 0xff, 0xe8, 0x76,    0x21, 0x02, 0x30, 0x8b, 0x02, 0x10, 0x13, 0x99, 0x67, 0x26, 0x76, 0xaf, 0x44, 0x34, 0x55, 0xbd,    0xad, 0x4a, 0xbc, 0xc3, 0x8e, 0x58, 0x9f, 0xd1, 0xeb, 0x6e, 0xfa, 0xe7, 0xc8, 0x7c, 0xd9, 0xf5,    0x31, 0x83, 0x20, 0x0a, 0x12, 0x91, 0x03, 0x18, 0x77, 0xa7, 0x66, 0x2e, 0x54, 0xb5, 0x45, 0x3c,    0xbd, 0xcb, 0xac, 0x42, 0x9e, 0xd9, 0x8f, 0x50, 0xfb, 0xef, 0xea, 0x66, 0xd8, 0xfd, 0xc9, 0x74,    0x42, 0x04, 0x53, 0x8d, 0x61, 0x16, 0x70, 0x9f, 0x04, 0x20, 0x15, 0xa9, 0x27, 0x32, 0x36, 0xbb,    0xce, 0x4c, 0xdf, 0xc5, 0xed, 0x5e, 0xfc, 0xd7, 0x88, 0x68, 0x99, 0xe1, 0xab, 0x7a, 0xba, 0xf3,    0x52, 0x85, 0x43, 0x0c, 0x71, 0x97, 0x60, 0x1e, 0x14, 0xa1, 0x05, 0x28, 0x37, 0xb3, 0x26, 0x3a,    0xde, 0xcd, 0xcf, 0x44, 0xfd, 0xdf, 0xec, 0x56, 0x98, 0xe9, 0x89, 0x60, 0xbb, 0xfb, 0xaa, 0x72,    0x63, 0x06, 0x72, 0x8f, 0x40, 0x14, 0x51, 0x9d, 0x25, 0x22, 0x34, 0xab, 0x06, 0x30, 0x17, 0xb9,    0xef, 0x4e, 0xfe, 0xc7, 0xcc, 0x5c, 0xdd, 0xd5, 0xa9, 0x6a, 0xb8, 0xe3, 0x8a, 0x78, 0x9b, 0xf1,    0x73, 0x87, 0x62, 0x0e, 0x50, 0x95, 0x41, 0x1c, 0x35, 0xa3, 0x24, 0x2a, 0x16, 0xb1, 0x07, 0x38,    0xff, 0xcf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 0x16, 0x90, 0xa9, 0x81, 0x20, 0xb3, 0xbb, 0xa2, 0x32,    0x5a, 0xc5, 0x4b, 0x4c, 0x79, 0xd7, 0x68, 0x5e, 0x1c, 0xe1, 0x0d, 0x68, 0x3f, 0xf3, 0x2e, 0x7a,    0xe7, 0x0e, 0xf6, 0x87, 0xc4, 0x1c, 0xd5, 0x95, 0xa1, 0x2a, 0xb0, 0xa3, 0x82, 0x38, 0x93, 0xb1,    0x6b, 0x46, 0x7a, 0xcf, 0x48, 0x54, 0x59, 0xdd, 0x2d, 0x62, 0x3c, 0xeb, 0x0e, 0x70, 0x1f, 0xf9,    0xf7, 0x8f, 0xe6, 0x06, 0xd4, 0x9d, 0xc5, 0x14, 0xb1, 0xab, 0xa0, 0x22, 0x92, 0xb9, 0x83, 0x30,    0x7b, 0xc7, 0x6a, 0x4e, 0x58, 0xd5, 0x49, 0x5c, 0x3d, 0xe3, 0x2c, 0x6a, 0x1e, 0xf1, 0x0f, 0x78};/*! * Checks the 32-bit ACCM to see if the byte needs un-escaping */#define IN_ACC_MAP(c, m) (( ((u_char) (c)) < 0x20)  && ((m) & (1UL << (c))) != 0)#ifndef NUT_THREAD_AHDLCRXSTACK#define NUT_THREAD_AHDLCRXSTACK     512#endif/* * Handle AVR UART0 transmit complete interrupts. */static void Tx0Complete(void *arg){    AHDLCDCB *dcb = arg;    if (dcb->dcb_tx_idx != dcb->dcb_wr_idx) {#ifdef UART0_CTS_BIT        if (bit_is_set(UART0_CTS_PIN, UART0_CTS_BIT)) {            cbi(UCR, UDRIE);            return;        }#endif        outp(dcb->dcb_tx_buf[dcb->dcb_tx_idx], UDR);        dcb->dcb_tx_idx++;    } else {        cbi(UCR, UDRIE);        NutEventPostFromIrq(&dcb->dcb_tx_rdy);    }}#ifdef UART0_CTS_BIT/* * Handle AVR UART0 transmit handshake interrupts. */static void Cts0Interrupt(void *arg){    sbi(UCR, UDRIE);}#endif#ifdef __AVR_ENHANCED__/* * Handle AVR UART1 transmit complete interrupts. */static void Tx1Complete(void *arg){    register AHDLCDCB *dcb = arg;    if (dcb->dcb_tx_idx != dcb->dcb_wr_idx) {#ifdef UART1_CTS_BIT        if (bit_is_set(UART1_CTS_PIN, UART1_CTS_BIT)) {            cbi(UCSR1B, UDRIE);            return;        }#endif        outp(dcb->dcb_tx_buf[dcb->dcb_tx_idx], UDR1);        dcb->dcb_tx_idx++;    } else {        cbi(UCSR1B, UDRIE);        NutEventPostFromIrq(&dcb->dcb_tx_rdy);    }}#ifdef UART1_CTS_BIT/* * Handle AVR UART1 transmit handshake interrupts. */static void Cts1Interrupt(void *arg){    sbi(UCSR1B, UDRIE);}#endif#endif                          /* __AVR_ENHANCED__ *//* * Handle AVR UART0 receive complete interrupts. */static void Rx0Complete(void *arg){    AHDLCDCB *dcb = arg;    dcb->dcb_rx_buf[dcb->dcb_rx_idx] = inp(UDR);    if (dcb->dcb_rd_idx == dcb->dcb_rx_idx)        NutEventPostFromIrq(&dcb->dcb_rx_rdy);    /* Late increment fixes ICCAVR bug on volatile variables. */    dcb->dcb_rx_idx++;}#ifdef __AVR_ENHANCED__/* * Handle AVR UART1 receive complete interrupts. */static void Rx1Complete(void *arg){    AHDLCDCB *dcb = arg;    dcb->dcb_rx_buf[dcb->dcb_rx_idx] = inp(UDR1);    if (dcb->dcb_rd_idx == dcb->dcb_rx_idx)        NutEventPostFromIrq(&dcb->dcb_rx_rdy);    /* Late increment fixes ICCAVR bug on volatile variables. */    dcb->dcb_rx_idx++;}#endif                          /* __AVR_ENHANCED__ *//* * \return 0 on success, -1 in case of any errors. */static int SendRawByte(AHDLCDCB * dcb, u_char ch, u_char flush){    /*     * If transmit buffer is full, wait until interrupt routine     * signals an empty buffer or until a timeout occurs.     */    while ((u_char) (dcb->dcb_wr_idx + 1) == dcb->dcb_tx_idx) {        if (NutEventWait(&dcb->dcb_tx_rdy, dcb->dcb_wtimeout))            break;    }    /*     * If transmit buffer is still full, we have a write timeout.     */    if ((u_char) (dcb->dcb_wr_idx + 1) == dcb->dcb_tx_idx) {        return -1;    }    /*     * Buffer has room for more data. Put the byte in the buffer     * and increment the write index.     */    dcb->dcb_tx_buf[dcb->dcb_wr_idx] = ch;    dcb->dcb_wr_idx++;    /*     * If transmit buffer has become full and the transmitter     * is not active, then activate it.     */    if (flush || (u_char) (dcb->dcb_wr_idx + 1) == dcb->dcb_tx_idx) {        /*         * TODO: Check handshake.         */        NutEnterCritical();#ifdef __AVR_ENHANCED__        if (dcb->dcb_base)            sbi(UCSR1B, UDRIE);        else#endif            sbi(UCR, UDRIE);        NutExitCritical();    }    return 0;}/* * Characters are properly escaped and checksum is updated. * * \return 0 on success, -1 in case of any errors. */static int SendHdlcData(AHDLCDCB * dcb, CONST u_char * data, u_short len, u_short * txfcs){    u_short tbx;    register u_short fcs;    if (txfcs)        fcs = *txfcs;    else        fcs = 0;    while (len) {        tbx = (u_short) ((u_char) fcs ^ *data) << 1;        fcs >>= 8;        fcs ^= ((u_short) PRG_RDB(fcstab + tbx) << 8) | PRG_RDB(fcstab + tbx + 1);        if (IN_ACC_MAP(*data, dcb->dcb_tx_accm) || *data == AHDLC_FLAG || *data == AHDLC_ESCAPE) {            if (SendRawByte(dcb, AHDLC_ESCAPE, 0))                return -1;            if (SendRawByte(dcb, *data ^ AHDLC_TRANS, 0))                return -1;        } else if (SendRawByte(dcb, *data, 0))            return -1;        data++;        len--;    }    if (txfcs)        *txfcs = fcs;    return 0;}/*! * \brief Send HDLC frame. * * \param dev   Identifies the device to use. * \param nb    Network buffer structure containing the packet to be sent. *              The structure must have been allocated by a previous *              call NutNetBufAlloc(). * * \return 0 on success, -1 in case of any errors. */int AhdlcOutput(NUTDEVICE * dev, NETBUF * nb){    u_short txfcs;    AHDLCDCB *dcb = dev->dev_dcb;    u_short sz;    /*     * If we are in RAW mode we are not allowed to send AHDLC output.     * We just emulate packet loss behaviour in here.     */    if (dcb->dcb_modeflags & UART_MF_RAWMODE) {        return 0;    }    /*     * Calculate the number of bytes to be send. Do not     * send packets larger than transmit mru.     */    sz = nb->nb_dl.sz + nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz;    if (sz > dcb->dcb_tx_mru) {        return -1;    }    /*     * TODO: If transmitter is running, we may omit the flag.     */    SendRawByte(dcb, AHDLC_FLAG, 0);    /* Initialize the checksum and send the NETBUF. */    txfcs = AHDLC_INITFCS;    if (SendHdlcData(dcb, nb->nb_dl.vp, nb->nb_dl.sz, &txfcs))        return -1;    if (SendHdlcData(dcb, nb->nb_nw.vp, nb->nb_nw.sz, &txfcs))        return -1;    if (SendHdlcData(dcb, nb->nb_tp.vp, nb->nb_tp.sz, &txfcs))        return -1;    if (SendHdlcData(dcb, nb->nb_ap.vp, nb->nb_ap.sz, &txfcs))        return -1;    /* Send the checksum and the final flag. */    txfcs ^= 0xffff;    if (SendHdlcData(dcb, (u_char *) & txfcs, 2, 0))        return -1;    SendRawByte(dcb, AHDLC_FLAG, 1);    return 0;}/*! \fn AhdlcRx(void *arg) * \brief Asynchronous HDLC receiver thread. * * * Running at high priority. */THREAD(AhdlcRx, arg){    NUTDEVICE *dev = arg;    NUTDEVICE *netdev;    AHDLCDCB *dcb = dev->dev_dcb;    IFNET *ifn;    NETBUF *nb;    u_char *rxbuf;    u_char *rxptr;    u_short rxcnt;    u_char ch;    u_short tbx;    u_char inframe;    u_char escaped;    u_short rxfcs;    NutThreadSetPriority(9);    for (;;) {        /*         * Reset variables to their initial state         */        rxptr = 0;        rxcnt = 0;        escaped = 0;        rxfcs = AHDLC_INITFCS;        inframe = 0;        for (;;) {            /*             * Wait until the network interface has been attached.             * This will be initiated by the application calling             * NutNetIfConfig(), which in turn calls a HDLC_SETIFNET             * ioctl() to store the NUTDEVICE pointer of the network             * device in dev_icb and trigger an event on dcb_mf_evt.             */            while ((netdev = dev->dev_icb) == 0) {                NutEventWait(&dcb->dcb_mf_evt, 1000);            }            ifn = netdev->dev_icb;            dcb->dcb_rtimeout = 1000;            inframe = 0;            /*             * Allocate the receive buffer, if this fails, we are in a             * low memory situation. Take a nap and see, if the             * situation improved.             */            if ((rxbuf = NutHeapAlloc(dcb->dcb_rx_mru)) != 0) {                break;            }            NutSleep(1000);        }        /*         * Signal the link driver that we are up.         */        ifn->if_send = AhdlcOutput;        netdev->dev_ioctl(netdev, LCP_LOWERUP, 0);        for (;;) {            /*

⌨️ 快捷键说明

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