diff --git a/firmware/Makefile b/firmware/Makefile index 5eaabd3..5dc5f6c 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -1,6 +1,6 @@ NAME = edubrm CFLAGS = -Wall -Werror -D__REDLIB__ -DDEBUG -D__CODE_RED -D__USE_CMSIS=CMSISv1p30_LPC13xx -O0 -g3 -Wall -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=cortex-m3 -mthumb -LDFLAGS = -nostdlib -Xlinker -Map=obj/$(NAME).map --gc-sections -mcpu=cortex-m3 -mthumb -T obj/linker.ld +LDFLAGS = -nostdlib -Xlinker -Map=obj/$(NAME).map --gc-sections -mcpu=cortex-m3 -mthumb -T obj/usb_buffer.ld LIST = $(shell cd src/ ; ls *.c) OBJS = $(addprefix obj/,$(LIST:.c=.o)) SRCS = $(addprefix src/,$(LIST)) @@ -28,3 +28,6 @@ deploy: crt_emu_lpc11_13_nxp -g -2 -pLPC1343 -wire=winusb -info-target crt_emu_lpc11_13_nxp -g -2 -pLPC1343 -wire=winusb -flash-erase crt_emu_lpc11_13_nxp -g -2 -pLPC1343 -wire=winusb -flash-load=obj/$(NAME).axf + +test: + gcc test.c -o test -lusb-1.0 diff --git a/firmware/obj/linker.ld b/firmware/obj/linker.ld deleted file mode 100644 index cefaabd..0000000 --- a/firmware/obj/linker.ld +++ /dev/null @@ -1,98 +0,0 @@ -/* -* GENERATED FILE - DO NOT EDIT -* (C) Code Red Technologies Ltd, 2008-10 -* Generated linker script file for LPC1343 -* Created from nxp_lpc13_c.ld (vLPCXpresso 3.8 (2 [Build 129] [31/01/2011] )) -* By LPCXpresso 3.8.2 [Build 129] [31/01/2011] on Fri Apr 01 20:01:18 CEST 2011 -*/ - - -INCLUDE "obj/linker_lib.ld" -INCLUDE "obj/linker_mem.ld" - -ENTRY(ResetISR) - -SECTIONS -{ - - /* MAIN TEXT SECTION */ - .text : ALIGN(4) - { - FILL(0xff) - KEEP(*(.isr_vector)) - - /* Global Section Table */ - . = ALIGN(4) ; - __section_table_start = .; - __data_section_table = .; - LONG(LOADADDR(.data)); - LONG( ADDR(.data)) ; - LONG( SIZEOF(.data)); - __data_section_table_end = .; - __bss_section_table = .; - LONG( ADDR(.bss)); - LONG( SIZEOF(.bss)); - __bss_section_table_end = .; - __section_table_end = . ; - /* End of Global Section Table */ - - - *(.after_vectors*) - - *(.text*) - *(.rodata .rodata.*) - . = ALIGN(4); - - } > MFlash32 - - /* - * for exception handling/unwind - some Newlib functions (in common - * with C++ and STDC++) use this. - */ - .ARM.extab : ALIGN(4) - { - *(.ARM.extab* .gnu.linkonce.armextab.*) - } > MFlash32 - __exidx_start = .; - - .ARM.exidx : ALIGN(4) - { - *(.ARM.exidx* .gnu.linkonce.armexidx.*) - } > MFlash32 - __exidx_end = .; - - _etext = .; - - - /* MAIN DATA SECTION */ - - .uninit_RESERVED : ALIGN(4) - { - KEEP(*(.bss.$RESERVED*)) - } > RamLoc8 - - .data : ALIGN(4) - { - FILL(0xff) - _data = .; - *(vtable) - *(.data*) - . = ALIGN(4) ; - _edata = .; - } > RamLoc8 AT>MFlash32 - - - /* MAIN BSS SECTION */ - .bss : ALIGN(4) - { - _bss = .; - *(.bss*) - *(COMMON) - . = ALIGN(4) ; - _ebss = .; - PROVIDE(end = .); - } > RamLoc8 - - PROVIDE(_pvHeapStart = .); - PROVIDE(_vStackTop = __top_RamLoc8 - 0); -} diff --git a/firmware/obj/linker_lib.ld b/firmware/obj/linker_lib.ld deleted file mode 100644 index dcdbf24..0000000 --- a/firmware/obj/linker_lib.ld +++ /dev/null @@ -1,8 +0,0 @@ -/* - * GENERATED FILE - DO NOT EDIT - * (C) Code Red Technologies Ltd, 2008-9 - * Generated linker script library include file for Redlib (none) - * (created from redlib_none_c.ld (LPCXpresso 3.8.2 [Build 129] [31/01/2011] ) on Fri Apr 01 20:01:18 CEST 2011) -*/ - -GROUP(libcr_c.a libcr_eabihelpers.a) diff --git a/firmware/obj/usb_buffer.ld b/firmware/obj/usb_buffer.ld new file mode 100644 index 0000000..4d8c42e --- /dev/null +++ b/firmware/obj/usb_buffer.ld @@ -0,0 +1,71 @@ +/* + * GENERATED FILE - DO NOT EDIT + * (C) Code Red Technologies Ltd, 2008-9 + * Generated C linker script file for LPC1343 + * (created from nxp_lpc13_c.ld (v3.0.6 (200911181345)) on Fri Nov 20 17:14:35 PST 2009) +*/ + +INCLUDE "obj/usb_buffer_lib.ld" +INCLUDE "obj/usb_buffer_mem.ld" + +ENTRY(ResetISR) + +SECTIONS +{ + .text : + { + KEEP(*(.isr_vector)) + *(.text*) + *(.rodata*) + + } > MFlash32 + + + /* for exception handling/unwind - some Newlib functions (in common with C++ and STDC++) use this. */ + + .ARM.extab : + { + *(.ARM.extab* .gnu.linkonce.armextab.*) + } > MFlash32 + + __exidx_start = .; + .ARM.exidx : + { + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + } > MFlash32 + __exidx_end = .; + + _etext = .; + + .data : AT (__exidx_end) + { + _data = .; + *(vtable) + *(.data*) + _edata = .; + } > RamLoc8 + + /* zero initialized data */ + .bss : + { + _bss = .; + *(.bss*) + *(COMMON) + _ebss = .; + } > RamLoc8 + + /* Where we put the heap with cr_clib */ + .cr_heap : + { + end = .; + _pvHeapStart = .; + } > RamLoc8 + +/* + Note: (ref: M0000066) + Moving the stack down by 16 is to work around a GDB bug. + This space can be reclaimed for Production Builds. +*/ + _vRamTop = __top_RamLoc8 ; + _vStackTop = _vRamTop - 16; +} diff --git a/firmware/obj/usb_buffer_lib.ld b/firmware/obj/usb_buffer_lib.ld new file mode 100644 index 0000000..5d88bfc --- /dev/null +++ b/firmware/obj/usb_buffer_lib.ld @@ -0,0 +1,8 @@ +/* + * GENERATED FILE - DO NOT EDIT + * (C) Code Red Technologies Ltd, 2008-9 + * Generated linker script library include file for Redlib (semihost) + * (created from redlib_semihost_c.ld (v3.0.6 (200911181345)) on Fri Nov 20 17:14:35 PST 2009) +*/ + +GROUP(libcr_c.a libcr_semihost.a libcr_eabihelpers.a) diff --git a/firmware/obj/linker_mem.ld b/firmware/obj/usb_buffer_mem.ld similarity index 66% rename from firmware/obj/linker_mem.ld rename to firmware/obj/usb_buffer_mem.ld index 9a41d5d..bf81734 100644 --- a/firmware/obj/linker_mem.ld +++ b/firmware/obj/usb_buffer_mem.ld @@ -2,14 +2,14 @@ * GENERATED FILE - DO NOT EDIT * (C) Code Red Technologies Ltd, 2008-9 * Generated linker script include file for - * (created from LinkMemoryTemplate (LPCXpresso 3.8.2 [Build 129] [31/01/2011] ) on Fri Apr 01 20:01:18 CEST 2011) + * (created from LinkMemoryTemplate (v3.0.6 (200911181345)) on Fri Nov 20 17:14:35 PST 2009) */ MEMORY { /* Define each memory region */ MFlash32 (rx) : ORIGIN = 0x0, LENGTH = 0x8000 /* 32k */ - RamLoc8 (rwx) : ORIGIN = 0x10000000, LENGTH = 0x2000 /* 8k */ + RamLoc8 (rwx) : ORIGIN = 0x10000180, LENGTH = 0x1E80 /* 8k */ } /* Define a symbol for the top of each memory region */ diff --git a/firmware/src/cdc.h b/firmware/src/cdc.h deleted file mode 100644 index f7af7d3..0000000 --- a/firmware/src/cdc.h +++ /dev/null @@ -1,236 +0,0 @@ -/*---------------------------------------------------------------------------- - * U S B - K e r n e l - *---------------------------------------------------------------------------- - * Name: CDC.h - * Purpose: USB Communication Device Class Definitions - * Version: V1.00 - *---------------------------------------------------------------------------- - * 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 __CDC_H -#define __CDC_H - -/*---------------------------------------------------------------------------- - * 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. -typedef struct _CDC_HEADER_DESCRIPTOR{ - 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 -} __attribute__((packed)) CDC_HEADER_DESCRIPTOR; - -//Call management functional descriptor -// (usbcdc11.pdf, 5.2.3.2) -// Describes the processing of calls for the communication class interface. -typedef struct _CDC_CALL_MANAGEMENT_DESCRIPTOR { - 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) -} __attribute__((packed)) 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. -typedef struct _CDC_ABSTRACT_CONTROL_MANAGEMENT_DESCRIPTOR { - 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 -} __attribute__((packed)) 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. -typedef struct _CDC_UNION_DESCRIPTOR { - 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 -} __attribute__((packed)) CDC_UNION_DESCRIPTOR; - -// Union functional descriptors with one slave interface -// (usbcdc11.pdf, 5.2.3.8) -typedef struct _CDC_UNION_1SLAVE_DESCRIPTOR { - CDC_UNION_DESCRIPTOR sUnion; // Union functional descriptor - uint8_t bSlaveInterfaces[1]; // Slave interface 0 -} __attribute__((packed)) CDC_UNION_1SLAVE_DESCRIPTOR; - -// Line coding structure -// Format of the data returned when a GetLineCoding request is received -// (usbcdc11.pdf, 6.2.13) -typedef struct _CDC_LINE_CODING { - 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 -} __attribute__((packed)) 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/firmware/src/cdcuser.c b/firmware/src/cdcuser.c deleted file mode 100644 index f815608..0000000 --- a/firmware/src/cdcuser.c +++ /dev/null @@ -1,360 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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 "type.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 (void) { - - ser_OpenPort (); - ser_InitPort (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]; - - ser_ClosePort(); - ser_OpenPort (); - ser_InitPort (CDC_LineCoding.dwDTERate, - CDC_LineCoding.bDataBits, - CDC_LineCoding.bParityType, - CDC_LineCoding.bCharFormat); - 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/firmware/src/cdcuser.h b/firmware/src/cdcuser.h deleted file mode 100644 index 7512157..0000000 --- a/firmware/src/cdcuser.h +++ /dev/null @@ -1,63 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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 int CDC_WrOutBuf (const char *buffer, int *length); -extern int CDC_OutBufAvailChar (int *availChar); - - -/* CDC Data In/Out Endpoint Address */ -#define CDC_DEP_IN 0x83 -#define CDC_DEP_OUT 0x03 - -/* 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 (void); - -/* 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/firmware/src/clkconfig.c b/firmware/src/clkconfig.c deleted file mode 100644 index 346a83e..0000000 --- a/firmware/src/clkconfig.c +++ /dev/null @@ -1,60 +0,0 @@ -/****************************************************************************** - * clkconfig.c: clock configuration C file for NXP LPC13xx Family - * Microprocessors - * - * Copyright(C) 2009, NXP Semiconductor - * All rights reserved. - * - * History - * 2009.08.20 ver 1.00 Preliminary version, first Release - * -******************************************************************************/ -#include "LPC13xx.h" -#include "clkconfig.h" - -/***************************************************************************** -** Function name: WDT_CLK_Setup -** -** Descriptions: Configure WDT clock. -** parameters: clock source: irc_osc(0), main_clk(1), wdt_osc(2). -** -** Returned value: None -** -*****************************************************************************/ -void WDT_CLK_Setup ( uint32_t clksrc ) -{ - /* Watchdog configuration. */ - /* Freq = 0.5Mhz, div_sel is 0, divided by 2. WDT_OSC should be 250khz */ - LPC_SYSCON->WDTOSCCTRL = (0x1<<5)|0x00; - LPC_SYSCON->WDTCLKSEL = clksrc; /* Select clock source */ - LPC_SYSCON->WDTCLKUEN = 0x01; /* Update clock */ - LPC_SYSCON->WDTCLKUEN = 0x00; /* Toggle update register once */ - LPC_SYSCON->WDTCLKUEN = 0x01; - while ( !(LPC_SYSCON->WDTCLKUEN & 0x01) ); /* Wait until updated */ - LPC_SYSCON->WDTCLKDIV = 1; /* Divided by 1 */ - LPC_SYSCON->PDRUNCFG &= ~(0x1<<6); /* Let WDT clock run */ - return; -} - -/***************************************************************************** -** Function name: CLKOUT_Setup -** -** Descriptions: Configure CLKOUT for reference clock check. -** parameters: clock source: irc_osc(0), sys_osc(1), wdt_osc(2), -** main_clk(3). -** -** Returned value: None -** -*****************************************************************************/ -void CLKOUT_Setup ( uint32_t clksrc ) -{ - /* debug PLL after configuration. */ - LPC_SYSCON->CLKOUTCLKSEL = clksrc; /* Select Main clock */ - LPC_SYSCON->CLKOUTUEN = 0x01; /* Update clock */ - LPC_SYSCON->CLKOUTUEN = 0x00; /* Toggle update register once */ - LPC_SYSCON->CLKOUTUEN = 0x01; - while ( !(LPC_SYSCON->CLKOUTUEN & 0x01) ); /* Wait until updated */ - LPC_SYSCON->CLKOUTDIV = 1; /* Divided by 1 */ - return; -} - diff --git a/firmware/src/clkconfig.h b/firmware/src/clkconfig.h deleted file mode 100644 index b1e2866..0000000 --- a/firmware/src/clkconfig.h +++ /dev/null @@ -1,28 +0,0 @@ -/***************************************************************************** - * clkconfig.h: Header file for NXP LPC13xx Family Microprocessors - * - * Copyright(C) 2009, NXP Semiconductor - * All rights reserved. - * - * History - * 2009.08.20 ver 1.00 Preliminary version, first Release - * -******************************************************************************/ -#ifndef __CLKCONFIG_H -#define __CLKCONFIG_H - -#define WDTCLK_SRC_IRC_OSC 0 -#define WDTCLK_SRC_MAIN_CLK 1 -#define WDTCLK_SRC_WDT_OSC 2 - -#define CLKOUTCLK_SRC_IRC_OSC 0 -#define CLKOUTCLK_SRC_SYS_OSC 1 -#define CLKOUTCLK_SRC_WDT_OSC 2 -#define CLKOUTCLK_SRC_MAIN_CLK 3 - -void WDT_CLK_Setup(uint32_t timer_num); -void CLKOUT_Setup(uint32_t timer_num); -#endif /* end __CLKCONFIG_H */ -/***************************************************************************** -** End Of File -******************************************************************************/ diff --git a/firmware/src/compiler.h b/firmware/src/compiler.h new file mode 100644 index 0000000..098b9f5 --- /dev/null +++ b/firmware/src/compiler.h @@ -0,0 +1,48 @@ +/***************************************************************************** + * compiler.h: Compiler-specific header file for NXP Family + * Microprocessors + * + * Copyright(C) 2009, NXP Semiconductor + * All rights reserved. + * + * History + * 2009.12.23 ver 1.00 Preliminary version, first Release + * +******************************************************************************/ +#ifndef __COMPILER_H__ +#define __COMPILER_H__ + +#include "LPC13xx.h" // Defines __ASM and __INLINE + +#if !defined(__IAR__) && !defined(__KEIL__) && !defined(__CODERED__) + +#if defined(__IAR_SYSTEMS_ICC__) +#define __IAR__ +#endif + +#if defined(__GNUC__) +#define __CODERED__ +#endif + +#if defined(__CC_ARM) +#define __KEIL__ +#endif + +#endif +/* +Various compiler declaration methods for packed structures: +GNU/IAR: typedef struct { blah blah blah } __attribute__((packed)) structName; +Keil: typedef __packed struct { blah blah blah } structName; +*/ +#if defined(__GNUC__) +#define PACKED_PRE /**/ +#define PACKED_POST __attribute__((packed)) +#elif defined(__IAR_SYSTEMS_ICC__) +#define PACKED_PRE /**/ +#define PACKED_POST __packed +#else /* Keil? */ +#define PACKED_PRE __packed +#define PACKED_POST /**/ +#endif + +#endif /* __COMPILER_H__ */ diff --git a/firmware/src/config.h b/firmware/src/config.h index 28af6b5..838bd4c 100644 --- a/firmware/src/config.h +++ b/firmware/src/config.h @@ -1,41 +1,4 @@ -/***************************************************************************** - * config.h: config file for usbcdc example for NXP LPC13xx Family - * Microprocessors - * - * Copyright(C) 2008, NXP Semiconductor - * All rights reserved. - * - * History - * 2008.07.19 ver 1.00 Preliminary version, first Release - * -******************************************************************************/ - -/* -Overview: - This example shows how to use the USB driver to implement a CDC class USB peripheral. - To run this example, you must attach a USB cable to the board. See - the "Getting Started Guide" appendix for details. - -How to use: - Click the debug toolbar button. - Click the go button. - Plug the LPCXpresso's target side into a PC using a USB cable retrofit - or a 3rd party base board. - - * You should be able to see a new COM port on your PC. -*/ - -#define NXP_VID 0x1FC9 -#define MY_VID 0x???? - -#define USB_VENDOR_ID NXP_VID // Vendor ID +#define NXP_VID 0x1FC9 +#define USB_VENDOR_ID NXP_VID // Vendor ID #define USB_PROD_ID 0x0003 // Product ID #define USB_DEVICE 0x0100 // Device ID - -#define LED_PORT 0 // Port for led -#define LED_BIT 7 // Bit on port for led - - -/********************************************************************************* -** End Of File -*********************************************************************************/ diff --git a/firmware/src/cr_startup_lpc13.c b/firmware/src/cr_startup_lpc13.c index 89e42a3..c49cb72 100644 --- a/firmware/src/cr_startup_lpc13.c +++ b/firmware/src/cr_startup_lpc13.c @@ -34,6 +34,7 @@ #include "system_LPC13xx.h" #endif + //***************************************************************************** // // Forward declaration of the default handlers. These are aliased. @@ -81,6 +82,13 @@ void PIOINT1_IRQHandler (void) ALIAS(IntDefaultHandler); void PIOINT0_IRQHandler (void) ALIAS(IntDefaultHandler); void WAKEUP_IRQHandler (void) ALIAS(IntDefaultHandler); +//***************************************************************************** +// +// The entry point for the C++ library startup +// +//***************************************************************************** +extern WEAK void __libc_init_array(void); + //***************************************************************************** // // The entry point for the application. @@ -249,6 +257,12 @@ Reset_Handler(void) " strlt r2, [r0], #4\n" " blt zero_loop"); + // + // Call C++ library initialization, if present + // + if (__libc_init_array) + __libc_init_array() ; + #ifdef __USE_CMSIS SystemInit(); #endif diff --git a/firmware/src/edubrm.c b/firmware/src/edubrm.c deleted file mode 100644 index e12655e..0000000 --- a/firmware/src/edubrm.c +++ /dev/null @@ -1,118 +0,0 @@ -#include "LPC13xx.h" -#include "type.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" -#include "mscuser.h" -#include "memory.h" -#include "stdio.h" -#include "string.h" - -#include "edubrm.h" - - -#define mainLED_BIT ( 7 ) - -void VCOM_Brm2Usb() { -/* - static char serBuf [USB_CDC_BUFSIZE]; - int numBytesRead, numAvailByte; - - - serBuf[0] = 'A'; - numBytesRead = 1; - - 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); - } - } -*/ - -} - -static char cmdInbuffer[256]; -int cmdInbufferIndex = 0; - -int checkForCommand() { - int i=0; - for (i=0;iDIR |= ( 0x1 << mainLED_BIT ); -} - -void disableLED() { - LPC_GPIO0->DIR |= ( 0x0 << mainLED_BIT ); -} - -void toggleLED() { - ulLEDState = !ulLEDState; - LPC_GPIO0->MASKED_ACCESS[ ( 1 << mainLED_BIT) ] = ( ulLEDState << mainLED_BIT ); - -} - -void sendToUSB(char *string) { - USB_WriteEP (CDC_DEP_IN, (unsigned char *)string, strlen(string)); -} - - -void commandReceived(char * receivedCommand) { - - if (strcmp("PING", receivedCommand) == 0) { - //echo back - sendToUSB("PONG\n"); - toggleLED(); - } else if (strcmp("VERSION", receivedCommand) == 0) { - sendToUSB(VERSION); - toggleLED(); - } -} - - - -void VCOM_Usb2Brm() { - static char serBuf [32]; - int numBytesToRead, numBytesRead, numAvailByte; - int i=0; - static char receivedCommand[128]; - - CDC_OutBufAvailChar (&numAvailByte); - if (numAvailByte > 0) { - numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; - numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead); - for (i=0;iSYSAHBCLKCTRL |= (1<<6); + +#ifdef __JTAG_DISABLED + LPC_IOCON->JTAG_TDO_PIO1_1 &= ~0x07; + LPC_IOCON->JTAG_TDO_PIO1_1 |= 0x01; +#endif + + /* Set up NVIC when I/O pins are configured as external interrupts. */ + NVIC_EnableIRQ(EINT0_IRQn); + NVIC_EnableIRQ(EINT1_IRQn); + NVIC_EnableIRQ(EINT2_IRQn); + NVIC_EnableIRQ(EINT3_IRQn); + return; +} + +/***************************************************************************** +** Function name: GPIOSetInterrupt +** +** Descriptions: Set interrupt sense, event, etc. +** edge or level, 0 is edge, 1 is level +** single or double edge, 0 is single, 1 is double +** active high or low, etc. +** +** parameters: port num, bit position, sense, single/doube, polarity +** Returned value: None +** +*****************************************************************************/ +void GPIOSetInterrupt( uint32_t portNum, uint32_t bitPosi, uint32_t sense, + uint32_t single, uint32_t event ) +{ + switch ( portNum ) + { + case PORT0: + if ( sense == 0 ) + { + LPC_GPIO0->IS &= ~(0x1<IBE &= ~(0x1<IBE |= (0x1<IS |= (0x1<IEV &= ~(0x1<IEV |= (0x1<IS &= ~(0x1<IBE &= ~(0x1<IBE |= (0x1<IS |= (0x1<IEV &= ~(0x1<IEV |= (0x1<IS &= ~(0x1<IBE &= ~(0x1<IBE |= (0x1<IS |= (0x1<IEV &= ~(0x1<IEV |= (0x1<IS &= ~(0x1<IBE &= ~(0x1<IBE |= (0x1<IS |= (0x1<IEV &= ~(0x1<IEV |= (0x1<IE |= (0x1<IE |= (0x1<IE |= (0x1<IE |= (0x1<IE &= ~(0x1<IE &= ~(0x1<IE &= ~(0x1<IE &= ~(0x1<MIS & (0x1<MIS & (0x1<MIS & (0x1<MIS & (0x1<IC |= (0x1<IC |= (0x1<IC |= (0x1<IC |= (0x1<MASKED_ACCESS[(1<DIR |= 1<DIR &= ~(1< MSC_MAX_PACKET) { - n = MSC_MAX_PACKET; - } else { - n = Length; - } - - if ((Offset + n) > MSC_MemorySize) { - n = MSC_MemorySize - Offset; - BulkStage = MSC_BS_DATA_IN_LAST_STALL; - } - - USB_WriteEP(MSC_EP_IN, &Memory[Offset], n); - Offset += n; - Length -= n; - - CSW.dDataResidue -= n; - - if (Length == 0) { - BulkStage = MSC_BS_DATA_IN_LAST; - } - - if (BulkStage != MSC_BS_DATA_IN) { - CSW.bStatus = CSW_CMD_PASSED; - } -} - - -/* - * MSC Memory Write Callback - * Called automatically on Memory Write Event - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_MemoryWrite (void) { - uint32_t n; - - if ((Offset + BulkLen) > MSC_MemorySize) { - BulkLen = MSC_MemorySize - Offset; - BulkStage = MSC_BS_CSW; - MSC_SetStallEP(MSC_EP_OUT); - } - - for (n = 0; n < BulkLen; n++) { - Memory[Offset + n] = BulkBuf[n]; - } - - Offset += BulkLen; - Length -= BulkLen; - - CSW.dDataResidue -= BulkLen; - - if ((Length == 0) || (BulkStage == MSC_BS_CSW)) { - CSW.bStatus = CSW_CMD_PASSED; - MSC_SetCSW(); - } -} - - -/* - * MSC Memory Verify Callback - * Called automatically on Memory Verify Event - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_MemoryVerify (void) { - uint32_t n; - - if ((Offset + BulkLen) > MSC_MemorySize) { - BulkLen = MSC_MemorySize - Offset; - BulkStage = MSC_BS_CSW; - MSC_SetStallEP(MSC_EP_OUT); - } - - for (n = 0; n < BulkLen; n++) { - if (Memory[Offset + n] != BulkBuf[n]) { - MemOK = FALSE; - break; - } - } - - Offset += BulkLen; - Length -= BulkLen; - - CSW.dDataResidue -= BulkLen; - - if ((Length == 0) || (BulkStage == MSC_BS_CSW)) { - CSW.bStatus = (MemOK) ? CSW_CMD_PASSED : CSW_CMD_FAILED; - MSC_SetCSW(); - } -} - - -/* - * MSC SCSI Read/Write Setup Callback - * Parameters: None (global variables) - * Return Value: TRUE - Success, FALSE - Error - */ - -uint32_t MSC_RWSetup (void) { - uint32_t n; - - /* Logical Block Address of First Block */ - n = (CBW.CB[2] << 24) | - (CBW.CB[3] << 16) | - (CBW.CB[4] << 8) | - (CBW.CB[5] << 0); - - Offset = n * MSC_BlockSize; - - /* Number of Blocks to transfer */ - switch (CBW.CB[0]) { - case SCSI_READ10: - case SCSI_WRITE10: - case SCSI_VERIFY10: - n = (CBW.CB[7] << 8) | - (CBW.CB[8] << 0); - break; - - case SCSI_READ12: - case SCSI_WRITE12: - n = (CBW.CB[6] << 24) | - (CBW.CB[7] << 16) | - (CBW.CB[8] << 8) | - (CBW.CB[9] << 0); - break; - } - - Length = n * MSC_BlockSize; - - if (CBW.dDataLength == 0) { /* host requests no data */ - CSW.bStatus = CSW_CMD_FAILED; - MSC_SetCSW(); - return (FALSE); - } - - if (CBW.dDataLength != Length) { - if ((CBW.bmFlags & 0x80) != 0) { /* stall appropriate EP */ - MSC_SetStallEP(MSC_EP_IN); - } else { - MSC_SetStallEP(MSC_EP_OUT); - } - - CSW.bStatus = CSW_CMD_FAILED; - MSC_SetCSW(); - - return (FALSE); - } - - return (TRUE); -} - - -/* - * Check Data IN Format - * Parameters: None (global variables) - * Return Value: TRUE - Success, FALSE - Error - */ - -uint32_t DataInFormat (void) { - - if (CBW.dDataLength == 0) { - CSW.bStatus = CSW_PHASE_ERROR; - MSC_SetCSW(); - return (FALSE); - } - if ((CBW.bmFlags & 0x80) == 0) { - MSC_SetStallEP(MSC_EP_OUT); - CSW.bStatus = CSW_PHASE_ERROR; - MSC_SetCSW(); - return (FALSE); - } - return (TRUE); -} - - -/* - * Perform Data IN Transfer - * Parameters: None (global variables) - * Return Value: TRUE - Success, FALSE - Error - */ - -void DataInTransfer (void) { - - if (BulkLen >= CBW.dDataLength) - BulkLen = CBW.dDataLength; - - BulkStage = MSC_BS_DATA_IN_LAST; - - USB_WriteEP(MSC_EP_IN, BulkBuf, BulkLen); - - CSW.dDataResidue -= BulkLen; - CSW.bStatus = CSW_CMD_PASSED; -} - - -/* - * MSC SCSI Test Unit Ready Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_TestUnitReady (void) { - - if (CBW.dDataLength != 0) { - if ((CBW.bmFlags & 0x80) != 0) { - MSC_SetStallEP(MSC_EP_IN); - } else { - MSC_SetStallEP(MSC_EP_OUT); - } - } - - CSW.bStatus = CSW_CMD_PASSED; - MSC_SetCSW(); -} - - -/* - * MSC SCSI Request Sense Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_RequestSense (void) { - - if (!DataInFormat()) return; - - BulkBuf[ 0] = 0x70; /* Response Code */ - BulkBuf[ 1] = 0x00; - BulkBuf[ 2] = 0x02; /* Sense Key */ - BulkBuf[ 3] = 0x00; - BulkBuf[ 4] = 0x00; - BulkBuf[ 5] = 0x00; - BulkBuf[ 6] = 0x00; - BulkBuf[ 7] = 0x0A; /* Additional Length */ - BulkBuf[ 8] = 0x00; - BulkBuf[ 9] = 0x00; - BulkBuf[10] = 0x00; - BulkBuf[11] = 0x00; - BulkBuf[12] = 0x30; /* ASC */ - BulkBuf[13] = 0x01; /* ASCQ */ - BulkBuf[14] = 0x00; - BulkBuf[15] = 0x00; - BulkBuf[16] = 0x00; - BulkBuf[17] = 0x00; - - BulkLen = 18; - DataInTransfer(); -} - - -/* - * MSC SCSI Inquiry Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_Inquiry (void) { - - if (!DataInFormat()) return; - - BulkBuf[ 0] = 0x00; /* Direct Access Device */ - BulkBuf[ 1] = 0x80; /* RMB = 1: Removable Medium */ - BulkBuf[ 2] = 0x00; /* Version: No conformance claim to standard */ - BulkBuf[ 3] = 0x01; - - BulkBuf[ 4] = 36-4; /* Additional Length */ - BulkBuf[ 5] = 0x80; /* SCCS = 1: Storage Controller Component */ - BulkBuf[ 6] = 0x00; - BulkBuf[ 7] = 0x00; - - BulkBuf[ 8] = 'K'; /* Vendor Identification */ - BulkBuf[ 9] = 'e'; - BulkBuf[10] = 'i'; - BulkBuf[11] = 'l'; - BulkBuf[12] = ' '; - BulkBuf[13] = ' '; - BulkBuf[14] = ' '; - BulkBuf[15] = ' '; - - BulkBuf[16] = 'L'; /* Product Identification */ - BulkBuf[17] = 'P'; - BulkBuf[18] = 'C'; - BulkBuf[19] = '1'; - BulkBuf[20] = '3'; - BulkBuf[21] = '4'; - BulkBuf[22] = 'x'; - BulkBuf[23] = ' '; - BulkBuf[24] = 'D'; - BulkBuf[25] = 'i'; - BulkBuf[26] = 's'; - BulkBuf[27] = 'k'; - BulkBuf[28] = ' '; - BulkBuf[29] = ' '; - BulkBuf[30] = ' '; - BulkBuf[31] = ' '; - - BulkBuf[32] = '1'; /* Product Revision Level */ - BulkBuf[33] = '.'; - BulkBuf[34] = '0'; - BulkBuf[35] = ' '; - - BulkLen = 36; - DataInTransfer(); -} - - -/* - * MSC SCSI Mode Sense (6-Byte) Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_ModeSense6 (void) { - - if (!DataInFormat()) return; - - BulkBuf[ 0] = 0x03; - BulkBuf[ 1] = 0x00; - BulkBuf[ 2] = 0x00; - BulkBuf[ 3] = 0x00; - - BulkLen = 4; - DataInTransfer(); -} - - -/* - * MSC SCSI Mode Sense (10-Byte) Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_ModeSense10 (void) { - - if (!DataInFormat()) return; - - BulkBuf[ 0] = 0x00; - BulkBuf[ 1] = 0x06; - BulkBuf[ 2] = 0x00; - BulkBuf[ 3] = 0x00; - BulkBuf[ 4] = 0x00; - BulkBuf[ 5] = 0x00; - BulkBuf[ 6] = 0x00; - BulkBuf[ 7] = 0x00; - - BulkLen = 8; - DataInTransfer(); -} - - -/* - * MSC SCSI Read Capacity Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_ReadCapacity (void) { - - if (!DataInFormat()) return; - - /* Last Logical Block */ - BulkBuf[ 0] = ((MSC_BlockCount - 1) >> 24) & 0xFF; - BulkBuf[ 1] = ((MSC_BlockCount - 1) >> 16) & 0xFF; - BulkBuf[ 2] = ((MSC_BlockCount - 1) >> 8) & 0xFF; - BulkBuf[ 3] = ((MSC_BlockCount - 1) >> 0) & 0xFF; - - /* Block Length */ - BulkBuf[ 4] = (MSC_BlockSize >> 24) & 0xFF; - BulkBuf[ 5] = (MSC_BlockSize >> 16) & 0xFF; - BulkBuf[ 6] = (MSC_BlockSize >> 8) & 0xFF; - BulkBuf[ 7] = (MSC_BlockSize >> 0) & 0xFF; - - BulkLen = 8; - DataInTransfer(); -} - - -/* - * MSC SCSI Read Format Capacity Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_ReadFormatCapacity (void) { - - if (!DataInFormat()) return; - - BulkBuf[ 0] = 0x00; - BulkBuf[ 1] = 0x00; - BulkBuf[ 2] = 0x00; - BulkBuf[ 3] = 0x08; /* Capacity List Length */ - - /* Block Count */ - BulkBuf[ 4] = (MSC_BlockCount >> 24) & 0xFF; - BulkBuf[ 5] = (MSC_BlockCount >> 16) & 0xFF; - BulkBuf[ 6] = (MSC_BlockCount >> 8) & 0xFF; - BulkBuf[ 7] = (MSC_BlockCount >> 0) & 0xFF; - - /* Block Length */ - BulkBuf[ 8] = 0x02; /* Descriptor Code: Formatted Media */ - BulkBuf[ 9] = (MSC_BlockSize >> 16) & 0xFF; - BulkBuf[10] = (MSC_BlockSize >> 8) & 0xFF; - BulkBuf[11] = (MSC_BlockSize >> 0) & 0xFF; - - BulkLen = 12; - DataInTransfer(); -} - - -/* - * MSC Get Command Block Wrapper Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_GetCBW (void) { - uint32_t n; - - for (n = 0; n < BulkLen; n++) { - *((uint8_t *)&CBW + n) = BulkBuf[n]; - } - if ((BulkLen == sizeof(CBW)) && (CBW.dSignature == MSC_CBW_Signature)) { - /* Valid CBW */ - CSW.dTag = CBW.dTag; - CSW.dDataResidue = CBW.dDataLength; - if ((CBW.bLUN != 0) || - (CBW.bCBLength < 1) || - (CBW.bCBLength > 16) ) { -fail: - CSW.bStatus = CSW_CMD_FAILED; - MSC_SetCSW(); - } else { - switch (CBW.CB[0]) { - case SCSI_TEST_UNIT_READY: - MSC_TestUnitReady(); - break; - case SCSI_REQUEST_SENSE: - MSC_RequestSense(); - break; - case SCSI_FORMAT_UNIT: - goto fail; - case SCSI_INQUIRY: - MSC_Inquiry(); - break; - case SCSI_START_STOP_UNIT: - goto fail; - case SCSI_MEDIA_REMOVAL: - goto fail; - case SCSI_MODE_SELECT6: - goto fail; - case SCSI_MODE_SENSE6: - MSC_ModeSense6(); - break; - case SCSI_MODE_SELECT10: - goto fail; - case SCSI_MODE_SENSE10: - MSC_ModeSense10(); - break; - case SCSI_READ_FORMAT_CAPACITIES: - MSC_ReadFormatCapacity(); - break; - case SCSI_READ_CAPACITY: - MSC_ReadCapacity(); - break; - case SCSI_READ10: - case SCSI_READ12: - if (MSC_RWSetup()) { - if ((CBW.bmFlags & 0x80) != 0) { - BulkStage = MSC_BS_DATA_IN; - MSC_MemoryRead(); - } else { /* direction mismatch */ - MSC_SetStallEP(MSC_EP_OUT); - CSW.bStatus = CSW_PHASE_ERROR; - MSC_SetCSW(); - } - } - break; - case SCSI_WRITE10: - case SCSI_WRITE12: - if (MSC_RWSetup()) { - if ((CBW.bmFlags & 0x80) == 0) { - BulkStage = MSC_BS_DATA_OUT; - } else { /* direction mismatch */ - MSC_SetStallEP(MSC_EP_IN); - CSW.bStatus = CSW_PHASE_ERROR; - MSC_SetCSW(); - } - } - break; - case SCSI_VERIFY10: - if ((CBW.CB[1] & 0x02) == 0) { - // BYTCHK = 0 -> CRC Check (not implemented) - CSW.bStatus = CSW_CMD_PASSED; - MSC_SetCSW(); - break; - } - if (MSC_RWSetup()) { - if ((CBW.bmFlags & 0x80) == 0) { - BulkStage = MSC_BS_DATA_OUT; - MemOK = TRUE; - } else { - MSC_SetStallEP(MSC_EP_IN); - CSW.bStatus = CSW_PHASE_ERROR; - MSC_SetCSW(); - } - } - break; - default: - goto fail; - } - } - } else { - /* Invalid CBW */ - MSC_SetStallEP(MSC_EP_IN); - /* set EP to stay stalled */ - USB_EndPointStall |= (MSC_EP_IN & 0x80) ? ((1 << 16) << (MSC_EP_IN & 0x0F)) : (1 << MSC_EP_IN); - MSC_SetStallEP(MSC_EP_OUT); - /* set EP to stay stalled */ - USB_EndPointStall |= (MSC_EP_OUT & 0x80) ? ((1 << 16) << (MSC_EP_OUT & 0x0F)) : (1 << MSC_EP_OUT); - BulkStage = MSC_BS_ERROR; - } -} - - -/* - * MSC Set Command Status Wrapper Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_SetCSW (void) { - - CSW.dSignature = MSC_CSW_Signature; - USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW)); - BulkStage = MSC_BS_CSW; -} - - -/* - * MSC Bulk In Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_BulkIn (void) { - - switch (BulkStage) { - case MSC_BS_DATA_IN: - switch (CBW.CB[0]) { - case SCSI_READ10: - case SCSI_READ12: - MSC_MemoryRead(); - break; - } - break; - case MSC_BS_DATA_IN_LAST: - MSC_SetCSW(); - break; - case MSC_BS_DATA_IN_LAST_STALL: - MSC_SetStallEP(MSC_EP_IN); - MSC_SetCSW(); - break; - case MSC_BS_CSW: - BulkStage = MSC_BS_CBW; - break; - } -} - - -/* - * MSC Bulk Out Callback - * Parameters: None (global variables) - * Return Value: None - */ - -void MSC_BulkOut (void) { - - BulkLen = USB_ReadEP(MSC_EP_OUT, BulkBuf); - switch (BulkStage) { - case MSC_BS_CBW: - MSC_GetCBW(); - break; - case MSC_BS_DATA_OUT: - switch (CBW.CB[0]) { - case SCSI_WRITE10: - case SCSI_WRITE12: - MSC_MemoryWrite(); - break; - case SCSI_VERIFY10: - MSC_MemoryVerify(); - break; - } - break; - default: - MSC_SetStallEP(MSC_EP_OUT); - CSW.bStatus = CSW_PHASE_ERROR; - MSC_SetCSW(); - break; - } -} diff --git a/firmware/src/mscuser.h b/firmware/src/mscuser.h deleted file mode 100644 index 1d9a1fa..0000000 --- a/firmware/src/mscuser.h +++ /dev/null @@ -1,47 +0,0 @@ -/*---------------------------------------------------------------------------- - * U S B - K e r n e l - *---------------------------------------------------------------------------- - * Name: mscuser.h - * Purpose: Mass Storage Class 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 microcontroller devices only. Nothing else - * gives you the right to use this software. - * - * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. - *---------------------------------------------------------------------------*/ - -#ifndef __MSCUSER_H__ -#define __MSCUSER_H__ - - -/* Mass Storage Memory Layout */ -#define MSC_MemorySize 6144 -#define MSC_BlockSize 512 -#define MSC_BlockCount (MSC_MemorySize / MSC_BlockSize) - - -/* Max In/Out Packet Size */ -#define MSC_MAX_PACKET 64 - -/* MSC In/Out Endpoint Address */ -#define MSC_EP_IN 0x82 -#define MSC_EP_OUT 0x02 - -/* MSC Requests Callback Functions */ -extern uint32_t MSC_Reset (void); -extern uint32_t MSC_GetMaxLUN (void); - -/* MSC Bulk Callback Functions */ -extern void MSC_GetCBW (void); -extern void MSC_SetCSW (void); -extern void MSC_BulkIn (void); -extern void MSC_BulkOut(void); - - -#endif /* __MSCUSER_H__ */ diff --git a/firmware/src/rom_drivers.h b/firmware/src/rom_drivers.h new file mode 100644 index 0000000..2dfbf63 --- /dev/null +++ b/firmware/src/rom_drivers.h @@ -0,0 +1,59 @@ +/***************************************************************************** + * rom_drivers.h: Header file for NXP LPC13xx Family Microprocessors + * + * Copyright(C) 2009, NXP Semiconductor + * All rights reserved. + * + * History + * 2009.09.17 ver 1.00 Preliminary version, first Release + * +******************************************************************************/ +#ifndef ROM_DRIVERS_H_ +#define ROM_DRIVERS_H_ + +#include "type.h" + +typedef struct _USB_DEVICE_INFO { + uint16_t DevType; + uint32_t DevDetailPtr; +} USB_DEV_INFO; + +typedef struct _USBD { + void (*init_clk_pins)(void); + void (*isr)(void); + void (*init)( USB_DEV_INFO * DevInfoPtr ); + void (*connect)(uint32_t con); +} USBD; + +#define init_msdstate() *((uint32_t *)(0x10000054)) = 0x0 + +typedef struct _ROM { + const USBD * pUSBD; +} ROM; + +typedef struct _MSC_DEVICE_INFO { + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint32_t StrDescPtr; + uint32_t MSCInquiryStr; + uint32_t BlockCount; + uint32_t BlockSize; + uint32_t MemorySize; + void (*MSC_Write)( uint32_t offset, uint8_t src[], uint32_t length); + void (*MSC_Read)( uint32_t offset, uint8_t dst[], uint32_t length); +} MSC_DEVICE_INFO; + +typedef struct _HID_DEVICE_INFO { + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint32_t StrDescPtr; + uint8_t InReportCount; + uint8_t OutReportCount; + uint8_t SampleInterval; + void (*InReport)( uint8_t src[], uint32_t length); + void (*OutReport)(uint8_t dst[], uint32_t length); +} HID_DEVICE_INFO; + +#endif /*ROM_DRIVERS_H_*/ diff --git a/firmware/src/serial.c b/firmware/src/serial.c deleted file mode 100644 index 7b27fd2..0000000 --- a/firmware/src/serial.c +++ /dev/null @@ -1,243 +0,0 @@ -/*---------------------------------------------------------------------------- - * Name: serial.c - * Purpose: serial port handling for LPC134x - * 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 "LPC13xx.h" // LPC13xx definitions -#include "type.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 (void) { - - NVIC_DisableIRQ(UART_IRQn); - - LPC_IOCON->PIO1_6 &= ~0x07; /* UART I/O config */ - LPC_IOCON->PIO1_6 |= 0x01; /* UART RXD */ - LPC_IOCON->PIO1_7 &= ~0x07; - LPC_IOCON->PIO1_7 |= 0x01; /* UART TXD */ - /* Enable UART clock */ - LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); - LPC_SYSCON->UARTCLKDIV = 0x1; /* divided by 1 */ - return; -} - -/*---------------------------------------------------------------------------- - close the serial port - *---------------------------------------------------------------------------*/ -void ser_ClosePort (void) { - LPC_IOCON->PIO1_6 &= ~0x07; /* UART I/O config */ - LPC_IOCON->PIO1_7 &= ~0x07; - - /* Disable the interrupt in the VIC and UART controllers */ - LPC_UART->IER = 0; - NVIC_DisableIRQ(UART_IRQn); - return; -} - -/*---------------------------------------------------------------------------- - initialize the serial port - *---------------------------------------------------------------------------*/ -void ser_InitPort (unsigned long baudrate, unsigned int databits, - unsigned int parity, unsigned int stopbits) { - - uint8_t lcr_p, lcr_s, lcr_d; - uint32_t dll; - uint32_t Fdiv; - - 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 - - /* Note that the pclk is 24,0 MHz. (48.0 MHz / 2) */ - /* 24 MHz PCLK generates also rates for 115200, 57600 baud */ - Fdiv = LPC_SYSCON->UARTCLKDIV; - dll = (((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/Fdiv)/16)/baudrate ; /*baud rate */ - LPC_UART->FDR = 0; // Fractional divider not used - LPC_UART->LCR = 0x80 | lcr_d | lcr_p | lcr_s; // Data bits, Parity, Stop bit - LPC_UART->DLL = dll; // Baud Rate depending on PCLK - LPC_UART->DLM = (dll >> 8); // High divisor latch - LPC_UART->LCR = 0x00 | lcr_d | lcr_p | lcr_s; // DLAB = 0 - LPC_UART->IER = 0x03; // Enable TX/RX interrupts - - LPC_UART->FCR = 0x07; /* Enable and reset TX and RX FIFO. */ - ser_txRestart = 1; // TX fifo is empty - - /* Enable the UART Interrupt */ - NVIC_EnableIRQ(UART_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 (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; - LPC_UART->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 1 interrupt - *---------------------------------------------------------------------------*/ -void UART_IRQHandler(void) -{ - volatile unsigned long iir; - - iir = LPC_UART->IIR; - - if ((iir & 0x4) || (iir & 0xC)) { // RDA or CTI pending - while (LPC_UART->LSR & 0x01) { // Rx FIFO is not empty - SER_BUF_WR(ser_in, LPC_UART->RBR); // Read Rx FIFO to buffer - } - } - if ((iir & 0x2)) { // TXMIS pending - if (SER_BUF_COUNT(ser_out) != 0) { - LPC_UART->THR = SER_BUF_RD(ser_out); // Write to the Tx FIFO - ser_txRestart = 0; - } - else { - ser_txRestart = 1; - } - } - ser_lineState = LPC_UART->LSR & 0x1E; // update linestate - return; -} - - diff --git a/firmware/src/serial.h b/firmware/src/serial.h deleted file mode 100644 index f8a6799..0000000 --- a/firmware/src/serial.h +++ /dev/null @@ -1,28 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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. - *---------------------------------------------------------------------------*/ - - -/*---------------------------------------------------------------------------- - Serial interface related prototypes - *---------------------------------------------------------------------------*/ -extern void ser_OpenPort (void); -extern void ser_ClosePort (void); -extern void ser_InitPort (unsigned long baudrate, unsigned int databits, unsigned int parity, unsigned int stopbits); -extern void ser_AvailChar (int *availChar); -extern int ser_Write (const char *buffer, int *length); -extern int ser_Read (char *buffer, const int *length); -extern void ser_LineState (unsigned short *lineState); - diff --git a/firmware/src/system_LPC13xx.c b/firmware/src/system_LPC13xx.c index fec287a..e41d5ae 100644 --- a/firmware/src/system_LPC13xx.c +++ b/firmware/src/system_LPC13xx.c @@ -3,7 +3,7 @@ * @brief CMSIS Cortex-M3 Device Peripheral Access Layer Source File * for the NXP LPC13xx Device Series * @version V1.02 - * @date 18. February 2010 + * @date 21. October 2009 * * @note * Copyright (C) 2009 ARM Limited. All rights reserved. @@ -22,10 +22,6 @@ * ******************************************************************************/ -// ******** Code Red ************** -// * Changed USBCLK_SETUP to 1 -// * Changed SYSPLLCTRL_Val to 0x25 -// ******************************** #include #include "LPC13xx.h" @@ -142,12 +138,8 @@ #define WDTOSCCTRL_Val 0x000000A0 #define SYSPLLCLKSEL_Val 0x00000001 #define SYSPLL_SETUP 1 -#define SYSPLLCTRL_Val 0x00000025 +#define SYSPLLCTRL_Val 0x00000005 #define MAINCLKSEL_Val 0x00000003 - -// ******** Code Red ********* -// * Changed USBCLK_SETUP to 1 -// *************************** #define USBCLK_SETUP 1 #define USBPLL_SETUP 1 #define USBPLLCLKSEL_Val 0x00000001 diff --git a/firmware/src/type.h b/firmware/src/type.h index a46427d..4fbf2f3 100644 --- a/firmware/src/type.h +++ b/firmware/src/type.h @@ -12,24 +12,23 @@ #ifndef __TYPE_H__ #define __TYPE_H__ -// CodeRed - ifdef for GNU added to avoid potential clash with stdint.h -#if defined ( __GNUC__ ) +#include "compiler.h" + +#if !defined(__KEIL__) #include #else - /* exact-width signed integer types */ typedef signed char int8_t; typedef signed short int int16_t; typedef signed int int32_t; typedef signed __int64 int64_t; - /* exact-width unsigned integer types */ + /* exact-width unsigned integer types */ typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned int uint32_t; typedef unsigned __int64 uint64_t; - -#endif // __GNUC__ +#endif #ifndef NULL #define NULL ((void *)0) diff --git a/firmware/src/usb.h b/firmware/src/usb.h index f7bea18..74217cc 100644 --- a/firmware/src/usb.h +++ b/firmware/src/usb.h @@ -19,14 +19,15 @@ #ifndef __USB_H__ #define __USB_H__ - -typedef union { +#include "compiler.h" + +typedef PACKED_PRE union { uint16_t W; - struct { + PACKED_PRE struct { uint8_t L; uint8_t H; - } __attribute__((packed)) WB; -} __attribute__((packed)) WORD_BYTE; + } PACKED_POST WB; +} PACKED_POST WORD_BYTE; /* bmRequestType.Dir */ @@ -46,14 +47,14 @@ typedef union { #define REQUEST_TO_OTHER 3 /* bmRequestType Definition */ -typedef union _REQUEST_TYPE { - struct _BM { +typedef PACKED_PRE union _REQUEST_TYPE { + PACKED_PRE struct _BM { uint8_t Recipient : 5; uint8_t Type : 2; uint8_t Dir : 1; - } __attribute__((packed)) BM; + } PACKED_POST BM; uint8_t B; -} __attribute__((packed)) REQUEST_TYPE; +} PACKED_POST REQUEST_TYPE; /* USB Standard Request Codes */ #define USB_REQUEST_GET_STATUS 0 @@ -78,13 +79,13 @@ typedef union _REQUEST_TYPE { #define USB_FEATURE_REMOTE_WAKEUP 1 /* USB Default Control Pipe Setup Packet */ -typedef struct _USB_SETUP_PACKET { +typedef PACKED_PRE struct _USB_SETUP_PACKET { REQUEST_TYPE bmRequestType; uint8_t bRequest; WORD_BYTE wValue; WORD_BYTE wIndex; uint16_t wLength; -} __attribute__((packed)) USB_SETUP_PACKET; +} PACKED_POST USB_SETUP_PACKET; /* USB Descriptor Types */ @@ -146,7 +147,7 @@ typedef struct _USB_SETUP_PACKET { #define USB_ENDPOINT_USAGE_RESERVED 0x30 /* USB Standard Device Descriptor */ -typedef struct _USB_DEVICE_DESCRIPTOR { +typedef PACKED_PRE struct _USB_DEVICE_DESCRIPTOR { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; @@ -161,10 +162,10 @@ typedef struct _USB_DEVICE_DESCRIPTOR { uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; -} __attribute__((packed)) USB_DEVICE_DESCRIPTOR; +} PACKED_POST USB_DEVICE_DESCRIPTOR; /* USB 2.0 Device Qualifier Descriptor */ -typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR { +typedef PACKED_PRE struct _USB_DEVICE_QUALIFIER_DESCRIPTOR { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; @@ -174,10 +175,10 @@ typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR { uint8_t bMaxPacketSize0; uint8_t bNumConfigurations; uint8_t bReserved; -} __attribute__((packed)) USB_DEVICE_QUALIFIER_DESCRIPTOR; +} PACKED_POST USB_DEVICE_QUALIFIER_DESCRIPTOR; /* USB Standard Configuration Descriptor */ -typedef struct _USB_CONFIGURATION_DESCRIPTOR { +typedef PACKED_PRE struct _USB_CONFIGURATION_DESCRIPTOR { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; @@ -186,10 +187,10 @@ typedef struct _USB_CONFIGURATION_DESCRIPTOR { uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; -} __attribute__((packed)) USB_CONFIGURATION_DESCRIPTOR; +} PACKED_POST USB_CONFIGURATION_DESCRIPTOR; -/* USB Standard Interface Association Descriptor */ -typedef struct _USB_INTERFACE_DESCRIPTOR { +/* USB Standard Interface Descriptor */ +typedef PACKED_PRE struct _USB_INTERFACE_DESCRIPTOR { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; @@ -199,42 +200,30 @@ typedef struct _USB_INTERFACE_DESCRIPTOR { uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; -} __attribute__((packed)) USB_INTERFACE_DESCRIPTOR; - -/* USB Interface Descriptor */ -typedef struct _USB_INTERFACE_ASSOCIATION_DESCRIPTOR { - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bFirstInterface; - uint8_t bInterfaceCount; - uint8_t bFunctionClass; - uint8_t bFunctionSubClass; - uint8_t bFunctionProtocol; - uint8_t iFunction; -} __attribute__((packed)) USB_INTERFACE_ASSOCIATION_DESCRIPTOR; +} PACKED_POST USB_INTERFACE_DESCRIPTOR; /* USB Standard Endpoint Descriptor */ -typedef struct _USB_ENDPOINT_DESCRIPTOR { +typedef PACKED_PRE struct _USB_ENDPOINT_DESCRIPTOR { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; -} __attribute__((packed)) USB_ENDPOINT_DESCRIPTOR; +} PACKED_POST USB_ENDPOINT_DESCRIPTOR; /* USB String Descriptor */ -typedef struct _USB_STRING_DESCRIPTOR { +typedef PACKED_PRE struct _USB_STRING_DESCRIPTOR { uint8_t bLength; uint8_t bDescriptorType; uint16_t bString/*[]*/; -} __attribute__((packed)) USB_STRING_DESCRIPTOR; +} PACKED_POST USB_STRING_DESCRIPTOR; /* USB Common Descriptor */ -typedef struct _USB_COMMON_DESCRIPTOR { +typedef PACKED_PRE struct _USB_COMMON_DESCRIPTOR { uint8_t bLength; uint8_t bDescriptorType; -} __attribute__((packed)) USB_COMMON_DESCRIPTOR; +} PACKED_POST USB_COMMON_DESCRIPTOR; #endif /* __USB_H__ */ diff --git a/firmware/src/usbcfg.h b/firmware/src/usbcfg.h deleted file mode 100644 index 572b16a..0000000 --- a/firmware/src/usbcfg.h +++ /dev/null @@ -1,157 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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 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 -// -// -*/ - -#define USB_POWER 0 -#define USB_IF_NUM 1 -#define USB_LOGIC_EP_NUM 5 -#define USB_EP_NUM 10 -#define USB_MAX_PACKET0 64 - -/* -// 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 1 -#define USB_RESUME_EVENT 1 -#define USB_WAKEUP_EVENT 0 -#define USB_SOF_EVENT 0 -#define USB_ERROR_EVENT 0 -#define USB_EP_EVENT 0x000f -#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 1 -#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 1 -#define USB_CDC_DIF_NUM 2 -#define USB_CDC_BUFSIZE 64 - -/* -// USB Vendor Support -// enables USB Vendor specific Requests -// -*/ -#define USB_VENDOR 0 - - -#endif /* __USBCFG_H__ */ diff --git a/firmware/src/usbcomp.c b/firmware/src/usbcomp.c deleted file mode 100644 index 354472b..0000000 --- a/firmware/src/usbcomp.c +++ /dev/null @@ -1,153 +0,0 @@ -/*---------------------------------------------------------------------------- - * Name: vcomdemo.c - * Purpose: USB virtual COM port Demo - * 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. - *---------------------------------------------------------------------------*/ - -#include "LPC13xx.h" -#include "type.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" -#include "mscuser.h" -#include "memory.h" -#include "stdio.h" -#include "string.h" - - -#include "edubrm.h" - -extern uint8_t Memory[MSC_MemorySize]; /* MSC Memory in RAM */ - -#define EN_TIMER32_1 (1<<10) -#define EN_IOCON (1<<16) -#define EN_USBREG (1<<14) - -/*---------------------------------------------------------------------------- - Initializes the VCOM port. - Call this function before using VCOM_putchar or VCOM_getchar - *---------------------------------------------------------------------------*/ -void VCOM_Init(void) { - - CDC_Init (); -} - - -/*---------------------------------------------------------------------------- - 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); - ser_Write (&serBuf[0], &numBytesRead); - } - -} - -/*---------------------------------------------------------------------------- - Reads character from USB buffer and writes to serial port buffer - *---------------------------------------------------------------------------*/ -void VCOM_Usb2SerialTest(void) { - static char serBuf [32]; - int numBytesRead; - - strcpy(serBuf,"Test\n"); - numBytesRead = strlen(serBuf); - ser_Write (&serBuf[0], &numBytesRead); - -} - - -/*---------------------------------------------------------------------------- - 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) { - uint32_t n; - - for (n = 0; n < MSC_ImageSize; n++) { /* Copy Initial Disk Image */ - Memory[n] = DiskImage[n]; /* from Flash to RAM */ - } - - /* Basic chip initialization is taken care of in SystemInit() called - * from the startup code. SystemInit() and chip settings are defined - * in the CMSIS system_.c file. - */ - - /* Enable Timer32_1, IOCON, and USB blocks */ - LPC_SYSCON->SYSAHBCLKCTRL |= (EN_TIMER32_1 | EN_IOCON | EN_USBREG); - - USBIOClkConfig(); - - VCOM_Init(); // VCOM Initialization - - USB_Init(); // USB Initialization - USB_Connect(TRUE); // USB Connect - - while (!USB_Configuration) ; // wait until USB is configured - - //VCOM_Usb2SerialTest(); - - enableLED(); - - while (1) { // Loop forever - VCOM_Brm2Usb(); - VCOM_CheckSerialState(); - VCOM_Usb2Brm(); - } // end while -} // end main () - - diff --git a/firmware/src/usbcore.c b/firmware/src/usbcore.c deleted file mode 100644 index de97d0f..0000000 --- a/firmware/src/usbcore.c +++ /dev/null @@ -1,1085 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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 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 "type.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 - -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 - */ - -__inline uint32_t USB_ReqGetStatus (void) { - 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)) { - *((uint16_t __attribute__((packed)) *)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)) { - *((uint16_t __attribute__((packed)) *)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 - */ - -__inline uint32_t USB_ReqSetClrFeature (uint32_t sc) { - 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 - */ - -__inline uint32_t USB_ReqSetAddress (void) { - - 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 - */ - -__inline uint32_t USB_ReqGetDescriptor (void) { - 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 - */ - -__inline uint32_t USB_ReqGetConfiguration (void) { - - switch (SetupPacket.bmRequestType.BM.Recipient) { - case REQUEST_TO_DEVICE: - EP0Data.pData = &USB_Configuration; - break; - default: - return (FALSE); - } - return (TRUE); -} - - -/* - * Add a number of bytes to a pointer's address - * Harder than you might think. Some compilers say: - * Expected an lvalue -- Assignment expects its first operand to be - * an lvalue. Please note that a cast removes the lvaluedness of an - * expression. - * - * vpptr = void pointer to pointer - * n = number of bytes to add to pointer - * Call looks like: AddPtr((void **)&myPointer, 8); - */ - -__inline void UsbAddPtr(void **vpptr, uint32_t n) -{ - /* Declare a pointer to a pointer to a byte. Only a byte pointer - * can be incremented by a number of bytes. Other pointers will - * increment by a multiple of what they point to. - */ - uint8_t **bpptr; - - /* Convert our void pointer to a pointer to a byte pointer to a pointer */ - bpptr = (uint8_t **)vpptr; - - /* Add 'n' bytes to our pointer value */ - (*bpptr) += n; -} -/* - * Set Configuration USB Request - * Parameters: None (global SetupPacket) - * Return Value: TRUE - Success, FALSE - Error - */ - -__inline uint32_t USB_ReqSetConfiguration (void) { - USB_COMMON_DESCRIPTOR *pD; - uint32_t alt = 0; - uint32_t n, m; - - 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 { - UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength); - 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; - } - UsbAddPtr((void **)&pD, pD->bLength); - } - } - 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 - */ - -__inline uint32_t USB_ReqGetInterface (void) { - - 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 - */ - -__inline uint32_t USB_ReqSetInterface (void) { - USB_COMMON_DESCRIPTOR *pD; - uint32_t ifn = 0, alt = 0, old = 0, msk = 0; - uint32_t n, m; - uint32_t set; - - 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) { - UsbAddPtr((void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength); - 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; - } - UsbAddPtr((void **)&pD, pD->bLength); - } - 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/firmware/src/usbcore.h b/firmware/src/usbcore.h deleted file mode 100644 index 13ac422..0000000 --- a/firmware/src/usbcore.h +++ /dev/null @@ -1,55 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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); - -/* Newer C compilers make it really difficult to add - * an integer to a pointer */ -__inline void UsbAddPtr(void **vpptr, uint32_t n); - - -#endif /* __USBCORE_H__ */ diff --git a/firmware/src/usbdesc.c b/firmware/src/usbdesc.c index 40739b2..84b986c 100644 --- a/firmware/src/usbdesc.c +++ b/firmware/src/usbdesc.c @@ -1,8 +1,8 @@ /*---------------------------------------------------------------------------- * U S B - K e r n e l *---------------------------------------------------------------------------- - * Name: usbdesc.c - * Purpose: USB Descriptors + * Name: usbdesc.h + * Purpose: USB Descriptors Definitions * Version: V1.20 *---------------------------------------------------------------------------- * This software is supplied "AS IS" without any warranties, express, @@ -14,266 +14,79 @@ * 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 "type.h" +#include "LPC13xx.h" #include "usb.h" -#include "cdc.h" -#include "usbcfg.h" #include "usbdesc.h" - -#include "msc.h" #include "config.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_MISCELLANEOUS, /* bDeviceClass */ - 0x02, /* bDeviceSubClass */ - 0x01, /* bDeviceProtocol */ - USB_MAX_PACKET0, /* bMaxPacketSize0 */ - -// 0x70, 0x08, /* idVendorL */ //this vendor code is here in order to bypase linux probing for ttyACM0 -// 0x01, 0x00, /* idProductL */ //this product code is here in order to bypase linux probing for ttyACM0 - - WBVAL(USB_VENDOR_ID), /* idVendor */ - WBVAL(USB_PROD_ID), /* idProduct */ - WBVAL(USB_DEVICE), /* 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 + /* mass storage interface */ - 2*USB_ENDPOINT_DESC_SIZE + /* bulk endpoints */ - 1*USB_INTERFACE_ASSOCIATION_DESC_SIZE + /* interface association */ - 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 */ - 0 - ), - - 0x03, /* 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, MSC Class */ - USB_INTERFACE_DESC_SIZE, /* bLength */ - USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */ - USB_MSC_IF_NUM, /* bInterfaceNumber */ - 0x00, /* bAlternateSetting */ - 0x02, /* bNumEndpoints */ - USB_DEVICE_CLASS_STORAGE, /* bInterfaceClass */ - MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ - MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ - 0x04, /* iInterface */ - - /* 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(0x0040), /* wMaxPacketSize */ - 0x00, /* bInterval: ignore for Bulk transfer */ - - /* 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(0x0040), /* wMaxPacketSize */ - 0x00, /* bInterval: ignore for Bulk transfer */ - - /* IAD to associate the two CDC interfaces */ - USB_INTERFACE_ASSOCIATION_DESC_SIZE, /* bLength */ - USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE, /* bDescriptorType */ - USB_CDC_CIF_NUM, /* bFirstInterface */ - 2, /* bInterfaceCount */ - CDC_COMMUNICATION_INTERFACE_CLASS, /* bFunctionClass */ - CDC_ABSTRACT_CONTROL_MODEL, /* bFunctionSubClass */ - 0, /* bFunctionProtocol */ - 0x06, /* iFunction (Index of string descriptor describing this function) */ - - /* 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 */ - 0x05, /* 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 */ - USB_CDC_DIF_NUM, /* 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 */ - 0x05, /* iInterface: */ - /* Endpoint, EP3 Bulk Out */ - USB_ENDPOINT_DESC_SIZE, /* bLength */ - USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */ - USB_ENDPOINT_OUT(3), /* bEndpointAddress */ - USB_ENDPOINT_TYPE_BULK, /* bmAttributes */ - WBVAL(USB_CDC_BUFSIZE), /* wMaxPacketSize */ - 0x00, /* bInterval: ignore for Bulk transfer */ - /* Endpoint, EP3 Bulk In */ - USB_ENDPOINT_DESC_SIZE, /* bLength */ - USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */ - USB_ENDPOINT_IN(3), /* 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 */ - (21*2 + 2), /* bLength ( 21 Char + Type + lenght) */ - USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ - 'N',0, - 'X',0, - 'P',0, - ' ',0, - 'L',0, - 'P',0, - 'C',0, - '1',0, - '3',0, - 'x',0, - 'x',0, - ' ',0, - 'M',0, - 'S',0, - 'D',0, - '/',0, - 'V',0, - 'C',0, - 'O',0, - 'M',0, - ' ',0, - /* Index 0x03: Serial Number */ - (16*2 + 2), /* bLength (12 Char + Type + lenght) */ - USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ - 'C',0, - 'O',0, - 'M',0, - 'P',0, - 'O',0, - 'S',0, - 'I',0, - 'T',0, - 'E',0, - ' ',0, - 'D',0, - 'E',0, - 'M',0, - 'O',0, - ' ',0, - ' ',0, - /* Index 0x04: Interface 0, Alternate Setting 0 */ - ( 6*2 + 2), /* bLength (6 Char + Type + lenght) */ - USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ - 'M',0, - 'e',0, - 'm',0, - 'o',0, - 'r',0, - 'y',0, - /* Index 0x05: 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, - /* Index 0x05: Interface 0, Alternate Setting 0 */ - ( 8*2 + 2), /* bLength (4 Char + Type + lenght) */ - USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ - 'C',0, - 'O',0, - 'M',0, - '/',0, - 'D',0, - 'A',0, - 'T',0, - 'A',0, + /* Index 0x00: LANGID Codes */ + 0x04, /* bLength */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + WBVAL(NXP_VID), /* US English */ /* wLANGID */ + /* Index 0x04: Manufacturer */ + 0x1C, /* bLength */ + 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 0x20: Product */ + 0x28, /* bLength */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + 'N',0, + 'X',0, + 'P',0, + ' ',0, + 'L',0, + 'P',0, + 'C',0, + '1',0, + '3',0, + 'X',0, + 'X',0, + ' ',0, + 'H',0, + 'I',0, + 'D',0, + ' ',0, + ' ',0, + ' ',0, + ' ',0, + /* Index 0x48: Serial Number */ + 0x1A, /* bLength */ + 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 0x62: Interface 0, Alternate Setting 0 */ + 0x0E, /* bLength */ + USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */ + 'H',0, + 'I',0, + 'D',0, + ' ',0, + ' ',0, + ' ',0, }; diff --git a/firmware/src/usbdesc.h b/firmware/src/usbdesc.h index 5f65692..ea4ff13 100644 --- a/firmware/src/usbdesc.h +++ b/firmware/src/usbdesc.h @@ -22,15 +22,21 @@ #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_INTERFACE_ASSOCIATION_DESC_SIZE (sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR)) -#define USB_ENDPOINT_DESC_SIZE (sizeof(USB_ENDPOINT_DESCRIPTOR)) +#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)) + +#define HID_DESC_OFFSET 0x0012 +#define HID_DESC_SIZE (sizeof(HID_DESCRIPTOR)) +#define HID_REPORT_DESC_SIZE (sizeof(HID_ReportDescriptor)) extern const uint8_t USB_DeviceDescriptor[]; extern const uint8_t USB_ConfigDescriptor[]; extern const uint8_t USB_StringDescriptor[]; +extern const uint8_t HID_ReportDescriptor[]; +extern const uint16_t HID_ReportDescSize; + #endif /* __USBDESC_H__ */ diff --git a/firmware/src/usbhidrom_main.c b/firmware/src/usbhidrom_main.c new file mode 100644 index 0000000..1345bd8 --- /dev/null +++ b/firmware/src/usbhidrom_main.c @@ -0,0 +1,99 @@ +/*---------------------------------------------------------------------------- + * Name: DEMO.C + * Purpose: USB HID 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 "LPC13xx.h" /* LPC13xx definitions */ + +#include "usb.h" +#include "usbdesc.h" +#include "gpio.h" +#include "rom_drivers.h" +#include "config.h" + +#define EN_TIMER32_1 (1<<10) +#define EN_IOCON (1<<16) +#define EN_USBREG (1<<14) + +USB_DEV_INFO DeviceInfo; +HID_DEVICE_INFO HidDevInfo; +ROM ** rom = (ROM **)0x1fff1ff8; + +/* + * Get HID Input Report -> InReport + */ + +volatile uint8_t buffer[8]; + +void GetInReport (uint8_t src[], uint32_t length) +{ + src[0] = buffer[0]; +} + +/* + * Set HID Output Report <- OutReport + */ +void SetOutReport (uint8_t dst[], uint32_t length) +{ + buffer[0] = dst[0]; +} + +int main (void) +{ + /* for delay loop */ + volatile int n; +// Code Red Red Suite and LPCXpresso by Code Red both call SystemInit() in +// the C startup code +#ifndef __CODERED__ + SystemInit(); +#endif + + + HidDevInfo.idVendor = USB_VENDOR_ID; + HidDevInfo.idProduct = USB_PROD_ID; + HidDevInfo.bcdDevice = USB_DEVICE; + HidDevInfo.StrDescPtr = (uint32_t)&USB_StringDescriptor[0]; + HidDevInfo.InReportCount = 1; + HidDevInfo.OutReportCount = 1; + HidDevInfo.SampleInterval = 0x20; + HidDevInfo.InReport = GetInReport; + HidDevInfo.OutReport = SetOutReport; + + DeviceInfo.DevType = USB_DEVICE_CLASS_HUMAN_INTERFACE; + DeviceInfo.DevDetailPtr = (uint32_t)&HidDevInfo; + + /* Enable Timer32_1, IOCON, and USB blocks (for USB ROM driver) */ + LPC_SYSCON->SYSAHBCLKCTRL |= (EN_TIMER32_1 | EN_IOCON | EN_USBREG); + + /* Use pll and pin init function in rom */ + (*rom)->pUSBD->init_clk_pins(); + + /* insert a delay between clk init and usb init */ + for (n = 0; n < 75; n++) {} + + (*rom)->pUSBD->init(&DeviceInfo); /* USB Initialization */ + (*rom)->pUSBD->connect(TRUE); /* USB Connect */ + + while (1) + __WFI(); +} + +#if defined(__IAR__) +void USBIRQ_IRQHandler() +#else +void USB_IRQHandler() +#endif +{ + (*rom)->pUSBD->isr(); +} diff --git a/firmware/src/usbhw.c b/firmware/src/usbhw.c deleted file mode 100644 index 2874b7a..0000000 --- a/firmware/src/usbhw.c +++ /dev/null @@ -1,552 +0,0 @@ -/*---------------------------------------------------------------------------- - * U S B - K e r n e l - *---------------------------------------------------------------------------- - * Name: usbhw.c - * Purpose: USB Hardware Layer Module for Philips 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. - *---------------------------------------------------------------------------- - * History: - * V1.20 Added USB_ClearEPBuf - * V1.00 Initial Version - *----------------------------------------------------------------------------*/ -#include "LPC13xx.h" /* LPC13xx definitions */ -#include "usb.h" -#include "usbcfg.h" -#include "usbreg.h" -#include "usbhw.h" -#include "usbcore.h" -#include "usbuser.h" - - -/* - * USB and IO Clock configuration only. - * The same as call PeriClkIOInit(IOCON_USB); - * The purpose is to reduce the code space for - * overall USB project and reserve code space for - * USB debugging. - * Parameters: None - * Return Value: None - */ -void USBIOClkConfig( void ) -{ - /* Enable AHB clock to the GPIO domain. */ - LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6); - - LPC_IOCON->PIO0_1 &= ~0x07; - LPC_IOCON->PIO0_1 |= 0x01; /* CLK OUT */ - - /* Enable AHB clock to the USB block. */ - LPC_SYSCON->SYSAHBCLKCTRL |= (1<<14); - LPC_IOCON->PIO0_3 &= ~0x1F; - LPC_IOCON->PIO0_3 |= 0x01; /* Secondary function VBUS */ - LPC_IOCON->PIO0_6 &= ~0x07; - LPC_IOCON->PIO0_6 |= 0x01; /* Secondary function SoftConn */ - return; -} - -/* - * Delay number of clock cycles - * Parameters: Delay length - * Return Value: None - */ - -void delay (uint32_t length ) { - uint32_t i; - - for ( i = 0; i < length; i++ ); - return; -} - -/* - * 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) { - - LPC_USB->DevIntClr = CCEMTY_INT; - LPC_USB->CmdCode = cmd; - while ((LPC_USB->DevIntSt & (CCEMTY_INT | DEV_STAT_INT)) == 0); -} - - -/* - * Write Command Data - * Parameters: cmd: Command - * val: Data - * Return Value: None - */ - -void WrCmdDat (uint32_t cmd, uint32_t val) { - - WrCmd(cmd); - WrCmd(val); -} - - -/* - * Write Command to Endpoint - * Parameters: cmd: Command - * val: Data - * Return Value: None - */ - -void WrCmdEP (uint32_t EPNum, uint32_t cmd){ - - WrCmd(CMD_SEL_EP(EPAdr(EPNum))); - WrCmd(cmd); -} - - -/* - * Read Command Data - * Parameters: cmd: Command - * Return Value: Data Value - */ - -uint32_t RdCmdDat (uint32_t cmd) { - - LPC_USB->DevIntClr = CCEMTY_INT | CDFULL_INT; - LPC_USB->CmdCode = cmd; - while ((LPC_USB->DevIntSt & (CDFULL_INT | DEV_STAT_INT)) == 0); - return (LPC_USB->CmdData); -} - - -/* - * USB Initialize Function - * Called by the User to initialize USB - * Return Value: None - */ - -void USB_Init (void) { - -#if USB_FIQ_EVENT - /* It's important that only BULK and FRAME(ISO) can be routed - to FIQ. */ - LPC_USB->DevFIQSel = 0x01; /* SOF Use FIQ */ - - /* Enable the USB Interrupt */ - NVIC_EnableIRQ(USB_FIQn); -#endif - - /* Enable the USB Interrupt */ - NVIC_EnableIRQ(USB_IRQn); - - USB_Reset(); - USB_SetAddress(0); - return; -} - - -/* - * 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) { - - LPC_USB->DevIntClr = 0x000FFFFF; - /* Enable all eight(8) EPs, note: EP won't be ready until it's - configured/enabled when device sending SetEPStatus command - to the command engine. */ - LPC_USB->DevIntEn = DEV_STAT_INT | (0xFF<<1) | - (USB_SOF_EVENT ? FRAME_INT : 0); - return; -} - - -/* - * 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) { - cfg = 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)); - return; -} - - -/* - * Configure USB Endpoint according to Descriptor - * Parameters: pEPD: Pointer to Endpoint Descriptor - * Return Value: None - */ - -void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) { - return; -} - - -/* - * Set Direction for USB Control Endpoint - * Parameters: dir: Out (dir == 0), In (dir <> 0) - * Return Value: None - */ - -void USB_DirCtrlEP (uint32_t dir) { - dir = 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; - - LPC_USB->Ctrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN; - /* 3 clock cycles to fetch the packet length from RAM. */ - delay( 5 ); - - do { - cnt = LPC_USB->RxPLen; - } while ((cnt & PKT_DV) == 0); - cnt &= PKT_LNGTH_MASK; - - for (n = 0; n < (cnt + 3) / 4; n++) { - *((uint32_t __attribute__((packed)) *)pData) = LPC_USB->RxData; - pData += 4; - } - - LPC_USB->Ctrl = 0; - - if ((EPNum & 0x80) != 0x04) { /* 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; - - LPC_USB->Ctrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN; - /* 3 clock cycles to fetch the packet length from RAM. */ - delay( 5 ); - LPC_USB->TxPLen = cnt; - - for (n = 0; n < (cnt + 3) / 4; n++) { - LPC_USB->TxData = *((uint32_t __attribute__((packed)) *)pData); - pData += 4; - } - - LPC_USB->Ctrl = 0; - - WrCmdEP(EPNum, CMD_VALID_BUF); - - return (cnt); -} - -/* - * 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; - - disr = LPC_USB->DevIntSt; /* Device Interrupt Status */ - LPC_USB->DevIntClr = disr; - - /* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */ - if (disr & 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) { - LPC_USB->DevIntClr = FRAME_INT; - USB_SOF_Event(); - SOFIRQCount++; - } -#endif - -#if USB_ERROR_EVENT - /* NO error interrupt anymore, below code can be used - as example to get error status from command engine. */ - /* Error Interrupt */ - if (disr & ERR_INT) { - WrCmd(CMD_RD_ERR_STAT); - val = RdCmdDat(DAT_RD_ERR_STAT); - USB_Error_Event(val); - } -#endif - - /* Endpoint's Interrupt */ - if (disr & (0xFF<<1)) { - /* if any of the EP0 through EP7 is set, or bit 1 through 9 on disr */ - for (n = 0; n < USB_EP_NUM; n++) { /* Check All Endpoints */ - /* skip frame interrupt at bit 0 in disr */ -// if (disr & ((1 << n)<<1)) { - if ((disr>>1) & (1 << n)) { - m = n >> 1; - /* clear EP interrupt by sending cmd to the command engine. */ - WrCmd(CMD_SEL_EP_CLRI(n)); - val = RdCmdDat(DAT_SEL_EP_CLRI(n)); - 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); - } - } - } - } - } -isr_end: - return; -} diff --git a/firmware/src/usbhw.h b/firmware/src/usbhw.h deleted file mode 100644 index f31d7d3..0000000 --- a/firmware/src/usbhw.h +++ /dev/null @@ -1,62 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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 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__ - - -/* 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 Hardware Functions */ -extern void USBIOClkConfig (void); -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_GetFrame(void); -extern void USB_IRQHandler (void); - - -#endif /* __USBHW_H__ */ diff --git a/firmware/src/usbreg.h b/firmware/src/usbreg.h deleted file mode 100644 index d059e6a..0000000 --- a/firmware/src/usbreg.h +++ /dev/null @@ -1,134 +0,0 @@ -/*---------------------------------------------------------------------------- - * U S B - K e r n e l - *---------------------------------------------------------------------------- - * Name: USBREG.H - * Purpose: USB Hardware Layer Definitions for NXP LPC13xx - * 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 __USBREG_H -#define __USBREG_H - -/* Device Interrupt Bit Definitions */ -#define FRAME_INT (0x1<<0) -#define EP0_INT (0x1<<1) -#define EP1_INT (0x1<<2) -#define EP2_INT (0x1<<3) -#define EP3_INT (0x1<<4) -#define EP4_INT (0x1<<5) -#define EP5_INT (0x1<<6) -#define EP6_INT (0x1<<7) -#define EP7_INT (0x1<<8) -#define DEV_STAT_INT (0x1<<9) -#define CCEMTY_INT (0x1<<10) -#define CDFULL_INT (0x1<<11) -#define RxENDPKT_INT (0x1<<12) -#define TxENDPKT_INT (0x1<<13) - -/* 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_INT 0x00F40500 -#define DAT_RD_INT 0x00F40200 -#define CMD_RD_FRAME 0x00F50500 -#define DAT_RD_FRAME 0x00F50200 -#define CMD_RD_CHIP_ID 0x00FD0500 -#define DAT_RD_CHIP_ID 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 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 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_AI 0x08 -#define INAK_AO 0x10 - -/* 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_NOERROR 0x00 -#define ERR_PID_ENCODE 0x01 -#define ERR_UNKNOWN_PID 0x02 -#define ERR_UNEXPECT_PKT 0x03 -#define ERR_TCRC 0x04 -#define ERR_DCRC 0x05 -#define ERR_TIMEOUT 0x06 -#define ERR_BABBIE 0x07 -#define ERR_EOF_PKT 0x08 -#define ERR_TX_RX_NAK 0x09 -#define ERR_SENT_STALL 0x0A -#define ERR_BUF_OVERRUN 0x0B -#define ERR_SENT_EPT_PKT 0x0C -#define ERR_BIT_STUFF 0x0D -#define ERR_SYNC 0x0E -#define ERR_TOGGLE_BIT 0x0F - -/* 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 - -#endif /* __USBREG_H */ diff --git a/firmware/src/usbuser.c b/firmware/src/usbuser.c deleted file mode 100644 index c13556d..0000000 --- a/firmware/src/usbuser.c +++ /dev/null @@ -1,217 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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 microcontroller devices only. Nothing else - * gives you the right to use this software. - * - * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. - *---------------------------------------------------------------------------*/ -#include "type.h" - -#include "usb.h" -#include "usbcfg.h" -#include "usbhw.h" -#include "usbcore.h" -#include "usbuser.h" -#include "cdcuser.h" -#include "mscuser.h" -#include "memory.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[USB_LOGIC_EP_NUM]) (uint32_t event) = { - P_EP(0), - P_EP(1), - P_EP(2), - P_EP(3), - P_EP(4), -}; - - -/* - * 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: - MSC_BulkOut(); - break; - case USB_EVT_IN: - MSC_BulkIn(); - break; - } -} - - -/* - * USB Endpoint 3 Event Callback - * Called automatically on USB Endpoint 3 Event - * Parameter: event - */ - -void USB_EndPoint3 (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; - } -} - - diff --git a/firmware/src/usbuser.h b/firmware/src/usbuser.h deleted file mode 100644 index e4109ec..0000000 --- a/firmware/src/usbuser.h +++ /dev/null @@ -1,57 +0,0 @@ -/*---------------------------------------------------------------------------- - * U S B - K e r n e l - *---------------------------------------------------------------------------- - * Name: USBUSER.H - * Purpose: USB Custom User 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) 2005-2009 Keil Software. - *---------------------------------------------------------------------------*/ - -#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 */ - -/* USB Endpoint Events Callback Pointers */ -extern void (* const USB_P_EP[USB_LOGIC_EP_NUM])(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); - -/* 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/firmware/src/vcomdemo.h b/firmware/src/vcomdemo.h deleted file mode 100644 index 63b3b61..0000000 --- a/firmware/src/vcomdemo.h +++ /dev/null @@ -1,31 +0,0 @@ -/*---------------------------------------------------------------------------- - * 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 */ - diff --git a/firmware/test.c b/firmware/test.c new file mode 100644 index 0000000..1983d6f --- /dev/null +++ b/firmware/test.c @@ -0,0 +1,194 @@ +/* + * generic_hid.c + * + * Created on: Jan 30, 2011 + * Author: Jan Axelson + * + * Demonstrates communicating with generic HID-class USB devices + * using libusb-1.0 (libusb.org). + * Sends and receives 2-byte reports. + * Requires: an attached HID-class device that supports 2-byte + * Input, Output, and Feature reports. + * The device firmware should respond to a received report by sending a report. + * Change VENDOR_ID and PRODUCT_ID to match your device's Vendor ID and Product ID. + * See Lvr.com/hidpage.htm for example device firmware. + * This firmware is adapted from code provided by Xiaofan. + * Note: libusb error codes are negative numbers. + * Compile with the -lusb option. + */ + +#include +#include +#include +#include +#include + +// Change these as needed to match idVendor and idProduct in your device's device descriptor. + +static const int VENDOR_ID = 0x1fc9; +static const int PRODUCT_ID = 0x0003; + +// Values for bmRequestType in the Setup transaction's Data packet. + +static const int CONTROL_REQUEST_TYPE_IN = LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE; +static const int CONTROL_REQUEST_TYPE_OUT = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE; + +static const int CONTROL_ENDPOINT_PACKET_SIZE = 16; +static const int INTERRUPT_ENDPOINT_PACKET_SIZE = 16; +static const int INTERFACE_NUMBER = 0; + +// Uses interrupt endpoint 1 IN and OUT: + +static const int INTERRUPT_IN_ENDPOINT = 0x81; +static const int INTERRUPT_OUT_ENDPOINT = 0x01; +static const int TIMEOUT_MS = 5000; + +// From the HID spec: + +static const int HID_GET_REPORT = 0x01; +static const int HID_SET_REPORT = 0x01; +static const int HID_REPORT_TYPE_INPUT = 0x01; +static const int HID_REPORT_TYPE_OUTPUT = 0x02; +static const int HID_REPORT_TYPE_FEATURE = 0x03; + +int exchange_input_and_output_reports_via_interrupt_transfers(libusb_device_handle *devh); + +int main(void) +{ + struct libusb_device_handle *devh = NULL; + int hid_ready = 0; + int result = 1; + + result = libusb_init(NULL); + if (result >= 0) + { + devh = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID); + + if (devh != NULL) + { + // The HID has been detected. + // Detach the hidusb driver from the HID to enable using libusb. + + libusb_detach_kernel_driver(devh, 0); + result = libusb_set_configuration(devh, 1); + if (result >= 0) + { + result = libusb_claim_interface(devh, 0); + if (result >= 0) + { + hid_ready = 1; + } + else + { + fprintf(stderr, "libusb_claim_interface error %d\n", result); + } + } + else + { + fprintf(stderr, "libusb_set_configuration error %d\n", result); + } + } + else + { + fprintf(stderr, "Unable to find the HID.\n"); + } + } + else + { + fprintf(stderr, "Unable to initialize libusb.\n"); + } + + if (hid_ready) + { + // Send and receive reports. + + exchange_input_and_output_reports_via_interrupt_transfers(devh); + + // Finished using the HID. + + libusb_release_interface(devh, 0); + } + libusb_close(devh); + libusb_exit(NULL); + return 0; +} + +// Uses interrupt transfers to write an Output report to the HID +// and receive an Input report from the HID. +// Returns - zero on success, libusb error code on failure. + +int exchange_input_and_output_reports_via_interrupt_transfers(libusb_device_handle *devh) +{ + int bytes_transferred; + char data_in[INTERRUPT_ENDPOINT_PACKET_SIZE-1]; + char data_out[INTERRUPT_ENDPOINT_PACKET_SIZE-1]; + int i = 0;; + int result = 0;; + + // Store data in the output buffer for sending. + + for (i=0;i= 0) + { + printf("Output report data sent via interrupt transfer:\n"); + for(i = 0; i < CONTROL_ENDPOINT_PACKET_SIZE; i++) + { + printf("%02x ",data_out[i]); + } + printf("\n"); + + // Read an Input report from the device. + + result = libusb_interrupt_transfer( + devh, + INTERRUPT_IN_ENDPOINT, + data_in, + INTERRUPT_ENDPOINT_PACKET_SIZE, + &bytes_transferred, + TIMEOUT_MS); + + if (result >= 0) + { + if (bytes_transferred == INTERRUPT_ENDPOINT_PACKET_SIZE) + { + printf("Input report received via interrupt transfer:\n"); + for(i = 0; i < CONTROL_ENDPOINT_PACKET_SIZE; i++) + { + printf("%02x ",data_in[i]); + } + printf("\n"); + } + else + { + fprintf(stderr, "Error: not all data received in interrupt transfer (%d)\n", result); + return -1; + } + } + else + { + fprintf(stderr, "Error receiving Input report via interrupt transfer %d\n", result); + return result; + } + } + else + { + fprintf(stderr, "Error sending Output report via interrupt transfer %d\n", result); + return result; + } + return 0; + } + +