Commits

Anonymous committed 5a4d54a Draft

add code showing audiocodec working with USB still active--shows how to set interrupt controller priorties

Comments (0)

Files changed (4)

testserial/testserialclient.py

 
 
 
-def download_1024_int16():
+def test_download_1024_int16():
     ser.write('d')
     resp = ser.read(size=2048) # two bytes per uint16
     assert len(resp) == 2048
     return iarr, resp 
 
 
+def raw_download_1024_int16():
+    """
+    ignore the results, get it it as fast as possible
+    """
+    ser.write('d')
+    resp = ser.read(size=2048) # two bytes per uint16
+
+
+
 def upload_int16arr(int16arr):
     ser.write('D')
     s = int16arr.tostring()
 def torture_test():
     ii = 0
     while 1:
-        a,b = download_1024_int16()
+        a,b = test_download_1024_int16()
         print ii
         ii += 1

upload_play/testclient.py

 # ser = serial.Serial(r'/dev/ttyUSB2', 9600)   # safe, works
 # ser = serial.Serial(r'/dev/ttyUSB2', 921600) # works
 
-BAUDRATE =  2000000 # works
+BAUDRATE =  1000000 # works
 ser = serial.Serial(r'/dev/ttyUSB2', BAUDRATE, timeout=10.0) 
 print "Number of char in receive buffer: ", ser.inWaiting()
 # can also flushInput() and flushOutput()
     # now check for correct response
     return resp
 
-def test_download_1024_uint16():
+def download_1024_int16():
     ser.write('d')
     resp = ser.read(size=2048) # two bytes per uint16
-    return resp # returns bytes
-#def convert_char2uint8(lstarr):
-#    np.
+    iarr= np.fromstring(resp, dtype='<i2')
+    return iarr, resp 
+
+
+def upload_int16arr(int16arr):
+    ser.write('D')
+    s = int16arr.tostring()
+    ser.write(s)
+    
 
 def assemble_uints(bytearray):
     # probably there is a better way to do this ? see structure or array package

upload_play/upload_play.pde

 (this also applies to the Maple RET6 edition)
 These pins are also listed as being ***5V tolerant***
 
+--------------- for the maple-ret6 edition -----------------
+Pin, GPIO, ADC,  Timer,     I2C,     UART/USART, SPI,    5 V?
+D32, PB13, -,    1_CH1N,    -,       3_CTS,      2_SCK,  Yes
+D33, PB14, -,    1_CH2N,    -,       3_RTS,      2_MISO, Yes
+
+--------------------- for the maple ------------------------
+D32, PB13, -,    -,         -,       3_CTS,      2_SCK,  Yes
+D33, PB14, -,    -,         -,       3_RTS,      2_MISO, Yes
+
 
 I chose Serial3 because these pins are on the 2x8 header "EXT" block which aren't
 connected to the audio codec shield.
 // the FTDI 232R datasheet says it can go to 3 Mbaud
 // the stm32F103 data sheet mentioned a minimum of 2.25 Mbits/s or one at 4 Mbits/s
 
-// #define BAUDRATE 9600    // works, safe
+#define BAUDRATE 9600    // works, safe 8N1
 // #define BAUDRATE 921600  // works
 // #define BAUDRATE 1500000 // works
-#define BAUDRATE  2000000
+// #define BAUDRATE  1000000
    
 #define N_SENDSERIAL 100 // send a serial value every N steps through the loop   
 // include necessary libraries
 	break;
 
       case 'w':
-	//  Serial1.println("Hello World!");
-	// Serial2.println("Hello World!");
-	Serial3.println("Hello World!");
+	Serial3.println("Hello!");
 	break;
 
       case 'm':
 	break;
 
       case 'd':
-	// Dumping 1024 uint16 data to Serial3 LSB first 
-	Serial3.flush();
+	// Dumping from 0 to 1023 uint16 data to Serial3 LSB first 
 	for(uint16 ii =0; ii < 1024; ii++) {
 	  Serial3.write((unsigned char)ii);  
 	  Serial3.write((unsigned char) (ii >> 8));

workingUSBAudioCodec/workingUSBAudioCodec.pde

+/*
+sine_generator.pde (MAPLE)
+guest openmusiclabs 7.30.12
+test program for usb communication with codecshield
+plays back a glitch free audio stream while usb is active
+
+url=http://forums.openmusiclabs.com/viewtopic.php?f=21&p=622&sid=cea1bbb8d6731bc34e487cf5893644b2#p622
+*/
+
+// setup codec parameters
+// must be done before #includes
+// see readme file in libraries folder for explanations
+#define SAMPLE_RATE 44 // 44.1kHz sample rate
+#define ADCS 2 // use both ADCs
+
+// include necessary libraries
+// note the Maple library
+#include <AudioCodec_Maple.h>
+#include <scb.h> // for setting the interrupt priorities
+
+int16 left_in = 0; // in from codec (LINE_IN)
+int16 right_in = 0;
+volatile int16 left_out = 0; // out to codec (HP_OUT)
+volatile int16 right_out = 0;
+volatile int16 waittimer = 0;
+
+uint16 mod0_value = 0;
+uint16 mod1_value = 0;
+
+int16 buffer[8];
+volatile uint8 buffertop = 0;
+volatile uint8 bufferofset = 2;
+
+int16 sinewave[] __FLASH__ = {
+  #include <sinetable.inc>
+};
+uint16 location; // lookup table value location
+
+
+void setup() {
+  // setup codec and microcontroller registers
+  AudioCodec_init(); // call this last if you are setting up other things
+  SCB_BASE->AIRCR = 0x05FA0300;
+  nvic_irq_set_priority(NVIC_TIMER4, 0);
+  nvic_irq_set_priority(NVIC_USB_HP_CAN_TX, 15);
+  nvic_irq_set_priority(NVIC_USB_LP_CAN_RX0, 15);
+  nvic_irq_set_priority(NVIC_USBWAKEUP, 15);
+}
+
+void loop() {
+  while (1) { // reduces clock jitter
+  while (waittimer == 0);
+  while (!(SPI1_BASE->SR & (1 << SPI_SR_RXNE_BIT))) { // wait for data to arrive
+  }
+  left_in = SPI1_BASE->DR; // recieve left data
+  while (!(SPI1_BASE->SR & (1 << SPI_SR_RXNE_BIT))) { // wait for data to arrive
+  }
+  right_in = SPI1_BASE->DR; // recieve right data
+  waittimer = 0;
+  while (bufferofset < 6) {
+    int16 temp1;
+    temp1 = sinewave[location];
+    location += 1 + (mod1_value >> 8);
+    location &= 0x03ff;
+    temp1 = ((temp1 * mod0_value) >> 16) & 0xfffe;
+    buffer[(buffertop + bufferofset)&0x07] = temp1;
+    bufferofset++;
+  }
+  AudioCodec_ADC(&mod0_value, &mod1_value);
+  }
+}
+
+// timer1 interrupt routine - all data processed here
+void AudioCodec_interrupt() {
+  left_out = buffer[buffertop];
+  GPIOA_BASE->BSRR = 0x00000010; // set ss high
+  SPI1_BASE->DR = left_out; // send out left data
+  asm volatile ("nop"); // delay for appropriate timing
+  asm volatile ("nop");
+  asm volatile ("nop");
+  asm volatile ("nop");
+  asm volatile ("nop");
+  asm volatile ("nop");
+  asm volatile ("nop");
+  asm volatile ("nop");
+  asm volatile ("nop");
+  GPIOA_BASE->BSRR = 0x00100000; // set ss low
+  right_out =  left_out;
+  buffertop++;
+  bufferofset--;
+  buffertop &= 0x07;
+  while (!(SPI1_BASE->SR & (1 << SPI_SR_TXE_BIT))) { // wait for data to be sent
+  }
+  SPI1_BASE->DR = right_out; // send out right data
+  waittimer = 1;
+}