changeset 84:dab53ea16bd1

merge
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 17 Jul 2015 22:58:25 +0100
parents eec746389e20 (current diff) d6e5f7ca4958 (diff)
children 91e1a3a220d4
files
diffstat 3 files changed, 75 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/core/PRU.cpp	Fri Jul 17 22:54:35 2015 +0100
+++ b/core/PRU.cpp	Fri Jul 17 22:58:25 2015 +0100
@@ -387,6 +387,46 @@
 		}
 	}
 
+	// Allocate audio buffers
+	context->audioIn = (float *)malloc(2 * context->audioFrames * sizeof(float));
+	context->audioOut = (float *)malloc(2 * context->audioFrames * sizeof(float));
+	if(context->audioIn == 0 || context->audioOut == 0) {
+		rt_printf("Error: couldn't allocate audio buffers\n");
+		return 1;
+	}
+
+	// Allocate analog buffers
+	if(analog_enabled) {
+		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));
+
+		if(context->analogIn == 0 || context->analogOut == 0 || last_analog_out_frame == 0) {
+			rt_printf("Error: couldn't allocate analog buffers\n");
+			return 1;
+		}
+
+		memset(last_analog_out_frame, 0, context->analogChannels * sizeof(float));
+	}
+
+	// Allocate digital buffers
+	digital_buffer0 = pru_buffer_digital;
+	digital_buffer1 = pru_buffer_digital + MEM_DIGITAL_BUFFER1_OFFSET / sizeof(uint32_t);
+	if(digital_enabled) {
+		last_digital_buffer = (uint32_t *)malloc(context->digitalFrames * sizeof(uint32_t)); //temp buffer to hold previous states
+		if(last_digital_buffer == 0) {
+			rt_printf("Error: couldn't allocate digital buffers\n");
+			return 1;
+		}
+
+		for(unsigned int n = 0; n < context->digitalFrames; n++){
+			// Initialize lastDigitalFrames to all inputs
+			last_digital_buffer[n] = 0x0000ffff;
+		}
+	}
+
+	context->digital = digital_buffer0;
+
 	return 0;
 }
 
@@ -427,45 +467,24 @@
 	RTIME sleepTime = PRU_SAMPLE_INTERVAL_NS * (context->analogChannels / 2) * context->analogFrames / 4;
 #endif
 
-	float *lastAnalogOutFrame;
-	uint32_t *digitalBuffer0, *digitalBuffer1, *lastDigitalBuffer;
 	uint32_t pru_audio_offset, pru_spi_offset;
 
-	// Allocate audio buffers
-	context->audioIn = (float *)malloc(2 * context->audioFrames * sizeof(float));
-	context->audioOut = (float *)malloc(2 * context->audioFrames * sizeof(float));
-	if(context->audioIn == 0 || context->audioOut == 0) {
-		rt_printf("Error: couldn't allocate audio buffers\n");
-		return;
+	// Before starting, look at the last state of the analog and digital outputs which might
+	// have been changed by the user during the setup() function. This lets us start with pin
+	// directions and output values at something other than defaults.
+
+	if(analog_enabled) {
+		if(context->flags & BEAGLERT_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];
+			}
+		}
 	}
 
-	// Allocate analog buffers
-	if(analog_enabled) {
-		context->analogIn = (float *)malloc(context->analogChannels * context->analogFrames * sizeof(float));
-		context->analogOut = (float *)malloc(context->analogChannels * context->analogFrames * sizeof(float));
-		lastAnalogOutFrame = (float *)malloc(context->analogChannels * sizeof(float));
-
-		if(context->analogIn == 0 || context->analogOut == 0 || lastAnalogOutFrame == 0) {
-			rt_printf("Error: couldn't allocate analog buffers\n");
-			return;
-		}
-
-		memset(lastAnalogOutFrame, 0, context->analogChannels * sizeof(float));
-	}
-
-	// Allocate digital buffers
-	digitalBuffer0 = pru_buffer_digital; 
-	digitalBuffer1 = pru_buffer_digital + MEM_DIGITAL_BUFFER1_OFFSET / sizeof(uint32_t);
 	if(digital_enabled) {
-		lastDigitalBuffer = (uint32_t *)malloc(context->digitalFrames * sizeof(uint32_t)); //temp buffer to hold previous states
-		if(lastDigitalBuffer == 0) {
-			rt_printf("Error: couldn't allocate digital buffers\n");
-			return;
-		}
-
 		for(unsigned int n = 0; n < context->digitalFrames; n++){
-			// Initialize lastDigitalFrames to all inputs
-			lastDigitalBuffer[n] = 0x0000ffff;
+			last_digital_buffer[n] = context->digital[n];
 		}
 	}
 
@@ -518,14 +537,14 @@
 			pru_audio_offset = 0;
 			pru_spi_offset = 0;
 			if(digital_enabled)
-				context->digital = digitalBuffer0;
+				context->digital = digital_buffer0;
 		}
 		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(digital_enabled)
-				context->digital = digitalBuffer1;
+				context->digital = digital_buffer1;
 		}
 
 		// FIXME: some sort of margin is needed here to prevent the audio
@@ -553,7 +572,7 @@
 				// 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 n = 0; n < context->analogFrames; n++){
-						context->analogOut[n * context->analogChannels + ch] = lastAnalogOutFrame[ch];
+						context->analogOut[n * context->analogChannels + ch] = last_analog_out_frame[ch];
 					}
 				}
 			}
@@ -570,12 +589,12 @@
 			// - pins previously set as inputs will carry the newly read input value
 
 			for(unsigned int n = 0; n < context->digitalFrames; n++){
-				uint16_t inputs = lastDigitalBuffer[n] & 0xffff; // half-word, has 1 for inputs and 0 for outputs
+				uint16_t inputs = last_digital_buffer[n] & 0xffff; // half-word, has 1 for inputs and 0 for outputs
 
 				uint16_t outputs = ~inputs; // half-word has 1 for outputs and 0 for inputs;
-				context->digital[n] = (lastDigitalBuffer[context->digitalFrames - 1] & (outputs << 16)) | // keep output values set in the last frame of the previous buffer
+				context->digital[n] = (last_digital_buffer[context->digitalFrames - 1] & (outputs << 16)) | // keep output values set in the last frame of the previous buffer
 									   (context->digital[n] & (inputs << 16))   | // inputs from current context->digital[n];
-									   (lastDigitalBuffer[n] & (inputs));     // keep pin configuration from previous context->digital[n]
+									   (last_digital_buffer[n] & (inputs));     // keep pin configuration from previous context->digital[n]
 //                    context->digital[n]=digitalBufferTemp[n]; //ignores inputs
 			}
 		}
@@ -587,9 +606,9 @@
 
 		if(analog_enabled) {
 			if(context->flags & BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST) {
-				// Remember the content of the lastAnalogOutFrame
+				// Remember the content of the last_analog_out_frame
 				for(unsigned int ch = 0; ch < context->analogChannels; ch++){
-					lastAnalogOutFrame[ch] = context->analogOut[context->analogChannels * (context->analogFrames - 1) + ch];
+					last_analog_out_frame[ch] = context->analogOut[context->analogChannels * (context->analogFrames - 1) + ch];
 				}
 			}
 
@@ -604,7 +623,7 @@
 
 		if(digital_enabled) { // keep track of past digital values
 			for(unsigned int n = 0; n < context->digitalFrames; n++){
-				lastDigitalBuffer[n] = context->digital[n];
+				last_digital_buffer[n] = context->digital[n];
 			}
 		}
 
@@ -653,11 +672,11 @@
 	if(analog_enabled) {
 		free(context->analogIn);
 		free(context->analogOut);
-		free(lastAnalogOutFrame);
+		free(last_analog_out_frame);
 	}
 
 	if(digital_enabled) {
-		free(lastDigitalBuffer);
+		free(last_digital_buffer);
 	}
 
 	context->audioIn = context->audioOut = 0;
--- a/include/PRU.h	Fri Jul 17 22:54:35 2015 +0100
+++ b/include/PRU.h	Fri Jul 17 22:58:25 2015 +0100
@@ -72,6 +72,9 @@
 	int16_t *pru_buffer_audio_dac;
 	int16_t *pru_buffer_audio_adc;
 
+	float *last_analog_out_frame;
+	uint32_t *digital_buffer0, *digital_buffer1, *last_digital_buffer;
+
 	int xenomai_gpio_fd;	// File descriptor for /dev/mem for fast GPIO
 	uint32_t *xenomai_gpio;	// Pointer to GPIO registers
 };
--- a/projects/basic_blink/render.cpp	Fri Jul 17 22:54:35 2015 +0100
+++ b/projects/basic_blink/render.cpp	Fri Jul 17 22:58:25 2015 +0100
@@ -14,6 +14,7 @@
 
 bool setup(BeagleRTContext *context, void *userData)
 {
+    pinModeFrame(context, 0, P8_07, OUTPUT);
 	return true;
 }
 
@@ -36,12 +37,14 @@
     if(count==context->digitalSampleRate*interval){ //if enough samples have elapsed
       count=0; //reset the counter
     // status=digitalReadFrame(context, 0, P8_07);
-      if(status==GPIO_LOW) //toggle the status
-        status=GPIO_HIGH;
-      else
-        status=GPIO_LOW;
-      pinModeFrame(context, n, P8_07, OUTPUT);
-      digitalWriteFrame(context, n, P8_07, status); //write the status to the LED
+      if(status==GPIO_LOW) { //toggle the status
+          digitalWriteFrame(context, n, P8_07, status); //write the status to the LED
+          status=GPIO_HIGH;
+      }
+      else {
+          digitalWriteFrame(context, n, P8_07, status); //write the status to the LED
+          status=GPIO_LOW;
+      }
     }
     count++;
   }