Browse Source

add tuh_cdc_get_local_line_coding()

hathach 3 years ago
parent
commit
f33883c308
3 changed files with 45 additions and 4 deletions
  1. 13 2
      examples/host/cdc_msc_hid/src/cdc_app.c
  2. 20 2
      src/class/cdc/cdc_host.c
  3. 12 0
      src/class/cdc/cdc_host.h

+ 13 - 2
examples/host/cdc_msc_hid/src/cdc_app.c

@@ -90,7 +90,18 @@ void tuh_cdc_mount_cb(uint8_t idx)
   tuh_cdc_itf_info_t itf_info = { 0 };
   tuh_cdc_itf_get_info(idx, &itf_info);
 
-  printf("CDC Interface is mounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
+  printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
+
+#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
+  // CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration
+  // otherwise you need to call tuh_cdc_set_line_coding() first
+  cdc_line_coding_t line_coding = { 0 };
+  if ( tuh_cdc_get_local_line_coding(idx, &line_coding) )
+  {
+    printf("  Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
+    printf("  Parity  : %u, Data Width: %u\r\n", line_coding.parity  , line_coding.data_bits);
+  }
+#endif
 }
 
 void tuh_cdc_umount_cb(uint8_t idx)
@@ -98,5 +109,5 @@ void tuh_cdc_umount_cb(uint8_t idx)
   tuh_cdc_itf_info_t itf_info = { 0 };
   tuh_cdc_itf_get_info(idx, &itf_info);
 
-  printf("CDC Interface is unmounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
+  printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
 }

+ 20 - 2
src/class/cdc/cdc_host.c

@@ -52,8 +52,8 @@ typedef struct {
   cdc_acm_capability_t acm_capability;
   uint8_t ep_notif;
 
-  cdc_line_coding_t line_coding; // Baudrate, stop bits, parity, data width
-  uint8_t line_state; // DTR (bit0), RTS (bit1)
+  cdc_line_coding_t line_coding;  // Baudrate, stop bits, parity, data width
+  uint8_t line_state;             // DTR (bit0), RTS (bit1)
 
   tuh_xfer_cb_t user_control_cb;
 
@@ -162,6 +162,20 @@ bool tuh_cdc_get_rts(uint8_t idx)
   return (p_cdc->line_state & CDC_CONTROL_LINE_STATE_RTS) ? true : false;
 }
 
+bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding)
+{
+  cdch_interface_t* p_cdc = get_itf(idx);
+  TU_VERIFY(p_cdc);
+
+  *line_coding = p_cdc->line_coding;
+
+  return true;
+}
+
+//--------------------------------------------------------------------+
+// Write
+//--------------------------------------------------------------------+
+
 uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize)
 {
   cdch_interface_t* p_cdc = get_itf(idx);
@@ -194,6 +208,10 @@ uint32_t tuh_cdc_write_available(uint8_t idx)
   return tu_edpt_stream_write_available(&p_cdc->stream.tx);
 }
 
+//--------------------------------------------------------------------+
+// Read
+//--------------------------------------------------------------------+
+
 uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize)
 {
   cdch_interface_t* p_cdc = get_itf(idx);

+ 12 - 0
src/class/cdc/cdc_host.h

@@ -102,6 +102,12 @@ TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx)
   return tuh_cdc_get_dtr(idx);
 }
 
+// Get local (saved/cached) version of line coding.
+// This function should return correct values if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding()
+// are invoked previously or CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined.
+// NOTE: This function does not make any USB transfer request to device.
+bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding);
+
 //--------------------------------------------------------------------+
 // Write API
 //--------------------------------------------------------------------+
@@ -133,6 +139,7 @@ bool tuh_cdc_read_clear (uint8_t idx);
 
 //--------------------------------------------------------------------+
 // Control Endpoint (Request) API
+// Each Function will make a USB transfer request to/from device
 //--------------------------------------------------------------------+
 
 // Request to Set Control Line State: DTR (bit 0), RTS (bit 1)
@@ -141,6 +148,11 @@ bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_c
 // Request to Set Line Coding
 bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
 
+// Request to Get Line Coding
+// Should only use if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() never got invoked and
+// CFG_TUH_CDC_LINE_CODING_ON_ENUM is not defined
+// bool tuh_cdc_get_line_coding(uint8_t idx, cdc_line_coding_t* coding);
+
 // Connect by set both DTR, RTS
 static inline bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
 {