This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 51ffa625a7c7d7ffa5e2ae6b5a0e21e005dc4ebe
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Thu Aug 18 12:40:37 2022 +0200

    tinyusb: Add cdc common package
    
    TinyUSB can handle several CDC interfaces at once but there
    is only onc set of weak functions callbacks.
    So far only USB console was implementing CDC TinyUSB interface.
    To be able to have more interfaces (several COM ports on Windows,
    or several /dev/ttyACMx devices on Linux) cdc package provides
    way to register more CDC interface clients.
    Each client has to provide own set of callbacks and registers it
    with cdc_itf_add() function.
---
 hw/usb/tinyusb/cdc/include/cdc/cdc.h |  57 +++++++++++++++++++
 hw/usb/tinyusb/cdc/pkg.yml           |  31 +++++++++++
 hw/usb/tinyusb/cdc/src/cdc.c         | 105 +++++++++++++++++++++++++++++++++++
 3 files changed, 193 insertions(+)

diff --git a/hw/usb/tinyusb/cdc/include/cdc/cdc.h 
b/hw/usb/tinyusb/cdc/include/cdc/cdc.h
new file mode 100644
index 000000000..d8a3789e3
--- /dev/null
+++ b/hw/usb/tinyusb/cdc/include/cdc/cdc.h
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __CDC_H__
+#define __CDC_H__
+
+#include <inttypes.h>
+#include <stdbool.h>
+#include <class/cdc/cdc_device.h>
+
+struct cdc_itf;
+typedef struct cdc_itf cdc_itf_t;
+
+struct cdc_callbacks {
+    /* Invoked when received new data */
+    void (*cdc_rx_cb)(cdc_itf_t *itf);
+
+    /* Invoked when received `wanted_char` */
+    void (*cdc_rx_wanted_cb)(cdc_itf_t *itf, char wanted_char);
+
+    /* Invoked when space becomes available in TX buffer */
+    void (*cdc_tx_complete_cb)(cdc_itf_t *itf);
+
+    /* Invoked when line state DTR & RTS are changed via 
SET_CONTROL_LINE_STATE */
+    void (*cdc_line_state_cb)(cdc_itf_t *itf, bool dtr, bool rts);
+
+    /* Invoked when line coding is change via SET_LINE_CODING */
+    void (*cdc_line_coding_cb)(cdc_itf_t *itf, cdc_line_coding_t const 
*p_line_coding);
+
+    /* Invoked when received send break */
+    void (*cdc_send_break_cb)(cdc_itf_t *itf, uint16_t duration_ms);
+};
+
+struct cdc_itf {
+    const struct cdc_callbacks *callbacks;
+    uint8_t cdc_num;
+};
+
+uint8_t cdc_itf_add(cdc_itf_t *cdc_ift);
+
+#endif /* __CDC_H__ */
diff --git a/hw/usb/tinyusb/cdc/pkg.yml b/hw/usb/tinyusb/cdc/pkg.yml
new file mode 100644
index 000000000..49a6c2335
--- /dev/null
+++ b/hw/usb/tinyusb/cdc/pkg.yml
@@ -0,0 +1,31 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/usb/tinyusb/cdc
+pkg.description: USB CDC.
+pkg.author: "Apache Mynewt <[email protected]>"
+pkg.homepage: "http://mynewt.apache.org/";
+pkg.keywords:
+    - usb
+
+pkg.deps:
+    - "@apache-mynewt-core/hw/hal"
+    - "@apache-mynewt-core/kernel/os"
+    - "@apache-mynewt-core/hw/usb/tinyusb"
+    - "@tinyusb/tinyusb"
diff --git a/hw/usb/tinyusb/cdc/src/cdc.c b/hw/usb/tinyusb/cdc/src/cdc.c
new file mode 100644
index 000000000..ac7f2a1ef
--- /dev/null
+++ b/hw/usb/tinyusb/cdc/src/cdc.c
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <cdc/cdc.h>
+
+static cdc_itf_t *cdc_itfs[CFG_TUD_CDC];
+static uint8_t cdc_itf_count;
+
+/* Invoked when received new data */
+void
+tud_cdc_rx_cb(uint8_t itf)
+{
+    cdc_itf_t *cdc_itf = cdc_itfs[itf];
+
+    if (cdc_itf->callbacks->cdc_rx_cb) {
+        cdc_itf->callbacks->cdc_rx_cb(cdc_itf);
+    }
+}
+
+/* Invoked when received `wanted_char` */
+void
+tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
+{
+    cdc_itf_t *cdc_itf = cdc_itfs[itf];
+
+    if (cdc_itf->callbacks->cdc_rx_wanted_cb) {
+        cdc_itf->callbacks->cdc_rx_wanted_cb(cdc_itf, wanted_char);
+    }
+}
+
+/* Invoked when space becomes available in TX buffer */
+void
+tud_cdc_tx_complete_cb(uint8_t itf)
+{
+    cdc_itf_t *cdc_itf = cdc_itfs[itf];
+
+    if (cdc_itf->callbacks->cdc_tx_complete_cb) {
+        cdc_itf->callbacks->cdc_tx_complete_cb(cdc_itf);
+    }
+}
+
+/* Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE */
+void
+tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
+{
+    cdc_itf_t *cdc_itf = cdc_itfs[itf];
+
+    if (cdc_itf->callbacks->cdc_line_state_cb) {
+        cdc_itf->callbacks->cdc_line_state_cb(cdc_itf, dtr, rts);
+    }
+}
+
+/* Invoked when line coding is change via SET_LINE_CODING */
+void
+tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const *p_line_coding)
+{
+    cdc_itf_t *cdc_itf = cdc_itfs[itf];
+
+    if (cdc_itf->callbacks->cdc_line_coding_cb) {
+        cdc_itf->callbacks->cdc_line_coding_cb(cdc_itf, p_line_coding);
+    }
+}
+
+/* Invoked when received send break */
+void
+tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms)
+{
+    cdc_itf_t *cdc_itf = cdc_itfs[itf];
+
+    if (cdc_itf->callbacks->cdc_send_break_cb) {
+        cdc_itf->callbacks->cdc_send_break_cb(cdc_itf, duration_ms);
+    }
+}
+
+uint8_t
+cdc_itf_add(cdc_itf_t *cdc_itf)
+{
+    int sr;
+
+    assert(cdc_itf_count < CFG_TUD_CDC);
+
+    OS_ENTER_CRITICAL(sr);
+    cdc_itfs[cdc_itf_count] = cdc_itf;
+    cdc_itf->cdc_num = cdc_itf_count++;
+    OS_EXIT_CRITICAL(sr);
+
+    return cdc_itf->cdc_num;
+}

Reply via email to