diff core/PRU.cpp @ 41:4255ecbb9bec ultra-staging

Timers to measure performances, ultra experimental
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 19 May 2015 16:41:07 +0100
parents a9af130097e8
children 3068421c0737
line wrap: on
line diff
--- a/core/PRU.cpp	Wed May 13 12:23:37 2015 +0100
+++ b/core/PRU.cpp	Tue May 19 16:41:07 2015 +0100
@@ -20,6 +20,7 @@
 #include "../include/GPIOcontrol.h"
 #include "../include/render.h"
 #include "../include/pru_rtaudio_bin.h"
+#include "../include/intervals.h"
 
 #include <iostream>
 #include <stdlib.h>
@@ -99,12 +100,14 @@
 const unsigned int PRU::kPruGPIOTestPin2 = 31;	// GPIO0(31); P9-13
 const unsigned int PRU::kPruGPIOTestPin3 = 26;	// GPIO0(26); P8-14
 
-extern int gShouldStop;
+//extern int gShouldStop;
 extern int gRTAudioVerbose;
+extern PRU *gPRU;
 
 // Constructor: specify a PRU number (0 or 1)
 PRU::PRU()
-: pru_number(0), running(false), spi_enabled(false), gpio_enabled(false), led_enabled(false),
+:  renderTimer(100,0,44100.0,"renderTimer"), sleepTimer(100,0,44100.0,"sleepTimer"),
+  pru_number(0), running(false), spi_enabled(false), gpio_enabled(false), led_enabled(false),
   gpio_test_pin_enabled(false), spi_num_channels(0), xenomai_gpio_fd(-1), xenomai_gpio(0)
 {
 
@@ -435,7 +438,6 @@
 	float *audioInBuffer, *audioOutBuffer;
 	float *analogInBuffer, *analogOutBuffer, *lastAnalogOutFrame;
 	uint32_t *digitalBuffer0, *digitalBuffer1, *lastDigitalBuffer;
-
 	audioInBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float));
 	audioOutBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float));
 	analogInBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float));
@@ -447,7 +449,6 @@
     																	// - embed in the digitalWrite/Read macros a check whether digital is enabled
     																	// - allocate some memory in ARM just to allow render() to run regardless.
     																	//     in this case it can be digitalBuffer0 == digitalBuffer1
-	printf("digital_buffer_frames: %d;\n",digital_buffer_frames);
     lastDigitalBuffer = (uint32_t *)malloc(digital_buffer_frames*sizeof(uint32_t)); //temp buffer to hold previous states
 	if(audioInBuffer == 0 || audioOutBuffer == 0) {
 		rt_printf("Error: couldn't allocate audio buffers\n");
@@ -462,17 +463,25 @@
 		return;
 	}
 
-	for(unsigned int n=0; n<digital_buffer_frames; n++){ //initialize lastDigitalFrames to all inputs
-		lastDigitalBuffer[n]= 0x0000ffff;
-	}
+    if(digital_enabled){
+		for(unsigned int n=0; n<digital_buffer_frames; n++){ //initialize lastDigitalFrames to all inputs
+			lastDigitalBuffer[n]= 0x0000ffff;
+		}
+    }
+	int count=0;
+	sleepTimer.setNumFrames(audio_buffer_frames);
+	renderTimer.setNumFrames(audio_buffer_frames);
 	while(!gShouldStop) {
 		// Wait for PRU to move to buffer 1
+		sleepTimer.start();
 		while(pru_buffer_comm[PRU_CURRENT_BUFFER] == 0 && !gShouldStop) {
 			rt_task_sleep(sleepTime);
 		}
+		sleepTimer.split();
+
 		if(gShouldStop)
 			break;
-
+		renderTimer.start();
 		if(xenomai_gpio != 0) {
 			// Set the test pin high
 			xenomai_gpio[GPIO_SETDATAOUT] = TEST_PIN_MASK;
@@ -489,9 +498,10 @@
 			//initialize the output buffer with the values that were in the last frame of the previous output
 			for(int n = 0; n < spi_num_channels; n++){
 				for(unsigned int j = 0; j < spi_buffer_frames; j++){
-					analogOutBuffer[j*spi_buffer_frames + n] = lastAnalogOutFrame[n];
+					analogOutBuffer[j*spi_num_channels + n] = lastAnalogOutFrame[n];
 				}
 			}
+
         //use past digital values to initialize the array properly.
 		//For each frame:
         //- pins previously set as outputs will keep the output value they had in the last frame of the previous buffer,
@@ -539,15 +549,17 @@
 			// Set the test pin high
 			xenomai_gpio[GPIO_CLEARDATAOUT] = TEST_PIN_MASK;
 		}
-
+		renderTimer.split();
 		// Wait for PRU to move to buffer 0
+		sleepTimer.start();
 		while(pru_buffer_comm[PRU_CURRENT_BUFFER] != 0 && !gShouldStop) {
 			rt_task_sleep(sleepTime);
 		}
-
+		sleepTimer.split();
 		if(gShouldStop)
 			break;
 
+		renderTimer.start();
 		if(xenomai_gpio != 0) {
 			// Set the test pin high
 			xenomai_gpio[GPIO_SETDATAOUT] = TEST_PIN_MASK;
@@ -567,7 +579,7 @@
 			//initialize the output buffer with the values that were in the last frame of the previous output
 			for(int n = 0; n < spi_num_channels; n++){
 				for(unsigned int j = 0; j < spi_buffer_frames; j++){
-					analogOutBuffer[j*spi_buffer_frames + n] = lastAnalogOutFrame[n];
+					analogOutBuffer[j*spi_num_channels + n] = lastAnalogOutFrame[n];
 				}
 			}
             if(digital_enabled){
@@ -614,15 +626,19 @@
 			// Set the test pin high
 			xenomai_gpio[GPIO_CLEARDATAOUT] = TEST_PIN_MASK;
 		}
+		renderTimer.split();
+		count+=audio_buffer_frames;
+		if((count&32767)==0){
+			scheduleAuxiliaryTask(gPRU->printIntervalsTask);
+		}
 	}
 
 	// Tell PRU to stop
 	pru_buffer_comm[PRU_SHOULD_STOP] = 1;
-
 	free(analogOutBuffer);
+	free(analogInBuffer);
+	free(audioOutBuffer);
 	free(audioInBuffer);
-	free(audioOutBuffer);
-	free(analogInBuffer);
 	free(lastAnalogOutFrame);
     free(lastDigitalBuffer);
 }