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 #pragma once
2 
3 #include <stdint.h>
4 #include "Bela.h"
5 #include "Gpio.h"
6 #include "PruManager.h"
7 struct McaspRegisters;
8 
16 typedef struct _InternalBelaContext {
22  float *audioIn;
23 
29  float *audioOut;
30 
36  float *analogIn;
37 
43  float *analogOut;
44 
48  uint32_t *digital;
49 
51  uint32_t audioFrames;
53  uint32_t audioInChannels;
55  uint32_t audioOutChannels;
58 
62  uint32_t analogFrames;
63 
67  uint32_t analogInChannels;
68 
73 
81 
83  uint32_t digitalFrames;
87  uint32_t digitalChannels;
90 
98 
104 
110 
117 
123 
132  uint32_t flags;
133 
135  char projectName[MAX_PROJECTNAME_LENGTH];
136 
138  unsigned int underrunCount;
139  operator BelaContext () {return *(BelaContext*)this;}
141 
142 class PruMemory;
143 class PRU
144 {
145 private:
146  static const unsigned int kPruGPIODACSyncPin;
147  static const unsigned int kPruGPIOADCSyncPin;
148 
149 public:
150  // Constructor
151  PRU(InternalBelaContext *input_context);
152 
153  // Destructor
154  ~PRU();
155 
156  // Prepare the GPIO pins needed for the PRU
157  int prepareGPIO(int include_led);
158 
159  // Clean up the GPIO at the end
160  void cleanupGPIO();
161 
162  // Initialise and open the PRU
163  int initialise(BelaHw newBelaHw, int pru_num, bool uniformSampleRate,
164  int mux_channels,
165  int stopButtonPin, bool enableLed);
166 
167  // Run the code image in pru_rtaudio_bin.h
168  int start(char * const filename, const McaspRegisters& mcaspRegisters);
169 
170  // Loop: read and write data from the PRU and call the user-defined audio callback
171  void loop(void *userData, void(*render)(BelaContext*, void*), bool highPerformanceMode, BelaCpuData* cpuData);
172 
173  // Wait for an interrupt from the PRU indicate it is finished
174  void waitForFinish();
175 
176  // Turn off the PRU when done
177  void disable();
178 
179  // Exit the whole PRU subsystem
180  void exitPRUSS();
181  PruManager *pruManager;
182 
183 private:
184  void initialisePruCommon(const McaspRegisters& mcaspRegisters);
185  int testPruError();
186  InternalBelaContext *context; // Overall settings
187 
188  int pru_number; // Which PRU we use
189  bool initialised; // Whether the prussdrv system is initialised
190  bool running; // Whether the PRU is running
191  bool analog_enabled; // Whether SPI ADC and DAC are used
192  bool digital_enabled; // Whether digital is used
193  float analogs_per_audio; // How many analog frames per each audio frame
194  bool uniform_sample_rate; // Should the sampling rate of the analog and audio forced to be the same, as far as ARM is concerned
195  int hardware_analog_frames; // The actual number of frames for the analog channels, as far as the PRU is concerned
196  bool gpio_enabled; // Whether GPIO has been prepared
197  bool led_enabled; // Whether a user LED is enabled
198  bool analog_out_is_audio;
199  size_t pru_audio_out_channels;
200 
201  PruMemory* pruMemory;
202  volatile uint32_t *pru_buffer_comm;
203  uint32_t pruBufferMcaspFrames;
204 
205  float *last_analog_out_frame;
206  uint32_t *last_digital_buffer;
207  float *audio_expander_input_history;
208  float *audio_expander_output_history;
209  float audio_expander_filter_coeff;
210  bool pruUsesMcaspIrq;
211  BelaHw belaHw;
212 
213  Gpio stopButton; // Monitoring the bela cape button
214  Gpio underrunLed; // Flashing an LED upon underrun
215  Gpio adcNrstPin; // Resetting the ADC on Bela Mini Rev C
216 };
uint32_t audioInChannels
Number of input audio channels.
Definition: PRU.h:53
uint64_t audioFramesElapsed
Number of elapsed audio frames since the start of rendering.
Definition: PRU.h:97
float * analogIn
Buffer holding analog input samples.
Definition: PRU.h:36
float * audioOut
Buffer holding audio output samples.
Definition: PRU.h:29
Definition: PRU.h:16
uint32_t flags
Other audio/sensor settings.
Definition: PRU.h:132
float digitalSampleRate
Digital sample rate in Hz (currently always 44100.0)
Definition: PRU.h:89
unsigned int underrunCount
Number of detected underruns.
Definition: PRU.h:138
uint32_t analogInChannels
Number of input analog channels.
Definition: PRU.h:67
float analogSampleRate
Analog sample rate in Hz.
Definition: PRU.h:80
BelaHw
Definition: Bela.h:105
float audioSampleRate
Audio sample rate in Hz (currently always 44100.0)
Definition: PRU.h:57
Definition: PRU.h:143
uint32_t audioExpanderEnabled
Flags for whether audio expander is enabled on given analog channels.
Definition: PRU.h:122
uint32_t digitalFrames
Number of digital frames per period.
Definition: PRU.h:83
Definition: Gpio.h:5
Definition: Bela.h:868
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition: Bela.h:229
float * analogOut
Buffer holding analog output samples.
Definition: PRU.h:43
uint32_t multiplexerStartingChannel
Multiplexer channel corresponding to the first analog frame.
Definition: PRU.h:109
float * audioIn
Buffer holding audio input samples.
Definition: PRU.h:22
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition: render.cpp:68
Definition: Mcasp.h:9
uint32_t digitalChannels
Number of digital channels.
Definition: PRU.h:87
uint32_t analogOutChannels
Number of output analog channels.
Definition: PRU.h:72
Main Bela public API.
Definition: PruManager.h:20
uint32_t analogFrames
Number of analog frames per period.
Definition: PRU.h:62
float * multiplexerAnalogIn
Buffer which holds multiplexed analog inputs, when multiplexer capelet is enabled.
Definition: PRU.h:116
uint32_t multiplexerChannels
Number of multiplexer channels for each analog input.
Definition: PRU.h:103
uint32_t * digital
Buffer holding digital input/output samples.
Definition: PRU.h:48
uint32_t audioFrames
Number of audio frames per period.
Definition: PRU.h:51
uint32_t audioOutChannels
Number of output audio channels.
Definition: PRU.h:55
char projectName[MAX_PROJECTNAME_LENGTH]
Name of running project.
Definition: PRU.h:135