You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2024/06/10 10:12:47 UTC

(mynewt-core) branch master updated (7939d1e50 -> d0e995403)

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

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


    from 7939d1e50 ci: Update ignored BSP list for bootloader build test
     new 1ac7f5e80 util/stream: Add pumping functions
     new d0e995403 i2s: Add stream for i2s_out

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../i2s_nrfx.h => include/i2s/i2s_stream.h}        | 27 ++++---
 hw/drivers/i2s/src/i2s_stream.c                    | 93 ++++++++++++++++++++++
 util/stream/include/stream/stream.h                | 52 +++++++++++-
 util/stream/src/stream.c                           | 29 +++++++
 4 files changed, 186 insertions(+), 15 deletions(-)
 copy hw/drivers/i2s/{i2s_nrfx/include/i2s_nrfx/i2s_nrfx.h => include/i2s/i2s_stream.h} (67%)
 create mode 100644 hw/drivers/i2s/src/i2s_stream.c


(mynewt-core) 02/02: i2s: Add stream for i2s_out

Posted by je...@apache.org.
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 d0e99540372d50b961f2b139937e566cef35d715
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Fri May 24 14:13:20 2024 +0200

    i2s: Add stream for i2s_out
    
    Code provides wrapper to use i2s with streaming API
    
    Signed-off-by: Jerzy Kasenberg <je...@codecoup.pl>
---
 hw/drivers/i2s/include/i2s/i2s_stream.h | 46 ++++++++++++++++
 hw/drivers/i2s/src/i2s_stream.c         | 93 +++++++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+)

diff --git a/hw/drivers/i2s/include/i2s/i2s_stream.h b/hw/drivers/i2s/include/i2s/i2s_stream.h
new file mode 100644
index 000000000..83849a1ab
--- /dev/null
+++ b/hw/drivers/i2s/include/i2s/i2s_stream.h
@@ -0,0 +1,46 @@
+/*
+ * 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 _HW_DRIVERS_I2S_STREAM_H
+#define _HW_DRIVERS_I2S_STREAM_H
+
+#include <stdint.h>
+#include <i2s/i2s.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct i2s_out_stream {
+    struct out_stream ostream;
+    struct i2s *i2s;
+    struct i2s_sample_buffer *buffer;
+};
+
+#define I2S_OUT_STREAM_DEF(var) \
+    extern const struct out_stream_vft i2s_out_stream_vft; \
+    struct i2s_out_stream var = { \
+        OSTREAM_INIT(i2s_out_stream, ostream), \
+    };
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _HW_DRIVERS_I2S_STREAM_H */
diff --git a/hw/drivers/i2s/src/i2s_stream.c b/hw/drivers/i2s/src/i2s_stream.c
new file mode 100644
index 000000000..93a4e8a13
--- /dev/null
+++ b/hw/drivers/i2s/src/i2s_stream.c
@@ -0,0 +1,93 @@
+/*
+ * 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 <os/util.h>
+#include <stream/stream.h>
+#include <i2s/i2s.h>
+#include <i2s/i2s_stream.h>
+
+OSTREAM_DEF(i2s_out_stream);
+
+static int
+i2s_out_stream_pump_from(struct out_stream *ostream, struct in_stream *istream, uint32_t count)
+{
+    struct i2s_out_stream *i2s_str = CONTAINER_OF(ostream, struct i2s_out_stream, ostream);
+    struct i2s_sample_buffer *buffer = i2s_str->buffer;
+    struct i2s *i2s = i2s_str->i2s;
+    int written = 0;
+    uint32_t available = istream_available(istream);
+    uint32_t sample_count;
+
+    if (available < count) {
+        count = available;
+    }
+    sample_count = count / i2s->sample_size_in_bytes;
+
+    while (sample_count) {
+        if (buffer == NULL) {
+            buffer = i2s_buffer_get(i2s, 0);
+            if (buffer == NULL) {
+                break;
+            }
+            buffer->sample_count = 0;
+        }
+        uint32_t space = buffer->capacity - buffer->sample_count;
+        size_t copied = min(space, sample_count);
+        if (copied) {
+            uint8_t *buf = buffer->sample_data;
+            istream_read(istream, buf + buffer->sample_count * i2s->sample_size_in_bytes,
+                         copied * i2s->sample_size_in_bytes);
+            buffer->sample_count += copied;
+            written += copied;
+            sample_count -= copied;
+        }
+        if (buffer->sample_count >= buffer->capacity) {
+            i2s_buffer_put(i2s, buffer);
+            buffer = NULL;
+        }
+    }
+    i2s_str->buffer = buffer;
+    return written * i2s_str->i2s->sample_size_in_bytes;
+}
+
+static int
+i2s_out_stream_write(struct out_stream *ostream, const uint8_t *buf, uint32_t count)
+{
+    struct mem_in_stream mstr;
+    mem_istream_init(&mstr, buf, count);
+
+    return i2s_out_stream_pump_from(ostream, (struct in_stream *)&mstr, count);
+}
+
+static int
+i2s_out_stream_flush(struct out_stream *ostream)
+{
+    struct i2s_out_stream *str = CONTAINER_OF(ostream, struct i2s_out_stream, ostream);
+    struct i2s *i2s = str->i2s;
+    struct i2s_sample_buffer *buffer = str->buffer;
+
+    str->buffer = NULL;
+    if (buffer) {
+        i2s_buffer_put(i2s, buffer);
+    }
+
+    return 0;
+}


(mynewt-core) 01/02: util/stream: Add pumping functions

Posted by je...@apache.org.
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 1ac7f5e802512b8cf565aee7da049907396c80a3
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Fri May 24 14:11:20 2024 +0200

    util/stream: Add pumping functions
    
    This extends in_stream and out_stream to provide pumping
    functions
    
    Additionally stream_pump() function is added to allow easy
    pumping data between streams.
    
    Signed-off-by: Jerzy Kasenberg <je...@codecoup.pl>
---
 util/stream/include/stream/stream.h | 52 +++++++++++++++++++++++++++++++++++--
 util/stream/src/stream.c            | 29 +++++++++++++++++++++
 2 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/util/stream/include/stream/stream.h b/util/stream/include/stream/stream.h
index 209c660ce..61137951f 100644
--- a/util/stream/include/stream/stream.h
+++ b/util/stream/include/stream/stream.h
@@ -61,6 +61,17 @@ struct in_stream_vft {
      *         negative error code
      */
     int (*available)(struct in_stream *istream);
+
+    /**
+     * Pump data from this in_stream to other out_stream
+     *
+     * @param istream - this input stream to get data from
+     * @param ostream - output stream to put data to
+     * @param count - number of bytes to pump
+     * @return non-negative number of bytes written
+     *         negative error code
+     */
+    int (*pump_to)(struct in_stream *istream, struct out_stream *ostream, uint32_t count);
 };
 
 /* Output stream functions */
@@ -83,6 +94,17 @@ struct out_stream_vft {
      *         negative error code
      */
     int (*flush)(struct out_stream *ostream);
+
+    /**
+     * Pump data from in_stream to this out_stream
+     *
+     * @param ostream - this output stream to write data to
+     * @param istream - other input stream to get data from
+     * @param count - number for bytes to pump
+     * @return non-negative number of bytes written
+     *         negative error code
+     */
+    int (*pump_from)(struct out_stream *ostream, struct in_stream *istream, uint32_t count);
 };
 
 /* Plain input stream */
@@ -102,15 +124,39 @@ struct mem_in_stream {
     uint32_t read_ptr;
 };
 
+#define OSTREAM_DEF(type) \
+    static int type ## _write(struct out_stream *ostream, const uint8_t *buf, uint32_t count); \
+    static int type ## _flush(struct out_stream *ostream); \
+    static int type ## _pump_from(struct out_stream *ostream, struct in_stream *istream, uint32_t count); \
+    const struct out_stream_vft type ## _vft = { \
+        .write = type ## _write, \
+        .flush = type ## _flush, \
+        .pump_from = type ## _pump_from, \
+    }
+
+#define OSTREAM_VFT(type, _write, _flush, _pump) \
+    static int _write(struct out_stream *ostream, const uint8_t *buf, uint32_t count); \
+    static int _flush(struct out_stream *ostream); \
+    static int _pump_from(struct out_stream *ostream, struct in_stream *istream, uint32_t count); \
+    const struct out_stream_vft type ## _vft = { \
+        .write = _write, \
+        .flush = _flush, \
+        .pump_from = _pump_from, \
+    }
+
 #define OSTREAM(type, name)     \
     struct type name = {        \
         .vft = &type ## _vft    \
     }
 
+#define OSTREAM_INIT(type, field)     \
+    .field.vft = &type ## _vft
+
 #define ISTREAM_DEF(type)                       \
     const struct in_stream_vft type ## _vft = { \
         .available = type ## _available,        \
         .read = type ## _read,                  \
+        .pump_to = type ## _pump_to,            \
     }
 
 #define ISTREAM(type, name)     \
@@ -118,8 +164,8 @@ struct mem_in_stream {
         .vft = &type ## _vft    \
     }
 
-#define ISTREAM_INIT(type)     \
-    .type.vft = &type ## _vft  \
+#define ISTREAM_INIT(type, field)     \
+    .field.vft = &type ## _vft  \
 
 /**
  * Check how many bytes can be read out of stream.
@@ -175,6 +221,8 @@ int ostream_flush(struct out_stream *ostream);
  */
 int ostream_write(struct out_stream *ostream, const uint8_t *buf, uint32_t count, bool flush);
 
+int stream_pump(struct in_stream *istream, struct out_stream *ostream, uint32_t count);
+
 /**
  * Initialize memory input stream
  *
diff --git a/util/stream/src/stream.c b/util/stream/src/stream.c
index e0ce166fe..fe7605157 100644
--- a/util/stream/src/stream.c
+++ b/util/stream/src/stream.c
@@ -20,6 +20,7 @@
 #include <string.h>
 
 #include <stream/stream.h>
+#include "os/os.h"
 
 int
 istream_flush(struct in_stream *istream)
@@ -107,3 +108,31 @@ istream_read(struct in_stream *istream, uint8_t *buf, uint32_t count)
     return istream->vft->read(istream, buf, count);
 }
 
+int
+stream_pump(struct in_stream *istream, struct out_stream *ostream, uint32_t count)
+{
+    int pumped;
+
+    /* If output stream has pump_from function used it */
+    if (ostream->vft->pump_from) {
+        pumped = ostream->vft->pump_from(ostream, istream, count);
+    } else if (istream->vft->pump_to) {
+        /* When input stream has pump_to used it */
+        pumped = istream->vft->pump_to(istream, ostream, count);
+    } else {
+        /* Both stream don't provide pump functions, use local buffer for pumping data */
+        uint8_t buf[16];
+        pumped = 0;
+        while (count) {
+            uint32_t n = min(count, sizeof(buf));
+            uint32_t r = istream_read(istream, buf, n);
+            pumped += r;
+            count -= r;
+            ostream_write(ostream, buf, r, false);
+            if (r == 0) {
+                break;
+            }
+        }
+    }
+    return pumped;
+}