Advertisement
Guest User

Untitled

a guest
Nov 28th, 2012
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.22 KB | None | 0 0
  1. /*
  2.  * usbdefs.h
  3.  *
  4.  * Created: 11.9.2012 12:42:01
  5.  *  Author: Ondra
  6.  */
  7.  
  8.  
  9. #ifndef USBDEFS_H_
  10. #define USBDEFS_H_
  11.  
  12. #include "types.h"
  13. #include "../XBoot.h"
  14. #include "usb_cdc.h"
  15.  
  16.  
  17.  
  18.  
  19. #define byte    unsigned char
  20. #define word    uint16_t
  21. #define dword   uint32_t
  22.  
  23.  
  24. struct usb_ram_t
  25. {
  26.     uint16_t FIFO[(XBOOT_ENDPOINTS + 1) * 2];
  27.     USB_EP_t endpoint[XBOOT_ENDPOINTS * 2];
  28.     unsigned char payload[XBOOT_ENDPOINTS * 2][64];
  29. } ;
  30.  
  31.  
  32. typedef struct USB_DeviceRequest_t USB_DeviceRequest;
  33.  
  34. #define bReqGET_STATUS      0
  35. #define bReqCLEAR_FEATURE   1
  36. #define bReqSET_FEATURE     3
  37. #define bReqSET_ADDRESS     5
  38. #define bReqGET_DESCRIPTOR  6
  39. #define bReqSET_DESCRIPTOR  7
  40. #define bReqGET_CONFIGURATION   8
  41. #define bReqSET_CONFIGURATION   9
  42. #define bReqGET_INTERFACE   10
  43. #define bReqSET_INTERFACE   11
  44. #define bReqSYNCH_FRAME     12
  45.  
  46. #define DescriptorDEVICE    1
  47. #define DescriptorCONFIGURATION 2
  48. #define DescriptorSTRING    3
  49. #define DescriptorINTERFACE 4
  50. #define DescriptorENDPOINT  5
  51. #define DescriptorDEVICE_QUALIFIER  6
  52. #define DescriptorOTHER_SPEED_CONFIGURATION 7
  53. #define DescriptorINTERFACE_POWER   8
  54.  
  55. #define  CDC_CLASS_DEVICE     0x02
  56.  
  57. #define XBOOT_VID       0x03EB
  58. #define XBOOT_PID       0x2404
  59.  
  60. #define USB_STRING_LIST             0
  61. #define USB_STRING_MANUFACTURER     1
  62. #define USB_STRING_PRODUCT          2
  63. #define USB_STRING_SERIAL           3
  64.  
  65. #define usb_ack_ctrl_in()       LASR16(&usb_ram.endpoint[1].STATUS, USB_EP_BUSNACK0_bm);
  66. #define usb_ack_ctrl_out()      LASR16(&usb_ram.endpoint[0].STATUS, USB_EP_BUSNACK0_bm);
  67.  
  68. #define USB_STATE_DEFAULT       1
  69. #define USB_STATE_ADDRESS       2
  70. #define USB_STATE_CONFIGURED    3
  71. #define USB_STATE_POSTADDR      4
  72.  
  73. #define USB_CONF_SELF_POWERED   (1 << 6)
  74. #define USB_CONF_REMOTE_WAKEUP  (1 << 5)
  75. #define USB_CONF_ALWAYSONE      (1 << 7)
  76.  
  77. enum usb_ep_type {
  78.     USB_EP_TYPE_CONTROL = 0x00,
  79.     USB_EP_TYPE_ISOCHRONOUS = 0x01,
  80.     USB_EP_TYPE_BULK = 0x02,
  81.     USB_EP_TYPE_INTERRUPT = 0x03,
  82.     USB_EP_TYPE_MASK = 0x03,
  83. };
  84.  
  85.  
  86. #define XBOOT_MANUFACTURER  L"XBoard"
  87. #define XBOOT_PRODUCT       L"XBoard coco"
  88. #define XBOOT_SN            L"0001"
  89.  
  90.  
  91. typedef struct Device_Descriptor_t
  92. {
  93.     byte bLength;
  94.     byte bDescriptorType;
  95.     word bcdUSB;
  96.     byte bDeviceClass;
  97.     byte bDeviceSubClass;
  98.     byte bDeviceProtocol;
  99.     byte bMaxPacketSize0;
  100.     word idVendor;
  101.     word idProduct;
  102.     word bcdDevice;
  103.     byte iManufacturer;
  104.     byte iProduct;
  105.     byte iSerialNumber;
  106.     byte bNumConfigurations;
  107. } Device_Descriptor_t;
  108.  
  109. typedef struct Device_Qualifier_Descriptor_t
  110. {
  111.     byte bLength;
  112.     byte bDescriptorType;
  113.     word bcdUSB;
  114.     byte bDeviceClass;
  115.     byte bDeviceSubClass;
  116.     byte bDeviceProtocol;
  117.     byte bMaxPacketSize0;
  118.     byte bNumConfigurations;
  119.     byte bReserved;
  120. } Device_Qualifier_Descriptor_t;
  121.  
  122. typedef struct String_Descriptor_t
  123. {
  124.     byte bLength;
  125.     byte bDescriptorType;
  126.     wchar_t bString[24];
  127. } String_Descriptor_t;
  128.  
  129. typedef struct Configuration_Descriptor_t
  130. {
  131.     byte bLength;
  132.     byte bDescriptorType;
  133.     word wTotalLength;
  134.     byte bNumInterfaces;
  135.     byte bConfigurationValue;
  136.     byte iConfiguration;
  137.     byte bmAttributes;
  138.     byte bMaxPower;
  139. } Configuration_Descriptor_t;
  140.  
  141. typedef struct Interface_Descriptor_t
  142. {
  143.     byte bLength;
  144.     byte bDescriptorType;
  145.     byte bInterfaceNumber;
  146.     byte bAlternateSetting;
  147.     byte bNumEndpoints;
  148.     byte bInterfaceClass;
  149.     byte bInterfaceSubClass;
  150.     byte bInterfaceProtocol;
  151.     byte iInterface;
  152. } Interface_Descriptor_t;
  153.  
  154. typedef struct Endpoint_Descriptor_t
  155. {
  156.     byte bLength;
  157.     byte bDescriptorType;
  158.     byte bEndpointAddress;
  159.     byte bmAttributes;
  160.     word wMaxPacketSize;
  161.     byte bInterval;
  162. } Endpoint_Descriptor_t;
  163.  
  164. typedef struct Language_Descriptor_t
  165. {
  166.     byte bLength;
  167.     byte bDescriptorType;
  168.     word wLANGID;
  169. } Language_Descriptor_t;
  170.  
  171. typedef struct Get_Status_t
  172. {
  173.     byte b1;
  174.     byte b2;
  175. } Get_Status_t;
  176.  
  177. typedef struct {
  178.     byte bFunctionLength;
  179.     byte bDescriptorType;
  180.     byte bDescriptorSubtype;
  181.     word bcdCDC;
  182. } CDC_Header_t;
  183.  
  184. typedef struct {
  185.     byte bFunctionLength;
  186.     byte bDescriptorType;
  187.     byte bDescriptorSubtype;
  188.     byte bmCapabilities;
  189. } CDC_ACM_t;
  190.  
  191. typedef struct {
  192.     byte bFunctionLength;
  193.     byte bDescriptorType;
  194.     byte bDescriptorSubtype;
  195.     byte bControlInterface;
  196.     byte bMasterInterface;
  197.     byte bSlaveInterface0;
  198. } CDC_Union_t;
  199.  
  200. typedef struct {
  201.     byte bFunctionLength;
  202.     byte bDescriptorType;
  203.     byte bDescriptorSubtype;
  204.     byte bmCapabilities;
  205.     byte bDataInterface;
  206. } CDC_Management_t;
  207.  
  208. typedef struct {
  209.     Interface_Descriptor_t Interface;
  210.     CDC_Header_t Header;
  211.     CDC_ACM_t ACM;
  212.     CDC_Union_t Union;
  213.     CDC_Management_t Management;
  214.     Endpoint_Descriptor_t EndpointNotify;
  215. } USB_CDC_Descriptor_Communication_t;
  216.  
  217. typedef struct {
  218.     Interface_Descriptor_t Interface;
  219.     Endpoint_Descriptor_t EndpointIn;
  220.     Endpoint_Descriptor_t EndpointOut;
  221. } USB_CDC_Descriptor_Data_t;
  222.  
  223.  
  224. typedef struct Complete_Configuration_t
  225. {
  226.     Configuration_Descriptor_t Configuration;
  227.     USB_CDC_Descriptor_Communication_t Comm;
  228.     USB_CDC_Descriptor_Data_t Data;
  229. } Complete_Configuration_t;
  230.  
  231. typedef struct {
  232.     Configuration_Descriptor_t Configuration;
  233.     USB_CDC_Descriptor_Communication_t Communication;
  234.     USB_CDC_Descriptor_Data_t Data;
  235. } UDC_Descriptor_t;
  236.  
  237. typedef struct
  238. {
  239.     byte bLength;
  240.     byte bDescriptorType;
  241.     word wTotalLength;
  242.     byte bNumInterfaces;
  243.     byte bConfigurationValue;
  244.     byte iConfiguration;
  245.     byte bmAttributes;
  246.     byte bMaxPower;
  247. } Other_Speed_Configuration_t;
  248.  
  249. Device_Descriptor_t Device_Descriptor = {
  250.     .bLength = sizeof(Device_Descriptor_t),
  251.     .bDescriptorType = DescriptorDEVICE,
  252.     .bcdUSB = 0x0200, // 02 00 in LE
  253.     .bDeviceClass = CDC_CLASS_DEVICE,
  254.     .bDeviceSubClass = 0,
  255.     .bDeviceProtocol = 0,
  256.     .bMaxPacketSize0 = XBOOT_DATASIZE,
  257.     .idVendor = XBOOT_VID,
  258.     .idProduct = XBOOT_PID,
  259.     .bcdDevice = XBOOT_VERSION,
  260.     .iManufacturer = USB_STRING_MANUFACTURER,
  261.     .iProduct = USB_STRING_PRODUCT,
  262.     .iSerialNumber = 0,
  263.     .bNumConfigurations = 1
  264. };
  265.  
  266. String_Descriptor_t String_Manufacturer = {
  267.     .bLength = 2 + sizeof(XBOOT_MANUFACTURER) - 2,
  268.     .bDescriptorType = DescriptorSTRING,
  269.     .bString = XBOOT_MANUFACTURER
  270. };
  271.  
  272. String_Descriptor_t String_Product = {
  273.     .bLength = 2 + sizeof(XBOOT_PRODUCT) - 2,
  274.     .bDescriptorType = DescriptorSTRING,
  275.     .bString = XBOOT_PRODUCT
  276. };
  277.  
  278. String_Descriptor_t String_SerialNumber = {
  279.     .bLength = 2 + sizeof(XBOOT_SN) - 2,
  280.     .bDescriptorType = DescriptorSTRING,
  281.     .bString = XBOOT_SN
  282. };
  283.  
  284. Device_Qualifier_Descriptor_t Device_Qualifier_Descriptor = {
  285.     .bLength = sizeof(Device_Qualifier_Descriptor_t),
  286.     .bDescriptorType = DescriptorDEVICE_QUALIFIER,
  287.     .bcdUSB = 0x2000,
  288.     .bDeviceClass = CDC_CLASS_DEVICE,
  289.     .bDeviceSubClass = 0,
  290.     .bDeviceProtocol = 0,
  291.     .bMaxPacketSize0 = XBOOT_DATASIZE,
  292.     .bNumConfigurations = 1,
  293.     .bReserved = 0
  294. };
  295.  
  296. Language_Descriptor_t Language_Descriptor = {
  297.     .bLength = 4,
  298.     .bDescriptorType = DescriptorSTRING,
  299.     .wLANGID = 0x0409
  300. };
  301.  
  302. Complete_Configuration_t Complete_Configuration = {
  303.     .Configuration = {
  304.         .bLength = sizeof(Configuration_Descriptor_t),
  305.         .bDescriptorType = DescriptorCONFIGURATION,
  306.         .wTotalLength = sizeof(Complete_Configuration_t),
  307.         .bNumInterfaces = 2,
  308.         .bConfigurationValue = 1,
  309.         .iConfiguration = 0,
  310.         .bmAttributes = USB_CONF_ALWAYSONE,
  311.         .bMaxPower = 500/2
  312.     },
  313.     .Comm = {
  314.         .Interface = {
  315.             .bLength = sizeof(Interface_Descriptor_t),
  316.             .bDescriptorType = DescriptorINTERFACE,
  317.             .bInterfaceNumber = 0,
  318.             .bAlternateSetting = 0,
  319.             .bNumEndpoints = 1,
  320.             .bInterfaceClass = CDC_CLASS_DEVICE,
  321.             .bInterfaceSubClass = USB_CDC_Subclass_AbstractControlModel,
  322.             .bInterfaceProtocol = USB_CDC_Interface_Class_Protocol_ITU_T_V250,
  323.             .iInterface = 0
  324.         },
  325.         .Header = {
  326.             .bFunctionLength = sizeof(CDC_Header_t),
  327.             .bDescriptorType = CDC_CS_INTERFACE,
  328.             .bDescriptorSubtype = CDC_SCS_HEADER,
  329.             .bcdCDC = 0x0110
  330.         },
  331.         .ACM = {
  332.             .bFunctionLength = sizeof(CDC_ACM_t),
  333.             .bDescriptorType = CDC_CS_INTERFACE,
  334.             .bDescriptorSubtype = CDC_SCS_ACM,
  335.             .bmCapabilities = CDC_ACM_SUPPORT_LINE_REQUESTS
  336.            
  337.         },
  338.         .Union = {
  339.             .bFunctionLength = sizeof(CDC_Union_t),
  340.             .bDescriptorType = CDC_CS_INTERFACE,
  341.             .bDescriptorSubtype = CDC_SCS_UNION
  342.         },
  343.         .Management = {
  344.             .bFunctionLength = sizeof(CDC_Management_t),
  345.             .bDescriptorType = CDC_CS_INTERFACE,
  346.             .bDescriptorSubtype = CDC_SCS_CALL_MGMT,
  347.             .bmCapabilities = CDC_CALL_MGMT_OVER_DCI | CDC_CALL_MGMT_SUPPORTED
  348.         },
  349.         .EndpointNotify = {
  350.             .bLength = sizeof(Endpoint_Descriptor_t),
  351.             .bDescriptorType = DescriptorENDPOINT,
  352.             .bmAttributes = USB_EP_TYPE_INTERRUPT,
  353.             .wMaxPacketSize = 64, // 64 bytes
  354.             .bInterval = 0x10,
  355.             .bEndpointAddress = 0x82
  356.         }
  357.     },
  358.     .Data = {
  359.         .Interface = {
  360.             .bLength = sizeof(Interface_Descriptor_t),
  361.             .bDescriptorType = DescriptorINTERFACE,
  362.             .bAlternateSetting = 0,
  363.             .bNumEndpoints = 2,
  364.             .bInterfaceClass = CDC_CLASS_DEVICE,
  365.             .bInterfaceSubClass = 0,
  366.             .bInterfaceProtocol = 0,
  367.             .bInterfaceNumber = 1,
  368.             .iInterface = 0
  369.         },
  370.         .EndpointIn = {
  371.             .bLength = sizeof(Endpoint_Descriptor_t),
  372.             .bDescriptorType = DescriptorENDPOINT,
  373.             .bmAttributes = USB_EP_TYPE_BULK,
  374.             .bInterval = 2,
  375.             .bEndpointAddress = 0x81,
  376.             .wMaxPacketSize = 64
  377.         },
  378.         .EndpointOut = {
  379.             .bLength = sizeof(Endpoint_Descriptor_t),
  380.             .bDescriptorType = DescriptorENDPOINT,
  381.             .bmAttributes = USB_EP_TYPE_BULK,
  382.             .bInterval = 2,
  383.             .bEndpointAddress = 0x02,
  384.             .wMaxPacketSize = 64
  385.         }
  386.     }
  387. };
  388.  
  389.  
  390. Get_Status_t Get_Status_Reply = {
  391.     .b1 = 0,
  392.     .b2 = 0
  393. };
  394.  
  395.  
  396. #define USB_REQ_DIRECTION_MASK      0b1     << 7
  397. #define USB_REQ_TYPE_MASK           0b11    << 5
  398. #define USB_REQ_RECIPIENT_MASK      0b11111 << 0   
  399.  
  400.  
  401. #define USB_REQ_DIRECTION_HOSTTODEVICE      0 << 7
  402. #define USB_REQ_DIRECTION_DEVICETOHOST      1 << 7
  403.  
  404. #define USB_REQ_TYPE_STANDARD       0 << 5
  405. #define USB_REQ_TYPE_CLASS          1 << 5
  406. #define USB_REQ_TYPE_VENDOR         2 << 5
  407.  
  408. #define USB_REQ_RECIPIENT_DEVICE    0 << 0
  409. #define USB_REQ_RECIPIENT_INTERFACE 1 << 0
  410. #define USB_REQ_RECIPIENT_ENDPOINT  2 << 0
  411. #define USB_REQ_RECIPIENT_OTHER     3 << 0
  412.  
  413.  
  414. CDC_Line_Coding_t CDC_Line_Coding = {
  415.     .dwDTERate = 115200,
  416.     .bCharFormat = 0, // 1 stop bit
  417.     .bParityType = 0, // no parity
  418.     .bDataBits = 8 // 8 bits per byte
  419. };
  420.  
  421.  
  422.  
  423. #define LACR16(addr,msk) \
  424. __asm__ __volatile__ ( \
  425. "ldi r16, %1" "\n\t" \
  426. ".dc.w 0x9306" "\n\t"\
  427. ::"z" (addr), "M" (msk):"r16")
  428.  
  429. /*#define LASR16(addr,msk) \
  430.    __asm__ __volatile__ ( \
  431.          "ldi r16, %1" "\n\t" \
  432.          ".dc.w 0x9305" "\n\t"\
  433.          ::"z" (addr), "M" (msk):"r16")
  434. */
  435.  
  436. #define LASR16(addr,mask) *addr &= ~mask
  437.  
  438.  
  439.  
  440. #endif /* USBDEFS_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement