• AVR Freaks

USB Li-Ion Charger

Page: 12 > Showing page 1 of 2
Author
UK_Sonics
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2005/05/26 12:05:35
  • Status: offline
2008/10/20 05:24:36 (permalink)
0

USB Li-Ion Charger

Hi
 
I am designing a product that has an internal Li-Ion battery and a USB connector.  I need to charge the Li-Ion battery through the USB connector.  One source will the from a AC adapter fitted with a USB connector and the other source will be from a PC USB connector (or USB hub).  Whilst the USB is connected to the PC I would like the ability to communicate with the product. 
 
Li-Ion charger IC's generally have 2 current settings, say 100mA and 500mA. 
 
Before enumeration the PC USB port should capable of providing 100mA, and after enumeration the PC could increase the current to a maximum of 500mA (Depending on the PC USB overall power budget).
 
My first question is, how can the eg PIC on the product know if the request for more current has been sucessful?
 
My second question is, how can the eg PIC on the product know if the USB is connected or the AC adapter has been plugged in??
 
Many thanks in advance for your thoughts,
 
John
#1

21 Replies Related Threads

    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/20 05:43:59 (permalink)
    0
    The USB battery charging spec seems to be quite new but you can try to follow it.
    http://www.usb.org/developers/devclass_docs

    Microchip has the IC MCP73837/73838 which may meet your requirement. But I am not so sure if it is compliant to the above specification.

    Google will have more. Summit Microelectronics SMB339 seems to be a nice one.


      USB_Links and libusb
    #2
    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/20 05:55:52 (permalink)
    0
    If you do not want to be compliant with the USB charger specification (I believe you do not need to since you can declare that your product is not a USB charger as per the definition of the specification).

    But you will still need to obey the USB specification. In that case, the following may be true.
    1) Your device may need to have two configurations --  a low power one and a high power one. You need a driver for that. Initially it enumerates with configuration 1 and draw 100mA. After that the drive will issue a set configuration command and the device will become a high power device and draw up to 500mA. Not so easy, right?

    2) You will need to have some detection circuitry to differentiate the source.


      USB_Links and libusb
    #3
    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    UK_Sonics
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2005/05/26 12:05:35
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/20 07:00:07 (permalink)
    0
    Hi xiaofan, thank you for a super fast response,  still digesting your comments and will post back soon. 
     
    -John
    #5
    chinzei
    Super Member
    • Total Posts : 2250
    • Reward points : 0
    • Joined: 2003/11/07 12:39:02
    • Location: Tokyo, Japan
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/20 09:07:57 (permalink)
    0
    My first question is, how can the eg PIC on the product know if the request for more current has been sucessful?

    As Xiaofan said, it is an ideal way that the device has two configuration set for 500mA and 100mA, and the PC device driver switches the configuration. But it means you have to develop a custom PC device driver on Windows, because Windows built-in device drivers don't have this ability.

    Then, there is a trick to do this switch with existing Windows drivers.
    - Switch the bMaxPower (config desc) on the device side after challenge

    Just after plug-in (power-up), the device exposes bMaxPower of 500mA (value = 250)
    If this setting is accepted by the host PC or hub, the device is enumerated successfully;
    the device receives Set_Configuration to this default config.
    If this setting is not accepted, the device gets bus reset without receiving Set_Configuration. In this case, the device swaps bMaxPower value to 100mA (value=50) at the second Set_Address. 100mA should be always accepted.

    Successful enumeration sequence with 500mA
    - Plug in
    - bus reset
    - Get_Descriptor( DEVICE )
    - bus reset
    - Set_Address
    - Get_Descriptor( DEVICE )
    - Get_Descriptor( CONFIG ) - bMaxPower 500mA
    ...
    - Set_Configuration -- success


    Switch from 500mA to 100mA on failed enumeration
    - Plug in
    - bus reset
    - Get_Descriptor( DEVICE )
    - bus reset
    - Set_Address
    - Get_Descriptor( DEVICE )
    - Get_Descriptor( CONFIG ) - bMaxPower 500mA
    ...
    - bus reset -- failed the first enumeration
    - Get_Descriptor( DEVICE )
    - bus reset
    - Set_Address -- switch bMaxPower to 100mA at the second Set_Address
    - Get_Descriptor( DEVICE )
    - Get_Descriptor( CONFIG ) - bMaxPower 100mA
    ...
    - Set_Configuration -- should succeed

    Tsuneo

    PS.
    In any methods, the charger IC on the device starts with 100mA setting. When the configuration of bMaxPower=500mA succeeds, the charger IC switches to 500mA at Set_Configuration.
    post edited by chinzei - 2008/10/20 09:38:54
    #6
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/22 11:36:07 (permalink)
    0
    I am developing a CDC class sevice that may need 500 mA bus power for a DC-DC converter. The method you show seems like it should work very well. I plan to have one section of the circuit connected to the computer USB, and it will enumerate as a serial port. I will be using a digital isolator (TI ISO7221BD) to connect to a second PIC in an isolated section of the circuit, which will do analog data processing. It will normally be line powered, in which case the higher current will not be needed, but if AC power is not available, I want to be able to power the isolated section with a DC-DC converter, which will need about 2 watts of power from the USB bus.
     
    Thanks for the detailed method of doing this without using a custom driver.
     
    Paul
    #7
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/23 14:14:41 (permalink)
    0
    I am searching the code to determine the best way to perform this switching between two configurations.

    First, I set up a CFG02 in usbdsc.c, with the only change being the change of max power from 50 to 250. I also allocated space for this configuration (as well as the serial number, which I had not done before):

    rom const unsigned char *rom USB_CD_Ptr[]=
    {
       (rom const unsigned char *rom)&cfg01,
       (rom const unsigned char *rom)&cfg01,
       (rom const unsigned char *rom)&cfg02  // Added 10/23/08
    };
    rom const unsigned char *rom USB_SD_Ptr[]=
    {
       (rom const unsigned char *rom)&sd000,
       (rom const unsigned char *rom)&sd001,
       (rom const unsigned char *rom)&sd002,
       (rom const unsigned char *rom)&sd003  // Added 10/23/08
    };
     
    I also made changes in usbdsc.h:

    extern CFG02;

    It appears that the configuration is set up by a variable in SetupPkt in module usb9.c:


    void USBStdSetCfgHandler(void)
        ...
       usb_active_cfg = SetupPkt.bCfgValue;

     

    void USBCheckStdRequest(void)
        ...
           case GET_CFG:
               ctrl_trf_session_owner = MUID_USB9;
               pSrc.bRam = (byte*)&usb_active_cfg;         // Set Source
               usb_stat.ctrl_trf_mem = _RAM;               // Set memory type
               LSB(wCount) = 1;                            // Set data count
               break;

    At this point I am not sure how to set up the second configuration properly. It appears that just setting this value to a 1 or 2 should select the proper configuration (and I think it is reset to zero for a default configuration). I'm not prepared to dig too deeply into the mechanisms of USB operation at this point, but I would like to know the proper way to do this, which seems like it should be simple enough.
     
    Thanks,
     
    Paul
    post edited by PStechPaul - 2008/10/23 14:18:24
    #8
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/23 21:33:08 (permalink)
    0
    I tried to make a new post about changing USB configurations, but it's not working. Website problems?
    #9
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/23 22:04:40 (permalink)
    0
    This was supposed to be in a new post, but there seems to be a problem with that, and the content does apply here:
     
    I have been trying various ways to use two configurations for my USB device as noted in the thread on Li-Ion charger, but I have not had much success. I was able to set up the second CFG02, but I was unable to get the device to re-enumerate as a higher power configuration. My code in usbdsc.c is:
     
    rom USB_DEV_DSC device_dsc=
    {
    ...

        0x02                    // Number of possible configurations *** Was 0x01, 10/23/08
    };

    /* Configuration 2 Descriptor */ //Added 10/23/08 higher current
    CFG02=
    {
        /* Configuration Descriptor */
        sizeof(USB_CFG_DSC),    // Size of this descriptor in bytes
        DSC_CFG,                // CONFIGURATION descriptor type
        sizeof(cfg02),          // Total length of data for this cfg
        2,                      // Number of interfaces in this cfg
        2,                      // Index value of this configuration *** Was 1
        0,                      // Configuration string index
        _DEFAULT | _SELF,     // Attributes, see usbdefs_std_dsc.h
        250,                     // Max power consumption (2X mA)

        /* Interface Descriptor */
        sizeof(USB_INTF_DSC),   // Size of this descriptor in bytes
        DSC_INTF,               // INTERFACE descriptor type
        0,                      // Interface Number
        0,                      // Alternate Setting Number
        1,                      // Number of endpoints in this intf
        COMM_INTF,              // Class code
        ABSTRACT_CONTROL_MODEL, // Subclass code
        V25TER,                 // Protocol code
        0,                      // Interface string index

       /* CDC Class-Specific Descriptors */
        sizeof(USB_CDC_HEADER_FN_DSC),CS_INTERFACE,DSC_FN_HEADER,0x0110,
        sizeof(USB_CDC_ACM_FN_DSC),CS_INTERFACE,DSC_FN_ACM,0x02,
        sizeof(USB_CDC_UNION_FN_DSC),CS_INTERFACE,DSC_FN_UNION,CDC_COMM_INTF_ID,CDC_DATA_INTF_ID,
        sizeof(USB_CDC_CALL_MGT_FN_DSC),CS_INTERFACE,DSC_FN_CALL_MGT,0x00,CDC_DATA_INTF_ID,

        /* Endpoint Descriptor */
        sizeof(USB_EP_DSC),DSC_EP,_EP02_IN,_INT,CDC_INT_EP_SIZE,0x02,

        /* Interface Descriptor */
        sizeof(USB_INTF_DSC),   // Size of this descriptor in bytes
        DSC_INTF,               // INTERFACE descriptor type
        1,                      // Interface Number
        0,                      // Alternate Setting Number
        2,                      // Number of endpoints in this intf
        DATA_INTF,              // Class code
        0,                      // Subclass code
        NO_PROTOCOL,            // Protocol code
        0,                      // Interface string index

        /* Endpoint Descriptors */
        sizeof(USB_EP_DSC),DSC_EP,_EP03_OUT,_BULK,CDC_BULK_OUT_EP_SIZE,0x00,
        sizeof(USB_EP_DSC),DSC_EP,_EP03_IN,_BULK,CDC_BULK_IN_EP_SIZE,0x00
    };

     
    rom const unsigned char *rom USB_CD_Ptr[]=
    {
        (rom const unsigned char *rom)&cfg01,
        (rom const unsigned char *rom)&cfg01,
        (rom const unsigned char *rom)&cfg02  // Added 10/23/08
    };

     
    Then I tried various ways to change the configuration. I used a button press in user.c to set a "highpower" variable to 1, and do a soft detach:
     
    if (sw2==0) {
        mLED_6_On();
     highpower = 1;
     if (usb_active_cfg != 2) {
       USBSoftDetach();
       }
     }

    and then I tried this in USB9.c:
     
    void USBStdSetCfgHandler(void)
    {
        ctrl_trf_session_owner = MUID_USB9;
        mDisableEP1to15();                          // See usbdrv.h
        ClearArray((byte*)&usb_alt_intf,MAX_NUM_INT);
        usb_active_cfg = SetupPkt.bCfgValue;
        if(SetupPkt.bCfgValue == 0)
            usb_device_state = ADDRESS_STATE;
        else
        {
         if (highpower == 1)
           SetupPkt.bCfgValue = 2;

            usb_device_state = CONFIGURED_STATE;
    ......

     
    I set a monitor in the process_io loop to detect if the config was set to 2:
     
     if (usb_active_cfg == 1) {
       mLED_3_On();
       mLED_5_Off(); }
     else if (usb_active_cfg == 2) {
       mLED_3_Off();
       mLED_5_On(); }

    The device seems to set the config to 2 as it re-enumerates, but then it gets set back to 1. I have tried this in bus-powered and self-powered modes, in case the processor was being reset. I don't think this is the right approach, but I can't find any more information or a working example to make this work. I think somehow the host is resetting the configuration.
     
    I'm using files that are labeled Microchip USB C18 Firmware Version 1.0. I don't know if the newer framework is any better or if it has new information that might help. I got started on this project back in April and then just recently got back on it. Any help will be appreciated.
     
    Thanks,
     
    Paul

    #10
    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/23 22:31:29 (permalink)
    0
    Stack V1.0 is a bit too old, you should at least update to V1.3 (bug fixes).

    But your question seems to be different. Just one question, are you using XP or Vista? I do not know whether this is true of not but it is said there are some differences when dealing with device with two configurations for these two.

    Going back to the original idea, is it possible to use generic driver (WinUSB) to do the job of the role of the customer driver?

      USB_Links and libusb
    #11
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/23 23:04:39 (permalink)
    0
    I will try to get the later version of the USB framework. Maybe it will help.
     
    But I found a reference to how multiple configurations are chosen at http://coen.boisestate.edu/Ssmith/BusInt/overheads/USB9.doc.
     
    It seems that the host tries each configuration available and uses the first one that meets the requirements, such as maximum power. I tried making the CFG01 as 500 mA and CFG02 as 100 mA. It enumerated and worked OK on the native port, but raised an error message of excess current when I tried it on the hub, and stopped there. But it did not seem to try the second configuration, which should have worked.
     
    I am using Win XP, but my system will probably need to be compatible with Vista. sad
     
    I hope the transition will not be too painful. I don't really need the dual configuration at this point, but I would like to figure it out for a future application.
     
    Thanks,
     
    Paul
    #12
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/23 23:40:10 (permalink)
    0
    Just a little more information. If I set the default cfg (index 0) to CFG02 (low power), and index 1 to CFG01, and index 2 to CFG02, the device always enumerates as CFG02. But if I set index 0 to CFG01 (high power), and index 1 to CFG02, the device enumerates as CFG01 if it is plugged into a native port. But if I plug it into the hub, I get an error and the device is stopped. But my LED indicators show that the device has switched to CFG02, although it is stuck in the POWERED_STATE.
     
    Maybe there is a timing problem where Windows shuts down the device before allowing the second configuration to be tested?
     
    Paul
    #13
    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/23 23:54:29 (permalink)
    0
    This makes sense. You may need to use a powered hub (a hub with power input). I bought one for carrying out USB IF compliance testing. It is pretty cheap.

      USB_Links and libusb
    #14
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/24 12:46:42 (permalink)
    0
    Yes, a powered hub would be a work-around, but there must be some way for the second low power configuration to be selected and used if not enough power is available. I would like to understand what is going on before giving up on this. I am not actually using the extra power, so the Windows message is not quite correct. I have only requested the additional power, and I expected the host to reject that request and try the low power configuration, which has been shown to work OK on the bus powered hub.
     
    Maybe the new WinUSB driver will work better. Is it available separately from Vista, and can it be used with XP? If so, I could probably copy it to my computer and point to it with the INF file.
     
    Paul
    #15
    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/24 16:31:49 (permalink)
    0

    ORIGINAL: PStechPaul
    Maybe the new WinUSB driver will work better. Is it available separately from Vista, and can it be used with XP? If so, I could probably copy it to my computer and point to it with the INF file.


    It is available to XP as well. You need to install WDK.
    http://www.microsoft.com/whdc/devtools/wdk/wdkpkg.mspx

      USB_Links and libusb
    #16
    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/24 16:51:03 (permalink)
    0
    By the way, when I mentioned WinUSB, my intention is to say that we can use WinUSB as the custom driver for configuration switching. In that case, we do not need to write a Windows driver (which is not easy). I am not so sure how feasible is that.

      USB_Links and libusb
    #17
    janaxelson
    Starting Member
    • Total Posts : 77
    • Reward points : 0
    • Joined: 2006/02/09 18:43:28
    • Location: 0
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/24 17:55:57 (permalink)
    0
    Host drivers aren't required to search for a configuration that will work. A driver may just examine the first configuration and refuse to configure the device if that configuration isn't acceptable.

    The WinUSB driver has a function to select an interface; I don't know if the driver will search among multiple configurations during enumeration.

    Jan Axelson
    www.Lvr.com


    #18
    xiaofan
    Super Member
    • Total Posts : 6247
    • Reward points : 0
    • Joined: 2005/04/14 07:05:25
    • Location: Singapore
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/24 18:25:10 (permalink)
    0
    The current libusb-win32 device driver does not define the configuration and the users can explicitly set the configuration they want to use. So maybe it is another option and it works under Windows 2000/XP/Vista 32 (and maybe even Windows 98SE). But it does not work under Vista 64.

      USB_Links and libusb
    #19
    PStechPaul
    Super Member
    • Total Posts : 2431
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    RE: USB Li-Ion Charger 2008/10/24 19:05:47 (permalink)
    0
    I found a thread where this was discussed, and apparently it does require a custom driver to search for a working configuration:
    https://www.usb.org/phpbb/viewtopic.php?t=13934
     
    The information in my other link was apparently wrong in its assumption that the driver will search for configurations.
     
    I will probably just make two versions of the firmware, for the low power and high power applications. This will not be a major issue. I just wanted to make sure I was not just overlooking something simple, but it appears that this is a rather difficult thing to do. I need to finalize my design decisions and complete the hardware schematic, so I can lay out a PCB and do some real testing. I am trying to do the following:
     
    1. Bus powered USB-to-serial section with PIC18F2450, communicating through a digital isolator to a PIC18F2420 for data acquisition, with a 120 VAC mains supply.
     
    2. Optionally adding a bus-powered hub to the USB section to provide one or two additional low-power ports.
     
    3. Same as option #1, but with high power configuration to power the data acquisition section through a DC-DC converter, with no mains power.
     
    I will try to make the design support all three options. At least now I have a better understanding of what can be done.
     
    Thanks,
     
    Paul
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5