andrewm@0
|
1 /*
|
andrewm@0
|
2 * RTAudio.cpp
|
andrewm@0
|
3 *
|
andrewm@0
|
4 * Central control code for hard real-time audio on BeagleBone Black
|
andrewm@0
|
5 * using PRU and Xenomai Linux extensions. This code began as part
|
andrewm@0
|
6 * of the Hackable Instruments project (EPSRC) at Queen Mary University
|
andrewm@0
|
7 * of London, 2013-14.
|
andrewm@0
|
8 *
|
andrewm@0
|
9 * (c) 2014 Victor Zappi and Andrew McPherson
|
andrewm@0
|
10 * Queen Mary University of London
|
andrewm@0
|
11 */
|
andrewm@0
|
12
|
andrewm@0
|
13
|
andrewm@0
|
14 #include <stdio.h>
|
andrewm@0
|
15 #include <stdlib.h>
|
andrewm@0
|
16 #include <string.h>
|
andrewm@0
|
17 #include <strings.h>
|
andrewm@0
|
18 #include <math.h>
|
andrewm@0
|
19 #include <iostream>
|
andrewm@0
|
20 #include <assert.h>
|
andrewm@0
|
21 #include <vector>
|
andrewm@0
|
22
|
andrewm@0
|
23 // Xenomai-specific includes
|
andrewm@0
|
24 #include <sys/mman.h>
|
andrewm@0
|
25 #include <native/task.h>
|
andrewm@0
|
26 #include <native/timer.h>
|
andrewm@45
|
27 #include <native/intr.h>
|
andrewm@0
|
28 #include <rtdk.h>
|
andrewm@0
|
29
|
andrewm@45
|
30 #include "../include/BeagleRT.h"
|
andrewm@0
|
31 #include "../include/PRU.h"
|
andrewm@0
|
32 #include "../include/I2c_Codec.h"
|
andrewm@0
|
33 #include "../include/GPIOcontrol.h"
|
andrewm@0
|
34
|
andrewm@45
|
35 // ARM interrupt number for PRU event EVTOUT7
|
andrewm@45
|
36 #define PRU_RTAUDIO_IRQ 21
|
andrewm@45
|
37
|
andrewm@0
|
38 using namespace std;
|
andrewm@0
|
39
|
andrewm@0
|
40 // Data structure to keep track of auxiliary tasks we
|
andrewm@0
|
41 // can schedule
|
andrewm@0
|
42 typedef struct {
|
andrewm@0
|
43 RT_TASK task;
|
l@254
|
44 void (*function)(void*);
|
andrewm@0
|
45 char *name;
|
andrewm@0
|
46 int priority;
|
giuliomoro@174
|
47 bool started;
|
l@254
|
48 void* args;
|
andrewm@0
|
49 } InternalAuxiliaryTask;
|
andrewm@0
|
50
|
andrewm@0
|
51 const char gRTAudioThreadName[] = "beaglert-audio";
|
andrewm@45
|
52 const char gRTAudioInterruptName[] = "beaglert-pru-irq";
|
andrewm@0
|
53
|
andrewm@0
|
54 // Real-time tasks and objects
|
andrewm@0
|
55 RT_TASK gRTAudioThread;
|
andrewm@50
|
56 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
|
andrewm@45
|
57 RT_INTR gRTAudioInterrupt;
|
andrewm@50
|
58 #endif
|
andrewm@0
|
59 PRU *gPRU = 0;
|
andrewm@0
|
60 I2c_Codec *gAudioCodec = 0;
|
andrewm@0
|
61
|
giuliomoro@176
|
62 vector<InternalAuxiliaryTask*> &getAuxTasks(){
|
giuliomoro@176
|
63 static vector<InternalAuxiliaryTask*> auxTasks;
|
giuliomoro@176
|
64 return auxTasks;
|
giuliomoro@176
|
65 }
|
andrewm@0
|
66
|
andrewm@0
|
67 // Flag which tells the audio task to stop
|
giuliomoro@233
|
68 int gShouldStop = false;
|
andrewm@0
|
69
|
andrewm@0
|
70 // general settings
|
andrewm@45
|
71 char gPRUFilename[MAX_PRU_FILENAME_LENGTH]; // Path to PRU binary file (internal code if empty)_
|
andrewm@0
|
72 int gRTAudioVerbose = 0; // Verbosity level for debugging
|
andrewm@0
|
73 int gAmplifierMutePin = -1;
|
andrewm@5
|
74 int gAmplifierShouldBeginMuted = 0;
|
andrewm@0
|
75
|
andrewm@45
|
76 // Context which holds all the audio/sensor data passed to the render routines
|
andrewm@45
|
77 BeagleRTContext gContext;
|
andrewm@45
|
78
|
andrewm@45
|
79 // User data passed in from main()
|
andrewm@45
|
80 void *gUserData;
|
andrewm@0
|
81
|
andrewm@0
|
82 // initAudio() prepares the infrastructure for running PRU-based real-time
|
andrewm@0
|
83 // audio, but does not actually start the calculations.
|
giuliomoro@178
|
84 // periodSize indicates the number of audio frames per period: the analog period size
|
giuliomoro@178
|
85 // will depend on the number of analog channels, in such a way that
|
giuliomoro@178
|
86 // analogPeriodSize = 4*periodSize/numAnalogChannels
|
giuliomoro@178
|
87 // In total, the audio latency in frames will be 2*periodSize,
|
andrewm@0
|
88 // plus any latency inherent in the ADCs and DACs themselves.
|
giuliomoro@19
|
89 // useAnalog indicates whether to enable the ADC and DAC or just use the audio codec.
|
giuliomoro@19
|
90 // numAnalogChannels indicates how many ADC and DAC channels to use.
|
andrewm@56
|
91 // userData is an opaque pointer which will be passed through to the setup()
|
andrewm@0
|
92 // function for application-specific use
|
andrewm@0
|
93 //
|
andrewm@0
|
94 // Returns 0 on success.
|
andrewm@0
|
95
|
andrewm@45
|
96 int BeagleRT_initAudio(BeagleRTInitSettings *settings, void *userData)
|
andrewm@0
|
97 {
|
andrewm@0
|
98 rt_print_auto_init(1);
|
andrewm@45
|
99
|
andrewm@45
|
100 BeagleRT_setVerboseLevel(settings->verbose);
|
andrewm@45
|
101 strncpy(gPRUFilename, settings->pruFilename, MAX_PRU_FILENAME_LENGTH);
|
andrewm@45
|
102 gUserData = userData;
|
andrewm@45
|
103
|
andrewm@45
|
104 // Initialise context data structure
|
andrewm@45
|
105 memset(&gContext, 0, sizeof(BeagleRTContext));
|
andrewm@0
|
106
|
andrewm@5
|
107 if(gRTAudioVerbose) {
|
andrewm@5
|
108 cout << "Starting with period size " << settings->periodSize << "; ";
|
giuliomoro@19
|
109 if(settings->useAnalog)
|
giuliomoro@19
|
110 cout << "analog enabled\n";
|
andrewm@5
|
111 else
|
giuliomoro@19
|
112 cout << "analog disabled\n";
|
andrewm@5
|
113 cout << "DAC level " << settings->dacLevel << "dB; ADC level " << settings->adcLevel;
|
andrewm@5
|
114 cout << "dB; headphone level " << settings->headphoneLevel << "dB\n";
|
andrewm@5
|
115 if(settings->beginMuted)
|
andrewm@5
|
116 cout << "Beginning with speaker muted\n";
|
andrewm@5
|
117 }
|
andrewm@0
|
118
|
andrewm@0
|
119 // Prepare GPIO pins for amplifier mute and status LED
|
andrewm@5
|
120 if(settings->ampMutePin >= 0) {
|
andrewm@5
|
121 gAmplifierMutePin = settings->ampMutePin;
|
andrewm@5
|
122 gAmplifierShouldBeginMuted = settings->beginMuted;
|
andrewm@0
|
123
|
andrewm@5
|
124 if(gpio_export(settings->ampMutePin)) {
|
andrewm@0
|
125 if(gRTAudioVerbose)
|
giuliomoro@16
|
126 cout << "Warning: couldn't export amplifier mute pin " << settings-> ampMutePin << "\n";
|
andrewm@0
|
127 }
|
andrewm@5
|
128 if(gpio_set_dir(settings->ampMutePin, OUTPUT_PIN)) {
|
andrewm@0
|
129 if(gRTAudioVerbose)
|
andrewm@0
|
130 cout << "Couldn't set direction on amplifier mute pin\n";
|
andrewm@0
|
131 return -1;
|
andrewm@0
|
132 }
|
andrewm@5
|
133 if(gpio_set_value(settings->ampMutePin, LOW)) {
|
andrewm@0
|
134 if(gRTAudioVerbose)
|
andrewm@0
|
135 cout << "Couldn't set value on amplifier mute pin\n";
|
andrewm@0
|
136 return -1;
|
andrewm@0
|
137 }
|
andrewm@0
|
138 }
|
andrewm@0
|
139
|
giuliomoro@19
|
140 // Limit the analog channels to sane values
|
giuliomoro@19
|
141 if(settings->numAnalogChannels >= 8)
|
giuliomoro@19
|
142 settings->numAnalogChannels = 8;
|
giuliomoro@19
|
143 else if(settings->numAnalogChannels >= 4)
|
giuliomoro@19
|
144 settings->numAnalogChannels = 4;
|
andrewm@12
|
145 else
|
giuliomoro@19
|
146 settings->numAnalogChannels = 2;
|
andrewm@12
|
147
|
andrewm@45
|
148 // Initialise the rendering environment: sample rates, frame counts, numbers of channels
|
andrewm@45
|
149 gContext.audioSampleRate = 44100.0;
|
andrewm@45
|
150 gContext.audioChannels = 2;
|
andrewm@45
|
151
|
andrewm@45
|
152 if(settings->useAnalog) {
|
giuliomoro@178
|
153 gContext.audioFrames = settings->periodSize;
|
andrewm@45
|
154
|
giuliomoro@178
|
155 gContext.analogFrames = gContext.audioFrames * 4 / settings->numAnalogChannels;
|
andrewm@45
|
156 gContext.analogChannels = settings->numAnalogChannels;
|
andrewm@45
|
157 gContext.analogSampleRate = gContext.audioSampleRate * 4.0 / (float)settings->numAnalogChannels;
|
andrewm@45
|
158 }
|
andrewm@45
|
159 else {
|
giuliomoro@178
|
160 gContext.audioFrames = settings->periodSize;
|
andrewm@45
|
161
|
andrewm@45
|
162 gContext.analogFrames = 0;
|
andrewm@45
|
163 gContext.analogChannels = 0;
|
andrewm@45
|
164 gContext.analogSampleRate = 0;
|
andrewm@45
|
165 }
|
andrewm@45
|
166
|
giuliomoro@178
|
167 // Sanity check the combination of channels and period size
|
giuliomoro@210
|
168 if( gContext.analogChannels != 0 && ((gContext.analogChannels <= 4 && gContext.analogFrames < 2) ||
|
giuliomoro@210
|
169 (gContext.analogChannels <= 2 && gContext.analogFrames < 4)) )
|
giuliomoro@178
|
170 {
|
giuliomoro@178
|
171 cout << "Error: " << gContext.analogChannels << " channels and period size of " << gContext.analogFrames << " not supported.\n";
|
giuliomoro@178
|
172 return 1;
|
giuliomoro@178
|
173 }
|
giuliomoro@178
|
174
|
andrewm@45
|
175 // For now, digital frame rate is equal to audio frame rate
|
andrewm@45
|
176 if(settings->useDigital) {
|
andrewm@45
|
177 gContext.digitalFrames = gContext.audioFrames;
|
andrewm@45
|
178 gContext.digitalSampleRate = gContext.audioSampleRate;
|
andrewm@45
|
179 gContext.digitalChannels = settings->numDigitalChannels;
|
andrewm@45
|
180 }
|
andrewm@45
|
181 else {
|
andrewm@45
|
182 gContext.digitalFrames = 0;
|
andrewm@45
|
183 gContext.digitalSampleRate = 0;
|
andrewm@45
|
184 gContext.digitalChannels = 0;
|
andrewm@45
|
185 }
|
andrewm@45
|
186
|
andrewm@45
|
187 // Set flags based on init settings
|
andrewm@45
|
188 if(settings->interleave)
|
andrewm@45
|
189 gContext.flags |= BEAGLERT_FLAG_INTERLEAVED;
|
andrewm@45
|
190 if(settings->analogOutputsPersist)
|
andrewm@45
|
191 gContext.flags |= BEAGLERT_FLAG_ANALOG_OUTPUTS_PERSIST;
|
andrewm@45
|
192
|
andrewm@0
|
193 // Use PRU for audio
|
andrewm@45
|
194 gPRU = new PRU(&gContext);
|
andrewm@0
|
195 gAudioCodec = new I2c_Codec();
|
andrewm@0
|
196
|
andrewm@45
|
197 // Initialise the GPIO pins, including possibly the digital pins in the render routines
|
andrewm@45
|
198 if(gPRU->prepareGPIO(1, 1)) {
|
andrewm@0
|
199 cout << "Error: unable to prepare GPIO for PRU audio\n";
|
andrewm@0
|
200 return 1;
|
andrewm@0
|
201 }
|
andrewm@45
|
202
|
andrewm@45
|
203 // Get the PRU memory buffers ready to go
|
giuliomoro@178
|
204 if(gPRU->initialise(0, gContext.analogFrames, gContext.analogChannels, true)) {
|
andrewm@0
|
205 cout << "Error: unable to initialise PRU\n";
|
andrewm@0
|
206 return 1;
|
andrewm@0
|
207 }
|
andrewm@45
|
208
|
andrewm@45
|
209 // Prepare the audio codec, which clocks the whole system
|
andrewm@5
|
210 if(gAudioCodec->initI2C_RW(2, settings->codecI2CAddress, -1)) {
|
andrewm@0
|
211 cout << "Unable to open codec I2C\n";
|
andrewm@0
|
212 return 1;
|
andrewm@0
|
213 }
|
andrewm@0
|
214 if(gAudioCodec->initCodec()) {
|
andrewm@0
|
215 cout << "Error: unable to initialise audio codec\n";
|
andrewm@0
|
216 return 1;
|
andrewm@0
|
217 }
|
giuliomoro@172
|
218
|
andrewm@5
|
219 // Set default volume levels
|
andrewm@5
|
220 BeagleRT_setDACLevel(settings->dacLevel);
|
andrewm@5
|
221 BeagleRT_setADCLevel(settings->adcLevel);
|
giuliomoro@174
|
222 // TODO: add more argument checks
|
giuliomoro@171
|
223 for(int n = 0; n < 2; n++){
|
giuliomoro@172
|
224 if(settings->pgaGain[n] > 59.5){
|
giuliomoro@172
|
225 std::cerr << "PGA gain out of range [0,59.5]\n";
|
giuliomoro@172
|
226 exit(1);
|
giuliomoro@172
|
227 }
|
giuliomoro@171
|
228 BeagleRT_setPgaGain(settings->pgaGain[n], n);
|
giuliomoro@171
|
229 }
|
andrewm@5
|
230 BeagleRT_setHeadphoneLevel(settings->headphoneLevel);
|
andrewm@5
|
231
|
andrewm@45
|
232 // Call the user-defined initialisation function
|
andrewm@56
|
233 if(!setup(&gContext, userData)) {
|
andrewm@0
|
234 cout << "Couldn't initialise audio rendering\n";
|
andrewm@0
|
235 return 1;
|
andrewm@0
|
236 }
|
andrewm@0
|
237
|
andrewm@0
|
238 return 0;
|
andrewm@0
|
239 }
|
andrewm@0
|
240
|
andrewm@0
|
241 // audioLoop() is the main function which starts the PRU audio code
|
andrewm@0
|
242 // and then transfers control to the PRU object. The PRU object in
|
andrewm@0
|
243 // turn will call the audio render() callback function every time
|
andrewm@0
|
244 // there is new data to process.
|
andrewm@0
|
245
|
andrewm@0
|
246 void audioLoop(void *)
|
andrewm@0
|
247 {
|
andrewm@0
|
248 if(gRTAudioVerbose==1)
|
andrewm@0
|
249 rt_printf("_________________Audio Thread!\n");
|
andrewm@0
|
250
|
andrewm@0
|
251 // PRU audio
|
andrewm@0
|
252 assert(gAudioCodec != 0 && gPRU != 0);
|
andrewm@0
|
253
|
andrewm@0
|
254 if(gAudioCodec->startAudio(0)) {
|
andrewm@0
|
255 rt_printf("Error: unable to start I2C audio codec\n");
|
andrewm@0
|
256 gShouldStop = 1;
|
andrewm@0
|
257 }
|
andrewm@0
|
258 else {
|
giuliomoro@16
|
259 if(gPRU->start(gPRUFilename)) {
|
giuliomoro@16
|
260 rt_printf("Error: unable to start PRU from file %s\n", gPRUFilename);
|
andrewm@0
|
261 gShouldStop = 1;
|
andrewm@0
|
262 }
|
andrewm@0
|
263 else {
|
andrewm@0
|
264 // All systems go. Run the loop; it will end when gShouldStop is set to 1
|
andrewm@5
|
265
|
andrewm@5
|
266 if(!gAmplifierShouldBeginMuted) {
|
andrewm@5
|
267 // First unmute the amplifier
|
andrewm@5
|
268 if(BeagleRT_muteSpeakers(0)) {
|
andrewm@5
|
269 if(gRTAudioVerbose)
|
andrewm@5
|
270 rt_printf("Warning: couldn't set value (high) on amplifier mute pin\n");
|
andrewm@5
|
271 }
|
andrewm@0
|
272 }
|
andrewm@0
|
273
|
andrewm@50
|
274 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
|
andrewm@45
|
275 gPRU->loop(&gRTAudioInterrupt, gUserData);
|
andrewm@50
|
276 #else
|
andrewm@50
|
277 gPRU->loop(0, gUserData);
|
andrewm@50
|
278 #endif
|
andrewm@0
|
279 // Now clean up
|
andrewm@0
|
280 // gPRU->waitForFinish();
|
andrewm@0
|
281 gPRU->disable();
|
andrewm@0
|
282 gAudioCodec->stopAudio();
|
andrewm@0
|
283 gPRU->cleanupGPIO();
|
andrewm@0
|
284 }
|
andrewm@0
|
285 }
|
andrewm@0
|
286
|
andrewm@0
|
287 if(gRTAudioVerbose == 1)
|
andrewm@0
|
288 rt_printf("audio thread ended\n");
|
andrewm@0
|
289 }
|
andrewm@0
|
290
|
andrewm@0
|
291 // Create a calculation loop which can run independently of the audio, at a different
|
andrewm@45
|
292 // (equal or lower) priority. Audio priority is defined in BEAGLERT_AUDIO_PRIORITY;
|
andrewm@45
|
293 // priority should be generally be less than this.
|
andrewm@0
|
294 // Returns an (opaque) pointer to the created task on success; 0 on failure
|
l@254
|
295 AuxiliaryTask BeagleRT_createAuxiliaryTask(void (*functionToCall)(void*), int priority, const char *name, void* args)
|
andrewm@0
|
296 {
|
andrewm@0
|
297 InternalAuxiliaryTask *newTask = (InternalAuxiliaryTask*)malloc(sizeof(InternalAuxiliaryTask));
|
andrewm@0
|
298
|
andrewm@0
|
299 // Attempt to create the task
|
andrewm@0
|
300 if(rt_task_create(&(newTask->task), name, 0, priority, T_JOINABLE | T_FPU)) {
|
andrewm@0
|
301 cout << "Error: unable to create auxiliary task " << name << endl;
|
andrewm@0
|
302 free(newTask);
|
andrewm@0
|
303 return 0;
|
andrewm@0
|
304 }
|
andrewm@0
|
305
|
andrewm@0
|
306 // Populate the rest of the data structure and store it in the vector
|
andrewm@0
|
307 newTask->function = functionToCall;
|
andrewm@0
|
308 newTask->name = strdup(name);
|
andrewm@0
|
309 newTask->priority = priority;
|
giuliomoro@174
|
310 newTask->started = false;
|
l@254
|
311 newTask->args = args;
|
andrewm@0
|
312
|
giuliomoro@176
|
313 getAuxTasks().push_back(newTask);
|
andrewm@0
|
314
|
andrewm@0
|
315 return (AuxiliaryTask)newTask;
|
andrewm@0
|
316 }
|
andrewm@0
|
317
|
giuliomoro@174
|
318 // Schedule a previously created (and started) auxiliary task. It will run when the priority rules next
|
andrewm@0
|
319 // allow it to be scheduled.
|
andrewm@47
|
320 void BeagleRT_scheduleAuxiliaryTask(AuxiliaryTask task)
|
andrewm@0
|
321 {
|
andrewm@0
|
322 InternalAuxiliaryTask *taskToSchedule = (InternalAuxiliaryTask *)task;
|
giuliomoro@174
|
323 if(taskToSchedule->started == false){ // Note: this is not the safest method to check if a task
|
giuliomoro@174
|
324 BeagleRT_startAuxiliaryTask(task); // is started (or ready to be resumed), but it probably is the fastest.
|
giuliomoro@174
|
325 // A safer approach would use rt_task_inquire()
|
giuliomoro@174
|
326 }
|
andrewm@0
|
327 rt_task_resume(&taskToSchedule->task);
|
andrewm@0
|
328 }
|
andrewm@0
|
329
|
andrewm@0
|
330 // Calculation loop that can be used for other tasks running at a lower
|
andrewm@0
|
331 // priority than the audio thread. Simple wrapper for Xenomai calls.
|
andrewm@0
|
332 // Treat the argument as containing the task structure
|
andrewm@0
|
333 void auxiliaryTaskLoop(void *taskStruct)
|
andrewm@0
|
334 {
|
andrewm@0
|
335 // Get function to call from the argument
|
l@254
|
336 void (*auxiliary_function)(void*) = ((InternalAuxiliaryTask *)taskStruct)->function;
|
andrewm@0
|
337 const char *name = ((InternalAuxiliaryTask *)taskStruct)->name;
|
andrewm@0
|
338
|
andrewm@0
|
339 // Wait for a notification
|
andrewm@0
|
340 rt_task_suspend(NULL);
|
andrewm@0
|
341
|
andrewm@0
|
342 while(!gShouldStop) {
|
andrewm@0
|
343 // Then run the calculations
|
l@254
|
344 auxiliary_function(((InternalAuxiliaryTask *)taskStruct)->args);
|
andrewm@0
|
345
|
andrewm@0
|
346 // Wait for a notification
|
andrewm@0
|
347 rt_task_suspend(NULL);
|
andrewm@0
|
348 }
|
andrewm@0
|
349
|
andrewm@0
|
350 if(gRTAudioVerbose == 1)
|
andrewm@0
|
351 rt_printf("auxiliary task %s ended\n", name);
|
andrewm@0
|
352 }
|
andrewm@0
|
353
|
giuliomoro@174
|
354
|
giuliomoro@174
|
355 int BeagleRT_startAuxiliaryTask(AuxiliaryTask task){
|
giuliomoro@174
|
356 InternalAuxiliaryTask *taskStruct;
|
giuliomoro@174
|
357 taskStruct = (InternalAuxiliaryTask *)task;
|
giuliomoro@174
|
358 if(taskStruct->started == true)
|
giuliomoro@174
|
359 return 0;
|
giuliomoro@174
|
360 if(rt_task_start(&(taskStruct->task), &auxiliaryTaskLoop, taskStruct)) {
|
giuliomoro@174
|
361 cerr << "Error: unable to start Xenomai task " << taskStruct->name << endl;
|
giuliomoro@174
|
362 return -1;
|
giuliomoro@174
|
363 }
|
giuliomoro@174
|
364 taskStruct->started = true;
|
giuliomoro@174
|
365 return 0;
|
giuliomoro@174
|
366 }
|
giuliomoro@174
|
367
|
andrewm@0
|
368 // startAudio() should be called only after initAudio() successfully completes.
|
andrewm@0
|
369 // It launches the real-time Xenomai task which runs the audio loop. Returns 0
|
andrewm@0
|
370 // on success.
|
andrewm@0
|
371
|
andrewm@5
|
372 int BeagleRT_startAudio()
|
andrewm@0
|
373 {
|
andrewm@45
|
374 // Create audio thread with high Xenomai priority
|
andrewm@45
|
375 if(rt_task_create(&gRTAudioThread, gRTAudioThreadName, 0, BEAGLERT_AUDIO_PRIORITY, T_JOINABLE | T_FPU)) {
|
andrewm@0
|
376 cout << "Error: unable to create Xenomai audio thread" << endl;
|
andrewm@0
|
377 return -1;
|
andrewm@0
|
378 }
|
andrewm@0
|
379
|
andrewm@50
|
380 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
|
andrewm@45
|
381 // Create an interrupt which the audio thread receives from the PRU
|
andrewm@45
|
382 int result = 0;
|
andrewm@45
|
383 if((result = rt_intr_create(&gRTAudioInterrupt, gRTAudioInterruptName, PRU_RTAUDIO_IRQ, I_NOAUTOENA)) != 0) {
|
andrewm@45
|
384 cout << "Error: unable to create Xenomai interrupt for PRU (error " << result << ")" << endl;
|
andrewm@45
|
385 return -1;
|
andrewm@45
|
386 }
|
andrewm@50
|
387 #endif
|
andrewm@45
|
388
|
andrewm@0
|
389 // Start all RT threads
|
andrewm@0
|
390 if(rt_task_start(&gRTAudioThread, &audioLoop, 0)) {
|
andrewm@0
|
391 cout << "Error: unable to start Xenomai audio thread" << endl;
|
andrewm@0
|
392 return -1;
|
andrewm@0
|
393 }
|
andrewm@0
|
394
|
andrewm@0
|
395 // The user may have created other tasks. Start those also.
|
andrewm@0
|
396 vector<InternalAuxiliaryTask*>::iterator it;
|
giuliomoro@176
|
397 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
|
giuliomoro@177
|
398 int ret = BeagleRT_startAuxiliaryTask(*it);
|
giuliomoro@177
|
399 if(ret != 0)
|
giuliomoro@177
|
400 return -2;
|
andrewm@0
|
401 }
|
andrewm@0
|
402 return 0;
|
andrewm@0
|
403 }
|
andrewm@0
|
404
|
andrewm@0
|
405 // Stop the PRU-based audio from running and wait
|
andrewm@0
|
406 // for the tasks to complete before returning.
|
andrewm@0
|
407
|
andrewm@5
|
408 void BeagleRT_stopAudio()
|
andrewm@0
|
409 {
|
andrewm@0
|
410 // Tell audio thread to stop (if this hasn't been done already)
|
andrewm@0
|
411 gShouldStop = true;
|
andrewm@0
|
412
|
andrewm@5
|
413 if(gRTAudioVerbose)
|
andrewm@5
|
414 cout << "Stopping audio...\n";
|
andrewm@5
|
415
|
andrewm@0
|
416 // Now wait for threads to respond and actually stop...
|
andrewm@0
|
417 rt_task_join(&gRTAudioThread);
|
andrewm@0
|
418
|
andrewm@0
|
419 // Stop all the auxiliary threads too
|
andrewm@0
|
420 vector<InternalAuxiliaryTask*>::iterator it;
|
giuliomoro@176
|
421 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
|
andrewm@0
|
422 InternalAuxiliaryTask *taskStruct = *it;
|
andrewm@0
|
423
|
andrewm@0
|
424 // Wake up each thread and join it
|
andrewm@0
|
425 rt_task_resume(&(taskStruct->task));
|
andrewm@0
|
426 rt_task_join(&(taskStruct->task));
|
andrewm@0
|
427 }
|
andrewm@0
|
428 }
|
andrewm@0
|
429
|
andrewm@0
|
430 // Free any resources associated with PRU real-time audio
|
andrewm@5
|
431 void BeagleRT_cleanupAudio()
|
andrewm@0
|
432 {
|
andrewm@56
|
433 cleanup(&gContext, gUserData);
|
andrewm@0
|
434
|
andrewm@0
|
435 // Clean up the auxiliary tasks
|
andrewm@0
|
436 vector<InternalAuxiliaryTask*>::iterator it;
|
giuliomoro@176
|
437 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
|
andrewm@0
|
438 InternalAuxiliaryTask *taskStruct = *it;
|
andrewm@0
|
439
|
andrewm@45
|
440 // Delete the task
|
andrewm@45
|
441 rt_task_delete(&taskStruct->task);
|
andrewm@45
|
442
|
andrewm@0
|
443 // Free the name string and the struct itself
|
andrewm@0
|
444 free(taskStruct->name);
|
andrewm@0
|
445 free(taskStruct);
|
andrewm@0
|
446 }
|
giuliomoro@176
|
447 getAuxTasks().clear();
|
andrewm@0
|
448
|
andrewm@45
|
449 // Delete the audio task and its interrupt
|
andrewm@50
|
450 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
|
andrewm@45
|
451 rt_intr_delete(&gRTAudioInterrupt);
|
andrewm@50
|
452 #endif
|
andrewm@45
|
453 rt_task_delete(&gRTAudioThread);
|
andrewm@45
|
454
|
andrewm@0
|
455 if(gPRU != 0)
|
andrewm@0
|
456 delete gPRU;
|
andrewm@0
|
457 if(gAudioCodec != 0)
|
andrewm@0
|
458 delete gAudioCodec;
|
andrewm@0
|
459
|
andrewm@0
|
460 if(gAmplifierMutePin >= 0)
|
andrewm@0
|
461 gpio_unexport(gAmplifierMutePin);
|
andrewm@0
|
462 gAmplifierMutePin = -1;
|
andrewm@0
|
463 }
|
andrewm@0
|
464
|
andrewm@5
|
465 // Set the level of the DAC; affects all outputs (headphone, line, speaker)
|
andrewm@5
|
466 // 0dB is the maximum, -63.5dB is the minimum; 0.5dB steps
|
andrewm@5
|
467 int BeagleRT_setDACLevel(float decibels)
|
andrewm@5
|
468 {
|
andrewm@5
|
469 if(gAudioCodec == 0)
|
andrewm@5
|
470 return -1;
|
andrewm@5
|
471 return gAudioCodec->setDACVolume((int)floorf(decibels * 2.0 + 0.5));
|
andrewm@5
|
472 }
|
andrewm@5
|
473
|
andrewm@5
|
474 // Set the level of the ADC
|
andrewm@5
|
475 // 0dB is the maximum, -12dB is the minimum; 1.5dB steps
|
andrewm@5
|
476 int BeagleRT_setADCLevel(float decibels)
|
andrewm@5
|
477 {
|
andrewm@5
|
478 if(gAudioCodec == 0)
|
andrewm@5
|
479 return -1;
|
andrewm@5
|
480 return gAudioCodec->setADCVolume((int)floorf(decibels * 2.0 + 0.5));
|
andrewm@5
|
481 }
|
andrewm@5
|
482
|
giuliomoro@171
|
483 // Set the level of the Programmable Gain Amplifier
|
giuliomoro@171
|
484 // 59.5dB is maximum, 0dB is minimum; 0.5dB steps
|
giuliomoro@171
|
485 int BeagleRT_setPgaGain(float decibels, int channel){
|
giuliomoro@171
|
486 if(gAudioCodec == 0)
|
giuliomoro@171
|
487 return -1;
|
giuliomoro@171
|
488 return gAudioCodec->setPga(decibels, channel);
|
giuliomoro@171
|
489 }
|
giuliomoro@171
|
490
|
andrewm@5
|
491 // Set the level of the onboard headphone amplifier; affects headphone
|
andrewm@5
|
492 // output only (not line out or speaker)
|
andrewm@5
|
493 // 0dB is the maximum, -63.5dB is the minimum; 0.5dB steps
|
andrewm@5
|
494 int BeagleRT_setHeadphoneLevel(float decibels)
|
andrewm@5
|
495 {
|
andrewm@5
|
496 if(gAudioCodec == 0)
|
andrewm@5
|
497 return -1;
|
andrewm@5
|
498 return gAudioCodec->setHPVolume((int)floorf(decibels * 2.0 + 0.5));
|
andrewm@5
|
499 }
|
andrewm@5
|
500
|
andrewm@5
|
501 // Mute or unmute the onboard speaker amplifiers
|
andrewm@5
|
502 // mute == 0 means unmute; otherwise mute
|
andrewm@5
|
503 // Returns 0 on success
|
andrewm@5
|
504 int BeagleRT_muteSpeakers(int mute)
|
andrewm@5
|
505 {
|
andrewm@5
|
506 int pinValue = mute ? LOW : HIGH;
|
andrewm@5
|
507
|
andrewm@5
|
508 // Check that we have an enabled pin for controlling the mute
|
andrewm@5
|
509 if(gAmplifierMutePin < 0)
|
andrewm@5
|
510 return -1;
|
andrewm@5
|
511
|
andrewm@5
|
512 return gpio_set_value(gAmplifierMutePin, pinValue);
|
andrewm@5
|
513 }
|
andrewm@5
|
514
|
andrewm@0
|
515 // Set the verbosity level
|
andrewm@45
|
516 void BeagleRT_setVerboseLevel(int level)
|
andrewm@0
|
517 {
|
andrewm@0
|
518 gRTAudioVerbose = level;
|
andrewm@0
|
519 }
|