AnsweredAssumed Answered

USB device Problem

Question asked by Shreeram Muley on Jan 12, 2017
Latest reply on Jan 17, 2017 by Bob Paddock

Hi, I am using kinetis Mk64fx microcontroller in a custom board. I am facing problem while writing a baremetal code for usb device driver. I think USB enumeration process failed and it is unable to load drivers. Below is an usb init process along with SIM and MCG reg configuration.

#define USB0_CTL_VALUE_1 0x02U
#define USB0_CTL_MASK_1 0x02U
#define USB0_CLK_RECOVER_IRC_EN_VALUE 0x03U
#define USB0_CLK_RECOVER_IRC_EN_MASK 0x03U
#define USB0_CLK_RECOVER_CTRL_VALUE 0x00U
#define USB0_CLK_RECOVER_CTRL_MASK 0xE0U


static void USB0_CommonInit(void)
{

/* SIM_SOPT2: USBSRC=1 */
//SIM_SOPT2 |= SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL(1); // Selecting PLL and PLLFLLSEL PLL
SIM_SOPT2 |= SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL(3); // Selecting IRC48 and PLLFLLSEL IRC48
SIM_CLKDIV2 |= SIM_CLKDIV2_USBDIV(1); //USB Division 1
SIM_CLKDIV2 |= SIM_CLKDIV2_USBFRAC_MASK; // USB Fraction 1
/* SIM_SCGC4: USBOTG=1 */
SIM_SCGC4 |= SIM_SCGC4_USBOTG_MASK; // Enabling usb OTG module
/* NVICIP53: PRI53=0 */
// NVICIP53 = NVIC_IP_PRI53(0x00); // setting up interrupt priority

}

uint8 usb_init(uint8 ip)
{
USB0_CommonInit();

SIM_SOPT1 |= SIM_SOPT1_USBREGEN_MASK; //Enable usb regulator
USB0_USBTRC0 |= USB_USBTRC0_USBRESET_MASK; // Reset module

// Base Descriptor Table
MX64_USB_BDT_PAGE_01 = (hcc_u8)((((hcc_u32)BDT_BASE) >> 0x08));
MX64_USB_BDT_PAGE_02 = (hcc_u8)((((hcc_u32)BDT_BASE) >> 0x16));
MX64_USB_BDT_PAGE_03 = (hcc_u8)((((hcc_u32)BDT_BASE) >> 0x24));

USB0_OTGCTL = USB_OTGCTL_OTGEN_MASK; // OTG Module Enable
USB0_OTGICR |= USB_OTGICR_ONEMSECEN_MASK; // Enable one mili sec interrupt
USB0_CTL |= (0 | USB_CTL_HOSTMODEEN_MASK | USB_CTL_USBENSOFEN_MASK); // Host mode and usb sof token
//USB0_CTL |= USB_CTL_USBENSOFEN_MASK;

/* Register 'USB0_CTL' initialization */
#if USB0_CTL_MASK_1
#if USB0_CTL_MASK_1 == 0xFF
USB0_CTL = USB0_CTL_VALUE_1;
#elif USB0_CTL_MASK_1 == USB0_CTL_VALUE_1
USB0_CTL |= USB0_CTL_VALUE_1;
#elif USB0_CTL_VALUE_1 == 0
USB0_CTL &= ~USB0_CTL_MASK_1;
#else
USB0_CTL = (USB0_CTL & (~USB0_CTL_MASK_1)) | USB0_CTL_VALUE_1;
#endif
#elif defined(USB0_CTL_VALUE_1)
USB0_CTL = USB0_CTL_VALUE_1;
#endif

/* Register 'USB0_CLK_RECOVER_IRC_EN' initialization */
#if USB0_CLK_RECOVER_IRC_EN_MASK
#if USB0_CLK_RECOVER_IRC_EN_MASK == 0xFF
USB0_CLK_RECOVER_IRC_EN = USB0_CLK_RECOVER_IRC_EN_VALUE;
#elif USB0_CLK_RECOVER_IRC_EN_MASK == USB0_CLK_RECOVER_IRC_EN_VALUE
USB0_CLK_RECOVER_IRC_EN |= USB0_CLK_RECOVER_IRC_EN_VALUE;
#elif USB0_CLK_RECOVER_IRC_EN_VALUE == 0
USB0_CLK_RECOVER_IRC_EN &= ~USB0_CLK_RECOVER_IRC_EN_MASK;
#else
USB0_CLK_RECOVER_IRC_EN = (USB0_CLK_RECOVER_IRC_EN & (~USB0_CLK_RECOVER_IRC_EN_MASK)) | USB0_CLK_RECOVER_IRC_EN_VALUE;
#endif
#elif defined(USB0_CLK_RECOVER_IRC_EN_VALUE)
USB0_CLK_RECOVER_IRC_EN = USB0_CLK_RECOVER_IRC_EN_VALUE;
#endif

/* Register 'USB0_CLK_RECOVER_CTRL' initialization */
#if USB0_CLK_RECOVER_CTRL_MASK
#if USB0_CLK_RECOVER_CTRL_MASK == 0xFF
USB0_CLK_RECOVER_CTRL = USB0_CLK_RECOVER_CTRL_VALUE;
#elif USB0_CLK_RECOVER_CTRL_MASK == USB0_CLK_RECOVER_CTRL_VALUE
USB0_CLK_RECOVER_CTRL |= USB0_CLK_RECOVER_CTRL_VALUE;
#elif USB0_CLK_RECOVER_CTRL_VALUE == 0
USB0_CLK_RECOVER_CTRL &= ~USB0_CLK_RECOVER_CTRL_MASK;
#else
USB0_CLK_RECOVER_CTRL = (USB0_CLK_RECOVER_CTRL & (~USB0_CLK_RECOVER_CTRL_MASK)) | USB0_CLK_RECOVER_CTRL_VALUE;
#endif
#elif defined(USB0_CLK_RECOVER_CTRL_VALUE)
USB0_CLK_RECOVER_CTRL = USB0_CLK_RECOVER_CTRL_VALUE;
#endif

enter_default_state(); // this function is used to set all endpoints to its default state and call set configuration function to load default descriptors.

// Clear any pending OTG interupts.
USB0_OTGISTAT = 0xff;

// Clear any pending USB interrupts.
MX64_USB_INT_STAT = 0xff;

// Clear any pending USB error interrupt.
MX64_USB_ERR_STAT = 0xff;

// Enable USB interrupts.
MX64_USB_INT_ENB |= MX64_USB_INT_ENB_TOK_DNE| MX64_USB_INT_ENB_ERROR | MX64_USB_INT_ENB_USB_RST| MX64_USB_INT_ENB_STALL; // Enabling all the USB0_INTEN parameters

MX64_USB_INT_ENB &= ~MX64_USB_INT_ENB_SLEEP;

USB0_OTGCTL = USB_OTGCTL_OTGEN_MASK; // OTG Module Enable

// Enable USB functionality
USB0_OTGCTL = MX64_USB_CTL_USB_EN_SOF_EN;
USB0_USBCTRL = 0;

USB0_INTEN |= USB_INTEN_USBRSTEN_MASK;

enable_irq(53); // NVIC interrupt enable
set_irq_priority(53,ip); // irq=53 for USB OTG and level 0 Set

USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG_MASK; //Enable pull-up resistor on D+ (Full speed, 12Mbit/s)

MX64_USB_OTG_CTRL = MX64_USB_OTG_CTRL_DP_HIGH | MX64_USB_OTG_CTRL_OTG_EN;


return(0);
}

when I connect usb to PC it shows USB device Not Recognized. I have checked USB module waveform using logic analyzer USB Handshake packets are missing in this case. Please help me and suggest me necessary changes to resolve this issue. Thank you in advance.@@#

Outcomes