diff core/PRU.cpp @ 528:5c8f46fcd4d0 API-update

Updated BelaContext to use separate values for in/ou channels
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 23 Jun 2016 18:17:35 +0100
parents 3a28a4eb948d
children
line wrap: on
line diff
--- a/core/PRU.cpp	Thu Jun 23 18:15:26 2016 +0100
+++ b/core/PRU.cpp	Thu Jun 23 18:17:35 2016 +0100
@@ -334,7 +334,7 @@
 		pru_buffer_spi_dac = (uint16_t *)&pruMem[PRU_MEM_DAC_OFFSET/sizeof(uint32_t)];
 
 		/* ADC memory starts after N(ch)*2(buffers)*bufsize samples */
-		pru_buffer_spi_adc = &pru_buffer_spi_dac[2 * context->analogChannels * context->analogFrames];
+		pru_buffer_spi_adc = &pru_buffer_spi_dac[2 * context->analogInChannels * context->analogFrames];
 	}
 	else {
 		pru_buffer_spi_dac = pru_buffer_spi_adc = 0;
@@ -376,7 +376,12 @@
     }
     if(analog_enabled) {
     	pru_buffer_comm[PRU_USE_SPI] = 1;
-    	pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = context->analogChannels;
+    	if(context->analogInChannels != context->analogOutChannels){
+    		printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n");
+    		return 1;
+    	}
+    	unsigned int analogChannels = context->analogInChannels;
+    	pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = analogChannels;
     }
     else {
     	pru_buffer_comm[PRU_USE_SPI] = 0;
@@ -460,9 +465,9 @@
 			return 1;
 		}		
 #else
-		context->analogIn = (float *)malloc(context->analogChannels * context->analogFrames * sizeof(float));
-		context->analogOut = (float *)malloc(context->analogChannels * context->analogFrames * sizeof(float));
-		last_analog_out_frame = (float *)malloc(context->analogChannels * sizeof(float));
+		context->analogIn = (float *)malloc(context->analogInChannels * context->analogFrames * sizeof(float));
+		context->analogOut = (float *)malloc(context->analogOutChannels * context->analogFrames * sizeof(float));
+		last_analog_out_frame = (float *)malloc(context->analogOutChannels * sizeof(float));
 
 		if(context->analogIn == 0 || context->analogOut == 0 || last_analog_out_frame == 0) {
 			rt_printf("Error: couldn't allocate analog buffers\n");
@@ -470,7 +475,7 @@
 		}
 #endif
 		
-		memset(last_analog_out_frame, 0, context->analogChannels * sizeof(float));
+		memset(last_analog_out_frame, 0, context->analogOutChannels * sizeof(float));
 	}
 
 	// Allocate digital buffers
@@ -528,7 +533,12 @@
 	RTIME irqTimeout = PRU_SAMPLE_INTERVAL_NS * 1024;	// Timeout for PRU interrupt: about 10ms, much longer than any expected period
 #else
 	// Polling interval is 1/4 of the period
-	RTIME sleepTime = PRU_SAMPLE_INTERVAL_NS * (context->analogChannels / 2) * context->analogFrames / 4;
+	if(context->analogInChannels != context->analogOutChannels){
+		printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n");
+		return;
+	}
+	unsigned int analogChannels = context->analogInChannels;
+	RTIME sleepTime = PRU_SAMPLE_INTERVAL_NS * (analogChannels / 2) * context->analogFrames / 4;
 #endif
 
 	uint32_t pru_audio_offset, pru_spi_offset;
@@ -540,8 +550,8 @@
 	if(analog_enabled) {
 		if(context->flags & BELA_FLAG_ANALOG_OUTPUTS_PERSIST) {
 			// Remember the content of the last_analog_out_frame
-			for(unsigned int ch = 0; ch < context->analogChannels; ch++){
-				last_analog_out_frame[ch] = context->analogOut[context->analogChannels * (context->analogFrames - 1) + ch];
+			for(unsigned int ch = 0; ch < context->analogOutChannels; ch++){
+				last_analog_out_frame[ch] = context->analogOut[context->analogOutChannels * (context->analogFrames - 1) + ch];
 			}
 		}
 	}
@@ -605,8 +615,18 @@
 		}
 		else {
 			// PRU is on buffer 0. We read and write to buffer 1
-			pru_audio_offset = context->audioFrames * 2;
-			pru_spi_offset = context->analogFrames * context->analogChannels;
+			if(context->audioInChannels != context->audioOutChannels){
+				printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n");
+				return;
+			}
+			unsigned int audioChannels = context->audioInChannels;
+			pru_audio_offset = context->audioFrames * audioChannels;
+			if(context->analogInChannels != context->analogOutChannels){
+				printf("Error: TODO: a different number of channels for inputs and outputs is not yet supported\n");
+				return;
+			}
+	    	unsigned int analogChannels = context->analogInChannels;
+			pru_spi_offset = context->analogFrames * analogChannels;
 			if(digital_enabled)
 				context->digital = digital_buffer1;
 		}
@@ -644,22 +664,22 @@
 			int16_to_float_analog(context->analogChannels * context->analogFrames, 
 									&pru_buffer_spi_adc[pru_spi_offset], context->analogIn);
 #else	
-			for(unsigned int n = 0; n < context->analogChannels * context->analogFrames; n++) {
+			for(unsigned int n = 0; n < context->analogInChannels * context->analogFrames; n++) {
 				context->analogIn[n] = (float)pru_buffer_spi_adc[n + pru_spi_offset] / 65536.0f;
 			}
 #endif
 
 			if(context->flags & BELA_FLAG_ANALOG_OUTPUTS_PERSIST) {
 				// Initialize the output buffer with the values that were in the last frame of the previous output
-				for(unsigned int ch = 0; ch < context->analogChannels; ch++){
+				for(unsigned int ch = 0; ch < context->analogOutChannels; ch++){
 					for(unsigned int n = 0; n < context->analogFrames; n++){
-						context->analogOut[n * context->analogChannels + ch] = last_analog_out_frame[ch];
+						context->analogOut[n * context->analogOutChannels + ch] = last_analog_out_frame[ch];
 					}
 				}
 			}
 			else {
 				// Outputs are 0 unless set otherwise
-				memset(context->analogOut, 0, context->analogChannels * context->analogFrames * sizeof(float));
+				memset(context->analogOut, 0, context->analogOutChannels * context->analogFrames * sizeof(float));
 			}
 		}
 
@@ -688,8 +708,8 @@
 		if(analog_enabled) {
 			if(context->flags & BELA_FLAG_ANALOG_OUTPUTS_PERSIST) {
 				// Remember the content of the last_analog_out_frame
-				for(unsigned int ch = 0; ch < context->analogChannels; ch++){
-					last_analog_out_frame[ch] = context->analogOut[context->analogChannels * (context->analogFrames - 1) + ch];
+				for(unsigned int ch = 0; ch < context->analogOutChannels; ch++){
+					last_analog_out_frame[ch] = context->analogOut[context->analogOutChannels * (context->analogFrames - 1) + ch];
 				}
 			}
 
@@ -698,7 +718,7 @@
 			float_to_int16_analog(context->analogChannels * context->analogFrames, 
 								  context->analogOut, (uint16_t*)&pru_buffer_spi_dac[pru_spi_offset]);
 #else		
-			for(unsigned int n = 0; n < context->analogChannels * context->analogFrames; n++) {
+			for(unsigned int n = 0; n < context->analogOutChannels * context->analogFrames; n++) {
 				int out = context->analogOut[n] * 65536.0f;
 				if(out < 0) out = 0;
 				else if(out > 65535) out = 65535;
@@ -717,7 +737,7 @@
 #ifdef USE_NEON_FORMAT_CONVERSION
 		float_to_int16_audio(2 * context->audioFrames, context->audioOut, &pru_buffer_audio_dac[pru_audio_offset]);
 #else	
-		for(unsigned int n = 0; n < 2 * context->audioFrames; n++) {
+		for(unsigned int n = 0; n < context->audioOutChannels * context->audioFrames; n++) {
 			int out = context->audioOut[n] * 32768.0f;
 			if(out < -32768) out = -32768;
 			else if(out > 32767) out = 32767;
@@ -735,9 +755,6 @@
 		
 		Bela_autoScheduleAuxiliaryTasks();
 
-		// FIXME: TESTING!!
-		// if(testCount > 100000)
-		//	break;
 	}
 
 #ifdef BELA_USE_XENOMAI_INTERRUPTS
@@ -745,11 +762,6 @@
 	rt_intr_disable(pru_interrupt);
 #endif
 
-	// FIXME: TESTING
-	// RTIME endTime = rt_timer_read();
-	// RTIME diffTime = endTime - startTime;
-	// rt_printf("%d blocks elapsed in %f seconds, %f Hz block rate\n", testCount, ((float)diffTime / 1.0e9), (float)testCount / ((float)diffTime / 1.0e9));
-
 	// Tell PRU to stop
 	pru_buffer_comm[PRU_SHOULD_STOP] = 1;