summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2017-07-09 19:01:29 +0200
committerChristian Pointner <equinox@spreadspace.org>2017-07-09 19:01:29 +0200
commit90502d960a962489dc821333b8e6d398dc7c29c0 (patch)
tree9f5464cf27a37bd42b4b5b2e955deed8d83f27ff
parentvusb cdc-acm example (still not working) (diff)
vusb-led example works now
-rw-r--r--vusb-led/usbconfig.h56
-rw-r--r--vusb-led/vusb-led.c288
2 files changed, 63 insertions, 281 deletions
diff --git a/vusb-led/usbconfig.h b/vusb-led/usbconfig.h
index a111c69..b1cf315 100644
--- a/vusb-led/usbconfig.h
+++ b/vusb-led/usbconfig.h
@@ -17,32 +17,28 @@
#define USB_CFG_DPLUS_BIT 4
#define USB_CFG_CLOCK_KHZ (F_CPU/1000)
-#if USB_CFG_CLOCK_KHZ==18000
-#define USB_CFG_CHECK_CRC 1
-#else
#define USB_CFG_CHECK_CRC 0
-#endif
/* ----------------------- Optional Hardware Config ------------------------ */
/* #define USB_CFG_PULLUP_IOPORTNAME B */
-/* #define USB_CFG_PULLUP_BIT 3 */
+/* #define USB_CFG_PULLUP_BIT 2 */
/* --------------------------- Functional Range ---------------------------- */
-#define USB_CFG_HAVE_INTRIN_ENDPOINT 1
-#define USB_CFG_HAVE_INTRIN_ENDPOINT3 1
+#define USB_CFG_HAVE_INTRIN_ENDPOINT 0
+#define USB_CFG_HAVE_INTRIN_ENDPOINT3 0
#define USB_CFG_EP3_NUMBER 3
/* #define USB_INITIAL_DATATOKEN USBPID_DATA1 */
#define USB_CFG_IMPLEMENT_HALT 0
#define USB_CFG_SUPPRESS_INTR_CODE 0
-#define USB_CFG_INTR_POLL_INTERVAL 255
+#define USB_CFG_INTR_POLL_INTERVAL 10
#define USB_CFG_IS_SELF_POWERED 0
-#define USB_CFG_MAX_BUS_POWER 100
-#define USB_CFG_IMPLEMENT_FN_WRITE 1
-#define USB_CFG_IMPLEMENT_FN_READ 1
-#define USB_CFG_IMPLEMENT_FN_WRITEOUT 1
-#define USB_CFG_HAVE_FLOWCONTROL 1
+#define USB_CFG_MAX_BUS_POWER 40
+#define USB_CFG_IMPLEMENT_FN_WRITE 0
+#define USB_CFG_IMPLEMENT_FN_READ 0
+#define USB_CFG_IMPLEMENT_FN_WRITEOUT 0
+#define USB_CFG_HAVE_FLOWCONTROL 0
#define USB_CFG_DRIVER_FLASH_PAGE 0
#define USB_CFG_LONG_TRANSFERS 0
/* #define USB_RX_USER_HOOK(data, len) if(usbRxToken == (uchar)USBPID_SETUP) blinkLED(); */
@@ -64,26 +60,26 @@
/* -------------------------- Device Description --------------------------- */
#define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */
-#define USB_CFG_DEVICE_ID 0xe1, 0x05 /* = 0x05e1 = 1505 */
+#define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x05dc = 1500 */
#define USB_CFG_DEVICE_VERSION 0x00, 0x01
-#define USB_CFG_VENDOR_NAME 'o', 'b', 'd', 'e', 'v', '.', 'a', 't'
-#define USB_CFG_VENDOR_NAME_LEN 8
-#define USB_CFG_DEVICE_NAME 'T', 'e', 'm', 'p', 'l', 'a', 't', 'e'
+#define USB_CFG_VENDOR_NAME 's', 'p', 'r', 'e', 'a', 'd', 's', 'p', 'a', 'c', 'e'
+#define USB_CFG_VENDOR_NAME_LEN 11
+#define USB_CFG_DEVICE_NAME 'v', 'u', 's', 'b', '-', 'l', 'e', 'd'
#define USB_CFG_DEVICE_NAME_LEN 8
/*#define USB_CFG_SERIAL_NUMBER 'N', 'o', 'n', 'e' */
/*#define USB_CFG_SERIAL_NUMBER_LEN 0 */
-#define USB_CFG_DEVICE_CLASS 2 /* set to 0 if deferred to interface */
+#define USB_CFG_DEVICE_CLASS 0xff /* set to 0 if deferred to interface */
#define USB_CFG_DEVICE_SUBCLASS 0
-#define USB_CFG_INTERFACE_CLASS 2 /* CDC class */
-#define USB_CFG_INTERFACE_SUBCLASS 2 /* Abstract (Modem) */
-#define USB_CFG_INTERFACE_PROTOCOL 1 /* AT-Commands */
+#define USB_CFG_INTERFACE_CLASS 0 /* define class here if not at device level */
+#define USB_CFG_INTERFACE_SUBCLASS 0
+#define USB_CFG_INTERFACE_PROTOCOL 0
/* #define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 42 */
/* #define USB_PUBLIC static */
/* ------------------- Fine Control over USB Descriptors ------------------- */
#define USB_CFG_DESCR_PROPS_DEVICE 0
-#define USB_CFG_DESCR_PROPS_CONFIGURATION USB_PROP_IS_DYNAMIC
+#define USB_CFG_DESCR_PROPS_CONFIGURATION 0
#define USB_CFG_DESCR_PROPS_STRINGS 0
#define USB_CFG_DESCR_PROPS_STRING_0 0
#define USB_CFG_DESCR_PROPS_STRING_VENDOR 0
@@ -98,13 +94,13 @@
/* ----------------------- Optional MCU Description ------------------------ */
-/* #define USB_INTR_CFG MCUCR */
-/* #define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) */
-/* #define USB_INTR_CFG_CLR 0 */
-/* #define USB_INTR_ENABLE GIMSK */
-/* #define USB_INTR_ENABLE_BIT INT0 */
-/* #define USB_INTR_PENDING GIFR */
-/* #define USB_INTR_PENDING_BIT INTF0 */
-/* #define USB_INTR_VECTOR INT0_vect */
+#define USB_INTR_CFG PCMSK
+#define USB_INTR_CFG_SET (1 << USB_CFG_DPLUS_BIT)
+#define USB_INTR_CFG_CLR 0
+#define USB_INTR_ENABLE GIMSK
+#define USB_INTR_ENABLE_BIT PCIE
+#define USB_INTR_PENDING GIFR
+#define USB_INTR_PENDING_BIT PCIF
+#define USB_INTR_VECTOR PCINT0_vect
#endif /* __usbconfig_h_included__ */
diff --git a/vusb-led/vusb-led.c b/vusb-led/vusb-led.c
index d47d415..31e7c13 100644
--- a/vusb-led/vusb-led.c
+++ b/vusb-led/vusb-led.c
@@ -35,278 +35,64 @@
#include "led.h"
-enum {
- SEND_ENCAPSULATED_COMMAND = 0,
- GET_ENCAPSULATED_RESPONSE,
- SET_COMM_FEATURE,
- GET_COMM_FEATURE,
- CLEAR_COMM_FEATURE,
- SET_LINE_CODING = 0x20,
- GET_LINE_CODING,
- SET_CONTROL_LINE_STATE,
- SEND_BREAK
-};
-
-#define VUSBIO_BULK_OUT_SIZE 8
-#define VUSBIO_BULK_IN_SIZE 8
-
-static PROGMEM const char configDescrCDC[] = { /* USB configuration descriptor */
- 9, /* sizeof(usbDescrConfig): length of descriptor in bytes */
- USBDESCR_CONFIG, /* descriptor type */
- 67,
- 0, /* total length of data returned (including inlined descriptors) */
- 2, /* number of interfaces in this configuration */
- 1, /* index of this configuration */
- 0, /* configuration name string index */
-#if USB_CFG_IS_SELF_POWERED
- (1 << 7) | USBATTR_SELFPOWER, /* attributes */
-#else
- (1 << 7), /* attributes */
-#endif
- USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
-
- /* interface descriptor follows inline: */
- 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
- USBDESCR_INTERFACE, /* descriptor type */
- 0, /* index of this interface */
- 0, /* alternate setting for this interface */
- USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
- USB_CFG_INTERFACE_CLASS,
- USB_CFG_INTERFACE_SUBCLASS,
- USB_CFG_INTERFACE_PROTOCOL,
- 0, /* string index for interface */
-
- /* CDC Class-Specific descriptor */
- 5, /* sizeof(usbDescrCDC_HeaderFn): length of descriptor in bytes */
- 0x24, /* descriptor type */
- 0, /* header functional descriptor */
- 0x10, 0x01,
-
- 4, /* sizeof(usbDescrCDC_AcmFn): length of descriptor in bytes */
- 0x24, /* descriptor type */
- 2, /* abstract control management functional descriptor */
- 0x02, /* SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE */
-
- 5, /* sizeof(usbDescrCDC_UnionFn): length of descriptor in bytes */
- 0x24, /* descriptor type */
- 6, /* union functional descriptor */
- 0, /* CDC_COMM_INTF_ID */
- 1, /* CDC_DATA_INTF_ID */
-
- 5, /* sizeof(usbDescrCDC_CallMgtFn): length of descriptor in bytes */
- 0x24, /* descriptor type */
- 1, /* call management functional descriptor */
- 3, /* allow management on data interface, handles call management by itself */
- 1, /* CDC_DATA_INTF_ID */
-
- /* Endpoint Descriptor */
- 7, /* sizeof(usbDescrEndpoint) */
- USBDESCR_ENDPOINT, /* descriptor type = endpoint */
- 0x80|USB_CFG_EP3_NUMBER, /* IN endpoint number 3 */
- 0x03, /* attrib: Interrupt endpoint */
- 8, 0, /* maximum packet size */
- USB_CFG_INTR_POLL_INTERVAL, /* in ms */
-
- /* Interface Descriptor */
- 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
- USBDESCR_INTERFACE, /* descriptor type */
- 1, /* index of this interface */
- 0, /* alternate setting for this interface */
- 2, /* endpoints excl 0: number of endpoint descriptors to follow */
- 0x0A, /* Data Interface Class Codes */
- 0,
- 0, /* Data Interface Class Protocol Codes */
- 0, /* string index for interface */
-
- /* Endpoint Descriptor */
- 7, /* sizeof(usbDescrEndpoint) */
- USBDESCR_ENDPOINT, /* descriptor type = endpoint */
- 0x01, /* OUT endpoint number 1 */
- 0x02, /* attrib: Bulk endpoint */
- VUSBIO_BULK_OUT_SIZE, 0, /* maximum packet size */
- 0, /* in ms */
-
- /* Endpoint Descriptor */
- 7, /* sizeof(usbDescrEndpoint) */
- USBDESCR_ENDPOINT, /* descriptor type = endpoint */
- 0x81, /* IN endpoint number 1 */
- 0x02, /* attrib: Bulk endpoint */
- VUSBIO_BULK_IN_SIZE, 0, /* maximum packet size */
- 0, /* in ms */
-};
-
-
-uchar usbFunctionDescriptor(usbRequest_t *rq)
-{
- if(rq->wValue.bytes[1] == USBDESCR_DEVICE) {
- usbMsgPtr = (uchar *)usbDescriptorDevice;
- return usbDescriptorDevice[0];
- } else { /* must be config descriptor */
- usbMsgPtr = (uchar *)configDescrCDC;
- return sizeof(configDescrCDC);
- }
-}
-
-
-uchar sendEmptyFrame;
-static uchar intr3Status; /* used to control interrupt endpoint transmissions */
-static usbWord_t baud;
-
-
-/* ------------------------------------------------------------------------- */
-/* ----------------------------- USB interface ----------------------------- */
-/* ------------------------------------------------------------------------- */
-
-uchar usbFunctionSetup(uchar data[8])
+void vusbio_init(void)
{
- usbRequest_t *rq = (void *)data;
-
- if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
- if( rq->bRequest==GET_LINE_CODING || rq->bRequest==SET_LINE_CODING ) {
- return 0xff;
- /* GET_LINE_CODING -> usbFunctionRead() */
- /* SET_LINE_CODING -> usbFunctionWrite() */
- }
- if(rq->bRequest == SET_CONTROL_LINE_STATE) {
- /* Report serial state (carrier detect). On several Unix platforms,
- * tty devices can only be opened when carrier detect is set.
- */
- if(intr3Status==0)
- intr3Status = 2;
- }
+ odDebugInit();
+ USBOUT &= ~USBMASK;
+ USBDDR &= ~USBMASK;
+ usbInit();
- /* Prepare bulk-in endpoint to respond to early termination */
- if((rq->bmRequestType & USBRQ_DIR_MASK) == USBRQ_DIR_HOST_TO_DEVICE)
- sendEmptyFrame = 1;
+ usbDeviceDisconnect();
+ uchar i = 0;
+ while(--i){
+ wdt_reset();
+ _delay_ms(1);
}
-
- return 0;
-}
-
-
-/*---------------------------------------------------------------------------*/
-/* usbFunctionRead */
-/*---------------------------------------------------------------------------*/
-
-uchar usbFunctionRead(uchar *data, uchar len)
-{
- data[0] = baud.bytes[0];
- data[1] = baud.bytes[1];
- data[2] = 0;
- data[3] = 0;
- data[4] = 0;
- data[5] = 0;
- data[6] = 8;
-
- return 7;
+ usbDeviceConnect();
}
-
-/*---------------------------------------------------------------------------*/
-/* usbFunctionWrite */
-/*---------------------------------------------------------------------------*/
-
-uchar usbFunctionWrite(uchar *data, uchar len)
+void vusbio_task(void)
{
- /* SET_LINE_CODING */
- baud.bytes[0] = data[0];
- baud.bytes[1] = data[1];
-
- //uartInit(baud.word);
- return 1;
+ usbPoll();
}
-void usbFunctionWriteOut(uchar *data, uchar len)
+usbMsgLen_t usbFunctionSetup(uchar data[8])
{
- for(;len;len--) {
-// tx_buf[uwptr++] = *data++;
+ usbRequest_t *req = (void *)data;
+ static uchar buf[4]; /* buffer must stay valid when usbFunctionSetup returns */
+
+ switch(req->bRequest) {
+ case 0: { /* echo */
+ buf[0] = req->wValue.bytes[0];
+ buf[1] = req->wValue.bytes[1];
+ buf[2] = req->wIndex.bytes[0];
+ buf[3] = req->wIndex.bytes[1];
+ usbMsgPtr = (usbMsgPtr_t)buf;
+ return 4;
+ }
+ case 1: {
+ if(req->wValue.bytes[0] & 1) {
+ led_on();
+ } else {
+ led_off();
+ }
+ break;
}
-}
-
-static void vusbio_init(void)
-{
- /* activate pull-ups except on USB lines */
- USB_CFG_IOPORT = (uchar)~((1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT));
- /* all pins input except USB (-> USB reset) */
-#ifdef USB_CFG_PULLUP_IOPORT /* use usbDeviceConnect()/usbDeviceDisconnect() if available */
- USBDDR = 0; /* we do RESET by deactivating pullup */
- usbDeviceDisconnect();
-#else
- USBDDR = (1<<USB_CFG_DMINUS_BIT)|(1<<USB_CFG_DPLUS_BIT);
-#endif
-
- /* 250 ms disconnect */
- wdt_reset();
- _delay_ms(250);
-
-#ifdef USB_CFG_PULLUP_IOPORT
- usbDeviceConnect();
-#else
- USBDDR = 0; /* remove USB reset condition */
-#endif
-}
-
-
-
-void handle_cmd(uint8_t cmd)
-{
- switch(cmd) {
- case '0': led_off(); break;
- case '1': led_on(); break;
- case 't': led_toggle(); break;
- case 'o': led2_off(); break;
- case 'i': led2_on(); break;
- case 'T': led2_toggle(); break;
- case 'r': reset2bootloader(); break;
- default: printf("error\r\n"); return;
}
- printf("ok\r\n");
+ return 0;
}
int main(void)
{
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
+ wdt_enable(WDTO_1S);
cpu_init();
- led_init();
- odDebugInit();
- usbInit();
vusbio_init();
+ led_init();
sei();
for(;;) {
- }
-
- intr3Status = 0;
- sendEmptyFrame = 0;
-
- sei();
- for(;;) { /* main event loop */
wdt_reset();
- usbPoll();
-
- /* int16_t BytesReceived = vusbio_bytes_received(); */
- /* while(BytesReceived > 0) { */
- /* int ReceivedByte = fgetc(stdin); */
- /* if(ReceivedByte != EOF) { */
- /* handle_cmd(ReceivedByte); */
- /* } */
- /* BytesReceived--; */
- /* } */
-
-// vusbio_task();
-
- /* We need to report rx and tx carrier after open attempt */
- if(intr3Status != 0 && usbInterruptIsReady3()){
- static uchar serialStateNotification[10] = {0xa1, 0x20, 0, 0, 0, 0, 2, 0, 3, 0};
-
- if(intr3Status == 2){
- usbSetInterrupt3(serialStateNotification, 8);
- }else{
- usbSetInterrupt3(serialStateNotification+8, 2);
- }
- intr3Status--;
- }
+ vusbio_task();
}
}