Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
18:31503d9de101 | 19:c98863e63174 |
---|---|
14 */ | 14 */ |
15 | 15 |
16 #include "../include/PRU.h" | 16 #include "../include/PRU.h" |
17 #include "../include/prussdrv.h" | 17 #include "../include/prussdrv.h" |
18 #include "../include/pruss_intc_mapping.h" | 18 #include "../include/pruss_intc_mapping.h" |
19 #include "../include/matrix_gpio_mapping.h" | 19 #include "../include/digital_gpio_mapping.h" |
20 #include "../include/GPIOcontrol.h" | 20 #include "../include/GPIOcontrol.h" |
21 #include "../include/render.h" | 21 #include "../include/render.h" |
22 #include "../include/pru_rtaudio_bin.h" | 22 #include "../include/pru_rtaudio_bin.h" |
23 | 23 |
24 #include <iostream> | 24 #include <iostream> |
40 #define PRU_MEM_MCASP_OFFSET 0x2000 // Offset within PRU-SHARED RAM | 40 #define PRU_MEM_MCASP_OFFSET 0x2000 // Offset within PRU-SHARED RAM |
41 #define PRU_MEM_MCASP_LENGTH 0x2000 // Length of McASP memory, in bytes | 41 #define PRU_MEM_MCASP_LENGTH 0x2000 // Length of McASP memory, in bytes |
42 #define PRU_MEM_DAC_OFFSET 0x0 // Offset within PRU0 RAM | 42 #define PRU_MEM_DAC_OFFSET 0x0 // Offset within PRU0 RAM |
43 #define PRU_MEM_DAC_LENGTH 0x2000 // Length of ADC+DAC memory, in bytes | 43 #define PRU_MEM_DAC_LENGTH 0x2000 // Length of ADC+DAC memory, in bytes |
44 #define PRU_MEM_COMM_OFFSET 0x0 // Offset within PRU-SHARED RAM | 44 #define PRU_MEM_COMM_OFFSET 0x0 // Offset within PRU-SHARED RAM |
45 #define PRU_MEM_MATRIX_GPIO_OFFSET 0x1000 //Offset within PRU-SHARED RAM | 45 #define PRU_MEM_DIGITAL_OFFSET 0x1000 //Offset within PRU-SHARED RAM |
46 #define MEM_MATRIX_GPIO_BUFFER1_OFFSET 0x400 //Start pointer to MATRIX_GPIO_BUFFER1, which is 256 words. | 46 #define MEM_DIGITAL_BUFFER1_OFFSET 0x400 //Start pointer to DIGITAL_BUFFER1, which is 256 words. |
47 // 256 is the maximum number of frames allowed | 47 // 256 is the maximum number of frames allowed |
48 #define PRU_SHOULD_STOP 0 | 48 #define PRU_SHOULD_STOP 0 |
49 #define PRU_CURRENT_BUFFER 1 | 49 #define PRU_CURRENT_BUFFER 1 |
50 #define PRU_BUFFER_FRAMES 2 | 50 #define PRU_BUFFER_FRAMES 2 |
51 #define PRU_SHOULD_SYNC 3 | 51 #define PRU_SHOULD_SYNC 3 |
54 #define PRU_LED_ADDRESS 6 | 54 #define PRU_LED_ADDRESS 6 |
55 #define PRU_LED_PIN_MASK 7 | 55 #define PRU_LED_PIN_MASK 7 |
56 #define PRU_FRAME_COUNT 8 | 56 #define PRU_FRAME_COUNT 8 |
57 #define PRU_USE_SPI 9 | 57 #define PRU_USE_SPI 9 |
58 #define PRU_SPI_NUM_CHANNELS 10 | 58 #define PRU_SPI_NUM_CHANNELS 10 |
59 #define PRU_USE_GPIO_MATRIX 11 | 59 #define PRU_USE_GPIO_ANALOG 11 |
60 | 60 |
61 short int matrixGpioPins[NUM_MATRIX_GPIOS]={ | 61 short int digitalPins[NUM_DIGITALS]={ |
62 GPIO_NO_BIT_0, | 62 GPIO_NO_BIT_0, |
63 GPIO_NO_BIT_1, | 63 GPIO_NO_BIT_1, |
64 GPIO_NO_BIT_2, | 64 GPIO_NO_BIT_2, |
65 GPIO_NO_BIT_3, | 65 GPIO_NO_BIT_3, |
66 GPIO_NO_BIT_4, | 66 GPIO_NO_BIT_4, |
125 // If include_test_pin is set, the GPIO output | 125 // If include_test_pin is set, the GPIO output |
126 // is also prepared for an output which can be | 126 // is also prepared for an output which can be |
127 // viewed on a scope. If include_led is set, | 127 // viewed on a scope. If include_led is set, |
128 // user LED 3 on the BBB is taken over by the PRU | 128 // user LED 3 on the BBB is taken over by the PRU |
129 // to indicate activity | 129 // to indicate activity |
130 int PRU::prepareGPIO(int use_spi, int use_matrix_gpio, int include_test_pin, int include_led) | 130 int PRU::prepareGPIO(int use_spi, int use_digital, int include_test_pin, int include_led) |
131 { | 131 { |
132 if(use_spi) { | 132 if(use_spi) { |
133 // Prepare DAC CS/ pin: output, high to begin | 133 // Prepare DAC CS/ pin: output, high to begin |
134 if(gpio_export(kPruGPIODACSyncPin)) { | 134 if(gpio_export(kPruGPIODACSyncPin)) { |
135 if(gRTAudioVerbose) | 135 if(gRTAudioVerbose) |
163 } | 163 } |
164 | 164 |
165 spi_enabled = true; | 165 spi_enabled = true; |
166 } | 166 } |
167 | 167 |
168 if(use_matrix_gpio){ | 168 if(use_digital){ |
169 printf("gNumMatrixGpioChannels: %d;\n",gNumMatrixGpioChannels); | 169 printf("gNumDigitalChannels: %d;\n",gNumDigitalChannels); |
170 for(int i=0; i<gNumMatrixGpioChannels; i++){ | 170 for(int i=0; i<gNumDigitalChannels; i++){ |
171 if(gpio_export(matrixGpioPins[i])) { | 171 if(gpio_export(digitalPins[i])) { |
172 if(gRTAudioVerbose) | 172 if(gRTAudioVerbose) |
173 cout << "Warning: couldn't export matrix GPIO pin " << matrixGpioPins[i] << "\n"; | 173 cout << "Warning: couldn't export digital GPIO pin " << digitalPins[i] << "\n"; |
174 } | 174 } |
175 if(gpio_set_dir(matrixGpioPins[i], OUTPUT_PIN)) { | 175 if(gpio_set_dir(digitalPins[i], OUTPUT_PIN)) { |
176 if(gRTAudioVerbose) | 176 if(gRTAudioVerbose) |
177 cout << "Couldn't set direction on matrix GPIO pin " << matrixGpioPins[i] << "\n"; | 177 cout << "Couldn't set direction on digital GPIO pin " << digitalPins[i] << "\n"; |
178 return -1; | 178 return -1; |
179 } | 179 } |
180 if(gpio_set_value(matrixGpioPins[i], HIGH)) { | 180 if(gpio_set_value(digitalPins[i], HIGH)) { |
181 if(gRTAudioVerbose) | 181 if(gRTAudioVerbose) |
182 cout << "Couldn't set value on matrix GPIO pin " << matrixGpioPins[i] << "\n"; | 182 cout << "Couldn't set value on digital GPIO pin " << digitalPins[i] << "\n"; |
183 return -1; | 183 return -1; |
184 } | 184 } |
185 } | 185 } |
186 matrix_gpio_enabled=true; | 186 digital_enabled=true; |
187 } | 187 } |
188 | 188 |
189 if(include_test_pin) { | 189 if(include_test_pin) { |
190 // Prepare GPIO test output (for debugging), low to begin | 190 // Prepare GPIO test output (for debugging), low to begin |
191 if(gpio_export(kPruGPIOTestPin)) { | 191 if(gpio_export(kPruGPIOTestPin)) { |
253 return; | 253 return; |
254 if(spi_enabled) { | 254 if(spi_enabled) { |
255 gpio_unexport(kPruGPIODACSyncPin); | 255 gpio_unexport(kPruGPIODACSyncPin); |
256 gpio_unexport(kPruGPIOADCSyncPin); | 256 gpio_unexport(kPruGPIOADCSyncPin); |
257 } | 257 } |
258 if(matrix_gpio_enabled){ | 258 if(digital_enabled){ |
259 for(int i=0; i<gNumMatrixGpioChannels; i++){ | 259 for(int i=0; i<gNumDigitalChannels; i++){ |
260 gpio_unexport(matrixGpioPins[i]); | 260 gpio_unexport(digitalPins[i]); |
261 } | 261 } |
262 } | 262 } |
263 if(gpio_test_pin_enabled) { | 263 if(gpio_test_pin_enabled) { |
264 gpio_unexport(kPruGPIOTestPin); | 264 gpio_unexport(kPruGPIOTestPin); |
265 gpio_unexport(kPruGPIOTestPin2); | 265 gpio_unexport(kPruGPIOTestPin2); |
307 /* Map PRU's INTC */ | 307 /* Map PRU's INTC */ |
308 prussdrv_pruintc_init(&pruss_intc_initdata); | 308 prussdrv_pruintc_init(&pruss_intc_initdata); |
309 | 309 |
310 spi_buffer_frames = frames_per_buffer; | 310 spi_buffer_frames = frames_per_buffer; |
311 audio_buffer_frames = spi_buffer_frames * spi_num_channels / 4; | 311 audio_buffer_frames = spi_buffer_frames * spi_num_channels / 4; |
312 matrix_gpio_buffer_frames = audio_buffer_frames; | 312 digital_buffer_frames = audio_buffer_frames; |
313 | 313 |
314 /* Map PRU memory to pointers */ | 314 /* Map PRU memory to pointers */ |
315 prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem); | 315 prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem); |
316 pru_buffer_comm = (uint32_t *)&pruMem[PRU_MEM_COMM_OFFSET/sizeof(uint32_t)]; | 316 pru_buffer_comm = (uint32_t *)&pruMem[PRU_MEM_COMM_OFFSET/sizeof(uint32_t)]; |
317 pru_buffer_audio_dac = (int16_t *)&pruMem[PRU_MEM_MCASP_OFFSET/sizeof(uint32_t)]; | 317 pru_buffer_audio_dac = (int16_t *)&pruMem[PRU_MEM_MCASP_OFFSET/sizeof(uint32_t)]; |
328 } | 328 } |
329 else { | 329 else { |
330 pru_buffer_spi_dac = pru_buffer_spi_adc = 0; | 330 pru_buffer_spi_dac = pru_buffer_spi_adc = 0; |
331 } | 331 } |
332 | 332 |
333 if(matrix_gpio_enabled) { | 333 if(digital_enabled) { |
334 prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem); | 334 prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem); |
335 pru_buffer_matrix_gpio = (uint32_t *)&pruMem[PRU_MEM_MATRIX_GPIO_OFFSET/sizeof(uint32_t)]; | 335 pru_buffer_digital = (uint32_t *)&pruMem[PRU_MEM_DIGITAL_OFFSET/sizeof(uint32_t)]; |
336 } | 336 } |
337 else { | 337 else { |
338 pru_buffer_matrix_gpio = 0; | 338 pru_buffer_digital = 0; |
339 } | 339 } |
340 /* Set up flags */ | 340 /* Set up flags */ |
341 pru_buffer_comm[PRU_SHOULD_STOP] = 0; | 341 pru_buffer_comm[PRU_SHOULD_STOP] = 0; |
342 pru_buffer_comm[PRU_CURRENT_BUFFER] = 0; | 342 pru_buffer_comm[PRU_CURRENT_BUFFER] = 0; |
343 pru_buffer_comm[PRU_BUFFER_FRAMES] = spi_buffer_frames; | 343 pru_buffer_comm[PRU_BUFFER_FRAMES] = spi_buffer_frames; |
358 } | 358 } |
359 else { | 359 else { |
360 pru_buffer_comm[PRU_USE_SPI] = 0; | 360 pru_buffer_comm[PRU_USE_SPI] = 0; |
361 pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = 0; | 361 pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = 0; |
362 } | 362 } |
363 if(matrix_gpio_enabled) { | 363 if(digital_enabled) { |
364 pru_buffer_comm[PRU_USE_GPIO_MATRIX] = 1; | 364 pru_buffer_comm[PRU_USE_GPIO_ANALOG] = 1; |
365 pru_buffer_comm[NUM_MATRIX_GPIOS] = spi_num_channels; | 365 pru_buffer_comm[NUM_DIGITALS] = spi_num_channels; |
366 } | 366 } |
367 else { | 367 else { |
368 pru_buffer_comm[PRU_USE_GPIO_MATRIX] = 0; | 368 pru_buffer_comm[PRU_USE_GPIO_ANALOG] = 0; |
369 pru_buffer_comm[NUM_MATRIX_GPIOS] = 0; | 369 pru_buffer_comm[NUM_DIGITALS] = 0; |
370 } | 370 } |
371 | 371 |
372 /* Clear ADC and DAC memory */ | 372 /* Clear ADC and DAC memory */ |
373 if(spi_enabled) { | 373 if(spi_enabled) { |
374 for(int i = 0; i < PRU_MEM_DAC_LENGTH / 2; i++) | 374 for(int i = 0; i < PRU_MEM_DAC_LENGTH / 2; i++) |
430 void PRU::loop() | 430 void PRU::loop() |
431 { | 431 { |
432 // Polling interval is 1/4 of the period | 432 // Polling interval is 1/4 of the period |
433 RTIME sleepTime = PRU_SAMPLE_INTERVAL_NS * (spi_num_channels / 2) * spi_buffer_frames / 4; | 433 RTIME sleepTime = PRU_SAMPLE_INTERVAL_NS * (spi_num_channels / 2) * spi_buffer_frames / 4; |
434 float *audioInBuffer, *audioOutBuffer; | 434 float *audioInBuffer, *audioOutBuffer; |
435 float *matrixInBuffer, *matrixOutBuffer; | 435 float *analogInBuffer, *analogOutBuffer; |
436 uint32_t *matrixGpioBuffer0, *matrixGpioBuffer1, *matrixGpioBufferTemp; | 436 uint32_t *digitalBuffer0, *digitalBuffer1, *digitalBufferTemp; |
437 | 437 |
438 audioInBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float)); | 438 audioInBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float)); |
439 audioOutBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float)); | 439 audioOutBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float)); |
440 matrixInBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); | 440 analogInBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); |
441 matrixOutBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); | 441 analogOutBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float)); |
442 matrixGpioBuffer0 = pru_buffer_matrix_gpio; | 442 digitalBuffer0 = pru_buffer_digital; |
443 matrixGpioBuffer1 = pru_buffer_matrix_gpio+MEM_MATRIX_GPIO_BUFFER1_OFFSET/sizeof(uint32_t); | 443 digitalBuffer1 = pru_buffer_digital+MEM_DIGITAL_BUFFER1_OFFSET/sizeof(uint32_t); |
444 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.: | 444 digital_buffer_frames = digital_enabled ? audio_buffer_frames : 0; //TODO: find a more elegant solution for when the digital is disabled e.g.: |
445 // - embed in the digitalWrite/Read macros a check whether matrix_gpio is enabled | 445 // - embed in the digitalWrite/Read macros a check whether digital is enabled |
446 // - allocate some memory in ARM just to allow render() to run regardless. | 446 // - allocate some memory in ARM just to allow render() to run regardless. |
447 // in this case it can be matrixGpioBuffer0 == matrixGpioBuffer1 | 447 // in this case it can be digitalBuffer0 == digitalBuffer1 |
448 printf("matrix_gpio_buffer_frames: %d;\n",matrix_gpio_buffer_frames); | 448 printf("digital_buffer_frames: %d;\n",digital_buffer_frames); |
449 matrixGpioBufferTemp = (uint32_t *)malloc(matrix_gpio_buffer_frames*sizeof(uint32_t)); //temp buffer to hold previous states | 449 digitalBufferTemp = (uint32_t *)malloc(digital_buffer_frames*sizeof(uint32_t)); //temp buffer to hold previous states |
450 if(audioInBuffer == 0 || audioOutBuffer == 0) { | 450 if(audioInBuffer == 0 || audioOutBuffer == 0) { |
451 rt_printf("Error: couldn't allocate audio buffers\n"); | 451 rt_printf("Error: couldn't allocate audio buffers\n"); |
452 return; | 452 return; |
453 } | 453 } |
454 if(matrixInBuffer == 0 || matrixOutBuffer == 0) { | 454 if(analogInBuffer == 0 || analogOutBuffer == 0) { |
455 rt_printf("Error: couldn't allocate matrix buffers\n"); | 455 rt_printf("Error: couldn't allocate analog buffers\n"); |
456 return; | 456 return; |
457 } | 457 } |
458 if(matrixGpioBufferTemp == 0) { | 458 if(digitalBufferTemp == 0) { |
459 rt_printf("Error: couldn't allocate matrix GPIO buffers\n"); | 459 rt_printf("Error: couldn't allocate digital buffers\n"); |
460 return; | 460 return; |
461 } | 461 } |
462 | 462 |
463 while(!gShouldStop) { | 463 while(!gShouldStop) { |
464 // Wait for PRU to move to buffer 1 | 464 // Wait for PRU to move to buffer 1 |
478 // Convert short (16-bit) samples to float | 478 // Convert short (16-bit) samples to float |
479 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) | 479 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) |
480 audioInBuffer[n] = (float)pru_buffer_audio_adc[n] / 32768.0; | 480 audioInBuffer[n] = (float)pru_buffer_audio_adc[n] / 32768.0; |
481 if(spi_enabled) { | 481 if(spi_enabled) { |
482 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) | 482 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) |
483 matrixInBuffer[n] = (float)pru_buffer_spi_adc[n] / 65536.0; | 483 analogInBuffer[n] = (float)pru_buffer_spi_adc[n] / 65536.0; |
484 //use past matrix_gpio values to initialize the array properly: | 484 //use past digital values to initialize the array properly: |
485 //- pins previously set as outputs will keep their previously set output value, | 485 //- pins previously set as outputs will keep their previously set output value, |
486 //- pins previously set as inputs will carry the newly read input value | 486 //- pins previously set as inputs will carry the newly read input value |
487 if(matrix_gpio_enabled){ | 487 if(digital_enabled){ |
488 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ | 488 for(unsigned int n = 0; n < digital_buffer_frames; n++){ |
489 uint16_t inputs=matrixGpioBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs | 489 uint16_t inputs=digitalBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs |
490 // printf("inputs: 0x%x\n",inputs); | 490 // printf("inputs: 0x%x\n",inputs); |
491 uint16_t outputs=~inputs; //half-word has 1 for outputs and 0 for inputs; | 491 uint16_t outputs=~inputs; //half-word has 1 for outputs and 0 for inputs; |
492 matrixGpioBuffer0[n]=(matrixGpioBufferTemp[n]&(outputs<<16))| //keep output values set in previous matrixGpioBuffer1[n] | 492 digitalBuffer0[n]=(digitalBufferTemp[n]&(outputs<<16))| //keep output values set in previous digitalBuffer1[n] |
493 (matrixGpioBuffer0[n]&(inputs<<16)) | //inputs from current matrixGpioBuffer0[n]; | 493 (digitalBuffer0[n]&(inputs<<16)) | //inputs from current digitalBuffer0[n]; |
494 (matrixGpioBufferTemp[n]&(inputs)); //keep pin configuration from previous matrixGpioBuffer1[n] | 494 (digitalBufferTemp[n]&(inputs)); //keep pin configuration from previous digitalBuffer1[n] |
495 // matrixGpioBuffer0[n]=matrixGpioBufferTemp[n]; //ignores inputs | 495 // digitalBuffer0[n]=digitalBufferTemp[n]; //ignores inputs |
496 } | 496 } |
497 } | 497 } |
498 render(spi_buffer_frames, matrix_gpio_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, | 498 render(spi_buffer_frames, digital_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, |
499 matrixInBuffer, matrixOutBuffer, matrixGpioBuffer0); | 499 analogInBuffer, analogOutBuffer, digitalBuffer0); |
500 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) { | 500 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) { |
501 int out = matrixOutBuffer[n] * 65536.0; | 501 int out = analogOutBuffer[n] * 65536.0; |
502 if(out < 0) out = 0; | 502 if(out < 0) out = 0; |
503 else if(out > 65535) out = 65535; | 503 else if(out > 65535) out = 65535; |
504 pru_buffer_spi_dac[n] = (uint16_t)out; | 504 pru_buffer_spi_dac[n] = (uint16_t)out; |
505 } | 505 } |
506 if(matrix_gpio_enabled){ // keep track of past matrix_gpio values | 506 if(digital_enabled){ // keep track of past digital values |
507 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ | 507 for(unsigned int n = 0; n < digital_buffer_frames; n++){ |
508 matrixGpioBufferTemp[n]=matrixGpioBuffer0[n]; | 508 digitalBufferTemp[n]=digitalBuffer0[n]; |
509 } | 509 } |
510 } | 510 } |
511 } | 511 } |
512 else | 512 else |
513 render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass matrixGpioBuffer, just it is unused | 513 render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass digitalBuffer, just it is unused |
514 // Convert float back to short | 514 // Convert float back to short |
515 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) { | 515 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) { |
516 int out = audioOutBuffer[n] * 32768.0; | 516 int out = audioOutBuffer[n] * 32768.0; |
517 if(out < -32768) out = -32768; | 517 if(out < -32768) out = -32768; |
518 else if(out > 32767) out = 32767; | 518 else if(out > 32767) out = 32767; |
543 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) | 543 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) |
544 audioInBuffer[n] = (float)pru_buffer_audio_adc[n + audio_buffer_frames * 2] / 32768.0; | 544 audioInBuffer[n] = (float)pru_buffer_audio_adc[n + audio_buffer_frames * 2] / 32768.0; |
545 | 545 |
546 if(spi_enabled) { | 546 if(spi_enabled) { |
547 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) | 547 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) |
548 matrixInBuffer[n] = (float)pru_buffer_spi_adc[n + spi_buffer_frames * spi_num_channels] / 65536.0; | 548 analogInBuffer[n] = (float)pru_buffer_spi_adc[n + spi_buffer_frames * spi_num_channels] / 65536.0; |
549 | 549 |
550 //use past matrix_gpio values to initialize the array properly: | 550 //use past digital values to initialize the array properly: |
551 //- pins previously set as outputs will keep their previously set output value, | 551 //- pins previously set as outputs will keep their previously set output value, |
552 //- pins previously set as inputs will carry the newly read input value | 552 //- pins previously set as inputs will carry the newly read input value |
553 if(matrix_gpio_enabled){ | 553 if(digital_enabled){ |
554 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ | 554 for(unsigned int n = 0; n < digital_buffer_frames; n++){ |
555 uint16_t inputs=matrixGpioBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs | 555 uint16_t inputs=digitalBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs |
556 uint16_t outputs=~inputs; //half-word has 1 for outputs and one for inputs; | 556 uint16_t outputs=~inputs; //half-word has 1 for outputs and one for inputs; |
557 matrixGpioBuffer1[n]=(matrixGpioBufferTemp[n]&(outputs<<16))| //keep output values set in previous matrixGpioBuffer1[n] | 557 digitalBuffer1[n]=(digitalBufferTemp[n]&(outputs<<16))| //keep output values set in previous digitalBuffer1[n] |
558 (matrixGpioBuffer1[n]&(inputs<<16)) | //inputs from current matrixGpioBuffer1[n]; | 558 (digitalBuffer1[n]&(inputs<<16)) | //inputs from current digitalBuffer1[n]; |
559 (matrixGpioBufferTemp[n]&(inputs)); //keep pin configuration from previous matrixGpioBuffer1[n] | 559 (digitalBufferTemp[n]&(inputs)); //keep pin configuration from previous digitalBuffer1[n] |
560 // matrixGpioBuffer1[n]=matrixGpioBufferTemp[n]; //ignores inputs | 560 // digitalBuffer1[n]=digitalBufferTemp[n]; //ignores inputs |
561 } | 561 } |
562 } | 562 } |
563 render(spi_buffer_frames, matrix_gpio_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, | 563 render(spi_buffer_frames, digital_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer, |
564 matrixInBuffer, matrixOutBuffer, matrixGpioBuffer1); | 564 analogInBuffer, analogOutBuffer, digitalBuffer1); |
565 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) { | 565 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) { |
566 int out = matrixOutBuffer[n] * 65536.0; | 566 int out = analogOutBuffer[n] * 65536.0; |
567 if(out < 0) out = 0; | 567 if(out < 0) out = 0; |
568 else if(out > 65535) out = 65535; | 568 else if(out > 65535) out = 65535; |
569 pru_buffer_spi_dac[n + spi_buffer_frames * spi_num_channels] = (uint16_t)out; | 569 pru_buffer_spi_dac[n + spi_buffer_frames * spi_num_channels] = (uint16_t)out; |
570 } | 570 } |
571 if(matrix_gpio_enabled){ // keep track of past matrix_gpio values | 571 if(digital_enabled){ // keep track of past digital values |
572 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){ | 572 for(unsigned int n = 0; n < digital_buffer_frames; n++){ |
573 matrixGpioBufferTemp[n]=matrixGpioBuffer1[n]; | 573 digitalBufferTemp[n]=digitalBuffer1[n]; |
574 } | 574 } |
575 } | 575 } |
576 } | 576 } |
577 else | 577 else |
578 render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass matrixGpioBuffer, just it is unused | 578 render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass digitalBuffer, just it is unused |
579 | 579 |
580 // Convert float back to short | 580 // Convert float back to short |
581 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) { | 581 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) { |
582 int out = audioOutBuffer[n] * 32768.0; | 582 int out = audioOutBuffer[n] * 32768.0; |
583 if(out < -32768) out = -32768; | 583 if(out < -32768) out = -32768; |
592 } | 592 } |
593 | 593 |
594 // Tell PRU to stop | 594 // Tell PRU to stop |
595 pru_buffer_comm[PRU_SHOULD_STOP] = 1; | 595 pru_buffer_comm[PRU_SHOULD_STOP] = 1; |
596 | 596 |
597 free(matrixOutBuffer); | 597 free(analogOutBuffer); |
598 free(audioInBuffer); | 598 free(audioInBuffer); |
599 free(audioOutBuffer); | 599 free(audioOutBuffer); |
600 free(matrixInBuffer); | 600 free(analogInBuffer); |
601 free(matrixGpioBufferTemp); | 601 free(digitalBufferTemp); |
602 } | 602 } |
603 | 603 |
604 // Wait for an interrupt from the PRU indicate it is finished | 604 // Wait for an interrupt from the PRU indicate it is finished |
605 void PRU::waitForFinish() | 605 void PRU::waitForFinish() |
606 { | 606 { |