]> mj.ucw.cz Git - home-hw.git/commitdiff
USB: Do not forget to allocate PMA
authorMartin Mares <mj@ucw.cz>
Sat, 30 Jun 2018 21:40:07 +0000 (23:40 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 30 Jun 2018 21:40:07 +0000 (23:40 +0200)
usb/Inc/app.h
usb/Inc/usb.h
usb/Src/bmp085.c
usb/Src/main.c
usb/Src/usb.c
usb/host/test.c

index 18e58d192615a8e3a54a9c6aef2de4d05701386b..d0061dd75537289993416a175b05bc5e72c1a409 100644 (file)
@@ -18,6 +18,7 @@ void tx_packet_send(void);
 
 extern int adjusted_temp;
 extern int adjusted_press;
+extern u32 bmp_counter;
 
 extern volatile byte *bmp_i2c_ptr;
 extern volatile byte bmp_i2c_len;
index 2aea056771dd35497642f44e313e4379364599c4..79b892c01bebb53a2d71a06b36c8e08920b47a06 100644 (file)
@@ -21,14 +21,17 @@ struct usb {
   PCD_HandleTypeDef *hpcd;
   byte state;                  // USB_STATE_xxx
   byte pre_suspend_state;
-  byte address;
-  byte config;
-  byte remote_wakeup;
+  byte address;                        // Device address assigned by the host (0=none)
+  byte config;                 // Selected configuration (0=none)
+  byte remote_wakeup;          // State of remote wakeup feature
+
+  // State of endpoint 00
   byte ep0_state;              // USB_EP0_xxx
   u16 ep0_setup_data_length;
   u16 ep0_remaining_length;
   u16 ep0_total_length;
   byte ep0_buf[USB_EP0_BUF_SIZE];
+
   // Descriptor data to be filled by the user during usb_dev_reset()
   const byte *desc_device;
   const byte *desc_config;
@@ -38,6 +41,9 @@ struct usb {
   u16 desc_config_len;
   u16 desc_string_items;
   u16 desc_languages_len;
+
+  // Internal use
+  u16 last_pma_alloc;
 };
 
 enum usb_device_state {
@@ -181,11 +187,15 @@ enum usb_ep_type {
 
 // Wrappers return HAL_OK / HAL_ERROR / HAL_BUSY / HAL_TIMEOUT
 
+// Call from configure callback
 static inline HAL_StatusTypeDef usb_ep_open(struct usb *usb, byte ep_addr, byte ep_type, byte ep_max_size)
 {
+  HAL_PCDEx_PMAConfig(usb->hpcd, ep_addr, PCD_SNG_BUF, usb->last_pma_alloc);
+  usb->last_pma_alloc += (ep_max_size + 7) & ~7U;
   return HAL_PCD_EP_Open(usb->hpcd, ep_addr, ep_max_size, ep_type);
 }
 
+// Call from unconfigure callback
 static inline HAL_StatusTypeDef usb_ep_close(struct usb *usb, byte ep_addr)
 {
   return HAL_PCD_EP_Close(usb->hpcd, ep_addr);
index 9b241a6f068bb1961c01e8c9a0c986cd27c20a80..c5712c962adc4e5368839a4dd2d8e403b3591ae0 100644 (file)
@@ -166,6 +166,7 @@ static u16 raw_temp;
 static u32 raw_press;
 int adjusted_temp;
 int adjusted_press;
+u32 bmp_counter;
 
 void bmp_step(void)
 {
@@ -201,6 +202,7 @@ void bmp_step(void)
       bmp_debug("BMP: Raw pressure: %06x\n", raw_press);
       bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
       bmp_debug("BMP: Adjusted temp %d, press %d\n", adjusted_temp, adjusted_press);
+      bmp_counter++;
       bmp_state = BMP_IDLE;
       break;
     }
index 0ff373e6635e5ab6c23bbb31f3fc9f8ac678c197..e8d8abbef682c59fbc4479618ebb5cbe05e011c9 100644 (file)
@@ -157,7 +157,8 @@ int main(void)
        tx_packet_state = 1;
        put_u32_be(tx_packet, adjusted_temp);
        put_u32_be(tx_packet + 4, adjusted_press);
-       usb_ep_send(&usb, 0x82, tx_packet, 8);
+       put_u32_be(tx_packet + 8, bmp_counter);
+       usb_ep_send(&usb, 0x82, tx_packet, 12);
        rx_packet_state = 0;
        usb_ep_receive(&usb, 0x01, rx_packet, 64);
       }
index ffac01d0e768ac94c08786519b0dc0bd52d95d6e..f7b92a66ebb19829fd3c80d44b0adb09bad71e75 100644 (file)
@@ -6,6 +6,11 @@
 
 #include <string.h>
 
+// Layout of packet memory
+#define PMA_EP00 0x18
+#define PMA_EP80 0x58
+#define PMA_USER 0x98
+
 void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd)
 {
   memset(usb, 0, sizeof(*usb));
@@ -17,8 +22,8 @@ void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd)
 
 void usb_start(struct usb *usb)
 {
-  HAL_PCDEx_PMAConfig(usb->hpcd, 0x00, PCD_SNG_BUF, 0x18);
-  HAL_PCDEx_PMAConfig(usb->hpcd, 0x80, PCD_SNG_BUF, 0x58);
+  HAL_PCDEx_PMAConfig(usb->hpcd, 0x00, PCD_SNG_BUF, PMA_EP00);
+  HAL_PCDEx_PMAConfig(usb->hpcd, 0x80, PCD_SNG_BUF, PMA_EP80);
 
   HAL_PCD_Start(usb->hpcd);
 }
@@ -223,6 +228,7 @@ static void dev_set_configuration(struct usb *usb, struct setup_request *setup)
        {
          usb->config = cfg;
          usb->state = USB_STATE_CONFIGURED;
+         usb->last_pma_alloc = PMA_USER;
          usb_dev_configure(usb);
        }
       usb_ctl_send_status(usb);
@@ -239,6 +245,7 @@ static void dev_set_configuration(struct usb *usb, struct setup_request *setup)
        {
          usb_dev_unconfigure(usb);
          usb->config = cfg;
+         usb->last_pma_alloc = PMA_USER;
          usb_dev_configure(usb);
        }
       usb_ctl_send_status(usb);
index 6533c1004e1f488802cd4b9edcb108de2ff5e83c..2d94a98ec8c9e31cba8341c7a8149a7b8fcf5415 100644 (file)
@@ -87,11 +87,12 @@ int main(void)
       if (err = libusb_bulk_transfer(devh, 0x82, resp, 64, &received, 2000))
        die("Receive failed: error %d\n", err);
       // printf("Received %d bytes\n", received);
-      if (received >= 8)
+      if (received >= 12)
        {
          int t = get_u32_be(resp);
          int p = get_u32_be(resp + 4);
-         msg(L_INFO, "Temperature %d ddegC, pressure %d Pa", t, p);
+         uint cnt = get_u32_be(resp + 8);
+         msg(L_INFO, "Temperature %d ddegC, pressure %d Pa, cnt %u", t, p, cnt);
        }
 
       sleep(1);