?? zlg85c30.c
字號:
#include "vxWorks.h"
#include "iv.h"
#include "tyLib.h"
#include "intLib.h"
#include "rngLib.h"
#include "logLib.h"
#include "Zlg85C30.h"
#include "pc.h"
#include "stdio.h"
LOCAL int dataAddr[ComMaxUnit];
LOCAL int commandAddr[ComMaxUnit];
LOCAL int comMode[ComMaxUnit];
int comIntTaskId;
//RING_ID comInt_ring_id=NULL;
//SEM_ID comInt_sem_id=NULL;
RING_ID comR_ring_id[ComMaxUnit];
RING_ID comT_ring_id[ComMaxUnit];
RING_ID comS_ring_id[ComMaxUnit];
SEM_ID comR_sem_id[ComMaxUnit];
int portsending[ComMaxUnit];
LOCAL void ComIntHandle(int iPortNo);
LOCAL void ComIntHandle2(int iPortNo);
LOCAL void ComIntHandle3(int iPortNo);
//LOCAL void ComIntHandle4(int iPortNo);
LOCAL void ComOneIntHandle(int iPortNoN,unsigned char intflag);
LOCAL void com_IntTask(void);
int Zlg85C30Debugstatus=0;
/******************************************************************/
STATUS ComIntInit(int iPortNo,int dataport,unsigned long lBaudrate,unsigned char intLevel)
{
unsigned long iDiviValue=115200L/lBaudrate;
unsigned char chLbr;
unsigned char chMbr;
int lockKey;
printf("ComIntInit PortNo=%d dataAddr=%x baudrate=%d intLevel=%d\n", iPortNo,dataport,lBaudrate,intLevel);
if(iPortNo>=ComMaxUnit)return 0;
dataAddr[iPortNo]=dataport;
commandAddr[iPortNo]=dataport;
sysOutByte(dataAddr[iPortNo]+com_lineclt,0x03);
sysOutByte(dataAddr[iPortNo]+com_lineclt,(0x80|sysInByte(dataAddr[iPortNo] + com_lineclt)));
chLbr=iDiviValue&0xff;
chMbr=(iDiviValue>>8)&0xff;
sysOutByte(dataAddr[iPortNo],chLbr);
sysOutByte(dataAddr[iPortNo]+1,chMbr);
sysOutByte(dataAddr[iPortNo]+com_lineclt,(0x7f&sysInByte(dataAddr[iPortNo] + com_lineclt)));
portsending[iPortNo]=0;
comR_ring_id[iPortNo]=rngCreate(ComMaxData);
if(comR_ring_id[iPortNo]==NULL){printf("Error: can not create ring\n");return ERROR;}
comS_ring_id[iPortNo]=rngCreate(ComMaxData);
if(comS_ring_id[iPortNo]==NULL){printf("Error: can not create ring\n");return ERROR;}
if((comR_sem_id[iPortNo]=semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL)
{printf("Error: can not create sem!\n");return ERROR;}
lockKey = intLock ();
if(sysIntDisablePIC(intLevel)==ERROR){intUnlock (lockKey); return ERROR;}
if(intConnect(INUM_TO_IVEC(0x20+intLevel),ComIntHandle,iPortNo)==ERROR)
{
intUnlock (lockKey);
logMsg("Error: connect int\n",0,0,0,0,0,0);
return ERROR;
}
if(sysIntEnablePIC(intLevel)==ERROR){intUnlock (lockKey); return ERROR;}
sysOutByte(dataAddr[iPortNo]+ com_modemclt,0x0b);
sysOutByte(dataAddr[iPortNo]+ com_intenable,0x03);
intUnlock (lockKey);
return OK;
}
LOCAL void ComIntHandle(int iPortNo)
{
unsigned char intflag=0,lineStatus;
int lockKey;
char str[2];
/*lockKey = intLock ();
if(iPortNo>=ComMaxUnit){intUnlock (lockKey); return;}
FOREVER
{
portconfig[iPortNo].intNum++;
intflag=(sysInByte(dataAddr[iPortNo]+com_intflag)&0x06);
if((intflag==0)||(intflag==1)||(intflag==3))break;
//intflag=sysInByte(dataAddr[iPortNo]+com_intflag)&0x07;
//if((intflag==1)||(intflag==0))break;
//intflag &=0x06;
//if(intflag==0)break;
switch (intflag)
{
case 2:
if(rngIsEmpty(comS_ring_id[iPortNo])==FALSE)
{
rngBufGet(comS_ring_id[iPortNo],str,1);
sysOutByte(dataAddr[iPortNo]+com_dataport,str[0]);
portsending[iPortNo]=1;
portconfig[iPortNo].SendByteNum++;
}
else
{
portsending[iPortNo]=0;
}
break;
case 4:
str[0]=sysInByte(dataAddr[iPortNo]+com_dataport);
portconfig[iPortNo].RevByteNum++;
if(rngFreeBytes(comR_ring_id[iPortNo])>0)
{
if(rngBufPut(comR_ring_id[iPortNo],str,1)<1)
logMsg("Put flow!\n",0,0,0,0,0,0);
}
break;
case 6:
//lineStatus = sysInByte(dataAddr[iPortNo]+com_lineflag);
break;
define: break;
}
}
if(rngIsEmpty(comR_ring_id[iPortNo])==FALSE)semGive(comR_sem_id[iPortNo]);
intUnlock (lockKey);*/
lockKey = intLock ();
portconfig[iPortNo].intNum++;
if(iPortNo>=ComMaxUnit){intUnlock (lockKey); return;}
intflag=sysInByte(dataAddr[iPortNo]+com_intflag);
while((intflag&0x06)!=0)
{
switch (intflag)
{
case 2:
if(rngIsEmpty(comS_ring_id[iPortNo])==FALSE)
{
rngBufGet(comS_ring_id[iPortNo],str,1);
sysOutByte(dataAddr[iPortNo]+com_dataport,str[0]);
portsending[iPortNo]=1;
portconfig[iPortNo].SendByteNum++;
}
else
{
portsending[iPortNo]=0;
}
break;
case 4:
str[0]=sysInByte(dataAddr[iPortNo]+com_dataport);
portconfig[iPortNo].RevByteNum++;
if(rngFreeBytes(comR_ring_id[iPortNo])>0)
{
if(rngBufPut(comR_ring_id[iPortNo],str,1)<1)
logMsg("Put flow!\n",0,0,0,0,0,0);
}
break;
define: break;
}
intflag=sysInByte(dataAddr[iPortNo]+com_intflag);
}
if(rngIsEmpty(comR_ring_id[iPortNo])==FALSE)semGive(comR_sem_id[iPortNo]);
intUnlock (lockKey);
}
/****************************************
*
*
Register Value Comments
WR9 C0H Force Hardware Reset
WR4 4CH x 16 clock, 2 stop bits, no parity
WR2 00H Interrupt Vector 00
WR3 C0H Rx 8 bits, Rx disabled
WR5 60H Tx 8 bits, DTR, RTS, Tx off
WR9 00H Int Disabled
WR10 00H NRZ
WR11 56H Tx & Rx = BRG out, TRxC = BRG out
WR12 06H Time constant = 6
WR13 00H Time constant high = 0
WR14 10H BRG in = RTxC, BRG of, loopback
Enables
WR14 11H BRG enable
WR3 C1H Rx enable
WR5 68H Tx enable
Enable Interrupts
WR1 12H Rx Int on all char and Tx Int enables
WR9 08H MIE
C4H → WR4, 00H → WR2,C0H → WR3 60H → WR5,00H → WR9, 50H → WR11 02H → WR14
,計算時間常數 時間常數低字節→WR12 時間常數高字節→WR13
03H → WR14,C1H → WR3,68H → WR5 12H → WR1,00H → WR15,08H → WR9
*
*****************************************/
STATUS com_asyncInitial(int iPortNo
,int commandport
,int dataport
,unsigned long baudRate)
{
unsigned char baut_hi;
unsigned char baut_lo;
unsigned long tc=0,tc1=0,tc2=0;
unsigned long br=0;
printf("com_asyncInitial PortNo=%d dataAddr=%x baudrate=%d\n", iPortNo,dataport,baudRate);
dataAddr[iPortNo]=dataport;
commandAddr[iPortNo]=commandport;
comMode[iPortNo]=0;
sysOutByte(commandport,0x04);
sysOutByte(commandport,0x84);//0xc4:X64;0x84:X32;0x44:X16;0x04:X1
sysOutByte(commandport,0x02);
sysOutByte(commandport,0x0);
sysOutByte(commandport,0x03);
sysOutByte(commandport,0xc0);
sysOutByte(commandport,0x05);
sysOutByte(commandport,0x60);
sysOutByte(commandport,0x09);
sysOutByte(commandport,0x0);
sysOutByte(commandport,0x0b);
sysOutByte(commandport,0x50);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x02);
br=baudRate;
//tc=(clock_f/(2*br*64))-2;
tc=(clock_f/(2*br*32))-2;
tc1=tc2=tc;
baut_hi=(tc1&0xff00)>>8;
baut_lo=(tc2&0x00ff);
printf("%ld %2x %2X",tc,baut_hi,baut_lo);
sysOutByte(commandport,0x0c);
sysOutByte(commandport,baut_lo);
sysOutByte(commandport,0x0d);
sysOutByte(commandport,baut_hi);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x03);
sysOutByte(commandport,0x03);
sysOutByte(commandport,0xc1);
sysOutByte(commandport,0x05);
sysOutByte(commandport,0x68);
sysOutByte(commandport,0x01);
sysOutByte(commandport,0x12);
sysOutByte(commandport,0x0f);
sysOutByte(commandport,0x0);
sysOutByte(commandport,0x09);
sysOutByte(commandport,0x08);
/*sysInByte(dataport);*/
portsending[iPortNo]=0;
comR_ring_id[iPortNo]=rngCreate(ComMaxData);
if(comR_ring_id[iPortNo]==NULL){printf("Error: can not create ring\n");return ERROR;}
comS_ring_id[iPortNo]=rngCreate(ComMaxData);
if(comS_ring_id[iPortNo]==NULL){printf("Error: can not create ring\n");return ERROR;}
if((comR_sem_id[iPortNo]=semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL)
{printf("Error: can not create sem!\n");return ERROR;}
return OK;
}
/*********************************
*
Initializing in SDLC Mode
Bit #
Reg D7 D6 D5 D4 D3 D2 D1 D0 Description
WR4 0 0 1 0 0 0 0 0 Select x1 clock,
SDLC mode, enable sync mode
WR3 r x 0 1 1 1 0 0 rx=# of Rx bits/char, No auto enable, enter Hunt.
Enable Rx CRC, Address Search, No sync character
load inhibit
WR5 d t x 0 0 0 r 1 d=inverse of DTR pin, tx=# of Tx bits/char, use SDLC CRC,
r=inverse state of /RTS pin, CRC enable
WR7 0 1 1 1 1 1 1 0 SDLC Flag
WR6 x x x x x x x x Receiver secondary address
WR15 x x x x x x x 1 Enable access to new register
WR7' 0 1 1 d 1 r 1 1 Enable extended read, Tx INT on FIFO empty,
d=REQUEST timing mode, Rx INT on 4 char, r=RTS
deactivation, auto EOM reset, auto flag tx CRC preset to
zero, NRZ data,i=idle line
WR10 0 0 0 0 i 0 0 0 CRC preset to zero, NRZ data, i=idle line
WR3 r x 0 1 1 1 0 1 Enable Receiver
WR5 d t x 0 1 0 r 1 Enable Transmitter
WR0 1 0 0 0 0 0 0 0 Reset CRC generator
*
00H→WR2,D8H→WR3,61H→WR5,00H→WR9
00H→WR15,7EH→WR7,FFH→WR6,01H→WR15
43H→WR7,88H→WR10,60H→WR14,
計算時間常數時間常數低字節→WR12,時間常數高字節→WR13,02H→WR14
*
*
**********************************/
STATUS com_syncInitial(int iPortNo
,int commandport
,int dataport
,unsigned long baudRate
,int Tclock_type)
{
int baut_hi;
int baut_lo;
unsigned long tc=0,tc1=0,tc2=0;
unsigned long br=0;
printf("com_syncInitial PortNo=%d dataAddr=%x baudrate=%d clockType=%d\n", iPortNo,dataport,baudRate,Tclock_type);
dataAddr[iPortNo]=dataport;
commandAddr[iPortNo]=commandport;
comMode[iPortNo]=1;
sysOutByte(commandport,0x04);
if(Tclock_type==0)
sysOutByte(commandport,0xa0);
else
sysOutByte(commandport,0x20);
sysOutByte(commandport,0x02);
sysOutByte(commandport,0x00);
sysOutByte(commandport,0x03);
sysOutByte(commandport,0xd8);
sysOutByte(commandport,0x05);
sysOutByte(commandport,0x61);
sysOutByte(commandport,0x09);
sysOutByte(commandport,0x0);
sysOutByte(commandport,0x0f);
sysOutByte(commandport,0x0);
sysOutByte(commandport,0x07);
sysOutByte(commandport,0x7e);
sysOutByte(commandport,0x06);
sysOutByte(commandport,0xff);
sysOutByte(commandport,0x0f);
sysOutByte(commandport,0x01);
sysOutByte(commandport,0x07);
sysOutByte(commandport,0x43);
sysOutByte(commandport,0x0a);
sysOutByte(commandport,0x88);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x60);
if(Tclock_type==0)
{
br=baudRate;
tc=(clock_f/(2*br*32))-2;
tc1=tc2=tc;
baut_hi=(tc1&0xff00)>>8;
baut_lo=(tc2&0x00ff);
}
else
{
br=baudRate;
tc=(clock_f/(2*br))-2;
tc1=tc2=tc;
baut_hi=(tc1&0xff00)>>8;
baut_lo=(tc2&0x00ff);
}
sysOutByte(commandport,0x0c);
sysOutByte(commandport,baut_lo);
sysOutByte(commandport,0x0d);
sysOutByte(commandport,baut_hi);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x02);
if(Tclock_type==0)
{
sysOutByte(commandport,0x0b);
sysOutByte(commandport,0x7f);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x83);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0xe3);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x23);
}
else if(Tclock_type==1)
{
sysOutByte(commandport,0x0b);
sysOutByte(commandport,0x56);
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x63);
}
else
{
/*
Write Register 11:
D1 D0 :
0 0 /TRxC Out = Xtal Output
0 1 /TRxC Out = Transmit Clock
1 0 /TRxC Out = BR Generator Output
1 1 /TRxC Out = DPLL Output
D2 : /TRxC O/I
D4 D3:
0 0 Transmit Clock = /RTxC Pin
0 1 Transmit Clock = /TRxC Pin
1 0 Transmit Clock = BR Generator Output
1 1 Transmit Clock = DPLL Output
D6 D5:
0 0 Receive Clock = /RTxC Pin
0 1 Receive Clock = /TRxC Pin
1 0 Receive Clock = BR Generator Output
1 1 Receive Clock = DPLL Output
D7: /RTxC Xtal//No Xtal
*/
sysOutByte(commandport,0x0b);
sysOutByte(commandport,0x00);
/*sysOutByte(commandport,0x2d);*/
sysOutByte(commandport,0x0e);
sysOutByte(commandport,0x63);
}
sysOutByte(commandport,0x03);
sysOutByte(commandport,0xd9);
sysOutByte(commandport,0x05);
sysOutByte(commandport,0x69);
sysOutByte(commandport,0x01);
sysOutByte(commandport,0x13);
sysOutByte(commandport,0x0f);
sysOutByte(commandport,0x40);
sysOutByte(commandport,0x09);
sysOutByte(commandport,0x08);
/*sysOutByte(commandport,0x40);*/
/*sysInByte(dataport);*/
portsending[iPortNo]=0;
comR_ring_id[iPortNo]=rngCreate(ComMaxData);
if(comR_ring_id[iPortNo]==NULL){printf("Error: can not create ring\n");return ERROR;}
comS_ring_id[iPortNo]=rngCreate(ComMaxData);
if(comS_ring_id[iPortNo]==NULL){printf("Error: can not create ring\n");return ERROR;}
if((comR_sem_id[iPortNo]=semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL)
{printf("Error: can not create sem!\n");return ERROR;}
return OK;
}
/*
LOCAL void com_IntTask(void)
{
unsigned char iPortNo,intflag;
if(comInt_sem_id==NULL)return;
FOREVER
{
semTake(comInt_sem_id,WAIT_FOREVER);
while(rngIsEmpty(comInt_ring_id)==FALSE)
{
rngBufGet(comInt_ring_id,&iPortNo,1);
rngBufGet(comInt_ring_id,&intflag,1);
//ComOneIntHandle(iPortNo,intflag);
}
}
}
*/
STATUS com_intConnect(int iAPortNo,unsigned char intLevel)
{
int lockKey;
if(iAPortNo>=ComMaxUnit)return ERROR;
/* // 060423 Add wzk //
if(comInt_ring_id==NULL)
{
comInt_ring_id=rngCreate(ComMaxData);
if(comInt_ring_id==NULL)
{printf("Error: can not create ring\n");return ERROR;}
if((comInt_sem_id=semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL)
{printf("Error: can not create sem!\n");return ERROR;}
if((comIntTaskId=taskSpawn("comTask",100,0,10000,(FUNCPTR)com_IntTask,0,0,0,0,0,0,0,0,0,0))==ERROR)
{
printf("comIntTest task cannot be create.");
return ERROR;
}
printf(" Create Zlg85C30 Interrupt Ring\n");
}*/
lockKey = intLock ();
if(sysIntDisablePIC(intLevel)==ERROR){intUnlock (lockKey); return ERROR;}
if(intConnect(INUM_TO_IVEC(0x20+intLevel),ComIntHandle3,iAPortNo)==ERROR)
{
intUnlock (lockKey);
return ERROR;
}
if(sysIntEnablePIC(intLevel)==ERROR){intUnlock (lockKey); return ERROR;}
intUnlock (lockKey);
return OK;
}
/******************************************************************
0 0 0 0(0x00) Ch B Transmit Buffer Empty
0 0 1 0(0x02) Ch B External/Status Change
0 1 0 0(0x04) Ch B Receive Character Available
0 1 1 0(0x06) Ch B Special Receive Condition
1 0 0 0(0x08) Ch A Transmit Buffer Empty
1 0 1 0(0x0a) Ch A External/Status Change
1 1 0 0(0x0c) Ch A Receive Character Available
1 1 1 0(0x0e) Ch A Special Receive Condition
*
LOCAL void ComIntHandle4(int iPortNo)
{
unsigned char intflag=0,rr1=0;
int lockKey;
char outChar[ComMaxUnit];
char inChar[ComMaxUnit];
int dataAddrA;
int commandAddrA;
int dataAddrB;
int commandAddrB;
int iPortNoA,iPortNoB;
int whilenum=0;
if(iPortNo>=ComMaxUnit)return;
if(iPortNo<1)return;
lockKey = intLock ();
iPortNoA=iPortNo;
iPortNoB=iPortNo-1;
dataAddrA=dataAddr[iPortNoA];
commandAddrA=commandAddr[iPortNoA];
dataAddrB=dataAddr[iPortNoB];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -