From 0163b16211dc58d6619b923b370be8491fb984cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Thu, 2 Dec 2010 21:07:10 +0000 Subject: [PATCH] initial usb MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Asbjørn Sloth Tønnesen --- new_cmsis/usb/.Makefile.swp | Bin 0 -> 12288 bytes new_cmsis/usb/Abstract.txt | 141 +++++ new_cmsis/usb/LPC1768-flash.ld | 50 ++ new_cmsis/usb/Makefile | 26 + new_cmsis/usb/cdc.h | 283 ++++++++++ new_cmsis/usb/cdcuser.c | 379 ++++++++++++++ new_cmsis/usb/cdcuser.h | 63 +++ new_cmsis/usb/lpc17xx-vcom.inf | 65 +++ new_cmsis/usb/lpc17xx_libcfg.h | 144 ++++++ new_cmsis/usb/lpc_types.h | 196 +++++++ new_cmsis/usb/serial.c | 404 +++++++++++++++ new_cmsis/usb/serial.h | 30 ++ new_cmsis/usb/startup.c | 316 ++++++++++++ new_cmsis/usb/usb.h | 352 +++++++++++++ new_cmsis/usb/usbcfg.h | 181 +++++++ new_cmsis/usb/usbcore.c | 1112 ++++++++++++++++++++++++++++++++++++++++ new_cmsis/usb/usbcore.h | 52 ++ new_cmsis/usb/usbdesc.c | 201 ++++++++ new_cmsis/usb/usbdesc.h | 35 ++ new_cmsis/usb/usbhw.c | 811 +++++++++++++++++++++++++++++ new_cmsis/usb/usbhw.h | 112 ++++ new_cmsis/usb/usbreg.h | 130 +++++ new_cmsis/usb/usbuser.c | 336 ++++++++++++ new_cmsis/usb/usbuser.h | 74 +++ new_cmsis/usb/vcomdemo.c | 124 +++++ new_cmsis/usb/vcomdemo.h | 31 ++ 26 files changed, 5648 insertions(+) create mode 100644 new_cmsis/usb/.Makefile.swp create mode 100755 new_cmsis/usb/Abstract.txt create mode 100644 new_cmsis/usb/LPC1768-flash.ld create mode 100644 new_cmsis/usb/Makefile create mode 100755 new_cmsis/usb/cdc.h create mode 100755 new_cmsis/usb/cdcuser.c create mode 100755 new_cmsis/usb/cdcuser.h create mode 100755 new_cmsis/usb/lpc17xx-vcom.inf create mode 100755 new_cmsis/usb/lpc17xx_libcfg.h create mode 100755 new_cmsis/usb/lpc_types.h create mode 100755 new_cmsis/usb/serial.c create mode 100755 new_cmsis/usb/serial.h create mode 100644 new_cmsis/usb/startup.c create mode 100755 new_cmsis/usb/usb.h create mode 100755 new_cmsis/usb/usbcfg.h create mode 100755 new_cmsis/usb/usbcore.c create mode 100755 new_cmsis/usb/usbcore.h create mode 100755 new_cmsis/usb/usbdesc.c create mode 100755 new_cmsis/usb/usbdesc.h create mode 100755 new_cmsis/usb/usbhw.c create mode 100755 new_cmsis/usb/usbhw.h create mode 100755 new_cmsis/usb/usbreg.h create mode 100755 new_cmsis/usb/usbuser.c create mode 100755 new_cmsis/usb/usbuser.h create mode 100755 new_cmsis/usb/vcomdemo.c create mode 100755 new_cmsis/usb/vcomdemo.h diff --git a/new_cmsis/usb/.Makefile.swp b/new_cmsis/usb/.Makefile.swp new file mode 100644 index 0000000000000000000000000000000000000000..93fa5b15d3e747f544268f1c59a094db08c8f48a GIT binary patch literal 12288 zcmeI2%}x|S5XT#hCM1YPZ=PBKBaxn2ArV-e2%;;PU3Y^h@kDxhdY6Iu%Jd8$G0~GJ zeF*iW2XFcUUi1lk4Ly4FKkR_(nh>tU>g1oU>Z-2p`t4pe+ivB_QiD%hlMLe=WA9J? zsxMYvo;n>fCXKrhr%@3(bhy&!Lles2^Rl4r=`A~zNupD`<7alHx7So?d}HUvwHtC% zxBcKq6_0W%BHW90HBe&PIM0z(9d01+SpM1Tko0U|&Ih`@hD zAWH_=3-o)U?D=9j5A@BYOA`?w0z`la5CI}U1c(3;AOb{y2oM1xa104ZkFhU9jMdKI z^7#LM|M&mTvy6R*K0r4i3lh)}^lq53x6m8tHS`KfpbBI|SD+uL_YL|AeTF_kAE5x+ zgxXMX<}-)}5g-CYfCvx)B0vO)01+SpMBw-k7#zLYs?V>~n#+xG(eYdkf4TYK-onbb zrGqxZQkAj6G~}Y)7lYfpQ)E6G|6^9I*s;7~;h;sXrqQ5iOfvynv9QG@x++rO^nWAd!xnRSFx!2 z&Ivq3Wzu&|xb?f`**(p5FVNEwz*LdD-#y-&2SKrL&~ibJu`=!$o%%AcVva}RDo%CR z^0ZN1Ykj*r=SYOLrQ*=jVGPq`GR^V?mhw~?wk+H1noNgl_141m>75-bW+OayH?+zO zm#OAPCo+|p_PD#tO(GRuq>pr&UUNj#u&(WhRW4R>buNU-Jf{=ov$Ob^G0)5jKMMRv za}lZ}cW~=W?}%^;ezu;6F4Ee0q%uE_gu%g>4c0uF$sT{n;-bS!q>d0B)f4P+vBLLS kxG^Kzfi&w@;IaKLBfssSmrz7;q=lBQ&q}xsAQp7kZ?#?gGXMYp literal 0 HcmV?d00001 diff --git a/new_cmsis/usb/Abstract.txt b/new_cmsis/usb/Abstract.txt new file mode 100755 index 0000000..8f49cbf --- /dev/null +++ b/new_cmsis/usb/Abstract.txt @@ -0,0 +1,141 @@ + ******************** (C) COPYRIGHT 2010 NXPSemiconductors ******************* + * @file USBDEV\USBCDC\abstract.txt + * @author NXP MCU SW Application Team + * @version 2.0 + * @date + * @brief Description of the USBDEV CDC example. + ****************************************************************************** + * Software that is described herein is for illustrative purposes only + * which provides customers with programming information regarding the + * products. This software is supplied "AS IS" without any warranties. + * NXP Semiconductors assumes no responsibility or liability for the + * use of the software, conveys no license or title under any patent, + * copyright, or mask work right to the product. NXP Semiconductors + * reserves the right to make changes in the software without + * notification. NXP Semiconductors also make no representation or + * warranty that such application will be suitable for the specified + * use without further testing or modification. + ****************************************************************************** + +@Example description: + Purpose: + This example describes how to use USBDEV on LPC1768 to demo the virtual COM port. + Process: + Clock Settings: + - XTAL = 12 MHz + - PLL = 400 MHz + - processor clock = CCLK = 100 MHz + - USB clock = 48 MHz + - CCLK / 4 clock = 25 MHz + + The PC will install a virtual COM port on the PC (see Driver Installation). + After installation an additional port "LPC17xx USB VCom Port(COMx)" + can be found under System/Hardware/Device Manager/Ports(COM&LPT). + Number "x" is not fixed as different PC configuration may have different + "x" displayed on the device manager. The USB host driver assigns "x" + dynamically based on the existing COM port configuration of the system. + + Testing the USB Virtual COM port with serial cable: + + Open two Hyperterminal windows. + One with "LPC17xx USB VCom Port(COMx)" + One with "Communications Port (COM1)". + Connect PC port COM1 to the comport on the board and open "COM1" + and "COMx". Data from COM1 will be echoed on "COMx" and visa versa. + So, this is bi-directional communication between the physical COM + port 0 or 1 on the board and the virtual COM port COMx on host PC. + By default, COM PORT1 on the board is used for VirtualCOM port test. + In order to use COM PORT0 on the board, modify the definition PORT_NUM + from 1 to 0 in serial.h, recompile and reprogram the flash. RST jumper + needs to removed to start the Virtual COM port test. + +@Driver Installation: + "Welcome to the Found New Hardware Wizard" appears + - select 'No, not this time' + - press 'Next' + + - select 'Install from a list or specific location (Advanced)' + - press 'Next' + + - select 'Search for the best driver in these locations' + - check 'include this location in the serach' + - set to + - press 'Next' + + "Hardware Installation" appears + "has not passed Windows Logo testing..." + - press 'Continue Anyway' + + "Completing the Found New Hardware Wizard" appears + - press 'Finish' + +@Directory contents: + \EWARM: includes EWARM (IAR) project and configuration files + \Keil: includes RVMDK (Keil)project and configuration files + + cdc.h: USB CDC (Communication Device) Definitions + cdcuser.h/.c: USB Communication Device Class User module + lpc17xx_libcfg.h: Library configuration file - include needed driver library for this example + serial.h/.c: serial port handling for LPC17xx + usb.h: USB Definitions + usbcfg.h: USB Custom Configuration + usbcore.h/.c: USB Core Module + usbdesc.h/.c: USB Descriptors + usbhw.h/.c: SB Hardware Layer Module + usbreg.h: USB Hardware Layer Definitions for NXP Semiconductors LPC + usbuser.h/.c: USB Custom User Module + vcomdemo.h/.c: main program + makefile: Example's makefile (to build with GNU toolchain) + lpc17xx-vom.inf: driver info for VCOM LPC17xx (used when Windows requires install driver) + +@How to run: + Hardware configuration: + This example was tested only on: + Keil MCB1700 with LPC1768 vers.1 + These jumpers must be configured as following: + - VDDIO: ON + - VDDREGS: ON + - VBUS: ON + - D+: DEVICE + - D-: DEVICE + - UMODE: 1-2 (USB) + - E/U: 1-2 (USB) + - Remain jumpers: OFF + + Running mode: + This example can run on RAM/ROM mode. + + Note: If want to burn hex file to board by using Flash Magic, these jumpers need + to be connected: + - MCB1700 with LPC1768 ver.1: + + RST: ON + + ISP: ON + - IAR LPC1768 KickStart vers.A: + + RST_E: ON + + ISP_E: ON + + (Please reference "LPC1000 Software Development Toolchain" - chapter 4 "Creating and working with + LPC1000CMSIS project" for more information) + + Step to run: + - Step 1: Build example. + - Step 2: Burn hex file into board (if run on ROM mode) + - Step 3: Configure hardware as above instruction + - Step 4: Hit reset button to run example. (install driver if required) + - Step 5: After see UGL(USB Good Link) led on board turn on, open Device Manager > Ports (COM & LPT) + see if "LPC17xx USB Vcom Port (COMx)" appears or not. + - Step 6: Open one HyperTerminal for COM1 and other for COMx with below configuration: + - 9600 bps + - 8 data bits + - none parity + - 1 stop bit + - None flow control + - Step 7: Sure that you already connected COM1 port on board with PC. Type any character on + one HyperTerminal screen and see this character will be echoed in other screen and vice versa + + (Pls see "LPC17xx Example Description" document - chapter "Examples > USBDEV > USBCDC" + for more details) + +@Tip: + - Open \EWARM\*.eww project file to run example on IAR + - Open \RVMDK\*.uvproj project file to run example on Keil \ No newline at end of file diff --git a/new_cmsis/usb/LPC1768-flash.ld b/new_cmsis/usb/LPC1768-flash.ld new file mode 100644 index 0000000..cebf954 --- /dev/null +++ b/new_cmsis/usb/LPC1768-flash.ld @@ -0,0 +1,50 @@ +/* + * This is the NXP LPC1768 linker file for code running from flash. + * + * TODO: + * - handle the exotic input sections (e.g. glue and veneer, C++ sections) + * - add additional Ethernet and USB RAM memory regions (2x16k) + * - add boot ROM memory regions + * + * See also: http://bitbucket.org/jpc/lpc1768/ + * + * Copyright (c) 2010 LoEE - Jakub Piotr Cłapa + * This program is released under the new BSD license. + */ +OUTPUT_FORMAT("elf32-littlearm") +OUTPUT_ARCH(arm) + +ENTRY(Reset_Handler) + +MEMORY { + flash (rx) : ORIGIN = 0x00000000, LENGTH = 512K + ram (rwx) : ORIGIN = 0x10000000, LENGTH = 32K +} + +SECTIONS { + . = 0; + + .text : { + _stext = .; + KEEP(*(.isr_vector)) + *(.text*) + *(.rodata*) + . = ALIGN(4); + _etext = .; + } > flash + + .data : { + _sdata = .; + *(.data*) + _edata = .; + } > ram AT > flash + + .bss : { + _sbss = .; + *(.bss*) + . = ALIGN(4); + _ebss = .; + } > ram + + _sstack = ORIGIN(ram) + LENGTH(ram); +} diff --git a/new_cmsis/usb/Makefile b/new_cmsis/usb/Makefile new file mode 100644 index 0000000..5c55c09 --- /dev/null +++ b/new_cmsis/usb/Makefile @@ -0,0 +1,26 @@ +PROJECT=usbdemo +PLATFORM ?= arm-none-eabi +LDFLAGS=--gc-sections -g -T LPC1768-flash.ld +CFLAGS=-W -Wall --std=gnu99 -fgnu89-inline -mcpu=cortex-m3 -mthumb -ffunction-sections -fdata-sections -I. -g +#CFLAGS+=-Os + +# objects are separated by space +OBJECTS=serial.o usbcore.o usbdesc.o usbhw.o usbuser.o vcomdemo.o startup.o cdcuser.o ../system_LPC17xx.o + + +all: $(PROJECT).elf + +$(PROJECT).elf: $(OBJECTS) + $(PLATFORM)-ld -Map $(PROJECT).map $(LDFLAGS) $(OBJECTS) -o $@ + +%.o: %.c + $(PLATFORM)-gcc -MM $< -MF $(patsubst %.o,%.d,$@) -MP + $(PLATFORM)-gcc $(CFLAGS) -c $< -o $@ + +.PHONY: clean gdb + +clean: + rm -f $(PROJECT).elf $(OBJECTS) $(OBJECTS:.o=.d) $(PROJECT).map + +gdb: + $(PLATFORM)-gdb $(PROJECT).elf diff --git a/new_cmsis/usb/cdc.h b/new_cmsis/usb/cdc.h new file mode 100755 index 0000000..720517a --- /dev/null +++ b/new_cmsis/usb/cdc.h @@ -0,0 +1,283 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: CDC.h + * Purpose: USB Communication Device Class Definitions + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#ifndef __CDC_H +#define __CDC_H +#include "lpc_types.h" + +#if defined ( __GNUC__ ) +#define __packed __attribute__((__packed__)) +#endif +/*---------------------------------------------------------------------------- + * Definitions based on usbcdc11.pdf (www.usb.org) + *---------------------------------------------------------------------------*/ +// Communication device class specification version 1.10 +#define CDC_V1_10 0x0110 + +// Communication interface class code +// (usbcdc11.pdf, 4.2, Table 15) +#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 + +// Communication interface class subclass codes +// (usbcdc11.pdf, 4.3, Table 16) +#define CDC_DIRECT_LINE_CONTROL_MODEL 0x01 +#define CDC_ABSTRACT_CONTROL_MODEL 0x02 +#define CDC_TELEPHONE_CONTROL_MODEL 0x03 +#define CDC_MULTI_CHANNEL_CONTROL_MODEL 0x04 +#define CDC_CAPI_CONTROL_MODEL 0x05 +#define CDC_ETHERNET_NETWORKING_CONTROL_MODEL 0x06 +#define CDC_ATM_NETWORKING_CONTROL_MODEL 0x07 + +// Communication interface class control protocol codes +// (usbcdc11.pdf, 4.4, Table 17) +#define CDC_PROTOCOL_COMMON_AT_COMMANDS 0x01 + +// Data interface class code +// (usbcdc11.pdf, 4.5, Table 18) +#define CDC_DATA_INTERFACE_CLASS 0x0A + +// Data interface class protocol codes +// (usbcdc11.pdf, 4.7, Table 19) +#define CDC_PROTOCOL_ISDN_BRI 0x30 +#define CDC_PROTOCOL_HDLC 0x31 +#define CDC_PROTOCOL_TRANSPARENT 0x32 +#define CDC_PROTOCOL_Q921_MANAGEMENT 0x50 +#define CDC_PROTOCOL_Q921_DATA_LINK 0x51 +#define CDC_PROTOCOL_Q921_MULTIPLEXOR 0x52 +#define CDC_PROTOCOL_V42 0x90 +#define CDC_PROTOCOL_EURO_ISDN 0x91 +#define CDC_PROTOCOL_V24_RATE_ADAPTATION 0x92 +#define CDC_PROTOCOL_CAPI 0x93 +#define CDC_PROTOCOL_HOST_BASED_DRIVER 0xFD +#define CDC_PROTOCOL_DESCRIBED_IN_PUFD 0xFE + +// Type values for bDescriptorType field of functional descriptors +// (usbcdc11.pdf, 5.2.3, Table 24) +#define CDC_CS_INTERFACE 0x24 +#define CDC_CS_ENDPOINT 0x25 + +// Type values for bDescriptorSubtype field of functional descriptors +// (usbcdc11.pdf, 5.2.3, Table 25) +#define CDC_HEADER 0x00 +#define CDC_CALL_MANAGEMENT 0x01 +#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 +#define CDC_DIRECT_LINE_MANAGEMENT 0x03 +#define CDC_TELEPHONE_RINGER 0x04 +#define CDC_REPORTING_CAPABILITIES 0x05 +#define CDC_UNION 0x06 +#define CDC_COUNTRY_SELECTION 0x07 +#define CDC_TELEPHONE_OPERATIONAL_MODES 0x08 +#define CDC_USB_TERMINAL 0x09 +#define CDC_NETWORK_CHANNEL 0x0A +#define CDC_PROTOCOL_UNIT 0x0B +#define CDC_EXTENSION_UNIT 0x0C +#define CDC_MULTI_CHANNEL_MANAGEMENT 0x0D +#define CDC_CAPI_CONTROL_MANAGEMENT 0x0E +#define CDC_ETHERNET_NETWORKING 0x0F +#define CDC_ATM_NETWORKING 0x10 + +// CDC class-specific request codes +// (usbcdc11.pdf, 6.2, Table 46) +// see Table 45 for info about the specific requests. +#define CDC_SEND_ENCAPSULATED_COMMAND 0x00 +#define CDC_GET_ENCAPSULATED_RESPONSE 0x01 +#define CDC_SET_COMM_FEATURE 0x02 +#define CDC_GET_COMM_FEATURE 0x03 +#define CDC_CLEAR_COMM_FEATURE 0x04 +#define CDC_SET_AUX_LINE_STATE 0x10 +#define CDC_SET_HOOK_STATE 0x11 +#define CDC_PULSE_SETUP 0x12 +#define CDC_SEND_PULSE 0x13 +#define CDC_SET_PULSE_TIME 0x14 +#define CDC_RING_AUX_JACK 0x15 +#define CDC_SET_LINE_CODING 0x20 +#define CDC_GET_LINE_CODING 0x21 +#define CDC_SET_CONTROL_LINE_STATE 0x22 +#define CDC_SEND_BREAK 0x23 +#define CDC_SET_RINGER_PARMS 0x30 +#define CDC_GET_RINGER_PARMS 0x31 +#define CDC_SET_OPERATION_PARMS 0x32 +#define CDC_GET_OPERATION_PARMS 0x33 +#define CDC_SET_LINE_PARMS 0x34 +#define CDC_GET_LINE_PARMS 0x35 +#define CDC_DIAL_DIGITS 0x36 +#define CDC_SET_UNIT_PARAMETER 0x37 +#define CDC_GET_UNIT_PARAMETER 0x38 +#define CDC_CLEAR_UNIT_PARAMETER 0x39 +#define CDC_GET_PROFILE 0x3A +#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40 +#define CDC_SET_ETHERNET_PMP_FILTER 0x41 +#define CDC_GET_ETHERNET_PMP_FILTER 0x42 +#define CDC_SET_ETHERNET_PACKET_FILTER 0x43 +#define CDC_GET_ETHERNET_STATISTIC 0x44 +#define CDC_SET_ATM_DATA_FORMAT 0x50 +#define CDC_GET_ATM_DEVICE_STATISTICS 0x51 +#define CDC_SET_ATM_DEFAULT_VC 0x52 +#define CDC_GET_ATM_VC_STATISTICS 0x53 + +// Communication feature selector codes +// (usbcdc11.pdf, 6.2.2..6.2.4, Table 47) +#define CDC_ABSTRACT_STATE 0x01 +#define CDC_COUNTRY_SETTING 0x02 + +// Feature Status returned for ABSTRACT_STATE Selector +// (usbcdc11.pdf, 6.2.3, Table 48) +#define CDC_IDLE_SETTING (1 << 0) +#define CDC_DATA_MULTPLEXED_STATE (1 << 1) + + +// Control signal bitmap values for the SetControlLineState request +// (usbcdc11.pdf, 6.2.14, Table 51) +#define CDC_DTE_PRESENT (1 << 0) +#define CDC_ACTIVATE_CARRIER (1 << 1) + +// CDC class-specific notification codes +// (usbcdc11.pdf, 6.3, Table 68) +// see Table 67 for Info about class-specific notifications +#define CDC_NOTIFICATION_NETWORK_CONNECTION 0x00 +#define CDC_RESPONSE_AVAILABLE 0x01 +#define CDC_AUX_JACK_HOOK_STATE 0x08 +#define CDC_RING_DETECT 0x09 +#define CDC_NOTIFICATION_SERIAL_STATE 0x20 +#define CDC_CALL_STATE_CHANGE 0x28 +#define CDC_LINE_STATE_CHANGE 0x29 +#define CDC_CONNECTION_SPEED_CHANGE 0x2A + +// UART state bitmap values (Serial state notification). +// (usbcdc11.pdf, 6.3.5, Table 69) +#define CDC_SERIAL_STATE_OVERRUN (1 << 6) // receive data overrun error has occurred +#define CDC_SERIAL_STATE_PARITY (1 << 5) // parity error has occurred +#define CDC_SERIAL_STATE_FRAMING (1 << 4) // framing error has occurred +#define CDC_SERIAL_STATE_RING (1 << 3) // state of ring signal detection +#define CDC_SERIAL_STATE_BREAK (1 << 2) // state of break detection +#define CDC_SERIAL_STATE_TX_CARRIER (1 << 1) // state of transmission carrier +#define CDC_SERIAL_STATE_RX_CARRIER (1 << 0) // state of receiver carrier + + +/*---------------------------------------------------------------------------- + * Structures based on usbcdc11.pdf (www.usb.org) + *---------------------------------------------------------------------------*/ + +// Header functional descriptor +// (usbcdc11.pdf, 5.2.3.1) +// This header must precede any list of class-specific descriptors. + +#if defined ( __CC_ARM ) +typedef __packed struct _CDC_HEADER_DESCRIPTOR{ +#elif defined ( __GNUC__ ) +typedef struct __packed _CDC_HEADER_DESCRIPTOR{ +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _CDC_HEADER_DESCRIPTOR { +#endif + uint8_t bFunctionLength; // size of this descriptor in bytes + uint8_t bDescriptorType; // CS_INTERFACE descriptor type + uint8_t bDescriptorSubtype; // Header functional descriptor subtype + uint16_t bcdCDC; // USB CDC specification release version +} CDC_HEADER_DESCRIPTOR; + +//Call management functional descriptor +// (usbcdc11.pdf, 5.2.3.2) +// Describes the processing of calls for the communication class interface. +#if defined ( __CC_ARM ) +typedef __packed struct _CDC_CALL_MANAGEMENT_DESCRIPTOR{ +#elif defined ( __GNUC__ ) +typedef struct __packed _CDC_CALL_MANAGEMENT_DESCRIPTOR{ +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _CDC_CALL_MANAGEMENT_DESCRIPTOR { +#endif + uint8_t bFunctionLength; // size of this descriptor in bytes + uint8_t bDescriptorType; // CS_INTERFACE descriptor type + uint8_t bDescriptorSubtype; // call management functional descriptor subtype + uint8_t bmCapabilities; // capabilities that this configuration supports + uint8_t bDataInterface; // interface number of the data class interface used for call management (optional) +} CDC_CALL_MANAGEMENT_DESCRIPTOR; + +// Abstract control management functional descriptor +// (usbcdc11.pdf, 5.2.3.3) +// Describes the command supported by the communication interface class with the Abstract Control Model subclass code. +#if defined ( __CC_ARM ) +typedef __packed struct _CDC_ABSTRACT_CONTROL_MANAGEMENT_DESCRIPTOR{ +#elif defined ( __GNUC__ ) +typedef struct __packed _CDC_ABSTRACT_CONTROL_MANAGEMENT_DESCRIPTOR{ +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _CDC_ABSTRACT_CONTROL_MANAGEMENT_DESCRIPTOR { +#endif + uint8_t bFunctionLength; // size of this descriptor in bytes + uint8_t bDescriptorType; // CS_INTERFACE descriptor type + uint8_t bDescriptorSubtype; // abstract control management functional descriptor subtype + uint8_t bmCapabilities; // capabilities supported by this configuration +} CDC_ABSTRACT_CONTROL_MANAGEMENT_DESCRIPTOR; + +// Union functional descriptors +// (usbcdc11.pdf, 5.2.3.8) +// Describes the relationship between a group of interfaces that can be considered to form a functional unit. +#if defined ( __CC_ARM ) +typedef __packed struct _CDC_UNION_DESCRIPTOR{ +#elif defined ( __GNUC__ ) +typedef struct __packed _CDC_UNION_DESCRIPTOR{ +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _CDC_UNION_DESCRIPTOR { +#endif + uint8_t bFunctionLength; // size of this descriptor in bytes + uint8_t bDescriptorType; // CS_INTERFACE descriptor type + uint8_t bDescriptorSubtype; // union functional descriptor subtype + uint8_t bMasterInterface; // interface number designated as master +} CDC_UNION_DESCRIPTOR; + +// Union functional descriptors with one slave interface +// (usbcdc11.pdf, 5.2.3.8) +#if defined ( __CC_ARM ) +typedef __packed struct _CDC_UNION_1SLAVE_DESCRIPTOR{ +#elif defined ( __GNUC__ ) +typedef struct __packed _CDC_UNION_1SLAVE_DESCRIPTOR{ +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _CDC_UNION_1SLAVE_DESCRIPTOR { +#endif + CDC_UNION_DESCRIPTOR sUnion; // Union functional descriptor + uint8_t bSlaveInterfaces[1]; // Slave interface 0 +} CDC_UNION_1SLAVE_DESCRIPTOR; + +// Line coding structure +// Format of the data returned when a GetLineCoding request is received +// (usbcdc11.pdf, 6.2.13) +#if defined ( __CC_ARM ) +typedef __packed struct _CDC_LINE_CODING{ +#elif defined ( __GNUC__ ) +typedef struct __packed _CDC_LINE_CODING{ +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _CDC_LINE_CODING { +#endif + uint32_t dwDTERate; // Data terminal rate in bits per second + uint8_t bCharFormat; // Number of stop bits + uint8_t bParityType; // Parity bit type + uint8_t bDataBits; // Number of data bits +} CDC_LINE_CODING; + +// Notification header +// Data sent on the notification endpoint must follow this header. +// see USB_SETUP_PACKET in file usb.h +typedef USB_SETUP_PACKET CDC_NOTIFICATION_HEADER; + +#endif /* __CDC_H */ + diff --git a/new_cmsis/usb/cdcuser.c b/new_cmsis/usb/cdcuser.c new file mode 100755 index 0000000..3b4f52f --- /dev/null +++ b/new_cmsis/usb/cdcuser.c @@ -0,0 +1,379 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: cdcuser.c + * Purpose: USB Communication Device Class User module + * Version: V1.10 + *---------------------------------------------------------------------------- +* This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#include "lpc_types.h" + +#include "usb.h" +#include "usbhw.h" +#include "usbcfg.h" +#include "usbcore.h" +#include "cdc.h" +#include "cdcuser.h" +#include "serial.h" + + +unsigned char BulkBufIn [USB_CDC_BUFSIZE]; // Buffer to store USB IN packet +unsigned char BulkBufOut [USB_CDC_BUFSIZE]; // Buffer to store USB OUT packet +unsigned char NotificationBuf [10]; + +CDC_LINE_CODING CDC_LineCoding = {9600, 0, 0, 8}; +unsigned short CDC_SerialState = 0x0000; +unsigned short CDC_DepInEmpty = 1; // Data IN EP is empty + +/*---------------------------------------------------------------------------- + We need a buffer for incomming data on USB port because USB receives + much faster than UART transmits + *---------------------------------------------------------------------------*/ +/* Buffer masks */ +#define CDC_BUF_SIZE (64) // Output buffer in bytes (power 2) + // large enough for file transfer +#define CDC_BUF_MASK (CDC_BUF_SIZE-1ul) + +/* Buffer read / write macros */ +#define CDC_BUF_RESET(cdcBuf) (cdcBuf.rdIdx = cdcBuf.wrIdx = 0) +#define CDC_BUF_WR(cdcBuf, dataIn) (cdcBuf.data[CDC_BUF_MASK & cdcBuf.wrIdx++] = (dataIn)) +#define CDC_BUF_RD(cdcBuf) (cdcBuf.data[CDC_BUF_MASK & cdcBuf.rdIdx++]) +#define CDC_BUF_EMPTY(cdcBuf) (cdcBuf.rdIdx == cdcBuf.wrIdx) +#define CDC_BUF_FULL(cdcBuf) (cdcBuf.rdIdx == cdcBuf.wrIdx+1) +#define CDC_BUF_COUNT(cdcBuf) (CDC_BUF_MASK & (cdcBuf.wrIdx - cdcBuf.rdIdx)) + + +// CDC output buffer +typedef struct __CDC_BUF_T { + unsigned char data[CDC_BUF_SIZE]; + unsigned int wrIdx; + unsigned int rdIdx; +} CDC_BUF_T; + +CDC_BUF_T CDC_OutBuf; // buffer for all CDC Out data + +/*---------------------------------------------------------------------------- + read data from CDC_OutBuf + *---------------------------------------------------------------------------*/ +int CDC_RdOutBuf (char *buffer, const int *length) { + int bytesToRead, bytesRead; + + /* Read *length bytes, block if *bytes are not avaialable */ + bytesToRead = *length; + bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length); + bytesRead = bytesToRead; + + + // ... add code to check for underrun + + while (bytesToRead--) { + *buffer++ = CDC_BUF_RD(CDC_OutBuf); + } + return (bytesRead); +} + +/*---------------------------------------------------------------------------- + write data to CDC_OutBuf + *---------------------------------------------------------------------------*/ +int CDC_WrOutBuf (const char *buffer, int *length) { + int bytesToWrite, bytesWritten; + + // Write *length bytes + bytesToWrite = *length; + bytesWritten = bytesToWrite; + + + // ... add code to check for overwrite + + while (bytesToWrite) { + CDC_BUF_WR(CDC_OutBuf, *buffer++); // Copy Data to buffer + bytesToWrite--; + } + + return (bytesWritten); +} + +/*---------------------------------------------------------------------------- + check if character(s) are available at CDC_OutBuf + *---------------------------------------------------------------------------*/ +int CDC_OutBufAvailChar (int *availChar) { + + *availChar = CDC_BUF_COUNT(CDC_OutBuf); + + return (0); +} +/* end Buffer handling */ + + +/*---------------------------------------------------------------------------- + CDC Initialisation + Initializes the data structures and serial port + Parameters: None + Return Value: None + *---------------------------------------------------------------------------*/ +void CDC_Init (char portNum ) { + + if ( portNum == 0 ) + { + ser_OpenPort (0); + ser_InitPort0 (CDC_LineCoding.dwDTERate, + CDC_LineCoding.bDataBits, + CDC_LineCoding.bParityType, + CDC_LineCoding.bCharFormat); + } + else + { + ser_OpenPort (1); + ser_InitPort1 (CDC_LineCoding.dwDTERate, + CDC_LineCoding.bDataBits, + CDC_LineCoding.bParityType, + CDC_LineCoding.bCharFormat); + } + CDC_DepInEmpty = 1; + CDC_SerialState = CDC_GetSerialState(); + + CDC_BUF_RESET(CDC_OutBuf); +} + + +/*---------------------------------------------------------------------------- + CDC SendEncapsulatedCommand Request Callback + Called automatically on CDC SEND_ENCAPSULATED_COMMAND Request + Parameters: None (global SetupPacket and EP0Buf) + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_SendEncapsulatedCommand (void) { + + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC GetEncapsulatedResponse Request Callback + Called automatically on CDC Get_ENCAPSULATED_RESPONSE Request + Parameters: None (global SetupPacket and EP0Buf) + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_GetEncapsulatedResponse (void) { + + /* ... add code to handle request */ + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC SetCommFeature Request Callback + Called automatically on CDC Set_COMM_FATURE Request + Parameters: FeatureSelector + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_SetCommFeature (unsigned short wFeatureSelector) { + + /* ... add code to handle request */ + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC GetCommFeature Request Callback + Called automatically on CDC Get_COMM_FATURE Request + Parameters: FeatureSelector + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_GetCommFeature (unsigned short wFeatureSelector) { + + /* ... add code to handle request */ + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC ClearCommFeature Request Callback + Called automatically on CDC CLEAR_COMM_FATURE Request + Parameters: FeatureSelector + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_ClearCommFeature (unsigned short wFeatureSelector) { + + /* ... add code to handle request */ + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC SetLineCoding Request Callback + Called automatically on CDC SET_LINE_CODING Request + Parameters: none (global SetupPacket and EP0Buf) + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_SetLineCoding (void) { + + CDC_LineCoding.dwDTERate = (EP0Buf[0] << 0) + | (EP0Buf[1] << 8) + | (EP0Buf[2] << 16) + | (EP0Buf[3] << 24); + CDC_LineCoding.bCharFormat = EP0Buf[4]; + CDC_LineCoding.bParityType = EP0Buf[5]; + CDC_LineCoding.bDataBits = EP0Buf[6]; + +#if PORT_NUM + ser_ClosePort(1); + ser_OpenPort (1); + ser_InitPort1 (CDC_LineCoding.dwDTERate, + CDC_LineCoding.bDataBits, + CDC_LineCoding.bParityType, + CDC_LineCoding.bCharFormat); +#else + ser_ClosePort(0); + ser_OpenPort (0); + ser_InitPort0 (CDC_LineCoding.dwDTERate, + CDC_LineCoding.bDataBits, + CDC_LineCoding.bParityType, + CDC_LineCoding.bCharFormat); +#endif + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC GetLineCoding Request Callback + Called automatically on CDC GET_LINE_CODING Request + Parameters: None (global SetupPacket and EP0Buf) + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_GetLineCoding (void) { + + EP0Buf[0] = (CDC_LineCoding.dwDTERate >> 0) & 0xFF; + EP0Buf[1] = (CDC_LineCoding.dwDTERate >> 8) & 0xFF; + EP0Buf[2] = (CDC_LineCoding.dwDTERate >> 16) & 0xFF; + EP0Buf[3] = (CDC_LineCoding.dwDTERate >> 24) & 0xFF; + EP0Buf[4] = CDC_LineCoding.bCharFormat; + EP0Buf[5] = CDC_LineCoding.bParityType; + EP0Buf[6] = CDC_LineCoding.bDataBits; + + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC SetControlLineState Request Callback + Called automatically on CDC SET_CONTROL_LINE_STATE Request + Parameters: ControlSignalBitmap + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_SetControlLineState (unsigned short wControlSignalBitmap) { + + /* ... add code to handle request */ + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC SendBreak Request Callback + Called automatically on CDC Set_COMM_FATURE Request + Parameters: 0xFFFF start of Break + 0x0000 stop of Break + 0x#### Duration of Break + Return Value: TRUE - Success, FALSE - Error + *---------------------------------------------------------------------------*/ +uint32_t CDC_SendBreak (unsigned short wDurationOfBreak) { + + /* ... add code to handle request */ + return (TRUE); +} + + +/*---------------------------------------------------------------------------- + CDC_BulkIn call on DataIn Request + Parameters: none + Return Value: none + *---------------------------------------------------------------------------*/ +void CDC_BulkIn(void) { + int numBytesRead, numBytesAvail; + + ser_AvailChar (&numBytesAvail); + + // ... add code to check for overwrite + + numBytesRead = ser_Read ((char *)&BulkBufIn[0], &numBytesAvail); + + // send over USB + if (numBytesRead > 0) { + USB_WriteEP (CDC_DEP_IN, &BulkBufIn[0], numBytesRead); + } + else { + CDC_DepInEmpty = 1; + } +} + + +/*---------------------------------------------------------------------------- + CDC_BulkOut call on DataOut Request + Parameters: none + Return Value: none + *---------------------------------------------------------------------------*/ +void CDC_BulkOut(void) { + int numBytesRead; + + // get data from USB into intermediate buffer + numBytesRead = USB_ReadEP(CDC_DEP_OUT, &BulkBufOut[0]); + + // ... add code to check for overwrite + + // store data in a buffer to transmit it over serial interface + CDC_WrOutBuf ((char *)&BulkBufOut[0], &numBytesRead); + +} + + +/*---------------------------------------------------------------------------- + Get the SERIAL_STATE as defined in usbcdc11.pdf, 6.3.5, Table 69. + Parameters: none + Return Value: SerialState as defined in usbcdc11.pdf + *---------------------------------------------------------------------------*/ +unsigned short CDC_GetSerialState (void) { + unsigned short temp; + + CDC_SerialState = 0; + ser_LineState (&temp); + + if (temp & 0x8000) CDC_SerialState |= CDC_SERIAL_STATE_RX_CARRIER; + if (temp & 0x2000) CDC_SerialState |= CDC_SERIAL_STATE_TX_CARRIER; + if (temp & 0x0010) CDC_SerialState |= CDC_SERIAL_STATE_BREAK; + if (temp & 0x4000) CDC_SerialState |= CDC_SERIAL_STATE_RING; + if (temp & 0x0008) CDC_SerialState |= CDC_SERIAL_STATE_FRAMING; + if (temp & 0x0004) CDC_SerialState |= CDC_SERIAL_STATE_PARITY; + if (temp & 0x0002) CDC_SerialState |= CDC_SERIAL_STATE_OVERRUN; + + return (CDC_SerialState); +} + + +/*---------------------------------------------------------------------------- + Send the SERIAL_STATE notification as defined in usbcdc11.pdf, 6.3.5. + *---------------------------------------------------------------------------*/ +void CDC_NotificationIn (void) { + + NotificationBuf[0] = 0xA1; // bmRequestType + NotificationBuf[1] = CDC_NOTIFICATION_SERIAL_STATE; // bNotification (SERIAL_STATE) + NotificationBuf[2] = 0x00; // wValue + NotificationBuf[3] = 0x00; + NotificationBuf[4] = 0x00; // wIndex (Interface #, LSB first) + NotificationBuf[5] = 0x00; + NotificationBuf[6] = 0x02; // wLength (Data length = 2 bytes, LSB first) + NotificationBuf[7] = 0x00; + NotificationBuf[8] = (CDC_SerialState >> 0) & 0xFF; // UART State Bitmap (16bits, LSB first) + NotificationBuf[9] = (CDC_SerialState >> 8) & 0xFF; + + USB_WriteEP (CDC_CEP_IN, &NotificationBuf[0], 10); // send notification +} diff --git a/new_cmsis/usb/cdcuser.h b/new_cmsis/usb/cdcuser.h new file mode 100755 index 0000000..388f07a --- /dev/null +++ b/new_cmsis/usb/cdcuser.h @@ -0,0 +1,63 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: cdcuser.h + * Purpose: USB Communication Device Class User module Definitions + * Version: V1.10 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#ifndef __CDCUSER_H__ +#define __CDCUSER_H__ + +/* CDC buffer handling */ +extern int CDC_RdOutBuf (char *buffer, const int *length); +extern CDC_WrOutBuf (const char *buffer, int *length); +extern CDC_OutBufAvailChar (int *availChar); + + +/* CDC Data In/Out Endpoint Address */ +#define CDC_DEP_IN 0x82 +#define CDC_DEP_OUT 0x02 + +/* CDC Communication In Endpoint Address */ +#define CDC_CEP_IN 0x81 + +/* CDC Requests Callback Functions */ +extern uint32_t CDC_SendEncapsulatedCommand (void); +extern uint32_t CDC_GetEncapsulatedResponse (void); +extern uint32_t CDC_SetCommFeature (unsigned short wFeatureSelector); +extern uint32_t CDC_GetCommFeature (unsigned short wFeatureSelector); +extern uint32_t CDC_ClearCommFeature (unsigned short wFeatureSelector); +extern uint32_t CDC_GetLineCoding (void); +extern uint32_t CDC_SetLineCoding (void); +extern uint32_t CDC_SetControlLineState (unsigned short wControlSignalBitmap); +extern uint32_t CDC_SendBreak (unsigned short wDurationOfBreak); + +/* CDC Bulk Callback Functions */ +extern void CDC_BulkIn (void); +extern void CDC_BulkOut (void); + +/* CDC Notification Callback Function */ +extern void CDC_NotificationIn (void); + +/* CDC Initializtion Function */ +extern void CDC_Init (char portNum); + +/* CDC prepare the SERAIAL_STATE */ +extern unsigned short CDC_GetSerialState (void); + +/* flow control */ +extern unsigned short CDC_DepInEmpty; // DataEndPoint IN empty + +#endif /* __CDCUSER_H__ */ + diff --git a/new_cmsis/usb/lpc17xx-vcom.inf b/new_cmsis/usb/lpc17xx-vcom.inf new file mode 100755 index 0000000..82b6b0c --- /dev/null +++ b/new_cmsis/usb/lpc17xx-vcom.inf @@ -0,0 +1,65 @@ +; +; Keil - An ARM Company Comunication Device Class driver installation file +; (C)2007 Copyright +; + +[Version] +Signature="$Windows NT$" +Class=Ports +ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318} +Provider=%Keil% +;LayoutFile=layout.inf +DriverVer=01/06/07 + +[Manufacturer] +%Keil%=DeviceList + +[DestinationDirs] +DefaultDestDir=12 + +[SourceDisksFiles] + +[SourceDisksNames] + +[DeviceList] +%DESCRIPTION%=LPC17xxUSB, USB\VID_1FC9&PID_2002 + +;------------------------------------------------------------------------------ +; Windows 2000/XP Sections +;------------------------------------------------------------------------------ + +[LPC17xxUSB.nt] +include=mdmcpq.inf +CopyFiles=DriverCopyFiles +AddReg=LPC17xxUSB.nt.AddReg + +[DriverCopyFiles] +usbser.sys,,,0x20 + +[LPC17xxUSB.nt.AddReg] +HKR,,DevLoader,,*ntkern +HKR,,NTMPDriver,,usbser.sys +HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider" + +[LPC17xxUSB.nt.Services] +include=mdmcpq.inf +AddService=usbser, 0x00000002, DriverService + + +[LPC17xxUSB.nt.HW] +include=mdmcpq.inf + +[DriverService] +DisplayName=%DESCRIPTION% +ServiceType=1 +StartType=3 +ErrorControl=1 +ServiceBinary=%12%\usbser.sys + +;------------------------------------------------------------------------------ +; String Definitions +;------------------------------------------------------------------------------ + +[Strings] +NXP="NXP - Founded by Philips" +DESCRIPTION="LPC17xx USB VCom Port" diff --git a/new_cmsis/usb/lpc17xx_libcfg.h b/new_cmsis/usb/lpc17xx_libcfg.h new file mode 100755 index 0000000..4308dbd --- /dev/null +++ b/new_cmsis/usb/lpc17xx_libcfg.h @@ -0,0 +1,144 @@ +/***********************************************************************//** + * @file lpc17xx_libcfg.h + * @purpose Library configuration file + * @version 2.0 + * @date 21. May. 2010 + * @author NXP MCU SW Application Team + ************************************************************************** + * Software that is described herein is for illustrative purposes only + * which provides customers with programming information regarding the + * products. This software is supplied "AS IS" without any warranties. + * NXP Semiconductors assumes no responsibility or liability for the + * use of the software, conveys no license or title under any patent, + * copyright, or mask work right to the product. NXP Semiconductors + * reserves the right to make changes in the software without + * notification. NXP Semiconductors also make no representation or + * warranty that such application will be suitable for the specified + * use without further testing or modification. + **************************************************************************/ + +#ifndef LPC17XX_LIBCFG_H_ +#define LPC17XX_LIBCFG_H_ + +#include "lpc_types.h" + + +/************************** DEBUG MODE DEFINITIONS *********************************/ +/* Un-comment the line below to compile the library in DEBUG mode, this will expanse + the "CHECK_PARAM" macro in the FW library code */ + +#define DEBUG + + +/******************* PERIPHERAL FW LIBRARY CONFIGURATION DEFINITIONS ***********************/ + +/* Comment the line below to disable the specific peripheral inclusion */ + +/* DEBUG_FRAMWORK -------------------- */ +///#define _DBGFWK + +/* GPIO ------------------------------- */ +//#define _GPIO + +/* EXTI ------------------------------- */ +//#define _EXTI + +/* UART ------------------------------- */ +//#define _UART +//#define _UART0 +//#define _UART1 +//#define _UART2 +//#define _UART3 + +/* SPI ------------------------------- */ +//#define _SPI + +/* SYSTICK --------------------------- */ +//#define _SYSTICK + +/* SSP ------------------------------- */ +//#define _SSP +//#define _SSP0 +//#define _SSP1 + + +/* I2C ------------------------------- */ +//#define _I2C +//#define _I2C0 +//#define _I2C1 +//#define _I2C2 + +/* TIMER ------------------------------- */ +//#define _TIM + +/* WDT ------------------------------- */ +//#define _WDT + + +/* GPDMA ------------------------------- */ +//#define _GPDMA + + +/* DAC ------------------------------- */ +//#define _DAC + +/* DAC ------------------------------- */ +//#define _ADC + + +/* PWM ------------------------------- */ +//#define _PWM +//#define _PWM1 + +/* RTC ------------------------------- */ +//#define _RTC + +/* I2S ------------------------------- */ +//#define _I2S + +/* USB device ------------------------------- */ +//#define _USBDEV +//#define _USB_DMA + +/* QEI ------------------------------- */ +//#define _QEI + +/* MCPWM ------------------------------- */ +//#define _MCPWM + +/* CAN--------------------------------*/ +//#define _CAN + +/* RIT ------------------------------- */ +//#define _RIT + +/* EMAC ------------------------------ */ +//#define _EMAC + +/************************** GLOBAL/PUBLIC MACRO DEFINITIONS *********************************/ + +#ifdef DEBUG +/******************************************************************************* +* @brief The CHECK_PARAM macro is used for function's parameters check. +* It is used only if the library is compiled in DEBUG mode. +* @param[in] expr - If expr is false, it calls check_failed() function +* which reports the name of the source file and the source +* line number of the call that failed. +* - If expr is true, it returns no value. +* @return None +*******************************************************************************/ +#define CHECK_PARAM(expr) ((expr) ? (void)0 : check_failed((uint8_t *)__FILE__, __LINE__)) +#else +#define CHECK_PARAM(expr) +#endif /* DEBUG */ + + + +/************************** GLOBAL/PUBLIC FUNCTION DECLARATION *********************************/ + +#ifdef DEBUG +void check_failed(uint8_t *file, uint32_t line); +#endif + + +#endif /* LPC17XX_LIBCFG_H_ */ diff --git a/new_cmsis/usb/lpc_types.h b/new_cmsis/usb/lpc_types.h new file mode 100755 index 0000000..9cfb3be --- /dev/null +++ b/new_cmsis/usb/lpc_types.h @@ -0,0 +1,196 @@ +/***********************************************************************//** + * @file lpc_types.h + * @brief Contains the NXP ABL typedefs for C standard types. + * It is intended to be used in ISO C conforming development + * environments and checks for this insofar as it is possible + * to do so. + * @version 1.0 + * @date 27 Jul. 2008 + * @author wellsk + ************************************************************************** + * Software that is described herein is for illustrative purposes only + * which provides customers with programming information regarding the + * products. This software is supplied "AS IS" without any warranties. + * NXP Semiconductors assumes no responsibility or liability for the + * use of the software, conveys no license or title under any patent, + * copyright, or mask work right to the product. NXP Semiconductors + * reserves the right to make changes in the software without + * notification. NXP Semiconductors also make no representation or + * warranty that such application will be suitable for the specified + * use without further testing or modification. + **************************************************************************/ + +/* Type group ----------------------------------------------------------- */ +/** @defgroup LPC_Types LPC_Types + * @ingroup LPC1700CMSIS_FwLib_Drivers + * @{ + */ + +#ifndef LPC_TYPES_H +#define LPC_TYPES_H + +/* Includes ------------------------------------------------------------------- */ +#include + + +/* Public Types --------------------------------------------------------------- */ +/** @defgroup LPC_Types_Public_Types LPC_Types Public Types + * @{ + */ + +/** + * @brief Boolean Type definition + */ +typedef enum {FALSE = 0, TRUE = !FALSE} Bool; + +/** + * @brief Flag Status and Interrupt Flag Status type definition + */ +typedef enum {RESET = 0, SET = !RESET} FlagStatus, IntStatus, SetState; +#define PARAM_SETSTATE(State) ((State==RESET) || (State==SET)) + +/** + * @brief Functional State Definition + */ +typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; +#define PARAM_FUNCTIONALSTATE(State) ((State==DISABLE) || (State==ENABLE)) + +/** + * @ Status type definition + */ +typedef enum {ERROR = 0, SUCCESS = !ERROR} Status; + + +/** + * Read/Write transfer type mode (Block or non-block) + */ +typedef enum +{ + NONE_BLOCKING = 0, /**< None Blocking type */ + BLOCKING, /**< Blocking type */ +} TRANSFER_BLOCK_Type; + + +/** Pointer to Function returning Void (any number of parameters) */ +typedef void (*PFV)(); + +/** Pointer to Function returning int32_t (any number of parameters) */ +typedef int32_t(*PFI)(); + +/** + * @} + */ + + +/* Public Macros -------------------------------------------------------------- */ +/** @defgroup LPC_Types_Public_Macros LPC_Types Public Macros + * @{ + */ + +/* _BIT(n) sets the bit at position "n" + * _BIT(n) is intended to be used in "OR" and "AND" expressions: + * e.g., "(_BIT(3) | _BIT(7))". + */ +#undef _BIT +/* Set bit macro */ +#define _BIT(n) (1< = (any_expression) & _BITMASK(x), where 0 < x <= 32. + * If "any_expression" results in a value that is larger than can be + * contained in 'x' bits, the bits above 'x - 1' are masked off. When + * used with the _SBF example above, the example would be written: + * a_reg = ((_SBF(5,7) | _SBF(12,0xF)) & _BITMASK(16)) + * This ensures that the value written to a_reg is no wider than + * 16 bits, and makes the code easier to read and understand. + */ +#undef _BITMASK +/* Bitmask creation macro */ +#define _BITMASK(field_width) ( _BIT(field_width) - 1) + +/* NULL pointer */ +#ifndef NULL +#define NULL ((void*) 0) +#endif + +/* Number of elements in an array */ +#define NELEMENTS(array) (sizeof (array) / sizeof (array[0])) + +/* Static data/function define */ +#define STATIC static +/* External data/function define */ +#define EXTERN extern + +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + +/** + * @} + */ + + +/* Old Type Definition compatibility ------------------------------------------ */ +/** @addtogroup LPC_Types_Public_Types LPC_Types Public Types + * @{ + */ + +/** SMA type for character type */ +typedef char CHAR; + +/** SMA type for 8 bit unsigned value */ +typedef uint8_t UNS_8; + +/** SMA type for 8 bit signed value */ +typedef int8_t INT_8; + +/** SMA type for 16 bit unsigned value */ +typedef uint16_t UNS_16; + +/** SMA type for 16 bit signed value */ +typedef int16_t INT_16; + +/** SMA type for 32 bit unsigned value */ +typedef uint32_t UNS_32; + +/** SMA type for 32 bit signed value */ +typedef int32_t INT_32; + +/** SMA type for 64 bit signed value */ +typedef int64_t INT_64; + +/** SMA type for 64 bit unsigned value */ +typedef uint64_t UNS_64; + +/** 32 bit boolean type */ +typedef Bool BOOL_32; + +/** 16 bit boolean type */ +typedef Bool BOOL_16; + +/** 8 bit boolean type */ +typedef Bool BOOL_8; + +/** + * @} + */ + + +#endif /* LPC_TYPES_H */ + +/** + * @} + */ + +/* --------------------------------- End Of File ------------------------------ */ diff --git a/new_cmsis/usb/serial.c b/new_cmsis/usb/serial.c new file mode 100755 index 0000000..938ae18 --- /dev/null +++ b/new_cmsis/usb/serial.c @@ -0,0 +1,404 @@ +/*---------------------------------------------------------------------------- + * Name: serial.c + * Purpose: serial port handling for LPC17xx + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ +#include "../LPC17xx.h" // LPC17xx definitions +#include "lpc_types.h" +#include "serial.h" + + +/*---------------------------------------------------------------------------- + Defines for ring buffers + *---------------------------------------------------------------------------*/ +#define SER_BUF_SIZE (128) // serial buffer in bytes (power 2) +#define SER_BUF_MASK (SER_BUF_SIZE-1ul) // buffer size mask + +/* Buffer read / write macros */ +#define SER_BUF_RESET(serBuf) (serBuf.rdIdx = serBuf.wrIdx = 0) +#define SER_BUF_WR(serBuf, dataIn) (serBuf.data[SER_BUF_MASK & serBuf.wrIdx++] = (dataIn)) +#define SER_BUF_RD(serBuf) (serBuf.data[SER_BUF_MASK & serBuf.rdIdx++]) +#define SER_BUF_EMPTY(serBuf) (serBuf.rdIdx == serBuf.wrIdx) +#define SER_BUF_FULL(serBuf) (serBuf.rdIdx == serBuf.wrIdx+1) +#define SER_BUF_COUNT(serBuf) (SER_BUF_MASK & (serBuf.wrIdx - serBuf.rdIdx)) + +// buffer type +typedef struct __SER_BUF_T { + unsigned char data[SER_BUF_SIZE]; + unsigned int wrIdx; + unsigned int rdIdx; +} SER_BUF_T; + +unsigned long ser_txRestart; // NZ if TX restart is required +unsigned short ser_lineState; // ((msr << 8) | (lsr)) +SER_BUF_T ser_out; // Serial data buffers +SER_BUF_T ser_in; + +/*---------------------------------------------------------------------------- + open the serial port + *---------------------------------------------------------------------------*/ +void ser_OpenPort (char portNum) { + + if ( portNum == 0 ) + { + /* Port 0 */ + NVIC_DisableIRQ(UART0_IRQn); + PINCON->PINSEL0 &= ~0x000000F0; + PINCON->PINSEL0 |= 0x00000050; /* RxD0 is P0.3 and TxD0 is P0.2 */ + } + else + { + /* Port 1 */ + NVIC_DisableIRQ(UART1_IRQn); + PINCON->PINSEL4 &= ~0x0000000F; + PINCON->PINSEL4 |= 0x0000000A; /* Enable RxD1 P2.1, TxD1 P2.0 */ + } + return; +} + +/*---------------------------------------------------------------------------- + close the serial port + *---------------------------------------------------------------------------*/ +void ser_ClosePort (char portNum ) { + if ( portNum == 0 ) + { + /* POrt 0 */ + PINCON->PINSEL0 &= ~0x000000F0; + /* Disable the interrupt in the VIC and UART controllers */ + UART0->IER = 0; + NVIC_DisableIRQ(UART0_IRQn); + } + else + { + /* Port 1 */ + PINCON->PINSEL4 &= ~0x0000000F; + /* Disable the interrupt in the VIC and UART controllers */ + UART1->IER = 0; + NVIC_DisableIRQ(UART1_IRQn); + } + return; +} + +/*---------------------------------------------------------------------------- + initialize the serial port + *---------------------------------------------------------------------------*/ +void ser_InitPort0 (unsigned long baudrate, unsigned int databits, + unsigned int parity, unsigned int stopbits) { + + unsigned char lcr_p, lcr_s, lcr_d; + unsigned int dll; + unsigned int pclkdiv, pclk; + + switch (databits) { + case 5: // 5 Data bits + lcr_d = 0x00; + break; + case 6: // 6 Data bits + lcr_d = 0x01; + break; + case 7: // 7 Data bits + lcr_d = 0x02; + break; + case 8: // 8 Data bits + default: + lcr_d = 0x03; + break; + } + + switch (stopbits) { + case 1: // 1,5 Stop bits + case 2: // 2 Stop bits + lcr_s = 0x04; + break; + case 0: // 1 Stop bit + default: + lcr_s = 0x00; + break; + } + + switch (parity) { + case 1: // Parity Odd + lcr_p = 0x08; + break; + case 2: // Parity Even + lcr_p = 0x18; + break; + case 3: // Parity Mark + lcr_p = 0x28; + break; + case 4: // Parity Space + lcr_p = 0x38; + break; + case 0: // Parity None + default: + lcr_p = 0x00; + break; + } + + SER_BUF_RESET(ser_out); // reset out buffer + SER_BUF_RESET(ser_in); // reset in buffer + + /* Bit 6~7 is for UART0 */ + pclkdiv = (SC->PCLKSEL0 >> 6) & 0x03; + + switch ( pclkdiv ) + { + case 0x00: + default: + pclk = SystemFrequency/4; + break; + case 0x01: + pclk = SystemFrequency; + break; + case 0x02: + pclk = SystemFrequency/2; + break; + case 0x03: + pclk = SystemFrequency/8; + break; + } + + dll = (pclk/16)/baudrate ; /*baud rate */ + UART0->FDR = 0; // Fractional divider not used + UART0->LCR = 0x80 | lcr_d | lcr_p | lcr_s; // Data bits, Parity, Stop bit + UART0->DLL = dll; // Baud Rate depending on PCLK + UART0->DLM = (dll >> 8); // High divisor latch + UART0->LCR = 0x00 | lcr_d | lcr_p | lcr_s; // DLAB = 0 + UART0->IER = 0x03; // Enable TX/RX interrupts + + UART0->FCR = 0x07; /* Enable and reset TX and RX FIFO. */ + ser_txRestart = 1; // TX fifo is empty + + /* Enable the UART Interrupt */ + NVIC_EnableIRQ(UART0_IRQn); + return; +} + +/*---------------------------------------------------------------------------- + initialize the serial port + *---------------------------------------------------------------------------*/ +void ser_InitPort1 (unsigned long baudrate, unsigned int databits, + unsigned int parity, unsigned int stopbits) { + + unsigned char lcr_p, lcr_s, lcr_d; + unsigned int dll; + unsigned int pclkdiv, pclk; + + switch (databits) { + case 5: // 5 Data bits + lcr_d = 0x00; + break; + case 6: // 6 Data bits + lcr_d = 0x01; + break; + case 7: // 7 Data bits + lcr_d = 0x02; + break; + case 8: // 8 Data bits + default: + lcr_d = 0x03; + break; + } + + switch (stopbits) { + case 1: // 1,5 Stop bits + case 2: // 2 Stop bits + lcr_s = 0x04; + break; + case 0: // 1 Stop bit + default: + lcr_s = 0x00; + break; + } + + switch (parity) { + case 1: // Parity Odd + lcr_p = 0x08; + break; + case 2: // Parity Even + lcr_p = 0x18; + break; + case 3: // Parity Mark + lcr_p = 0x28; + break; + case 4: // Parity Space + lcr_p = 0x38; + break; + case 0: // Parity None + default: + lcr_p = 0x00; + break; + } + + SER_BUF_RESET(ser_out); // reset out buffer + SER_BUF_RESET(ser_in); // reset in buffer + + /* Bit 8,9 are for UART1 */ + pclkdiv = (SC->PCLKSEL0 >> 8) & 0x03; + + switch ( pclkdiv ) + { + case 0x00: + default: + pclk = SystemFrequency/4; + break; + case 0x01: + pclk = SystemFrequency; + break; + case 0x02: + pclk = SystemFrequency/2; + break; + case 0x03: + pclk = SystemFrequency/8; + break; + } + + dll = (pclk/16)/baudrate ; /*baud rate */ + UART1->FDR = 0; // Fractional divider not used + UART1->LCR = 0x80 | lcr_d | lcr_p | lcr_s; // Data bits, Parity, Stop bit + UART1->DLL = dll; // Baud Rate depending on PCLK + UART1->DLM = (dll >> 8); // High divisor latch + UART1->LCR = 0x00 | lcr_d | lcr_p | lcr_s; // DLAB = 0 + UART1->IER = 0x03; // Enable TX/RX interrupts + + UART1->FCR = 0x07; /* Enable and reset TX and RX FIFO. */ + ser_txRestart = 1; // TX fifo is empty + + /* Enable the UART Interrupt */ + NVIC_EnableIRQ(UART1_IRQn); + return; +} + +/*---------------------------------------------------------------------------- + read data from serial port + *---------------------------------------------------------------------------*/ +int ser_Read (char *buffer, const int *length) { + int bytesToRead, bytesRead; + + /* Read *length bytes, block if *bytes are not avaialable */ + bytesToRead = *length; + bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length); + bytesRead = bytesToRead; + + while (bytesToRead--) { + while (SER_BUF_EMPTY(ser_in)); // Block until data is available if none + *buffer++ = SER_BUF_RD(ser_in); + } + return (bytesRead); +} + +/*---------------------------------------------------------------------------- + write data to the serial port + *---------------------------------------------------------------------------*/ +int ser_Write (char portNum, const char *buffer, int *length) { + int bytesToWrite, bytesWritten; + + // Write *length bytes + bytesToWrite = *length; + bytesWritten = bytesToWrite; + + while (!SER_BUF_EMPTY(ser_out)); // Block until space is available if none + while (bytesToWrite) { + SER_BUF_WR(ser_out, *buffer++); // Read Rx FIFO to buffer + bytesToWrite--; + } + + if (ser_txRestart) { + ser_txRestart = 0; + if ( portNum == 0 ) + { + UART0->THR = SER_BUF_RD(ser_out); // Write to the Tx Register + } + else + { + UART1->THR = SER_BUF_RD(ser_out); // Write to the Tx Register + } + } + + return (bytesWritten); +} + +/*---------------------------------------------------------------------------- + check if character(s) are available at the serial interface + *---------------------------------------------------------------------------*/ +void ser_AvailChar (int *availChar) { + + *availChar = SER_BUF_COUNT(ser_in); + +} + +/*---------------------------------------------------------------------------- + read the line state of the serial port + *---------------------------------------------------------------------------*/ +void ser_LineState (unsigned short *lineState) { + + *lineState = ser_lineState; + ser_lineState = 0; + +} + +/*---------------------------------------------------------------------------- + serial port 0 interrupt + *---------------------------------------------------------------------------*/ +void UART0_IRQHandler(void) +{ + volatile unsigned long iir; + + iir = UART0->IIR; + + if ((iir & 0x4) || (iir & 0xC)) { // RDA or CTI pending + while (UART0->LSR & 0x01) { // Rx FIFO is not empty + SER_BUF_WR(ser_in, UART0->RBR); // Read Rx FIFO to buffer + } + } + if ((iir & 0x2)) { // TXMIS pending + if (SER_BUF_COUNT(ser_out) != 0) { + UART0->THR = SER_BUF_RD(ser_out); // Write to the Tx FIFO + ser_txRestart = 0; + } + else { + ser_txRestart = 1; + } + } + ser_lineState = UART0->LSR & 0x1E; // update linestate + return; +} + +/*---------------------------------------------------------------------------- + serial port 1 interrupt + *---------------------------------------------------------------------------*/ +void UART1_IRQHandler(void) +{ + volatile unsigned long iir; + + iir = UART1->IIR; + + if ((iir & 0x4) || (iir & 0xC)) { // RDA or CTI pending + while (UART1->LSR & 0x01) { // Rx FIFO is not empty + SER_BUF_WR(ser_in, UART1->RBR); // Read Rx FIFO to buffer + } + } + if ((iir & 0x2)) { // TXMIS pending + if (SER_BUF_COUNT(ser_out) != 0) { + UART1->THR = SER_BUF_RD(ser_out); // Write to the Tx FIFO + ser_txRestart = 0; + } + else { + ser_txRestart = 1; + } + } + ser_lineState = ((UART1->MSR<<8)|UART1->LSR) & 0xE01E; // update linestate + return; +} + + diff --git a/new_cmsis/usb/serial.h b/new_cmsis/usb/serial.h new file mode 100755 index 0000000..83ffd55 --- /dev/null +++ b/new_cmsis/usb/serial.h @@ -0,0 +1,30 @@ +/*---------------------------------------------------------------------------- + * Name: serial.h + * Purpose: serial port handling + * Version: V1.10 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#define PORT_NUM 1 + +/*---------------------------------------------------------------------------- + Serial interface related prototypes + *---------------------------------------------------------------------------*/ +extern void ser_OpenPort (char portNum); +extern void ser_ClosePort (char portNum); +extern void ser_InitPort0 (unsigned long baudrate, unsigned int databits, unsigned int parity, unsigned int stopbits); +extern void ser_InitPort1 (unsigned long baudrate, unsigned int databits, unsigned int parity, unsigned int stopbits); +extern void ser_AvailChar (int *availChar); +extern int ser_Write (char portNum, const char *buffer, int *length); +extern int ser_Read (char *buffer, const int *length); +extern void ser_LineState (unsigned short *lineState); + diff --git a/new_cmsis/usb/startup.c b/new_cmsis/usb/startup.c new file mode 100644 index 0000000..07fd0fa --- /dev/null +++ b/new_cmsis/usb/startup.c @@ -0,0 +1,316 @@ +//***************************************************************************** +// +// startup_gcc.c - Startup code for use with GNU tools. +// +// Copyright (c) 2009 Luminary Micro, Inc. All rights reserved. +// Software License Agreement +// +// Luminary Micro, Inc. (LMI) is supplying this software for use solely and +// exclusively on LMI's microcontroller products. +// +// The software is owned by LMI and/or its suppliers, and is protected under +// applicable copyright laws. All rights are reserved. You may not combine +// this software with "viral" open-source software in order to form a larger +// program. Any use in violation of the foregoing restrictions may subject +// the user to criminal sanctions under applicable laws, as well as to civil +// liability for the breach of the terms and conditions of this license. +// +// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED +// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. +// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR +// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. +// +// This is part of revision 32 of the Stellaris CMSIS Package. +// +//***************************************************************************** + +#define WEAK __attribute__ ((weak)) + +//***************************************************************************** +// +// Forward declaration of the default fault handlers. +// +//***************************************************************************** +void WEAK Reset_Handler(void); +static void Default_Handler(void); +void WEAK NMI_Handler(void); +void WEAK HardFault_Handler(void); +void WEAK MemManage_Handler(void); +void WEAK BusFault_Handler(void); +void WEAK UsageFault_Handler(void); +void WEAK MemManage_Handler(void); +void WEAK SVC_Handler(void); +void WEAK DebugMon_Handler(void); +void WEAK PendSV_Handler(void); +void WEAK SysTick_Handler(void); +void WEAK GPIOPortA_IRQHandler(void); +void WEAK GPIOPortB_IRQHandler(void); +void WEAK GPIOPortC_IRQHandler(void); +void WEAK GPIOPortD_IRQHandler(void); +void WEAK GPIOPortE_IRQHandler(void); +void WEAK UART0_IRQHandler(void); +void WEAK UART1_IRQHandler(void); +void WEAK SSI0_IRQHandler(void); +void WEAK I2C0_IRQHandler(void); +void WEAK PWMFault_IRQHandler(void); +void WEAK PWMGen0_IRQHandler(void); +void WEAK PWMGen1_IRQHandler(void); +void WEAK PWMGen2_IRQHandler(void); +void WEAK QEI0_IRQHandler(void); +void WEAK ADCSeq0_IRQHandler(void); +void WEAK ADCSeq1_IRQHandler(void); +void WEAK ADCSeq2_IRQHandler(void); +void WEAK ADCSeq3_IRQHandler(void); +void WEAK Watchdog_IRQHandler(void); +void WEAK Timer0A_IRQHandler(void); +void WEAK Timer0B_IRQHandler(void); +void WEAK Timer1A_IRQHandler(void); +void WEAK Timer1B_IRQHandler(void); +void WEAK Timer2A_IRQHandler(void); +void WEAK Timer2B_IRQHandler(void); +void WEAK Comp0_IRQHandler(void); +void WEAK Comp1_IRQHandler(void); +void WEAK Comp2_IRQHandler(void); +void WEAK SysCtrl_IRQHandler(void); +void WEAK FlashCtrl_IRQHandler(void); +void WEAK GPIOPortF_IRQHandler(void); +void WEAK GPIOPortG_IRQHandler(void); +void WEAK GPIOPortH_IRQHandler(void); +void WEAK UART2_IRQHandler(void); +void WEAK SSI1_IRQHandler(void); +void WEAK Timer3A_IRQHandler(void); +void WEAK Timer3B_IRQHandler(void); +void WEAK I2C1_IRQHandler(void); +void WEAK QEI1_IRQHandler(void); +void WEAK CAN0_IRQHandler(void); +void WEAK CAN1_IRQHandler(void); +void WEAK CAN2_IRQHandler(void); +void WEAK Ethernet_IRQHandler(void); +void WEAK Hibernate_IRQHandler(void); + +//***************************************************************************** +// +// The entry point for the application. +// +//***************************************************************************** +extern int main(void); + +//***************************************************************************** +// +// Reserve space for the system stack. +// +//***************************************************************************** +static unsigned long pulStack[64]; + +//***************************************************************************** +// +// The vector table. Note that the proper constructs must be placed on this to +// ensure that it ends up at physical address 0x0000.0000. +// +//***************************************************************************** +__attribute__ ((section(".isr_vector"))) +void (* const g_pfnVectors[])(void) = +{ + (void (*)(void))((unsigned long)pulStack + sizeof(pulStack)), + // The initial stack pointer + Reset_Handler, // The reset handler + NMI_Handler, // The NMI handler + HardFault_Handler, // The hard fault handler + MemManage_Handler, // The MPU fault handler + BusFault_Handler, // The bus fault handler + UsageFault_Handler, // The usage fault handler + 0xefff9d6e, // Reserved + 0, // Reserved + 0, // Reserved + 0, // Reserved + SVC_Handler, // SVCall handler + DebugMon_Handler, // Debug monitor handler + 0, // Reserved + PendSV_Handler, // The PendSV handler + SysTick_Handler, // The SysTick handler + + // + // External Interrupts + // + GPIOPortA_IRQHandler, // GPIO Port A + GPIOPortB_IRQHandler, // GPIO Port B + GPIOPortC_IRQHandler, // GPIO Port C + GPIOPortD_IRQHandler, // GPIO Port D + GPIOPortE_IRQHandler, // GPIO Port E + UART0_IRQHandler, // UART0 Rx and Tx + UART1_IRQHandler, // UART1 Rx and Tx + SSI0_IRQHandler, // SSI0 Rx and Tx + I2C0_IRQHandler, // I2C0 Master and Slave + PWMFault_IRQHandler, // PWM Fault + PWMGen0_IRQHandler, // PWM Generator 0 + PWMGen1_IRQHandler, // PWM Generator 1 + PWMGen2_IRQHandler, // PWM Generator 2 + QEI0_IRQHandler, // Quadrature Encoder 0 + ADCSeq0_IRQHandler, // ADC Sequence 0 + ADCSeq1_IRQHandler, // ADC Sequence 1 + ADCSeq2_IRQHandler, // ADC Sequence 2 + ADCSeq3_IRQHandler, // ADC Sequence 3 + Watchdog_IRQHandler, // Watchdog timer + Timer0A_IRQHandler, // Timer 0 subtimer A + Timer0B_IRQHandler, // Timer 0 subtimer B + Timer1A_IRQHandler, // Timer 1 subtimer A + Timer1B_IRQHandler, // Timer 1 subtimer B + Timer2A_IRQHandler, // Timer 2 subtimer A + Timer2B_IRQHandler, // Timer 2 subtimer B + Comp0_IRQHandler, // Analog Comparator 0 + Comp1_IRQHandler, // Analog Comparator 1 + Comp2_IRQHandler, // Analog Comparator 2 + SysCtrl_IRQHandler, // System Control (PLL, OSC, BO) + FlashCtrl_IRQHandler, // FLASH Control + GPIOPortF_IRQHandler, // GPIO Port F + GPIOPortG_IRQHandler, // GPIO Port G + GPIOPortH_IRQHandler, // GPIO Port H + UART2_IRQHandler, // UART2 Rx and Tx + SSI1_IRQHandler, // SSI1 Rx and Tx + Timer3A_IRQHandler, // Timer 3 subtimer A + Timer3B_IRQHandler, // Timer 3 subtimer B + I2C1_IRQHandler, // I2C1 Master and Slave + QEI1_IRQHandler, // Quadrature Encoder 1 + CAN0_IRQHandler, // CAN0 + CAN1_IRQHandler, // CAN1 + CAN2_IRQHandler, // CAN2 + Ethernet_IRQHandler, // Ethernet + Hibernate_IRQHandler // Hibernate +}; + +//***************************************************************************** +// +// The following are constructs created by the linker, indicating where the +// the "data" and "bss" segments reside in memory. The initializers for the +// for the "data" segment resides immediately following the "text" segment. +// +//***************************************************************************** +extern unsigned long _etext; +extern unsigned long _sdata; +extern unsigned long _edata; +extern unsigned long _sbss; +extern unsigned long _ebss; + +//***************************************************************************** +// +// This is the code that gets called when the processor first starts execution +// following a reset event. Only the absolutely necessary set is performed, +// after which the application supplied entry() routine is called. Any fancy +// actions (such as making decisions based on the reset cause register, and +// resetting the bits in that register) are left solely in the hands of the +// application. +// +//***************************************************************************** +void +Reset_Handler(void) +{ + unsigned long *pulSrc, *pulDest; + + // + // Copy the data segment initializers from flash to SRAM. + // + pulSrc = &_etext; + for(pulDest = &_sdata; pulDest < &_edata; ) + { + *pulDest++ = *pulSrc++; + } + + // + // Zero fill the bss segment. This is done with inline assembly since this + // will clear the value of pulDest if it is not kept in a register. + // + __asm(" ldr r0, =_sbss\n" + " ldr r1, =_ebss\n" + " mov r2, #0\n" + " .thumb_func\n" + "zero_loop:\n" + " cmp r0, r1\n" + " it lt\n" + " strlt r2, [r0], #4\n" + " blt zero_loop"); + + // + // Call the application's entry point. + // + main(); +} + +//***************************************************************************** +// +// Provide weak aliases for each Exception handler to the Default_Handler. +// As they are weak aliases, any function with the same name will override +// this definition. +// +//***************************************************************************** +#pragma weak NMI_Handler = Default_Handler +#pragma weak HardFault_Handler = Default_Handler +#pragma weak MemManage_Handler = Default_Handler +#pragma weak BusFault_Handler = Default_Handler +#pragma weak UsageFault_Handler = Default_Handler +#pragma weak SVC_Handler = Default_Handler +#pragma weak DebugMon_Handler = Default_Handler +#pragma weak PendSV_Handler = Default_Handler +#pragma weak SysTick_Handler = Default_Handler +#pragma weak GPIOPortA_IRQHandler = Default_Handler +#pragma weak GPIOPortB_IRQHandler = Default_Handler +#pragma weak GPIOPortC_IRQHandler = Default_Handler +#pragma weak GPIOPortD_IRQHandler = Default_Handler +#pragma weak GPIOPortE_IRQHandler = Default_Handler +#pragma weak UART0_IRQHandler = Default_Handler +#pragma weak UART1_IRQHandler = Default_Handler +#pragma weak SSI0_IRQHandler = Default_Handler +#pragma weak I2C0_IRQHandler = Default_Handler +#pragma weak PWMFault_IRQHandler = Default_Handler +#pragma weak PWMGen0_IRQHandler = Default_Handler +#pragma weak PWMGen1_IRQHandler = Default_Handler +#pragma weak PWMGen2_IRQHandler = Default_Handler +#pragma weak QEI0_IRQHandler = Default_Handler +#pragma weak ADCSeq0_IRQHandler = Default_Handler +#pragma weak ADCSeq1_IRQHandler = Default_Handler +#pragma weak ADCSeq2_IRQHandler = Default_Handler +#pragma weak ADCSeq3_IRQHandler = Default_Handler +#pragma weak Watchdog_IRQHandler = Default_Handler +#pragma weak Timer0A_IRQHandler = Default_Handler +#pragma weak Timer0B_IRQHandler = Default_Handler +#pragma weak Timer1A_IRQHandler = Default_Handler +#pragma weak Timer1B_IRQHandler = Default_Handler +#pragma weak Timer2A_IRQHandler = Default_Handler +#pragma weak Timer2B_IRQHandler = Default_Handler +#pragma weak Comp0_IRQHandler = Default_Handler +#pragma weak Comp1_IRQHandler = Default_Handler +#pragma weak Comp2_IRQHandler = Default_Handler +#pragma weak SysCtrl_IRQHandler = Default_Handler +#pragma weak FlashCtrl_IRQHandler = Default_Handler +#pragma weak GPIOPortF_IRQHandler = Default_Handler +#pragma weak GPIOPortG_IRQHandler = Default_Handler +#pragma weak GPIOPortH_IRQHandler = Default_Handler +#pragma weak UART2_IRQHandler = Default_Handler +#pragma weak SSI1_IRQHandler = Default_Handler +#pragma weak Timer3A_IRQHandler = Default_Handler +#pragma weak Timer3B_IRQHandler = Default_Handler +#pragma weak I2C1_IRQHandler = Default_Handler +#pragma weak QEI1_IRQHandler = Default_Handler +#pragma weak CAN0_IRQHandler = Default_Handler +#pragma weak CAN1_IRQHandler = Default_Handler +#pragma weak CAN2_IRQHandler = Default_Handler +#pragma weak Ethernet_IRQHandler = Default_Handler +#pragma weak Hibernate_IRQHandler = Default_Handler + +//***************************************************************************** +// +// This is the code that gets called when the processor receives an unexpected +// interrupt. This simply enters an infinite loop, preserving the system state +// for examination by a debugger. +// +//***************************************************************************** +static void +Default_Handler(void) +{ + // + // Go into an infinite loop. + // + while(1) + { + } +} diff --git a/new_cmsis/usb/usb.h b/new_cmsis/usb/usb.h new file mode 100755 index 0000000..21db7b4 --- /dev/null +++ b/new_cmsis/usb/usb.h @@ -0,0 +1,352 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usb.h + * Purpose: USB Definitions + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#ifndef __USB_H__ +#define __USB_H__ +#include "lpc_types.h" + +#if defined ( __GNUC__ ) +#define __packed __attribute__((__packed__)) +#endif + +#if defined ( __CC_ARM ) +typedef __packed union { +#elif defined ( __GNUC__ ) +typedef union __packed { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef union { +#endif + uint16_t W; +#if defined ( __CC_ARM ) + __packed struct { +#elif defined ( __GNUC__ ) + struct __packed { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) + struct { +#endif + uint8_t L; + uint8_t H; + } WB; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif +} WORD_BYTE; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + + +/* bmRequestType.Dir */ +#define REQUEST_HOST_TO_DEVICE 0 +#define REQUEST_DEVICE_TO_HOST 1 + +/* bmRequestType.Type */ +#define REQUEST_STANDARD 0 +#define REQUEST_CLASS 1 +#define REQUEST_VENDOR 2 +#define REQUEST_RESERVED 3 + +/* bmRequestType.Recipient */ +#define REQUEST_TO_DEVICE 0 +#define REQUEST_TO_INTERFACE 1 +#define REQUEST_TO_ENDPOINT 2 +#define REQUEST_TO_OTHER 3 + +/* bmRequestType Definition */ +#if defined ( __CC_ARM ) +typedef __packed union _REQUEST_TYPE { +#elif defined ( __GNUC__ ) +typedef union __packed _REQUEST_TYPE { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef union _REQUEST_TYPE { +#endif +#if defined ( __CC_ARM ) + __packed struct _BM { +#elif defined ( __GNUC__ ) + struct __packed _BM { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) + struct _BM { +#endif + uint8_t Recipient : 5; + uint8_t Type : 2; + uint8_t Dir : 1; + } BM; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + uint8_t B; +} REQUEST_TYPE; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + +/* USB Standard Request Codes */ +#define USB_REQUEST_GET_STATUS 0 +#define USB_REQUEST_CLEAR_FEATURE 1 +#define USB_REQUEST_SET_FEATURE 3 +#define USB_REQUEST_SET_ADDRESS 5 +#define USB_REQUEST_GET_DESCRIPTOR 6 +#define USB_REQUEST_SET_DESCRIPTOR 7 +#define USB_REQUEST_GET_CONFIGURATION 8 +#define USB_REQUEST_SET_CONFIGURATION 9 +#define USB_REQUEST_GET_INTERFACE 10 +#define USB_REQUEST_SET_INTERFACE 11 +#define USB_REQUEST_SYNC_FRAME 12 + +/* USB GET_STATUS Bit Values */ +#define USB_GETSTATUS_SELF_POWERED 0x01 +#define USB_GETSTATUS_REMOTE_WAKEUP 0x02 +#define USB_GETSTATUS_ENDPOINT_STALL 0x01 + +/* USB Standard Feature selectors */ +#define USB_FEATURE_ENDPOINT_STALL 0 +#define USB_FEATURE_REMOTE_WAKEUP 1 + +/* USB Default Control Pipe Setup Packet */ +#if defined ( __CC_ARM ) +typedef __packed struct _USB_SETUP_PACKET { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_SETUP_PACKET { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_SETUP_PACKET { +#endif + REQUEST_TYPE bmRequestType; + uint8_t bRequest; + WORD_BYTE wValue; + WORD_BYTE wIndex; + uint16_t wLength; +} USB_SETUP_PACKET; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + + +/* USB Descriptor Types */ +#define USB_DEVICE_DESCRIPTOR_TYPE 1 +#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2 +#define USB_STRING_DESCRIPTOR_TYPE 3 +#define USB_INTERFACE_DESCRIPTOR_TYPE 4 +#define USB_ENDPOINT_DESCRIPTOR_TYPE 5 +#define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE 6 +#define USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE 7 +#define USB_INTERFACE_POWER_DESCRIPTOR_TYPE 8 +#define USB_OTG_DESCRIPTOR_TYPE 9 +#define USB_DEBUG_DESCRIPTOR_TYPE 10 +#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE 11 + +/* USB Device Classes */ +#define USB_DEVICE_CLASS_RESERVED 0x00 +#define USB_DEVICE_CLASS_AUDIO 0x01 +#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02 +#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03 +#define USB_DEVICE_CLASS_MONITOR 0x04 +#define USB_DEVICE_CLASS_PHYSICAL_INTERFACE 0x05 +#define USB_DEVICE_CLASS_POWER 0x06 +#define USB_DEVICE_CLASS_PRINTER 0x07 +#define USB_DEVICE_CLASS_STORAGE 0x08 +#define USB_DEVICE_CLASS_HUB 0x09 +#define USB_DEVICE_CLASS_MISCELLANEOUS 0xEF +#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF + +/* bmAttributes in Configuration Descriptor */ +#define USB_CONFIG_POWERED_MASK 0x40 +#define USB_CONFIG_BUS_POWERED 0x80 +#define USB_CONFIG_SELF_POWERED 0xC0 +#define USB_CONFIG_REMOTE_WAKEUP 0x20 + +/* bMaxPower in Configuration Descriptor */ +#define USB_CONFIG_POWER_MA(mA) ((mA)/2) + +/* bEndpointAddress in Endpoint Descriptor */ +#define USB_ENDPOINT_DIRECTION_MASK 0x80 +#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00) +#define USB_ENDPOINT_IN(addr) ((addr) | 0x80) + +/* bmAttributes in Endpoint Descriptor */ +#define USB_ENDPOINT_TYPE_MASK 0x03 +#define USB_ENDPOINT_TYPE_CONTROL 0x00 +#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01 +#define USB_ENDPOINT_TYPE_BULK 0x02 +#define USB_ENDPOINT_TYPE_INTERRUPT 0x03 +#define USB_ENDPOINT_SYNC_MASK 0x0C +#define USB_ENDPOINT_SYNC_NO_SYNCHRONIZATION 0x00 +#define USB_ENDPOINT_SYNC_ASYNCHRONOUS 0x04 +#define USB_ENDPOINT_SYNC_ADAPTIVE 0x08 +#define USB_ENDPOINT_SYNC_SYNCHRONOUS 0x0C +#define USB_ENDPOINT_USAGE_MASK 0x30 +#define USB_ENDPOINT_USAGE_DATA 0x00 +#define USB_ENDPOINT_USAGE_FEEDBACK 0x10 +#define USB_ENDPOINT_USAGE_IMPLICIT_FEEDBACK 0x20 +#define USB_ENDPOINT_USAGE_RESERVED 0x30 + +/* USB Standard Device Descriptor */ +#if defined ( __CC_ARM ) +typedef __packed struct _USB_DEVICE_DESCRIPTOR { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_DEVICE_DESCRIPTOR { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_DEVICE_DESCRIPTOR { +#endif + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize0; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +} USB_DEVICE_DESCRIPTOR; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + +/* USB 2.0 Device Qualifier Descriptor */ +#if defined ( __CC_ARM ) +typedef __packed struct _USB_DEVICE_QUALIFIER_DESCRIPTOR { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_DEVICE_QUALIFIER_DESCRIPTOR { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR { +#endif + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize0; + uint8_t bNumConfigurations; + uint8_t bReserved; +} USB_DEVICE_QUALIFIER_DESCRIPTOR; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + +#if defined ( __CC_ARM ) +typedef __packed struct _USB_CONFIGURATION_DESCRIPTOR { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_CONFIGURATION_DESCRIPTOR { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_CONFIGURATION_DESCRIPTOR { +#endif + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t bMaxPower; +} USB_CONFIGURATION_DESCRIPTOR; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + +/* USB Standard Interface Descriptor */ +#if defined ( __CC_ARM ) +typedef __packed struct _USB_INTERFACE_DESCRIPTOR { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_INTERFACE_DESCRIPTOR { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_INTERFACE_DESCRIPTOR { +#endif + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; +} USB_INTERFACE_DESCRIPTOR; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + +/* USB Standard Endpoint Descriptor */ +#if defined ( __CC_ARM ) +typedef __packed struct _USB_ENDPOINT_DESCRIPTOR { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_ENDPOINT_DESCRIPTOR { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_ENDPOINT_DESCRIPTOR { +#endif + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint16_t wMaxPacketSize; + uint8_t bInterval; +} USB_ENDPOINT_DESCRIPTOR; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + +/* USB String Descriptor */ +#if defined ( __CC_ARM ) +typedef __packed struct _USB_STRING_DESCRIPTOR { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_STRING_DESCRIPTOR { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_STRING_DESCRIPTOR { +#endif + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bString/*[]*/; +} USB_STRING_DESCRIPTOR; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + +/* USB Common Descriptor */ +#if defined ( __CC_ARM ) +typedef __packed struct _USB_COMMON_DESCRIPTOR { +#elif defined ( __GNUC__ ) +typedef struct __packed _USB_COMMON_DESCRIPTOR { +#elif defined ( __IAR_SYSTEMS_ICC__ ) +#pragma pack(1) +typedef struct _USB_COMMON_DESCRIPTOR { +#endif + uint8_t bLength; + uint8_t bDescriptorType; +} USB_COMMON_DESCRIPTOR; +#ifdef __IAR_SYSTEMS_ICC__ +#pragma pack() +#endif + + + +#endif /* __USB_H__ */ diff --git a/new_cmsis/usb/usbcfg.h b/new_cmsis/usb/usbcfg.h new file mode 100755 index 0000000..b8610da --- /dev/null +++ b/new_cmsis/usb/usbcfg.h @@ -0,0 +1,181 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbcfg.h + * Purpose: USB Custom Configuration + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------- + * History: + * V1.20 Added vendor specific support + * V1.00 Initial Version + *---------------------------------------------------------------------------*/ + +#ifndef __USBCFG_H__ +#define __USBCFG_H__ + + +//*** <<< Use Configuration Wizard in Context Menu >>> *** + + +/* +// USB Configuration +// USB Power +// Default Power Setting +// <0=> Bus-powered +// <1=> Self-powered +// Max Number of Interfaces <1-256> +// Max Number of Endpoints <1-32> +// Max Endpoint 0 Packet Size +// <8=> 8 Bytes <16=> 16 Bytes <32=> 32 Bytes <64=> 64 Bytes +// DMA Transfer +// Use DMA for selected Endpoints +// Endpoint 0 Out +// Endpoint 0 In +// Endpoint 1 Out +// Endpoint 1 In +// Endpoint 2 Out +// Endpoint 2 In +// Endpoint 3 Out +// Endpoint 3 In +// Endpoint 4 Out +// Endpoint 4 In +// Endpoint 5 Out +// Endpoint 5 In +// Endpoint 6 Out +// Endpoint 6 In +// Endpoint 7 Out +// Endpoint 7 In +// Endpoint 8 Out +// Endpoint 8 In +// Endpoint 9 Out +// Endpoint 9 In +// Endpoint 10 Out +// Endpoint 10 In +// Endpoint 11 Out +// Endpoint 11 In +// Endpoint 12 Out +// Endpoint 12 In +// Endpoint 13 Out +// Endpoint 13 In +// Endpoint 14 Out +// Endpoint 14 In +// Endpoint 15 Out +// Endpoint 15 In +// +// +*/ + +#define USB_POWER 0 +#define USB_IF_NUM 4 +#define USB_EP_NUM 32 +#define USB_MAX_PACKET0 8 +#define USB_DMA 0 +#define USB_DMA_EP 0x00000000 + + +/* +// USB Event Handlers +// Device Events +// Power Event +// Reset Event +// Suspend Event +// Resume Event +// Remote Wakeup Event +// Start of Frame Event +// Error Event +// +// Endpoint Events +// Endpoint 0 Event +// Endpoint 1 Event +// Endpoint 2 Event +// Endpoint 3 Event +// Endpoint 4 Event +// Endpoint 5 Event +// Endpoint 6 Event +// Endpoint 7 Event +// Endpoint 8 Event +// Endpoint 9 Event +// Endpoint 10 Event +// Endpoint 11 Event +// Endpoint 12 Event +// Endpoint 13 Event +// Endpoint 14 Event +// Endpoint 15 Event +// +// USB Core Events +// Set Configuration Event +// Set Interface Event +// Set/Clear Feature Event +// +// +*/ + +#define USB_POWER_EVENT 0 +#define USB_RESET_EVENT 1 +#define USB_SUSPEND_EVENT 0 +#define USB_RESUME_EVENT 0 +#define USB_WAKEUP_EVENT 0 +#define USB_SOF_EVENT 0 +#define USB_ERROR_EVENT 0 +#define USB_EP_EVENT 0x0007 +#define USB_CONFIGURE_EVENT 1 +#define USB_INTERFACE_EVENT 0 +#define USB_FEATURE_EVENT 0 + + +/* +// USB Class Support +// enables USB Class specific Requests +// Human Interface Device (HID) +// Interface Number <0-255> +// +// Mass Storage +// Interface Number <0-255> +// +// Audio Device +// Control Interface Number <0-255> +// Streaming Interface 1 Number <0-255> +// Streaming Interface 2 Number <0-255> +// +// Communication Device +// Control Interface Number <0-255> +// Bulk Interface Number <0-255> +// Max Communication Device Buffer Size +// <8=> 8 Bytes <16=> 16 Bytes <32=> 32 Bytes <64=> 64 Bytes +// +// +*/ + +#define USB_CLASS 1 +#define USB_HID 0 +#define USB_HID_IF_NUM 0 +#define USB_MSC 0 +#define USB_MSC_IF_NUM 0 +#define USB_AUDIO 0 +#define USB_ADC_CIF_NUM 0 +#define USB_ADC_SIF1_NUM 1 +#define USB_ADC_SIF2_NUM 2 +#define USB_CDC 1 +#define USB_CDC_CIF_NUM 0 +#define USB_CDC_DIF_NUM 1 +#define USB_CDC_BUFSIZE 64 + +/* +// USB Vendor Support +// enables USB Vendor specific Requests +// +*/ +#define USB_VENDOR 0 + + +#endif /* __USBCFG_H__ */ diff --git a/new_cmsis/usb/usbcore.c b/new_cmsis/usb/usbcore.c new file mode 100755 index 0000000..f80c7ee --- /dev/null +++ b/new_cmsis/usb/usbcore.c @@ -0,0 +1,1112 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbcore.c + * Purpose: USB Core Module + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------- + * History: + * V1.20 Added vendor specific requests + * Changed string descriptor handling + * Reworked Endpoint0 + * V1.00 Initial Version + *----------------------------------------------------------------------------*/ +#include "lpc_types.h" + +#include "usb.h" +#include "usbcfg.h" +#include "usbhw.h" +#include "usbcore.h" +#include "usbdesc.h" +#include "usbuser.h" + +#if (USB_CLASS) + +#if (USB_AUDIO) +#include "audio.h" +#include "adcuser.h" +#endif + +#if (USB_HID) +#include "hid.h" +#include "hiduser.h" +#endif + +#if (USB_MSC) +#include "msc.h" +#include "mscuser.h" +extern MSC_CSW CSW; +#endif + +#if (USB_CDC) +#include "cdc.h" +#include "cdcuser.h" +#endif + +#endif + +#if (USB_VENDOR) +#include "vendor.h" +#endif + +#if defined ( __CC_ARM ) +#pragma diag_suppress 111,1441 +#endif + +#if defined ( __GNUC__ ) +#define __packed __attribute__((__packed__)) +#endif + +uint16_t USB_DeviceStatus; +uint8_t USB_DeviceAddress; +uint8_t USB_Configuration; +uint32_t USB_EndPointMask; +uint32_t USB_EndPointHalt; +uint32_t USB_EndPointStall; /* EP must stay stalled */ +uint8_t USB_NumInterfaces; +uint8_t USB_AltSetting[USB_IF_NUM]; + +uint8_t EP0Buf[USB_MAX_PACKET0]; + + +USB_EP_DATA EP0Data; + +USB_SETUP_PACKET SetupPacket; + + +/* + * Reset USB Core + * Parameters: None + * Return Value: None + */ + +void USB_ResetCore (void) { + + USB_DeviceStatus = USB_POWER; + USB_DeviceAddress = 0; + USB_Configuration = 0; + USB_EndPointMask = 0x00010001; + USB_EndPointHalt = 0x00000000; + USB_EndPointStall = 0x00000000; +} + + +/* + * USB Request - Setup Stage + * Parameters: None (global SetupPacket) + * Return Value: None + */ + +void USB_SetupStage (void) { + USB_ReadEP(0x00, (uint8_t *)&SetupPacket); +} + + +/* + * USB Request - Data In Stage + * Parameters: None (global EP0Data) + * Return Value: None + */ + +void USB_DataInStage (void) { + uint32_t cnt; + + if (EP0Data.Count > USB_MAX_PACKET0) { + cnt = USB_MAX_PACKET0; + } else { + cnt = EP0Data.Count; + } + cnt = USB_WriteEP(0x80, EP0Data.pData, cnt); + EP0Data.pData += cnt; + EP0Data.Count -= cnt; +} + + +/* + * USB Request - Data Out Stage + * Parameters: None (global EP0Data) + * Return Value: None + */ + +void USB_DataOutStage (void) { + uint32_t cnt; + + cnt = USB_ReadEP(0x00, EP0Data.pData); + EP0Data.pData += cnt; + EP0Data.Count -= cnt; +} + + +/* + * USB Request - Status In Stage + * Parameters: None + * Return Value: None + */ + +void USB_StatusInStage (void) { + USB_WriteEP(0x80, NULL, 0); +} + + +/* + * USB Request - Status Out Stage + * Parameters: None + * Return Value: None + */ + +void USB_StatusOutStage (void) { + USB_ReadEP(0x00, EP0Buf); +} + + +/* + * Get Status USB Request + * Parameters: None (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ + +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqGetStatus (void) { +#else +__inline uint32_t USB_ReqGetStatus (void) { +#endif + uint32_t n, m; + + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_DEVICE: + EP0Data.pData = (uint8_t *)&USB_DeviceStatus; + break; + case REQUEST_TO_INTERFACE: + if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) { + *((__packed uint16_t *)EP0Buf) = 0; + *((uint16_t *)EP0Buf) = 0; + EP0Data.pData = EP0Buf; + } else { + return (FALSE); + } + break; + case REQUEST_TO_ENDPOINT: + n = SetupPacket.wIndex.WB.L & 0x8F; + m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); + if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) { + *((__packed uint16_t *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0; + *((uint16_t *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0; + EP0Data.pData = EP0Buf; + } else { + return (FALSE); + } + break; + default: + return (FALSE); + } + return (TRUE); +} + + +/* + * Set/Clear Feature USB Request + * Parameters: sc: 0 - Clear, 1 - Set + * (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ + +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqSetClrFeature (uint32_t sc) { +#else +__inline uint32_t USB_ReqSetClrFeature (uint32_t sc) { +#endif + uint32_t n, m; + + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_DEVICE: + if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) { + if (sc) { + USB_WakeUpCfg(TRUE); + USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP; + } else { + USB_WakeUpCfg(FALSE); + USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP; + } + } else { + return (FALSE); + } + break; + case REQUEST_TO_INTERFACE: + return (FALSE); + case REQUEST_TO_ENDPOINT: + n = SetupPacket.wIndex.WB.L & 0x8F; + m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); + if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) { + if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) { + if (sc) { + USB_SetStallEP(n); + USB_EndPointHalt |= m; + } else { + if ((USB_EndPointStall & m) != 0) { + return (TRUE); + } + USB_ClrStallEP(n); +#if (USB_MSC) + if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) { + /* Compliance Test: rewrite CSW after unstall */ + if (CSW.dSignature == MSC_CSW_Signature) { + USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW)); + } + } +#endif + USB_EndPointHalt &= ~m; + } + } else { + return (FALSE); + } + } else { + return (FALSE); + } + break; + default: + return (FALSE); + } + return (TRUE); +} + + +/* + * Set Address USB Request + * Parameters: None (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ + +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqSetAddress (void) { +#else +__inline uint32_t USB_ReqSetAddress (void) { +#endif + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_DEVICE: + USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L; + break; + default: + return (FALSE); + } + return (TRUE); +} + + +/* + * Get Descriptor USB Request + * Parameters: None (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ + +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqGetDescriptor (void) { +#else +__inline uint32_t USB_ReqGetDescriptor (void) { +#endif + uint8_t *pD; + uint32_t len, n; + + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_DEVICE: + switch (SetupPacket.wValue.WB.H) { + case USB_DEVICE_DESCRIPTOR_TYPE: + EP0Data.pData = (uint8_t *)USB_DeviceDescriptor; + len = USB_DEVICE_DESC_SIZE; + break; + case USB_CONFIGURATION_DESCRIPTOR_TYPE: + pD = (uint8_t *)USB_ConfigDescriptor; + for (n = 0; n != SetupPacket.wValue.WB.L; n++) { + if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) { + pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; + } + } + if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) { + return (FALSE); + } + EP0Data.pData = pD; + len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; + break; + case USB_STRING_DESCRIPTOR_TYPE: + pD = (uint8_t *)USB_StringDescriptor; + for (n = 0; n != SetupPacket.wValue.WB.L; n++) { + if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) { + pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength; + } + } + if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) { + return (FALSE); + } + EP0Data.pData = pD; + len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength; + break; + default: + return (FALSE); + } + break; + case REQUEST_TO_INTERFACE: + switch (SetupPacket.wValue.WB.H) { +#if USB_HID + case HID_HID_DESCRIPTOR_TYPE: + if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) { + return (FALSE); /* Only Single HID Interface is supported */ + } + EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET; + len = HID_DESC_SIZE; + break; + case HID_REPORT_DESCRIPTOR_TYPE: + if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) { + return (FALSE); /* Only Single HID Interface is supported */ + } + EP0Data.pData = (uint8_t *)HID_ReportDescriptor; + len = HID_ReportDescSize; + break; + case HID_PHYSICAL_DESCRIPTOR_TYPE: + return (FALSE); /* HID Physical Descriptor is not supported */ +#endif + default: + return (FALSE); + } +// break; + default: + return (FALSE); + } + + if (EP0Data.Count > len) { + EP0Data.Count = len; + } + + return (TRUE); +} + + +/* + * Get Configuration USB Request + * Parameters: None (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ + +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqGetConfiguration (void) { +#else +__inline uint32_t USB_ReqGetConfiguration (void) { +#endif + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_DEVICE: + EP0Data.pData = &USB_Configuration; + break; + default: + return (FALSE); + } + return (TRUE); +} + + +/* + * Set Configuration USB Request + * Parameters: None (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ + +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqSetConfiguration (void) { +#else +__inline uint32_t USB_ReqSetConfiguration (void) { +#endif + USB_COMMON_DESCRIPTOR *pD; + uint32_t alt = 0; + uint32_t n, m; + uint32_t tmp; + + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_DEVICE: + + if (SetupPacket.wValue.WB.L) { + pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor; + while (pD->bLength) { + switch (pD->bDescriptorType) { + case USB_CONFIGURATION_DESCRIPTOR_TYPE: + if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) { + USB_Configuration = SetupPacket.wValue.WB.L; + USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces; + for (n = 0; n < USB_IF_NUM; n++) { + USB_AltSetting[n] = 0; + } + for (n = 1; n < 16; n++) { + if (USB_EndPointMask & (1 << n)) { + USB_DisableEP(n); + } + if (USB_EndPointMask & ((1 << 16) << n)) { + USB_DisableEP(n | 0x80); + } + } + USB_EndPointMask = 0x00010001; + USB_EndPointHalt = 0x00000000; + USB_EndPointStall= 0x00000000; + USB_Configure(TRUE); + if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) { + USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED; + } else { + USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED; + } + } else { +// (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; + tmp = (uint32_t)pD; + tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; + pD = (USB_COMMON_DESCRIPTOR *)tmp; + continue; + } + break; + case USB_INTERFACE_DESCRIPTOR_TYPE: + alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; + break; + case USB_ENDPOINT_DESCRIPTOR_TYPE: + if (alt == 0) { + n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; + m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); + USB_EndPointMask |= m; + USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD); + USB_EnableEP(n); + USB_ResetEP(n); + } + break; + } +// (uint8_t *)pD += pD->bLength; + tmp = (uint32_t)pD; + tmp += pD->bLength; + pD = (USB_COMMON_DESCRIPTOR *)tmp; + } + } + else { + USB_Configuration = 0; + for (n = 1; n < 16; n++) { + if (USB_EndPointMask & (1 << n)) { + USB_DisableEP(n); + } + if (USB_EndPointMask & ((1 << 16) << n)) { + USB_DisableEP(n | 0x80); + } + } + USB_EndPointMask = 0x00010001; + USB_EndPointHalt = 0x00000000; + USB_EndPointStall = 0x00000000; + USB_Configure(FALSE); + } + + if (USB_Configuration != SetupPacket.wValue.WB.L) { + return (FALSE); + } + break; + default: + return (FALSE); + } + return (TRUE); +} + + +/* + * Get Interface USB Request + * Parameters: None (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ + +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqGetInterface (void) { +#else +__inline uint32_t USB_ReqGetInterface (void) { +#endif + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_INTERFACE: + if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) { + EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L; + } else { + return (FALSE); + } + break; + default: + return (FALSE); + } + return (TRUE); +} + + +/* + * Set Interface USB Request + * Parameters: None (global SetupPacket) + * Return Value: TRUE - Success, FALSE - Error + */ +#if defined ( __IAR_SYSTEMS_ICC__ ) +inline uint32_t USB_ReqSetInterface (void) { +#else +__inline uint32_t USB_ReqSetInterface (void) { +#endif + USB_COMMON_DESCRIPTOR *pD; + uint32_t ifn = 0, alt = 0, old = 0, msk = 0; + uint32_t n, m; + uint32_t set; + uint32_t tmp; + + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_INTERFACE: + if (USB_Configuration == 0) return (FALSE); + set = FALSE; + pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor; + while (pD->bLength) { + switch (pD->bDescriptorType) { + case USB_CONFIGURATION_DESCRIPTOR_TYPE: + if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) { +// (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; + tmp = (uint32_t)pD; + tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; + pD = (USB_COMMON_DESCRIPTOR *)tmp; + + continue; + } + break; + case USB_INTERFACE_DESCRIPTOR_TYPE: + ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber; + alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting; + msk = 0; + if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) { + set = TRUE; + old = USB_AltSetting[ifn]; + USB_AltSetting[ifn] = (uint8_t)alt; + } + break; + case USB_ENDPOINT_DESCRIPTOR_TYPE: + if (ifn == SetupPacket.wIndex.WB.L) { + n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F; + m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n); + if (alt == SetupPacket.wValue.WB.L) { + USB_EndPointMask |= m; + USB_EndPointHalt &= ~m; + USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD); + USB_EnableEP(n); + USB_ResetEP(n); + msk |= m; + } + else if ((alt == old) && ((msk & m) == 0)) { + USB_EndPointMask &= ~m; + USB_EndPointHalt &= ~m; + USB_DisableEP(n); + } + } + break; + } +// (uint8_t *)pD += pD->bLength; + tmp = (uint32_t)pD; + tmp += pD->bLength; + pD = (USB_COMMON_DESCRIPTOR *)tmp; + } + break; + default: + return (FALSE); + } + + return (set); +} + + +/* + * USB Endpoint 0 Event Callback + * Parameters: event + * Return Value: none + */ + +void USB_EndPoint0 (uint32_t event) { + + switch (event) { + case USB_EVT_SETUP: + USB_SetupStage(); + USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir); + EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */ + switch (SetupPacket.bmRequestType.BM.Type) { + + case REQUEST_STANDARD: + switch (SetupPacket.bRequest) { + case USB_REQUEST_GET_STATUS: + if (!USB_ReqGetStatus()) { + goto stall_i; + } + USB_DataInStage(); + break; + + case USB_REQUEST_CLEAR_FEATURE: + if (!USB_ReqSetClrFeature(0)) { + goto stall_i; + } + USB_StatusInStage(); +#if USB_FEATURE_EVENT + USB_Feature_Event(); +#endif + break; + + case USB_REQUEST_SET_FEATURE: + if (!USB_ReqSetClrFeature(1)) { + goto stall_i; + } + USB_StatusInStage(); +#if USB_FEATURE_EVENT + USB_Feature_Event(); +#endif + break; + + case USB_REQUEST_SET_ADDRESS: + if (!USB_ReqSetAddress()) { + goto stall_i; + } + USB_StatusInStage(); + break; + + case USB_REQUEST_GET_DESCRIPTOR: + if (!USB_ReqGetDescriptor()) { + goto stall_i; + } + USB_DataInStage(); + break; + + case USB_REQUEST_SET_DESCRIPTOR: +/*stall_o:*/ USB_SetStallEP(0x00); /* not supported */ + EP0Data.Count = 0; + break; + + case USB_REQUEST_GET_CONFIGURATION: + if (!USB_ReqGetConfiguration()) { + goto stall_i; + } + USB_DataInStage(); + break; + + case USB_REQUEST_SET_CONFIGURATION: + if (!USB_ReqSetConfiguration()) { + goto stall_i; + } + USB_StatusInStage(); +#if USB_CONFIGURE_EVENT + USB_Configure_Event(); +#endif + break; + + case USB_REQUEST_GET_INTERFACE: + if (!USB_ReqGetInterface()) { + goto stall_i; + } + USB_DataInStage(); + break; + + case USB_REQUEST_SET_INTERFACE: + if (!USB_ReqSetInterface()) { + goto stall_i; + } + USB_StatusInStage(); +#if USB_INTERFACE_EVENT + USB_Interface_Event(); +#endif + break; + + default: + goto stall_i; + } + break; /* end case REQUEST_STANDARD */ + +#if USB_CLASS + case REQUEST_CLASS: + switch (SetupPacket.bmRequestType.BM.Recipient) { + + case REQUEST_TO_DEVICE: + goto stall_i; /* not supported */ + + case REQUEST_TO_INTERFACE: +#if USB_HID + if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */ + switch (SetupPacket.bRequest) { + case HID_REQUEST_GET_REPORT: + if (HID_GetReport()) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case HID_REQUEST_SET_REPORT: + EP0Data.pData = EP0Buf; /* data to be received */ + goto setup_class_ok; + case HID_REQUEST_GET_IDLE: + if (HID_GetIdle()) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case HID_REQUEST_SET_IDLE: + if (HID_SetIdle()) { + USB_StatusInStage(); /* send Acknowledge */ + goto setup_class_ok; + } + break; + case HID_REQUEST_GET_PROTOCOL: + if (HID_GetProtocol()) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case HID_REQUEST_SET_PROTOCOL: + if (HID_SetProtocol()) { + USB_StatusInStage(); /* send Acknowledge */ + goto setup_class_ok; + } + break; + } + } +#endif /* USB_HID */ +#if USB_MSC + if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { /* IF number correct? */ + switch (SetupPacket.bRequest) { + case MSC_REQUEST_RESET: + if ((SetupPacket.wValue.W == 0) && /* RESET with invalid parameters -> STALL */ + (SetupPacket.wLength == 0)) { + if (MSC_Reset()) { + USB_StatusInStage(); + goto setup_class_ok; + } + } + break; + case MSC_REQUEST_GET_MAX_LUN: + if ((SetupPacket.wValue.W == 0) && /* GET_MAX_LUN with invalid parameters -> STALL */ + (SetupPacket.wLength == 1)) { + if (MSC_GetMaxLUN()) { + EP0Data.pData = EP0Buf; + USB_DataInStage(); + goto setup_class_ok; + } + } + break; + } + } +#endif /* USB_MSC */ +#if USB_AUDIO + if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */ + (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) || + (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) { + switch (SetupPacket.bRequest) { + case AUDIO_REQUEST_GET_CUR: + case AUDIO_REQUEST_GET_MIN: + case AUDIO_REQUEST_GET_MAX: + case AUDIO_REQUEST_GET_RES: + if (ADC_IF_GetRequest()) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case AUDIO_REQUEST_SET_CUR: +// case AUDIO_REQUEST_SET_MIN: +// case AUDIO_REQUEST_SET_MAX: +// case AUDIO_REQUEST_SET_RES: + EP0Data.pData = EP0Buf; /* data to be received */ + goto setup_class_ok; + } + } +#endif /* USB_AUDIO */ +#if USB_CDC + if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */ + (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) { + switch (SetupPacket.bRequest) { + case CDC_SEND_ENCAPSULATED_COMMAND: + EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */ + goto setup_class_ok; + case CDC_GET_ENCAPSULATED_RESPONSE: + if (CDC_GetEncapsulatedResponse()) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case CDC_SET_COMM_FEATURE: + EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */ + goto setup_class_ok; + case CDC_GET_COMM_FEATURE: + if (CDC_GetCommFeature(SetupPacket.wValue.W)) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case CDC_CLEAR_COMM_FEATURE: + if (CDC_ClearCommFeature(SetupPacket.wValue.W)) { + USB_StatusInStage(); /* send Acknowledge */ + goto setup_class_ok; + } + break; + case CDC_SET_LINE_CODING: + EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */ + goto setup_class_ok; + case CDC_GET_LINE_CODING: + if (CDC_GetLineCoding()) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case CDC_SET_CONTROL_LINE_STATE: + if (CDC_SetControlLineState(SetupPacket.wValue.W)) { + USB_StatusInStage(); /* send Acknowledge */ + goto setup_class_ok; + } + break; + case CDC_SEND_BREAK: + if (CDC_SendBreak(SetupPacket.wValue.W)) { + USB_StatusInStage(); /* send Acknowledge */ + goto setup_class_ok; + } + break; + } + } +#endif /* USB_CDC */ + goto stall_i; /* not supported */ + /* end case REQUEST_TO_INTERFACE */ + + case REQUEST_TO_ENDPOINT: +#if USB_AUDIO + switch (SetupPacket.bRequest) { + case AUDIO_REQUEST_GET_CUR: + case AUDIO_REQUEST_GET_MIN: + case AUDIO_REQUEST_GET_MAX: + case AUDIO_REQUEST_GET_RES: + if (ADC_EP_GetRequest()) { + EP0Data.pData = EP0Buf; /* point to data to be sent */ + USB_DataInStage(); /* send requested data */ + goto setup_class_ok; + } + break; + case AUDIO_REQUEST_SET_CUR: +// case AUDIO_REQUEST_SET_MIN: +// case AUDIO_REQUEST_SET_MAX: +// case AUDIO_REQUEST_SET_RES: + EP0Data.pData = EP0Buf; /* data to be received */ + goto setup_class_ok; + } +#endif /* USB_AUDIO */ + goto stall_i; + /* end case REQUEST_TO_ENDPOINT */ + + default: + goto stall_i; + } +setup_class_ok: /* request finished successfully */ + break; /* end case REQUEST_CLASS */ +#endif /* USB_CLASS */ + +#if USB_VENDOR + case REQUEST_VENDOR: + switch (SetupPacket.bmRequestType.BM.Recipient) { + + case REQUEST_TO_DEVICE: + if (!USB_ReqVendorDev(TRUE)) { + goto stall_i; /* not supported */ + } + break; + + case REQUEST_TO_INTERFACE: + if (!USB_ReqVendorIF(TRUE)) { + goto stall_i; /* not supported */ + } + break; + + case REQUEST_TO_ENDPOINT: + if (!USB_ReqVendorEP(TRUE)) { + goto stall_i; /* not supported */ + } + break; + + default: + goto stall_i; + } + + if (SetupPacket.wLength) { + if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) { + USB_DataInStage(); + } + } else { + USB_StatusInStage(); + } + + break; /* end case REQUEST_VENDOR */ +#endif /* USB_VENDOR */ + + default: +stall_i: USB_SetStallEP(0x80); + EP0Data.Count = 0; + break; + } + break; /* end case USB_EVT_SETUP */ + + case USB_EVT_OUT: + if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) { + if (EP0Data.Count) { /* still data to receive ? */ + USB_DataOutStage(); /* receive data */ + if (EP0Data.Count == 0) { /* data complete ? */ + switch (SetupPacket.bmRequestType.BM.Type) { + + case REQUEST_STANDARD: + goto stall_i; /* not supported */ + +#if (USB_CLASS) + case REQUEST_CLASS: + switch (SetupPacket.bmRequestType.BM.Recipient) { + case REQUEST_TO_DEVICE: + goto stall_i; /* not supported */ + + case REQUEST_TO_INTERFACE: +#if USB_HID + if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */ + switch (SetupPacket.bRequest) { + case HID_REQUEST_SET_REPORT: + if (HID_SetReport()) { + USB_StatusInStage(); /* send Acknowledge */ + goto out_class_ok; + } + break; + } + } +#endif /* USB_HID */ +#if USB_AUDIO + if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */ + (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) || + (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) { + switch (SetupPacket.bRequest) { + case AUDIO_REQUEST_SET_CUR: +// case AUDIO_REQUEST_SET_MIN: +// case AUDIO_REQUEST_SET_MAX: +// case AUDIO_REQUEST_SET_RES: + if (ADC_IF_SetRequest()) { + USB_StatusInStage(); /* send Acknowledge */ + goto out_class_ok; + } + break; + } + } +#endif /* USB_AUDIO */ +#if USB_CDC + if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */ + (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) { + switch (SetupPacket.bRequest) { + case CDC_SEND_ENCAPSULATED_COMMAND: + if (CDC_SendEncapsulatedCommand()) { + USB_StatusInStage(); /* send Acknowledge */ + goto out_class_ok; + } + break; + case CDC_SET_COMM_FEATURE: + if (CDC_SetCommFeature(SetupPacket.wValue.W)) { + USB_StatusInStage(); /* send Acknowledge */ + goto out_class_ok; + } + break; + case CDC_SET_LINE_CODING: + if (CDC_SetLineCoding()) { + USB_StatusInStage(); /* send Acknowledge */ + goto out_class_ok; + } + break; + } + } +#endif /* USB_CDC */ + goto stall_i; + /* end case REQUEST_TO_INTERFACE */ + + case REQUEST_TO_ENDPOINT: +#if USB_AUDIO + switch (SetupPacket.bRequest) { + case AUDIO_REQUEST_SET_CUR: +// case AUDIO_REQUEST_SET_MIN: +// case AUDIO_REQUEST_SET_MAX: +// case AUDIO_REQUEST_SET_RES: + if (ADC_EP_SetRequest()) { + USB_StatusInStage(); /* send Acknowledge */ + goto out_class_ok; + } + break; + } +#endif /* USB_AUDIO */ + goto stall_i; + /* end case REQUEST_TO_ENDPOINT */ + + default: + goto stall_i; + } +out_class_ok: /* request finished successfully */ + break; /* end case REQUEST_CLASS */ +#endif /* USB_CLASS */ + +#if USB_VENDOR + case REQUEST_VENDOR: + switch (SetupPacket.bmRequestType.BM.Recipient) { + + case REQUEST_TO_DEVICE: + if (!USB_ReqVendorDev(FALSE)) { + goto stall_i; /* not supported */ + } + break; + + case REQUEST_TO_INTERFACE: + if (!USB_ReqVendorIF(FALSE)) { + goto stall_i; /* not supported */ + } + break; + + case REQUEST_TO_ENDPOINT: + if (!USB_ReqVendorEP(FALSE)) { + goto stall_i; /* not supported */ + } + break; + + default: + goto stall_i; + } + + USB_StatusInStage(); + + break; /* end case REQUEST_VENDOR */ +#endif /* USB_VENDOR */ + + default: + goto stall_i; + } + } + } + } else { + USB_StatusOutStage(); /* receive Acknowledge */ + } + break; /* end case USB_EVT_OUT */ + + case USB_EVT_IN : + if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) { + USB_DataInStage(); /* send data */ + } else { + if (USB_DeviceAddress & 0x80) { + USB_DeviceAddress &= 0x7F; + USB_SetAddress(USB_DeviceAddress); + } + } + break; /* end case USB_EVT_IN */ + + case USB_EVT_OUT_STALL: + USB_ClrStallEP(0x00); + break; + + case USB_EVT_IN_STALL: + USB_ClrStallEP(0x80); + break; + + } +} diff --git a/new_cmsis/usb/usbcore.h b/new_cmsis/usb/usbcore.h new file mode 100755 index 0000000..9562160 --- /dev/null +++ b/new_cmsis/usb/usbcore.h @@ -0,0 +1,52 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbcore.h + * Purpose: USB Core Definitions + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#ifndef __USBCORE_H__ +#define __USBCORE_H__ + + +/* USB Endpoint Data Structure */ +typedef struct _USB_EP_DATA { + uint8_t *pData; + uint16_t Count; +} USB_EP_DATA; + +/* USB Core Global Variables */ +extern uint16_t USB_DeviceStatus; +extern uint8_t USB_DeviceAddress; +extern uint8_t USB_Configuration; +extern uint32_t USB_EndPointMask; +extern uint32_t USB_EndPointHalt; +extern uint32_t USB_EndPointStall; +extern uint8_t USB_AltSetting[USB_IF_NUM]; + +/* USB Endpoint 0 Buffer */ +extern uint8_t EP0Buf[USB_MAX_PACKET0]; + +/* USB Endpoint 0 Data Info */ +extern USB_EP_DATA EP0Data; + +/* USB Setup Packet */ +extern USB_SETUP_PACKET SetupPacket; + +/* USB Core Functions */ +extern void USB_ResetCore (void); + + + +#endif /* __USBCORE_H__ */ diff --git a/new_cmsis/usb/usbdesc.c b/new_cmsis/usb/usbdesc.c new file mode 100755 index 0000000..a2a75b0 --- /dev/null +++ b/new_cmsis/usb/usbdesc.c @@ -0,0 +1,201 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbdesc.c + * Purpose: USB Descriptors + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------- + * History: + * V1.20 Changed string descriptor handling + * V1.00 Initial Version + *---------------------------------------------------------------------------*/ +#include "lpc_types.h" +#include "usb.h" +#include "cdc.h" +#include "usbcfg.h" +#include "usbdesc.h" + + +/* USB Standard Device Descriptor */ +const uint8_t USB_DeviceDescriptor[] = { + USB_DEVICE_DESC_SIZE, /* bLength */ + USB_DEVICE_DESCRIPTOR_TYPE, /* bDescriptorType */ + WBVAL(0x0200), /* 2.0 */ /* bcdUSB */ + USB_DEVICE_CLASS_COMMUNICATIONS, /* bDeviceClass CDC*/ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + USB_MAX_PACKET0, /* bMaxPacketSize0 */ + WBVAL(0x1FC9), /* idVendor */ + WBVAL(0x2002), /* idProduct */ + WBVAL(0x0100), /* 1.00 */ /* bcdDevice */ + 0x01, /* iManufacturer */ + 0x02, /* iProduct */ + 0x03, /* iSerialNumber */ + 0x01 /* bNumConfigurations: one possible configuration*/ +}; + +/* USB Configuration Descriptor */ +/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ +const uint8_t USB_ConfigDescriptor[] = { +/* Configuration 1 */ + USB_CONFIGUARTION_DESC_SIZE, /* bLength */ + USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType */ + WBVAL( /* wTotalLength */ + 1*USB_CONFIGUARTION_DESC_SIZE + + 1*USB_INTERFACE_DESC_SIZE + /* communication interface */ + 0x0013 + /* CDC functions */ + 1*USB_ENDPOINT_DESC_SIZE + /* interrupt endpoint */ + 1*USB_INTERFACE_DESC_SIZE + /* data interface */ + 2*USB_ENDPOINT_DESC_SIZE /* bulk endpoints */ + ), + 0x02, /* bNumInterfaces */ + 0x01, /* bConfigurationValue: 0x01 is used to select this configuration */ + 0x00, /* iConfiguration: no string to describe this configuration */ + USB_CONFIG_BUS_POWERED /*|*/ /* bmAttributes */ +/*USB_CONFIG_REMOTE_WAKEUP*/, + USB_CONFIG_POWER_MA(100), /* bMaxPower, device power consumption is 100 mA */ +/* Interface 0, Alternate Setting 0, Communication class interface descriptor */ + USB_INTERFACE_DESC_SIZE, /* bLength */ + USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */ + USB_CDC_CIF_NUM, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoint used */ + CDC_COMMUNICATION_INTERFACE_CLASS, /* bInterfaceClass: Communication Interface Class */ + CDC_ABSTRACT_CONTROL_MODEL, /* bInterfaceSubClass: Abstract Control Model */ + 0x00, /* bInterfaceProtocol: no protocol used */ + 0x5E, /* iInterface: */ +/*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + CDC_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ + CDC_HEADER, /* bDescriptorSubtype: Header Func Desc */ + WBVAL(CDC_V1_10), /* 1.10 */ /* bcdCDC */ +/*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + CDC_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ + CDC_CALL_MANAGEMENT, /* bDescriptorSubtype: Call Management Func Desc */ + 0x01, /* bmCapabilities: device handles call management */ + 0x01, /* bDataInterface: CDC data IF ID */ +/*Abstract Control Management Functional Descriptor*/ + 0x04, /* bFunctionLength */ + CDC_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ + CDC_ABSTRACT_CONTROL_MANAGEMENT, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities: SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported */ +/*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + CDC_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ + CDC_UNION, /* bDescriptorSubtype: Union func desc */ + USB_CDC_CIF_NUM, /* bMasterInterface: Communication class interface is master */ + USB_CDC_DIF_NUM, /* bSlaveInterface0: Data class interface is slave 0 */ +/*Endpoint 1 Descriptor*/ /* event notification (optional) */ + USB_ENDPOINT_DESC_SIZE, /* bLength */ + USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */ + USB_ENDPOINT_IN(1), /* bEndpointAddress */ + USB_ENDPOINT_TYPE_INTERRUPT, /* bmAttributes */ + WBVAL(0x0010), /* wMaxPacketSize */ + 0x02, /* 2ms */ /* bInterval */ +/* Interface 1, Alternate Setting 0, Data class interface descriptor*/ + USB_INTERFACE_DESC_SIZE, /* bLength */ + USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */ + USB_CDC_DIF_NUM, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: no alternate setting */ + 0x02, /* bNumEndpoints: two endpoints used */ + CDC_DATA_INTERFACE_CLASS, /* bInterfaceClass: Data Interface Class */ + 0x00, /* bInterfaceSubClass: no subclass available */ + 0x00, /* bInterfaceProtocol: no protocol used */ + 0x5E, /* iInterface: */ +/* Endpoint, EP2 Bulk Out */ + USB_ENDPOINT_DESC_SIZE, /* bLength */ + USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */ + USB_ENDPOINT_OUT(2), /* bEndpointAddress */ + USB_ENDPOINT_TYPE_BULK, /* bmAttributes */ + WBVAL(USB_CDC_BUFSIZE), /* wMaxPacketSize */ + 0x00, /* bInterval: ignore for Bulk transfer */ +/* Endpoint, EP2 Bulk In */ + USB_ENDPOINT_DESC_SIZE, /* bLength */ + USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */ + USB_ENDPOINT_IN(2), /* bEndpointAddress */ + USB_ENDPOINT_TYPE_BULK, /* bmAttributes */ + WBVAL(USB_CDC_BUFSIZE), /* wMaxPacketSize */ + 0x00, /* bInterval: ignore for Bulk transfer */ +/* Terminator */ + 0 /* bLength */ +}; + + + + +/* USB String Descriptor (optional) */ +const uint8_t USB_StringDescriptor[] = { +/* Index 0x00: LANGID Codes */ + 0x04, /* bLength */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + WBVAL(0x0409), /* US English */ /* wLANGID */ +/* Index 0x01: Manufacturer */ + (13*2 + 2), /* bLength (13 Char + Type + lenght) */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + 'N',0, + 'X',0, + 'P',0, + ' ',0, + 'S',0, + 'E',0, + 'M',0, + 'I',0, + 'C',0, + 'O',0, + 'N',0, + 'D',0, + ' ',0, +/* Index 0x02: Product */ + (17*2 + 2), /* bLength ( 17 Char + Type + lenght) */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + 'N',0, + 'X',0, + 'P',0, + ' ',0, + 'L',0, + 'P',0, + 'C',0, + '1',0, + '7',0, + 'x',0, + 'x',0, + ' ',0, + 'V',0, + 'C',0, + 'O',0, + 'M',0, + ' ',0, +/* Index 0x03: Serial Number */ + (12*2 + 2), /* bLength (12 Char + Type + lenght) */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + 'D',0, + 'E',0, + 'M',0, + 'O',0, + '0',0, + '0',0, + '0',0, + '0',0, + '0',0, + '0',0, + '0',0, + '0',0, +/* Index 0x04: Interface 0, Alternate Setting 0 */ + ( 4*2 + 2), /* bLength (4 Char + Type + lenght) */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + 'V',0, + 'C',0, + 'O',0, + 'M',0, +}; diff --git a/new_cmsis/usb/usbdesc.h b/new_cmsis/usb/usbdesc.h new file mode 100755 index 0000000..851b80e --- /dev/null +++ b/new_cmsis/usb/usbdesc.h @@ -0,0 +1,35 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbdesc.h + * Purpose: USB Descriptors Definitions + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#ifndef __USBDESC_H__ +#define __USBDESC_H__ + + +#define WBVAL(x) (x & 0xFF),((x >> 8) & 0xFF) + +#define USB_DEVICE_DESC_SIZE (sizeof(USB_DEVICE_DESCRIPTOR)) +#define USB_CONFIGUARTION_DESC_SIZE (sizeof(USB_CONFIGURATION_DESCRIPTOR)) +#define USB_INTERFACE_DESC_SIZE (sizeof(USB_INTERFACE_DESCRIPTOR)) +#define USB_ENDPOINT_DESC_SIZE (sizeof(USB_ENDPOINT_DESCRIPTOR)) + +extern const uint8_t USB_DeviceDescriptor[]; +extern const uint8_t USB_ConfigDescriptor[]; +extern const uint8_t USB_StringDescriptor[]; + + +#endif /* __USBDESC_H__ */ diff --git a/new_cmsis/usb/usbhw.c b/new_cmsis/usb/usbhw.c new file mode 100755 index 0000000..e58b268 --- /dev/null +++ b/new_cmsis/usb/usbhw.c @@ -0,0 +1,811 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbhw.c + * Purpose: USB Hardware Layer Module for NXP's LPC17xx MCU + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------- + * History: + * V1.20 Added USB_ClearEPBuf + * V1.00 Initial Version + *----------------------------------------------------------------------------*/ +#include "../LPC17xx.h" /* LPC17xx definitions */ +#include "usb.h" +#include "usbcfg.h" +#include "usbreg.h" +#include "usbhw.h" +#include "usbcore.h" +#include "usbuser.h" + +#if defined ( __CC_ARM__ ) +#pragma diag_suppress 1441 +#endif + + +#define EP_MSK_CTRL 0x0001 /* Control Endpoint Logical Address Mask */ +#define EP_MSK_BULK 0xC924 /* Bulk Endpoint Logical Address Mask */ +#define EP_MSK_INT 0x4492 /* Interrupt Endpoint Logical Address Mask */ +#define EP_MSK_ISO 0x1248 /* Isochronous Endpoint Logical Address Mask */ + + +#if USB_DMA + +#pragma arm section zidata = "USB_RAM" +uint32_t UDCA[USB_EP_NUM]; /* UDCA in USB RAM */ +uint32_t DD_NISO_Mem[4*DD_NISO_CNT]; /* Non-Iso DMA Descriptor Memory */ +uint32_t DD_ISO_Mem [5*DD_ISO_CNT]; /* Iso DMA Descriptor Memory */ +#pragma arm section zidata +uint32_t udca[USB_EP_NUM]; /* UDCA saved values */ + +uint32_t DDMemMap[2]; /* DMA Descriptor Memory Usage */ + +#endif + + +/* + * Get Endpoint Physical Address + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: Endpoint Physical Address + */ + +uint32_t EPAdr (uint32_t EPNum) { + uint32_t val; + + val = (EPNum & 0x0F) << 1; + if (EPNum & 0x80) { + val += 1; + } + return (val); +} + + +/* + * Write Command + * Parameters: cmd: Command + * Return Value: None + */ + +void WrCmd (uint32_t cmd) { + + USB->USBDevIntClr = CCEMTY_INT; + USB->USBCmdCode = cmd; + while ((USB->USBDevIntSt & CCEMTY_INT) == 0); +} + + +/* + * Write Command Data + * Parameters: cmd: Command + * val: Data + * Return Value: None + */ + +void WrCmdDat (uint32_t cmd, uint32_t val) { + + USB->USBDevIntClr = CCEMTY_INT; + USB->USBCmdCode = cmd; + while ((USB->USBDevIntSt & CCEMTY_INT) == 0); + USB->USBDevIntClr = CCEMTY_INT; + USB->USBCmdCode = val; + while ((USB->USBDevIntSt & CCEMTY_INT) == 0); +} + + +/* + * Write Command to Endpoint + * Parameters: cmd: Command + * val: Data + * Return Value: None + */ + +void WrCmdEP (uint32_t EPNum, uint32_t cmd){ + + USB->USBDevIntClr = CCEMTY_INT; + USB->USBCmdCode = CMD_SEL_EP(EPAdr(EPNum)); + while ((USB->USBDevIntSt & CCEMTY_INT) == 0); + USB->USBDevIntClr = CCEMTY_INT; + USB->USBCmdCode = cmd; + while ((USB->USBDevIntSt & CCEMTY_INT) == 0); +} + + +/* + * Read Command Data + * Parameters: cmd: Command + * Return Value: Data Value + */ + +uint32_t RdCmdDat (uint32_t cmd) { + + USB->USBDevIntClr = CCEMTY_INT | CDFULL_INT; + USB->USBCmdCode = cmd; + while ((USB->USBDevIntSt & CDFULL_INT) == 0); + return (USB->USBCmdData); +} + + +/* + * USB Initialize Function + * Called by the User to initialize USB + * Return Value: None + */ + +void USB_Init (void) { + + PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); /* P0.29 D+, P0.30 D- */ + PINCON->PINSEL1 |= ((1<<26)|(1<<28)); /* PINSEL1 26.27, 28.29 = 01 */ + + PINCON->PINSEL3 &= ~((3<< 4)|(3<<28)); /* P1.18 GoodLink, P1.30 VBUS */ + PINCON->PINSEL3 |= ((1<< 4)|(2<<28)); /* PINSEL3 4.5 = 01, 28.29 = 10 */ + + PINCON->PINSEL4 &= ~((3<<18) ); /* P2.9 SoftConnect */ + PINCON->PINSEL4 |= ((1<<18) ); /* PINSEL4 18.19 = 01 */ + + SC->PCONP |= (1UL<<31); /* USB PCLK -> enable USB Per. */ + + USB->USBClkCtrl = 0x1A; /* Dev, PortSel, AHB clock enable */ + while ((USB->USBClkSt & 0x1A) != 0x1A); + + NVIC_EnableIRQ(USB_IRQn); /* enable USB interrupt */ + + USB_Reset(); + USB_SetAddress(0); +} + + +/* + * USB Connect Function + * Called by the User to Connect/Disconnect USB + * Parameters: con: Connect/Disconnect + * Return Value: None + */ + +void USB_Connect (uint32_t con) { + WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(con ? DEV_CON : 0)); +} + + +/* + * USB Reset Function + * Called automatically on USB Reset + * Return Value: None + */ + +void USB_Reset (void) { +#if USB_DMA + uint32_t n; +#endif + + USB->USBEpInd = 0; + USB->USBMaxPSize = USB_MAX_PACKET0; + USB->USBEpInd = 1; + USB->USBMaxPSize = USB_MAX_PACKET0; + while ((USB->USBDevIntSt & EP_RLZED_INT) == 0); + + USB->USBEpIntClr = 0xFFFFFFFF; + USB->USBEpIntEn = 0xFFFFFFFF ^ USB_DMA_EP; + USB->USBDevIntClr = 0xFFFFFFFF; + USB->USBDevIntEn = DEV_STAT_INT | EP_SLOW_INT | + (USB_SOF_EVENT ? FRAME_INT : 0) | + (USB_ERROR_EVENT ? ERR_INT : 0); + +#if USB_DMA + USB->USBUDCAH = USB_RAM_ADR; + USB->USBDMARClr = 0xFFFFFFFF; + USB->USBEpDMADis = 0xFFFFFFFF; + USB->USBEpDMAEn = USB_DMA_EP; + USB->USBEoTIntClr = 0xFFFFFFFF; + USB->USBNDDRIntClr = 0xFFFFFFFF; + USB->USBSysErrIntClr = 0xFFFFFFFF; + USB->USBDMAIntEn = 0x00000007; + DDMemMap[0] = 0x00000000; + DDMemMap[1] = 0x00000000; + for (n = 0; n < USB_EP_NUM; n++) { + udca[n] = 0; + UDCA[n] = 0; + } +#endif +} + + +/* + * USB Suspend Function + * Called automatically on USB Suspend + * Return Value: None + */ + +void USB_Suspend (void) { + /* Performed by Hardware */ +} + + +/* + * USB Resume Function + * Called automatically on USB Resume + * Return Value: None + */ + +void USB_Resume (void) { + /* Performed by Hardware */ +} + + +/* + * USB Remote Wakeup Function + * Called automatically on USB Remote Wakeup + * Return Value: None + */ + +void USB_WakeUp (void) { + + if (USB_DeviceStatus & USB_GETSTATUS_REMOTE_WAKEUP) { + WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON)); + } +} + + +/* + * USB Remote Wakeup Configuration Function + * Parameters: cfg: Enable/Disable + * Return Value: None + */ + +void USB_WakeUpCfg (uint32_t cfg) { + /* Not needed */ +} + + +/* + * USB Set Address Function + * Parameters: adr: USB Address + * Return Value: None + */ + +void USB_SetAddress (uint32_t adr) { + WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Don't wait for next */ + WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Setup Status Phase */ +} + + +/* + * USB Configure Function + * Parameters: cfg: Configure/Deconfigure + * Return Value: None + */ + +void USB_Configure (uint32_t cfg) { + + WrCmdDat(CMD_CFG_DEV, DAT_WR_BYTE(cfg ? CONF_DVICE : 0)); + + USB->USBReEp = 0x00000003; + while ((USB->USBDevIntSt & EP_RLZED_INT) == 0); + USB->USBDevIntClr = EP_RLZED_INT; +} + + +/* + * Configure USB Endpoint according to Descriptor + * Parameters: pEPD: Pointer to Endpoint Descriptor + * Return Value: None + */ + +void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) { + uint32_t num; + + num = EPAdr(pEPD->bEndpointAddress); + USB->USBReEp |= (1 << num); + USB->USBEpInd = num; + USB->USBMaxPSize = pEPD->wMaxPacketSize; + while ((USB->USBDevIntSt & EP_RLZED_INT) == 0); + USB->USBDevIntClr = EP_RLZED_INT; +} + + +/* + * Set Direction for USB Control Endpoint + * Parameters: dir: Out (dir == 0), In (dir <> 0) + * Return Value: None + */ + +void USB_DirCtrlEP (uint32_t dir) { + /* Not needed */ +} + + +/* + * Enable USB Endpoint + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_EnableEP (uint32_t EPNum) { + WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0)); +} + + +/* + * Disable USB Endpoint + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_DisableEP (uint32_t EPNum) { + WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_DA)); +} + + +/* + * Reset USB Endpoint + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_ResetEP (uint32_t EPNum) { + WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0)); +} + + +/* + * Set Stall for USB Endpoint + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_SetStallEP (uint32_t EPNum) { + WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_ST)); +} + + +/* + * Clear Stall for USB Endpoint + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_ClrStallEP (uint32_t EPNum) { + WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0)); +} + + +/* + * Clear USB Endpoint Buffer + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_ClearEPBuf (uint32_t EPNum) { + WrCmdEP(EPNum, CMD_CLR_BUF); +} + + +/* + * Read USB Endpoint Data + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * pData: Pointer to Data Buffer + * Return Value: Number of bytes read + */ + +uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData) { + uint32_t cnt, n; + + USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN; + + do { + cnt = USB->USBRxPLen; + } while ((cnt & PKT_RDY) == 0); + cnt &= PKT_LNGTH_MASK; + + for (n = 0; n < (cnt + 3) / 4; n++) { + *((__packed uint32_t *)pData) = USB->USBRxData; + pData += 4; + } + USB->USBCtrl = 0; + + if (((EP_MSK_ISO >> EPNum) & 1) == 0) { /* Non-Isochronous Endpoint */ + WrCmdEP(EPNum, CMD_CLR_BUF); + } + + return (cnt); +} + + +/* + * Write USB Endpoint Data + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * pData: Pointer to Data Buffer + * cnt: Number of bytes to write + * Return Value: Number of bytes written + */ + +uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt) { + uint32_t n; + + USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN; + + USB->USBTxPLen = cnt; + + for (n = 0; n < (cnt + 3) / 4; n++) { + USB->USBTxData = *((__packed uint32_t *)pData); + pData += 4; + } + USB->USBCtrl = 0; + WrCmdEP(EPNum, CMD_VALID_BUF); + return (cnt); +} + +#if USB_DMA + +/* DMA Descriptor Memory Layout */ +const uint32_t DDAdr[2] = { DD_NISO_ADR, DD_ISO_ADR }; +const uint32_t DDSz [2] = { 16, 20 }; + + +/* + * Setup USB DMA Transfer for selected Endpoint + * Parameters: EPNum: Endpoint Number + * pDD: Pointer to DMA Descriptor + * Return Value: TRUE - Success, FALSE - Error + */ + +uint32_t USB_DMA_Setup(uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD) { + uint32_t num, ptr, nxt, iso, n; + + iso = pDD->Cfg.Type.IsoEP; /* Iso or Non-Iso Descriptor */ + num = EPAdr(EPNum); /* Endpoint's Physical Address */ + + ptr = 0; /* Current Descriptor */ + nxt = udca[num]; /* Initial Descriptor */ + while (nxt) { /* Go through Descriptor List */ + ptr = nxt; /* Current Descriptor */ + if (!pDD->Cfg.Type.Link) { /* Check for Linked Descriptors */ + n = (ptr - DDAdr[iso]) / DDSz[iso]; /* Descriptor Index */ + DDMemMap[iso] &= ~(1 << n); /* Unmark Memory Usage */ + } + nxt = *((uint32_t *)ptr); /* Next Descriptor */ + } + + for (n = 0; n < 32; n++) { /* Search for available Memory */ + if ((DDMemMap[iso] & (1 << n)) == 0) { + break; /* Memory found */ + } + } + if (n == 32) return (FALSE); /* Memory not available */ + + DDMemMap[iso] |= 1 << n; /* Mark Memory Usage */ + nxt = DDAdr[iso] + n * DDSz[iso]; /* Next Descriptor */ + + if (ptr && pDD->Cfg.Type.Link) { + *((uint32_t *)(ptr + 0)) = nxt; /* Link in new Descriptor */ + *((uint32_t *)(ptr + 4)) |= 0x00000004; /* Next DD is Valid */ + } else { + udca[num] = nxt; /* Save new Descriptor */ + UDCA[num] = nxt; /* Update UDCA in USB */ + } + + /* Fill in DMA Descriptor */ + *(((uint32_t *)nxt)++) = 0; /* Next DD Pointer */ + *(((uint32_t *)nxt)++) = pDD->Cfg.Type.ATLE | + (pDD->Cfg.Type.IsoEP << 4) | + (pDD->MaxSize << 5) | + (pDD->BufLen << 16); + *(((uint32_t *)nxt)++) = pDD->BufAdr; + *(((uint32_t *)nxt)++) = pDD->Cfg.Type.LenPos << 8; + if (iso) { + *((uint32_t *)nxt) = pDD->InfoAdr; + } + + return (TRUE); /* Success */ +} + + +/* + * Enable USB DMA Endpoint + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_DMA_Enable (uint32_t EPNum) { + USB->USBEpDMAEn = 1 << EPAdr(EPNum); +} + + +/* + * Disable USB DMA Endpoint + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: None + */ + +void USB_DMA_Disable (uint32_t EPNum) { + USB->USBEpDMADis = 1 << EPAdr(EPNum); +} + + +/* + * Get USB DMA Endpoint Status + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: DMA Status + */ + +uint32_t USB_DMA_Status (uint32_t EPNum) { + uint32_t ptr, val; + + ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */ + if (ptr == 0) + return (USB_DMA_INVALID); + + val = *((uint32_t *)(ptr + 3*4)); /* Status Information */ + switch ((val >> 1) & 0x0F) { + case 0x00: /* Not serviced */ + return (USB_DMA_IDLE); + case 0x01: /* Being serviced */ + return (USB_DMA_BUSY); + case 0x02: /* Normal Completition */ + return (USB_DMA_DONE); + case 0x03: /* Data Under Run */ + return (USB_DMA_UNDER_RUN); + case 0x08: /* Data Over Run */ + return (USB_DMA_OVER_RUN); + case 0x09: /* System Error */ + return (USB_DMA_ERROR); + } + + return (USB_DMA_UNKNOWN); +} + + +/* + * Get USB DMA Endpoint Current Buffer Address + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: DMA Address (or -1 when DMA is Invalid) + */ + +uint32_t USB_DMA_BufAdr (uint32_t EPNum) { + uint32_t ptr, val; + + ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */ + if (ptr == 0) + { + return ((uint32_t)(-1)); /* DMA Invalid */ + } + + val = *((uint32_t *)(ptr + 2*4)); /* Buffer Address */ + return (val); /* Current Address */ +} + + +/* + * Get USB DMA Endpoint Current Buffer Count + * Number of transfered Bytes or Iso Packets + * Parameters: EPNum: Endpoint Number + * EPNum.0..3: Address + * EPNum.7: Dir + * Return Value: DMA Count (or -1 when DMA is Invalid) + */ + +uint32_t USB_DMA_BufCnt (uint32_t EPNum) { + uint32_t ptr, val; + + ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */ + if (ptr == 0) + { + return ((uint32_t)(-1)); /* DMA Invalid */ + } + val = *((uint32_t *)(ptr + 3*4)); /* Status Information */ + return (val >> 16); /* Current Count */ +} + + +#endif /* USB_DMA */ + + +/* + * Get USB Last Frame Number + * Parameters: None + * Return Value: Frame Number + */ + +uint32_t USB_GetFrame (void) { + uint32_t val; + + WrCmd(CMD_RD_FRAME); + val = RdCmdDat(DAT_RD_FRAME); + val = val | (RdCmdDat(DAT_RD_FRAME) << 8); + + return (val); +} + + +/* + * USB Interrupt Service Routine + */ + +void USB_IRQHandler (void) { + uint32_t disr, val, n, m; + uint32_t episr, episrCur; + + disr = USB->USBDevIntSt; /* Device Interrupt Status */ + + /* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */ + if (disr & DEV_STAT_INT) { + USB->USBDevIntClr = DEV_STAT_INT; + WrCmd(CMD_GET_DEV_STAT); + val = RdCmdDat(DAT_GET_DEV_STAT); /* Device Status */ + if (val & DEV_RST) { /* Reset */ + USB_Reset(); +#if USB_RESET_EVENT + USB_Reset_Event(); +#endif + } + if (val & DEV_CON_CH) { /* Connect change */ +#if USB_POWER_EVENT + USB_Power_Event(val & DEV_CON); +#endif + } + if (val & DEV_SUS_CH) { /* Suspend/Resume */ + if (val & DEV_SUS) { /* Suspend */ + USB_Suspend(); +#if USB_SUSPEND_EVENT + USB_Suspend_Event(); +#endif + } else { /* Resume */ + USB_Resume(); +#if USB_RESUME_EVENT + USB_Resume_Event(); +#endif + } + } + goto isr_end; + } + +#if USB_SOF_EVENT + /* Start of Frame Interrupt */ + if (disr & FRAME_INT) { + USB_SOF_Event(); + } +#endif + +#if USB_ERROR_EVENT + /* Error Interrupt */ + if (disr & ERR_INT) { + WrCmd(CMD_RD_ERR_STAT); + val = RdCmdDat(DAT_RD_ERR_STAT); + USB_Error_Event(val); + } +#endif + + /* Endpoint's Slow Interrupt */ + if (disr & EP_SLOW_INT) { + episrCur = 0; + episr = USB->USBEpIntSt; + for (n = 0; n < USB_EP_NUM; n++) { /* Check All Endpoints */ + if (episr == episrCur) break; /* break if all EP interrupts handled */ + if (episr & (1 << n)) { + episrCur |= (1 << n); + m = n >> 1; + + USB->USBEpIntClr = (1 << n); + while ((USB->USBDevIntSt & CDFULL_INT) == 0); + val = USB->USBCmdData; + + if ((n & 1) == 0) { /* OUT Endpoint */ + if (n == 0) { /* Control OUT Endpoint */ + if (val & EP_SEL_STP) { /* Setup Packet */ + if (USB_P_EP[0]) { + USB_P_EP[0](USB_EVT_SETUP); + continue; + } + } + } + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_OUT); + } + } else { /* IN Endpoint */ + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_IN); + } + } + } + } + USB->USBDevIntClr = EP_SLOW_INT; + } + +#if USB_DMA + + if (USB->USBDMAIntSt & 0x00000001) { /* End of Transfer Interrupt */ + val = USB->USBEoTIntSt; + for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */ + if (val & (1 << n)) { + m = n >> 1; + if ((n & 1) == 0) { /* OUT Endpoint */ + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_OUT_DMA_EOT); + } + } else { /* IN Endpoint */ + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_IN_DMA_EOT); + } + } + } + } + USB->USBEoTIntClr = val; + } + + if (USB->USBDMAIntSt & 0x00000002) { /* New DD Request Interrupt */ + val = USB->USBNDDRIntSt; + for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */ + if (val & (1 << n)) { + m = n >> 1; + if ((n & 1) == 0) { /* OUT Endpoint */ + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_OUT_DMA_NDR); + } + } else { /* IN Endpoint */ + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_IN_DMA_NDR); + } + } + } + } + USB->USBNDDRIntClr = val; + } + + if (USB->USBDMAIntSt & 0x00000004) { /* System Error Interrupt */ + val = USB->USBSysErrIntSt; + for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */ + if (val & (1 << n)) { + m = n >> 1; + if ((n & 1) == 0) { /* OUT Endpoint */ + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_OUT_DMA_ERR); + } + } else { /* IN Endpoint */ + if (USB_P_EP[m]) { + USB_P_EP[m](USB_EVT_IN_DMA_ERR); + } + } + } + } + USB->USBSysErrIntClr = val; + } + +#endif /* USB_DMA */ + +isr_end: + return; +} diff --git a/new_cmsis/usb/usbhw.h b/new_cmsis/usb/usbhw.h new file mode 100755 index 0000000..e3bec8d --- /dev/null +++ b/new_cmsis/usb/usbhw.h @@ -0,0 +1,112 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbhw.h + * Purpose: USB Hardware Layer Definitions + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------- + * History: + * V1.20 Added USB_ClearEPBuf + * V1.00 Initial Version + *----------------------------------------------------------------------------*/ + +#ifndef __USBHW_H__ +#define __USBHW_H__ +#include "lpc_types.h" + +/* USB RAM Definitions */ +#define USB_RAM_ADR 0x20080000 /* USB RAM Start Address */ +#define USB_RAM_SZ 0x00004000 /* USB RAM Size (4kB) */ + +/* DMA Endpoint Descriptors */ +#define DD_NISO_CNT 16 /* Non-Iso EP DMA Descr. Count (max. 32) */ +#define DD_ISO_CNT 8 /* Iso EP DMA Descriptor Count (max. 32) */ +#define DD_NISO_SZ (DD_NISO_CNT * 16) /* Non-Iso DMA Descr. Size */ +#define DD_ISO_SZ (DD_ISO_CNT * 20) /* Iso DMA Descriptor Size */ +#define DD_NISO_ADR (USB_RAM_ADR + 128) /* Non-Iso DMA Descr. Address */ +#define DD_ISO_ADR (DD_NISO_ADR + DD_NISO_SZ) /* Iso DMA Descr. Address */ +#define DD_SZ (128 + DD_NISO_SZ + DD_ISO_SZ) /* Descr. Size */ + +/* DMA Buffer Memory Definitions */ +#define DMA_BUF_ADR (USB_RAM_ADR + DD_SZ) /* DMA Buffer Start Address */ +#define DMA_BUF_SZ (USB_RAM_SZ - DD_SZ) /* DMA Buffer Size */ + +/* USB Error Codes */ +#define USB_ERR_PID 0x0001 /* PID Error */ +#define USB_ERR_UEPKT 0x0002 /* Unexpected Packet */ +#define USB_ERR_DCRC 0x0004 /* Data CRC Error */ +#define USB_ERR_TIMOUT 0x0008 /* Bus Time-out Error */ +#define USB_ERR_EOP 0x0010 /* End of Packet Error */ +#define USB_ERR_B_OVRN 0x0020 /* Buffer Overrun */ +#define USB_ERR_BTSTF 0x0040 /* Bit Stuff Error */ +#define USB_ERR_TGL 0x0080 /* Toggle Bit Error */ + +/* USB DMA Status Codes */ +#define USB_DMA_INVALID 0x0000 /* DMA Invalid - Not Configured */ +#define USB_DMA_IDLE 0x0001 /* DMA Idle - Waiting for Trigger */ +#define USB_DMA_BUSY 0x0002 /* DMA Busy - Transfer in progress */ +#define USB_DMA_DONE 0x0003 /* DMA Transfer Done (no Errors)*/ +#define USB_DMA_OVER_RUN 0x0004 /* Data Over Run */ +#define USB_DMA_UNDER_RUN 0x0005 /* Data Under Run (Short Packet) */ +#define USB_DMA_ERROR 0x0006 /* Error */ +#define USB_DMA_UNKNOWN 0xFFFF /* Unknown State */ + +/* USB DMA Descriptor */ +typedef struct _USB_DMA_DESCRIPTOR { + uint32_t BufAdr; /* DMA Buffer Address */ + uint16_t BufLen; /* DMA Buffer Length */ + uint16_t MaxSize; /* Maximum Packet Size */ + uint32_t InfoAdr; /* Packet Info Memory Address */ + union { /* DMA Configuration */ + struct { + uint32_t Link : 1; /* Link to existing Descriptors */ + uint32_t IsoEP : 1; /* Isonchronous Endpoint */ + uint32_t ATLE : 1; /* ATLE (Auto Transfer Length Extract) */ + uint32_t Rsrvd : 5; /* Reserved */ + uint32_t LenPos : 8; /* Length Position (ATLE) */ + } Type; + uint32_t Val; + } Cfg; +} USB_DMA_DESCRIPTOR; + +/* USB Hardware Functions */ +extern void USB_Init (void); +extern void USB_Connect (uint32_t con); +extern void USB_Reset (void); +extern void USB_Suspend (void); +extern void USB_Resume (void); +extern void USB_WakeUp (void); +extern void USB_WakeUpCfg (uint32_t cfg); +extern void USB_SetAddress (uint32_t adr); +extern void USB_Configure (uint32_t cfg); +extern void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD); +extern void USB_DirCtrlEP (uint32_t dir); +extern void USB_EnableEP (uint32_t EPNum); +extern void USB_DisableEP (uint32_t EPNum); +extern void USB_ResetEP (uint32_t EPNum); +extern void USB_SetStallEP (uint32_t EPNum); +extern void USB_ClrStallEP (uint32_t EPNum); +extern void USB_ClearEPBuf (uint32_t EPNum); +extern uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData); +extern uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt); +extern uint32_t USB_DMA_Setup (uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD); +extern void USB_DMA_Enable (uint32_t EPNum); +extern void USB_DMA_Disable(uint32_t EPNum); +extern uint32_t USB_DMA_Status (uint32_t EPNum); +extern uint32_t USB_DMA_BufAdr (uint32_t EPNum); +extern uint32_t USB_DMA_BufCnt (uint32_t EPNum); +extern uint32_t USB_GetFrame (void); +extern void USB_IRQHandler (void); + + +#endif /* __USBHW_H__ */ diff --git a/new_cmsis/usb/usbreg.h b/new_cmsis/usb/usbreg.h new file mode 100755 index 0000000..fe1d58d --- /dev/null +++ b/new_cmsis/usb/usbreg.h @@ -0,0 +1,130 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbreg.h + * Purpose: USB Hardware Layer Definitions for NXP LPC Family MCUs + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#ifndef __USBREG_H +#define __USBREG_H + +/* Device Interrupt Bit Definitions */ +#define FRAME_INT 0x00000001 +#define EP_FAST_INT 0x00000002 +#define EP_SLOW_INT 0x00000004 +#define DEV_STAT_INT 0x00000008 +#define CCEMTY_INT 0x00000010 +#define CDFULL_INT 0x00000020 +#define RxENDPKT_INT 0x00000040 +#define TxENDPKT_INT 0x00000080 +#define EP_RLZED_INT 0x00000100 +#define ERR_INT 0x00000200 + +/* Rx & Tx Packet Length Definitions */ +#define PKT_LNGTH_MASK 0x000003FF +#define PKT_DV 0x00000400 +#define PKT_RDY 0x00000800 + +/* USB Control Definitions */ +#define CTRL_RD_EN 0x00000001 +#define CTRL_WR_EN 0x00000002 + +/* Command Codes */ +#define CMD_SET_ADDR 0x00D00500 +#define CMD_CFG_DEV 0x00D80500 +#define CMD_SET_MODE 0x00F30500 +#define CMD_RD_FRAME 0x00F50500 +#define DAT_RD_FRAME 0x00F50200 +#define CMD_RD_TEST 0x00FD0500 +#define DAT_RD_TEST 0x00FD0200 +#define CMD_SET_DEV_STAT 0x00FE0500 +#define CMD_GET_DEV_STAT 0x00FE0500 +#define DAT_GET_DEV_STAT 0x00FE0200 +#define CMD_GET_ERR_CODE 0x00FF0500 +#define DAT_GET_ERR_CODE 0x00FF0200 +#define CMD_RD_ERR_STAT 0x00FB0500 +#define DAT_RD_ERR_STAT 0x00FB0200 +#define DAT_WR_BYTE(x) (0x00000100 | ((x) << 16)) +#define CMD_SEL_EP(x) (0x00000500 | ((x) << 16)) +#define DAT_SEL_EP(x) (0x00000200 | ((x) << 16)) +#define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16)) +#define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16)) +#define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16)) +#define CMD_CLR_BUF 0x00F20500 +#define DAT_CLR_BUF 0x00F20200 +#define CMD_VALID_BUF 0x00FA0500 + +/* Device Address Register Definitions */ +#define DEV_ADDR_MASK 0x7F +#define DEV_EN 0x80 + +/* Device Configure Register Definitions */ +#define CONF_DVICE 0x01 + +/* Device Mode Register Definitions */ +#define AP_CLK 0x01 +#define INAK_CI 0x02 +#define INAK_CO 0x04 +#define INAK_II 0x08 +#define INAK_IO 0x10 +#define INAK_BI 0x20 +#define INAK_BO 0x40 + +/* Device Status Register Definitions */ +#define DEV_CON 0x01 +#define DEV_CON_CH 0x02 +#define DEV_SUS 0x04 +#define DEV_SUS_CH 0x08 +#define DEV_RST 0x10 + +/* Error Code Register Definitions */ +#define ERR_EC_MASK 0x0F +#define ERR_EA 0x10 + +/* Error Status Register Definitions */ +#define ERR_PID 0x01 +#define ERR_UEPKT 0x02 +#define ERR_DCRC 0x04 +#define ERR_TIMOUT 0x08 +#define ERR_EOP 0x10 +#define ERR_B_OVRN 0x20 +#define ERR_BTSTF 0x40 +#define ERR_TGL 0x80 + +/* Endpoint Select Register Definitions */ +#define EP_SEL_F 0x01 +#define EP_SEL_ST 0x02 +#define EP_SEL_STP 0x04 +#define EP_SEL_PO 0x08 +#define EP_SEL_EPN 0x10 +#define EP_SEL_B_1_FULL 0x20 +#define EP_SEL_B_2_FULL 0x40 + +/* Endpoint Status Register Definitions */ +#define EP_STAT_ST 0x01 +#define EP_STAT_DA 0x20 +#define EP_STAT_RF_MO 0x40 +#define EP_STAT_CND_ST 0x80 + +/* Clear Buffer Register Definitions */ +#define CLR_BUF_PO 0x01 + + +/* DMA Interrupt Bit Definitions */ +#define EOT_INT 0x01 +#define NDD_REQ_INT 0x02 +#define SYS_ERR_INT 0x04 + + +#endif /* __USBREG_H */ diff --git a/new_cmsis/usb/usbuser.c b/new_cmsis/usb/usbuser.c new file mode 100755 index 0000000..03ade78 --- /dev/null +++ b/new_cmsis/usb/usbuser.c @@ -0,0 +1,336 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbuser.c + * Purpose: USB Custom User Module + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ +#include "lpc_types.h" + +#include "usb.h" +#include "usbcfg.h" +#include "usbhw.h" +#include "usbcore.h" +#include "usbuser.h" +#include "cdcuser.h" + + +/* + * USB Power Event Callback + * Called automatically on USB Power Event + * Parameter: power: On(TRUE)/Off(FALSE) + */ + +#if USB_POWER_EVENT +void USB_Power_Event (uint32_t power) { +} +#endif + + +/* + * USB Reset Event Callback + * Called automatically on USB Reset Event + */ + +#if USB_RESET_EVENT +void USB_Reset_Event (void) { + USB_ResetCore(); +} +#endif + + +/* + * USB Suspend Event Callback + * Called automatically on USB Suspend Event + */ + +#if USB_SUSPEND_EVENT +void USB_Suspend_Event (void) { +} +#endif + + +/* + * USB Resume Event Callback + * Called automatically on USB Resume Event + */ + +#if USB_RESUME_EVENT +void USB_Resume_Event (void) { +} +#endif + + +/* + * USB Remote Wakeup Event Callback + * Called automatically on USB Remote Wakeup Event + */ + +#if USB_WAKEUP_EVENT +void USB_WakeUp_Event (void) { +} +#endif + + +/* + * USB Start of Frame Event Callback + * Called automatically on USB Start of Frame Event + */ + +#if USB_SOF_EVENT +void USB_SOF_Event (void) { +} +#endif + + +/* + * USB Error Event Callback + * Called automatically on USB Error Event + * Parameter: error: Error Code + */ + +#if USB_ERROR_EVENT +void USB_Error_Event (uint32_t error) { +} +#endif + + +/* + * USB Set Configuration Event Callback + * Called automatically on USB Set Configuration Request + */ + +#if USB_CONFIGURE_EVENT +void USB_Configure_Event (void) { + + if (USB_Configuration) { /* Check if USB is configured */ + /* add your code here */ + } +} +#endif + + +/* + * USB Set Interface Event Callback + * Called automatically on USB Set Interface Request + */ + +#if USB_INTERFACE_EVENT +void USB_Interface_Event (void) { +} +#endif + + +/* + * USB Set/Clear Feature Event Callback + * Called automatically on USB Set/Clear Feature Request + */ + +#if USB_FEATURE_EVENT +void USB_Feature_Event (void) { +} +#endif + + +#define P_EP(n) ((USB_EP_EVENT & (1 << (n))) ? USB_EndPoint##n : NULL) + +/* USB Endpoint Events Callback Pointers */ +void (* const USB_P_EP[16]) (uint32_t event) = { + P_EP(0), + P_EP(1), + P_EP(2), + P_EP(3), + P_EP(4), + P_EP(5), + P_EP(6), + P_EP(7), + P_EP(8), + P_EP(9), + P_EP(10), + P_EP(11), + P_EP(12), + P_EP(13), + P_EP(14), + P_EP(15), +}; + + +/* + * USB Endpoint 1 Event Callback + * Called automatically on USB Endpoint 1 Event + * Parameter: event + */ + +void USB_EndPoint1 (uint32_t event) { + uint16_t temp; + static uint16_t serialState; + + switch (event) { + case USB_EVT_IN: + temp = CDC_GetSerialState(); + if (serialState != temp) { + serialState = temp; + CDC_NotificationIn(); /* send SERIAL_STATE notification */ + } + break; + } +} + + +/* + * USB Endpoint 2 Event Callback + * Called automatically on USB Endpoint 2 Event + * Parameter: event + */ + +void USB_EndPoint2 (uint32_t event) { + + switch (event) { + case USB_EVT_OUT: + CDC_BulkOut (); /* data received from Host */ + break; + case USB_EVT_IN: + CDC_BulkIn (); /* data expected from Host */ + break; + } +} + + +/* + * USB Endpoint 3 Event Callback + * Called automatically on USB Endpoint 3 Event + * Parameter: event + */ + +void USB_EndPoint3 (uint32_t event) { +} + + +/* + * USB Endpoint 4 Event Callback + * Called automatically on USB Endpoint 4 Event + * Parameter: event + */ + +void USB_EndPoint4 (uint32_t event) { +} + + +/* + * USB Endpoint 5 Event Callback + * Called automatically on USB Endpoint 5 Event + * Parameter: event + */ + +void USB_EndPoint5 (uint32_t event) { +} + + +/* + * USB Endpoint 6 Event Callback + * Called automatically on USB Endpoint 6 Event + * Parameter: event + */ + +void USB_EndPoint6 (uint32_t event) { +} + + +/* + * USB Endpoint 7 Event Callback + * Called automatically on USB Endpoint 7 Event + * Parameter: event + */ + +void USB_EndPoint7 (uint32_t event) { +} + + +/* + * USB Endpoint 8 Event Callback + * Called automatically on USB Endpoint 8 Event + * Parameter: event + */ + +void USB_EndPoint8 (uint32_t event) { +} + + +/* + * USB Endpoint 9 Event Callback + * Called automatically on USB Endpoint 9 Event + * Parameter: event + */ + +void USB_EndPoint9 (uint32_t event) { +} + + +/* + * USB Endpoint 10 Event Callback + * Called automatically on USB Endpoint 10 Event + * Parameter: event + */ + +void USB_EndPoint10 (uint32_t event) { +} + + +/* + * USB Endpoint 11 Event Callback + * Called automatically on USB Endpoint 11 Event + * Parameter: event + */ + +void USB_EndPoint11 (uint32_t event) { +} + + +/* + * USB Endpoint 12 Event Callback + * Called automatically on USB Endpoint 12 Event + * Parameter: event + */ + +void USB_EndPoint12 (uint32_t event) { +} + + +/* + * USB Endpoint 13 Event Callback + * Called automatically on USB Endpoint 13 Event + * Parameter: event + */ + +void USB_EndPoint13 (uint32_t event) { +} + + +/* + * USB Endpoint 14 Event Callback + * Called automatically on USB Endpoint 14 Event + * Parameter: event + */ + +void USB_EndPoint14 (uint32_t event) { +} + + +/* + * USB Endpoint 15 Event Callback + * Called automatically on USB Endpoint 15 Event + * Parameter: event + */ + +void USB_EndPoint15 (uint32_t event) { +} diff --git a/new_cmsis/usb/usbuser.h b/new_cmsis/usb/usbuser.h new file mode 100755 index 0000000..dee1d73 --- /dev/null +++ b/new_cmsis/usb/usbuser.h @@ -0,0 +1,74 @@ +/*---------------------------------------------------------------------------- + * U S B - K e r n e l + *---------------------------------------------------------------------------- + * Name: usbuser.h + * Purpose: USB Custom User Definitions + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC family microcontroller devices only. Nothing + * else gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#ifndef __USBUSER_H__ +#define __USBUSER_H__ + + +/* USB Device Events Callback Functions */ +extern void USB_Power_Event (uint32_t power); +extern void USB_Reset_Event (void); +extern void USB_Suspend_Event (void); +extern void USB_Resume_Event (void); +extern void USB_WakeUp_Event (void); +extern void USB_SOF_Event (void); +extern void USB_Error_Event (uint32_t error); + +/* USB Endpoint Callback Events */ +#define USB_EVT_SETUP 1 /* Setup Packet */ +#define USB_EVT_OUT 2 /* OUT Packet */ +#define USB_EVT_IN 3 /* IN Packet */ +#define USB_EVT_OUT_NAK 4 /* OUT Packet - Not Acknowledged */ +#define USB_EVT_IN_NAK 5 /* IN Packet - Not Acknowledged */ +#define USB_EVT_OUT_STALL 6 /* OUT Packet - Stalled */ +#define USB_EVT_IN_STALL 7 /* IN Packet - Stalled */ +#define USB_EVT_OUT_DMA_EOT 8 /* DMA OUT EP - End of Transfer */ +#define USB_EVT_IN_DMA_EOT 9 /* DMA IN EP - End of Transfer */ +#define USB_EVT_OUT_DMA_NDR 10 /* DMA OUT EP - New Descriptor Request */ +#define USB_EVT_IN_DMA_NDR 11 /* DMA IN EP - New Descriptor Request */ +#define USB_EVT_OUT_DMA_ERR 12 /* DMA OUT EP - Error */ +#define USB_EVT_IN_DMA_ERR 13 /* DMA IN EP - Error */ + +/* USB Endpoint Events Callback Pointers */ +extern void (* const USB_P_EP[16])(uint32_t event); + +/* USB Endpoint Events Callback Functions */ +extern void USB_EndPoint0 (uint32_t event); +extern void USB_EndPoint1 (uint32_t event); +extern void USB_EndPoint2 (uint32_t event); +extern void USB_EndPoint3 (uint32_t event); +extern void USB_EndPoint4 (uint32_t event); +extern void USB_EndPoint5 (uint32_t event); +extern void USB_EndPoint6 (uint32_t event); +extern void USB_EndPoint7 (uint32_t event); +extern void USB_EndPoint8 (uint32_t event); +extern void USB_EndPoint9 (uint32_t event); +extern void USB_EndPoint10 (uint32_t event); +extern void USB_EndPoint11 (uint32_t event); +extern void USB_EndPoint12 (uint32_t event); +extern void USB_EndPoint13 (uint32_t event); +extern void USB_EndPoint14 (uint32_t event); +extern void USB_EndPoint15 (uint32_t event); + +/* USB Core Events Callback Functions */ +extern void USB_Configure_Event (void); +extern void USB_Interface_Event (void); +extern void USB_Feature_Event (void); + + +#endif /* __USBUSER_H__ */ diff --git a/new_cmsis/usb/vcomdemo.c b/new_cmsis/usb/vcomdemo.c new file mode 100755 index 0000000..e99e94e --- /dev/null +++ b/new_cmsis/usb/vcomdemo.c @@ -0,0 +1,124 @@ +/*---------------------------------------------------------------------------- + * Name: vcomdemo.c + * Purpose: USB virtual COM port Demo + * Version: V1.20 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +#include "../LPC17xx.h" +#include "lpc_types.h" + +#include "usb.h" +#include "usbcfg.h" +#include "usbhw.h" +#include "usbcore.h" +#include "cdc.h" +#include "cdcuser.h" +#include "serial.h" +#include "vcomdemo.h" + +/* Example group ----------------------------------------------------------- */ +/** @defgroup USBDEV_USBCDC USBCDC + * @ingroup USBDEV_Examples + * @{ + */ + +/*---------------------------------------------------------------------------- + Initialises the VCOM port. + Call this function before using VCOM_putchar or VCOM_getchar + *---------------------------------------------------------------------------*/ +void VCOM_Init(void) { +#if PORT_NUM + CDC_Init (1); +#else + CDC_Init (0); +#endif +} + + +/*---------------------------------------------------------------------------- + Reads character from serial port buffer and writes to USB buffer + *---------------------------------------------------------------------------*/ +void VCOM_Serial2Usb(void) { + static char serBuf [USB_CDC_BUFSIZE]; + int numBytesRead, numAvailByte; + + ser_AvailChar (&numAvailByte); + if (numAvailByte > 0) { + if (CDC_DepInEmpty) { + numBytesRead = ser_Read (&serBuf[0], &numAvailByte); + + CDC_DepInEmpty = 0; + USB_WriteEP (CDC_DEP_IN, (unsigned char *)&serBuf[0], numBytesRead); + } + } + +} + +/*---------------------------------------------------------------------------- + Reads character from USB buffer and writes to serial port buffer + *---------------------------------------------------------------------------*/ +void VCOM_Usb2Serial(void) { + static char serBuf [32]; + int numBytesToRead, numBytesRead, numAvailByte; + + CDC_OutBufAvailChar (&numAvailByte); + if (numAvailByte > 0) { + numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; + numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead); +#if PORT_NUM + ser_Write (1, &serBuf[0], &numBytesRead); +#else + ser_Write (0, &serBuf[0], &numBytesRead); +#endif + } + +} + + +/*---------------------------------------------------------------------------- + checks the serial state and initiates notification + *---------------------------------------------------------------------------*/ +void VCOM_CheckSerialState (void) { + unsigned short temp; + static unsigned short serialState; + + temp = CDC_GetSerialState(); + if (serialState != temp) { + serialState = temp; + CDC_NotificationIn(); // send SERIAL_STATE notification + } +} + +/*---------------------------------------------------------------------------- + Main Program + *---------------------------------------------------------------------------*/ +int main (void) { + SystemInit(); + + VCOM_Init(); // VCOM Initialization + + USB_Init(); // USB Initialization + USB_Connect(TRUE); // USB Connect + + while (!USB_Configuration) ; // wait until USB is configured + + while (1) { // Loop forever + VCOM_Serial2Usb(); // read serial port and initiate USB event + VCOM_CheckSerialState(); + VCOM_Usb2Serial(); + } // end while +} // end main () + +/* + * @} + */ diff --git a/new_cmsis/usb/vcomdemo.h b/new_cmsis/usb/vcomdemo.h new file mode 100755 index 0000000..75cbbd8 --- /dev/null +++ b/new_cmsis/usb/vcomdemo.h @@ -0,0 +1,31 @@ +/*---------------------------------------------------------------------------- + * Name: vcomdemo.h + * Purpose: USB virtual COM port Demo Definitions + * Version: V1.02 + *---------------------------------------------------------------------------- + * This software is supplied "AS IS" without any warranties, express, + * implied or statutory, including but not limited to the implied + * warranties of fitness for purpose, satisfactory quality and + * noninfringement. Keil extends you a royalty-free right to reproduce + * and distribute executable files created using this software for use + * on NXP Semiconductors LPC microcontroller devices only. Nothing else + * gives you the right to use this software. + * + * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. + *---------------------------------------------------------------------------*/ + +/* Push Button Definitions */ +#define S2 0x00000400 /* P2.10 */ + +/* LED Definitions */ +#define LED1 0x00000001 /* P2.00 */ +#define LED2 0x00000002 /* P2.01 */ +#define LED3 0x00000004 /* P2.02 */ +#define LED4 0x00000008 /* P2.03 */ +#define LED5 0x00000010 /* P2.04 */ +#define LED6 0x00000020 /* P2.05 */ +#define LED7 0x00000040 /* P2.06 */ +#define LED8 0x00000080 /* P2.07 */ + +#define LEDMSK 0x000000FF /* P2.0..7 */ + -- 1.7.10.4