Exemplo n.º 1
0
/********************************************************************
 *	モニタコマンド受信と実行.
 ********************************************************************
 */
void ProcessIO(void)
{
	// 返答パケットが空であること、かつ、
	// 処理対象の受信データがある.
	if((ToPcRdy == 0) && (!mHIDRxIsBusy())) {

		//受信データがあれば、受信データを受け取る.
		HIDRxReport64((char *)&PacketFromPC);
		PacketToPC.raw[0]=Cmd0;		// CMD ECHOBACK

		//コマンドに対応する処理を呼び出す.
		if(Cmd0 >= HIDASP_PAGE_TX)  {cmd_avrspx();}	// AVRライターコマンド.
		else if(Cmd0 >= PICSPX_SETADRS24){cmd_picspx();}	// PICライターコマンド.
		else if(Cmd0==HIDASP_PEEK) 	{cmd_peek();}	// メモリー読み出し.
		else if(Cmd0==HIDASP_POKE) 	{cmd_poke();}	// メモリー書き込み.
		else if(Cmd0==HIDASP_JMP) 	{cmd_exec();}	// 実行.
		else if(Cmd0==HIDASP_SET_MODE)  {cmd_set_mode();}
		else if(Cmd0==HIDASP_GET_STRING){cmd_get_string();}
		else if(Cmd0==HIDASP_USER_CMD)  {cmd_user_cmd();}
		else if(Cmd0==HIDASP_TEST) 	{cmd_echo();}	// 接続テスト.
	}

	// 必要なら、返答パケットをインタラプト転送(EP1)でホストPCに返却する.
	if( ToPcRdy ) {
		if(!mHIDTxIsBusy()) {
			HIDTxReport64((char *)&PacketToPC);
			ToPcRdy = 0;

			if(poll_mode!=0) {
				if(mHIDRxIsBusy()) {	//コマンドが来ない限り送り続ける.
					make_report();
				}
			}
		}
	}
}
Exemplo n.º 2
0
Arquivo: BusMon.c Projeto: GBert/misc
/******************************************************************************
 * Function:        void ProcessIO
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void) {
    unsigned char n, b, c, y;
    int a;
    // User Application USB tasks

    if (!(isUsbPowered)) //Only generate traffic if NOT connected to USB
    {
        CheckLoadButton();
        CANLoadTX();
        return;
    }

    if ((usb_device_state < CONFIGURED_STATE) || (UCONbits.SUSPND == 1)) return;

    //----- Read USB buffer if it has data from the host -----
    if (HIDRxReport(inbuffer, 64)) // USB receive buffer has data
    {
        LED_RX_ON(); //Turn LED on
        T0CONbits.TMR0ON = 1; //Start timer for TX LED on time
        gTimeout = 0; //Reset timout

        //---- CANmsgs: Check if host has requested CAN messages to be transmited
        switch (inbuffer[u_CANmsgs]) // interpret command
        {
            case 0x00: // No messages are available
                break;

            case 0x01: // Message 1 is available
                GetUSBData(m1_SIDH, 13, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, 13, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x02: // Message 1 and 2 are available
                //Message 1
                GetUSBData(m1_SIDH, m1_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 2
                GetUSBData(m2_SIDH, m2_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x03: // Message 1, 2, and 3 are available
                //Message 1
                GetUSBData(m1_SIDH, m1_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 2
                GetUSBData(m2_SIDH, m2_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 3
                GetUSBData(m3_SIDH, m3_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m3_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x04: //--FUTURE-- Message 1, 2, 3, and 4 are available
                break;

            default: // unrecognized or null command
                ;
        }// END switch: inbuffer[u_CANmsgs]

        //---- CANCTRL: Write to the CANCTRL register if changed
        if (inbuffer[u_CANCTRL] != old_CANCTRL) //If host sent new CANCTRL value
        {
            SPIByteWrite(CANCTRL, inbuffer[u_CANCTRL]); //Write to CANCTRL
            EEPROMBYTEWrite(CANCTRL, inbuffer[u_CANCTRL]);
            EEPROMCRCWrite(0, 128);
            old_CANCTRL = inbuffer[u_CANCTRL]; //
            outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT);
            while ((outbuffer[u_CANSTAT] & 0xE0) != (inbuffer[u_CANCTRL] & 0xE0))//if didn't change modes yet
            {
                outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT);
            }
            UserFlag.USBsend = 1; //Set flag so will send USB message
        }

        //---- SPI: SPI command from host
        if (inbuffer[u_SPI]) //If host sent SPI command (non-zero)
        {
            switch (inbuffer[u_SPI]) {
                case CAN_RESET: //
                    SPIReset();
                    CANInit();

                    break;

                case CAN_READ: //
                    if (!UserFlag.USBQueue) // If previous message is queued
                    {
                        outbuffer[u_SPI] = CAN_READ; //Send back to host
                        outbuffer[u_REG] = inbuffer[u_REG]; //Send back to host
                        outbuffer[u_DATA] = SPIByteRead(inbuffer[u_REG]); //Send back to host
                    }
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    UserFlag.USBQueue = 1; //Indicates msg is queued, but not sent
                    break;

                case CAN_WRITE: //
                    //outbuffer[u_SPI] = 0;        //Send back to host //JM
                    SPIByteWrite(inbuffer[u_REG], inbuffer[u_DATA]);
                    EEPROMBYTEWrite(inbuffer[u_REG], inbuffer[u_DATA]);
                    EEPROMCRCWrite(0, 128);
                    break;

                case CAN_RTS: //
                    SPI_RTS(inbuffer[u_DATA]);
                    break;

                case CAN_RD_STATUS: //
                    outbuffer[u_DATA] = SPIReadStatus();
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    break;
                case FIRMWARE_VER_RD:
                    memmove(&outbuffer[u_STATUS], firmware_version, sizeof (firmware_version));
                    outbuffer[u_STATUS + sizeof (firmware_version)] = 0;
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    break;

                default: // unrecognized or null command
                    ;
            }// END switch: inbuffer[u_SPI]
        }
    }//END if (HIDRxReport(inbuffer, 1)

    //---- Check RXnBF pins and service messages as needed ---
    switch (CheckCANRX()) // Check if CAN message received
    {
        case 0x01: // Message in RXB0 (Msgs in this buffer are Standard)
            SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        case 0x02: // Message in RXB1 (Msgs in this buffer are Extended)
            SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        case 0x03: // Message in RXB0 and RXB1
            SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        default: // unrecognized or null command
            ;
    }// END switch: CheckCANRX()

    //---- The following turns off the TX and RX USB indicator LEDs after some time
    //Inst. cycle = 200 ns; TMR0IF sets every 51 us
    if (INTCONbits.TMR0IF) {
        TimerCounter++;
        if (!TimerCounter) //if rolled over, set flag. User code will handle the rest.
        {
            LED_TX_OFF(); //Turn LED off
            LED_RX_OFF(); //Turn LED off
            T0CONbits.TMR0ON = 0; //Start timer for TX LED on time
            TimerCounter = 0xFE;
            gTimeout = 1; //Reset timout
        }
        INTCONbits.TMR0IF = 0;
    }

    //------ Load USB Data to be transmitted to the host --------
    if (UserFlag.MCP_RXBn | UserFlag.USBsend) {
        if (!mHIDTxIsBusy()) {
            HIDTxReport(outbuffer, 64);

            outbuffer[0] = 0x00; //PKR$$$ Need this??

            UserFlag.MCP_RXBn = 0; //clear flag
            UserFlag.USBsend = 0; //clear flag
            UserFlag.USBQueue = 0; //Clear message queue

            LED_TX_ON(); //Turn LED on
            T0CONbits.TMR0ON = 1; //Start timer for TX LED on time
            gTimeout = 0; //Reset timout

            outbuffer[u_SPI] = 0x00; //clear back to 00h so host doesn't detect "SPI response"
            USB_ptr = 0xFF; //Point to location 0xFF
            outbuffer[u_CANmsgs] = 0x00; //Clear message status
        }
    }
}//end ProcessIO