lptio.c

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C语言 代码 · 共 330 行

C
330
字号
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-2000 Microsoft Corporation.  All rights reserved.

Module Name:

lptio.c

Abstract:
P1284 Standard Centronic port output function

Notes:


--*/

#include  <windows.h>
#include  <ceddk.h>
#include  <pardbg.h>
#include    "1284comm.h"
#include    "tmtick.h"
#include  "comdef.h"
#include  "1284neg.h"
#include  "lptio.h"

/* notifications passed in Event of Notification function */

#define   CN_RECEIVE     1     /* bytes are available in the input queue */
#define   CN_TRANSMIT    2     /* fewer than wOutTrigger bytes still */
                     /* remain in the output queue waiting */
                     /* to be transmitted. */
#define   CN_EVENT  4     /* an enabled event has occurred */


BYTE    BusyWaitPenalty[]={0,0,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3};

#define    LPT_CLEAR_MASK     0x40
#define    LPT_STROBE_HI      0x0D
#define    LPT_STROBE_LO      0x0C

#define    LPT_STATUS_BITS    0xF8
#define    LPT_BITS_INVERT    0x48
#define    LPT_NOTBUSY        0x80
#define    LPT_PAPEROUT       0x20
#define    LPT_SELECT         0x10
#define    LPT_NFAULT         0x08

#define    LPT_TimeOut        0x01

void   OutByte(ULONG dataport, BYTE databyte, BOOL fStrobe) {
#if x86
_asm {
    mov     dx, word ptr [dataport]
    mov     al, byte ptr [databyte]
    out     dx, al
    out     dx, al
    add     dx, 2
    in      al, dx
    and     al, LPT_CLEAR_MASK
    mov     cl, al
    or      al, LPT_STROBE_HI
    out     dx, al
    out     dx, al
    out     dx, al
    out     dx, al
    or      cl, LPT_STROBE_LO
    mov     al, cl
    out     dx, al
}
#else

     WRITE_PORT_UCHAR((PUCHAR)dataport,databyte);

if ( fStrobe )
{
     dataport+=2;
     databyte = (READ_PORT_UCHAR((PUCHAR)dataport) & LPT_CLEAR_MASK)
              | LPT_STROBE_HI;
     WRITE_PORT_UCHAR((PUCHAR)dataport,databyte);

     databyte = (READ_PORT_UCHAR((PUCHAR)dataport) & LPT_CLEAR_MASK)
              | LPT_STROBE_LO;
     WRITE_PORT_UCHAR((PUCHAR)dataport,databyte);
}
#endif
}
BYTE   CheckStatus(unsigned port) {
     BYTE bRet;
#if x86
_asm {
    mov     dx, word ptr[port]
Checkme:
    in      al, dx
    mov     ah, al
     nop
     nop
    in      al, dx
    cmp     al, ah
    jnz     Checkme
    and     ah, LPT_STATUS_BITS
    xor     ah, LPT_BITS_INVERT
    test    ah, LPT_PAPEROUT or LPT_NFAULT
    jnz     CS_HasError
    test    ah, LPT_SELECT
    jz      CS_HasError
    and     ah, LPT_NOTBUSY
    jz      CS_HasError
    xor     eax, eax
CS_HasError:
     mov       [bRet],al
}
#else
     BYTE bStatus;
     do {
          bRet= bStatus= READ_PORT_UCHAR((PUCHAR)port);
     } while (bStatus != READ_PORT_UCHAR((PUCHAR)port));
     bStatus&= LPT_STATUS_BITS;
     bStatus^= LPT_BITS_INVERT;

     if (!(bStatus & (LPT_PAPEROUT | LPT_NFAULT)) &&
          (bStatus & LPT_SELECT) && (bStatus & LPT_NOTBUSY))
          bRet=0;
#endif
     return bRet;
}


//---------------------------------------------------------------------------
DWORD
SetError
(
    BYTE    bData
)
// RETURN
//  CE_PTO  --> printer busy
//  CE_OOP  --> out of paper
//  CE_IOE  --> printer error
//  CE_DNS  --> device not selected
//---------------------------------------------------------------------------
{
    // for Centronic, nBusy high means not busy
    // no invert needed.
    // process error conditions
    if ((bData & LPT_PAPEROUT) == LPT_PAPEROUT)
    {
        return (CE_OOP);
    }
    if ((bData & LPT_NFAULT) == LPT_NFAULT)
    {
        return (CE_IOE);
    }
    if (((bData & LPT_SELECT) ^ LPT_SELECT) == LPT_SELECT)
    {
        return (CE_DNS);
    }
    if ((bData & LPT_NOTBUSY) == LPT_NOTBUSY)
    {
        return (0);
    }
    else // printer is busy
    {
        return (CE_PTO);
    }
}
//---------------------------------------------------------------------------
BOOL
CheckPrinterStatus
(
    PPortInformation  PortObj,
    unsigned          uStatusPort
)
{
    BYTE    bData;

     bData = CheckStatus(uStatusPort);

    if (bData)
    {
        PortObj->pPortData.dwLastError = PortObj->pPortData.dwCommError;

        bData = (BYTE)READ_PORT_UCHAR((PUCHAR)PortObj->ulBase+1);
        bData &= LPT_STATUS_BITS;
        bData ^= LPT_BITS_INVERT;
        PortObj->pPortData.dwCommError = SetError(bData);
        return (FALSE);
    }
    else
        return (TRUE);

}
//---------------------------------------------------------------------------
//       Probe BUSY after data strobes
//---------------------------------------------------------------------------
BOOL GetBusyDelay
(
    PPortInformation  PortObj,
    LPSTR             lpData
)
// Caller checks current mode and phase.
{
    ULONG uDataPort, uStatusPort;
    int   ticks;
    BYTE  i;

    // Initialize the LPT Port variables
    uDataPort = PortObj->ulBase;
    uStatusPort = uDataPort + 1;

    DEBUGMSG(ZONE_MAXIO,(TEXT("[GetBusyDelay]\r\n")));

    // We'll start with the best case
    PortObj->bWaitBusy = 0;

    // We'll test for 200 loops
    for (ticks = 200; ticks > 0; ticks--)
    {
        if (CheckPrinterStatus (PortObj, uStatusPort))
        {
            OutByte(uDataPort, *lpData ++, TRUE);

            // If we have already calculated a number
            // use it.  This way the final number should
            // be the worst case.
            for (i = 0; i < PortObj->bWaitBusy; i++)
                READ_PORT_UCHAR((PUCHAR)uStatusPort);

            // look for BUSY high, need to make sure printer responds host strobe
            // maximum no longer than 20us
            for (i = 0; i < MAX_WAIT_BUSY; i++)
            {
               if (!(READ_PORT_UCHAR((PUCHAR)uStatusPort) & 0x80))
                  goto bp_break;
            }
bp_break:

            switch (i)
            {

               case 0:
               case MAX_WAIT_BUSY:
                  // No adjustment required.
                  break;

               default:
                  PortObj->bWaitBusy = (BYTE)(PortObj->bWaitBusy + i);

                  if (PortObj->bWaitBusy > MAX_WAIT_BUSY)
                  {
                     // Something probably seriously wrong here...
                     PortObj->bWaitBusy = MAX_WAIT_BUSY;

                     // might as well exit
                     ticks = 0;
                  }
                  break;
            }

            PortObj->pPortData.QOutCount --;
            PortObj->pPortData.QOutGet ++;

            if (PortObj->pPortData.QOutCount == 0)
            {
                return (TRUE);
            }
        }
    }

     DEBUGMSG(ZONE_IO,(TEXT("[GetBusyDelay] Delay = %Xh\r\n"),PortObj->bWaitBusy));
    return (TRUE);
}
//---------------------------------------------------------------------------
//       Synchronous printing procedures
//---------------------------------------------------------------------------
BOOL
LPT_WriteFewBytes
(
    PPortInformation  PortObj,
    LPSTR             lpData
)
// Caller checks current mode and phase.
{
    ULONG uDataPort, uStatusPort;
    int   ticks;
//    int             i;

    DEBUGMSG(ZONE_IO,(TEXT("[LPT_WriteFewBytes] : Entry\r\n")));
    // Initialize the LPT Port variables
    uDataPort = PortObj->ulBase;
    uStatusPort = uDataPort + 1;


    // If the printer is busy when we enter the loop then we shouldn't wait.
    if (CheckPrinterStatus(PortObj, uStatusPort))
    {
         DEBUGMSG(ZONE_IO,(TEXT("[LPT_WriteFewBytes] : Printer not busy\r\nWriting %u bytes:"),PortObj->pPortData.QOutCount));
//        if (PortObj->bWaitBusy == (BYTE)-1)
//            return (GetBusyDelay(PortObj, lpData));

        for (ticks = LPT_MAX_WRITE; ticks > 0; ticks--)
        {
            if (CheckPrinterStatus (PortObj, uStatusPort))
            {
                   DEBUGMSG(0,(TEXT("%X "),*lpData));
                OutByte(uDataPort, *lpData ++, TRUE);
                PortObj->pPortData.QOutCount --;
                PortObj->pPortData.QOutGet ++;

                // Wait for the time calculated during
                // GetBusyDelay()
//                for (i = 0; i < PortObj->bWaitBusy; i++)
//                    READ_PORT_UCHAR((PUCHAR)uStatusPort);

                if (PortObj->pPortData.QOutCount == 0) {
                        DEBUGMSG(ZONE_IO,(TEXT("\r\n[LPT_WriteFewBytes] : write completed.\r\n")));
                    return (TRUE);
                    }

//                ticks -= BusyWaitPenalty[PortObj->bWaitBusy];

            }
        }
    }
    DEBUGMSG(ZONE_IO,(TEXT("\r\n[LPT_WriteFewBytes] : %u more bytes.\r\n"),PortObj->pPortData.QOutCount));
    return (TRUE);
}

⌨️ 快捷键说明

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