You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2021/05/11 09:58:18 UTC
[GitHub] [mynewt-core] utzig commented on a change in pull request #2591: hw/drivers/i2s: Add I2S driver for NRF5x families
utzig commented on a change in pull request #2591:
URL: https://github.com/apache/mynewt-core/pull/2591#discussion_r630025467
##########
File path: hw/drivers/i2s/i2s_nrfx/src/i2s_nrfx.c
##########
@@ -0,0 +1,280 @@
+/*
+ * 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 <os/mynewt.h>
+#include <bsp/bsp.h>
+#include <i2s/i2s.h>
+#include <i2s/i2s_driver.h>
+#include <i2s_nrfx/i2s_nrfx.h>
+#include <nrfx/drivers/include/nrfx_i2s.h>
+
+struct i2s_nrfx {
+ nrfx_i2s_config_t nrfx_i2s_cfg;
+ bool running;
+ int8_t nrfx_queued_count;
+ struct i2s *i2s;
+ struct i2s_sample_buffer *nrfx_buffers[2];
+};
+
+static struct i2s_nrfx i2s_nrfx;
+
+static void
+nrfx_add_buffer(struct i2s *i2s, struct i2s_sample_buffer *buffer)
+{
+ nrfx_i2s_buffers_t nrfx_buffers = {0};
+ nrfx_err_t err;
+
+ assert(i2s != NULL);
+ if (buffer == NULL) {
+ return;
+ }
+
+ if (i2s->direction == I2S_OUT || i2s->direction == I2S_OUT_IN) {
+ nrfx_buffers.p_tx_buffer = buffer->sample_data;
+ }
+ if (i2s->direction == I2S_IN || i2s->direction == I2S_OUT_IN) {
+ nrfx_buffers.p_rx_buffer = buffer->sample_data;
+ }
+
+ assert(i2s_nrfx.nrfx_queued_count < 2);
+ assert(i2s_nrfx.nrfx_buffers[i2s_nrfx.nrfx_queued_count] == NULL);
+
+ i2s_nrfx.nrfx_buffers[i2s_nrfx.nrfx_queued_count] = buffer;
+ i2s_nrfx.nrfx_queued_count++;
+ if (i2s_nrfx.nrfx_queued_count == 1) {
+ i2s_driver_state_changed (i2s, I2S_STATE_RUNNING);
+ err = nrfx_i2s_start(&nrfx_buffers, buffer->sample_count * i2s->sample_size_in_bytes / 4, 0);
+ } else {
+ err = nrfx_i2s_next_buffers_set(&nrfx_buffers);
+ }
+
+ assert(err == NRFX_SUCCESS);
+}
+
+static void
+feed_nrfx(void)
+{
+ struct i2s_sample_buffer *buffer;
+
+ buffer = i2s_driver_buffer_get(i2s_nrfx.i2s);
+ nrfx_add_buffer(i2s_nrfx.i2s, buffer);
+}
+
+static void
+i2s_nrfx_data_handler(const nrfx_i2s_buffers_t *p_released, uint32_t status)
+{
+ struct i2s_sample_buffer *buffer;
+
+ TP_ON(5);
+ TP_OFF(5);
+ if (p_released != NULL &&
+ (p_released->p_rx_buffer != NULL || p_released->p_tx_buffer != NULL)) {
+ i2s_nrfx.nrfx_queued_count--;
+ assert(i2s_nrfx.nrfx_queued_count >= 0);
+ buffer = i2s_nrfx.nrfx_buffers[0];
+ assert(buffer->sample_data == p_released->p_tx_buffer || buffer->sample_data == p_released->p_rx_buffer);
+ i2s_nrfx.nrfx_buffers[0] = i2s_nrfx.nrfx_buffers[1];
+ i2s_nrfx.nrfx_buffers[1] = NULL;
+ i2s_driver_buffer_put(i2s_nrfx.i2s, buffer);
+ }
+ if (i2s_nrfx.running && i2s_nrfx.nrfx_queued_count < 2) {
+ assert(i2s_nrfx.nrfx_buffers[1] == NULL);
+ feed_nrfx();
+ }
+ if (status == NRFX_I2S_STATUS_TRANSFER_STOPPED) {
+ i2s_driver_state_changed(i2s_nrfx.i2s, I2S_STATE_STOPPED);
+ }
+}
+
+static int
+i2s_nrfx_init(struct i2s *i2s, const struct i2s_cfg *cfg)
+{
+ int rc;
+
+ i2s_nrfx.i2s = i2s;
+
+ NVIC_SetVector(nrfx_get_irq_number(NRF_I2S), (uint32_t)nrfx_i2s_irq_handler);
+
+ i2s_nrfx.nrfx_i2s_cfg = cfg->nrfx_i2s_cfg;
+ switch (cfg->nrfx_i2s_cfg.sample_width) {
+ case NRF_I2S_SWIDTH_8BIT:
+#if defined(I2S_CONFIG_SWIDTH_SWIDTH_8BitIn16)
+ case NRF_I2S_SWIDTH_8BIT_IN16BIT:
+#endif
+#if defined(I2S_CONFIG_SWIDTH_SWIDTH_8BitIn32)
+ case NRF_I2S_SWIDTH_8BIT_IN32BIT:
+#endif
+ i2s->sample_size_in_bytes = 1;
+ break;
+ case NRF_I2S_SWIDTH_16BIT:
+#if defined(I2S_CONFIG_SWIDTH_SWIDTH_16BitIn32)
+ case NRF_I2S_SWIDTH_16BIT_IN32BIT:
+#endif
+ i2s->sample_size_in_bytes = 2;
+ break;
+ case NRF_I2S_SWIDTH_24BIT:
+#if defined(I2S_CONFIG_SWIDTH_SWIDTH_24BitIn32)
+ case NRF_I2S_SWIDTH_24BIT_IN32BIT:
+#endif
+#if defined(I2S_CONFIG_SWIDTH_SWIDTH_32Bit)
+ case NRF_I2S_SWIDTH_32BIT:
+#endif
+ i2s->sample_size_in_bytes = 4;
+ break;
+ }
+
+ i2s->direction = I2S_INVALID;
+ if (cfg->nrfx_i2s_cfg.sdin_pin != NRFX_I2S_PIN_NOT_USED) {
+ i2s->direction = I2S_IN;
+ }
+ if (cfg->nrfx_i2s_cfg.sdout_pin != NRFX_I2S_PIN_NOT_USED) {
+ i2s->direction |= I2S_OUT;
+ }
+
+ rc = i2s_init(i2s, cfg->pool);
+
+ if (rc != OS_OK) {
+ nrfx_i2s_uninit();
+ goto end;
+ }
+
+ i2s->sample_rate = cfg->sample_rate;
+ i2s->driver_data = &i2s_nrfx;
+end:
+ return rc;
+}
+
+int
+i2s_create(struct i2s *i2s, const char *name, const struct i2s_cfg *cfg)
+{
+ return os_dev_create(&i2s->dev, name, OS_DEV_INIT_PRIMARY,
+ 100, (os_dev_init_func_t)i2s_nrfx_init, (void *)cfg);
+}
+
+int
+i2s_driver_stop(struct i2s *i2s)
+{
+ struct i2s_sample_buffer *buffer;
+
+ i2s_nrfx.running = false;
+ nrfx_i2s_stop();
+
+ assert(i2s_nrfx.i2s->state == I2S_STATE_STOPPED);
+
+ while (NULL != (buffer = i2s_driver_buffer_get(i2s))) {
Review comment:
Ugly comparison! :-P
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org