1 /*****************************************************/
2 /*** Module : USB PDI ***/
3 /*** Author : Roman Bartosinski (C) 28.04.2002 ***/
4 /*** Modify : 08.08.2002, 16.04.2003 ***/
5 /*****************************************************/
7 #include <system_def.h>
9 #include <usb/lpcusb.h>
11 unsigned int lpc_ep2addr(unsigned int ep_num)
14 val = (ep_num & 0x0F) << 1;
20 void lpc_wait4devint(unsigned int intrs)
22 while ((USBDevIntSt & intrs) != intrs);
27 void lpc_write_cmd(unsigned int cmd)
29 USBDevIntClr = USBDevInt_CCEMTY | USBDevInt_CDFULL;
31 lpc_wait4devint(USBDevInt_CCEMTY);
34 void lpc_write_cmd_data (unsigned int cmd, unsigned int val)
38 lpc_wait4devint(USBDevInt_CCEMTY);
41 unsigned int lpc_read_cmd_data (unsigned int cmd)
44 lpc_wait4devint(USBDevInt_CDFULL);
48 void lpc_usb_realizeEP(unsigned int idx,unsigned int wmaxpsize)
50 USBReEp |= (1 << idx);
52 USBMaxPSize = wmaxpsize;
53 lpc_wait4devint(USBDevInt_EP_RLZED);
56 void lpc_usb_configEP(unsigned int ep_num,unsigned int wmaxpsize)
58 lpc_usb_realizeEP(lpc_ep2addr(ep_num),wmaxpsize);
59 lpc_usb_enableEP(ep_num);
62 void lpc_usb_setstallEP(unsigned int ep_num)
64 lpc_write_cmd_data(USB_CMD_SET_EP_STAT(lpc_ep2addr(ep_num)),USB_DAT_WR_BYTE(USBC_EP_STAT_ST));
67 void lpc_usb_clrstallEP(unsigned int ep_num)
69 lpc_write_cmd_data(USB_CMD_SET_EP_STAT(lpc_ep2addr(ep_num)),USB_DAT_WR_BYTE(0));
72 void lpc_usb_enableEP(unsigned int ep_num)
74 lpc_write_cmd_data(USB_CMD_SET_EP_STAT(lpc_ep2addr(ep_num)),USB_DAT_WR_BYTE(0));
77 void lpc_usb_disableEP(unsigned int ep_num)
79 lpc_write_cmd_data(USB_CMD_SET_EP_STAT(lpc_ep2addr(ep_num)),USB_DAT_WR_BYTE(USBC_EP_STAT_DA));
82 void lpc_usb_set_addr(unsigned int adr)
84 lpc_write_cmd_data(USB_CMD_SET_ADDR,USB_DAT_WR_BYTE(USBC_DEV_EN | adr)); /* Setup Status Phase */
87 void lpc_usb_config_device(int fConfigured)
89 lpc_write_cmd_data(USB_CMD_CFG_DEV,USB_DAT_WR_BYTE(fConfigured ? USBC_CONF_DEVICE : 0)); /* Setup Status Phase */
92 void lpc_usb_reset(void)
94 USBEpIntClr = 0xFFFFFFFF;
95 USBEpIntEn = 0xFFFFFFFF ^ USB_DMA_EP;
96 USBDevIntClr = 0xFFFFFFFF;
97 USBDevIntEn = USBDevInt_DEV_STAT | USBDevInt_EP_SLOW;
99 lpc_usb_configEP(0x00, USB_MAX_PACKET0);
100 lpc_usb_configEP(0x80, USB_MAX_PACKET0);
103 void lpc_usb_hw_init (void)
106 PINSEL1 &= ~0xC000C000;
107 // PINSEL1 |= 0x40004000; /* Select USB Link, VBUS */
108 PINSEL1 |= 0x80000000; /* Select USB Link, VBUS */
110 PCONP |= 0x80000000; /* Turn On USB PCLK */
112 /* Configure 48MHz USB Clock; FOsc = 12MHz, M = 4, P = 2 */
113 PLLCFG48 = 0x23; /* M = 4, P = 2 */
114 PLLCON48 = PLLCON_PLLE; /* PLL Enable */
115 PLLFEED48 = 0xAA; /* Feed Sequence 1 */
116 PLLFEED48 = 0x55; /* Feed Sequence 2 */
118 while ((PLLSTAT48 & PLLSTAT_LOCK) == 0); /* Wait for PLL Lock */
120 PLLCON48 = PLLCON_PLLE | PLLCON_PLLC; /* PLL Enable & Connect */
121 PLLFEED48 = 0xAA; /* Feed Sequence 1 */
122 PLLFEED48 = 0x55; /* Feed Sequence 2 */
124 USBDevIntEn = USBDevInt_DEV_STAT; /* Enable Device Status Interrupt */
126 /* Partial Manual Reset since Automatic Bus Reset is not working */
132 * lpc_usb_read_endpoint: Read USB Endpoint Data
133 * @EPNum: Endpoint Number - EPNum.0..3: Address, EPNum.7: Dir
134 * @ptr: Pointer to Data Buffer
136 * Return Value: Number of bytes read
138 int lpc_usb_read_endpoint( unsigned int ep_num, void *ptr, int size)
140 unsigned int cnt,i,dwData;
141 unsigned char *p=ptr;
143 USBCtrl = ((ep_num & 0x0F) << 2) | USBCtrl_RD_EN;
146 } while ((cnt & USBRxPLen_PKT_RDY) == 0);
147 cnt &= USBRxPLen_PKT_LNGTH;
150 while (USBCtrl & USBCtrl_RD_EN) {
153 for (i = 0; (i < 4) && size; i++) {
162 lpc_write_cmd(USB_CMD_SEL_EP(lpc_ep2addr(ep_num)));
163 lpc_write_cmd(USB_CMD_CLR_BUF);
169 * lpc_usb_write_endpoint: Write USB Endpoint Data
170 * @ep_num: Endpoint Number - ep_num.0..3: Address, ep_num.7: Dir
171 * @ptr: Pointer to Data Buffer
172 * @size: Number of bytes to write
173 * Return Value: Number of bytes written
175 int lpc_usb_write_endpoint( unsigned int ep_num, const void *ptr, int size)
178 const unsigned char *p=ptr;
180 USBCtrl = ((ep_num & 0x0F) << 2) | USBCtrl_WR_EN;
183 for (n = 0; n < (size + 3) / 4; n++) {
184 USBTxData = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
190 lpc_write_cmd(USB_CMD_SEL_EP(lpc_ep2addr(ep_num)));
191 lpc_write_cmd(USB_CMD_VALID_BUF);