annotate include/PRU.h @ 528:5c8f46fcd4d0 API-update

Updated BelaContext to use separate values for in/ou channels
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 23 Jun 2016 18:17:35 +0100
parents 493a07f6ec09
children
rev   line source
andrewm@0 1 /*
andrewm@0 2 * PRU.h
andrewm@0 3 *
andrewm@0 4 * Created on: May 27, 2014
andrewm@0 5 * Author: andrewm
andrewm@0 6 */
andrewm@0 7
andrewm@0 8 #ifndef PRU_H_
andrewm@0 9 #define PRU_H_
andrewm@0 10
andrewm@0 11 #include <stdint.h>
andrewm@45 12 #include <native/intr.h>
giuliomoro@301 13 #include "../include/Bela.h"
andrewm@0 14
andrewm@307 15 /**
andrewm@307 16 * Internal version of the BelaContext struct which does not have const
andrewm@307 17 * elements, so it can be modified by the code. When it's passed to the user
andrewm@307 18 * code, it is typecast to the standard BelaContext.
andrewm@307 19 *
andrewm@307 20 * Important: make sure this retains the same structure as BelaContext!
andrewm@307 21 */
andrewm@307 22 typedef struct {
andrewm@307 23 /// \brief Buffer holding audio input samples
andrewm@307 24 ///
andrewm@307 25 /// This buffer may be in either interleaved or non-interleaved format,
andrewm@307 26 /// depending on the contents of the BelaInitSettings structure.
andrewm@307 27 /// \b Note: this element is available in render() only.
andrewm@307 28 float *audioIn;
andrewm@307 29
andrewm@307 30 /// \brief Buffer holding audio output samples
andrewm@307 31 ///
andrewm@307 32 /// This buffer may be in either interleaved or non-interleaved format,
andrewm@307 33 /// depending on the contents of the BelaInitSettings structure.
andrewm@307 34 /// \b Note: this element is available in render() only.
andrewm@307 35 float *audioOut;
andrewm@307 36
andrewm@307 37 /// \brief Buffer holding analog input samples
andrewm@307 38 ///
andrewm@307 39 /// This buffer may be in either interleaved or non-interleaved format,
andrewm@307 40 /// depending on the contents of the BelaInitSettings structure.
andrewm@307 41 /// \b Note: this element is available in render() only.
andrewm@307 42 float *analogIn;
andrewm@307 43
andrewm@307 44 /// \brief Buffer holding analog output samples
andrewm@307 45 ///
andrewm@307 46 /// This buffer may be in either interleaved or non-interleaved format,
andrewm@307 47 /// depending on the contents of the BelaInitSettings structure.
andrewm@307 48 /// \b Note: this element is available in render() only.
andrewm@307 49 float *analogOut;
andrewm@307 50
andrewm@307 51 /// \brief Buffer holding digital input/output samples
andrewm@307 52 ///
andrewm@307 53 /// \b Note: this element is available in render() only.
andrewm@307 54 uint32_t *digital;
andrewm@307 55
andrewm@307 56 /// Number of audio frames per period
andrewm@307 57 uint32_t audioFrames;
giuliomoro@528 58 /// Number of input audio channels
giuliomoro@528 59 uint32_t audioInChannels;
giuliomoro@528 60 /// Number of output audio channels
giuliomoro@528 61 uint32_t audioOutChannels;
andrewm@307 62 /// Audio sample rate in Hz (currently always 44100.0)
andrewm@307 63 float audioSampleRate;
andrewm@307 64
andrewm@307 65 /// \brief Number of analog frames per period
andrewm@307 66 ///
andrewm@307 67 /// This will be 0 if analog I/O is disabled.
andrewm@307 68 uint32_t analogFrames;
andrewm@307 69
giuliomoro@528 70 /// \brief Number of input analog channels
andrewm@307 71 ///
giuliomoro@528 72 /// This will be 0 if analog I/O is disabled.
giuliomoro@528 73 uint32_t analogInChannels;
giuliomoro@528 74
giuliomoro@528 75 /// \brief Number of output analog channels
giuliomoro@528 76 ///
giuliomoro@528 77 /// This will be 0 if analog I/O is disabled.
giuliomoro@528 78 uint32_t analogOutChannels;
andrewm@307 79
andrewm@307 80 /// \brief Analog sample rate in Hz
andrewm@307 81 ///
andrewm@307 82 /// The analog sample rate depends on the number of analog channels used. If
andrewm@307 83 /// 8 channels are used, the sample rate is 22050. If 4 channels are used, the sample
andrewm@307 84 /// rate is 44100. If 2 channels are used, the sample rate is 88200. If analog I/O
andrewm@307 85 /// is disabled, the sample rate is 0.
andrewm@307 86 float analogSampleRate;
andrewm@307 87
andrewm@307 88 /// Number of digital frames per period
andrewm@307 89 uint32_t digitalFrames;
andrewm@307 90 /// \brief Number of digital channels
andrewm@307 91 ///
andrewm@307 92 /// Currently this will always be 16, unless digital I/O is disabled, in which case it will be 0.
andrewm@307 93 uint32_t digitalChannels;
andrewm@307 94 /// Digital sample rate in Hz (currently always 44100.0)
andrewm@307 95 float digitalSampleRate;
andrewm@307 96
andrewm@311 97 /// \brief Number of elapsed audio frames since the start of rendering.
andrewm@307 98 ///
andrewm@311 99 /// This holds the total number of audio frames as of the beginning of the current period. To
andrewm@311 100 /// find the current number of analog or digital frames elapsed, multiply by the ratio of the
andrewm@311 101 /// sample rates (e.g. half the number of analog frames will have elapsed if the analog sample
andrewm@307 102 /// rate is 22050).
andrewm@311 103 uint64_t audioFramesElapsed;
andrewm@307 104
andrewm@307 105 /// \brief Other audio/sensor settings
andrewm@307 106 ///
andrewm@307 107 /// Binary combination of flags including:
andrewm@307 108 ///
andrewm@307 109 /// BELA_FLAG_INTERLEAVED: indicates the audio and analog buffers are interleaved
andrewm@307 110 ///
andrewm@307 111 /// BELA_FLAG_ANALOG_OUTPUTS_PERSIST: indicates that writes to the analog outputs will
andrewm@307 112 /// persist for future frames. If not set, writes affect one frame only.
andrewm@307 113 uint32_t flags;
andrewm@307 114 } InternalBelaContext;
andrewm@307 115
andrewm@307 116
andrewm@0 117 class PRU
andrewm@0 118 {
andrewm@0 119 private:
andrewm@0 120 static const unsigned int kPruGPIODACSyncPin;
andrewm@0 121 static const unsigned int kPruGPIOADCSyncPin;
andrewm@0 122 static const unsigned int kPruGPIOTestPin;
andrewm@0 123 static const unsigned int kPruGPIOTestPin2;
andrewm@0 124 static const unsigned int kPruGPIOTestPin3;
andrewm@0 125
andrewm@0 126 public:
andrewm@0 127 // Constructor
andrewm@307 128 PRU(InternalBelaContext *input_context);
andrewm@0 129
andrewm@0 130 // Destructor
andrewm@0 131 ~PRU();
andrewm@0 132
andrewm@0 133 // Prepare the GPIO pins needed for the PRU
andrewm@45 134 int prepareGPIO(int include_test_pin, int include_led);
andrewm@0 135
andrewm@0 136 // Clean up the GPIO at the end
andrewm@0 137 void cleanupGPIO();
andrewm@0 138
andrewm@0 139 // Initialise and open the PRU
andrewm@45 140 int initialise(int pru_num, int frames_per_buffer,
andrewm@280 141 int spi_channels, int mux_channels = 0,
andrewm@280 142 bool xenomai_test_pin = false);
andrewm@0 143
andrewm@15 144 // Run the code image in pru_rtaudio_bin.h
giuliomoro@16 145 int start(char * const filename);
andrewm@0 146
andrewm@0 147 // Loop: read and write data from the PRU
andrewm@45 148 void loop(RT_INTR *pru_interrupt, void *userData);
andrewm@0 149
andrewm@0 150 // Wait for an interrupt from the PRU indicate it is finished
andrewm@0 151 void waitForFinish();
andrewm@0 152
andrewm@0 153 // Turn off the PRU when done
andrewm@0 154 void disable();
andrewm@0 155
andrewm@0 156 // For debugging:
andrewm@0 157 void setGPIOTestPin();
andrewm@0 158 void clearGPIOTestPin();
andrewm@0 159
andrewm@0 160 private:
andrewm@307 161 InternalBelaContext *context; // Overall settings
andrewm@45 162
andrewm@0 163 int pru_number; // Which PRU we use
andrewm@0 164 bool running; // Whether the PRU is running
andrewm@45 165 bool analog_enabled; // Whether SPI ADC and DAC are used
giuliomoro@19 166 bool digital_enabled; // Whether digital is used
andrewm@0 167 bool gpio_enabled; // Whether GPIO has been prepared
andrewm@0 168 bool led_enabled; // Whether a user LED is enabled
andrewm@303 169 int mux_channels; // How many mux channels are used (if enabled)
andrewm@0 170 bool gpio_test_pin_enabled; // Whether the test pin was also enabled
andrewm@0 171
andrewm@303 172
andrewm@0 173 volatile uint32_t *pru_buffer_comm;
andrewm@0 174 uint16_t *pru_buffer_spi_dac;
andrewm@0 175 uint16_t *pru_buffer_spi_adc;
giuliomoro@19 176 uint32_t *pru_buffer_digital;
andrewm@0 177 int16_t *pru_buffer_audio_dac;
andrewm@0 178 int16_t *pru_buffer_audio_adc;
andrewm@0 179
andrewm@81 180 float *last_analog_out_frame;
andrewm@81 181 uint32_t *digital_buffer0, *digital_buffer1, *last_digital_buffer;
andrewm@81 182
andrewm@0 183 int xenomai_gpio_fd; // File descriptor for /dev/mem for fast GPIO
andrewm@0 184 uint32_t *xenomai_gpio; // Pointer to GPIO registers
andrewm@0 185 };
andrewm@0 186
andrewm@0 187
andrewm@0 188 #endif /* PRU_H_ */