USBThread::USBThread(int* numSignals, QMutex* intLock) : QObject() { //initilize the data queue this->numSignalsLock = intLock; this->numSignals= numSignals; packetLock = new QMutex(); USBtimer = new QTimer(this); USBtimer->setInterval(1); QObject::connect(USBtimer, SIGNAL(timeout()), this, SLOT(GetUSBData()),Qt::UniqueConnection); magDataFile = new QFile("./magData.txt"); magDataFile->open(QIODevice::WriteOnly); magOut = new QDataStream(magDataFile); // serialize the data into the file connected = false; USBtimer->start(); }
/****************************************************************************** * 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