📄 usbhal.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: USBHAL.c
** Last modified Date: 2007-07-08
** Last Version: V1.0
** Descriptions: USBHAL.c, 硬件抽象层,设置 USB 设备的运行条件
** USBHAL.c, Hardware abstract layer, configure the running condition of USB device
**------------------------------------------------------------------------------------------------------
** Created by: 郑明远 MingYuan Zheng
** Created date: 2007-07-08
** Version: V1.0
** Descriptions: 初始版本 The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
/**************************************************************************************
PLL1: USB device PLL clock
P1.30: VBUS — Indicates the presence of USB bus power.
P1.18: UP_LED --- USB Good Link LED indicator.
P2.9 SoftConnect --- signal used to switch an external 1.5K resistor under software control
P0.29(Pin 10): USB D+
P0.30(Pin 11): USB D-
USB Interrupt VIC = 22
**************************************************************************************/
#include "config.h"
#include "USBConfig.h"
#include "USBCI.h"
#include "USBHAL.h"
#ifndef USBClkCtrl
#define USBClkCtrl (*(volatile unsigned long *)(0xFFE0CFF4))
#endif
#ifndef USBClkSt
#define USBClkSt (*(volatile unsigned long *)(0xFFE0CFF8))
#endif
#ifndef USBPortSel
#define USBPortSel (*(volatile unsigned long *)(0xFFE0C110)) /* LPC2378 Only */
#endif
#define DEV_CLK_EN (0x01 << 1)
#define PORTSEL_CLK_EN (0x01 << 3)
#define AHB_CLK_EN (0x01 << 4)
/*******************************************************************************************************************
** 函数名称: USB_InitHareware Name: USB_InitHareware
** 功能描述: 初始化 USB 设备控制器硬件 Function: Initialize the hardware of USB device controller
********************************************************************************************************************/
void USB_InitHardware(void)
{
INT32U clkctrl = DEV_CLK_EN | AHB_CLK_EN;
USBClkCtrl = clkctrl; /* 配置USB时钟控制寄存器 */
while (!(USBClkSt & (DEV_CLK_EN | AHB_CLK_EN)));
#if USB_PORT2_EN
USBClkCtrl |= PORTSEL_CLK_EN;
while (!(USBClkSt & (PORTSEL_CLK_EN)));
USBPortSel = 0x03;
PINSEL0 &= ~(0x03 << 26); /* P0.13 is LPC23xx USB Device(2) U2UP_LED */
PINSEL0 |= (0x01 << 26);
PINSEL0 &= ~(0x03 << 28); /* P0.14 is LPC23xx USB Device(2) U2CONNECT */
PINSEL0 |= (0x02 << 28);
PINSEL1 &= ~(0x03U << 30); /* P0.31 is USB2 D+ */
PINSEL1 |= (0x01 << 30);
PINSEL3 &= ~(0x03U << 28); /* P1.30 为 LPC23xx USB Device(1)和(2) 的VBUS */
PINSEL3 |= (0x02U << 28);
#else
PINSEL1 &= ~(0x03U << 26); /* P0.29 为 LPC23xx USB Device(1) 的D+ */
PINSEL1 |= (0x01U << 26);
PINSEL1 &= ~(0x03U << 28); /* P0.30 为 LPC23xx USB Device(1) 的D- */
PINSEL1 |= (0x01U << 28);
/* P2.9 is a GPIO */
PINSEL4 &= ~(0x03U << 18); /* P2.9 为 LPC23xx USB Device(1) 的SoftConnect */
//PINSEL4 |= (0x01U << 18);
FIO2DIR |= 0x01 << 9; /* P2.9 is output pin */
FIO2SET |= 0x01 << 9;
PINSEL3 &= ~(0x03U << 28); /* P1.30 为 LPC23xx USB Device(1)和(2) 的VBUS */
PINSEL3 |= (0x02U << 28);
PINSEL3 &= ~(0x03U << 4); /* P1.18 为 LPC23xx USB Device(1) UP_LED */
PINSEL3 |= (0x01U << 4);
#endif
PCONP |= 0x80000000; /* 给USB控制器提供电源 power on USB controller */
}
/************************************************************
** Name : USB_Disconnect
** Function: disconnect USB bus
************************************************************/
void USB_Disconnect(void)
{
FIO2SET |= 0x01 << 9;
}
/************************************************************
** Name : USB_Connect
** Function: connect USB bus
************************************************************/
void USB_Connect(void)
{
FIO2CLR |= 0x01 << 9;
}
/************************************************************
** Name : USB_Reconnect
** Function: reconnect USB bus
************************************************************/
void USB_Reconnect(void)
{
INT32U clk_cnt;
USB_Disconnect(); /* disconnect USB bus */
for (clk_cnt = 0;clk_cnt<= 0x1FFFF;clk_cnt++); /* delay */
USB_Connect(); /* connect USB bus */
}
/*******************************************************************************************************************
** 函数名称: USB_USBDevIntConfig Name: USB_USBDevIntConfig
** 功能描述: 配置USB设备控制器的中断 Function: Config the Interrupt of USB device controller
********************************************************************************************************************/
void USB_USBDevIntConfig(void)
{
INT32U tmp = EP_SLOW;
// Clears the frame interrupt flag in USBDevIntClr
USBDevIntClr = FRAME; /* 禁止同步传输帧中断 */
// Enables ep_slow and dev_stat interrupt only in USBDevIntEn
USBDevIntEn = tmp; /* 使能复位中断, 低速中断 */
// the EP_FAST interrupt is slow priority interrupt
USBDevIntPri = 0; /* 使能端点中断为低优先级中断 */
#if DMA_ENGINE_EN
tmp = (0x01 << 0) + (0x01 << 1); /* 使能控制传输端点从机中断 */
#else
tmp = (0x01 << 0) + (0x01 << 1) + (0x01 << 2) +
(0x01 << 3) + (0x01 << 4) + (0x01 << 5); /* 使能端点 0 ~ 5 从机中断 */
#endif
// Enable Endpoint Interrupt: endpoint 0 ~ 5
USBEpIntEn = tmp; /* 使能物理端点 0 ~ 5中断 */
// Routes all endp ints to the ep_slow int USBEpIntPri
USBEpIntPri &= ~tmp; /* 设置物理端点 0 ~ 5为低优先级中断 */
}
/*******************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -