1. Set_System(); Set_USBClock(); USB_Init(); /* Main loop */ while (1) {}
    1. void USB_Init(void) { pInformation = &Device_Info; pInformation->ControlState = 2;//此时Device_Info尚未负责 pProperty = &Device_Property; //在usbPro.c中定义时赋值 pUser_Standard_Requests = &User_Standard_Requests; pProperty->Init(); }
      1. pProperty->Init();
        1. void DFU_init(void) { DEVICE_INFO *pInfo = &Device_Info; Get_SerialNum();//把UID赋给相应数组 pInfo->Current_Configuration = 0; PowerOn(); /* USB interrupts initialization */ _SetISTR(0); /* clear pending interrupts */ wInterrupt_Mask = IMR_MSK; _SetCNTR(wInterrupt_Mask); /* set interrupts mask */ /* Enable USB interrupts */ USB_Interrupts_Config();//配置中断向量 bDeviceState = UNCONNECTED; }
      2. 其他事情都由中断完成
        1. 重要的宏定义了中断处理的类型 #define IMR_MSK 。。。
          1. CNTR_CTRM
          2. if (wIstr & ISTR_CTR & wInterrupt_Mask) { CTR_LP(); }
          3. CNTR_WKUPM
          4. CNTR_SUSPM
          5. CNTR_ERRM
          6. CNTR_SOFM
          7. CNTR_ESOFM
          8. CNTR_RESETM
          9. if (wIstr & ISTR_RESET & wInterrupt_Mask) { _SetISTR((uint16_t)CLR_RESET); Device_Property.Reset(); #ifdef RESET_CALLBACK RESET_Callback(); #endif }
        2. void USB_Istr(void)
  2. 全局量
    1. uint8_t EPindex;当前终端号 uint16_t SaveState ; uint16_t wInterrupt_Mask;
    2. DEVICE_PROP *pProperty
    3. DEVICE_INFO *pInformation
    4. DEVICE_PROP Device_Property
      1. DEVICE_PROP Device_Property = { DFU_init, //具体函数 DFU_Reset, DFU_Status_In, DFU_Status_Out, DFU_Data_Setup, DFU_NoData_Setup, DFU_Get_Interface_Setting, DFU_GetDeviceDescriptor, DFU_GetConfigDescriptor, DFU_GetStringDescriptor, 0, /*DFU_EP0Buffer*/ bMaxPacketSize0 /*Max Packet size*/ };
    5. USER_STANDARD_REQUESTS *pUser_Standard_Requests
    6. DEVICE Device_Table
      1. DEVICE Device_Table = { EP_NUM, //=1 1 };
    7. ONE_DESCRIPTOR Device_Descriptor
      1. ONE_DESCRIPTOR Device_Descriptor = { (uint8_t*)DFU_DeviceDescriptor, DFU_SIZ_DEVICE_DESC };
    8. ONE_DESCRIPTOR Config_Descriptor
      1. ONE_DESCRIPTOR Config_Descriptor = { (uint8_t*)DFU_ConfigDescriptor, DFU_SIZ_CONFIG_DESC };
    9. USER_STANDARD_REQUESTS User_Standard_Requests
      1. USER_STANDARD_REQUESTS User_Standard_Requests = { DFU_GetConfiguration, DFU_SetConfiguration, DFU_GetInterface, DFU_SetInterface, DFU_GetStatus, DFU_ClearFeature, DFU_SetEndPointFeature, DFU_SetDeviceFeature, DFU_SetDeviceAddress };
    10. ONE_DESCRIPTOR DFU_String_Descriptor[6]
      1. ONE_DESCRIPTOR DFU_String_Descriptor[6] = { { (uint8_t*)DFU_StringLangId, DFU_SIZ_STRING_LANGID }, { (uint8_t*)DFU_StringVendor, DFU_SIZ_STRING_VENDOR }, { (uint8_t*)DFU_StringProduct, DFU_SIZ_STRING_PRODUCT }, { (uint8_t*)DFU_StringSerial, DFU_SIZ_STRING_SERIAL }, { (uint8_t*)DFU_StringInterface0, DFU_SIZ_STRING_INTERFACE0 }, { (uint8_t*)DFU_StringInterface1, DFU_SIZ_STRING_INTERFACE1 } };
    11. DEVICE_INFO Device_Info; 该变量没有在定义时赋值, 更像一个状态量
  3. 数据结构usbcore.h
    1. DEVICE_PROP
      1. typedef struct _DEVICE_PROP { void (*Init)(void); /* Initialize the device */ void (*Reset)(void); /* Reset routine of this device */ void (*Process_Status_IN)(void); void (*Process_Status_OUT)(void); RESULT (*Class_Data_Setup)(uint8_t RequestNo); RESULT (*Class_NoData_Setup)(uint8_t RequestNo); RESULT (*Class_Get_Interface_Setting)(uint8_t Interface, uint8_t AlternateSetting); uint8_t* (*GetDeviceDescriptor)(uint16_t Length); uint8_t* (*GetConfigDescriptor)(uint16_t Length); uint8_t* (*GetStringDescriptor)(uint16_t Length); uint8_t* RxEP_buffer; uint8_t MaxPacketSize; }DEVICE_PROP;
    2. DEVICE_INFO
      1. typedef struct _DEVICE_INFO { uint8_t USBbmRequestType; /* bmRequestType */ uint8_t USBbRequest; /* bRequest */ uint16_t_uint8_t USBwValues; /* wValue */ uint16_t_uint8_t USBwIndexs; /* wIndex */ uint16_t_uint8_t USBwLengths; /* wLength */ uint8_t ControlState; /* of type CONTROL_STATE */ uint8_t Current_Feature; uint8_t Current_Configuration; /* Selected configuration */ uint8_t Current_Interface; /* Selected interface of current configuration */ uint8_t Current_AlternateSetting;/* Selected Alternate Setting of current*/ ENDPOINT_INFO Ctrl_Info; }DEVICE_INFO;
    3. ONE_DESCRIPTOR, *PONE_DESCRIPTOR;
      1. 数组指针; 数组大小;
      2. typedef struct OneDescriptor { uint8_t *Descriptor; uint16_t Descriptor_Size; }
    4. USER_STANDARD_REQUESTS
      1. typedef struct _USER_STANDARD_REQUESTS { void (*User_GetConfiguration)(void); /* Get Configuration */ void (*User_SetConfiguration)(void); /* Set Configuration */ void (*User_GetInterface)(void); /* Get Interface */ void (*User_SetInterface)(void); /* Set Interface */ void (*User_GetStatus)(void); /* Get Status */ void (*User_ClearFeature)(void); /* Clear Feature */ void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */ void (*User_SetDeviceFeature)(void); /* Set Device Feature */ void (*User_SetDeviceAddress)(void); /* Set Device Address */ } USER_STANDARD_REQUESTS;
    5. ENDPOINT_INFO
      1. typedef struct _ENDPOINT_INFO { uint16_t Usb_wLength; uint16_t Usb_wOffset; uint16_t PacketSize; uint8_t *(*CopyData)(uint16_t Length); }ENDPOINT_INFO;