I have an issue with making usb_stack working on my MCU.
I am using kinetis K22FN1M MCU. The PCB is made by myself. I have used the usb_core code from kinetis SDK and eveyrhing seems to be ok.
I am trying to create an MSD class device.
I was trying to do some debugging and I found out that after the line USB_CLK_RECOVER_IRC_EN=0x3 (device init stage) the MCU goes into HardFault interrupt. So, the stack does not working. If I try to comment the line mention above then the device initializes, but of course does not work as it should (even though windows detects a new device on the bus). Are there any ideas what to do? I suspect that there is some issues with clocks....
Second issue may be somehow connected, I found out that during startup MCU does not want to switch to HSRUN mode. It remains in RUN mode.
I found out that PMPROT is 0x2A. I was trying to change it to 0xAA but it cannot be changed... remains 0x2A. So, in startup file I had to comment this functionality because otherwise MCU will be in infinite loop waiting for MCU to go to HSRUN. How to fix the PMPROT register to be able to write there?
Waiting for any help.
May I disturb you once again?
I am still trying to make kinetis usb stack work on MK22FN1M0.
In usb_dev_bsp.c I set CLOCK_SYS_SetUsbfsDiv(i, 4U, 1U); so, 120 * (1+1) / (4 + 1) = 48Mhz.
So, I can get interrupts.
I also disabled MPU. But as soon as I disable MPU i get different Bus and Usage fault interrupts, in different places... even though the code seems to be ok. I cannot understand what is the reason.
If I enable MPU, no iterrupts but in MPU register I can see fauld in slave port 1.
Well... found out an answer....
My CPU is MK22FN1M0VLH12, which is not fucking the MK22FN512VLH12 which is used in dev. boards and widely used in KSDK.
That means that there is not only flash size difference.... but also:
>>> MK22FN1M0VLH12 cannot run in HSRUN mode.... so the startup in SDK will not work... needs modifications to remove the high speed run mode part. Also, my pcb does not contain 32k RTC oscillator, so I had to comment out the piece of code that sets up RTC.
>>> MK22FN1M0VLH12 does not have internal, so one cannot use IRC48 in USB stack!!! I tried to set "#define BSPCFG_USB_USE_IRC48M (0)" in usb_dev_bsp.c, but that does not really help.. It seems that windows detects device, but it does no go any further but to send RESET command to device.
So, here I am stuck.... Any suggestions? How can I troubleshoot it?
Since the K22FN1M-120 doesn't have support for the crystal-less operation it requires an external crystal to derive the accurate USB clock from (as you know).
But apart from that there should only be about two lines of code difference for the USB between this part and the K22FN512-120:
- disable the MPU (rather than not control the MPU)
- set SIM_SOPT2 to generate the correct frequency (rather than select the IRC48M and command it to start) - this is optional because also the K22FN512-120 can derive its clock from the crystal.
For other parts of the device there are also a few other differences (like Flash granuarity) but generally quite easy to handle.
I use USB-MSD on both of the parts without issues in the uTasker project, which automatically adapts itself to the the device's details and can switch any project between them by setting a single define.
Can you help me with the issue below?
I am still having problems with the USB and desperately trying to find out what is going on... hopeless... As I wrote in the thread if disable MPU I get strange interrupts... I do not get them only if USB cable is disconnected... that means that USB part performs something strange...
If I can run your code on a K21F120 tower board I can possibly tell you what is going wrong (in this case please post your binary file together with a MAP file of the project).
Alternative I have attached a binary file that may operate on your board (it assumes an 8MHz crystal). The USB enumerates as composite USB-CDC and USB-MSD with two LUMs (it will use the SD card - assumed to be connected to the SDHC - as well as the second half of internal Flash as second disk medium.
The USB driver (needed because of the CDC on interface 1) can be downloaded from http://www.uTasker.com/temp/uTaskerFreescale_MSD_VirtualCOM.zip
Assuming you can use the USB-CDC there are various menus allowing further oerations. The SD card can be worked with in the Disk Interface menu (on the Virtual COM port) as disk D and the internal Flash as disk E. The internal Flash will need to be formatted using the "format" command and then it will be seen by the PC via USB-MSD at the same time as the SD card. If you use WIndows 8.1 you can also use the "dirh" (directory display including hidden volumes) to see the hidden system directory that Windows 8.1 writes to it when first connected!
Should this not match your board you can also tell me what crystal it has instead since I can build for most combinations and simulate them. Below is the simulation of the K21F120 board as reference. The USB stack and drivers are HW independent and so don't suffer from any porting difficulties (operation on all KL and K parts is ensured) and the USD device support is extremely flexible since it allows combining classes into composite device with no effort at all (one just selects which classes are to be used and the number of CDC interfaces, when included, and the project adapts itself automatically).
P.S. The present uTasker project release doesn't have the K22 support in it but a new version will be available at the beginning of 2015 with many new features and with support for most new Freescale parts and boards.
Hi Mark, !
I attached the files (also sources in Keil and IAR, and sources for SDK usb stack... As i mentioned, I changed a file there). I configured UART0 at PORTB16/PORTB17.
I also looked at your binary, but it doesn't work at all.... Meaning no USB device regignized... when I look with debugger then I see the following:
WARNING: Target system has been power-cycled
Info: TotalIRLen = 4, IRPrint = 0x01
Info: TotalIRLen = 4, IRPrint = 0x01
Info: Found Cortex-M4 r0p1, Little endian.
Info: FPUnit: 6 code (BP) slots and 2 literal slots
Info: TPIU fitted.
Info: ETM fitted.
Info: ETB present.
Info: CSTF present.
PC = 0000ADB4, CycleCnt = 00000021
R0 = 00000000, R1 = 00000000, R2 = 00000000, R3 = 00000000
R4 = 00000000, R5 = 00000000, R6 = 00000000, R7 = 00000000
R8 = 00000000, R9 = 00000000, R10= 00000000, R11= 00000000
SP(R13)= 2000FFF4, MSP= 2000FFF4, PSP= 00000000, R14(LR) = FFFFFFFF
XPSR = 61000000: APSR = nZCvq, EPSR = 01000000, IPSR = 000 (NoException)
CFBP = 00000000, CONTROL = 00, FAULTMASK = 00, BASEPRI = 00, PRIMASK = 00
FPU regs: FPU not enabled / not implemented on connected CPU.
The binary that you posted looks to run on the TWR-K21F120M since the USB enumerates as CDC class. Therefore the code is compatible with the K21FN1M0, which is what I expect for the K22FN1M using just the USB and the processor clocked from an 8MHz crystal source.
I can't explain why the binary that I posted doesn't look to operate - the address that the debugger shows is the entry point main() - but it is also using low power modes (although I don't know of any problems with the modes on this device - as reference, devices with errate e7166 can't use USB together with the WAIT mode). In case the code is basically operating you would be able to see that the port PTD4 toggles as 2.5Hz. The attached version is the same but I removed the low power mode in case that was affecting anything.
I don't know whether this gives any clues or helps in any way (?)
Thanks for giving it a try.
Case is closed, my PCB is wrong somehow... I made a new one (same schem.), the code works great on that one. and the MCU on old one is burned somehow.. VCC and VSS are shorted now...:smileyhappy:
Can you advise why it is necessary to disable MPU for USB?
Thanks for the hints! It seems that I could use USB w/o external 48MHz oscillator. Can you point me to the latest source code of your uTasker project, so I can use it as a reference?
K22FN1NM0 has significant differences with K22FN512, check the next thread and document:
As you can see, porting the stack from one device to the other is a pain. While K22FN1M0 is supported by KSDK, you may start downloading the TWR-K21F120M sample code from the next link (click on Get Started with TWR-K21F120M and the IAR Embedded Workbench):
This sample code package includes USB host and device examples that fit your K22FN1M0.
Note: If this post answers your question, please click the Correct Answer button. Thank you!
Thanks a lot for the answer! I downloaded the project, complied and uploaded to my MCU and it worked like a charm! I now have a Freescale CDC device working.
That was like a Christmas present for me :smileyhappy:!