LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
HAL_LPC18xx.c
Go to the documentation of this file.
1 /*
2  * @brief HAL USB functions for the LPC18xx microcontrollers
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #if defined(__LPC18XX__) || defined(__LPC43XX__)
33 
34 #include "../HAL_LPC.h"
35 #include "../../../USBTask.h"
36 
37 #if defined(USB_CAN_BE_DEVICE)
38 #include "../../../Device.h"
39 
40 void HAL_USBConnect(uint8_t corenum, uint32_t con)
41 {
42 #if defined(USB_DEVICE_ROM_DRIVER)
43  if (con) {
44  USBD_API->hw->Connect(UsbHandle, 1);
45  }
46  else {
47  USBD_API->hw->Connect(UsbHandle, 0);
48  }
49 #else
50  if (con) {
51  USB_REG(corenum)->USBCMD_D |= (1 << 0);
52  }
53  else {
54  USB_REG(corenum)->USBCMD_D &= ~(1 << 0);
55  }
56 #endif
57 }
58 
59 #endif
60 
61 /* Assumed Xtal Frequency */
62 #define XTAL_FREQ 12000000
63 #define USB1_FREQ 60000000
64 
65 void HAL_USBInit(uint8_t corenum)
66 {
67  if (corenum) {
68  uint32_t core_freq;
69 #if defined(__LPC43XX__)
71 #endif
72  core_freq = Chip_Clock_GetMainPLLHz();
73  /* connect DIVD to PLL1, need a condition for calculate div factor */
74  LPC_CGU->IDIV_CTRL[CLK_IDIV_D] = (9 << 24) | (1 << 11) | ((core_freq / USB1_FREQ - 1) << 2);
75 
76  LPC_CGU->BASE_CLK[CLK_BASE_USB1] = (0x0F << 24) | (1 << 11); /* connect DIVD to base USB1 */
77 
78  /* Turn on the phy */
79  LPC_CREG->CREG0 &= ~(1 << 5);
80  #if defined(USB_CAN_BE_HOST)
81  /* enable USB1_DP and USB1_DN on chip FS phy */
82  LPC_SCU->SFSUSB = 0x16;
83  #endif
84  #if defined(USB_CAN_BE_DEVICE)
85  /* enable USB1_DP and USB1_DN on chip FS phy */
86  LPC_SCU->SFSUSB = 0x12;
87  #endif
88  LPC_USB1->PORTSC1_D |= (1 << 24);
89  }
90  else {
91  /* Set up USB0 clock */
92  /* Disable PLL first */
93  LPC_CGU->PLL0USB_CTRL |= 1;
94  /* the usb core require output clock = 480MHz */
95  if (XTAL_FREQ == 12000000) {
96  LPC_CGU->PLL0USB_MDIV = 0x06167FFA;
97  }
98  else if (XTAL_FREQ == 24000000) {
99  LPC_CGU->PLL0USB_MDIV = 0x030C00FF;
100  }
101  else if (XTAL_FREQ == 48000000) {
102  LPC_CGU->PLL0USB_MDIV = 0x02060007;
103  }
104  else {LPC_CGU->PLL0USB_MDIV = 0x06167FFA; }
105  LPC_CGU->PLL0USB_NP_DIV = 0;
106  LPC_CGU->PLL0USB_CTRL = 0x601C;
107  LPC_CGU->PLL0USB_CTRL |= ((6 << 24) | (1 << 11));
108  /* Enable PLL after all setting is done */
109  LPC_CGU->PLL0USB_CTRL &= ~1;
110  while ((LPC_CGU->PLL0USB_STAT & 1) == 0x0) ;
111  /* Turn on the phy */
112  LPC_CREG->CREG0 &= ~(1 << 5);
113  }
114 
115 #if defined(USB_CAN_BE_DEVICE) && (!defined(USB_DEVICE_ROM_DRIVER))
116  /* reset the controller */
117  USB_REG(corenum)->USBCMD_D = USBCMD_D_Reset;
118  /* wait for reset to complete */
119  while (USB_REG(corenum)->USBCMD_D & USBCMD_D_Reset) ;
120 
121  /* Program the controller to be the USB device controller */
122  USB_REG(corenum)->USBMODE_D = (0x2 << 0) /*| (1<<4)*//*| (1<<3)*/;
123  if (corenum == 0) {
124  /* set OTG transcever in proper state, device is present
125  on the port(CCS=1), port enable/disable status change(PES=1). */
126  LPC_USB0->OTGSC = (1 << 3) | (1 << 0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/;
127  #if (USB_FORCED_FULLSPEED)
128  LPC_USB0->PORTSC1_D |= (1 << 24);
129  #endif
130  }
131  HAL_Reset();
132 #endif
133 }
134 
135 void HAL_USBDeInit(uint8_t corenum)
136 {
137  HAL_DisableUSBInterrupt(corenum);
139  #if defined(USB_CAN_BE_HOST)
140  USB_REG(corenum)->USBSTS_H = 0xFFFFFFFF; /* clear all current interrupts */
141  USB_REG(corenum)->PORTSC1_H &= ~(1 << 12); /* clear port power */
142  USB_REG(corenum)->USBMODE_H = (1 << 0); /* set USB mode reserve */
143  #endif
144  }
145  else if (USB_CurrentMode == USB_MODE_Host) {
146  #if defined(USB_CAN_BE_DEVICE)
147  /* Clear all pending interrupts */
148  USB_REG(corenum)->USBSTS_D = 0xFFFFFFFF;
149  USB_REG(corenum)->ENDPTNAK = 0xFFFFFFFF;
150  USB_REG(corenum)->ENDPTNAKEN = 0;
151  USB_REG(corenum)->ENDPTSETUPSTAT = USB_REG(corenum)->ENDPTSETUPSTAT;
152  USB_REG(corenum)->ENDPTCOMPLETE = USB_REG(corenum)->ENDPTCOMPLETE;
153  while (USB_REG(corenum)->ENDPTPRIME) ; /* Wait until all bits are 0 */
154  USB_REG(corenum)->ENDPTFLUSH = 0xFFFFFFFF;
155  while (USB_REG(corenum)->ENDPTFLUSH) ; /* Wait until all bits are 0 */
156  #endif
157  }
158 }
159 
160 void HAL_EnableUSBInterrupt(uint8_t corenum)
161 {
162  NVIC_EnableIRQ((corenum) ? USB1_IRQn : USB0_IRQn); // enable USB interrupts
163 }
164 
165 void HAL_DisableUSBInterrupt(uint8_t corenum)
166 {
167  NVIC_DisableIRQ((corenum) ? USB1_IRQn : USB0_IRQn); // disable USB interrupts
168 }
169 
170 void USB0_IRQHandler(void)
171 {
173  #ifdef USB_CAN_BE_HOST
174  HcdIrqHandler(0);
175  #endif
176  }
178  #ifdef USB_CAN_BE_DEVICE
179  #ifdef USB_DEVICE_ROM_DRIVER
181  #else
182  DcdIrqHandler(0);
183  #endif
184  #endif
185  }
186 }
187 
188 void USB1_IRQHandler(void)
189 {
191  #ifdef USB_CAN_BE_HOST
192  HcdIrqHandler(1);
193  #endif
194  }
196  #ifdef USB_CAN_BE_DEVICE
197  #ifdef USB_DEVICE_ROM_DRIVER
199  #else
200  DcdIrqHandler(1);
201  #endif
202  #endif
203  }
204 }
205 
206 #endif /*__LPC18XX__*/