📄 usb.c
字号:
}
*/
/**********************************************************************/
/* This subroutine handles TX events for FIFO0 (endpoint 0) */
/**********************************************************************/
void tx_0(void)
{
unsigned char lim;
txstat=read_usb(TXS0); /*get transmitter status */
#if DEBG ==1
put_char('T');
puthex_char(txstat);
puthex_char(desc_t_idx);
#endif
/*if a transmission has completed successfully, check to see if */
/*we have anything else that needs to go out, otherwise turn the*/
/*receiver back on **********************************************/
if ((txstat & ACK_STAT) && (txstat & TX_DONE))
{
FLUSHTX0; /*flush TX0 and disable */
/*the desc. is sent in pieces; queue another piece if nec. */
if(sts_getdesc)
{
lim=desc_t_idx+8; /*set new max limit */
#if DEBG ==1
put_char('T');
puthex_char(desc_t_idx);
puthex_char(lim);
#endif
/*move the data into the FIFO */
for(; ((desc_t_idx<lim)&&(desc_t_idx<desc_sze)); desc_t_idx++)
get_desc();
#if DEBG == 1
put_char('E');
puthex_char(lim);
puthex_char(desc_sze);
puthex_char(desc_t_idx);
#endif
if(desc_t_idx == (desc_sze -1))
desc_t_idx = 0;
TXEN0_PID; /*enable TX, choose PID */
}
else
{
write_usb(RXC0,RX_EN); /*re-enable the receiver */
}
}
/*otherwise something must have gone wrong with the previous ****/
/*transmission, or we got here somehow we shouldn't have ********/
else
{
}
/*we do this stuff for all tx_0 events **************************/
}
/**********************************************************************/
/* This subroutine handles TX events for FIFO1 (endpoint 1) */
/**********************************************************************/
void tx_1(void)
{
unsigned int gp2021_lim;
unsigned char count;
txstat = read_usb(TXS1);
if((txstat & ACK_STAT) && (txstat & TX_DONE))
{
dta_pid1 = !dta_pid1; // reverse data0 and data1
FLUSHTX1;
if( sts_send_gp2021_TXD1 ) /* we still need to send regs value */
{
gp2021_lim = gp2021_TXD1_idx +64;
count = gp2021_lim <636 ? 64:636-gp2021_TXD1_idx;
bunch_write_usb( TXD1, (unsigned int)(gp2021_reg_buf+gp2021_TXD1_idx), count);
gp2021_TXD1_idx += count;
if( gp2021_TXD1_idx == 636 )
{
gp2021_TXD1_idx = 0;
sts_send_gp2021_TXD1 = 0;
}
TXEN1_PID_NO_TGL;
} // end of if( sts_send_gp2021_TXD1 )
else
{
FLUSHRX1;
write_usb(RXC1, RX_EN);
}
}// end of if((txstat & ACK_STAT) && (txstat & TX_DONE))
}
/**********************************************************************/
/* This subroutine handles TX events for FIFO2 (endpoint 3) */
/**********************************************************************/
void tx_2(void)
{
unsigned int data gp2021_lim ;
unsigned char count;
// put_timestamp();
txstat = read_usb(TXS2);
if((txstat & ACK_STAT) && (txstat & TX_DONE))
{
FLUSHTX2;
if( sts_send_gp2021_TXD23 ) /* we still need to send regs value */
{
// put_char('a');
write_usb(EPC3, 0); // disable TXD2 temporarily
gp2021_lim = gp2021_TXD23_idx + 64;
count = gp2021_lim <ONE_FRM_SIZE ? 64:ONE_FRM_SIZE - gp2021_TXD23_idx;
bunch_write_usb( TXD2, (unsigned int)(gp2021_reg_buf + usb_snd_idx + gp2021_TXD23_idx), count);
gp2021_TXD23_idx += count;
write_usb(TXC2,TX_LAST+TX_EN); // Begin TXD2 transmit, DATA0
if( gp2021_TXD23_idx == ONE_FRM_SIZE )
{
gp2021_TXD23_idx = 0;
sts_send_gp2021_TXD23 = 0;
}
}// end of if( sts_send_gp2021_TXD23 )
else // this transaction is done
{
// put_char('^');
sts_send_first_end = 0;
write_usb(EPC3,0);
// we need disable EPC3 because ONE_FRM_SIZE(352) bytes means 6 packets
// They are :
// 1: DATA0: EPC3
// 2: DATA1: EPC5
// 3: DATA0: EPC3 , gp2021_lim = 192, count = 64
// 4: DATA1: EPC5 , gp2021_lim = 256, count = 64
// 5: DATA0: EPC3 , gp2021_lim = 320, count = 64
// 6: DATA1: EPC5 , gp2021_lim = 352, count = 32
// Program here means the 13th packet is done, we still have one packet in TXD3
// In order for that packet to be transmitted, we must disable EPC3 here.
FLUSHRX2;
write_usb(RXC2, RX_EN);
}
}// end of if((txstat & ACK_STAT) && (txstat & TX_DONE))
else if(!(txstat & ACK_STAT) && (txstat & TX_DONE))
{
write_usb(TXC2,REF+TX_LAST+TX_EN);
}
else
{
}
}
/**********************************************************************/
/* This subroutine handles TX events for FIFO3 (endpoint 5) */
/**********************************************************************/
void tx_3(void)
{
unsigned int data gp2021_lim ;
unsigned char count;
// put_timestamp();
txstat = read_usb(TXS3);
if((txstat & ACK_STAT) && (txstat & TX_DONE))
{
FLUSHTX3;
if( sts_send_gp2021_TXD23 ) /* we still need to send regs value */
{
// put_char('b');
write_usb(EPC3,EP_EN+05); // enable TXD2 again
gp2021_lim = gp2021_TXD23_idx + 64;
count = gp2021_lim <ONE_FRM_SIZE ? 64:ONE_FRM_SIZE - gp2021_TXD23_idx;
bunch_write_usb( TXD3, (unsigned int)(gp2021_reg_buf + usb_snd_idx + gp2021_TXD23_idx), count);
gp2021_TXD23_idx += count;
write_usb(TXC3,TX_TOGL+TX_LAST+TX_EN); // Begin TXD2 transmit, DATA0
if( gp2021_TXD23_idx == ONE_FRM_SIZE )
{
gp2021_TXD23_idx = 0;
sts_send_gp2021_TXD23 = 0;
}
} // end of if( sts_send_gp2021_TXD23 )
else
{
// put_char('*');
sts_send_first_end = 0;
FLUSHRX3;
write_usb(RXC3, RX_EN);
}
} // end of if((txstat & ACK_STAT) && (txstat & TX_DONE))
else if(!(txstat & ACK_STAT) && (txstat & TX_DONE))
{
write_usb(TXC3,REF+TX_TOGL+TX_LAST+TX_EN);
}
else
{
}
}
/**********************************************************************/
/* This subroutine handles OUT NAK events for FIFO0 (endpoint 0) */
/**********************************************************************/
void nak0(void)
{
/*important note: even after servicing a NAK, another NAK */
/*interrupt may occur if another 'OUT' or 'IN' packet comes in */
/*during our NAK service. */
/*if we're currently doing something that requires multiple 'IN'*/
/*transactions, 'OUT' requests will get NAKs because the FIFO is*/
/*busy with the TX data. Since the 'OUT' here means a premature*/
/*end to the previous transfer, just flush the FIFO, disable the*/
/*transmitter, and re-enable the receiver. */
if (sts_getdesc) /*get_descr status stage? */
{
sts_getdesc=0; /*exit get_descr mode */
FLUSHTX0; /*flush TX0 and disable */
write_usb(RXC0,RX_EN); /*re-enable the receiver */
}
/*we do this stuff for all nak0 events **************************/
}
/**********************************************************************/
/* This subroutine handles OUT NAK events for FIFO1 (endpoint 2) */
/**********************************************************************/
void nak1(void)
{
}
/**********************************************************************/
/* This subroutine handles OUT NAK events for FIFO2 (endpoint 4) */
/**********************************************************************/
void nak2(void)
{
}
/**********************************************************************/
/* This subroutine handles OUT NAK events for FIFO3 (endpoint 6) */
/**********************************************************************/
void nak3(void)
{
}
void usb_isr(void) interrupt 0
{
evnt = read_usb(MAEV); /*check the events */
#if DEBG ==1
puthex_char(evnt);
put_char(' ');
#endif
if (evnt & RX_EV)
{
evnt=read_usb(RXEV); /*check the RX events */
if (evnt&RXFIFO0) rx_0(); /*endpoint 0 */
else if (evnt&RXFIFO1) rx_1(); /*endpoint 2 */
else if (evnt&RXFIFO2) rx_2(); /*endpoint 4 */
else if (evnt&RXFIFO3) rx_3(); /*endpoint 6 */
else /*some other RX event */
{
}
}
else if (evnt & TX_EV)
{
evnt=read_usb(TXEV); /*check the TX events */
// puthex_char(evnt);
if (evnt&TXFIFO0) tx_0(); /*endpoint 0 */
else if (evnt&TXFIFO1) tx_1(); /*endpoint 1 */
else if (evnt&TXFIFO2) tx_2(); /*endpoint 3 */
else if (evnt&TXFIFO3) tx_3(); /*endpoint 5 */
else /*some other TX event */
{
}
}
else if (evnt & ALT) usb_alt(); /*alternate event? */
else if (evnt & NAK)
{
evnt=read_usb(NAKEV); /*check the NAK events */
#if DEBG == 1
put_char('N');
puthex_char(evnt);
#endif
if (evnt & NAK_O0) nak0(); /*endpoint 0 */
else if (evnt & NAK_O1) nak1(); /*endpoint 2 */
else if (evnt & NAK_O2) nak2(); /*endpoint 4 */
else if (evnt & NAK_O3) nak3(); /*endpoint 6 */
else /*some other TX event */
{
}
}
else /*spurious event! */
{
}
}
/* function to process data from host */
/* data structure:
address :: description
0 :: flag to transfer data or not
1 :: total length of data needed to write to gp2021
/ 3 :: address to be written
| 4 :: low byte
\ 5 :: high byte
/ . :: ...
| . :: ...
\ . :: ...
*/
/* process data without int enabled */
/* the reason why this function exist is that */
/* to call this funtcion in timer0's int handler */
void process_host_data(void)
{
unsigned char idx, add, high, low;//,i;
/*
for(i=0; i<gp2021_reg_write[1]+1;i++)
gp2021_reg_buf[buf_idx+224+i] = gp2021_reg_write[i];
for(i=(gp2021_reg_write[1]+2);i<128;i++)
gp2021_reg_buf[buf_idx+224+i] = 0x55;
*/
for(idx = 0; idx < gp2021_reg_write[1]; idx++)
{
add = gp2021_reg_write[3*idx+2];
low = gp2021_reg_write[3*idx+3];
high = gp2021_reg_write[3*idx+4];
write_gps_noint( add, low, high);
}
}
/* process to control gp2021 tracking loop */
/* commd: 0 --- stop timer , thus stop reading gp2021
1 --- start timer, thus start reading gp2021
*/
void process_gp2021(char commd)
{
switch(commd){
case 0: ET0 = 0; frm_count = 0; frm_count1 = 0; write_gps(0x7f, 0,0);break;
case 1: ET0 = 1; break;
default: ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -