Skip to content

Commit

Permalink
Put in soft resets and NULL transmit checks
Browse files Browse the repository at this point in the history
Put in soft resets of the I2S receiver for all other input objects; also protect against attempting to transmit NULL audio blocks where it wasn't checked for.
  • Loading branch information
h4yn0nnym0u5e committed Nov 23, 2021
1 parent 920c5dd commit e7f2acd
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 46 deletions.
17 changes: 12 additions & 5 deletions input_i2s2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void AudioInputI2S2::begin(void)
//block_left_1st = NULL;
//block_right_1st = NULL;

// TODO: should we set & clear the I2S_RCSR_SR bit here?
I2S2_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic
AudioOutputI2S2::config_i2s();

CORE_PIN5_CONFIG = 2; //EMC_08, 2=SAI2_RX_DATA, page 434
Expand Down Expand Up @@ -154,10 +154,17 @@ void AudioInputI2S2::update(void)
block_offset = 0;
__enable_irq();
// then transmit the DMA's former blocks
transmit(out_left, 0);
release(out_left);
transmit(out_right, 1);
release(out_right);
if (NULL != out_left)
{
transmit(out_left, 0);
release(out_left);
}

if (NULL != out_right)
{
transmit(out_right, 1);
release(out_right);
}
//Serial.print(".");
} else if (new_left != NULL) {
// the DMA didn't fill blocks, but we allocated blocks
Expand Down
5 changes: 5 additions & 0 deletions input_i2s2.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#include "AudioStream.h"
#include "DMAChannel.h"

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)


class AudioInputI2S2 : public AudioStream
{
public:
Expand Down
51 changes: 37 additions & 14 deletions input_i2s_hex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void AudioInputI2SHex::begin(void)

const int pinoffset = 0; // TODO: make this configurable...
AudioOutputI2S::config_i2s();
I2S1_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic
I2S1_RCR3 = I2S_RCR3_RCE_3CH << pinoffset;
switch (pinoffset) {
case 0:
Expand Down Expand Up @@ -195,8 +196,8 @@ void AudioInputI2SHex::update(void)
}
__disable_irq();
if (block_offset >= AUDIO_BLOCK_SAMPLES) {
// the DMA filled 4 blocks, so grab them and get the
// 4 new blocks to the DMA, as quickly as possible
// the DMA filled 6 blocks, so grab them and get the
// 6 new blocks to the DMA, as quickly as possible
out1 = block_ch1;
block_ch1 = new1;
out2 = block_ch2;
Expand All @@ -212,18 +213,40 @@ void AudioInputI2SHex::update(void)
block_offset = 0;
__enable_irq();
// then transmit the DMA's former blocks
transmit(out1, 0);
release(out1);
transmit(out2, 1);
release(out2);
transmit(out3, 2);
release(out3);
transmit(out4, 3);
release(out4);
transmit(out5, 4);
release(out5);
transmit(out6, 5);
release(out6);
if (NULL != out1)
{
transmit(out1, 0);
release(out1);
}

if (NULL != out2)
{
transmit(out2, 1);
release(out2);
}

if (NULL != out3)
{
transmit(out3, 2);
release(out3);
}
if (NULL != out4)
{
transmit(out4, 3);
release(out4);
}

if (NULL != out5)
{
transmit(out5, 4);
release(out5);
}

if (NULL != out6)
{
transmit(out6, 5);
release(out6);
}
} else if (new1 != NULL) {
// the DMA didn't fill blocks, but we allocated blocks
if (block_ch1 == NULL) {
Expand Down
4 changes: 4 additions & 0 deletions input_i2s_hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "AudioStream.h"
#include "DMAChannel.h"

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)

class AudioInputI2SHex : public AudioStream
{
public:
Expand Down
67 changes: 50 additions & 17 deletions input_i2s_oct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ void AudioInputI2SOct::begin(void)
dma.begin(true); // Allocate the DMA channel first

AudioOutputI2S::config_i2s();
I2S1_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic
I2S1_RCR3 = I2S_RCR3_RCE_4CH;

CORE_PIN8_CONFIG = 3;
CORE_PIN6_CONFIG = 3;
CORE_PIN9_CONFIG = 3;
Expand Down Expand Up @@ -203,7 +205,7 @@ void AudioInputI2SOct::update(void)
}
__disable_irq();
if (block_offset >= AUDIO_BLOCK_SAMPLES) {
// the DMA filled 4 blocks, so grab them and get the
// the DMA filled 8 blocks, so grab them and get the
// 8 new blocks to the DMA, as quickly as possible
out1 = block_ch1;
block_ch1 = new1;
Expand All @@ -224,22 +226,53 @@ void AudioInputI2SOct::update(void)
block_offset = 0;
__enable_irq();
// then transmit the DMA's former blocks
transmit(out1, 0);
release(out1);
transmit(out2, 1);
release(out2);
transmit(out3, 2);
release(out3);
transmit(out4, 3);
release(out4);
transmit(out5, 4);
release(out5);
transmit(out6, 5);
release(out6);
transmit(out7, 6);
release(out7);
transmit(out8, 7);
release(out8);
if (NULL != out1)
{
transmit(out1, 0);
release(out1);
}

if (NULL != out2)
{
transmit(out2, 1);
release(out2);
}

if (NULL != out3)
{
transmit(out3, 2);
release(out3);
}
if (NULL != out4)
{
transmit(out4, 3);
release(out4);
}

if (NULL != out5)
{
transmit(out5, 4);
release(out5);
}

if (NULL != out6)
{
transmit(out6, 5);
release(out6);
}

if (NULL != out7)
{
transmit(out7, 6);
release(out7);
}

if (NULL != out8)
{
transmit(out8, 7);
release(out8);
}

} else if (new1 != NULL) {
// the DMA didn't fill blocks, but we allocated blocks
if (block_ch1 == NULL) {
Expand Down
4 changes: 4 additions & 0 deletions input_i2s_oct.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "AudioStream.h"
#include "DMAChannel.h"

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)

class AudioInputI2SOct : public AudioStream
{
public:
Expand Down
36 changes: 27 additions & 9 deletions input_i2s_quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void AudioInputI2SQuad::begin(void)
dma.begin(true); // Allocate the DMA channel first

#if defined(KINETISK)
// TODO: should we set & clear the I2S_RCSR_SR bit here?
I2S0_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic

AudioOutputI2SQuad::config_i2s();

CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
Expand Down Expand Up @@ -79,6 +80,8 @@ void AudioInputI2SQuad::begin(void)

#elif defined(__IMXRT1062__)
const int pinoffset = 0; // TODO: make this configurable...
I2S1_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic

AudioOutputI2S::config_i2s();
I2S1_RCR3 = I2S_RCR3_RCE_2CH << pinoffset;
switch (pinoffset) {
Expand Down Expand Up @@ -227,14 +230,29 @@ void AudioInputI2SQuad::update(void)
block_offset = 0;
__enable_irq();
// then transmit the DMA's former blocks
transmit(out1, 0);
release(out1);
transmit(out2, 1);
release(out2);
transmit(out3, 2);
release(out3);
transmit(out4, 3);
release(out4);
if (NULL != out1)
{
transmit(out1, 0);
release(out1);
}

if (NULL != out2)
{
transmit(out2, 1);
release(out2);
}

if (NULL != out3)
{
transmit(out3, 2);
release(out3);
}
if (NULL != out4)
{
transmit(out4, 3);
release(out4);
}

} else if (new1 != NULL) {
// the DMA didn't fill blocks, but we allocated blocks
if (block_ch1 == NULL) {
Expand Down
4 changes: 4 additions & 0 deletions input_i2s_quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "AudioStream.h"
#include "DMAChannel.h"

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)

class AudioInputI2SQuad : public AudioStream
{
public:
Expand Down
2 changes: 2 additions & 0 deletions input_pdm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ void AudioInputPDM::begin()
*/
I2S1_RMR = 0;
//I2S1_RCSR = (1<<25); //Reset
I2S1_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic

I2S1_RCR1 = I2S_RCR1_RFW(2); // 2 not 1
I2S1_RCR2 = I2S_RCR2_SYNC(rsync) | I2S_RCR2_BCP | (I2S_RCR2_BCD | I2S_RCR2_DIV((1)) | I2S_RCR2_MSEL(1)); // sync=0; rx is async;
I2S1_RCR3 = I2S_RCR3_RCE;
Expand Down
5 changes: 5 additions & 0 deletions input_pdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
#include "AudioStream.h"
#include "DMAChannel.h"

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)


class AudioInputPDM : public AudioStream
{
public:
Expand Down
2 changes: 2 additions & 0 deletions input_pdm_i2s2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ void AudioInputPDM2::begin(void)

I2S2_TMR = 0;
//I2S2_TCSR = (1<<25); //Reset
I2S2_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic

I2S2_TCR1 = I2S_TCR1_RFW(1);
I2S2_TCR2 = I2S_TCR2_SYNC(tsync) | I2S_TCR2_BCP | (I2S_TCR2_BCD | I2S_TCR2_DIV((1)) | I2S_TCR2_MSEL(1)); // sync=0; tx is async;
I2S2_TCR3 = I2S_TCR3_TCE;
Expand Down
5 changes: 5 additions & 0 deletions input_pdm_i2s2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
#include "AudioStream.h"
#include "DMAChannel.h"

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)


class AudioInputPDM2 : public AudioStream
{
public:
Expand Down
4 changes: 4 additions & 0 deletions input_tdm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void AudioInputTDM::begin(void)
// TODO: should we set & clear the I2S_RCSR_SR bit here?
AudioOutputTDM::config_tdm();
#if defined(KINETISK)
I2S0_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic

CORE_PIN13_CONFIG = PORT_PCR_MUX(4); // pin 13, PTC5, I2S0_RXD0
dma.TCD->SADDR = &I2S0_RDR0;
dma.TCD->SOFF = 0;
Expand All @@ -65,6 +67,8 @@ void AudioInputTDM::begin(void)
I2S0_RCSR |= I2S_RCSR_RE | I2S_RCSR_BCE | I2S_RCSR_FRDE | I2S_RCSR_FR;
I2S0_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE; // TX clock enable, because sync'd to TX
#elif defined(__IMXRT1062__)
I2S1_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic

CORE_PIN8_CONFIG = 3; //RX_DATA0
IOMUXC_SAI1_RX_DATA0_SELECT_INPUT = 2;
dma.TCD->SADDR = &I2S1_RDR0;
Expand Down
5 changes: 5 additions & 0 deletions input_tdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

#define AUDIO_TDM_BLOCKS 16

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)


class AudioInputTDM : public AudioStream
{
public:
Expand Down
2 changes: 1 addition & 1 deletion input_tdm2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void AudioInputTDM2::begin(void)
{
dma.begin(true); // Allocate the DMA channel first

// TODO: should we set & clear the I2S_RCSR_SR bit here?
AudioOutputTDM2::config_tdm();
I2S2_RCSR |= I2S_RCSR_SR; // soft-reset the I2S receiver logic

CORE_PIN5_CONFIG = 2; //2:RX_DATA0
IOMUXC_SAI2_RX_DATA0_SELECT_INPUT = 0;
Expand Down
5 changes: 5 additions & 0 deletions input_tdm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@

#define AUDIO_TDM_BLOCKS 16

#if !defined(I2S_RCSR_SR) // not always in the master header
#define I2S_RCSR_SR ((uint32_t)0x01000000) // Software Reset
#endif // !defined(I2S_RCSR_SR)


class AudioInputTDM2 : public AudioStream
{
public:
Expand Down

0 comments on commit e7f2acd

Please sign in to comment.