Author Topic: USB-MSD with K20FX512  (Read 6374 times)

Offline rjordan

  • Newbie
  • *
  • Posts: 3
    • View Profile
USB-MSD with K20FX512
« on: January 04, 2016, 07:43:06 PM »
I’m trying to get the USB-MSD device serial loader working on a custom board with a MK20FX512VLQ12 processor with an 8 MHz external clock.  I’m having a problem getting the serial loader to operate.
I read through the ?Tasker – Serial Loader User’s Guide and made the following changes.

In config.h:
Defined USB_INTERFACE and USB_MSD_DEVICE_LOADER
Added #define K20F120M to KINETIS_K20

In app_hw_kinetis.h added:
#define EXTERNAL_CLOCK  8000000
#define _EXTERNAL_CLOCK  EXTERNAL_CLOCK
#define CLOCK_DIV  2
#define CLOCK_MUL 30
#define FLEX_CLOCK_DIVIDE 3
#define FLASH_CLOCK_DIVIDE 5
#define USB_CLOCK_SOURCE_MCGPLL0CLK
#define USB_CLOCK_GENERATED_INTERNALLY
and changed PIN_COUNT and PACKAGE_TYPE to match processor

After building and flashing the software on the processor, plugging the USB into a PC, no new mass storage device is displayed.  I figure that I have missed something with the clock settings.  Has anyone had any experience setting up the USB-MSD serial loader on an MK20FX512?

Thank you for your time.

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: USB-MSD with K20FX512
« Reply #1 on: January 04, 2016, 08:56:12 PM »
Hi

I believe that the problem is that you have
#define CLOCK_DIV  2
This means that your 8MHz crystal is divided to 4MHz before the PLL. This is out of specification for the 20FX512, which should have an input reference frequency for the PLL between 8MHz and 16MHz.
It doesn't mean that it won't actually work because out-of-specifiation things may still operate but without guarantie.

If the PLL does work your will have a core speed of 60MHz.
This is however not suitable for USB because it is not possible to derive the required 48MHz USB clock from it (the USB clock can be derived from the core clock using a fractional divider (/1, /1.5, /2.0, /2.5, /3.0 etc. which allows 60MHz, 40MHz, 30MHz,  24MHz etc.) but not 48MHz.

If you use
#define CLOCK_DIV  1
it will keep the input in range and give 120MHz core clock (which can still be divided down for the CPU if needed), but more importantly it allows the USB clock to be derived by using /2.5 (120MHz/2.5 = 48MHz).

Depending on the project version that you use I would expect that the build would have errored with the present setting since it checks that the PLL input is within range.
            #if ((_EXTERNAL_CLOCK/CLOCK_DIV) < 8000000) || ((_EXTERNAL_CLOCK/CLOCK_DIV) > 16000000)
                #error PLL input frequency must be between 8MHz and 16MHz
            #endif


Also, when I ran your setup in the simulator, the USB driver exceptioned because it couldn't set the correct USB clock speed.

In any case this should fix it.

Regards

Mark

P.S. In the present developer's version there is in fact a target for a board with your chip and crystal - see K20FX512_120
The other thing that can go wrong with USB on custom boards is when the VREGIN in not connected to either 3.3V or 5V (this is used to generate the USB transceiver's voltage) and/or the VOUT33 pin doesn't have a suitable capacitor on it; 2.2uF low ESR is required otherwise it won't be stable enough for operation.






Offline rjordan

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: USB-MSD with K20FX512
« Reply #2 on: January 12, 2016, 08:29:31 PM »
Mark,

I changed the clock divisor to 1, but still could not get the serial loader to operate.

I updated to the developer version 1.4 and selected K20FX512_120 as the processor, defined USB_INTERFACE and USB_MSD_DEVICE_LOADER.  I am still having trouble getting the serial loader to operate.

For the clock setting, our board is using an 8MHz external clock, not an oscillator, could this be the reason I'm having trouble getting the serial loader to work?

Thank you for your time;

Randy

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: USB-MSD with K20FX512
« Reply #3 on: January 13, 2016, 01:24:37 AM »
Randy

If you have a clock rather than a crystal the clock initialistion is not exactly the same - I am not sure whether it would still work with an oscillator or not.
I do however suspect that it would have not been able to get past a check that the crystal oscillator has completed its initialisation (but possibly the clock source caused this to pass (?)):

    while ((MCG_S & MCG_S_OSCINIT) == 0) {                               // loop until the crystal source has been selected
     }



The modification to the configuration is however the following:

    #define CRYSTAL_FREQUENCY    8000000                                 // 8 MHz crystal
    #define _EXTERNAL_CLOCK      CRYSTAL_FREQUENCY


    #define EXTERNAL_CLOCK       8000000                                 // 8 MHz clock source
    #define _EXTERNAL_CLOCK      EXTERNAL_CLOCK


If that doesn't work:
- Is the SW operating otherwise? If you have a blink LED connected it should be toggling at exactly 5Hz - this allows verification that it is alive and also that the CPU clock speed is as expected.

Regards

Mark


Offline rjordan

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: USB-MSD with K20FX512
« Reply #4 on: January 13, 2016, 04:36:56 PM »
Mark,

Changing the EXTERNAL_CLOCK and _EXTERNAL_CLOCK #defines did not seem to work.  There is an LED and I set the GREEN_LED to the port and pin for our board (#define GREEN_LED  (PORTD_BIT15) ) and updated TOGGLE_WATCHDOG_LED() with  _TOGGLE_PORT(D, BLINK_LED).  Blink LED is set to GREEN_LED.  The LED did not blink after programming and cycling power.

We have a project built for the board so I know the USB works.  I also verified the proper capacitor are on VOUT33 (2.2uF) and 3.3V on VREGIN that you mentioned earlier.
Is there anything else you can think of that I may have missed?

Thanks,

Randy

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: USB-MSD with K20FX512
« Reply #5 on: January 13, 2016, 05:34:42 PM »
Randy

I can't think of aything else with respect to the clock or USB.
Can you step through the code to see whether the clock initialisation completes? Or try a setup that simply runs from the default internal clock (RUN_FROM_DEFAULT_CLOCK). This will not allow USB to work but it looks like the present problem is more fundamental if the LED is not blinking.

In case the clock initialisation completes but still the board doesn't run the following technique usually identifies what has happened:
- disable the watchdog
- let the board run (it will probably hang in a hard fault due to an invalid acccess somewhere)
- pause the debugger
- In disassembly mode step out of the fault handler to see the code/access that caused the error.
When the code location is known it is often clear what has gone wrong.

Regards

Mark