1
0
Dmitry 9 жил өмнө
parent
commit
175bf70e0b
4 өөрчлөгдсөн 22 нэмэгдсэн , 18 устгасан
  1. 2 2
      inc/usb_dfu.h
  2. 7 7
      inc/usbd_core.h
  3. 11 7
      src/usbd_core.c
  4. 2 2
      usb.h

+ 2 - 2
inc/usb_dfu.h

@@ -44,7 +44,7 @@
 #define USB_DFU_UPLOAD                  0x02    /**< This request is employed by the host to solicit firmware from the device.*/
 #define USB_DFU_GETSTATUS               0x03    /**< The host employs this request to facilitate synchronization with the device. \see usb_dfu_status */
 #define USB_DFU_CLRSTATAUS              0x04    /**< This request resets DFU machine state to DFU_IDLE*/
-#define USB_DFU_GETSATAE                0x05    /**< This request solicits a report about the state of the device. */
+#define USB_DFU_GETSTATE                0x05    /**< This request solicits a report about the state of the device. */
 #define USB_DFU_ABORT                   0x06    /**< This request enables the host to exit from certain states and return to the DFU_IDLE state*/
 /** @} */
 
@@ -62,7 +62,7 @@
 #define USB_DFU_STATUS_OK               0x00 /**< No error condition is present. */
 #define USB_DFU_STATUS_ERR_TARGET       0x01 /**< File is not targeted for use by this device. */
 #define USB_DFU_STATUS_ERR_FILE         0x02 /**< File is for this device but fails some vendor specific verification test. */
-#define USB_DFU_STATIS_ERR_WRITE        0x03 /**< Device is unable to write memory. */
+#define USB_DFU_STATUS_ERR_WRITE        0x03 /**< Device is unable to write memory. */
 #define USB_DFU_STATUS_ERR_ERASE        0x04 /**< Memory erase function failed. */
 #define USB_DFU_STATUS_ERR_CHECK_ERASED 0x05 /**< Memory erase check failed. */
 #define USB_DFU_STATUS_ERR_PROG         0x06 /**< Program memory function failed. */

+ 7 - 7
inc/usbd_core.h

@@ -106,9 +106,9 @@ enum usbd_commands {
     usbd_cmd_reset,             /**< Resets device */
 };
 
-typedef struct usbd_device usbd_device;
-typedef struct usbd_ctlreq usbd_ctlreq;
-typedef struct usbd_status usbd_status;
+typedef struct _usbd_device usbd_device;
+typedef struct _usbd_ctlreq usbd_ctlreq;
+typedef struct _usbd_status usbd_status;
 
 /**\name USB Device user callbacks function prototypes
  * @{ */
@@ -247,18 +247,18 @@ typedef uint16_t (*usbd_hw_get_serialno)(void *buffer);
 /** @} */
 
 /** Represents generic USB control request */
-struct usbd_ctlreq {
+struct _usbd_ctlreq {
     uint8_t     bmRequestType;  /**< This bitmapped field identifies the characteristics of the specific request. */
     uint8_t     bRequest;       /**< This field specifies the particular request. */
     uint16_t    wValue;         /**< It is used to pass a parameter to the device, specific to the request. */
     uint16_t    wIndex;         /**< It is used to pass a parameter to the device, specific to the request. */
     uint16_t    wLength;        /**< This field specifies the length of the data transferred during the second phase of the control transfer */
     uint8_t     data[];         /**< Request data payload */
-} __attribute__((packed));
+};
 
 
 /** USB device status data for control endpoint */
-struct usbd_status {
+struct _usbd_status {
     void        *data_buf;
     void        *data_ptr;      /**< Pointer to control endpoint current data buffer */
     uint16_t    data_count;     /**< Control endpoint data counter */
@@ -290,7 +290,7 @@ struct usbd_driver {
  *  \note use helper \ref usbd_device typedef
  *  \note structure must be aligned
  */
-struct usbd_device {
+struct _usbd_device {
     const struct usbd_driver    *driver;
     usbd_ctl_callback           control_callback;
     usbd_ctl_complete           complete_callback;

+ 11 - 7
src/usbd_core.c

@@ -240,7 +240,6 @@ static void usbd_process_eptx(usbd_device *dev, uint8_t ep) {
     }
 }
 
-
 /** \brief Control endpoint RX event processing
  * \param dev pointer to usb device
  * \param ep endpoint number
@@ -282,13 +281,11 @@ do_process_request:
         /* usb request received */
         /* let's handle it */
         /* preparing */
-        ep |= 0x80;
         dev->status.data_ptr = req->data;
         dev->status.data_count = dev->status.data_maxsize;
         if (usbd_process_request(dev, req)) {
             if (req->bmRequestType & USB_REQ_DEVTOHOST) {
                 /* return data from function */
-                dev->status.control_state = usbd_ctl_txdata;
                 if (dev->status.data_count >= req->wLength) {
                     dev->status.data_count = req->wLength;
                     dev->status.control_state = usbd_ctl_txdata;
@@ -297,11 +294,11 @@ do_process_request:
                     /* ZLP maybe wanted */
                     dev->status.control_state = usbd_ctl_ztxdata;
                 }
-                return usbd_process_eptx(dev, ep);
+                return usbd_process_eptx(dev, ep | 0x80);
             } else {
                 /* confirming by ZLP in STATUS_IN stage */
                 dev->status.control_state = usbd_ctl_statusin;
-                dev->driver->ep_write(ep, 0, 0);
+                dev->driver->ep_write(ep | 0x80, 0, 0);
             }
         } else {
             /* unsupported function. Let's send STALL_PID */
@@ -360,15 +357,22 @@ static void usbd_process_evt(usbd_device *dev, uint8_t evt, uint8_t ep) {
     if (dev->events[evt]) dev->events[evt](dev, evt, ep);
 }
 
+inline static void __memclr(void* ptr, uint32_t sz) {
+    do {
+        *(uint8_t*)ptr = 0x00;
+    } while (--sz);
+}
+
 
 
 void usbd_init(usbd_device *dev, const struct usbd_driver *drv, const uint8_t ep0size, void *buffer, const uint16_t bsize) {
-    memset(dev, 0, sizeof(struct usbd_device));
+    //memset(dev, 0, sizeof(usbd_device));
+    __memclr(dev, sizeof(usbd_device));
     dev->driver = drv;
     dev->status.ep0size = ep0size;
     dev->status.data_ptr = buffer;
     dev->status.data_buf = buffer;
-    dev->status.data_maxsize = bsize - offsetof(struct usbd_ctlreq, data);
+    dev->status.data_maxsize = bsize - offsetof(usbd_ctlreq, data);
 }
 
 void usbd_poll(usbd_device *dev) {

+ 2 - 2
usb.h

@@ -27,8 +27,8 @@
     #define USE_STMV0A_DRIVER
 #elif defined(STM32L052xx)
     #define USE_STMV0_DRIVER
-#elif defined(STM32L053xx) || defined(STM32F042xx) || defined(STM32F072xx) ||
-      defined(STM32L432xx) || defined(STM32L442xx) || defined(STM32L433xx) ||
+#elif defined(STM32L053xx) || defined(STM32F042xx) || defined(STM32F072xx) || \
+      defined(STM32L432xx) || defined(STM32L442xx) || defined(STM32L433xx) || \
       defined(STM32L443xx)
     #define USE_STMV0_DRIVER
     #warning "Driver has not been tested with this MPU"