Add-on OTG port hardware/register setup.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Add-on OTG port hardware/register setup.

3,691 Views
allankliu
Contributor III

I have an open discussion for add-on OTG port for FRDM-KL25Z (including schematics and image). Now I find it is maybe a hardware or register setup related issue.

 

Hardware modification:

connecting D+/D-/GND from KL25Z USB device port,

connecting VBUS from P10 of J9 (refers to FRDM schematics), which comes from P5V_SDA after a diode.

 

Situation:

  1. During reset, no mouse connected, D+/D- is 0V. (The host D+/D- has pull down resistor, which is correct.)
  2. When mouse is connected, D+ is 0V, D- is about 1.6V. (Mouse is LS device, D+ has pull up resistor, Why D+ is 0V? )
  3. The following transaction is hung and always bus timeout.

 

However the LED of mouse is refreshed because of USB reset command from host.

 

Attached are my source, which is clone of KL25Z-SC usb host demo.

 

kl25z.zip => D:\Freescale\Kinetis L Sample Code\kl25_sc_rev5\klxx-sc-baremetal\src\projects

kl25z_build.zip => D:\Freescale\Kinetis L Sample Code\kl25_sc_rev5\klxx-sc-baremetal\build\iar

 

Looking for someone to help me out. Thank you in advance.

Original Attachment has been moved to: kl25otg_build.zip

Original Attachment has been moved to: kl25otg.zip

Tags (2)
0 Kudos
13 Replies

1,961 Views
DerekLau
Contributor IV

For USB LS device, the 1.5K pullup is at D-.

0 Kudos

1,961 Views
allankliu
Contributor III

I use Arduino + UHS (MAX3421E) as a cross check.

If no mouse is connected, D+/D-  are 0.6V. (by pull-down resistors, not fully pull down to ground)

After mouse is connected, During idle, D- is about 2.5V, D+ is 0.9V. During active moving mouse, D- is about 3.1V, D+ is 0.2V.

You are right. D+ is quite low, although not a ground on UHS.

Well, I have no clue why Freescale usb host demo doesn't work and is always timeout.

0 Kudos

1,961 Views
DerekLau
Contributor IV

I suggest to try the USB Stack

USB Stack v4.0.3

0 Kudos

1,961 Views
allankliu
Contributor III

It seems that KL25Z is named as kinects_l2k in USB stack source. I find an empty project inside the USB stack:

D:\Freescale\Freescale USB Stack v4.0.3\Source\Host\examples\hid\mouse\iar_ew\kinetis_l2k\USBH_mouse.eww (EMPTY!)

So I will go on for other examples at following folders:

D:\Freescale\Freescale USB Stack v4.0.3\Source\Host\examples\hid\keyboard_mouse\iar_ew\kinetis_l2k (keyboard or mouse)

D:\Freescale\Freescale USB Stack v4.0.3\Source\OTG\examples\hid\keyboard\iar_ew\kinetis_l2k (only keyboard)

0 Kudos

1,961 Views
DerekLau
Contributor IV

Launch IAR and drag the USBH_Mouse.ewp file into the workspace window.

Remember to change MCG_C2 setting for Freedom board.

0 Kudos

1,961 Views
allankliu
Contributor III

Hi, Derek,

Thanks for your comments and supports on different topics.

The USBH_Mouse.eww is empty, so I use Project | Add exist projects... command to import USBH_Mouse.ewp into workspace.

I changed L2K.c::pll_init()

    /* First FEI must transition to FBE mode */

    //MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_HGO0_MASK | MCG_C2_EREFS0_MASK | MCG_C2_IRCS_MASK;

    MCG_C2 = (MCG_C2_LOCRE0_MASK | MCG_C2_RANGE0(1) | MCG_C2_EREFS0_MASK);

The USBH_mouse can be built, with same result of KL25Z_SC release.

Furthermore, I met anther strange issue. The code can not run in full speed, with or without debugger, it can only be used with step into command.

Load code, launch debugger, reset.

When press "go", it automatically stops at __iar_program_start: (addr, 0x4c50, instruct: 0x46c0)

When press "step over", it steps over _bsp_platform_init(), and stops at __iar_program_start: (addr, 0x4c50, instruct: 0x46c0)

When press "step into", it steps into each functions, if clock is setup, it can run in full speed then, the mouse LED keeps flashing by USB RESET.

Then I narrow down the issue into pll_init(), I found if I step into pll_init() function, it passed and never return to startup code, if I step over pll_init(), it will be reset to startup code, interesting but not good!

Conclusion: The pll_init() function is quite critical, since MCG registers have to be setup according to some procedure, first to FBE, then bala bala....


I also found two blue flags beside _bsp_platform_init() function in EWARM, however I have no idea about it, it isn't a red-dot breakpoints indicator.

I have checked hardware differences between FRDM and TWR boards. They are USB port (VBUS), UART pinout, slightly different on crystal but same 8MHz external clock. Other peripherals like infrared and etc are not important.

I started to doubt if the code has been fully tested in TWR board as well.

Anyway I will sit back and study documentation regarding Freescale USB stack and try to debug khci module.

0 Kudos

1,961 Views
DerekLau
Contributor IV

All MCG_C2 values in pll_init have to be changed.

Try providing 5V supply to the mouse through other supply.

1,961 Views
allankliu
Contributor III

Hi, Derek,

Thank you again for your support. The problem is identified by a legacy debug method, printf().

First I change UART driver to change TX/RX from PTA14/PTA15 to PTA1/PTA2 for FRDM board. This UART0 reveals information via OpenSDA CDC port.

Then I narrow down the issue at the end of khci_kinetis.c::_usb_khci_task() as following:

    print ("event: %02X",khci_event.VALUE);
    if (khci_event.VALUE & KHCI_EVENT_MASK){
        if (khci_event.VALUE & KHCI_EVENT_ATTACH)
           _usb_khci_attach(); printf("event_attach");

        if (khci_event.VALUE & KHCI_EVENT_RESET)
          _usb_khci_reset(); printf("event_reset");

        if (khci_event.VALUE & KHCI_EVENT_DETACH)
          _usb_khci_detach(); printf("event_detach");
    }

Then it comes out:

Even khci_event.VALUE=0x01 (attach), all three operations are executed consequently,

i.e, attach->reset->detach->attach->reset->detach->...... As a result, the USB mouse was reset repeatedly.

I feel it is cause by compiler optimization. So I change my code as following:

    if (khci_event.VALUE & KHCI_EVENT_MASK){
      //printf("check event");
        if (khci_event.VALUE & KHCI_EVENT_ATTACH)
        {
           _usb_khci_attach();
           printf("khci_event:%02X\n",khci_event.VALUE);
           printf("event_attach\n");
        }

        if (khci_event.VALUE & KHCI_EVENT_RESET)
        {
          _usb_khci_reset();
          printf("khci_event:%02X\n",khci_event.VALUE);
          printf("event_reset\n");
        }

        if (khci_event.VALUE & KHCI_EVENT_DETACH)
        {
          _usb_khci_detach();
          printf("khci_event:%02X\n",khci_event.VALUE);
          printf("event_detach\n");
        }
    }

The code looks stupid, I will modify it for better practice.

Anyway, the USB host HID mouse demo works on my FRDM now ! Uula!

So, add-on USB OTG port works, code works. I am going to merge Freescale USB host demo with Google ADK reference design to make FRDM (with OTG) as a mini ADK kit. Can't wait......

1,961 Views
allankliu
Contributor III

Actually it is not a correct and root cause. However changing code did help a lot to prevent going through all events in this function.

BTW, The EWARM version is v6.50.1.4452, 32K Kick start edition.

0 Kudos

1,961 Views
allankliu
Contributor III

Update:

  • If I step through khci.c, the mouse can be detected and run well. The mouse event can be printed out.
  • If I run in full speed, the mouse doesn't work.

It is tricky, then I have to use legacy printf as major debug method......

During step into functions. I found incoming parameters will be changed to other value in host_dev_list.c::usb_dev_list_attach_device(), speed (=1) becomes 0x17 at

dev_instance_ptr->speed = speed;

And then debugger crushed.

0 Kudos

1,961 Views
allankliu
Contributor III

Hi, Derek,

Thanks.

After changing all MCG_C2 values in pll_init(), the code can run in full speed.

And I use extra USB/UART bridge which has 5V output as VBUS source, 5V to USB_KL25Z_VBUS, ground to GND.

I also try to find relationship with VBUS sense in USB module. But MKL25Z has no VBUS pin at all. The VBUS sense is done by ADC in TWR.

With new 5V supply, It doesn't work well so far in full speed, no matter how many times I connect the mouse.

However, I did find once that mouse can work after stepping through low level USB functions. I am trying to narrow down the root cause.

Yours sincerely

Allan K Liu

0 Kudos

1,961 Views
allankliu
Contributor III

Thanks for your comments. I will try the code. Furthermore I will try to manipulate USB OTG register in debugger.

I will update my experiences in community.

0 Kudos

1,961 Views
allankliu
Contributor III

Do you think hardware is correct?

I have neither oscilloscope nor logic-scope at hand. So I can not see what happens on bus, and I have to measure the DC level on D+/D-, which reflects average level on both pins.

D+/D- should have chance to pull high because Differential 0/1 (state K/J) states are mixed on bus, otherwise the bit-stream should be 0x00 or 0xFF.

USB LS device has pull-up resistor on D-, which should pull it to logic high, (1.5K to VCC/VBUS vs 15K to ground), so 1.6V DC is normal.

USB LS device has no pull-up, only pull-down 15K to ground. However, USB host driver should drive D+ to high, D- to low in certain logic state (Differential 1, state K). On average DC level, it should be 1.5~1.6V as well, isn't it?

Both TWR and FRDM have no external pull-up/pull-down resistors, it should be enabled by GPIO or USB transceiver registers. Maybe KL25Z has some registers to test bus fault?

0 Kudos