//----------------------------------------------------------------- // txt_file_add_token: Add token (IN, OUT, SETUP, PING) //----------------------------------------------------------------- int txt_file_add_token(uint32_t value) { uint8_t pid = usb_get_pid(value); uint8_t device = usb_get_token_device(value); uint8_t endpoint = usb_get_token_endpoint(value); fprintf(_file, "%s Device %d Endpoint %d\n", usb_get_pid_str(pid), device, endpoint); return 0; }
/*""FUNC COMMENT""************************************************************ * ID : * Outline : Set pipe PID_BUF *----------------------------------------------------------------------------- * Include : *----------------------------------------------------------------------------- * Declaration : void usb_set_pid_buf(USB_U16 pipe); *----------------------------------------------------------------------------- * Description : 引数にて指定したパイプの通信可能状態(BUF)にします。 * : *----------------------------------------------------------------------------- * Argument : USB_U16 pipe ; pipe Number *----------------------------------------------------------------------------- * Return Value: void *----------------------------------------------------------------------------- * Notice : * : *""FUNC COMMENT END""*********************************************************/ void usb_set_pid_buf(USB_U16 pipe) { USB_U16 pid; pid = usb_get_pid( pipe ); if( pid == PID_STALL2 ){ /* When it makes the BUF state from the PID=STALL(11) state, */ /* write 01 after writing 00 once. */ usb_set_pid_nak( pipe ); } switch( pipe ) { case PIPE0: USB.DCPCTR.BIT.PID = PID_BUF; break; case PIPE1: USB.PIPE1CTR.BIT.PID = PID_BUF; break; case PIPE2: USB.PIPE2CTR.BIT.PID = PID_BUF; break; case PIPE3: USB.PIPE3CTR.BIT.PID = PID_BUF; break; case PIPE4: USB.PIPE4CTR.BIT.PID = PID_BUF; break; case PIPE5: USB.PIPE5CTR.BIT.PID = PID_BUF; break; case PIPE6: USB.PIPE6CTR.BIT.PID = PID_BUF; break; case PIPE7: USB.PIPE7CTR.BIT.PID = PID_BUF; break; case PIPE8: USB.PIPE8CTR.BIT.PID = PID_BUF; break; case PIPE9: USB.PIPE9CTR.BIT.PID = PID_BUF; break; default: break; } }
//----------------------------------------------------------------- // txt_file_add_data: Add data packet to log //----------------------------------------------------------------- int txt_file_add_data(uint32_t value, uint8_t *data, int length) { uint8_t pid = usb_get_pid(value); int i; fprintf(_file, " %s: Length %d\n", usb_get_pid_str(pid), length-2); fprintf(_file, " "); for (i=0; i<length-2; i++) { fprintf(_file, "%02x ", data[i]); if (!((i+1) & 0xF) || ((i+1) == (length-2))) fprintf(_file, "\n "); } fprintf(_file, "CRC = %02x%02x\n", data[length-2], data[length-1]); return 0; }
/*""FUNC COMMENT""************************************************************ * ID : * Outline : データ送信開始 *----------------------------------------------------------------------------- * Include : *----------------------------------------------------------------------------- * Declaration : int CDC_start_snd( unsigned long size, unsigned char *top ); *----------------------------------------------------------------------------- * Description : バルクIN転送を使用して、引数で指定されたデータサイズ分の送信 * : データをUSBホストに送信します。送信の完了は待たずに呼び出し元に * : 戻ります。 *----------------------------------------------------------------------------- * Argument : unsigned long size : I ; 送信データサイズ * : unsigned char *top : I ; 送信データの先頭アドレス *----------------------------------------------------------------------------- * Return Value: 0 : 送信起動成功 * : -1 : 送信エラー *----------------------------------------------------------------------------- * Notice : *""FUNC COMMENT END""*********************************************************/ int CDC_start_snd( unsigned long size, unsigned char *top ) { USB_U16 buffer; USB_U16 pipe = BULK_IN_PIPE; /* ---- STALL応答の場合はエラーを返す ---- */ buffer = usb_get_pid(pipe); if( ( buffer == PID_STALL ) || (buffer == PID_STALL2) ) { return -1; } /* ---- 前回の受信処理が完了していない場合はエラーを返す ---- */ if( (PipeFlag[pipe] != PIPE_IDLE) && (PipeFlag[pipe] != PIPE_DONE) ){ return -1; } /* ---- 送信開始(バルクIN転送を許可) ---- */ PipeFlag[pipe] = PIPE_WAIT; usb_start_send_transfer(pipe, size, top); return 0; }
/*""FUNC COMMENT""************************************************************ * ID : * Outline : データ受信開始 *----------------------------------------------------------------------------- * Include : *----------------------------------------------------------------------------- * Declaration : int CDC_start_rcv( unsigned long size, unsigned char *buf ); *----------------------------------------------------------------------------- * Description : バルクOUT転送を使用して、引数で指定されたデータサイズ分の受信 * : データをUSBホストから受信します。受信の完了は待たずに呼び出し元に * : 戻ります。 *----------------------------------------------------------------------------- * Argument : unsigned long size : I ; 受信データサイズ * : unsigned char *buf : O ; 受信バッファの先頭アドレス *----------------------------------------------------------------------------- * Return Value: 0 : 受信起動成功 * : -1 : 受信エラー *----------------------------------------------------------------------------- * Notice : *""FUNC COMMENT END""*********************************************************/ int CDC_start_rcv( unsigned long size, unsigned char *buf ) { USB_U16 buffer; USB_U16 pipe = BULK_OUT_PIPE; /* ---- STALL応答の場合はエラーを返す ---- */ buffer = usb_get_pid(pipe); if( ( buffer == PID_STALL ) || (buffer == PID_STALL2) ) { // BLUELED_on(); return -1; } /* ---- 前回の受信処理が完了していない場合はエラーを返す ---- */ if( (PipeFlag[pipe] != PIPE_IDLE) && (PipeFlag[pipe] != PIPE_DONE) ){ return -1; } // WHITELED_on(); /* ---- 受信開始(バルクOUT転送を許可) ---- */ PipeFlag[pipe] = PIPE_WAIT; usb_start_recieve_transfer( pipe, size, buf ); return 0; }
//----------------------------------------------------------------- // txt_file_add_handshake: Add handshake (ACK, NAK, NYET) //----------------------------------------------------------------- int txt_file_add_handshake(uint32_t value) { uint8_t pid = usb_get_pid(value); fprintf(_file, " %s\n", usb_get_pid_str(pid)); return 0; }
/*""FUNC COMMENT""************************************************************ * ID : * Outline : Set pipe PID_TALL *----------------------------------------------------------------------------- * Include : *----------------------------------------------------------------------------- * Declaration : void SetTALL(USB_U16 pipe); *----------------------------------------------------------------------------- * Description : 引数にて指定したパイプの通信禁止状態(STALL)にします。 * : *----------------------------------------------------------------------------- * Argument : USB_U16 pipe ; pipe Number *----------------------------------------------------------------------------- * Return Value: void *----------------------------------------------------------------------------- * Notice : * : *""FUNC COMMENT END""*********************************************************/ void usb_set_pid_stall(USB_U16 pipe) { USB_U16 pid; pid = usb_get_pid( pipe ); if( pid == PID_BUF ){ /* When it makes the STALL state from the PID=STALL(01) state, */ /* write 11. */ switch( pipe ) { case PIPE0: USB.DCPCTR.BIT.PID = PID_STALL2; break; case PIPE1: USB.PIPE1CTR.BIT.PID = PID_STALL2; break; case PIPE2: USB.PIPE2CTR.BIT.PID = PID_STALL2; break; case PIPE3: USB.PIPE3CTR.BIT.PID = PID_STALL2; break; case PIPE4: USB.PIPE4CTR.BIT.PID = PID_STALL2; break; case PIPE5: USB.PIPE5CTR.BIT.PID = PID_STALL2; break; case PIPE6: USB.PIPE6CTR.BIT.PID = PID_STALL2; break; case PIPE7: USB.PIPE7CTR.BIT.PID = PID_STALL2; break; case PIPE8: USB.PIPE8CTR.BIT.PID = PID_STALL2; break; case PIPE9: USB.PIPE9CTR.BIT.PID = PID_STALL2; break; default: break; } } else{ switch( pipe ) { case PIPE0: USB.DCPCTR.BIT.PID = PID_STALL; break; case PIPE1: USB.PIPE1CTR.BIT.PID = PID_STALL; break; case PIPE2: USB.PIPE2CTR.BIT.PID = PID_STALL; break; case PIPE3: USB.PIPE3CTR.BIT.PID = PID_STALL; break; case PIPE4: USB.PIPE4CTR.BIT.PID = PID_STALL; break; case PIPE5: USB.PIPE5CTR.BIT.PID = PID_STALL; break; case PIPE6: USB.PIPE6CTR.BIT.PID = PID_STALL; break; case PIPE7: USB.PIPE7CTR.BIT.PID = PID_STALL; break; case PIPE8: USB.PIPE8CTR.BIT.PID = PID_STALL; break; case PIPE9: USB.PIPE9CTR.BIT.PID = PID_STALL; break; default: break; } } }
/*""FUNC COMMENT""************************************************************ * ID : * Outline : Set pipe PID_NAK *----------------------------------------------------------------------------- * Include : *----------------------------------------------------------------------------- * Declaration : void usb_set_pid_nak(USB_U16 pipe); *----------------------------------------------------------------------------- * Description : 引数にて指定したパイプの通信禁止状態(NAK)にします。 * : 本関数実行前のパイプ状態が通信可能状態(BUF)である場合は、 * : 通信禁止設定後、パイプがレディーとなるまでソフトウェアにて待ちます。 * : *----------------------------------------------------------------------------- * Argument : USB_U16 pipe ; pipe Number *----------------------------------------------------------------------------- * Return Value: void *----------------------------------------------------------------------------- * Notice : * : *""FUNC COMMENT END""*********************************************************/ void usb_set_pid_nak(USB_U16 pipe) { USB_U16 pid, pbusy; pid = usb_get_pid( pipe ); if( pid == PID_STALL2 ){ /* When it makes the NAK state from the PID=STALL(11) state, */ /* write 00 after writing 10 once. */ usb_set_pid_stall( pipe ); } switch( pipe ) { case PIPE0: USB.DCPCTR.BIT.PID = PID_NAK; break; case PIPE1: USB.PIPE1CTR.BIT.PID = PID_NAK; break; case PIPE2: USB.PIPE2CTR.BIT.PID = PID_NAK; break; case PIPE3: USB.PIPE3CTR.BIT.PID = PID_NAK; break; case PIPE4: USB.PIPE4CTR.BIT.PID = PID_NAK; break; case PIPE5: USB.PIPE5CTR.BIT.PID = PID_NAK; break; case PIPE6: USB.PIPE6CTR.BIT.PID = PID_NAK; break; case PIPE7: USB.PIPE7CTR.BIT.PID = PID_NAK; break; case PIPE8: USB.PIPE8CTR.BIT.PID = PID_NAK; break; case PIPE9: USB.PIPE9CTR.BIT.PID = PID_NAK; break; default: break; } if( pid == PID_BUF ){ do { switch( pipe ) { case PIPE0: pbusy = USB.DCPCTR.BIT.PBUSY; break; case PIPE1: pbusy = USB.PIPE1CTR.BIT.PBUSY; break; case PIPE2: pbusy = USB.PIPE2CTR.BIT.PBUSY; break; case PIPE3: pbusy = USB.PIPE3CTR.BIT.PBUSY; break; case PIPE4: pbusy = USB.PIPE4CTR.BIT.PBUSY; break; case PIPE5: pbusy = USB.PIPE5CTR.BIT.PBUSY; break; case PIPE6: pbusy = USB.PIPE6CTR.BIT.PBUSY; break; case PIPE7: pbusy = USB.PIPE7CTR.BIT.PBUSY; break; case PIPE8: pbusy = USB.PIPE8CTR.BIT.PBUSY; break; case PIPE9: pbusy = USB.PIPE9CTR.BIT.PBUSY; break; default: pbusy = 1; break; } } while( pbusy == 1 ); } }