Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
PRU.h
1 /*
2  * PRU.h
3  *
4  * Created on: May 27, 2014
5  * Author: andrewm
6  */
7 
8 #ifndef PRU_H_
9 #define PRU_H_
10 
11 #include <stdint.h>
12 #include "Bela.h"
13 #include "Gpio.h"
14 #include "AudioCodec.h"
15 
23 typedef struct {
29  float *audioIn;
30 
36  float *audioOut;
37 
43  float *analogIn;
44 
50  float *analogOut;
51 
55  uint32_t *digital;
56 
58  uint32_t audioFrames;
60  uint32_t audioInChannels;
62  uint32_t audioOutChannels;
65 
69  uint32_t analogFrames;
70 
74  uint32_t analogInChannels;
75 
80 
88 
90  uint32_t digitalFrames;
94  uint32_t digitalChannels;
97 
105 
111 
117 
124 
130 
139  uint32_t flags;
140 
142  char projectName[MAX_PROJECTNAME_LENGTH];
143  operator BelaContext () {return *(BelaContext*)this;}
145 
146 class PruMemory;
147 class PRU
148 {
149 private:
150  static const unsigned int kPruGPIODACSyncPin;
151  static const unsigned int kPruGPIOADCSyncPin;
152 
153 public:
154  // Constructor
155  PRU(InternalBelaContext *input_context, AudioCodec *audio_codec);
156 
157  // Destructor
158  ~PRU();
159 
160  // Prepare the GPIO pins needed for the PRU
161  int prepareGPIO(int include_led);
162 
163  // Clean up the GPIO at the end
164  void cleanupGPIO();
165 
166  // Initialise and open the PRU
167  int initialise(BelaHw newBelaHw, int pru_num, bool uniformSampleRate,
168  int mux_channels,
169  int stopButtonPin, bool enableLed);
170 
171  // Run the code image in pru_rtaudio_bin.h
172  int start(char * const filename);
173 
174  // Loop: read and write data from the PRU and call the user-defined audio callback
175  void loop(void *userData, void(*render)(BelaContext*, void*), bool highPerformanceMode);
176 
177  // Wait for an interrupt from the PRU indicate it is finished
178  void waitForFinish();
179 
180  // Turn off the PRU when done
181  void disable();
182 
183  // Exit the whole PRU subsystem
184  void exitPRUSS();
185 
186 private:
187  void initialisePruCommon();
188  int testPruError();
189  InternalBelaContext *context; // Overall settings
190 
191  int pru_number; // Which PRU we use
192  bool initialised; // Whether the prussdrv system is initialised
193  bool running; // Whether the PRU is running
194  bool analog_enabled; // Whether SPI ADC and DAC are used
195  bool digital_enabled; // Whether digital is used
196  float analogs_per_audio; // How many analog frames per each audio frame
197  bool uniform_sample_rate; // Should the sampling rate of the analog and audio forced to be the same, as far as ARM is concerned
198  int hardware_analog_frames; // The actual number of frames for the analog channels, as far as the PRU is concerned
199  bool gpio_enabled; // Whether GPIO has been prepared
200  bool led_enabled; // Whether a user LED is enabled
201 
202  PruMemory* pruMemory;
203  volatile uint32_t *pru_buffer_comm;
204  uint32_t pruBufferMcaspFrames;
205 
206  float *last_analog_out_frame;
207  uint32_t *last_digital_buffer;
208  float *audio_expander_input_history;
209  float *audio_expander_output_history;
210  float audio_expander_filter_coeff;
211  bool pruUsesMcaspIrq;
212  BelaHw belaHw;
213 
214  Gpio stopButton; // Monitoring the bela cape button
215  Gpio underrunLed; // Flashing an LED upon underrun
216  AudioCodec *codec; // Required to hard reset audio codec from loop
217 };
218 
219 
220 #endif /* PRU_H_ */
uint32_t audioOutChannels
Number of output audio channels.
Definition: PRU.h:62
uint32_t analogOutChannels
Number of output analog channels.
Definition: PRU.h:79
Definition: AudioCodec.h:4
float digitalSampleRate
Digital sample rate in Hz (currently always 44100.0)
Definition: PRU.h:96
uint32_t digitalFrames
Number of digital frames per period.
Definition: PRU.h:90
float * audioIn
Buffer holding audio input samples.
Definition: PRU.h:29
uint32_t audioInChannels
Number of input audio channels.
Definition: PRU.h:60
uint32_t audioFrames
Number of audio frames per period.
Definition: PRU.h:58
float * multiplexerAnalogIn
Buffer which holds multiplexed analog inputs, when multiplexer capelet is enabled.
Definition: PRU.h:123
uint32_t digitalChannels
Number of digital channels.
Definition: PRU.h:94
uint32_t * digital
Buffer holding digital input/output samples.
Definition: PRU.h:55
uint32_t analogFrames
Number of analog frames per period.
Definition: PRU.h:69
Definition: PRU.h:147
Definition: Gpio.h:5
uint32_t analogInChannels
Number of input analog channels.
Definition: PRU.h:74
float * analogIn
Buffer holding analog input samples.
Definition: PRU.h:43
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition: Bela.h:186
uint32_t flags
Other audio/sensor settings.
Definition: PRU.h:139
float * analogOut
Buffer holding analog output samples.
Definition: PRU.h:50
float * audioOut
Buffer holding audio output samples.
Definition: PRU.h:36
uint32_t audioExpanderEnabled
Flags for whether audio expander is enabled on given analog channels.
Definition: PRU.h:129
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition: render.cpp:54
float audioSampleRate
Audio sample rate in Hz (currently always 44100.0)
Definition: PRU.h:64
uint32_t multiplexerStartingChannel
Multiplexer channel corresponding to the first analog frame.
Definition: PRU.h:116
Main Bela public API.
uint32_t multiplexerChannels
Number of multiplexer channels for each analog input.
Definition: PRU.h:110
uint64_t audioFramesElapsed
Number of elapsed audio frames since the start of rendering.
Definition: PRU.h:104
float analogSampleRate
Analog sample rate in Hz.
Definition: PRU.h:87
Definition: PRU.h:23