Mercurial > hg > beaglert
diff core/PRU.cpp @ 19:c98863e63174 matrix_gpio
Renamed matrixGpio to digital and matrix to analog
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Thu, 30 Apr 2015 16:58:41 +0100 |
parents | 670be80463a3 |
children | 182ae9367104 |
line wrap: on
line diff
--- a/core/PRU.cpp Thu Apr 30 16:02:47 2015 +0100 +++ b/core/PRU.cpp Thu Apr 30 16:58:41 2015 +0100 @@ -16,7 +16,7 @@ #include "../include/PRU.h" #include "../include/prussdrv.h" #include "../include/pruss_intc_mapping.h" -#include "../include/matrix_gpio_mapping.h" +#include "../include/digital_gpio_mapping.h" #include "../include/GPIOcontrol.h" #include "../include/render.h" #include "../include/pru_rtaudio_bin.h" @@ -42,8 +42,8 @@ #define PRU_MEM_DAC_OFFSET 0x0 // Offset within PRU0 RAM #define PRU_MEM_DAC_LENGTH 0x2000 // Length of ADC+DAC memory, in bytes #define PRU_MEM_COMM_OFFSET 0x0 // Offset within PRU-SHARED RAM -#define PRU_MEM_MATRIX_GPIO_OFFSET 0x1000 //Offset within PRU-SHARED RAM -#define MEM_MATRIX_GPIO_BUFFER1_OFFSET 0x400 //Start pointer to MATRIX_GPIO_BUFFER1, which is 256 words. +#define PRU_MEM_DIGITAL_OFFSET 0x1000 //Offset within PRU-SHARED RAM +#define MEM_DIGITAL_BUFFER1_OFFSET 0x400 //Start pointer to DIGITAL_BUFFER1, which is 256 words. // 256 is the maximum number of frames allowed #define PRU_SHOULD_STOP 0 #define PRU_CURRENT_BUFFER 1 @@ -56,9 +56,9 @@ #define PRU_FRAME_COUNT 8 #define PRU_USE_SPI 9 #define PRU_SPI_NUM_CHANNELS 10 -#define PRU_USE_GPIO_MATRIX 11 +#define PRU_USE_GPIO_ANALOG 11 -short int matrixGpioPins[NUM_MATRIX_GPIOS]={ +short int digitalPins[NUM_DIGITALS]={ GPIO_NO_BIT_0, GPIO_NO_BIT_1, GPIO_NO_BIT_2, @@ -127,7 +127,7 @@ // viewed on a scope. If include_led is set, // user LED 3 on the BBB is taken over by the PRU // to indicate activity -int PRU::prepareGPIO(int use_spi, int use_matrix_gpio, int include_test_pin, int include_led) +int PRU::prepareGPIO(int use_spi, int use_digital, int include_test_pin, int include_led) { if(use_spi) { // Prepare DAC CS/ pin: output, high to begin @@ -165,25 +165,25 @@ spi_enabled = true; } - if(use_matrix_gpio){ - printf("gNumMatrixGpioChannels: %d;\n",gNumMatrixGpioChannels); - for(int i=0; i<gNumMatrixGpioChannels; i++){ - if(gpio_export(matrixGpioPins[i])) { + if(use_digital){ + printf("gNumDigitalChannels: %d;\n",gNumDigitalChannels); + for(int i=0; i<gNumDigitalChannels; i++){ + if(gpio_export(digitalPins[i])) { if(gRTAudioVerbose) - cout << "Warning: couldn't export matrix GPIO pin " << matrixGpioPins[i] << "\n"; + cout << "Warning: couldn't export digital GPIO pin " << digitalPins[i] << "\n"; } - if(gpio_set_dir(matrixGpioPins[i], OUTPUT_PIN)) { + if(gpio_set_dir(digitalPins[i], OUTPUT_PIN)) { if(gRTAudioVerbose) - cout << "Couldn't set direction on matrix GPIO pin " << matrixGpioPins[i] << "\n"; + cout << "Couldn't set direction on digital GPIO pin " << digitalPins[i] << "\n"; return -1; } - if(gpio_set_value(matrixGpioPins[i], HIGH)) { + if(gpio_set_value(digitalPins[i], HIGH)) { if(gRTAudioVerbose) - cout << "Couldn't set value on matrix GPIO pin " << matrixGpioPins[i] << "\n"; + cout << "Couldn't set value on digital GPIO pin " << digitalPins[i] << "\n"; return -1; } } - matrix_gpio_enabled=true; + digital_enabled=true; } if(include_test_pin) { @@ -255,9 +255,9 @@ gpio_unexport(kPruGPIODACSyncPin); gpio_unexport(kPruGPIOADCSyncPin); } - if(matrix_gpio_enabled){ - for(int i=0; i<gNumMatrixGpioChannels; i++){ - gpio_unexport(matrixGpioPins[i]); + if(digital_enabled){ + for(int i=0; i<gNumDigitalChannels; i++){ + gpio_unexport(digitalPins[i]); } } if(gpio_test_pin_enabled) { @@ -309,7 +309,7 @@ spi_buffer_frames = frames_per_buffer; audio_buffer_frames = spi_buffer_frames * spi_num_channels / 4; - matrix_gpio_buffer_frames = audio_buffer_frames; + digital_buffer_frames = audio_buffer_frames; /* Map PRU memory to pointers */ prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem); @@ -330,12 +330,12 @@ pru_buffer_spi_dac = pru_buffer_spi_adc = 0; } - if(matrix_gpio_enabled) { + if(digital_enabled) { prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem); - pru_buffer_matrix_gpio = (uint32_t *)&pruMem[PRU_MEM_MATRIX_GPIO_OFFSET/sizeof(uint32_t)]; + pru_buffer_digital = (uint32_t *)&pruMem[PRU_MEM_DIGITAL_OFFSET/sizeof(uint32_t)]; } else { - pru_buffer_matrix_gpio = 0; + pru_buffer_digital = 0; } /* Set up flags */ pru_buffer_comm[PRU_SHOULD_STOP] = 0; @@ -360,13 +360,13 @@ pru_buffer_comm[PRU_USE_SPI] = 0; pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = 0; } - if(matrix_gpio_enabled) { - pru_buffer_comm[PRU_USE_GPIO_MATRIX] = 1; - pru_buffer_comm[NUM_MATRIX_GPIOS] = spi_num_channels; + if(digital_enabled) { + pru_buffer_comm[PRU_USE_GPIO_ANALOG] = 1; + pru_buffer_comm[NUM_DIGITALS] = spi_num_channels; } else { - pru_buffer_comm[PRU_USE_GPIO_MATRIX] = 0; - pru_buffer_comm[NUM_MATRIX_GPIOS] = 0; + pru_buffer_comm[PRU_USE_GPIO_ANALOG] = 0; + pru_buffer_comm[NUM_DIGITALS] = 0; } /* Clear ADC and DAC memory */ @@ -432,31 +432,31 @@ // Polling interval is 1/4 of the period RTIME sleepTime = PRU_SAMPLE_INTERVAL_NS * (spi_num_channels / 2) * spi_buffer_frames / 4; float *audioInBuffer, *audioOutBuffer; - float *matrixInBuffer, *matrixOutBuffer; - uint32_t *matrixGpioBuffer0, *matrixGpioBuffer1, *matrixGpioBufferTemp; + float *analogInBuffer, *analogOutBuffer; + uint32_t *digitalBuffer0, *digitalBuffer1, *digitalBufferTemp; audioInBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float)); audioOutBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float)); - matrixInBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); - matrixOutBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); - matrixGpioBuffer0 = pru_buffer_matrix_gpio; - matrixGpioBuffer1 = pru_buffer_matrix_gpio+MEM_MATRIX_GPIO_BUFFER1_OFFSET/sizeof(uint32_t); - matrix_gpio_buffer_frames = matrix_gpio_enabled ? audio_buffer_frames : 0; //TODO: find a more elegant solution for when the matrix_gpio is disabled e.g.: - // - embed in the digitalWrite/Read macros a check whether matrix_gpio is enabled + analogInBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); + analogOutBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); + digitalBuffer0 = pru_buffer_digital; + digitalBuffer1 = pru_buffer_digital+MEM_DIGITAL_BUFFER1_OFFSET/sizeof(uint32_t); + digital_buffer_frames = digital_enabled ? audio_buffer_frames : 0; //TODO: find a more elegant solution for when the digital is disabled e.g.: + // - 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 matrixGpioBuffer0 == matrixGpioBuffer1 - printf("matrix_gpio_buffer_frames: %d;\n",matrix_gpio_buffer_frames); - matrixGpioBufferTemp = (uint32_t *)malloc(matrix_gpio_buffer_frames*sizeof(uint32_t)); //temp buffer to hold previous states + // in this case it can be digitalBuffer0 == digitalBuffer1 + printf("digital_buffer_frames: %d;\n",digital_buffer_frames); + digitalBufferTemp = (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"); return; } - if(matrixInBuffer == 0 || matrixOutBuffer == 0) { - rt_printf("Error: couldn't allocate matrix buffers\n"); + if(analogInBuffer == 0 || analogOutBuffer == 0) { + rt_printf("Error: couldn't allocate analog buffers\n"); return; } - if(matrixGpioBufferTemp == 0) { - rt_printf("Error: couldn't allocate matrix GPIO buffers\n"); + if(digitalBufferTemp == 0) { + rt_printf("Error: couldn't allocate digital buffers\n"); return; } @@ -480,37 +480,37 @@ audioInBuffer[n] = (float)pru_buffer_audio_adc[n] / 32768.0; if(spi_enabled) { for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) - matrixInBuffer[n] = (float)pru_buffer_spi_adc[n] / 65536.0; - //use past matrix_gpio values to initialize the array properly: + analogInBuffer[n] = (float)pru_buffer_spi_adc[n] / 65536.0; + //use past digital values to initialize the array properly: //- pins previously set as outputs will keep their previously set output value, //- pins previously set as inputs will carry the newly read input value - if(matrix_gpio_enabled){ - for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ - uint16_t inputs=matrixGpioBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs + if(digital_enabled){ + for(unsigned int n = 0; n < digital_buffer_frames; n++){ + uint16_t inputs=digitalBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs // printf("inputs: 0x%x\n",inputs); uint16_t outputs=~inputs; //half-word has 1 for outputs and 0 for inputs; - matrixGpioBuffer0[n]=(matrixGpioBufferTemp[n]&(outputs<<16))| //keep output values set in previous matrixGpioBuffer1[n] - (matrixGpioBuffer0[n]&(inputs<<16)) | //inputs from current matrixGpioBuffer0[n]; - (matrixGpioBufferTemp[n]&(inputs)); //keep pin configuration from previous matrixGpioBuffer1[n] -// matrixGpioBuffer0[n]=matrixGpioBufferTemp[n]; //ignores inputs + digitalBuffer0[n]=(digitalBufferTemp[n]&(outputs<<16))| //keep output values set in previous digitalBuffer1[n] + (digitalBuffer0[n]&(inputs<<16)) | //inputs from current digitalBuffer0[n]; + (digitalBufferTemp[n]&(inputs)); //keep pin configuration from previous digitalBuffer1[n] +// digitalBuffer0[n]=digitalBufferTemp[n]; //ignores inputs } } - render(spi_buffer_frames, matrix_gpio_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, - matrixInBuffer, matrixOutBuffer, matrixGpioBuffer0); + render(spi_buffer_frames, digital_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, + analogInBuffer, analogOutBuffer, digitalBuffer0); for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) { - int out = matrixOutBuffer[n] * 65536.0; + int out = analogOutBuffer[n] * 65536.0; if(out < 0) out = 0; else if(out > 65535) out = 65535; pru_buffer_spi_dac[n] = (uint16_t)out; } - if(matrix_gpio_enabled){ // keep track of past matrix_gpio values - for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ - matrixGpioBufferTemp[n]=matrixGpioBuffer0[n]; + if(digital_enabled){ // keep track of past digital values + for(unsigned int n = 0; n < digital_buffer_frames; n++){ + digitalBufferTemp[n]=digitalBuffer0[n]; } } } else - render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass matrixGpioBuffer, just it is unused + render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass digitalBuffer, just it is unused // Convert float back to short for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) { int out = audioOutBuffer[n] * 32768.0; @@ -545,37 +545,37 @@ if(spi_enabled) { for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) - matrixInBuffer[n] = (float)pru_buffer_spi_adc[n + spi_buffer_frames * spi_num_channels] / 65536.0; + analogInBuffer[n] = (float)pru_buffer_spi_adc[n + spi_buffer_frames * spi_num_channels] / 65536.0; - //use past matrix_gpio values to initialize the array properly: + //use past digital values to initialize the array properly: //- pins previously set as outputs will keep their previously set output value, //- pins previously set as inputs will carry the newly read input value - if(matrix_gpio_enabled){ - for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ - uint16_t inputs=matrixGpioBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs + if(digital_enabled){ + for(unsigned int n = 0; n < digital_buffer_frames; n++){ + uint16_t inputs=digitalBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs uint16_t outputs=~inputs; //half-word has 1 for outputs and one for inputs; - matrixGpioBuffer1[n]=(matrixGpioBufferTemp[n]&(outputs<<16))| //keep output values set in previous matrixGpioBuffer1[n] - (matrixGpioBuffer1[n]&(inputs<<16)) | //inputs from current matrixGpioBuffer1[n]; - (matrixGpioBufferTemp[n]&(inputs)); //keep pin configuration from previous matrixGpioBuffer1[n] -// matrixGpioBuffer1[n]=matrixGpioBufferTemp[n]; //ignores inputs + digitalBuffer1[n]=(digitalBufferTemp[n]&(outputs<<16))| //keep output values set in previous digitalBuffer1[n] + (digitalBuffer1[n]&(inputs<<16)) | //inputs from current digitalBuffer1[n]; + (digitalBufferTemp[n]&(inputs)); //keep pin configuration from previous digitalBuffer1[n] +// digitalBuffer1[n]=digitalBufferTemp[n]; //ignores inputs } } - render(spi_buffer_frames, matrix_gpio_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, - matrixInBuffer, matrixOutBuffer, matrixGpioBuffer1); + render(spi_buffer_frames, digital_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, + analogInBuffer, analogOutBuffer, digitalBuffer1); for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) { - int out = matrixOutBuffer[n] * 65536.0; + int out = analogOutBuffer[n] * 65536.0; if(out < 0) out = 0; else if(out > 65535) out = 65535; pru_buffer_spi_dac[n + spi_buffer_frames * spi_num_channels] = (uint16_t)out; } - if(matrix_gpio_enabled){ // keep track of past matrix_gpio values - for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ - matrixGpioBufferTemp[n]=matrixGpioBuffer1[n]; + if(digital_enabled){ // keep track of past digital values + for(unsigned int n = 0; n < digital_buffer_frames; n++){ + digitalBufferTemp[n]=digitalBuffer1[n]; } } } else - render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass matrixGpioBuffer, just it is unused + render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass digitalBuffer, just it is unused // Convert float back to short for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) { @@ -594,11 +594,11 @@ // Tell PRU to stop pru_buffer_comm[PRU_SHOULD_STOP] = 1; - free(matrixOutBuffer); + free(analogOutBuffer); free(audioInBuffer); free(audioOutBuffer); - free(matrixInBuffer); - free(matrixGpioBufferTemp); + free(analogInBuffer); + free(digitalBufferTemp); } // Wait for an interrupt from the PRU indicate it is finished