?? main.c
字號:
/* Name: main.c * Project: AVR-Doper * Author: Christian Starkjohann <cs@obdev.at> * Creation Date: 2006-06-21 * Tabsize: 4 * Copyright: (c) 2006 by Christian Starkjohann, all rights reserved. * License: Proprietary, see documentation. * Revision: $Id: main.c 223 2006-07-18 09:28:13Z cs $ *//*General Description:This module implements hardware initialization and the USB interface*/#include "hardware.h"#include <avr/io.h>#include <avr/interrupt.h>#include <avr/pgmspace.h>#include <avr/wdt.h>#include <util/delay.h>#include <string.h>#include "utils.h"#include "usbdrv.h"#include "oddebug.h"#include "stk500protocol.h"#include "vreg.h"#include "serial.h"/* ------------------------------------------------------------------------- *//* CDC class requests: */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};/* defines for 'requestType' */#define REQUEST_TYPE_LINE_CODING 0 /* CDC GET/SET_LINE_CODING */#define REQUEST_TYPE_HID_FIRST 1 /* first block of HID reporting */#define REQUEST_TYPE_HID_SUBSEQUENT 2 /* subsequent block of HID reporting */#define REQUEST_TYPE_HID_DEBUGDATA 3 /* read/write data from/to debug interface */#define REQUEST_TYPE_VENDOR 4 /* vendor request for get/set debug data *//* ------------------------------------------------------------------------- */#if ENABLE_CDC_INTERFACEstatic uchar modeBuffer[7] = {0x80, 0x25, 0, 0, 0, 0, 8}; /* default: 9600 bps, 8n1 */# if USE_DCD_REPORTINGstatic uchar intr3Status;# endif#endif#if ENABLE_HID_INTERFACEstatic uchar hidStatus;#endifstatic uchar requestType;static uchar useHIDInterface;#if ENABLE_CDC_INTERFACEstatic PROGMEM char deviceDescrCDC[] = { /* USB device descriptor */ 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */ USBDESCR_DEVICE, /* descriptor type */ 0x01, 0x01, /* USB version supported */ 0x02, /* device class: CDC */ 0, /* subclass */ 0, /* protocol */ 8, /* max packet size */ USB_CFG_VENDOR_ID, /* 2 bytes */ 0xe1, 0x05, /* 2 bytes: shared PID for CDC-ACM devices */ USB_CFG_DEVICE_VERSION, /* 2 bytes */ 1, /* manufacturer string index */ 2, /* product string index */ 0, /* serial number string index */ 1, /* number of configurations */};static PROGMEM char configDescrCDC[] = { /* USB configuration descriptor */ 9, /* sizeof(usbDescriptorConfiguration): 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 USBATTR_SELFPOWER, /* attributes */#else USBATTR_BUSPOWER, /* attributes */#endif USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */ /* interface descriptors follow inline: */ /* Interface Descriptor for CDC-ACM Control */ 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ USBDESCR_INTERFACE, /* descriptor type */ 0, /* index of this interface */ 0, /* alternate setting for this interface */ 1, /* endpoints excl 0: number of endpoint descriptors to follow */ USB_CFG_INTERFACE_CLASS, /* see usbconfig.h */ USB_CFG_INTERFACE_SUBCLASS, USB_CFG_INTERFACE_PROTOCOL, 0, /* string index for interface */ /* CDC Class-Specific descriptors */ 5, /* sizeof(usbDescrCDC_HeaderFn): length of descriptor in bytes */ 0x24, /* descriptor type */ 0, /* Subtype: header functional descriptor */ 0x10, 0x01, /* CDC spec release number in BCD */ 4, /* sizeof(usbDescrCDC_AcmFn): length of descriptor in bytes */ 0x24, /* descriptor type */ 2, /* Subtype: abstract control management functional descriptor */ 0x02, /* capabilities: SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE */ 5, /* sizeof(usbDescrCDC_UnionFn): length of descriptor in bytes */ 0x24, /* descriptor type */ 6, /* Subtype: union functional descriptor */ 0, /* CDC_COMM_INTF_ID: master interface (control) */ 1, /* CDC_DATA_INTF_ID: slave interface (data) */ 5, /* sizeof(usbDescrCDC_CallMgtFn): length of descriptor in bytes */ 0x24, /* descriptor type */ 1, /* Subtype: call management functional descriptor */ 0x03, /* capabilities: allows management on data interface, handles call management by itself */ 1, /* CDC_DATA_INTF_ID: interface used for call management */ /* Endpoint Descriptor */ 7, /* sizeof(usbDescrEndpoint) */ USBDESCR_ENDPOINT, /* descriptor type = endpoint */ 0x83, /* IN endpoint number 3 */ 0x03, /* attrib: Interrupt endpoint */ 8, 0, /* maximum packet size */ 100, /* in ms */ /* Interface Descriptor for CDC-ACM Data */ 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, /* interface subclass */ 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 */ 8, 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 */ 8, 0, /* maximum packet size */ 0, /* in ms */};#endif /* ENABLE_CDC_INTERFACE */#if ENABLE_HID_INTERFACEstatic PROGMEM char deviceDescrHID[] = { /* USB device descriptor */ 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */ USBDESCR_DEVICE, /* descriptor type */ 0x01, 0x01, /* USB version supported */ 0, /* device class: composite */ 0, /* subclass */ 0, /* protocol */ 8, /* max packet size */ USB_CFG_VENDOR_ID, /* 2 bytes */ 0xdf, 0x05, /* 2 bytes: shared PID for HIDs */ USB_CFG_DEVICE_VERSION, /* 2 bytes */ 1, /* manufacturer string index */ 2, /* product string index */ 0, /* serial number string index */ 1, /* number of configurations */};static PROGMEM char configDescrHID[] = { /* USB configuration descriptor */ 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */ USBDESCR_CONFIG,/* descriptor type */ 18 + 7 + 9, 0, /* total length of data returned (including inlined descriptors) */ 1, /* number of interfaces in this configuration */ 1, /* index of this configuration */ 0, /* configuration name string index */#if USB_CFG_IS_SELF_POWERED USBATTR_SELFPOWER, /* attributes */#else USBATTR_BUSPOWER, /* 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 */ 3, /* interface class: HID */ 0, /* subclass */ 0, /* protocol */ 0, /* string index for interface */ 9, /* sizeof(usbDescrHID): length of descriptor in bytes */ USBDESCR_HID, /* descriptor type: HID */ 0x01, 0x01, /* BCD representation of HID version */ 0x00, /* target country code */ 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ 0x22, /* descriptor type: report */ USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */ 7, /* sizeof(usbDescrEndpoint) */ USBDESCR_ENDPOINT, /* descriptor type = endpoint */ 0x81, /* IN endpoint number 1 */ 0x03, /* attrib: Interrupt endpoint */ 8, 0, /* maximum packet size */ USB_CFG_INTR_POLL_INTERVAL, /* in ms */};PROGMEM char usbDescriptorHidReport[60] = { 0x06, 0x00, 0xff, // USAGE_PAGE (Generic Desktop) 0x09, 0x01, // USAGE (Vendor Usage 1) 0xa1, 0x01, // COLLECTION (Application) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255) 0x75, 0x08, // REPORT_SIZE (8) 0x85, 0x01, // REPORT_ID (1) 0x95, 0x0e, // REPORT_COUNT (14) 0x09, 0x00, // USAGE (Undefined) 0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf) 0x85, 0x02, // REPORT_ID (2) 0x95, 0x1e, // REPORT_COUNT (30) 0x09, 0x00, // USAGE (Undefined) 0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf) 0x85, 0x03, // REPORT_ID (3) 0x95, 0x3e, // REPORT_COUNT (62) 0x09, 0x00, // USAGE (Undefined) 0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf) 0x85, 0x04, // REPORT_ID (4) 0x95, 0x7e, // REPORT_COUNT (126) 0x09, 0x00, // USAGE (Undefined) 0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf) 0x85, 0x06, // REPORT_ID (5) [for debug interface] 0x95, 0x3e, // REPORT_COUNT (62) 0x09, 0x00, // USAGE (Undefined) 0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf) 0xc0 // END_COLLECTION};#endif /* ENABLE_HID_INTERFACE *//* ------------------------------------------------------------------------- */uchar usbFunctionDescriptor(usbRequest_t *rq){uchar *p = NULL, len = 0, useHID = useHIDInterface; if(useHID){#if ENABLE_HID_INTERFACE if(rq->wValue.bytes[1] == USBDESCR_DEVICE){ p = (uchar *)deviceDescrHID; len = sizeof(deviceDescrHID); }else if(rq->wValue.bytes[1] == USBDESCR_CONFIG){ p = (uchar *)configDescrHID; len = sizeof(configDescrHID); }else{ /* must be HID descriptor */ p = (uchar *)(configDescrHID + 18); len = 9; }#endif }else{#if ENABLE_CDC_INTERFACE if(rq->wValue.bytes[1] == USBDESCR_DEVICE){ p = (uchar *)deviceDescrCDC; len = sizeof(deviceDescrCDC); }else{ /* must be config descriptor */ p = (uchar *)configDescrCDC; len = sizeof(configDescrCDC); }#endif } usbMsgPtr = p; return len;}/* ------------------------------------------------------------------------- *//* ----------------------------- USB interface ----------------------------- *//* ------------------------------------------------------------------------- */uchar usbFunctionSetup(uchar data[8]){usbRequest_t *rq = (void *)data;uchar rqType = rq->bmRequestType & USBRQ_TYPE_MASK; if(rqType == USBRQ_TYPE_CLASS){ /* class request type */ if(useHIDInterface){#if ENABLE_HID_INTERFACE if(rq->bRequest == USBRQ_HID_GET_REPORT || rq->bRequest == USBRQ_HID_SET_REPORT){ hidStatus = rq->wValue.bytes[0]; /* store report ID */ if(rq->wValue.bytes[0] == 5){ /* report ID */ requestType = REQUEST_TYPE_HID_DEBUGDATA; }else{ requestType = REQUEST_TYPE_HID_FIRST; } /* the driver counts the total number of bytes for us */ return 0xff; }#endif }else{#if ENABLE_CDC_INTERFACE
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -