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