annotate core/PRU.cpp @ 16:670be80463a3 matrix_gpio

- analog matrixIn/matrixOut are now mapped as floats from 0 to 1 - use of an external PRU code can be enabled with -P <filename> - 16 channels of programmable GPIO can be accessed straight from render() either writing directly to the matrixGpio[] array or using digitalWrite(), digitalRead(), setDigitalDirection() macros from Utilities.h .
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 27 Apr 2015 13:01:57 +0100
parents 901d205d1a3c
children c98863e63174
rev   line source
andrewm@0 1 /*
andrewm@0 2 * PRU.cpp
andrewm@0 3 *
andrewm@0 4 * Code for communicating with the Programmable Realtime Unit (PRU)
andrewm@0 5 * on the BeagleBone AM335x series processors. The PRU loads and runs
andrewm@0 6 * a separate code image compiled from an assembly file. Here it is
andrewm@0 7 * used to handle audio and SPI ADC/DAC data.
andrewm@0 8 *
andrewm@0 9 * This code is specific to the PRU code in the assembly file; for example,
andrewm@0 10 * it uses certain GPIO resources that correspond to that image.
andrewm@0 11 *
andrewm@0 12 * Created on: May 27, 2014
andrewm@0 13 * Author: andrewm
andrewm@0 14 */
andrewm@0 15
andrewm@0 16 #include "../include/PRU.h"
andrewm@0 17 #include "../include/prussdrv.h"
andrewm@0 18 #include "../include/pruss_intc_mapping.h"
giuliomoro@16 19 #include "../include/matrix_gpio_mapping.h"
andrewm@0 20 #include "../include/GPIOcontrol.h"
andrewm@0 21 #include "../include/render.h"
andrewm@15 22 #include "../include/pru_rtaudio_bin.h"
andrewm@0 23
andrewm@0 24 #include <iostream>
andrewm@0 25 #include <stdlib.h>
andrewm@0 26 #include <cstdio>
andrewm@0 27 #include <cerrno>
andrewm@0 28 #include <fcntl.h>
andrewm@0 29 #include <sys/mman.h>
giuliomoro@16 30 #include <unistd.h>
andrewm@0 31
andrewm@0 32 // Xenomai-specific includes
andrewm@0 33 #include <sys/mman.h>
andrewm@0 34 #include <native/task.h>
andrewm@0 35 #include <native/timer.h>
andrewm@0 36 #include <rtdk.h>
andrewm@0 37
andrewm@0 38 using namespace std;
andrewm@0 39
andrewm@0 40 #define PRU_MEM_MCASP_OFFSET 0x2000 // Offset within PRU-SHARED RAM
andrewm@0 41 #define PRU_MEM_MCASP_LENGTH 0x2000 // Length of McASP memory, in bytes
andrewm@0 42 #define PRU_MEM_DAC_OFFSET 0x0 // Offset within PRU0 RAM
andrewm@0 43 #define PRU_MEM_DAC_LENGTH 0x2000 // Length of ADC+DAC memory, in bytes
andrewm@0 44 #define PRU_MEM_COMM_OFFSET 0x0 // Offset within PRU-SHARED RAM
giuliomoro@16 45 #define PRU_MEM_MATRIX_GPIO_OFFSET 0x1000 //Offset within PRU-SHARED RAM
giuliomoro@16 46 #define MEM_MATRIX_GPIO_BUFFER1_OFFSET 0x400 //Start pointer to MATRIX_GPIO_BUFFER1, which is 256 words.
giuliomoro@16 47 // 256 is the maximum number of frames allowed
andrewm@0 48 #define PRU_SHOULD_STOP 0
andrewm@0 49 #define PRU_CURRENT_BUFFER 1
andrewm@0 50 #define PRU_BUFFER_FRAMES 2
andrewm@0 51 #define PRU_SHOULD_SYNC 3
andrewm@0 52 #define PRU_SYNC_ADDRESS 4
andrewm@0 53 #define PRU_SYNC_PIN_MASK 5
andrewm@0 54 #define PRU_LED_ADDRESS 6
andrewm@0 55 #define PRU_LED_PIN_MASK 7
andrewm@0 56 #define PRU_FRAME_COUNT 8
andrewm@0 57 #define PRU_USE_SPI 9
andrewm@12 58 #define PRU_SPI_NUM_CHANNELS 10
giuliomoro@16 59 #define PRU_USE_GPIO_MATRIX 11
giuliomoro@16 60
giuliomoro@16 61 short int matrixGpioPins[NUM_MATRIX_GPIOS]={
giuliomoro@16 62 GPIO_NO_BIT_0,
giuliomoro@16 63 GPIO_NO_BIT_1,
giuliomoro@16 64 GPIO_NO_BIT_2,
giuliomoro@16 65 GPIO_NO_BIT_3,
giuliomoro@16 66 GPIO_NO_BIT_4,
giuliomoro@16 67 GPIO_NO_BIT_5,
giuliomoro@16 68 GPIO_NO_BIT_6,
giuliomoro@16 69 GPIO_NO_BIT_7,
giuliomoro@16 70 GPIO_NO_BIT_8,
giuliomoro@16 71 GPIO_NO_BIT_9,
giuliomoro@16 72 GPIO_NO_BIT_10,
giuliomoro@16 73 GPIO_NO_BIT_11,
giuliomoro@16 74 GPIO_NO_BIT_12,
giuliomoro@16 75 GPIO_NO_BIT_13,
giuliomoro@16 76 GPIO_NO_BIT_14,
giuliomoro@16 77 GPIO_NO_BIT_15,
giuliomoro@16 78 };
andrewm@0 79
andrewm@12 80 #define PRU_SAMPLE_INTERVAL_NS 11338 // 88200Hz per SPI sample = 11.338us
andrewm@0 81
andrewm@0 82 #define GPIO0_ADDRESS 0x44E07000
andrewm@0 83 #define GPIO1_ADDRESS 0x4804C000
andrewm@0 84 #define GPIO_SIZE 0x198
andrewm@0 85 #define GPIO_CLEARDATAOUT (0x190 / 4)
andrewm@0 86 #define GPIO_SETDATAOUT (0x194 / 4)
andrewm@0 87
andrewm@0 88 #define TEST_PIN_GPIO_BASE GPIO0_ADDRESS // Use GPIO0(31) for debugging
andrewm@0 89 #define TEST_PIN_MASK (1 << 31)
andrewm@0 90 #define TEST_PIN2_MASK (1 << 26)
andrewm@0 91
andrewm@0 92 #define USERLED3_GPIO_BASE GPIO1_ADDRESS // GPIO1(24) is user LED 3
andrewm@0 93 #define USERLED3_PIN_MASK (1 << 24)
andrewm@0 94
andrewm@0 95 const unsigned int PRU::kPruGPIODACSyncPin = 5; // GPIO0(5); P9-17
andrewm@0 96 const unsigned int PRU::kPruGPIOADCSyncPin = 48; // GPIO1(16); P9-15
andrewm@0 97
andrewm@0 98 const unsigned int PRU::kPruGPIOTestPin = 60; // GPIO1(28); P9-12
andrewm@0 99 const unsigned int PRU::kPruGPIOTestPin2 = 31; // GPIO0(31); P9-13
andrewm@0 100 const unsigned int PRU::kPruGPIOTestPin3 = 26; // GPIO0(26); P8-14
andrewm@0 101
andrewm@0 102 extern int gShouldStop;
andrewm@0 103 extern int gRTAudioVerbose;
andrewm@0 104
andrewm@0 105 // Constructor: specify a PRU number (0 or 1)
andrewm@0 106 PRU::PRU()
andrewm@0 107 : pru_number(0), running(false), spi_enabled(false), gpio_enabled(false), led_enabled(false),
andrewm@12 108 gpio_test_pin_enabled(false), spi_num_channels(0), xenomai_gpio_fd(-1), xenomai_gpio(0)
andrewm@0 109 {
andrewm@0 110
andrewm@0 111 }
andrewm@0 112
andrewm@0 113 // Destructor
andrewm@0 114 PRU::~PRU()
andrewm@0 115 {
andrewm@0 116 if(running)
andrewm@0 117 disable();
andrewm@0 118 if(gpio_enabled)
andrewm@0 119 cleanupGPIO();
andrewm@0 120 if(xenomai_gpio_fd >= 0)
andrewm@0 121 close(xenomai_gpio_fd);
andrewm@0 122 }
andrewm@0 123
andrewm@0 124 // Prepare the GPIO pins needed for the PRU
andrewm@0 125 // If include_test_pin is set, the GPIO output
andrewm@0 126 // is also prepared for an output which can be
andrewm@0 127 // viewed on a scope. If include_led is set,
andrewm@0 128 // user LED 3 on the BBB is taken over by the PRU
andrewm@0 129 // to indicate activity
giuliomoro@16 130 int PRU::prepareGPIO(int use_spi, int use_matrix_gpio, int include_test_pin, int include_led)
andrewm@0 131 {
andrewm@0 132 if(use_spi) {
andrewm@0 133 // Prepare DAC CS/ pin: output, high to begin
andrewm@0 134 if(gpio_export(kPruGPIODACSyncPin)) {
andrewm@0 135 if(gRTAudioVerbose)
andrewm@0 136 cout << "Warning: couldn't export DAC sync pin\n";
andrewm@0 137 }
andrewm@0 138 if(gpio_set_dir(kPruGPIODACSyncPin, OUTPUT_PIN)) {
andrewm@0 139 if(gRTAudioVerbose)
andrewm@0 140 cout << "Couldn't set direction on DAC sync pin\n";
andrewm@0 141 return -1;
andrewm@0 142 }
andrewm@0 143 if(gpio_set_value(kPruGPIODACSyncPin, HIGH)) {
andrewm@0 144 if(gRTAudioVerbose)
andrewm@0 145 cout << "Couldn't set value on DAC sync pin\n";
andrewm@0 146 return -1;
andrewm@0 147 }
andrewm@0 148
andrewm@0 149 // Prepare ADC CS/ pin: output, high to begin
andrewm@0 150 if(gpio_export(kPruGPIOADCSyncPin)) {
andrewm@0 151 if(gRTAudioVerbose)
andrewm@0 152 cout << "Warning: couldn't export ADC sync pin\n";
andrewm@0 153 }
andrewm@0 154 if(gpio_set_dir(kPruGPIOADCSyncPin, OUTPUT_PIN)) {
andrewm@0 155 if(gRTAudioVerbose)
andrewm@0 156 cout << "Couldn't set direction on ADC sync pin\n";
andrewm@0 157 return -1;
andrewm@0 158 }
andrewm@0 159 if(gpio_set_value(kPruGPIOADCSyncPin, HIGH)) {
andrewm@0 160 if(gRTAudioVerbose)
andrewm@0 161 cout << "Couldn't set value on ADC sync pin\n";
andrewm@0 162 return -1;
andrewm@0 163 }
andrewm@0 164
andrewm@0 165 spi_enabled = true;
andrewm@0 166 }
andrewm@0 167
giuliomoro@16 168 if(use_matrix_gpio){
giuliomoro@16 169 printf("gNumMatrixGpioChannels: %d;\n",gNumMatrixGpioChannels);
giuliomoro@16 170 for(int i=0; i<gNumMatrixGpioChannels; i++){
giuliomoro@16 171 if(gpio_export(matrixGpioPins[i])) {
giuliomoro@16 172 if(gRTAudioVerbose)
giuliomoro@16 173 cout << "Warning: couldn't export matrix GPIO pin " << matrixGpioPins[i] << "\n";
giuliomoro@16 174 }
giuliomoro@16 175 if(gpio_set_dir(matrixGpioPins[i], OUTPUT_PIN)) {
giuliomoro@16 176 if(gRTAudioVerbose)
giuliomoro@16 177 cout << "Couldn't set direction on matrix GPIO pin " << matrixGpioPins[i] << "\n";
giuliomoro@16 178 return -1;
giuliomoro@16 179 }
giuliomoro@16 180 if(gpio_set_value(matrixGpioPins[i], HIGH)) {
giuliomoro@16 181 if(gRTAudioVerbose)
giuliomoro@16 182 cout << "Couldn't set value on matrix GPIO pin " << matrixGpioPins[i] << "\n";
giuliomoro@16 183 return -1;
giuliomoro@16 184 }
giuliomoro@16 185 }
giuliomoro@16 186 matrix_gpio_enabled=true;
giuliomoro@16 187 }
giuliomoro@16 188
andrewm@0 189 if(include_test_pin) {
andrewm@0 190 // Prepare GPIO test output (for debugging), low to begin
andrewm@0 191 if(gpio_export(kPruGPIOTestPin)) {
andrewm@0 192 if(gRTAudioVerbose)
andrewm@0 193 cout << "Warning: couldn't export GPIO test pin\n";
andrewm@0 194 }
andrewm@0 195 if(gpio_set_dir(kPruGPIOTestPin, OUTPUT_PIN)) {
andrewm@0 196 if(gRTAudioVerbose)
andrewm@0 197 cout << "Couldn't set direction on GPIO test pin\n";
andrewm@0 198 return -1;
andrewm@0 199 }
andrewm@0 200 if(gpio_set_value(kPruGPIOTestPin, LOW)) {
andrewm@0 201 if(gRTAudioVerbose)
andrewm@0 202 cout << "Couldn't set value on GPIO test pin\n";
andrewm@0 203 return -1;
andrewm@0 204 }
andrewm@0 205
andrewm@0 206 if(gpio_export(kPruGPIOTestPin2)) {
andrewm@0 207 if(gRTAudioVerbose)
andrewm@0 208 cout << "Warning: couldn't export GPIO test pin 2\n";
andrewm@0 209 }
andrewm@0 210 if(gpio_set_dir(kPruGPIOTestPin2, OUTPUT_PIN)) {
andrewm@0 211 if(gRTAudioVerbose)
andrewm@0 212 cout << "Couldn't set direction on GPIO test pin 2\n";
andrewm@0 213 return -1;
andrewm@0 214 }
andrewm@0 215 if(gpio_set_value(kPruGPIOTestPin2, LOW)) {
andrewm@0 216 if(gRTAudioVerbose)
andrewm@0 217 cout << "Couldn't set value on GPIO test pin 2\n";
andrewm@0 218 return -1;
andrewm@0 219 }
andrewm@0 220
andrewm@0 221 if(gpio_export(kPruGPIOTestPin3)) {
andrewm@0 222 if(gRTAudioVerbose)
andrewm@0 223 cout << "Warning: couldn't export GPIO test pin 3\n";
andrewm@0 224 }
andrewm@0 225 if(gpio_set_dir(kPruGPIOTestPin3, OUTPUT_PIN)) {
andrewm@0 226 if(gRTAudioVerbose)
andrewm@0 227 cout << "Couldn't set direction on GPIO test pin 3\n";
andrewm@0 228 return -1;
andrewm@0 229 }
andrewm@0 230 if(gpio_set_value(kPruGPIOTestPin3, LOW)) {
andrewm@0 231 if(gRTAudioVerbose)
andrewm@0 232 cout << "Couldn't set value on GPIO test pin 3\n";
andrewm@0 233 return -1;
andrewm@0 234 }
andrewm@0 235 gpio_test_pin_enabled = true;
andrewm@0 236 }
andrewm@0 237
andrewm@0 238 if(include_led) {
andrewm@0 239 // Turn off system function for LED3 so it can be reused by PRU
andrewm@0 240 led_set_trigger(3, "none");
andrewm@0 241 led_enabled = true;
andrewm@0 242 }
andrewm@0 243
andrewm@0 244 gpio_enabled = true;
andrewm@0 245
andrewm@0 246 return 0;
andrewm@0 247 }
andrewm@0 248
andrewm@0 249 // Clean up the GPIO at the end
andrewm@0 250 void PRU::cleanupGPIO()
andrewm@0 251 {
andrewm@0 252 if(!gpio_enabled)
andrewm@0 253 return;
andrewm@0 254 if(spi_enabled) {
andrewm@0 255 gpio_unexport(kPruGPIODACSyncPin);
andrewm@0 256 gpio_unexport(kPruGPIOADCSyncPin);
andrewm@0 257 }
giuliomoro@16 258 if(matrix_gpio_enabled){
giuliomoro@16 259 for(int i=0; i<gNumMatrixGpioChannels; i++){
giuliomoro@16 260 gpio_unexport(matrixGpioPins[i]);
giuliomoro@16 261 }
giuliomoro@16 262 }
andrewm@0 263 if(gpio_test_pin_enabled) {
andrewm@0 264 gpio_unexport(kPruGPIOTestPin);
andrewm@0 265 gpio_unexport(kPruGPIOTestPin2);
andrewm@0 266 gpio_unexport(kPruGPIOTestPin3);
andrewm@0 267 }
andrewm@0 268 if(led_enabled) {
andrewm@0 269 // Set LED back to default eMMC status
andrewm@0 270 // TODO: make it go back to its actual value before this program,
andrewm@0 271 // rather than the system default
andrewm@0 272 led_set_trigger(3, "mmc1");
andrewm@0 273 }
andrewm@0 274 gpio_enabled = gpio_test_pin_enabled = false;
andrewm@0 275 }
andrewm@0 276
andrewm@0 277 // Initialise and open the PRU
andrewm@12 278 int PRU::initialise(int pru_num, int frames_per_buffer, int spi_channels, bool xenomai_test_pin)
andrewm@0 279 {
andrewm@0 280 uint32_t *pruMem = 0;
andrewm@0 281
andrewm@0 282 if(!gpio_enabled) {
andrewm@0 283 rt_printf("initialise() called before GPIO enabled\n");
andrewm@0 284 return 1;
andrewm@0 285 }
andrewm@0 286
andrewm@0 287 pru_number = pru_num;
andrewm@0 288
andrewm@12 289 /* Set number of SPI ADC / DAC channels to use. This implicitly
andrewm@12 290 * also determines the sample rate relative to the audio clock
andrewm@12 291 * (half audio clock for 8 channels, full audio clock for 4,
andrewm@12 292 * double audio clock for 2)
andrewm@12 293 */
andrewm@12 294 spi_num_channels = spi_channels;
andrewm@12 295
andrewm@0 296 /* Initialize structure used by prussdrv_pruintc_intc */
andrewm@0 297 /* PRUSS_INTC_INITDATA is found in pruss_intc_mapping.h */
andrewm@0 298 tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
andrewm@0 299
andrewm@0 300 /* Allocate and initialize memory */
andrewm@0 301 prussdrv_init();
andrewm@15 302 if(prussdrv_open(pru_number == 0 ? PRU_EVTOUT_0 : PRU_EVTOUT_1)) {
andrewm@0 303 rt_printf("Failed to open PRU driver\n");
andrewm@0 304 return 1;
andrewm@0 305 }
andrewm@0 306
andrewm@0 307 /* Map PRU's INTC */
andrewm@0 308 prussdrv_pruintc_init(&pruss_intc_initdata);
andrewm@0 309
andrewm@0 310 spi_buffer_frames = frames_per_buffer;
andrewm@12 311 audio_buffer_frames = spi_buffer_frames * spi_num_channels / 4;
giuliomoro@16 312 matrix_gpio_buffer_frames = audio_buffer_frames;
andrewm@0 313
andrewm@0 314 /* Map PRU memory to pointers */
andrewm@0 315 prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem);
andrewm@0 316 pru_buffer_comm = (uint32_t *)&pruMem[PRU_MEM_COMM_OFFSET/sizeof(uint32_t)];
andrewm@0 317 pru_buffer_audio_dac = (int16_t *)&pruMem[PRU_MEM_MCASP_OFFSET/sizeof(uint32_t)];
andrewm@0 318
andrewm@12 319 /* ADC memory starts 2(ch)*2(buffers)*bufsize samples later */
andrewm@12 320 pru_buffer_audio_adc = &pru_buffer_audio_dac[4 * audio_buffer_frames];
andrewm@0 321
andrewm@0 322 if(spi_enabled) {
andrewm@0 323 prussdrv_map_prumem (pru_number == 0 ? PRUSS0_PRU0_DATARAM : PRUSS0_PRU1_DATARAM, (void **)&pruMem);
andrewm@0 324 pru_buffer_spi_dac = (uint16_t *)&pruMem[PRU_MEM_DAC_OFFSET/sizeof(uint32_t)];
andrewm@0 325
andrewm@12 326 /* ADC memory starts after N(ch)*2(buffers)*bufsize samples */
andrewm@12 327 pru_buffer_spi_adc = &pru_buffer_spi_dac[2 * spi_num_channels * spi_buffer_frames];
andrewm@0 328 }
andrewm@0 329 else {
andrewm@0 330 pru_buffer_spi_dac = pru_buffer_spi_adc = 0;
andrewm@0 331 }
andrewm@0 332
giuliomoro@16 333 if(matrix_gpio_enabled) {
giuliomoro@16 334 prussdrv_map_prumem (PRUSS0_SHARED_DATARAM, (void **)&pruMem);
giuliomoro@16 335 pru_buffer_matrix_gpio = (uint32_t *)&pruMem[PRU_MEM_MATRIX_GPIO_OFFSET/sizeof(uint32_t)];
giuliomoro@16 336 }
giuliomoro@16 337 else {
giuliomoro@16 338 pru_buffer_matrix_gpio = 0;
giuliomoro@16 339 }
andrewm@0 340 /* Set up flags */
andrewm@0 341 pru_buffer_comm[PRU_SHOULD_STOP] = 0;
andrewm@0 342 pru_buffer_comm[PRU_CURRENT_BUFFER] = 0;
andrewm@0 343 pru_buffer_comm[PRU_BUFFER_FRAMES] = spi_buffer_frames;
andrewm@0 344 pru_buffer_comm[PRU_SHOULD_SYNC] = 0;
andrewm@0 345 pru_buffer_comm[PRU_SYNC_ADDRESS] = 0;
andrewm@0 346 pru_buffer_comm[PRU_SYNC_PIN_MASK] = 0;
andrewm@0 347 if(led_enabled) {
andrewm@0 348 pru_buffer_comm[PRU_LED_ADDRESS] = USERLED3_GPIO_BASE;
andrewm@0 349 pru_buffer_comm[PRU_LED_PIN_MASK] = USERLED3_PIN_MASK;
andrewm@0 350 }
andrewm@0 351 else {
andrewm@0 352 pru_buffer_comm[PRU_LED_ADDRESS] = 0;
andrewm@0 353 pru_buffer_comm[PRU_LED_PIN_MASK] = 0;
andrewm@0 354 }
andrewm@0 355 if(spi_enabled) {
andrewm@0 356 pru_buffer_comm[PRU_USE_SPI] = 1;
andrewm@12 357 pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = spi_num_channels;
andrewm@0 358 }
andrewm@0 359 else {
andrewm@0 360 pru_buffer_comm[PRU_USE_SPI] = 0;
andrewm@12 361 pru_buffer_comm[PRU_SPI_NUM_CHANNELS] = 0;
andrewm@0 362 }
giuliomoro@16 363 if(matrix_gpio_enabled) {
giuliomoro@16 364 pru_buffer_comm[PRU_USE_GPIO_MATRIX] = 1;
giuliomoro@16 365 pru_buffer_comm[NUM_MATRIX_GPIOS] = spi_num_channels;
giuliomoro@16 366 }
giuliomoro@16 367 else {
giuliomoro@16 368 pru_buffer_comm[PRU_USE_GPIO_MATRIX] = 0;
giuliomoro@16 369 pru_buffer_comm[NUM_MATRIX_GPIOS] = 0;
giuliomoro@16 370 }
andrewm@0 371
andrewm@0 372 /* Clear ADC and DAC memory */
andrewm@0 373 if(spi_enabled) {
andrewm@0 374 for(int i = 0; i < PRU_MEM_DAC_LENGTH / 2; i++)
andrewm@0 375 pru_buffer_spi_dac[i] = 0;
andrewm@0 376 }
andrewm@0 377 for(int i = 0; i < PRU_MEM_MCASP_LENGTH / 2; i++)
andrewm@0 378 pru_buffer_audio_dac[i] = 0;
giuliomoro@16 379 //TODO: maybe the lines below are to be deleted, as we removed the test code from pru_rtaudio.p ?
andrewm@0 380 /* If using GPIO test pin for Xenomai (for debugging), initialise the pointer now */
andrewm@0 381 if(xenomai_test_pin && xenomai_gpio_fd < 0) {
andrewm@0 382 xenomai_gpio_fd = open("/dev/mem", O_RDWR);
andrewm@0 383 if(xenomai_gpio_fd < 0)
andrewm@0 384 rt_printf("Unable to open /dev/mem for GPIO test pin\n");
andrewm@0 385 else {
andrewm@0 386 xenomai_gpio = (uint32_t *)mmap(0, GPIO_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, xenomai_gpio_fd, TEST_PIN_GPIO_BASE);
andrewm@0 387 if(xenomai_gpio == MAP_FAILED) {
andrewm@0 388 rt_printf("Unable to map GPIO address for test pin\n");
andrewm@0 389 xenomai_gpio = 0;
andrewm@0 390 close(xenomai_gpio_fd);
andrewm@0 391 xenomai_gpio_fd = -1;
andrewm@0 392 }
andrewm@0 393 }
andrewm@0 394 }
andrewm@0 395
andrewm@0 396 return 0;
andrewm@0 397 }
andrewm@0 398
andrewm@0 399 // Run the code image in the specified file
giuliomoro@16 400 int PRU::start(char * const filename)
andrewm@0 401 {
andrewm@0 402 /* Clear any old interrupt */
andrewm@15 403 if(pru_number == 0)
andrewm@15 404 prussdrv_pru_clear_event(PRU_EVTOUT_0, PRU0_ARM_INTERRUPT);
andrewm@15 405 else
andrewm@15 406 prussdrv_pru_clear_event(PRU_EVTOUT_1, PRU1_ARM_INTERRUPT);
giuliomoro@16 407 /* Load and execute binary on PRU */
giuliomoro@16 408 if(filename[0] == '\0') { //if the string is empty, load the embedded code
giuliomoro@16 409 if(gRTAudioVerbose)
giuliomoro@16 410 rt_printf("Using embedded PRU code\n");
giuliomoro@16 411 if(prussdrv_exec_code(pru_number, PRUcode, sizeof(PRUcode))) {
giuliomoro@16 412 rt_printf("Failed to execute PRU code\n");
giuliomoro@16 413 return 1;
giuliomoro@16 414 }
giuliomoro@16 415 } else {
giuliomoro@16 416 if(gRTAudioVerbose)
giuliomoro@16 417 rt_printf("Using PRU code from %s\n",filename);
giuliomoro@16 418 if(prussdrv_exec_program(pru_number, filename)) {
giuliomoro@16 419 rt_printf("Failed to execute PRU code from %s\n", filename);
giuliomoro@16 420 return 1;
giuliomoro@16 421 }
giuliomoro@16 422 }
andrewm@0 423
andrewm@0 424 running = true;
andrewm@0 425 return 0;
andrewm@0 426 }
giuliomoro@16 427 uint32_t empty[1024]={0x0};
andrewm@0 428
andrewm@0 429 // Main loop to read and write data from/to PRU
andrewm@0 430 void PRU::loop()
andrewm@0 431 {
andrewm@0 432 // Polling interval is 1/4 of the period
andrewm@12 433 RTIME sleepTime = PRU_SAMPLE_INTERVAL_NS * (spi_num_channels / 2) * spi_buffer_frames / 4;
andrewm@0 434 float *audioInBuffer, *audioOutBuffer;
giuliomoro@16 435 float *matrixInBuffer, *matrixOutBuffer;
giuliomoro@16 436 uint32_t *matrixGpioBuffer0, *matrixGpioBuffer1, *matrixGpioBufferTemp;
andrewm@0 437
andrewm@0 438 audioInBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float));
andrewm@0 439 audioOutBuffer = (float *)malloc(2 * audio_buffer_frames * sizeof(float));
giuliomoro@16 440 matrixInBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float));
giuliomoro@16 441 matrixOutBuffer = (float *)malloc(spi_num_channels * spi_buffer_frames * sizeof(float));
giuliomoro@16 442 matrixGpioBuffer0 = pru_buffer_matrix_gpio;
giuliomoro@16 443 matrixGpioBuffer1 = pru_buffer_matrix_gpio+MEM_MATRIX_GPIO_BUFFER1_OFFSET/sizeof(uint32_t);
giuliomoro@16 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.:
giuliomoro@16 445 // - embed in the digitalWrite/Read macros a check whether matrix_gpio is enabled
giuliomoro@16 446 // - allocate some memory in ARM just to allow render() to run regardless.
giuliomoro@16 447 // in this case it can be matrixGpioBuffer0 == matrixGpioBuffer1
giuliomoro@16 448 printf("matrix_gpio_buffer_frames: %d;\n",matrix_gpio_buffer_frames);
giuliomoro@16 449 matrixGpioBufferTemp = (uint32_t *)malloc(matrix_gpio_buffer_frames*sizeof(uint32_t)); //temp buffer to hold previous states
andrewm@0 450 if(audioInBuffer == 0 || audioOutBuffer == 0) {
giuliomoro@16 451 rt_printf("Error: couldn't allocate audio buffers\n");
andrewm@0 452 return;
andrewm@0 453 }
giuliomoro@16 454 if(matrixInBuffer == 0 || matrixOutBuffer == 0) {
giuliomoro@16 455 rt_printf("Error: couldn't allocate matrix buffers\n");
giuliomoro@16 456 return;
giuliomoro@16 457 }
giuliomoro@16 458 if(matrixGpioBufferTemp == 0) {
giuliomoro@16 459 rt_printf("Error: couldn't allocate matrix GPIO buffers\n");
giuliomoro@16 460 return;
giuliomoro@16 461 }
giuliomoro@16 462
andrewm@0 463 while(!gShouldStop) {
andrewm@0 464 // Wait for PRU to move to buffer 1
andrewm@0 465 while(pru_buffer_comm[PRU_CURRENT_BUFFER] == 0 && !gShouldStop) {
andrewm@0 466 rt_task_sleep(sleepTime);
andrewm@0 467 }
andrewm@0 468 if(gShouldStop)
andrewm@0 469 break;
andrewm@0 470
andrewm@0 471 if(xenomai_gpio != 0) {
andrewm@0 472 // Set the test pin high
andrewm@0 473 xenomai_gpio[GPIO_SETDATAOUT] = TEST_PIN_MASK;
andrewm@0 474 }
andrewm@0 475
andrewm@0 476 // Render from/to buffer 0
andrewm@0 477
andrewm@0 478 // Convert short (16-bit) samples to float
andrewm@0 479 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++)
andrewm@0 480 audioInBuffer[n] = (float)pru_buffer_audio_adc[n] / 32768.0;
giuliomoro@16 481 if(spi_enabled) {
giuliomoro@16 482 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++)
giuliomoro@16 483 matrixInBuffer[n] = (float)pru_buffer_spi_adc[n] / 65536.0;
giuliomoro@16 484 //use past matrix_gpio values to initialize the array properly:
giuliomoro@16 485 //- pins previously set as outputs will keep their previously set output value,
giuliomoro@16 486 //- pins previously set as inputs will carry the newly read input value
giuliomoro@16 487 if(matrix_gpio_enabled){
giuliomoro@16 488 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){
giuliomoro@16 489 uint16_t inputs=matrixGpioBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs
giuliomoro@16 490 // printf("inputs: 0x%x\n",inputs);
giuliomoro@16 491 uint16_t outputs=~inputs; //half-word has 1 for outputs and 0 for inputs;
giuliomoro@16 492 matrixGpioBuffer0[n]=(matrixGpioBufferTemp[n]&(outputs<<16))| //keep output values set in previous matrixGpioBuffer1[n]
giuliomoro@16 493 (matrixGpioBuffer0[n]&(inputs<<16)) | //inputs from current matrixGpioBuffer0[n];
giuliomoro@16 494 (matrixGpioBufferTemp[n]&(inputs)); //keep pin configuration from previous matrixGpioBuffer1[n]
giuliomoro@16 495 // matrixGpioBuffer0[n]=matrixGpioBufferTemp[n]; //ignores inputs
giuliomoro@16 496 }
giuliomoro@16 497 }
giuliomoro@16 498 render(spi_buffer_frames, matrix_gpio_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer,
giuliomoro@16 499 matrixInBuffer, matrixOutBuffer, matrixGpioBuffer0);
giuliomoro@16 500 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) {
giuliomoro@16 501 int out = matrixOutBuffer[n] * 65536.0;
giuliomoro@16 502 if(out < 0) out = 0;
giuliomoro@16 503 else if(out > 65535) out = 65535;
giuliomoro@16 504 pru_buffer_spi_dac[n] = (uint16_t)out;
giuliomoro@16 505 }
giuliomoro@16 506 if(matrix_gpio_enabled){ // keep track of past matrix_gpio values
giuliomoro@16 507 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){
giuliomoro@16 508 matrixGpioBufferTemp[n]=matrixGpioBuffer0[n];
giuliomoro@16 509 }
giuliomoro@16 510 }
giuliomoro@16 511 }
andrewm@0 512 else
giuliomoro@16 513 render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass matrixGpioBuffer, just it is unused
giuliomoro@16 514 // Convert float back to short
andrewm@0 515 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) {
andrewm@0 516 int out = audioOutBuffer[n] * 32768.0;
andrewm@0 517 if(out < -32768) out = -32768;
andrewm@0 518 else if(out > 32767) out = 32767;
andrewm@0 519 pru_buffer_audio_dac[n] = (int16_t)out;
andrewm@0 520 }
andrewm@0 521
andrewm@0 522 if(xenomai_gpio != 0) {
andrewm@0 523 // Set the test pin high
andrewm@0 524 xenomai_gpio[GPIO_CLEARDATAOUT] = TEST_PIN_MASK;
andrewm@0 525 }
andrewm@0 526
andrewm@0 527 // Wait for PRU to move to buffer 0
andrewm@0 528 while(pru_buffer_comm[PRU_CURRENT_BUFFER] != 0 && !gShouldStop) {
andrewm@0 529 rt_task_sleep(sleepTime);
andrewm@0 530 }
andrewm@0 531
andrewm@0 532 if(gShouldStop)
andrewm@0 533 break;
andrewm@0 534
andrewm@0 535 if(xenomai_gpio != 0) {
andrewm@0 536 // Set the test pin high
andrewm@0 537 xenomai_gpio[GPIO_SETDATAOUT] = TEST_PIN_MASK;
andrewm@0 538 }
andrewm@0 539
andrewm@0 540 // Render from/to buffer 1
andrewm@0 541
andrewm@0 542 // Convert short (16-bit) samples to float
andrewm@0 543 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++)
andrewm@0 544 audioInBuffer[n] = (float)pru_buffer_audio_adc[n + audio_buffer_frames * 2] / 32768.0;
andrewm@0 545
giuliomoro@16 546 if(spi_enabled) {
giuliomoro@16 547 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++)
giuliomoro@16 548 matrixInBuffer[n] = (float)pru_buffer_spi_adc[n + spi_buffer_frames * spi_num_channels] / 65536.0;
giuliomoro@16 549
giuliomoro@16 550 //use past matrix_gpio values to initialize the array properly:
giuliomoro@16 551 //- pins previously set as outputs will keep their previously set output value,
giuliomoro@16 552 //- pins previously set as inputs will carry the newly read input value
giuliomoro@16 553 if(matrix_gpio_enabled){
giuliomoro@16 554 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){
giuliomoro@16 555 uint16_t inputs=matrixGpioBufferTemp[n]&0xffff;//half-word, has 1 for inputs and 0 for outputs
giuliomoro@16 556 uint16_t outputs=~inputs; //half-word has 1 for outputs and one for inputs;
giuliomoro@16 557 matrixGpioBuffer1[n]=(matrixGpioBufferTemp[n]&(outputs<<16))| //keep output values set in previous matrixGpioBuffer1[n]
giuliomoro@16 558 (matrixGpioBuffer1[n]&(inputs<<16)) | //inputs from current matrixGpioBuffer1[n];
giuliomoro@16 559 (matrixGpioBufferTemp[n]&(inputs)); //keep pin configuration from previous matrixGpioBuffer1[n]
giuliomoro@16 560 // matrixGpioBuffer1[n]=matrixGpioBufferTemp[n]; //ignores inputs
giuliomoro@16 561 }
giuliomoro@16 562 }
giuliomoro@16 563 render(spi_buffer_frames, matrix_gpio_buffer_frames, audio_buffer_frames, audioInBuffer, audioOutBuffer,
giuliomoro@16 564 matrixInBuffer, matrixOutBuffer, matrixGpioBuffer1);
giuliomoro@16 565 for(unsigned int n = 0; n < spi_num_channels * spi_buffer_frames; n++) {
giuliomoro@16 566 int out = matrixOutBuffer[n] * 65536.0;
giuliomoro@16 567 if(out < 0) out = 0;
giuliomoro@16 568 else if(out > 65535) out = 65535;
giuliomoro@16 569 pru_buffer_spi_dac[n + spi_buffer_frames * spi_num_channels] = (uint16_t)out;
giuliomoro@16 570 }
giuliomoro@16 571 if(matrix_gpio_enabled){ // keep track of past matrix_gpio values
giuliomoro@16 572 for(unsigned int n = 0; n < matrix_gpio_buffer_frames; n++){
giuliomoro@16 573 matrixGpioBufferTemp[n]=matrixGpioBuffer1[n];
giuliomoro@16 574 }
giuliomoro@16 575 }
giuliomoro@16 576 }
andrewm@0 577 else
giuliomoro@16 578 render(0, 0, audio_buffer_frames, audioInBuffer, audioOutBuffer, 0, 0, 0); // we still pass matrixGpioBuffer, just it is unused
andrewm@0 579
andrewm@0 580 // Convert float back to short
andrewm@0 581 for(unsigned int n = 0; n < 2 * audio_buffer_frames; n++) {
andrewm@0 582 int out = audioOutBuffer[n] * 32768.0;
andrewm@0 583 if(out < -32768) out = -32768;
andrewm@0 584 else if(out > 32767) out = 32767;
andrewm@0 585 pru_buffer_audio_dac[n + audio_buffer_frames * 2] = (int16_t)out;
andrewm@0 586 }
andrewm@0 587
andrewm@0 588 if(xenomai_gpio != 0) {
andrewm@0 589 // Set the test pin high
andrewm@0 590 xenomai_gpio[GPIO_CLEARDATAOUT] = TEST_PIN_MASK;
andrewm@0 591 }
andrewm@0 592 }
andrewm@0 593
andrewm@0 594 // Tell PRU to stop
andrewm@0 595 pru_buffer_comm[PRU_SHOULD_STOP] = 1;
andrewm@0 596
giuliomoro@16 597 free(matrixOutBuffer);
andrewm@0 598 free(audioInBuffer);
andrewm@0 599 free(audioOutBuffer);
giuliomoro@16 600 free(matrixInBuffer);
giuliomoro@16 601 free(matrixGpioBufferTemp);
andrewm@0 602 }
andrewm@0 603
andrewm@0 604 // Wait for an interrupt from the PRU indicate it is finished
andrewm@0 605 void PRU::waitForFinish()
andrewm@0 606 {
andrewm@0 607 if(!running)
andrewm@0 608 return;
andrewm@15 609 prussdrv_pru_wait_event (pru_number == 0 ? PRU_EVTOUT_0 : PRU_EVTOUT_1);
andrewm@15 610 if(pru_number == 0)
andrewm@15 611 prussdrv_pru_clear_event(PRU_EVTOUT_0, PRU0_ARM_INTERRUPT);
andrewm@15 612 else
andrewm@15 613 prussdrv_pru_clear_event(PRU_EVTOUT_1, PRU1_ARM_INTERRUPT);
andrewm@0 614 }
andrewm@0 615
andrewm@0 616 // Turn off the PRU when done
andrewm@0 617 void PRU::disable()
andrewm@0 618 {
andrewm@0 619 /* Disable PRU and close memory mapping*/
andrewm@0 620 prussdrv_pru_disable(pru_number);
andrewm@0 621 prussdrv_exit();
andrewm@0 622 running = false;
andrewm@0 623 }
andrewm@0 624
andrewm@0 625 // Debugging
andrewm@0 626 void PRU::setGPIOTestPin()
andrewm@0 627 {
andrewm@0 628 if(!xenomai_gpio)
andrewm@0 629 return;
andrewm@0 630 xenomai_gpio[GPIO_SETDATAOUT] = TEST_PIN2_MASK;
andrewm@0 631 }
andrewm@0 632
andrewm@0 633 void PRU::clearGPIOTestPin()
andrewm@0 634 {
andrewm@0 635 if(!xenomai_gpio)
andrewm@0 636 return;
andrewm@0 637 xenomai_gpio[GPIO_CLEARDATAOUT] = TEST_PIN2_MASK;
andrewm@0 638 }