📄 user_generic_hid.c
字号:
}
}//end ReportLoopback
/******************************************************************************
* Function: void HandleControlOutReport(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Checks to see if an Output or Feature report has arrived
* on the control pipe. If yes, extracts and uses the data.
*
* Note: None
*****************************************************************************/
void HandleControlOutReport(void)
{
byte count;
ClrWdt();
// Find out if an Output or Feature report has arrived on the control pipe.
// Get the report type from the Setup packet.
switch (MSB(SetupPkt.W_Value))
{
case 0x02: // Output report
// Get the report ID from the Setup packet.
switch(LSB(SetupPkt.W_Value))
{
case 0: // Report ID 0
// This example application copies the Output report data
// to hid_report_in.
// (Assumes Input and Output reports are the same length.)
// A "real" application would do something more useful with the data.
// wCount holds the number of bytes read in the Data stage.
// This example assumes the report fits in one transaction.
for (count = 1; count <= HID_OUTPUT_REPORT_BYTES; count = count + 1)
{
hid_report_in[count-1] = hid_report_out[count-1];
}
// The number of bytes in the report (from usbcfg.h).
break;
} // end switch(LSB(SetupPkt.W_Value))
case 0x03: // Feature report
// Get the report ID from the Setup packet.
switch(LSB(SetupPkt.W_Value))
{
case 0: // Report ID 0
// The Feature report data is in hid_report_feature.
// This example application just sends the data back in the next
// Get_Report request for a Feature report.
// wCount holds the number of bytes read in the Data stage.
// This example assumes the report fits in one transaction.
break;
} // end switch(LSB(SetupPkt.W_Value))
} // end switch(MSB(SetupPkt.W_Value))
} // end HandleControlOutReport
/******************************************************************************
* Function: void BlinkUSBStatus(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: BlinkUSBStatus turns on and off LEDs corresponding to
* the USB device state.
*
* Note: mLED macros can be found in io_cfg.h
* usb_device_state is declared in usbmmap.c and is modified
* in usbdrv.c, usbctrltrf.c, and usb9.c
*****************************************************************************/
void BlinkUSBStatus(void)
{
static word led_count=0;
ClrWdt();
if(led_count == 0)led_count = 10000U;
led_count--;
#define mLED_Both_Off() {mLED_1_Off();mLED_2_Off();}
#define mLED_Both_On() {mLED_1_On();mLED_2_On();}
#define mLED_Only_1_On() {mLED_1_On();mLED_2_Off();}
#define mLED_Only_2_On() {mLED_1_Off();mLED_2_On();}
if(UCONbits.SUSPND == 1)
{
if(led_count==0)
{
mLED_1_Toggle();
mLED_2 = mLED_1; // Both blink at the same time
}//end if
}
else
{
if(usb_device_state == DETACHED_STATE)
{
mLED_Both_Off();
}
else if(usb_device_state == ATTACHED_STATE)
{
mLED_Both_On();
}
else if(usb_device_state == POWERED_STATE)
{
mLED_Only_1_On();
}
else if(usb_device_state == DEFAULT_STATE)
{
mLED_Only_2_On();
}
else if(usb_device_state == ADDRESS_STATE)
{
if(led_count == 0)
{
mLED_1_Toggle();
mLED_2_Off();
}//end if
}
else if(usb_device_state == CONFIGURED_STATE)
{
if(led_count==0)
{
mLED_1_Toggle();
mLED_2 = !mLED_1; // Alternate blink
}//end if
}//end if(...)
}//end if(UCONbits.SUSPND...)
}//end BlinkUSBStatus
/******************************************************************************
* Function: void GetInputReport0(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Prepares to send the data in hid_report_out in the Data
* stage of the current Get_Report transfer.
*
* Note: None
*****************************************************************************/
void GetInputReport0(void)
{
byte count;
// Set pSrc.bRam to point to the report.
//
pSrc.bRam = (byte*)&hid_report_in;
// The number of bytes in the report (from usbcfg.h).
LSB(wCount) = HID_INPUT_REPORT_BYTES;
} // end GetInputReport0
/******************************************************************************
* Function: void GetFeatureReport0(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Prepares to send the data in hid_report_feature in the Data
* stage of the current Get_Report transfer.
*
* Note: None
*****************************************************************************/
void GetFeatureReport0(void)
{
byte count;
// Set pSrc.bRam to point to the report.
pSrc.bRam = (byte*)&hid_report_feature;
// The number of bytes in the report (from usbcfg.h).
LSB(wCount) = HID_FEATURE_REPORT_BYTES;
} // end GetFeatureReport0
void ReceiveControllerStatus()
{
// Gerneral Packet: 1 byte cmd;1 byte length;3 bytes address; (64-5) bytes data
// int i; for(i=2;i<2+dataPacket.len;i++) // for furture use
unsigned char RcvCount,RcvBuffer,AddrRcvd,count,i;
word DelayCountL,DelayCountH;
DelayCountL=0;
DelayCountH=0;
RcvCount=0; // 接受数据记数器
RcvBuffer=0; // 接受缓冲区
AddrRcvd=0; // 地址收到标志,Address Reveived
count=0;
i=0;
// counter=0x02+dataPacket.len; // here we treat address and data as data section
// Current Packet:
// Packet Size : NULL : Power Status : MCU pulse : GPU pulse
// 1 byte 1 byte 1 byte 1 byte 1 byte
// O2 chroma : pressure : humidity : temperature : CO2 chroma : flow
// 1 byte 1 byte 1 byte 1 byte 2 bytes 2 bytes
// terminate string: 55AA
ClrWdt();
for(count=0;count<=dataPacketMAXSIZE;count++) dataPacket[count]=0xFF;
dataPacket[0]=18;
dataPacket[1]= READ_STATUS;
dataPacket[USB_FSP+0]=check_power();
dataPacket[USB_FSP+1]=check_MCU();
dataPacket[USB_FSP+2]=check_GPU();
/* read data from I2C and save in dataPacket*/
// I2C's slave address :8AH
// DataPacket Format
// O2 chroma : pressure : humidity : temperature : CO2 chroma : flow
// 1 byte 1 byte 1 byte 1 byte 2 bytes 2 bytes
//*
SDA_DIR=INPUT_PIN; // SDA in slave mode
SCL_DIR=INPUT_PIN; // SCL in salve mode
SSPCON1=0B00110110;
SSPADD=0x8A;
PIR1bits.SSPIF=0;
PIE1bits.SSPIE=0;
while(RcvCount<=I2C_Data_Length)
{
DelayCountL++;
if(DelayCountL==0) DelayCountH++;
if (DelayCountH==10) // change DelayCountH to inlarge query time
{
RcvCount=I2C_Data_Length+1;
break;
}
while (PIR1bits.SSPIF==1) // 查询I2C中断标志
{
DelayCountH=0;
RcvBuffer=SSPBUF;
if (AddrRcvd==0)
{ if (RcvBuffer=0x8A) //0x8A 是I2C的从机地址
{
AddrRcvd=1;
PIR1bits.SSPIF=0;
break;
}
}
else // AddrRcvd==1,说明收到了地址信息,接下收到的就是数据了!
{
dataPacket[USB_FSP+3+RcvCount]=SSPBUF;
RcvCount+=1;
PIR1bits.SSPIF=0;
}
}
}
SSPCON1bits.SSPEN=0; // 关闭I2C使能标志
dataPacket[USB_FSP+3+I2C_Data_Length+0]=0x55;
dataPacket[USB_FSP+3+I2C_Data_Length+1]=0xAA; // 55AA 是数据包结束标志
dataPacket[16]=0x44;
dataPacket[17]=0x33;
//*/
for(count=0;count<dataPacket[0];count+=HID_INPUT_REPORT_BYTES)
{
for(i=0;i<HID_INPUT_REPORT_BYTES;i++)
transmit_buffer[i]=dataPacket[count+i];
while(mHIDTxIsBusy()) USBDriverService();
HIDTxReport(transmit_buffer, HID_INPUT_REPORT_BYTES);
}
} // end ReceiveControllerStatus
unsigned char check_power(void)
{
ADCON1 |= 0x0F;
TRISAbits.TRISA0=IN;
if(PORTAbits.RA0==1)
return (unsigned char)POWER_OK;
else
return (unsigned char)POWER_OFF;
}
unsigned char check_MCU(void)
{
int i=0,j=0,okCounter=0;
MCU_STATUS_DIR=IN;
INTCON3bits.INT2IE=0;
INTCON3bits.INT2IF=0;
INTCONbits.GIEH=0;
INTCONbits.GIEL=0;
for(i=0;i<=255;i++){
for(j=0;j<=255;j++){
if( INTCON3bits.INT2IF==1){
if(okCounter>=20)
okCounter=20;
else
okCounter++;
INTCON3bits.INT2IF=0;
}
ClrWdt();
}
}
return (unsigned char) okCounter;
}
unsigned char check_GPU(void)
{
int i=0,j=0;
unsigned char okCounter=0;
GPU_STATUS_DIR=IN;
//INTCON2bits.NOT_RBPU=0; //太怪了,如果加上这句,check_MCU和check_GPU函数都出错.
INTCONbits.RBIE=0;
INTCONbits.RBIF=0;
INTCONbits.GIEH=0;
INTCONbits.GIEL=0;
for(i=0;i<=255;i++){
for(j=0;j<=255;j++){
if( INTCONbits.RBIF==1){
if(PORTBbits.RB5==1){
if(okCounter>=20)
{okCounter=20;}
else
{okCounter++;}
}
PORTBbits.RB5=0;
INTCONbits.RBIF=0;
}
ClrWdt();
}
}
return okCounter;
}
/** EOF user_generic_hid.c *********************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -