struct rt5677_dsp { struct device *dev; struct delayed_work copy_work; struct mutex dma_lock; struct snd_pcm_substream *substream;
size_t dma_offset; /* zero-based offset into runtime->dma_area */
size_t avail_bytes; /* number of new bytes since last period */
u32 mic_read_offset; /* zero-based offset into DSP's mic buffer */ bool new_hotword; /* a new hotword is fired */
};
staticint rt5677_spi_mic_write_offset(u32 *mic_write_offset)
{ int ret; /* Grab the first 4 bytes that hold the write pointer on the *dsp,andchecktomakesurethatitpointssomewhereinsidethe *buffer.
*/
ret = rt5677_spi_read(RT5677_MIC_BUF_ADDR, mic_write_offset, sizeof(u32)); if (ret) return ret; /* Adjust the offset so that it's zero-based */
*mic_write_offset = *mic_write_offset - sizeof(u32); return *mic_write_offset < RT5677_MIC_BUF_BYTES ? 0 : -EFAULT;
}
if (begin > end || runtime->dma_bytes < 2 * bytes_per_frame) {
dev_err(rt5677_dsp->dev, "Invalid copy from (%u, %u), dma_area size %zu\n",
begin, end, runtime->dma_bytes); return -EINVAL;
}
/* The block to copy is empty */ if (begin == end) return0;
/* If the incoming chunk is too big for the receiving buffer, only the *last"receivingbuffersize-oneframe"bytesarecopied.
*/ if (end - begin > runtime->dma_bytes - bytes_per_frame)
begin = end - (runtime->dma_bytes - bytes_per_frame);
/* May need to split to two chunks, calculate the size of each */
first_chunk_len = end - begin;
second_chunk_len = 0; if (rt5677_dsp->dma_offset + first_chunk_len > runtime->dma_bytes) { /* Receiving buffer wrapped around */
second_chunk_len = first_chunk_len;
first_chunk_len = runtime->dma_bytes - rt5677_dsp->dma_offset;
second_chunk_len -= first_chunk_len;
}
/* Copy first chunk */
ret = rt5677_spi_read(RT5677_MIC_BUF_ADDR + sizeof(u32) + begin,
runtime->dma_area + rt5677_dsp->dma_offset,
first_chunk_len); if (ret) return ret;
rt5677_dsp->dma_offset += first_chunk_len; if (rt5677_dsp->dma_offset == runtime->dma_bytes)
rt5677_dsp->dma_offset = 0;
/* Copy second chunk */ if (second_chunk_len) {
ret = rt5677_spi_read(RT5677_MIC_BUF_ADDR + sizeof(u32) +
begin + first_chunk_len, runtime->dma_area,
second_chunk_len); if (!ret)
rt5677_dsp->dma_offset = second_chunk_len;
} return ret;
}
target = rt5677_dsp->mic_read_offset + amount; /* Copy the first chunk in DSP's mic buffer */
ret |= rt5677_spi_copy_block(rt5677_dsp, rt5677_dsp->mic_read_offset,
min(target, RT5677_MIC_BUF_BYTES));
if (target >= RT5677_MIC_BUF_BYTES) { /* Wrap around, copy the second chunk */
target -= RT5677_MIC_BUF_BYTES;
ret |= rt5677_spi_copy_block(rt5677_dsp, 0, target);
}
if (!ret)
rt5677_dsp->mic_read_offset = target; return ret;
}
/* Ensure runtime->dma_area buffer does not go away while copying. */
mutex_lock(&rt5677_dsp->dma_lock); if (!rt5677_dsp->substream) {
dev_err(rt5677_dsp->dev, "No pcm substream\n"); goto done;
}
runtime = rt5677_dsp->substream->runtime;
if (rt5677_spi_mic_write_offset(&mic_write_offset)) {
dev_err(rt5677_dsp->dev, "No mic_write_offset\n"); goto done;
}
/* If this is the first time that we've asked for streaming data after *ahotwordisfired,weshouldstartreadingfromtheprevious2 *secondsofaudiofromwhereverthemic_write_offsetiscurrently.
*/ if (rt5677_dsp->new_hotword) {
rt5677_dsp->new_hotword = false; /* See if buffer wraparound happens */ if (mic_write_offset < RT5677_MIC_BUF_FIRST_READ_SIZE)
rt5677_dsp->mic_read_offset = RT5677_MIC_BUF_BYTES -
(RT5677_MIC_BUF_FIRST_READ_SIZE -
mic_write_offset); else
rt5677_dsp->mic_read_offset = mic_write_offset -
RT5677_MIC_BUF_FIRST_READ_SIZE;
}
/* Calculate the amount of new samples in bytes */ if (rt5677_dsp->mic_read_offset <= mic_write_offset)
new_bytes = mic_write_offset - rt5677_dsp->mic_read_offset; else
new_bytes = RT5677_MIC_BUF_BYTES + mic_write_offset
- rt5677_dsp->mic_read_offset;
/* Copy all new samples from DSP mic buffer, one period at a time */
period_bytes = snd_pcm_lib_period_bytes(rt5677_dsp->substream); while (new_bytes) {
copy_bytes = min(new_bytes, period_bytes
- rt5677_dsp->avail_bytes);
ret = rt5677_spi_copy(rt5677_dsp, copy_bytes); if (ret) {
dev_err(rt5677_dsp->dev, "Copy failed %d\n", ret); goto done;
}
rt5677_dsp->avail_bytes += copy_bytes; if (rt5677_dsp->avail_bytes >= period_bytes) {
snd_pcm_period_elapsed(rt5677_dsp->substream);
rt5677_dsp->avail_bytes = 0;
}
new_bytes -= copy_bytes;
}
/* Copy dstlen bytes from src to dst, while reversing byte order for each word. *Ifsrclen<dstlen,zerosarepadded.
*/ staticvoid rt5677_spi_reverse(u8 *dst, u32 dstlen, const u8 *src, u32 srclen)
{
u32 w, i, si;
u32 word_size = min_t(u32, dstlen, 8);
for (w = 0; w < dstlen; w += word_size) { for (i = 0; i < word_size && i + w < dstlen; i++) {
si = w + word_size - i - 1;
dst[w + i] = si < srclen ? src[si] : 0;
}
}
}
/* Read DSP address space using SPI. addr and len have to be 4-byte aligned. */ int rt5677_spi_read(u32 addr, void *rxbuf, size_t len)
{
u32 offset; int status = 0; struct spi_transfer t[2]; struct spi_message m; /* +4 bytes is for the DummyPhase following the AddressPhase */
u8 header[RT5677_SPI_HEADER + 4];
u8 body[RT5677_SPI_BURST_LEN];
u8 spi_cmd;
u8 *cb = rxbuf;
mutex_lock(&spi_mutex);
status |= spi_sync(g_spi, &m);
mutex_unlock(&spi_mutex);
/* Copy data back to caller buffer */
rt5677_spi_reverse(cb + offset, len - offset, body, t[1].len);
} return status;
}
EXPORT_SYMBOL_GPL(rt5677_spi_read);
/* Write DSP address space using SPI. addr has to be 4-byte aligned. *Iflenisnot4-bytealigned,thenextrazerosarewrittenattheend *aspadding.
*/ int rt5677_spi_write(u32 addr, constvoid *txbuf, size_t len)
{
u32 offset; int status = 0; struct spi_transfer t; struct spi_message m; /* +1 byte is for the DummyPhase following the DataPhase */
u8 buf[RT5677_SPI_HEADER + RT5677_SPI_BURST_LEN + 1];
u8 *body = buf + RT5677_SPI_HEADER;
u8 spi_cmd; const u8 *cb = txbuf;
staticint rt5677_spi_probe(struct spi_device *spi)
{ int ret;
g_spi = spi;
ret = devm_snd_soc_register_component(&spi->dev,
&rt5677_spi_dai_component,
&rt5677_spi_dai, 1); if (ret < 0)
dev_err(&spi->dev, "Failed to register component.\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.