• AVR Freaks

Hot!USB - HID put computer to sleep/wake up. Get system info

Author
daan.timmer
Super Member
  • Total Posts : 216
  • Reward points : 0
  • Joined: 2007/02/15 01:42:31
  • Location: The Netherlands
  • Status: offline
2009/02/20 17:30:22 (permalink)
0

USB - HID put computer to sleep/wake up. Get system info

Yes, descriptive thread subject, I know ;-).

Someone asked me to help him with a little idea he has and before I say yes I want to know the complicity of it all.

He wants to make a USB-power (led) lamp. That reacts on the state of the computer (on, sleep/standby, off). It should also be possible to put the computer to sleep/turn it off.

Now I know I can use the ACPI stuff for putting the computer to sleep/waking it up. But I was more wondering about retreiving the status of the computer.
Would this be possible? Would I need a backup battery in case the computer turns off the power on the USB bus etc?
Can a device 'know' when the computer is vast a sleep or not?

Ideas are welcome :-)

Embedded Software Engineer.
Also interested in: Artificial Intelligence, Artificial Neuro Systems.
#1

9 Replies Related Threads

    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    RE: USB - HID put computer to sleep/wake up. Get system info 2009/02/21 00:16:54 (permalink)
    0
    From PC to device,
    - When PC goes to Stand-by, the device falls to Suspend.
    - While Suspend, device is allowed to draw 2.5mA from USB VBUS.
    - When PC wakes up from Stand-by, the device gets Resume event (for remote wake-up device).

    Take care of the difference of Stand-by and hibernate.
    While hibernate, USB connection is cut off and VBUS supply may be dropped.


    From device to PC,
    - Remote wake-up event from device wakes up PC from Stand-by.

    - HID Power/Sleep button
    This spec briefly touches to Power and Sleep (Stand-by) button for HID device.
    "Input Device Class Power Management Reference Specification, v. 2.0"
    http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/Inpmspc.rtf


    "ACPI / Power Management - Architecture and Driver Support" on MS WHDC
    http://www.microsoft.com/whdc/system/pnppwr/powermgmt/default.mspx#a

    Tsuneo
    #2
    daan.timmer
    Super Member
    • Total Posts : 216
    • Reward points : 0
    • Joined: 2007/02/15 01:42:31
    • Location: The Netherlands
    • Status: offline
    RE: USB - HID put computer to sleep/wake up. Get system info 2009/03/05 17:24:26 (permalink)
    0
    SO I've just been toying with th eHID Descriptor Tool. Would something like this work?


    USAGE_PAGE (Generic Desktop)    05 01
    USAGE (System Control)    09 80
    COLLECTION (Application)    A1 01
      USAGE_PAGE (Generic Desktop)    05 01
      USAGE_MINIMUM (System Power Down)    19 81
      USAGE_MAXIMUM (System Wake Up)    29 83
      LOGICAL_MINIMUM (0)    15 00
      LOGICAL_MAXIMUM (2)    25 02
      REPORT_SIZE (3)    75 03
      REPORT_COUNT (1)    95 01
      INPUT (Data,Var,Abs)    81 02
    END_COLLECTION    C0


    or, how cute, BUT YOU ARE DOING IT WRONG!!! :P

    Embedded Software Engineer.
    Also interested in: Artificial Intelligence, Artificial Neuro Systems.
    #3
    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    RE: USB - HID put computer to sleep/wake up. Get system info 2009/03/06 02:25:35 (permalink)
    0
    Swap the number for the repost size and count

    REPORT_SIZE (1) 75 01 - bits / usage
    REPORT_COUNT (3) 95 03 - number of usages
    INPUT (Data,Var,Rel,Pref) 81 06


    In the report descriptor, you define three Usages (REPORT_COUNT), System Power Down - System Sleep - System Wake Up. These usages are assigned to each bit (REPORT_SIZE) of the input report, respectively. As these usages are OSC (One Shot Control) usage type, the Input is set to Relative, Preferred state.

    This MS WHDC web page shows similar example of the report descriptor.

    Enhanced Keyboards and Windows
    http://www.microsoft.com/whdc/archive/w2kbd.mspx
    "Second HID Interface Report Descriptor" - the second collection

    In this example, these global items are succeeded from the first collection for audio keys to the second collection.
    Logical Minimum (0), Logical Maximum (1), Report Size (1)

    Then, to rip off just the second collection, you have to add above items to the second collection. Just for single collection, the report ID is deleted.

    Tsuneo
    post edited by chinzei - 2009/03/06 02:45:37
    #4
    daan.timmer
    Super Member
    • Total Posts : 216
    • Reward points : 0
    • Joined: 2007/02/15 01:42:31
    • Location: The Netherlands
    • Status: offline
    RE: USB - HID put computer to sleep/wake up. Get system info 2009/03/06 06:37:26 (permalink)
    0
    getting somewhere, but not quite yet...

    I made the descriptor according to that site you gave, windows recognizes it as a HID-compliant device. It also installed correctly.
    ROM struct{
       BYTE report[HID_RPT01_SIZE];
    }hid_rpt01={
       {
           0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
           0x09, 0x80,                    // USAGE (System Control)
           0xa1, 0x01,                    // COLLECTION (Application)
               0x19, 0x82,                    //   USAGE_MINIMUM (System Sleep)
               0x29, 0x83,                    //   USAGE_MAXIMUM (System Wake Up)
               0x95, 0x02,                    //   REPORT_COUNT (2)
               0x81, 0x06,                    //   INPUT (Data,Var,Rel)
               0x95, 0x06,                    //   REPORT_COUNT (6)
               0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
           0xc0                           // END_COLLECTION
       }
    };/* End Collection,End Collection            */


    usb_config.h:
    #define _USB_CONFIG_VERSION_MAJOR 2
    #define _USB_CONFIG_VERSION_MINOR 0
    #define _USB_CONFIG_VERSION_DOT   0
    #define _USB_CONFIG_VERSION_BUILD 0

    #define USB_USER_DEVICE_DESCRIPTOR &device_dsc
    #define USB_USER_DEVICE_DESCRIPTOR_INCLUDE extern ROM USB_DEVICE_DESCRIPTOR device_dsc

    #define USB_USER_CONFIG_DESCRIPTOR USB_CD_Ptr
    #define USB_USER_CONFIG_DESCRIPTOR_INCLUDE extern ROM BYTE *ROM USB_CD_Ptr[]

    // Supported USB Configurations

    #define USB_SUPPORT_DEVICE

    // Hardware Configuration

    #define USB_PING_PONG_MODE  USB_PING_PONG__FULL_PING_PONG

    // Peripheral Configuration

    #define MY_VID            0x04D8
    #define MY_PID            0x0003
    #define USB_SPEED_OPTION       USB_FULL_SPEED
    #define USB_POLLING
    #define USB_PULLUP_OPTION      USB_PULLUP_ENABLE
    #define USB_TRANSCEIVER_OPTION USB_INTERNAL_TRANSCEIVER
    #define USB_EP0_BUFF_SIZE     8
    #define USB_MAX_NUM_INT       1
    #define USB_MAX_EP_NUMBER 1

    // HID Function Configuration

    #define USB_USE_HID
    #define HID_INTF_ID             0x00
    #define HID_EP                     1
    #define HID_INT_IN_EP_SIZE      1
    #define HID_INT_OUT_EP_SIZE     1
    #define HID_NUM_OF_DSC          1
    #define HID_RPT01_SIZE          19


    and this is called in the ProcessIO(); function:
    unsigned int testje1 = 65000;
    char testje2 = 1;

    void pwrSaveMe(void)
    {
       if(!HIDTxHandleBusy(USBInHandle))
       {
           if((testje1--) && testje2)
           {
               testje2 = 0;
               hid_report_in[0] = 1;
              
               USBInHandle = HIDTxPacket(HID_EP, (BYTE*)hid_report_in,1);
           }
       }
    }


    But, my computer is not going to sleep *-). And I don't know where to look for tbh :P

    Embedded Software Engineer.
    Also interested in: Artificial Intelligence, Artificial Neuro Systems.
    #5
    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    RE: USB - HID put computer to sleep/wake up. Get system info 2009/03/06 07:09:03 (permalink)
    0

    ROM struct{
    BYTE report[HID_RPT01_SIZE];
    }hid_rpt01={
    {
    0x05, 0x01, // USAGE_PAGE (Generic Desktop)
    0x09, 0x80, // USAGE (System Control)
    0xa1, 0x01, // COLLECTION (Application)
    0x19, 0x82, // USAGE_MINIMUM (System Sleep)
    0x29, 0x83, // USAGE_MAXIMUM (System Wake Up)
    0x15, 0x00, // LOGICAL_MINIMUM (0) <---------- Add these three lines
    0x25, 0x01, // LOGICAL_MAXIMUM (1) <----------
    0x75, 0x01, // REPORT_SIZE (1) <----------
    0x95, 0x02, // REPORT_COUNT (2)
    0x81, 0x06, // INPUT (Data,Var,Rel)
    0x95, 0x06, // REPORT_COUNT (6)
    0x81, 0x03, // INPUT (Cnst,Var,Abs)
    0xc0 // END_COLLECTION
    }
    };/* End Collection,End Collection */


    Tsuneo
    #6
    daan.timmer
    Super Member
    • Total Posts : 216
    • Reward points : 0
    • Joined: 2007/02/15 01:42:31
    • Location: The Netherlands
    • Status: offline
    RE: USB - HID put computer to sleep/wake up. Get system info 2009/03/06 07:58:20 (permalink)
    0
    yay, works :-].

    Now only thing that rests is *how do I know when the PC itself is in standby*. Note: it is of course also possible to switch the PC to standby without the use of the PIC. So storing the infromation of *I pushed the button on the pic and thus I assume the PC goes in to standby* can not be done. I really need to get the PC to tell me that the PC is going in standby mode. O.-

    Embedded Software Engineer.
    Also interested in: Artificial Intelligence, Artificial Neuro Systems.
    #7
    GaneshB
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2010/12/11 15:55:02
    • Location: 0
    • Status: offline
    Re:USB - HID put computer to sleep/wake up. Get system info 2010/12/11 16:01:32 (permalink)
    0

    #8
    mariontetardis
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/08/16 03:03:12
    • Location: 0
    • Status: offline
    Re:USB - HID put computer to sleep/wake up. Get system info 2019/08/20 03:29:46 (permalink)
    0
    Hi, i have the problem with implementing composite keyboard + system control device. Have no problem with keyboard but no luck with system control.
    Can u pls give me some advice?
     
    const struct{uint8_t report[HID_RPT02_SIZE];}hid_rpt02=
    {
    {0x05, 0x01, // USAGE_PAGE (Generic Desktop)
    0x09, 0x80, // USAGE (System Control)
    0xa1, 0x01, // COLLECTION (Application)
    0x19, 0x81, // USAGE_MINIMUM (System Power Off)
    0x29, 0x83, // USAGE_MAXIMUM (System Wake Up)
    0x15, 0x00, // LOGICAL_MINIMUM (0)
    0x25, 0x01, // LOGICAL_MAXIMUM (1)
    0x75, 0x01, // REPORT_SIZE (1)
    0x95, 0x03, // REPORT_COUNT (3)
    0x81, 0x06, // INPUT (Data,Var,Rel)
    0x95, 0x05, // REPORT_COUNT (5)
    0x81, 0x03, // INPUT (Cnst,Var,Abs)
    0xc0
    }
    };
    USB_HANDLE handle;
    signed int systemIdleRate;
    bool SendNewReportPacket;


    volatile signed int SOFCounter2;
    signed int TimeDeltaMilliseconds2;
    signed int LocalSOFCount2;
    static signed int OldSOFCount2;
    uint8_t systemReport[3]={0};
    uint8_t oldsystemReport[3]={0};




    void SystemKeyInit (void)
    {
    handle = 0;
    systemIdleRate = 500;
    SendNewReportPacket = false;
    while(OldSOFCount2 != SOFCounter2)
    {
    OldSOFCount2 = SOFCounter2;
    }
    USBEnableEndpoint(HID_EP2, USB_IN_ENABLED|USB_OUT_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
    }

    void SystemKeyCheck (void)
    {
    if (power==1){
    systemReport[0]=1;
    power = 0;
    }
    if (wakeup==1){
    systemReport[1]=1;
    wakeup = 0;
    }
    if (sleep==1){
    systemReport[2]= 1;
    sleep = 0;
    }
    }



    void SystemKeyPress(void)
    {
    //
    //
    if( USBGetDeviceState() < CONFIGURED_STATE )
    {
    return;
    }

    SendNewReportPacket = false;
    while(LocalSOFCount2 != SOFCounter2)
    {
    LocalSOFCount2 = SOFCounter2;
    }


    {
    TimeDeltaMilliseconds2 = (32767 - OldSOFCount2) + LocalSOFCount2;
    }

    if(TimeDeltaMilliseconds2 > 5000)
    {
    OldSOFCount2 = LocalSOFCount2 - 5000;
    }



    if(HIDTxHandleBusy(handle) == false)
    {

    systemReport[0]=0;
    systemReport[1]=0;
    systemReport[2]=0;

    SystemKeyCheck();
    for (int i=0; i<3; i++){
    if(oldsystemReport[i] != systemReport[i])
    {
    SendNewReportPacket = true;
    }
    }

    if(systemIdleRate != 0)
    {
    //Check if the idle rate time limit is met. If so, need to send another HID input report packet to the host
    if(TimeDeltaMilliseconds2 >= systemIdleRate)
    {
    SendNewReportPacket = true;
    }
    }

    if (SendNewReportPacket==true)
    {
    oldsystemReport[0] = systemReport[0];
    oldsystemReport[1] = systemReport[1];
    oldsystemReport[2] = systemReport[2];
    handle = HIDTxPacket (HID_EP2,(uint8_t*)systemReport, sizeof(systemReport));
    OldSOFCount2 = LocalSOFCount2;
    }

    }

    }
    #9
    ric
    Super Member
    • Total Posts : 23584
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re:USB - HID put computer to sleep/wake up. Get system info 2019/08/20 13:39:52 (permalink)
    +1 (1)
    mariontetardis
    Can u pls give me some advice?
    ...

    Yes. Please do NOT hijack other people's topics. Especially ancient ones like this.
    You have your own topic at https://www.microchip.com/forums/m1109501.aspx
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    Jump to:
    © 2019 APG vNext Commercial Version 4.5