From a86ec7c81d7ad2037e900899a0b32c5592cae7c0 Mon Sep 17 00:00:00 2001 From: AJM Date: Thu, 22 Apr 2010 23:27:09 -0400 Subject: c++ ified all of the usb_core files, added the auto-reset feature to the virtual com port, got the descriptors in functional although not pretty order that can be fixed using the attribute packing --- core/usb/descriptors.c | 165 +++++++++++++++++++++++--------------------- core/usb/descriptors.h | 125 +++++++++++++++++++++++++++------ core/usb/usb.c | 38 ++++++---- core/usb/usb.h | 9 ++- core/usb/usb_callbacks.c | 90 +++++++++++++++++------- core/usb/usb_callbacks.h | 53 +++++++++----- core/usb/usb_config.h | 33 +++++---- core/usb/usb_hardware.c | 5 +- core/usb/usb_hardware.h | 12 +++- core/usb/usb_lib/usb_conf.h | 86 ----------------------- core/usb/usb_lib/usb_core.c | 2 +- core/usb/usb_lib/usb_core.h | 8 +++ core/usb/usb_lib/usb_def.h | 8 +++ core/usb/usb_lib/usb_init.h | 8 +++ core/usb/usb_lib/usb_int.h | 8 +++ core/usb/usb_lib/usb_mem.h | 8 +++ core/usb/usb_lib/usb_regs.h | 8 +++ core/usb/usb_lib/usb_type.h | 10 ++- 18 files changed, 406 insertions(+), 270 deletions(-) delete mode 100644 core/usb/usb_lib/usb_conf.h (limited to 'core/usb') diff --git a/core/usb/descriptors.c b/core/usb/descriptors.c index 27d4250..7ff26a2 100644 --- a/core/usb/descriptors.c +++ b/core/usb/descriptors.c @@ -2,6 +2,8 @@ #include "descriptors.h" +#include "libmaple.h" +#include "usb_config.h" const USB_Descriptor_Device usbVcomDescriptor_Device = { bLength: sizeof(USB_Descriptor_Device), @@ -20,22 +22,19 @@ const USB_Descriptor_Device usbVcomDescriptor_Device = { bNumConfigurations: 0x01 }; -const USB_Descriptor_Configuration usbVcomDescriptor_Config = { - Header: - { - bLength: sizeof(USB_Descriptor_Config_Header), - bDescriptorType: USB_DESCRIPTOR_TYPE_CONFIGURATION, - wTotalLength: sizeof(USB_Descriptor_Configuration), - bNumInterfaces: 0x02, - bConfigurationValue: 0x01, - iConfiguration: 0x00, - bmAttributes: (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATT_SELF_POWERED), - bMaxPower: USB_CONFIG_MAX_POWER - }, +const USB_Descriptor_Config usbVcomDescriptor_Config = { + bLength: 0x09,//sizeof(USB_Descriptor_Config_Header), + bDescriptorType: USB_DESCRIPTOR_TYPE_CONFIGURATION, + wTotalLength: 0x43,//sizeof(USB_Descriptor_Config), + bNumInterfaces: 0x02, + bConfigurationValue: 0x01, + iConfiguration: 0x00, + bmAttributes: (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED), + bMaxPower: USB_CONFIG_MAX_POWER, CCI_Interface: { - bLength: sizeof(USB_Descriptor_Interface), + bLength: 0x09,//sizeof(USB_Descriptor_Interface), bDescriptorType: USB_DESCRIPTOR_TYPE_INTERFACE, bInterfaceNumber: 0x00, bAlternateSetting: 0x00, @@ -48,7 +47,7 @@ const USB_Descriptor_Configuration usbVcomDescriptor_Config = { CDC_Functional_IntHeader: { - bLength: sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), + bLength: 0x05,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), bDescriptorType: 0x24, SubType: 0x00, Data: {0x01, 0x10} @@ -56,7 +55,7 @@ const USB_Descriptor_Configuration usbVcomDescriptor_Config = { CDC_Functional_CallManagement: { - bLength: sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), + bLength: 0x05,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), bDescriptorType: 0x24, SubType: 0x01, Data: {0x03, 0x01} @@ -64,7 +63,7 @@ const USB_Descriptor_Configuration usbVcomDescriptor_Config = { CDC_Functional_ACM: { - bLength: sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)), + bLength: 0x04,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)), bDescriptorType: 0x24, SubType: 0x02, Data: {0x06} @@ -72,54 +71,59 @@ const USB_Descriptor_Configuration usbVcomDescriptor_Config = { CDC_Functional_Union: { - bLength: sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), + bLength: 0x05,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), bDescriptorType: 0x24, SubType: 0x06, Data: {0x00, 0x01} }, - ManagementEndpoint: - { - bLength: sizeof(USB_Descriptor_Endpoint), - bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT, - bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_IN | VCOM_NOTIFICATION_EPNUM), - bmAttributes: EP_TYPE_INTERRUPT, - wMaxPacketSize: VCOM_NOTIFICATION_EPSIZE, - bInterval: 0xFF - }, - - DCI_Interface: - { - bLength: sizeof(USB_Descriptor_Interface), - bDescriptorType: USB_DESCRIPTOR_TYPE_INTERFACE, - bInterfaceNumber: 0x01, - bAlternateSetting: 0x00, - bNumEndpoints: 0x02, - bInterfaceClass: 0x0A, - bInterfaceSubClass: 0x00, - bInterfaceProtocol: 0x00, - iInterface: 0x00 - }. - - DataOutEndpoint: - { - bLength: sizeof(USB_Descriptor_Endpoint), - bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT, - bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_OUT | VCOM_RX_EPNUM), - bmAttributes: EP_TYPE_BULK, - wMaxPacketSize: VCOM_RX_EPSIZE, - bInterval: 0x00 - }, - - DataInPoiint: - { - bLength: sizeof(USB_Descriptor_Endpoint), - bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT, - bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_IN | VCOM_TX_EPNUM), - bmAttributes: EP_TYPE_BULK, - wMaxPacketSize: VCOM_TX_EPSIZE, - bInterval: 0x00 - } + // ManagementEndpoint: + // { + EP1_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint), + EP1_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT, + EP1_bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_IN | VCOM_NOTIFICATION_EPNUM), + EP1_bmAttributes: EP_TYPE_INTERRUPT, + EP1_wMaxPacketSize0: VCOM_NOTIFICATION_EPSIZE, + EP1_wMaxPacketSize1: 0x00, + EP1_bInterval: 0xFF, + // }, + + // DCI_Interface: + // { + DCI_bLength: 0x09,//sizeof(USB_Descriptor_Interface), + DCI_bDescriptorType: USB_DESCRIPTOR_TYPE_INTERFACE, + DCI_bInterfaceNumber: 0x01, + DCI_bAlternateSetting: 0x00, + DCI_bNumEndpoints: 0x02, + DCI_bInterfaceClass: 0x0A, + DCI_bInterfaceSubClass: 0x00, + DCI_bInterfaceProtocol: 0x00, + DCI_iInterface: 0x00, + // }, + + //DataOutEndpoint: + // { + // }, + EP2_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint), + EP2_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT, + EP2_bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_OUT | VCOM_RX_EPNUM), + EP2_bmAttributes: EP_TYPE_BULK, + EP2_wMaxPacketSize0: VCOM_RX_EPSIZE, + EP2_wMaxPacketSize1: 0x00, + EP2_bInterval: 0x00, + + + // DataInEndpoint: + // { + EP3_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint), + EP3_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT, + EP3_bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_IN | VCOM_TX_EPNUM), + EP3_bmAttributes: EP_TYPE_BULK, + EP3_wMaxPacketSize0: VCOM_TX_EPSIZE, + EP3_wMaxPacketSize1: 0x00, + EP3_bInterval: 0x00 + + // } }; /* @@ -139,23 +143,26 @@ const USB_Descriptor_Configuration usbVcomDescriptor_Config = { which is 0x0409 for US English */ -const USB_Descriptor_String usbVcomDescriptor_LangID = - { - bLength: USB_DESCRIPTOR_STRING_LEN(1), - bDescriptorType: USB_DESCRIPTOR_TYPE_STRING, - bString: 0x0409 - }; - -const USB_Descriptor_String usbVcomDescriptor_iManufacturer = - { - bLength: USB_DESCRIPTOR_STRING_LEN(8), - bDescriptorType: USB_DESCRIPTOR_TYPE_STRING, - bString: L"LeafLabs" - }; - -const USB_Descriptor_String usbVcomDescriptor_iProduct = - { - bLength: USB_DESCRIPTOR_STRING_LEN(8), - bDescriptorType: USB_DESCRIPTOR_TYPE_STRING, - bString: L"Maple R3" - }; +uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] = +{ + USB_DESCRIPTOR_STRING_LEN(1), + USB_DESCRIPTOR_TYPE_STRING, + 0x09, + 0x04 +}; + +uint8 usbVcomDescriptor_iManufacturer[USB_DESCRIPTOR_STRING_LEN(8)] = +{ + USB_DESCRIPTOR_STRING_LEN(8), + USB_DESCRIPTOR_TYPE_STRING, + 'L', 0, 'e', 0, 'a', 0, 'f', 0, + 'L', 0, 'a', 0, 'b', 0, 's', 0 +}; + +uint8 usbVcomDescriptor_iProduct[USB_DESCRIPTOR_STRING_LEN(8)] = +{ + USB_DESCRIPTOR_STRING_LEN(8), + USB_DESCRIPTOR_TYPE_STRING, + 'M', 0, 'a', 0, 'p', 0, 'l', 0, + 'e', 0, ' ', 0, 'R', 0, '3', 0 +}; diff --git a/core/usb/descriptors.h b/core/usb/descriptors.h index 45d791d..f15221a 100644 --- a/core/usb/descriptors.h +++ b/core/usb/descriptors.h @@ -3,12 +3,8 @@ #define __DESCRIPTORS_H +#include "libmaple.h" #include "usb_lib.h" -#include "config.h" - -#if defined(__cplusplus) - extern "C" { -#endif #define USB_DESCRIPTOR_TYPE_DEVICE 0x01 #define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02 @@ -16,8 +12,11 @@ #define USB_DESCRIPTOR_TYPE_INTERFACE 0x04 #define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05 +#define USB_DEVICE_CLASS_CDC 0x02 +#define USB_DEVICE_SUBCLASS_CDC 0x00 + #define USB_CONFIG_ATTR_BUSPOWERED 0b10000000 -#define USB_CONFIG_ATTR_SELFPOWERED 0b11000000 +#define USB_CONFIG_ATTR_SELF_POWERED 0b11000000 #define EP_TYPE_INTERRUPT 0x03 #define EP_TYPE_BULK 0x02 @@ -27,6 +26,18 @@ #define USB_DESCRIPTOR_STRING_LEN(x) (2 + (x << 1)) + +#if defined(__cplusplus) +extern "C" { +#endif + +#define USB_DESCRIPTOR_STRING(len) \ + struct { \ + uint8 bLength; \ + uint8 bDescriptorType; \ + uint16 bString[len]; \ + } + #define CDC_FUCNTIONAL_DESCRIPTOR(DataSize) \ struct \ { \ @@ -86,29 +97,103 @@ typedef struct { } USB_Descriptor_Endpoint; typedef struct { - USB_Descriptor_Config_Header Header; + /* config header */ + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t bMaxPower; + USB_Descriptor_Interface CCI_Interface; - CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_IntHeader; - CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_CallManagement; - CDC_FUNCTIONAL_DESCRIPTOR(1) CDC_Functional_ACM; - CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_Union; + struct { + uint8 bLength; + uint8 bDescriptorType; + uint8 SubType; + uint8 Data[2]; + } CDC_Functional_IntHeader; + struct { + uint8 bLength; + uint8 bDescriptorType; + uint8 SubType; + uint8 Data[2]; + } CDC_Functional_CallManagement; + struct { + uint8 bLength; + uint8 bDescriptorType; + uint8 SubType; + uint8 Data[1]; + } CDC_Functional_ACM; + struct { + uint8 bLength; + uint8 bDescriptorType; + uint8 SubType; + uint8 Data[2]; + } CDC_Functional_Union; + + /* USB_Descriptor_Endpoint ManagementEndpoint; + */ + uint8_t EP1_bLength; + uint8_t EP1_bDescriptorType; + uint8_t EP1_bEndpointAddress; + uint8_t EP1_bmAttributes; + uint8_t EP1_wMaxPacketSize0; + uint8_t EP1_wMaxPacketSize1; + uint8_t EP1_bInterval; + + /* USB_Descriptor_Interface DCI_Interface; + */ + + uint8_t DCI_bLength; + uint8_t DCI_bDescriptorType; + uint8_t DCI_bInterfaceNumber; + uint8_t DCI_bAlternateSetting; + uint8_t DCI_bNumEndpoints; + uint8_t DCI_bInterfaceClass; + uint8_t DCI_bInterfaceSubClass; + uint8_t DCI_bInterfaceProtocol; + uint8_t DCI_iInterface; + + /* USB_Descriptor_Endpoint DataOutEndpoint; USB_Descriptor_Endpoint DataInEndpoint; -}USB_Descriptor_Configuration; + */ + + uint8_t EP2_bLength; + uint8_t EP2_bDescriptorType; + uint8_t EP2_bEndpointAddress; + uint8_t EP2_bmAttributes; + uint8_t EP2_wMaxPacketSize0; + uint8_t EP2_wMaxPacketSize1; + uint8_t EP2_bInterval; + + uint8_t EP3_bLength; + uint8_t EP3_bDescriptorType; + uint8_t EP3_bEndpointAddress; + uint8_t EP3_bmAttributes; + uint8_t EP3_wMaxPacketSize0; + uint8_t EP3_wMaxPacketSize1; + uint8_t EP3_bInterval; + + +}USB_Descriptor_Config; -typedef struct { - uint8 bLength; - uint8 bDescriptorType; - uint16 bString[]; -} USB_Descriptor_String; + typedef struct { + uint8 bLength; + uint8 bDescriptorType; + uint16 bString[]; + } USB_Descriptor_String; extern const USB_Descriptor_Device usbVcomDescriptor_Device; extern const USB_Descriptor_Config usbVcomDescriptor_Config; -extern const USB_Descriptor_String usbVcomDescriptor_LandID; -extern const USB_Descriptor_String usbVcomDescriptor_iManufacturer; -extern const USB_Descriptor_String usbVcomDescriptor_iProduct; + +extern uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)]; +extern uint8 usbVcomDescriptor_iManufacturer[USB_DESCRIPTOR_STRING_LEN(8)]; +extern uint8 usbVcomDescriptor_iProduct[USB_DESCRIPTOR_STRING_LEN(8)]; #if defined(__cplusplus) } diff --git a/core/usb/usb.c b/core/usb/usb.c index 2f458cc..8ef8697 100644 --- a/core/usb/usb.c +++ b/core/usb/usb.c @@ -31,12 +31,19 @@ */ #include "usb.h" +#include "libmaple.h" +#include "usb_lib.h" #include "gpio.h" +#include "usb_hardware.h" + +#include "usb_config.h" #include "usb_callbacks.h" +#include "usb_lib.h" /* persistent usb structs */ + volatile uint32 bDeviceState = UNCONNECTED; -volatile uint16 wIstr = 0; +volatile uint16_t wIstr = 0; volatile bIntPackSOF = 0; DEVICE Device_Table = @@ -55,31 +62,30 @@ DEVICE_PROP Device_Property = usbNoDataSetup, usbGetInterfaceSetting, usbGetDeviceDescriptor, - usbGetInterfaceDescriptor, usbGetConfigDescriptor, usbGetStringDescriptor, - usbGetFunctionalDescriptor, + NOP_Process, 0, bMaxPacketSize }; USER_STANDARD_REQUESTS User_Standard_Requests = { - usbGetConfiguration, + NOP_Process, usbSetConfiguration, - usbGetInterface, - usbSetInterface, - usbGetStatus, - usbClearFeature, - usbSetEndpointFeature, - usbSetDeviceFeature, + NOP_Process, + NOP_Process, + NOP_Process, + NOP_Process, + NOP_Process, + NOP_Process, usbSetDeviceAddress }; void (*pEpInt_IN[7])(void) = { - vcomManagementCb, vcomDataTxCb, + vcomManagementCb, NOP_Process, NOP_Process, NOP_Process, @@ -138,7 +144,6 @@ void usbResumeInit(void) { _SetCNTR(wCNTR); /* undo power reduction handlers here */ - _SetCNTR(ISR_MSK); } @@ -242,7 +247,7 @@ void usbDsbISR(void) { } /* overloaded ISR routine, this is the main usb ISR */ -void usb_lpIRQHandler(void); +void usb_lpIRQHandler(void) { wIstr = _GetISTR(); /* go nuts with the preproc switches since this is an ISTR and must be FAST */ @@ -327,5 +332,12 @@ if (wIstr & ISTR_CTR & wInterrupt_Mask) CTR_LP(); /* low priority ISR defined in the usb core lib */ } #endif +} +void usbSendHello(void) { + char* myStr = "HELLO!"; + char myCh = 'a'; + UserToPMABufferCopy((uint8*)myStr,VCOM_TX_ADDR,6); + _SetEPTxCount(VCOM_TX_ENDP,6); + _SetEPTxValid(VCOM_TX_ENDP); } diff --git a/core/usb/usb.h b/core/usb/usb.h index b2bf23c..087ca72 100644 --- a/core/usb/usb.h +++ b/core/usb/usb.h @@ -3,9 +3,9 @@ #ifndef __USB_H_ #define __USB_H_ -#include "usb_config.h" -#include "usb_callbacks.h" + #include "usb_lib.h" +#include "libmaple.h" #ifdef __cplusplus extern "C" { @@ -33,6 +33,8 @@ typedef enum CONFIGURED } DEVICE_STATE; + extern volatile uint32 bDeviceState; + void setupUSB(void); void usbSuspend(void); void usbResumeInit(void); @@ -47,7 +49,10 @@ typedef enum /* overloaded ISR routine, this is the main usb ISR */ void usb_lpIRQHandler(void); + void usbSendHello(void); + #ifdef __cplusplus } // extern "C" #endif + #endif //_USB_H diff --git a/core/usb/usb_callbacks.c b/core/usb/usb_callbacks.c index 3e881ae..83b39c7 100644 --- a/core/usb/usb_callbacks.c +++ b/core/usb/usb_callbacks.c @@ -2,22 +2,24 @@ #include "usb_callbacks.h" #include "usb_lib.h" -#include "usb_descriptors.h" +#include "descriptors.h" +#include "usb_config.h" +#include "usb.h" ONE_DESCRIPTOR Device_Descriptor = { - (uint8*)usbVcomDescriptor_Device, + (uint8*)&usbVcomDescriptor_Device, sizeof(USB_Descriptor_Device) }; ONE_DESCRIPTOR Config_Descriptor = { - (uint8*)usbVcomDescriptor_Config, - sizeof(USB_Descriptor_Config) + (uint8*)&usbVcomDescriptor_Config, + 0x43//sizeof(USB_Descriptor_Config) }; ONE_DESCRIPTOR String_Descriptor[3] = { - {(uint8*)usbVcomDescriptor_LangID, USB_DESCRIPTOR_STRING_LEN(1)}, - {(uint8*)usbVcomDescriptor_iManufacturer,USB_DESCRIPTOR_STRING_LEN(8)}, - {(uint8*)usbVcomDescriptor_iProduct, USB_DESCRIPTOR_STRING_LEN(8)} + {(uint8*)&usbVcomDescriptor_LangID, USB_DESCRIPTOR_STRING_LEN(1)}, + {(uint8*)&usbVcomDescriptor_iManufacturer,USB_DESCRIPTOR_STRING_LEN(8)}, + {(uint8*)&usbVcomDescriptor_iProduct, USB_DESCRIPTOR_STRING_LEN(8)} }; uint8 last_request = 0; @@ -29,11 +31,13 @@ USB_Line_Coding line_coding = { datatype: 0x08 }; -uint8 vcomBufferTx[VCOM_TX_SIZE]; -uint8 vcomBufferRx[VCOM_RX_SIZE]; +uint8 vcomBufferTx[VCOM_TX_EPSIZE]; +uint8 vcomBufferRx[VCOM_RX_EPSIZE]; uint8 countTx = 0; uint8 countRx = 0; +RESET_STATE reset_state = START; + void vcomDataTxCb(void) { /* do whatever after data has been sent to host */ countTx = 0; @@ -41,14 +45,14 @@ void vcomDataTxCb(void) { void vcomDataRxCb(void) { /* do whatever after data has been received from host */ - countRx = GetEPRxCount(VCOM_RX_ENDP); - PMAToUserBufferCopy(vcomBufferRx,VCOM_RX_ADDR,countRx); - SetEPRxValid(VCOM_RX_ENDP); + /* countRx = GetEPRxCount(VCOM_RX_ENDP); */ +/* PMAToUserBufferCopy(vcomBufferRx,VCOM_RX_ADDR,countRx); */ +/* SetEPRxValid(VCOM_RX_ENDP); */ - countTx = countRx; - UserToPMABufferCopy(vcomBufferRx,VCOM_TX_ADDR,countTx); - SetEPTxCount(VCOM_TX_ENDP,countTx); - SetEPTxValid(VCOM_TX_ENDP); +/* countTx = countRx; */ +/* UserToPMABufferCopy(vcomBufferRx,VCOM_TX_ADDR,countTx); */ +/* SetEPTxCount(VCOM_TX_ENDP,countTx); */ +/* SetEPTxValid(VCOM_TX_ENDP); */ } void vcomManagementCb(void) { @@ -58,12 +62,12 @@ void vcomManagementCb(void) { u8* vcomGetSetLineCoding(uint16 length) { if (length == 0) { - pInformation->Ctrl_Info.Usb_wLength = sizeof(Line_Coding); + pInformation->Ctrl_Info.Usb_wLength = sizeof(USB_Line_Coding); } return (uint8*)&line_coding; } -void vcomSetLineSate(uint16 wValue) { +vcomSetLineState(void) { } void usbInit(void) { @@ -71,7 +75,7 @@ void usbInit(void) { usbPowerOn(); _SetISTR(0); - wInterrupt_Mas = ISR_MSK; + wInterrupt_Mask = ISR_MSK; _SetCNTR(wInterrupt_Mask); usbEnbISR(); @@ -82,7 +86,7 @@ void usbReset(void) { pInformation->Current_Configuration = 0; /* current feature is current bmAttributes */ - pInformation->Current_Feature = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED); + pInformation->Current_Feature = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED); _SetBTABLE(USB_BTABLE_ADDRESS); @@ -90,7 +94,7 @@ void usbReset(void) { _SetEPType(ENDP0, EP_CONTROL); _SetEPTxStatus(ENDP0, EP_TX_STALL); _SetEPRxAddr(ENDP0,VCOM_CTRL_RX_ADDR); - _SetEpTxAddr(ENDP0,VCOM_CTRL_TX_ADDR); + _SetEPTxAddr(ENDP0,VCOM_CTRL_TX_ADDR); Clear_Status_Out(ENDP0); SetEPRxCount(ENDP0, pProperty->MaxPacketSize); @@ -105,14 +109,13 @@ void usbReset(void) { /* setup data endpoint OUT (rx) */ SetEPType (VCOM_RX_ENDP, EP_BULK); SetEPRxAddr (VCOM_RX_ENDP, VCOM_RX_ADDR); - SetEPRxCount (VCOM_RX_ENDP, VCOM_RX_SIZE); + SetEPRxCount (VCOM_RX_ENDP, VCOM_RX_EPSIZE); SetEPTxStatus (VCOM_RX_ENDP, EP_TX_DIS); SetEPRxStatus (VCOM_RX_ENDP, EP_RX_VALID); /* setup data endpoint IN (tx) */ SetEPType (VCOM_TX_ENDP, EP_BULK); SetEPRxAddr (VCOM_TX_ENDP, VCOM_TX_ADDR); - SetEPRxCount (VCOM_TX_ENDP, VCOM_TX_SIZE); SetEPTxStatus (VCOM_TX_ENDP, EP_TX_NAK); SetEPRxStatus (VCOM_TX_ENDP, EP_RX_DIS); @@ -159,14 +162,44 @@ RESULT usbDataSetup(uint8 request) { } RESULT usbNoDataSetup(u8 request) { + uint8 new_signal; + /* we support set com feature but dont handle it */ if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) { + switch (request) { case (SET_COMM_FEATURE): return USB_SUCCESS; case (SET_CONTROL_LINE_STATE): - /* handle the control line status signal (reset?) */ - vcomSetLineState(); + /* to reset the board, pull both dtr and rts low + then pulse dtr by itself */ + new_signal = pInformation->USBwValues.bw.bb0 & (CONTROL_LINE_DTR | CONTROL_LINE_RTS); + switch (reset_state) { + /* no default, covered enum */ + case START: + if (new_signal == 0) { + reset_state = NDTR_NRTS; + } + break; + + case NDTR_NRTS: + if (new_signal == CONTROL_LINE_DTR) { + reset_state = DTR_NRTS; + } else if (new_signal == 0) { + reset_state = NDTR_NRTS; + } else { + reset_state = START; + } + break; + + case DTR_NRTS: + if (new_signal == 0) { + systemHardReset(); + } else { + reset_state = START; + } + break; + } return USB_SUCCESS; } } @@ -193,7 +226,12 @@ u8* usbGetConfigDescriptor(u16 length) { } u8* usbGetStringDescriptor(u16 length) { - return Standard_GetDescriptorData(length, &String_Descriptor); + uint8 wValue0 = pInformation->USBwValue0; + + if (wValue0 > 2) { + return NULL; + } + return Standard_GetDescriptorData(length, &String_Descriptor[wValue0]); } /* internal callbacks to respond to standard requests */ diff --git a/core/usb/usb_callbacks.h b/core/usb/usb_callbacks.h index 4fe287f..53857ae 100644 --- a/core/usb/usb_callbacks.h +++ b/core/usb/usb_callbacks.h @@ -1,8 +1,21 @@ +/* insert license */ + +#ifndef __USB_CALLBACKS +#define __USB_CALLBACKS + +#include "libmaple.h" +#include "usb_lib.h" + #define SET_LINE_CODING 0x20 #define GET_LINE_CODING 0x21 +#define SET_COMM_FEATURE 0x02 #define SET_CONTROL_LINE_STATE 0x22 -#define CONTROL_LINE_DTR (1 << 0) -#define CONTROL_LINE_RTS (1 << 1) +#define CONTROL_LINE_DTR (0x01) +#define CONTROL_LINE_RTS (0x02) + +#if defined(__cplusplus) +extern "C" { +#endif typedef struct { uint32 bitrate; @@ -11,12 +24,18 @@ typedef struct { uint8 datatype; } USB_Line_Coding; +typedef enum { + START, + NDTR_NRTS, + DTR_NRTS +} RESET_STATE; + void vcomDataTxCb(void); void vcomDataRxCb(void); void vcomManagementCb(void); -u8* vcomGetSetLineCoding(uint16 length); -void vcomSetLineSate(uint16 wValue); +uint8* vcomGetSetLineCoding(uint16 length); +void vcomSetLineSate(void); void usbInit(void); /* internal functions (as per the usb_core pProperty structure) */ @@ -25,23 +44,21 @@ void usbReset(void); void usbStatusIn(void); void usbStatusOut(void); -RESULT usbDataSetup(u8 request); -RESULT usbNoDataSetup(u8 request); -RESULT usbGetInterfaceSetting(u8,u8); +RESULT usbDataSetup(uint8 request); +RESULT usbNoDataSetup(uint8 request); +RESULT usbGetInterfaceSetting(uint8,uint8); -u8* usbGetDeviceDescriptor(u16 length); -u8* usbGetConfigDescriptor(u16 length); -u8* usbGetStringDescriptor(u16 length); -u8* usbGetFunctionalDescriptor(u16 length); +uint8* usbGetDeviceDescriptor(uint16 length); +uint8* usbGetConfigDescriptor(uint16 length); +uint8* usbGetStringDescriptor(uint16 length); /* internal callbacks to respond to standard requests */ -void usbGetConfiguration(void); void usbSetConfiguration(void); -void usbGetInterface(void); -void usbSetInterface(void); -void usbGetStatus(void); -void usbClearFeature(void); -void usbSetEndpointFeature(void); -void usbSetDeviceFeature(void); void usbSetDeviceAddress(void); +#if defined(__cplusplus) +} +#endif + + +#endif diff --git a/core/usb/usb_config.h b/core/usb/usb_config.h index b457677..3ff24f8 100644 --- a/core/usb/usb_config.h +++ b/core/usb/usb_config.h @@ -13,19 +13,19 @@ /* choose addresses to give endpoints the max 64 byte buffers */ #define USB_BTABLE_ADDRESS 0x00 #define VCOM_CTRL_EPNUM 0x00 -#define VCOM_CTRL_TX_ADDR 0x40 -#define VCOM_CTRL_RX_ADDR 0x80 +#define VCOM_CTRL_RX_ADDR 0x40 +#define VCOM_CTRL_TX_ADDR 0x80 #define VCOM_CTRL_EPSIZE 0x40 -#define VCOM_NOTIFICATION_ENDP ENDP1 -#define VCOM_NOTIFICATION_EPNUM 0x01 -#define VCOM_NOTIFICATION_ADDR 0xC0 +#define VCOM_NOTIFICATION_ENDP ENDP2 +#define VCOM_NOTIFICATION_EPNUM 0x02 +#define VCOM_NOTIFICATION_ADDR 0x100 #define VCOM_NOTIFICATION_EPSIZE 0x40 -#define VCOM_TX_ENDP ENDP2 -#define VCOM_TX_EPNUM 0x02 -#define VCOM_TX_ADDR 0x100 -#define VCOM_TX_SIZE 0x040 +#define VCOM_TX_ENDP ENDP1 +#define VCOM_TX_EPNUM 0x01 +#define VCOM_TX_ADDR 0xC0 +#define VCOM_TX_EPSIZE 0x40 #define VCOM_RX_ENDP ENDP3 #define VCOM_RX_EPNUM 0x03 @@ -37,14 +37,13 @@ #define NUM_ENDPTS 0x03 /* handle all usb interrupts */ -#define ISR_MSK (CNTR_CTRM | \ - CNTR_WKUPM | \ - CNTR_SUSPM | \ - CNTR_ERRM | \ - CNTR_SOFM | \ - CNTR_ESOFM | \ - CNTR_RESETM | \ - ) +#define ISR_MSK ( CNTR_CTRM | \ + CNTR_WKUPM | \ + CNTR_SUSPM | \ + CNTR_ERRM | \ + CNTR_SOFM | \ + CNTR_ESOFM | \ + CNTR_RESETM ) #define USB_DISC_BANK GPIOC_BASE #define USB_DISC_PIN 12 diff --git a/core/usb/usb_hardware.c b/core/usb/usb_hardware.c index 10918ca..2f37df6 100644 --- a/core/usb/usb_hardware.c +++ b/core/usb/usb_hardware.c @@ -29,7 +29,7 @@ * */ -#include "hardware.h" +#include "usb_hardware.h" void setPin(u32 bank, u8 pin) { u32 pinMask = 0x1 << (pin); @@ -56,9 +56,6 @@ void setupCLK (void) { SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ - /* enable flash prefetch buffer */ - SET_REG(FLASH_ACR, 0x00000012); - /* Configure PLL */ SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ diff --git a/core/usb/usb_hardware.h b/core/usb/usb_hardware.h index e60567d..d7e4ff0 100644 --- a/core/usb/usb_hardware.h +++ b/core/usb/usb_hardware.h @@ -25,8 +25,7 @@ #ifndef __HARDWARE_H #define __HARDWARE_H -#include "stm32f10x_type.h" -#include "cortexm3_macro.h" +#include "usb_type.h" /* macro'd register and peripheral definitions */ #define RCC ((u32)0x40021000) @@ -74,6 +73,9 @@ #define SET_REG(addr,val) *(vu32*)(addr)=val #define GET_REG(addr) *(vu32*)(addr) +#if defined(__cplusplus) +extern "C" { +#endif /* todo: there must be some major misunderstanding in how we access regs. The direct access approach (GET_REG) causes the usb init to fail upon trying to activate RCC_APB1 |= 0x00800000. However, using the struct approach @@ -111,7 +113,7 @@ typedef struct { u8 NVIC_IRQChannel; u8 NVIC_IRQChannelPreemptionPriority; u8 NVIC_IRQChannelSubPriority; - bool NVIC_IRQChannelCmd; /* TRUE for enable */ + USB_Bool NVIC_IRQChannelCmd; /* TRUE for enable */ } NVIC_InitTypeDef; typedef struct { @@ -142,4 +144,8 @@ void setupCLK (void); void nvicInit (NVIC_InitTypeDef*); void nvicDisableInterrupts(void); +#if defined(__cplusplus) +} +#endif + #endif diff --git a/core/usb/usb_lib/usb_conf.h b/core/usb/usb_lib/usb_conf.h deleted file mode 100644 index e9b7f14..0000000 --- a/core/usb/usb_lib/usb_conf.h +++ /dev/null @@ -1,86 +0,0 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_conf.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Device Firmware Upgrade (DFU) configuration file -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CONF_H -#define __USB_CONF_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -/* External variables --------------------------------------------------------*/ -/*-------------------------------------------------------------*/ -/* EP_NUM */ -/* defines how many endpoints are used by the device */ -/*-------------------------------------------------------------*/ -#define EP_NUM (1) - -/*-------------------------------------------------------------*/ -/* -------------- Buffer Description Table -----------------*/ -/*-------------------------------------------------------------*/ -/* buffer table base address */ -/* buffer table base address */ -#define BTABLE_ADDRESS (0x00) - -/* EP0 */ -/* rx/tx buffer base address */ -#define ENDP0_RXADDR (0x10) -#define ENDP0_TXADDR (0x50) - - -/*-------------------------------------------------------------*/ -/* ------------------- ISTR events -------------------------*/ -/*-------------------------------------------------------------*/ -/* IMR_MSK */ -/* mask defining which events has to be handled */ -/* by the device application software */ -#define IMR_MSK (CNTR_CTRM | \ - CNTR_WKUPM | \ - CNTR_SUSPM | \ - CNTR_ERRM | \ - CNTR_SOFM | \ - CNTR_ESOFM | \ - CNTR_RESETM \ - ) - -/* CTR service routines */ -/* associated to defined endpoints */ -#define EP1_IN_Callback NOP_Process -#define EP2_IN_Callback NOP_Process -#define EP3_IN_Callback NOP_Process -#define EP4_IN_Callback NOP_Process -#define EP5_IN_Callback NOP_Process -#define EP6_IN_Callback NOP_Process -#define EP7_IN_Callback NOP_Process - - -#define EP1_OUT_Callback NOP_Process -#define EP2_OUT_Callback NOP_Process -#define EP3_OUT_Callback NOP_Process -#define EP4_OUT_Callback NOP_Process -#define EP5_OUT_Callback NOP_Process -#define EP6_OUT_Callback NOP_Process -#define EP7_OUT_Callback NOP_Process - - -#endif /*__USB_CONF_H*/ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ - - - - diff --git a/core/usb/usb_lib/usb_core.c b/core/usb/usb_lib/usb_core.c index f637165..223a2e1 100644 --- a/core/usb/usb_lib/usb_core.c +++ b/core/usb/usb_lib/usb_core.c @@ -37,7 +37,7 @@ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ u16_u8 StatusInfo; -bool Data_Mul_MaxPacketSize = FALSE; +USB_Bool Data_Mul_MaxPacketSize = FALSE; /* Private function prototypes -----------------------------------------------*/ static void DataStageOut(void); static void DataStageIn(void); diff --git a/core/usb/usb_lib/usb_core.h b/core/usb/usb_lib/usb_core.h index efeaad8..1931fbf 100644 --- a/core/usb/usb_lib/usb_core.h +++ b/core/usb/usb_lib/usb_core.h @@ -17,6 +17,10 @@ #ifndef __USB_CORE_H #define __USB_CORE_H +#if defined(__cplusplus) +extern "C" { +#endif + /* Includes ------------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/ typedef enum _CONTROL_STATE @@ -239,6 +243,10 @@ extern DEVICE_INFO Device_Info; extern u16 SaveRState; extern u16 SaveTState; +#if defined(__cplusplus) +} +#endif + #endif /* __USB_CORE_H */ /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/core/usb/usb_lib/usb_def.h b/core/usb/usb_lib/usb_def.h index 4261d46..aa6bcba 100644 --- a/core/usb/usb_lib/usb_def.h +++ b/core/usb/usb_lib/usb_def.h @@ -17,6 +17,10 @@ #ifndef __USB_DEF_H #define __USB_DEF_H +#if defined(__cplusplus) +extern "C" { +#endif + /* Includes ------------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/ typedef enum _RECIPIENT_TYPE @@ -75,6 +79,10 @@ typedef enum _FEATURE_SELECTOR /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ +#if defined(__cplusplus) +} +#endif + #endif /* __USB_DEF_H */ /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/core/usb/usb_lib/usb_init.h b/core/usb/usb_lib/usb_init.h index 76e9d8a..1091ce5 100644 --- a/core/usb/usb_lib/usb_init.h +++ b/core/usb/usb_lib/usb_init.h @@ -17,6 +17,10 @@ #ifndef __USB_INIT_H #define __USB_INIT_H +#if defined(__cplusplus) +extern "C" { +#endif + /* Includes ------------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ @@ -44,6 +48,10 @@ extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; extern u16 SaveState ; extern u16 wInterrupt_Mask; +#if defined(__cplusplus) +} +#endif + #endif /* __USB_INIT_H */ /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/core/usb/usb_lib/usb_int.h b/core/usb/usb_lib/usb_int.h index 1eb18a7..10a33fb 100644 --- a/core/usb/usb_lib/usb_int.h +++ b/core/usb/usb_lib/usb_int.h @@ -23,11 +23,19 @@ /* Exported constants --------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ +#if defined(__cplusplus) +extern "C" { +#endif + void CTR_LP(void); void CTR_HP(void); /* External variables --------------------------------------------------------*/ +#if defined(__cplusplus) +} +#endif + #endif /* __USB_INT_H */ /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/core/usb/usb_lib/usb_mem.h b/core/usb/usb_lib/usb_mem.h index d4cc4a6..a3d7927 100644 --- a/core/usb/usb_lib/usb_mem.h +++ b/core/usb/usb_lib/usb_mem.h @@ -22,9 +22,17 @@ /* Exported constants --------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ +#if defined(__cplusplus) +extern "C" { +#endif + void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); +#if defined(__cplusplus) +} +#endif + /* External variables --------------------------------------------------------*/ #endif /*__USB_MEM_H*/ diff --git a/core/usb/usb_lib/usb_regs.h b/core/usb/usb_lib/usb_regs.h index 8f32e44..9a4df75 100644 --- a/core/usb/usb_lib/usb_regs.h +++ b/core/usb/usb_lib/usb_regs.h @@ -19,6 +19,10 @@ /* Includes ------------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/ +#if defined(__cplusplus) +extern "C" { +#endif + typedef enum _EP_DBUF_DIR { /* double buffered endpoint direction */ @@ -614,6 +618,10 @@ void FreeUserBuffer(u8 bEpNum/*bEpNum*/, u8 bDir); u16 ToWord(u8, u8); u16 ByteSwap(u16); +#if defined(__cplusplus) +} +#endif + #endif /* __USB_REGS_H */ /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/core/usb/usb_lib/usb_type.h b/core/usb/usb_lib/usb_type.h index 9e428b9..44b1f8c 100644 --- a/core/usb/usb_lib/usb_type.h +++ b/core/usb/usb_lib/usb_type.h @@ -17,6 +17,10 @@ #ifndef __USB_TYPE_H #define __USB_TYPE_H +#if defined(__cplusplus) +extern "C" { +#endif + /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ #ifndef NULL @@ -54,7 +58,7 @@ typedef enum { FALSE = 0, TRUE = !FALSE } -bool; +USB_Bool; typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus; @@ -67,6 +71,10 @@ typedef enum { ERROR = 0, SUCCESS = !ERROR} ErrorStatus; /* Exported functions ------------------------------------------------------- */ /* External variables --------------------------------------------------------*/ +#if defined(__cplusplus) +} +#endif + #endif /* __USB_TYPE_H */ /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ -- cgit v1.2.3