CenDrv.c 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define CENDRV_C
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#include "Common.h"
#include "XCore.h"
#include "XTimer.h"
#include "CenDrv.h"
/******************************************************************************
* *
* L O C A L D E F I N E S *
* *
******************************************************************************/
#if defined(TTP245P_PCB)
#define CENTRONIC_STROBE ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC16) != AT91C_PIO_PC16 )
#define CENTRONIC_DATA InPort(EXT_IOPORT_01)
#define CENTRONIC_PE AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define CENTRONIC_SELECT AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define CENTRONIC_ERROR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define CENTRONIC_NOT_PE AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define CENTRONIC_NOT_SELECT AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define CENTRONIC_NOT_ERROR AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#elif defined(T045_PCB) ||defined(T40_PCB)
#if !defined(CENTRONIC_DISENABLE)
#define CENTRONIC_STROBE ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC14) != AT91C_PIO_PC14 )
#define CENTRONIC_DATA InPort(EXT_IOPORT_01)
#define CENTRONIC_PE AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC4)
#define CENTRONIC_SELECT AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC8)
#define CENTRONIC_ERROR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC9)
#define CENTRONIC_NOT_PE AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC4)
#define CENTRONIC_NOT_SELECT AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC8)
#define CENTRONIC_NOT_ERROR AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC9)
#endif
#endif
#if defined(TTP245P_PCB)
#define NIBBLE_ACTIVE ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC22) != AT91C_PIO_PC22 )
#define NIBBLE_HOST_BUSY ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC15) != AT91C_PIO_PC15 )
#define NIBBLE_HOST_CLK ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC16) != AT91C_PIO_PC16 )
#define NIBBLE_ENABLE AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC23)
#define NIBBLE_DISABLE AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC23)
#define NIBBLE_ACK_DATA_REQ_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_ACK_DATA_REQ_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_PRT_CLK_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC20)
#define NIBBLE_PRT_CLK_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC20)
#define NIBBLE_PRT_BUSY_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#define NIBBLE_PRT_BUSY_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#define NIBBLE_DATA_AVAIL_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_DATA_AVAIL_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_XFLAG_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_XFLAG_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_DATA_BIT0_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_DATA_BIT0_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_DATA_BIT1_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_DATA_BIT1_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_DATA_BIT2_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_DATA_BIT2_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_DATA_BIT3_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#define NIBBLE_DATA_BIT3_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#elif defined(T045_PCB) ||defined(T40_PCB)
//#define NIBBLE_ACTIVE ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC22) != AT91C_PIO_PC22 )
#define NIBBLE_HOST_BUSY ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC15) != AT91C_PIO_PC15 )
#define NIBBLE_HOST_CLK ( AT91F_PIO_IsInputSet(AT91C_BASE_PIOC, AT91C_PIO_PC16) != AT91C_PIO_PC16 )
#if defined(NIBBLE_ACTIVE)
#define NIBBLE_ENABLE AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC23)
#define NIBBLE_DISABLE AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC23)
#define NIBBLE_ACK_DATA_REQ_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_ACK_DATA_REQ_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_PRT_CLK_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC20)
#define NIBBLE_PRT_CLK_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC20)
#define NIBBLE_PRT_BUSY_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#define NIBBLE_PRT_BUSY_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#define NIBBLE_DATA_AVAIL_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_DATA_AVAIL_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_XFLAG_HIGH AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_XFLAG_LOW AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_DATA_BIT0_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_DATA_BIT0_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC19)
#define NIBBLE_DATA_BIT1_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_DATA_BIT1_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC18)
#define NIBBLE_DATA_BIT2_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_DATA_BIT2_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC17)
#define NIBBLE_DATA_BIT3_SET AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#define NIBBLE_DATA_BIT3_CLR AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC21)
#endif
#endif
#define NIBBLE_OVER_TIME 1000000
#define NIBBLE_DELAY_TIME 100
/******************************************************************************
* *
* L O C A L T Y P E D E F S *
* *
******************************************************************************/
typedef struct _NIBBLE_CLASS_DEVICE_ID
{
BYTE bReverse;
BYTE bLength;
BYTE Device_ID[100];
} NIBBLE_CLASS_DEVICE_ID;
/******************************************************************************
* *
* L O C A L F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* L O C A L I N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* L O C A L U N I T I A L I Z E D D A T A D E F I N I T I O N S *
* *
******************************************************************************/
STATIC BOOL CenSelect, CenFault, CenPError;
#if defined(CENTRONIC2_REV)
STATIC BOOL Centronic2Ready;
#endif
#if defined(NIBBLE_ACTIVE)
STATIC NIBBLE_CLASS_DEVICE_ID Class_Device_ID =
{
0,
53,
"MFG:TEC;CMD:TPCL;MDL:B-SV4;CLS:PRINTER;DES:TEC B-SV4;"
};
STATIC VOID NegotiationPhase(VOID)
{
NIBBLE_ENABLE;
NIBBLE_ACK_DATA_REQ_HIGH;
NIBBLE_PRT_CLK_LOW;
NIBBLE_PRT_BUSY_LOW;
NIBBLE_DATA_AVAIL_HIGH;
NIBBLE_XFLAG_HIGH;
}
STATIC VOID ReverseIdlePhase(VOID)
{
NIBBLE_ENABLE;
NIBBLE_ACK_DATA_REQ_HIGH;
NIBBLE_PRT_CLK_LOW;
NIBBLE_PRT_BUSY_LOW;
NIBBLE_DATA_AVAIL_HIGH;
NIBBLE_XFLAG_LOW;
}
STATIC VOID TerminationPhase(VOID)
{
NIBBLE_ENABLE;
NIBBLE_ACK_DATA_REQ_HIGH;
NIBBLE_PRT_CLK_LOW;
NIBBLE_PRT_BUSY_HIGH;
NIBBLE_DATA_AVAIL_HIGH;
NIBBLE_XFLAG_HIGH;
}
STATIC VOID DataAvailPhase(VOID)
{
NIBBLE_ENABLE;
NIBBLE_ACK_DATA_REQ_LOW;
NIBBLE_PRT_CLK_LOW;
NIBBLE_PRT_BUSY_LOW;
NIBBLE_DATA_AVAIL_LOW;
NIBBLE_XFLAG_LOW;
}
STATIC VOID DataNotAvailPhase(VOID)
{
NIBBLE_ENABLE;
NIBBLE_ACK_DATA_REQ_HIGH;
NIBBLE_PRT_CLK_LOW;
NIBBLE_PRT_BUSY_LOW;
NIBBLE_DATA_AVAIL_HIGH;
NIBBLE_XFLAG_LOW;
}
STATIC VOID CompatibilityMode(VOID)
{
NIBBLE_DISABLE;
if ( CenPError )
CENTRONIC_PE;
else
CENTRONIC_NOT_PE;
if ( CenSelect )
CENTRONIC_SELECT;
else
CENTRONIC_NOT_SELECT;
if ( CenPError )
CENTRONIC_ERROR;
else
CENTRONIC_NOT_ERROR;
}
STATIC VOID DataTransferPhase(BYTE data)
{
NIBBLE_ENABLE;
if ( data & ( 1 << 0 ) )
NIBBLE_DATA_BIT0_SET;
else
NIBBLE_DATA_BIT0_CLR;
if ( data & ( 1 << 1 ) )
NIBBLE_DATA_BIT1_SET;
else
NIBBLE_DATA_BIT1_CLR;
if ( data & ( 1 << 2 ) )
NIBBLE_DATA_BIT2_SET;
else
NIBBLE_DATA_BIT2_CLR;
if ( data & ( 1 << 3 ) )
NIBBLE_DATA_BIT3_SET;
else
NIBBLE_DATA_BIT3_CLR;
}
STATIC VOID HostRequest(VOID)
{
INT length;
BYTE *data;
BYTE mode;
INT tmp;
if ( !NIBBLE_ACTIVE )
return;
NegotiationPhase();
DelayTime(NIBBLE_DELAY_TIME);
tmp = 0;
while ( !NIBBLE_HOST_CLK )
{
if ( tmp++ > NIBBLE_OVER_TIME )
{
CompatibilityMode();
return;
}
}
mode = CENTRONIC_DATA;
if ( mode == 0x00 ) // Nibble Mode
{
length = 0;
data = (BYTE *)_NULL;
}
else if ( mode == 0x04 ) // Mode Reverse Channel Transfer
{
length = 54;
data = (BYTE *)&Class_Device_ID;
}
// AT91F_PIO_CfgInput(AT91C_BASE_PIOC, AT91C_PIO_PC15);
while ( length )
{
DataAvailPhase();
DelayTime(NIBBLE_DELAY_TIME);
//--------------------------------------------------
// Low Half
//--------------------------------------------------
NIBBLE_PRT_CLK_HIGH;
DelayTime(NIBBLE_DELAY_TIME);
tmp = 0;
while ( NIBBLE_HOST_BUSY )
{
if ( tmp++ > NIBBLE_OVER_TIME )
{
CompatibilityMode();
return;
}
}
DelayTime(NIBBLE_DELAY_TIME);
DataTransferPhase(*data & 0xF);
DelayTime(NIBBLE_DELAY_TIME);
NIBBLE_PRT_CLK_LOW;
DelayTime(NIBBLE_DELAY_TIME);
tmp = 0;
while ( !NIBBLE_HOST_BUSY )
{
if ( tmp++ > NIBBLE_OVER_TIME )
{
CompatibilityMode();
return;
}
}
DelayTime(NIBBLE_DELAY_TIME);
//--------------------------------------------------
// High Half
//--------------------------------------------------
NIBBLE_PRT_CLK_HIGH;
DelayTime(NIBBLE_DELAY_TIME);
tmp = 0;
while ( NIBBLE_HOST_BUSY )
{
if ( tmp++ > NIBBLE_OVER_TIME )
{
CompatibilityMode();
return;
}
}
DelayTime(NIBBLE_DELAY_TIME);
DataTransferPhase(*data >> 4);
DelayTime(NIBBLE_DELAY_TIME);
NIBBLE_PRT_CLK_LOW;
DelayTime(NIBBLE_DELAY_TIME);
tmp = 0;
while ( !NIBBLE_HOST_BUSY )
{
if ( tmp++ > NIBBLE_OVER_TIME )
{
CompatibilityMode();
return;
}
}
DelayTime(NIBBLE_DELAY_TIME);
length -= 1;
data += 1;
}
// AT91F_PIO_CfgPeriph(AT91C_BASE_PIOC, 0, AT91C_PC15_IRQ1);
CompatibilityMode();
return;
}
#endif
/******************************************************************************
*
* Function:
* SetCenStatus
*
* Description:
* This global function initializes Centronic pin status.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID SetCenStatus(BOOL online, BOOL err, BOOL pe)
{
CenSelect = online;
CenFault = err;
CenPError = pe;
#if defined(CENTRONIC_PE) && defined(CENTRONIC_NOT_PE)
if ( CenPError )
CENTRONIC_PE;
else
CENTRONIC_NOT_PE;
#endif
#if defined(CENTRONIC_SELECT) && defined(CENTRONIC_NOT_SELECT)
if ( CenSelect )
CENTRONIC_SELECT;
else
CENTRONIC_NOT_SELECT;
#endif
#if defined(CENTRONIC_ERROR) && defined(CENTRONIC_NOT_ERROR)
if ( CenFault )
CENTRONIC_ERROR;
else
CENTRONIC_NOT_ERROR;
#endif
#if defined(CENTRONIC2_REV)
if ( Centronic2Ready )
{
OutPin( CEN2_CTRL_PORT, PIN_PE_2, (pe ? HIGH : LOW) );
OutPin( CEN2_CTRL_PORT, PIN_SLCT_2, (online ? HIGH : LOW) );
OutPin( CEN2_CTRL_PORT, PIN_ERROR_2, (err ? LOW : HIGH) );
}
#endif
}
/******************************************************************************
*
* Function:
* GetCenData
*
* Description:
* This global function get Centronic data.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
INT GetCenData(BYTE *data)
{
#if defined(CENTRONIC_STROBE) && defined(CENTRONIC_DATA)
if ( CENTRONIC_STROBE )
{
*data = CENTRONIC_DATA;
return TRUE;
}
#endif
#if defined(CENTRONIC2_REV)
if ( Centronic2Ready )
{
if ( CENTRONIC2_STROBE )
{
*data = CENTRONIC2_DATA;
return TRUE;
}
}
#endif
return FALSE;
}
/******************************************************************************
*
* Function:
* CenDataCheck
*
* Description:
* This global function check have Centronic data.
*
* Input:
* None
*
* Output:
* TRUE or FALSE;
*
******************************************************************************/
INT CenDataCheck(VOID)
{
#if defined(CENTRONIC_STROBE)
if ( CENTRONIC_STROBE )
return TRUE;
#endif
#if defined(CENTRONIC2_REV)
if ( Centronic2Ready )
{
if ( CENTRONIC2_STROBE )
return TRUE;
}
#endif
return FALSE;
}
/******************************************************************************
*
* Function:
* InitialCenDrv
*
* Description:
* This global function initializes Centronic prot.
*
* Input:
* None
*
* Output:
* None.
*
******************************************************************************/
VOID InitialCenDrv(VOID)
{
#if defined(NIBBLE_ACTIVE)
NIBBLE_DISABLE;
AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, // AIC base address
AT91C_ID_IRQ1, // Peripheral ID
AT91C_AIC_PRIOR_LOWEST, // Priority
AT91C_AIC_SRCTYPE_EXT_POSITIVE_EDGE, // Level sensitive
HostRequest );
// Enable FIQ interrupt
AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_IRQ1);
#endif
#if defined(CENTRONIC2_REV)
Centronic2Ready = FALSE;
OutPort(CENTRONIC2_REV, 0xFF);
if ( InPort(CENTRONIC2_REV) == CENTRONIC2_REV_DEF | 0x01 )
{
OutPort(CENTRONIC2_REV, 0x00);
if ( InPort(CENTRONIC2_REV) == CENTRONIC2_REV_DEF )
Centronic2Ready = TRUE;
}
#endif
SetCenStatus(TRUE, FALSE, FALSE);
#if defined(CENTRONIC_DATA)
CENTRONIC_DATA;
#endif
#if defined(CENTRONIC2_REV)
if ( Centronic2Ready )
CENTRONIC2_DATA;
#endif
}
CenDrv.h 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
#ifndef CENDRV_H
/******************************************************************************
* *
* M O D U L E D E F I N E *
* *
******************************************************************************/
#define CENDRV_H
/******************************************************************************
* *
* C O M P I L E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* U S E R D E F I N E D I N C L U D E F I L E S *
* *
******************************************************************************/
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
/******************************************************************************
* *
* G L O B A L D E F I N E S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* S T R U C T U R E D E F I N I T I O N S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - N O I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* G L O B A L V A R I A B L E S - I N I T I A L I Z E R S *
* *
******************************************************************************/
/* None */
/******************************************************************************
* *
* F U N C T I O N P R O T O T Y P E S *
* *
******************************************************************************/
VOID InitialCenDrv(VOID);
VOID SetCenStatus(BOOL, BOOL, BOOL);
INT GetCenData(BYTE *);
INT CenDataCheck(VOID);
#ifdef __cplusplus
} /* End of extern "C" { */
#endif /* __cplusplus */
#endif