sample and play see http://mbed.org/users/okini3939/notebook/I2S_TLV320AIC23B/
Dependencies: I2SSlave TLV320 mbed
Fork of i2s_audio_echo by
Revision 1:2aeba027f61e, committed 2012-12-11
- Comitter:
- okini3939
- Date:
- Tue Dec 11 02:12:14 2012 +0000
- Parent:
- 0:68f7b69ecca0
- Commit message:
- sampler
Changed in this revision
diff -r 68f7b69ecca0 -r 2aeba027f61e PRAM.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PRAM.cpp Tue Dec 11 02:12:14 2012 +0000 @@ -0,0 +1,79 @@ +#include "mbed.h" +#include "PRAM.h" + + +PRAM::PRAM (SPI &spi, PinName cs) : _spi(spi), _cs(cs) { + int i; + + _cs = 1; + _spi.frequency(16000000); + wait_ms(10); + + _cs = 0; + _spi.write(CMD_RDID); + printf("RAM Manufacturer ID : %02x\r\n", spi.write(0)); + i = (spi.write(0) << 8) | spi.write(0); + printf("RAM Device ID : %04x\r\n", i); + _cs = 1; +} + +PRAM::PRAM (PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi, miso, sclk), _cs(cs) { +} + +int PRAM::status () { + int r; + + _cs = 0; + _spi.write(CMD_RDSR); + r = _spi.write(0); + _cs = 1; + return r; +} + +int PRAM::write (int addr, char *buf, int len) { + int i; + + while (status() & 1) { + // write in progress + wait_us(1); + } + + _cs = 0; + _spi.write(CMD_WREN); + _cs = 1; + wait_us(1); + + _cs = 0; + _spi.write(CMD_PP_BA); + _spi.write((addr >> 16) & 0xff); + _spi.write((addr >> 8) & 0xff); + _spi.write(addr & 0xff); + + len = len - (addr & 0x3f); + for (i = 0; i < len; i ++) { + _spi.write(buf[i]); + } + _cs = 1; + return i; +} + +int PRAM::read (int addr, char *buf, int len) { + int i; + + while (status() & 1) { + // write in progress + wait_us(1); + } + + _cs = 0; + _spi.write(CMD_READ); + _spi.write((addr >> 16) & 0xff); + _spi.write((addr >> 8) & 0xff); + _spi.write(addr & 0xff); + + for (i = 0; i < len; i ++) { + buf[i] = _spi.write(0); + } + _cs = 1; + return i; +}
diff -r 68f7b69ecca0 -r 2aeba027f61e PRAM.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PRAM.h Tue Dec 11 02:12:14 2012 +0000 @@ -0,0 +1,27 @@ +#include "mbed.h" + +#define CMD_WREN 0x06 // Write enable +#define CMD_WRDI 0x04 // Write disable +#define CMD_RDID 0x9f // Read identification +#define CMD_RDSR 0x05 // Read status register +#define CMD_WRSR 0x01 // Write status registe +#define CMD_READ 0x03 // Read data bytes +#define CMD_FREAD 0x0b // Read data bytes at higher speed +#define CMD_PP 0x02 // Page program (legacy program) +#define CMD_PP_BA 0x22 // Page program (bit-alterable write) +#define CMD_PP_1S 0xd1 // Page program (On all 1s) +#define CMD_SE 0xd8 // Sector erase + +class PRAM { +public: + PRAM (PinName mosi, PinName miso, PinName sclk, PinName cs); + PRAM (SPI &spi, PinName cs); + + int status (); + int write (int addr, char *buf, int len); + int read (int addr, char *buf, int len); + +private: + SPI _spi; + DigitalOut _cs; +};
diff -r 68f7b69ecca0 -r 2aeba027f61e main.cpp --- a/main.cpp Fri Nov 23 08:24:10 2012 +0000 +++ b/main.cpp Tue Dec 11 02:12:14 2012 +0000 @@ -1,71 +1,155 @@ #include "mbed.h" #include "TLV320.h" +#include "PRAM.h" -#define BUF_SIZE 4000 +#define BLOCK_SIZE 64 +#define BLOCK_NUM 3 + +#define MODE_STOP 0 +#define MODE_REC 1 +#define MODE_PLAY 2 Serial pc(USBTX, USBRX); + +SPI _spi(p11, p12, p13); +PRAM memory(_spi, p14); // SPI PRAM + TLV320 audio(p9, p10, 0x34, p5, p6, p7, p8, p16); // I2S Codec -DigitalOut myled(LED1); + +DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); +DigitalIn play(p28), rec(p29), stop(p30); -volatile int count = 0; -int addr = 0; -int buffer[BUF_SIZE]; +volatile int mode = MODE_STOP; +volatile int addr_r, num_r; +volatile int addr_w, num_w; +int buf_r[BLOCK_NUM][BLOCK_SIZE / 4]; +int buf_w[BLOCK_NUM][BLOCK_SIZE / 4]; + +volatile int mem_r, mem_w; +volatile int flg_r = -1, flg_w = -1; + +extern "C" void HardFault_Handler() { + register unsigned int _msp __asm("msp"); + printf("Hard Fault! address: %08x\r\n", *((unsigned int *)(_msp + 24))); + printf("buf: %d %d %d\r\n", num_w, addr_w, mem_w); + exit(-1); +} // interrupt TLV320 send/recv void isr_audio () { - int i, a; - short l, r, ll, rr; + int i; int buf[4]; // I2S input audio.read(); - for (i = 0; i < 4; i ++) { - buf[i] = audio.rxBuffer[i]; + + switch (mode) { + case MODE_STOP: + for (i = 0; i < 4; i ++) { + buf[i] = audio.rxBuffer[i]; + } + audio.write(buf, 0, 4); + break; + + case MODE_REC: + for (i = 0; i < 4; i ++) { + buf[i] = audio.rxBuffer[i]; + buf_w[num_w][addr_w] = buf[i]; + addr_w ++; + } + if (addr_w >= BLOCK_SIZE / 4) { + addr_w = 0; + flg_w = num_w; + num_w ++; + if (num_w >= BLOCK_NUM) { + num_w = 0; + } + led3 = 1; + } + audio.write(buf, 0, 4); + break; + + case MODE_PLAY: + for (i = 0; i < 4; i ++) { + buf[i] = buf_r[num_r][addr_r]; + addr_r ++; + } + if (addr_r >= BLOCK_SIZE / 4) { + addr_r = 0; + num_r ++; + if (num_r >= BLOCK_NUM) { + num_r = 0; + } + flg_r = num_r + 1 < BLOCK_NUM ? num_r + 1 : 0; + led4 = 1; + } + audio.write(buf, 0, 4); + break; } - for (i = 0; i < 4; i ++) { - l = (buffer[addr + i] >> 16); - r = (buffer[addr + i] & 0xffff); - ll = (buf[i] >> 16); - rr = (buf[i] & 0xffff); - l = (ll * 0.66) + (l * 0.33); - r = (rr * 0.66) + (r * 0.33); - buffer[addr + i] = buf[i]; - buf[i] = (l << 16) | (r & 0xffff); - } - addr += 4; - if (addr >= BUF_SIZE) addr = 0; - - audio.write(buf, 0, 4); - - count ++; } int main() { - int time = 0; - pc.baud(115200); - memset(buffer, 0, sizeof(buffer)); + pc.baud(115200 * 8); + rec.mode(PullUp); + play.mode(PullUp); + stop.mode(PullUp); audio.power(0x02); // mic off audio.outputVolume(1, 1); // headphone off - audio.inputVolume(1, 1); + audio.inputVolume(0.7, 0.7); audio.frequency(44100); audio.attach(&isr_audio); audio.start(RECEIVE); printf("Audio ready\r\n"); - - while (1) { - printf("%d / %d\r\n", time, count); - printf("L %04x / R %04x\r\n", (buffer[0] >> 16), (buffer[0] & 0xffff)); - time ++; + led1 = 1; - myled = 1; - wait(0.5); - myled = 0; - wait(0.5); + for (;;) { + if (rec == 0 && mode == MODE_STOP) { + printf("rec\r\n"); + addr_w = 0; num_w = 0; + mem_w = 0; + flg_w = -1; + led2 = 1; + mode = MODE_REC; + } + if (play == 0 && mode == MODE_STOP) { + printf("play\r\n"); + addr_r = 0; num_r = 0; + mem_r = 0; + flg_r = 0; + led2 = 1; + mode = MODE_PLAY; + } + if (stop == 0 && mode != MODE_STOP) { + printf("stop\r\n"); + led2 = 0; + mode = MODE_STOP; + } -// if (pc.readable()) break; + if (mode == MODE_PLAY) { + if (mem_r >= mem_w) { + printf("end\r\n"); + led2 = 0; + mode = MODE_STOP; + } + } + + if (flg_w >= 0) { +// printf("W %d %d\r\n", flg_w, mem_w); + memory.write(BLOCK_SIZE * mem_w, (char *)buf_w[flg_w], BLOCK_SIZE); + mem_w ++; + flg_w = -1; + led3 = 0; + } + if (flg_r >= 0) { +// printf("R %d %d\r\n", flg_r, mem_r); + memory.read(BLOCK_SIZE * mem_r, (char *)buf_r[flg_r], BLOCK_SIZE); + mem_r ++; + flg_r = -1; + led4 = 0; + } } audio.stop();