Skip to content

Commit 89dd228

Browse files
committed
CurieI2S add examples
-add basic examples using callback functions/interrupts -add callback function to fill tx buffer
1 parent 2bca6fc commit 89dd228

File tree

4 files changed

+146
-12
lines changed

4 files changed

+146
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* A simple sketch to test the rx channel of the i2s interface.
3+
* A callback function is used to fill up a buffer whenever data is received
4+
*
5+
* To test this sketch you will need a second Arduino/Genuino 101 board with the I2S_TxCallback sketch uploaded
6+
*
7+
* Connection:
8+
* I2S_RSCK(pin 8) -> I2S_TSCK(pin 2)
9+
* I2S_RWS(pin 3) -> I2S_TWS(pin 4)
10+
* I2S_RXD(pin 5) -> I2S_TXD(pin 7)
11+
*
12+
**/
13+
#include <CurieI2S.h>
14+
15+
uint32_t dataBuff[256];
16+
volatile int count = 0;
17+
18+
void setup()
19+
{
20+
// put your setup code here, to run once:
21+
Serial.begin(115200);
22+
while(!Serial);
23+
Serial.println("CurieI2S Rx Callback Example");
24+
CurieI2S.begin(I2S_44K, I2S_32bit);
25+
CurieI2S.setI2SMode(PHILIPS_MODE);
26+
CurieI2S.attachRxInterrupt(rxDataReceived);
27+
CurieI2S.initRX();
28+
CurieI2S.startRX();
29+
30+
}
31+
32+
void loop()
33+
{
34+
if(count>0)
35+
{
36+
for(int i =0; i < count; i++)
37+
{
38+
Serial.print("data: ");
39+
Serial.println(dataBuff[i], HEX);
40+
}
41+
count = 0;
42+
}
43+
delay(500);
44+
}
45+
46+
//This function is called inside an ISR so it is important to make this as atomic/fast as possible
47+
void rxDataReceived()
48+
{
49+
while(CurieI2S.available())
50+
{
51+
dataBuff[count++] = CurieI2S.requestdword();
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <CurieI2S.h>
2+
3+
//I2S_TX -> Pin 7
4+
//I2S_TSK -> Pin 4
5+
//I2S_TSCK -> pin 2
6+
7+
void setup()
8+
{
9+
Serial.begin(115200);
10+
while(!Serial);
11+
Serial.println("CurieI2S Tx Callback");
12+
CurieI2S.begin(I2S_44K, I2S_32bit);
13+
CurieI2S.setI2SMode(PHILIPS_MODE);
14+
CurieI2S.attachTxEmptyInterrupt(doneTX);
15+
CurieI2S.attachTxInterrupt(fillTxBuffer);
16+
CurieI2S.initTX();
17+
}
18+
19+
void loop()
20+
{
21+
Serial.println("+++");
22+
23+
//start filling the tx buffer
24+
CurieI2S.pushData(0xFFFFFFFF);
25+
CurieI2S.pushData(0x00000000);
26+
CurieI2S.pushData(0xDEADFACE);
27+
CurieI2S.pushData(0x10101010);
28+
Serial.println("start transmitting");
29+
//Start Transmission
30+
CurieI2S.startTX();
31+
for(int i = 0; i < 40; i++)
32+
{
33+
//keep filling the buffer
34+
while(!CurieI2S.pushData(0xFFFFFFFF));
35+
while(!CurieI2S.pushData(0x00000000));
36+
while(!CurieI2S.pushData(0xDEADFACE));
37+
while(!CurieI2S.pushData(0x10101010));
38+
}
39+
//Tx is automatically stopped after the tx buffer is emptied
40+
41+
delay(500);
42+
Serial.println("+++");
43+
}
44+
45+
void doneTX()
46+
{
47+
Serial.println("done transmitting");
48+
}
49+
50+
void fillTxBuffer()
51+
{
52+
//you can fill the tx buffer here if you want
53+
//CurieI2S.pushData(0xDEADDEAD);
54+
//CurieI2S.pushData(0xDEADFACE);
55+
}
56+

libraries/CurieI2S/src/CurieI2S.cpp

+21-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void i2sInterruptHandler(void)
4747
//Serial.println("rx almost full");
4848

4949
//disable RFIFO_AFULL interrupts
50-
*I2S_CID_CTRL = *I2S_CID_CTRL & 0x7FFFFFFF;
50+
//*I2S_CID_CTRL = *I2S_CID_CTRL & 0x7FFFFFFF;
5151

5252
int index;
5353
int fifoDataLength = (*I2S_RFIFO_STAT & 0x0000000F);
@@ -67,7 +67,7 @@ static void i2sInterruptHandler(void)
6767
#endif
6868

6969
}
70-
70+
7171
//enable RFIFO_EMPTY interrupt
7272
*I2S_CID_CTRL = *I2S_CID_CTRL | 0x10000000;
7373

@@ -124,6 +124,9 @@ static void i2sInterruptHandler(void)
124124
//digitalWrite(I2S_DEBUG_PIN, LOW);
125125
#endif
126126

127+
//call tx callback
128+
CurieI2S.i2s_tx_callback();
129+
127130
if(_i2s_Tx_BufferPtr->head != _i2s_Tx_BufferPtr->tail)
128131
{
129132
int index = _i2s_Tx_BufferPtr->tail;
@@ -227,8 +230,8 @@ static void i2sInterruptHandler(void)
227230
i2s_stat = i2s_stat & 0xFFFFF0FF;
228231
*I2S_STAT = i2s_stat | 0x00000001;
229232

230-
//call rx callback
231-
CurieI2S.i2s_tx_callback();
233+
//call tx empty callback
234+
CurieI2S.i2s_tx_empty_callback();
232235

233236
//enable TFIFO_AEMPTY and TFIFO_FULL interrupts
234237
*I2S_CID_CTRL = *I2S_CID_CTRL | 0x06000000;
@@ -263,8 +266,9 @@ static void i2sInterruptHandler(void)
263266

264267
Curie_I2S::Curie_I2S()
265268
{
266-
i2s_txCB = NULL;
267269
i2s_rxCB = NULL;
270+
i2s_txEmptyCB = NULL;
271+
i2s_txCB = NULL;
268272
}
269273

270274
void Curie_I2S::begin(uint32_t sampleRate, uint32_t resolution)
@@ -391,7 +395,7 @@ void Curie_I2S::init()
391395
*I2S_CTRL = i2s_ctrl;
392396

393397
//set threshold for FIFOs
394-
*I2S_TFIFO_CTRL |= 0x00030003;
398+
*I2S_TFIFO_CTRL |= 0x00030002;
395399
*I2S_RFIFO_CTRL |= 0x00010002;
396400

397401
//enable interrupts
@@ -633,6 +637,11 @@ void Curie_I2S::attachRxInterrupt(void (*userCallBack)())
633637
i2s_rxCB = userCallBack;
634638
}
635639

640+
void Curie_I2S::attachTxEmptyInterrupt(void (*userCallBack)())
641+
{
642+
i2s_txEmptyCB = userCallBack;
643+
}
644+
636645
void Curie_I2S::attachTxInterrupt(void (*userCallBack)())
637646
{
638647
i2s_txCB = userCallBack;
@@ -644,6 +653,12 @@ inline void Curie_I2S::i2s_rx_callback(void)
644653
i2s_rxCB();
645654
}
646655

656+
inline void Curie_I2S::i2s_tx_empty_callback(void)
657+
{
658+
if(i2s_txEmptyCB != NULL)
659+
i2s_txEmptyCB();
660+
}
661+
647662
inline void Curie_I2S::i2s_tx_callback(void)
648663
{
649664
if(i2s_txCB != NULL)

libraries/CurieI2S/src/CurieI2S.h

+16-6
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ class Curie_I2S
128128
//enables i2s interrupts
129129
void enableInterrupts();
130130

131+
void (*i2s_rxCB)();
132+
131133
void (*i2s_txCB)();
132134

133-
void (*i2s_rxCB)();
135+
void (*i2s_txEmptyCB)();
134136

135137
public:
136138
Curie_I2S();
@@ -200,21 +202,29 @@ class Curie_I2S
200202

201203
uint8_t getRxFIFOLength();
202204

203-
// Attach user callback when there is data pushed into the rx buffer from the RX_FIFO
205+
// Attach user callback that is triggered when there is data pushed into the rx buffer from the RX_FIFO
204206
void attachRxInterrupt(void (*userCallBack)());
205207

206208
void detachRxInterrupt(void) { return attachTxInterrupt(NULL); };
207209

208-
// Attach user callback when that gets called when TX_FIFO is empty(transmission done);
210+
// Attach user callback that is triggered when that gets called when TX_FIFO is empty(transmission done);
211+
void attachTxEmptyInterrupt(void (*userCallBack)());
212+
213+
void detachTxEmptyInterrupt(void) { return attachTxEmptyInterrupt(NULL); };
214+
215+
// Attach user callback that is triggered when that gets called when TX_FIFO has available space;
209216
void attachTxInterrupt(void (*userCallBack)());
210217

211-
void detachTxInterrupt(void) { return attachRxInterrupt(NULL); };
218+
void detachTxInterrupt(void) { return attachTxInterrupt(NULL); };
219+
220+
//rx callback
221+
void i2s_rx_callback(void);
212222

213223
//tx callback
214224
void i2s_tx_callback(void);
215225

216-
//rx callback
217-
void i2s_rx_callback(void);
226+
//tx empty callback
227+
void i2s_tx_empty_callback(void);
218228
};
219229

220230
extern Curie_I2S CurieI2S;

0 commit comments

Comments
 (0)