diff core/PRU.cpp @ 38:a9af130097e8 staging

GPIO pins are initialised as inputs by ARM to avoid spikes at startup, through gpio_set_dir. The buffers are set to 0x000ffff during initialisation. LastDigitalBuffer is initialized to 0x0000ffff.
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 12 May 2015 23:48:37 +0100
parents 182ae9367104
children 4255ecbb9bec 4cd9a8ca5745 579c86316008
line wrap: on
line diff
--- a/core/PRU.cpp	Mon May 11 20:11:20 2015 +0100
+++ b/core/PRU.cpp	Tue May 12 23:48:37 2015 +0100
@@ -56,7 +56,7 @@
 #define PRU_FRAME_COUNT		8
 #define PRU_USE_SPI			9
 #define PRU_SPI_NUM_CHANNELS 10
-#define PRU_USE_GPIO_ANALOG 11
+#define PRU_USE_DIGITAL    11
 
 short int digitalPins[NUM_DIGITALS]={
 		GPIO_NO_BIT_0,
@@ -170,16 +170,11 @@
 		for(int i=0; i<gNumDigitalChannels; i++){
 			if(gpio_export(digitalPins[i])) {
 				if(gRTAudioVerbose)
-					cout << "Warning: couldn't export digital GPIO pin " << digitalPins[i] << "\n";
+					cerr << "Warning: couldn't export digital GPIO pin " << digitalPins[i] << "\n"; // this is left as a warning because if the pin has been exported by somebody else, can still be used
 			}
-			if(gpio_set_dir(digitalPins[i], OUTPUT_PIN)) {
+			if(gpio_set_dir(digitalPins[i], INPUT_PIN)) {
 				if(gRTAudioVerbose)
-					cout << "Couldn't set direction on digital GPIO pin " << digitalPins[i] << "\n";
-				return -1;
-			}
-			if(gpio_set_value(digitalPins[i], HIGH)) {
-				if(gRTAudioVerbose)
-					cout << "Couldn't set value on digital GPIO pin " << digitalPins[i] << "\n";
+					cerr << "Error: Couldn't set direction on digital GPIO pin " << digitalPins[i] << "\n";
 				return -1;
 			}
 		}
@@ -361,18 +356,24 @@
     	pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = 0;
     }
     if(digital_enabled) {
-    	pru_buffer_comm[PRU_USE_GPIO_ANALOG] = 1;
-    	pru_buffer_comm[NUM_DIGITALS] = spi_num_channels;
+    	pru_buffer_comm[PRU_USE_DIGITAL] = 1;
+//TODO: add mask
     }
     else {
-    	pru_buffer_comm[PRU_USE_GPIO_ANALOG] = 0;
-    	pru_buffer_comm[NUM_DIGITALS] = 0;
+    	pru_buffer_comm[PRU_USE_DIGITAL] = 0;
+
     }
 
-    /* Clear ADC and DAC memory */
+    /* Clear ADC and DAC memory.*/
+    //TODO: this initialisation should only address the memory effectively used by these buffers, i.e.:depend on the number of frames
+    //  (otherwise might cause issues if we move memory locations later on)
     if(spi_enabled) {
 		for(int i = 0; i < PRU_MEM_DAC_LENGTH / 2; i++)
 			pru_buffer_spi_dac[i] = 0;
+		if(digital_enabled){
+			for(int i = 0; i < PRU_MEM_DIGITAL_OFFSET*2; i++)
+				pru_buffer_digital[i] = 0x0000ffff; // set to all inputs, to avoid unexpected spikes
+		}
     }
 	for(int i = 0; i < PRU_MEM_MCASP_LENGTH / 2; i++)
 		pru_buffer_audio_dac[i] = 0;
@@ -460,7 +461,10 @@
 		rt_printf("Error: couldn't allocate digital buffers\n");
 		return;
 	}
-	
+
+	for(unsigned int n=0; n<digital_buffer_frames; n++){ //initialize lastDigitalFrames to all inputs
+		lastDigitalBuffer[n]= 0x0000ffff;
+	}
 	while(!gShouldStop) {
 		// Wait for PRU to move to buffer 1
 		while(pru_buffer_comm[PRU_CURRENT_BUFFER] == 0 && !gShouldStop) {