/
PRU.cpp
1662 lines (1547 loc) · 57.1 KB
/
PRU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PRU.cpp
*
* Code for communicating with the Programmable Realtime Unit (PRU)
* on the BeagleBone AM335x series processors. The PRU loads and runs
* a separate code image compiled from an assembly file. Here it is
* used to handle audio and SPI ADC/DAC data.
*
* This code is specific to the PRU code in the assembly file; for example,
* it uses certain GPIO resources that correspond to that image.
*
* Created on: May 27, 2014
* Author: andrewm
*/
#include "../include/PRU.h"
#include "../include/digital_gpio_mapping.h"
#include "../include/GPIOcontrol.h"
#include "../include/Bela.h"
#include "../include/Gpio.h"
#include "../include/PruArmCommon.h"
#include "../include/board_detect.h"
#include "../include/Mcasp.h"
#include "../include/bela_hw_settings.h"
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <cmath>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <vector>
#include <sys/mman.h>
#include <string.h>
#if !(defined(BELA_USE_POLL) || defined(BELA_USE_RTDM) || defined(BELA_USE_BUSYWAIT))
#error Define one of BELA_USE_POLL, BELA_USE_RTDM, BELA_USE_BUSYWAIT
#endif
#ifdef BELA_USE_RTDM
#if __has_include(<linux/rtdm_pruss_irq.h>)
#include <linux/rtdm_pruss_irq.h>
#else /* has_include */
#define RTDM_PRUSS_IRQ_VERSION 0
#endif /* has_include */
#endif /* BELA_USE_RTDM */
#ifdef BELA_USE_RTDM
static char rtdm_driver[] = "/dev/rtdm/rtdm_pruss_irq_0";
static int rtdm_fd_pru_to_arm = 0;
#if RTDM_PRUSS_IRQ_VERSION >= 1
static const unsigned int pru_system_event_rtdm = PRU_SYSTEM_EVENT_RTDM;
#define PRU_SYS_EV_MCASP_RX_INTR 54 // mcasp_r_intr_pend
#define PRU_SYS_EV_MCASP_TX_INTR 55 // mcasp_x_intr_pend
static const uint8_t pru_system_events_mcasp[] = {PRU_SYS_EV_MCASP_RX_INTR, PRU_SYS_EV_MCASP_TX_INTR};
enum {mcasp_to_pru_channel = 1};
static int rtdm_fd_mcasp_to_pru = 0;
#endif /* RTDM_PRUSS_IRQ_VERSION >= 1 */
#endif
// Xenomai-specific includes
#if defined(XENOMAI_SKIN_native)
#include <native/task.h>
#include <native/timer.h>
#include <rtdk.h>
#endif
#if defined(XENOMAI_SKIN_posix)
#include <pthread.h>
#endif
#include "../include/xenomai_wraps.h"
using namespace std;
using namespace BelaHwComponent;
// Select whether to use NEON-based sample conversion
// (this will probably go away in a future commit once its performance
// is verified over extended use)
#undef USE_NEON_FORMAT_CONVERSION
// PRU memory: PRU0- and PRU1- DATA RAM are 8kB (0x2000) long each
// PRU-SHARED RAM is 12kB (0x3000) long
#define PRU_MEM_MCASP_OFFSET 0x2000 // Offset within PRU-SHARED RAM
#define PRU_MEM_MCASP_LENGTH 0x1000 // Length of McASP memory, in bytes
#define PRU_MEM_DAC_OFFSET 0x0 // Offset within PRU-DATA RAM
#define PRU_MEM_DAC_LENGTH 0x2000 // Length of ADC+DAC memory, in bytes
#define PRU_MEM_COMM_OFFSET 0x0 // Offset within PRU-SHARED RAM
#define PRU_MEM_DIGITAL_OFFSET 0x1000 //Offset within PRU-SHARED RAM
#define PRU_MEM_DIGITAL_BUFFER1_OFFSET 0x400 //Start pointer to DIGITAL_BUFFER1, which is 256 words.
// 256 is the maximum number of frames allowed
extern int gRTAudioVerbose;
class PruMemory
{
public:
PruMemory(int pruNumber, InternalBelaContext* context, PruManager& pruManager, size_t audioOutChannels)
{
pruSharedRam = static_cast<char*>(pruManager.getSharedMemory());
audioIn.resize(context->audioInChannels * context->audioFrames);
audioOut.resize(audioOutChannels * context->audioFrames);
digital.resize(context->digitalFrames);
pruAudioOutStart[0] = pruSharedRam + PRU_MEM_MCASP_OFFSET;
pruAudioOutStart[1] = pruSharedRam + PRU_MEM_MCASP_OFFSET + audioOut.size() * sizeof(audioOut[0]);
pruAudioInStart[0] = pruAudioOutStart[1] + audioOut.size() * sizeof(audioOut[0]);
pruAudioInStart[1] = pruAudioInStart[0] + audioIn.size() * sizeof(audioIn[0]);
pruDigitalStart[0] = pruSharedRam + PRU_MEM_DIGITAL_OFFSET;
pruDigitalStart[1] = pruSharedRam + PRU_MEM_DIGITAL_OFFSET + PRU_MEM_DIGITAL_BUFFER1_OFFSET;
if(context->analogFrames > 0)
{
pruDataRam = static_cast<char*>(pruManager.getOwnMemory());
analogOut.resize(context->analogOutChannels * context->analogFrames);
analogIn.resize(context->analogInChannels * context->analogFrames);
pruAnalogOutStart[0] = pruDataRam + PRU_MEM_DAC_OFFSET;
pruAnalogOutStart[1] = pruDataRam + PRU_MEM_DAC_OFFSET + analogOut.size() * sizeof(analogOut[0]);
pruAnalogInStart[0] = pruAnalogOutStart[1] + analogOut.size() * sizeof(analogOut[0]);
pruAnalogInStart[1] = pruAnalogInStart[0] + analogIn.size() * sizeof(analogIn[0]);
}
// Clear / initialize memory
for(int buffer = 0; buffer < 2; ++buffer)
{
for(unsigned int i = 0; i < analogOut.size(); i++)
pruAnalogOutStart[buffer][i] = 0;
for(unsigned int i = 0; i < analogIn.size(); i++)
pruAnalogInStart[buffer][i] = 0;
for(unsigned int i = 0; i < audioOut.size(); i++)
pruAudioOutStart[buffer][i] = 0;
for(unsigned int i = 0; i < audioIn.size(); i++)
pruAudioInStart[buffer][i] = 0;
// set digital to all inputs, to avoid unexpected spikes
uint32_t* digitalUint32View = (uint32_t*)pruDigitalStart[buffer];
for(unsigned int i = 0; i < digital.size(); i++)
{
digitalUint32View[i] = 0x0000ffff;
}
}
if(gRTAudioVerbose)
{
printf("PRU memory mapped to ARM:\n");
printf("digital: %p %p\n", pruDigitalStart[0], pruDigitalStart[1]);
printf("audio: %p %p %p %p\n", pruAudioOutStart[0], pruAudioOutStart[1], pruAudioInStart[0], pruAudioInStart[1]);
printf("analog: %p %p %p %p\n", pruAnalogOutStart[0], pruAnalogOutStart[1], pruAnalogInStart[0], pruAnalogInStart[1]);
printf("analog offset: %#x %#x %#x %#x\n", pruAnalogOutStart[0] - pruSharedRam, pruAnalogOutStart[1] - pruSharedRam, pruAnalogInStart[0] - pruSharedRam, pruAnalogInStart[1] - pruSharedRam);
}
}
void copyFromPru(int buffer)
{
// buffer must be 0 or 1
memcpy((void*)audioIn.data(), pruAudioInStart[buffer], audioIn.size() * sizeof(audioIn[0]));
memcpy((void*)analogIn.data(), pruAnalogInStart[buffer], analogIn.size() * sizeof(analogIn[0]));
memcpy((void*)digital.data(), pruDigitalStart[buffer], digital.size() * sizeof(digital[0]));
}
void copyToPru(int buffer)
{
// buffer must be 0 or 1
memcpy(pruAudioOutStart[buffer], (void*)audioOut.data(), audioOut.size() * sizeof(audioOut[0]));
memcpy(pruAnalogOutStart[buffer], (void*)analogOut.data(), analogOut.size() * sizeof(analogOut[0]));
memcpy(pruDigitalStart[buffer], (void*)digital.data(), digital.size() * sizeof(digital[0]));
}
uint16_t* getAnalogInPtr() { return analogIn.data(); }
uint16_t* getAnalogOutPtr() { return analogOut.data(); }
int16_t* getAudioInPtr() { return audioIn.data(); }
int16_t* getAudioOutPtr() { return audioOut.data(); }
uint32_t* getDigitalPtr() { return digital.data(); }
uint32_t* getPruBufferComm() { return (uint32_t*)(pruSharedRam + PRU_MEM_COMM_OFFSET); }
private:
char* pruDataRam = NULL;
char* pruSharedRam = NULL;
char* pruAnalogInStart[2];
char* pruAudioInStart[2];
char* pruAnalogOutStart[2];
char* pruAudioOutStart[2];
char* pruDigitalStart[2];
std::vector<uint16_t> analogIn;
std::vector<uint16_t> analogOut;
std::vector<int16_t> audioIn;
std::vector<int16_t> audioOut;
std::vector<uint32_t> digital;
};
static unsigned int* gDigitalPins = NULL;
const unsigned int belaMiniLedBlue = 87;
const unsigned int belaMiniLedRed = 89;
const unsigned int belaMiniRevCAdcPin = 65;
const unsigned int belaRevCLedRed = 81;
const unsigned int belaRevCLedBlue = 79;
const unsigned int underrunLedDuration = 20000;
const unsigned int saltSwitch1Gpio = 60; // P9_12
const unsigned int PRU::kPruGPIODACSyncPin = 5; // GPIO0(5); P9-17
const unsigned int PRU::kPruGPIOADCSyncPin = 48; // GPIO1(16); P9-15
#ifdef USE_NEON_FORMAT_CONVERSION
// These four functions are written in assembly in FormatConvert.S
extern "C" {
void int16_to_float_audio(int numSamples, int16_t *inBuffer, float *outBuffer);
void int16_to_float_analog(int numSamples, uint16_t *inBuffer, float *outBuffer);
void float_to_int16_audio(int numSamples, float *inBuffer, int16_t *outBuffer);
void float_to_int16_analog(int numSamples, float *inBuffer, uint16_t *outBuffer);
}
#endif /* USE_NEON_FORMAT_CONVERSION */
// Constructor: specify a PRU number (0 or 1)
PRU::PRU(InternalBelaContext *input_context)
: context(input_context),
pru_number(1),
initialised(false),
running(false),
analog_enabled(false),
digital_enabled(false), gpio_enabled(false), led_enabled(false),
analog_out_is_audio(false), pru_audio_out_channels(0),
pru_buffer_comm(0),
audio_expander_input_history(0), audio_expander_output_history(0),
audio_expander_filter_coeff(0), pruUsesMcaspIrq(false), belaHw(BelaHw_NoHw)
{
}
// Destructor
PRU::~PRU()
{
if(running)
disable();
exitPRUSS();
delete pruMemory;
if(gpio_enabled)
cleanupGPIO();
if(audio_expander_input_history != 0)
free(audio_expander_input_history);
if(audio_expander_output_history != 0)
free(audio_expander_output_history);
}
// Prepare the GPIO pins needed for the PRU
//If include_led is set,
// user LED 3 on the BBB is taken over by the PRU
// to indicate activity
int PRU::prepareGPIO(int include_led)
{
if(context->analogFrames != 0) {
// Prepare DAC CS/ pin: output, high to begin
if(gpio_export(kPruGPIODACSyncPin)) {
if(gRTAudioVerbose)
fprintf(stderr, "Warning: couldn't export DAC sync pin\n");
}
if(gpio_set_dir(kPruGPIODACSyncPin, OUTPUT_PIN)) {
if(gRTAudioVerbose)
fprintf(stderr, "Couldn't set direction on DAC sync pin\n");
return -1;
}
if(gpio_set_value(kPruGPIODACSyncPin, HIGH)) {
if(gRTAudioVerbose)
fprintf(stderr, "Couldn't set value on DAC sync pin\n");
return -1;
}
// Prepare ADC CS/ pin: output, high to begin
if(gpio_export(kPruGPIOADCSyncPin)) {
if(gRTAudioVerbose)
fprintf(stderr, "Warning: couldn't export ADC sync pin\n");
}
if(gpio_set_dir(kPruGPIOADCSyncPin, OUTPUT_PIN)) {
if(gRTAudioVerbose)
fprintf(stderr, "Couldn't set direction on ADC sync pin\n");
return -1;
}
if(gpio_set_value(kPruGPIOADCSyncPin, HIGH)) {
if(gRTAudioVerbose)
fprintf(stderr, "Couldn't set value on ADC sync pin\n");
return -1;
}
analog_enabled = true;
}
if(context->digitalFrames != 0){
if(Bela_hwContains(belaHw, PocketBeagle))
{
gDigitalPins = digitalPinsPocketBeagle;
} else {
gDigitalPins = digitalPinsBeagleBone;
}
for(unsigned int i = 0; i < context->digitalChannels; i++){
if(belaHw == BelaHw_Salt) {
if(gDigitalPins[i] == saltSwitch1Gpio)
continue; // leave alone this pin as it is used by bela_button.service
}
if(disabledDigitalChannels & (1 << i))
continue; // leave alone this pin because the user asked for it
if(gpio_export(gDigitalPins[i])) {
if(gRTAudioVerbose)
fprintf(stderr,"Warning: couldn't export digital GPIO pin %d\n" , gDigitalPins[i]); // this is left as a warning because if the pin has been exported by somebody else, can still be used
}
if(gpio_set_dir(gDigitalPins[i], Gpio::INPUT)) {
if(gRTAudioVerbose)
fprintf(stderr,"Error: Couldn't set direction on digital GPIO pin %d\n" , gDigitalPins[i]);
return -1;
}
}
digital_enabled = true;
}
if(include_led) {
if(Bela_hwContains(belaHw, BelaMiniCape))
{
//using on-board LED
gpio_export(belaMiniLedBlue);
gpio_set_dir(belaMiniLedBlue, OUTPUT_PIN);
} else if(Bela_hwContains(belaHw, BelaCapeRevC)) {
//using on-board LED
gpio_export(belaRevCLedBlue);
gpio_set_dir(belaRevCLedBlue, OUTPUT_PIN);
} else {
// Using BeagleBone's USR3 LED
// Turn off system function for LED3 so it can be reused by PRU
led_set_trigger(kUserLedNumber, "none");
led_enabled = true;
}
led_enabled = true;
}
gpio_enabled = true;
return 0;
}
// Clean up the GPIO at the end
void PRU::cleanupGPIO()
{
if(!gpio_enabled)
return;
if(analog_enabled) {
gpio_unexport(kPruGPIODACSyncPin);
gpio_unexport(kPruGPIOADCSyncPin);
}
if(digital_enabled){
for(unsigned int i = 0; i < context->digitalChannels; i++){
if(belaHw == BelaHw_Salt) {
if(gDigitalPins[i] == saltSwitch1Gpio)
continue; // leave alone this pin as it is used by bela_button.service
}
if(disabledDigitalChannels & (1 << i))
continue; // leave alone this pin because the user asked for it
if(gpio_unexport(gDigitalPins[i]))
{
// if unexport fails, we at least turn off the outputs
gpio_set_dir(gDigitalPins[i], OUTPUT_PIN);
gpio_set_value(gDigitalPins[i], 0);
}
}
}
if(led_enabled) {
if(Bela_hwContains(belaHw, BelaMiniCape))
{
//using on-board LED
gpio_unexport(belaMiniLedBlue);
} else if(Bela_hwContains(belaHw, BelaCapeRevC)) {
//using on-board LED
gpio_unexport(belaRevCLedBlue);
} else {
// Set LED back to default status
// TODO: make it go back to its actual value before this program,
// rather than the system default
led_set_trigger(kUserLedNumber, kUserLedDefaultTrigger);
}
}
gpio_enabled = false;
}
// Initialise and open the PRU
int PRU::initialise(BelaHw newBelaHw, int pru_num, bool uniformSampleRate, int mux_channels, int stopButtonPin, bool enableLed, uint32_t disabledDigitalChannels)
{
this->disabledDigitalChannels = disabledDigitalChannels;
belaHw = newBelaHw;
if(BelaHw_BelaRevC == belaHw)
{
analog_out_is_audio = true;
context->audioOutChannels = 2;
}
pru_audio_out_channels = analog_out_is_audio ? context->audioOutChannels + context->analogOutChannels : context->audioOutChannels;
// Initialise the GPIO pins, including possibly the digital pins in the render routines
if(prepareGPIO(enableLed)) {
fprintf(stderr, "Error: unable to prepare GPIO for PRU audio\n");
return 1;
}
hardware_analog_frames = context->analogFrames;
if(!gpio_enabled) {
fprintf(stderr, "PRU::initialise() called before GPIO enabled\n");
return 1;
}
pru_number = pru_num;
/* Allocate and initialize memory */
#if ENABLE_PRU_UIO == 1
pruManager = new PruManagerUio(pru_number, gRTAudioVerbose);
#endif // ENABLE_PRU_UIO
#if ENABLE_PRU_RPROC == 1
pruManager = new PruManagerRprocMmap(pru_number, gRTAudioVerbose);
#endif // ENABLE_PRU_RPROC
pruMemory = new PruMemory(pru_number, context, *pruManager, pru_audio_out_channels);
if(0 <= stopButtonPin){
stopButton.open(stopButtonPin, Gpio::INPUT, false);
}
if(enableLed)
{
int err = 0;
unsigned int ledRed;
if(Bela_hwContains(belaHw, BelaMiniCape)){
ledRed = belaMiniLedRed;
} else if (Bela_hwContains(belaHw, BelaCapeRevC)){
ledRed = belaRevCLedRed;
} else
err = 1;
if(!err)
{
err = underrunLed.open(ledRed, Gpio::OUTPUT, true);
if(!err)
underrunLed.clear();
}
}
if(Bela_hwContains(belaHw, BelaMiniCape))
{
// Bela Rev C and BelaMini Rev C requires resetting the SPI ADC via dedicated
// pin before we start
// It is done here for BelaMini, while for Bela it is done in
// the Es9080 initialisation code, as the reset line is shared
// between the ADS816x and the ES9080Q
if(context->analogInChannels)
{
adcNrstPin.open(belaMiniRevCAdcPin, Gpio::OUTPUT, true);
adcNrstPin.clear();
usleep(1000);
adcNrstPin.set();
}
}
// after setting all PRU settings, we adjust
// the "software" sampling rate with appropriate
// allowing for "resampling" as/if required
uniform_sample_rate = uniformSampleRate;
if(uniform_sample_rate)
{
if(context->analogOutChannels && (context->analogInChannels != context->analogOutChannels))
{
fprintf(stderr, "Different numbers of inputs and outputs is not supported yet\n");
return 1;
}
if(context->analogInChannels == 8)
analogs_per_audio = 0.5;
else if (context->analogInChannels == 4)
analogs_per_audio = 1;
else if (context->analogInChannels == 2)
analogs_per_audio = 2;
else if (context->analogInChannels != 0)
{
fprintf(stderr, "Unsupported number of analog channels per audio channels: %.3f\n", analogs_per_audio);
return 1;
}
context->analogSampleRate = context->audioSampleRate;
context->analogFrames = context->audioFrames;
}
// Allocate audio buffers
#ifdef USE_NEON_FORMAT_CONVERSION
if(belaHw == BelaHw_Salt)
{
fprintf(stderr, "USE_NEON_FORMAT_CONVERSION is incompatible with Salt\n");
return 1;
}
if(uniform_sample_rate && context->analogFrames != hardaware_analog_frames)
{
fprintf(stderr, "Error: using uniform_sample_rate is not allowed with USE_NEON_FORMAT_CONVERSION\n");
return 1;
}
if(posix_memalign((void **)&context->audioIn, 16, 2 * context->audioFrames * sizeof(float))) {
fprintf(stderr, "Error allocating audio input buffer\n");
return 1;
}
if(posix_memalign((void **)&context->audioOut, 16, 2 * context->audioFrames * sizeof(float))) {
fprintf(stderr, "Error allocating audio output buffer\n");
return 1;
}
#else
context->audioIn = (float *)malloc(context->audioInChannels * context->audioFrames * sizeof(float));
context->audioOut = (float *)calloc(1, context->audioOutChannels * context->audioFrames * sizeof(float));
if(context->audioIn == 0 || context->audioOut == 0) {
fprintf(stderr, "Error: couldn't allocate audio buffers\n");
return 1;
}
#endif
// Allocate analog buffers
if(analog_enabled) {
#ifdef USE_NEON_FORMAT_CONVERSION
if(posix_memalign((void **)&context->analogIn, 16,
context->analogInChannels * context->analogFrames * sizeof(float))) {
fprintf(stderr, "Error allocating analog input buffer\n");
return 1;
}
if(posix_memalign((void **)&context->analogOut, 16,
context->analogOutChannels * context->analogFrames * sizeof(float))) {
fprintf(stderr, "Error allocating analog output buffer\n");
return 1;
}
last_analog_out_frame = (float *)malloc(context->analogOutChannels * sizeof(float));
if(last_analog_out_frame == 0) {
fprintf(stderr, "Error: couldn't allocate analog persistence buffer\n");
return 1;
}
#else
context->analogIn = (float *)malloc(context->analogInChannels * context->analogFrames * sizeof(float));
context->analogOut = (float *)calloc(1, context->analogOutChannels * context->analogFrames * sizeof(float));
last_analog_out_frame = (float *)calloc(1, context->analogOutChannels * sizeof(float));
if(context->analogIn == 0 || context->analogOut == 0 || last_analog_out_frame == 0) {
fprintf(stderr, "Error: couldn't allocate analog buffers\n");
return 1;
}
#endif
memset(last_analog_out_frame, 0, context->analogOutChannels * sizeof(float));
context->multiplexerChannels = mux_channels;
if(mux_channels != 0 && mux_channels != 2 && mux_channels != 4 && mux_channels != 8)
{
fprintf(stderr, "Error: %d is not a valid number of multiplexer channels (options: 0 = off, 2, 4, 8).\n", mux_channels);
return 1;
}
/* Multiplexer only works with 8 analog channels. (It could be made to work with 4, with updates to the PRU code.) */
if(context->multiplexerChannels != 0 && context->analogInChannels != 8) {
fprintf(stderr, "Error: multiplexer capelet can only be used with 8 analog channels.\n");
return 1;
}
if(context->multiplexerChannels != 0) {
// If mux enabled, allocate buffers and set initial values
context->multiplexerStartingChannel = 0;
// Buffer holds 1 frame of every mux setting for every analog in
context->multiplexerAnalogIn = (float *)malloc(context->analogInChannels * context->multiplexerChannels * sizeof(float));
if(context->multiplexerAnalogIn == 0) {
fprintf(stderr, "Error: couldn't allocate audio buffers\n");
return 1;
}
}
else {
context->multiplexerStartingChannel = 0;
context->multiplexerAnalogIn = 0;
}
if((context->audioExpanderEnabled & 0x0000FFFF) != 0) {
// Audio expander enabled on at least one analog input. Allocate filter buffers
// and calculate coefficient.
audio_expander_input_history = (float *)malloc(context->analogInChannels * sizeof(float));
audio_expander_output_history = (float *)malloc(context->analogInChannels * sizeof(float));
if(audio_expander_input_history == 0 || audio_expander_output_history == 0) {
fprintf(stderr, "Error: couldn't allocate audio expander history buffers\n");
return 1;
}
for(unsigned int n = 0; n < context->analogInChannels; n++)
audio_expander_input_history[n] = audio_expander_output_history[n] = 0;
float cutoffFreqHz = 10.0;
audio_expander_filter_coeff = 1.0 / ((2.0 * M_PI * cutoffFreqHz / context->analogSampleRate) + 1.0);
}
}
else {
context->multiplexerChannels = 0;
context->multiplexerStartingChannel = 0;
context->multiplexerAnalogIn = 0;
}
if(digital_enabled) {
context->digital = pruMemory->getDigitalPtr();
last_digital_buffer = (uint32_t *)malloc(context->digitalFrames * sizeof(uint32_t)); //temp buffer to hold previous states
if(last_digital_buffer == 0) {
fprintf(stderr, "Error: couldn't allocate digital buffers\n");
return 1;
}
for(unsigned int n = 0; n < context->digitalFrames; n++){
// Initialize default value for digitals to all inputs
context->digital[n] = 0x0000ffff;
}
}
initialised = true;
return 0;
}
void PRU::initialisePruCommon(const McaspRegisters& mcaspRegisters)
{
uint32_t board_flags = 0;
switch(belaHw) {
case BelaHw_BelaMiniMultiTdm:
case BelaHw_BelaMiniMultiAudio:
case BelaHw_BelaMultiTdm:
case BelaHw_BelaMiniMultiI2s:
case BelaHw_CtagFace:
case BelaHw_CtagBeast:
case BelaHw_CtagFaceBela:
case BelaHw_CtagBeastBela:
case BelaHw_BelaEs9080:
case BelaHw_BelaRevC:
board_flags |= 1 << BOARD_FLAGS_BELA_GENERIC_TDM;
break;
case BelaHw_Bela:
case BelaHw_BelaMini:
case BelaHw_Salt:
case BelaHw_NoHw:
break;
case BelaHw_Batch: // won't actually call this function
break;
}
if(Bela_hwContains(belaHw, BelaMiniCape))
board_flags |= 1 << BOARD_FLAGS_BELA_MINI;
pru_buffer_comm[PRU_COMM_BOARD_FLAGS] = board_flags;
/* Set up flags */
pru_buffer_comm[PRU_COMM_SHOULD_STOP] = 0;
pru_buffer_comm[PRU_COMM_CURRENT_BUFFER] = 0;
unsigned int pruFrames;
if(analog_enabled)
pruFrames = hardware_analog_frames;
else
pruFrames = context->audioFrames / 2; // PRU assumes 8 "fake" channels when SPI is disabled
pru_buffer_comm[PRU_COMM_BUFFER_SPI_FRAMES] = pruFrames;
if(pruUsesMcaspIrq)
pruBufferMcaspFrames = context->audioFrames;
else // TODO: it seems that PRU_COMM_BUFFER_MCASP_FRAMES is not very meaningful(cf pru_rtaudio_irq.p)
pruBufferMcaspFrames = pruFrames * pru_audio_out_channels / 2;
pru_buffer_comm[PRU_COMM_BUFFER_MCASP_FRAMES] = pruBufferMcaspFrames;
pru_buffer_comm[PRU_COMM_SHOULD_SYNC] = 0;
pru_buffer_comm[PRU_COMM_SYNC_ADDRESS] = 0;
pru_buffer_comm[PRU_COMM_SYNC_PIN_MASK] = 0;
pru_buffer_comm[PRU_COMM_PRU_NUMBER] = pru_number;
pru_buffer_comm[PRU_COMM_ERROR_OCCURRED] = 0;
pru_buffer_comm[PRU_COMM_ACTIVE_CHANNELS] = ((uint16_t)pru_audio_out_channels & 0xFFFF) << 16 | ((uint16_t)(context->audioInChannels) & 0xFFFF);
memcpy((void*)(pru_buffer_comm + PRU_COMM_MCASP_CONF_PDIR), &mcaspRegisters,
sizeof(mcaspRegisters));
/* Set up multiplexer info */
if(context->multiplexerChannels == 2) {
pru_buffer_comm[PRU_COMM_MUX_CONFIG] = 1;
}
else if(context->multiplexerChannels == 4) {
pru_buffer_comm[PRU_COMM_MUX_CONFIG] = 2;
}
else if(context->multiplexerChannels == 8) {
pru_buffer_comm[PRU_COMM_MUX_CONFIG] = 3;
}
else {
// we trust that the number of multiplexer channels has been
// checked elsewhere
pru_buffer_comm[PRU_COMM_MUX_CONFIG] = 0;
}
if(led_enabled) {
unsigned int pin;
if(Bela_hwContains(belaHw, BelaMiniCape))
{
pin = belaMiniLedBlue;
} else if(Bela_hwContains(belaHw, BelaCapeRevC)) {
pin = belaRevCLedBlue;
} else {
pin = kUserLedGpioPin;
}
uint32_t base = Gpio::getBankAddress(pin / 32);
uint32_t mask = 1 << (pin % 32);
pru_buffer_comm[PRU_COMM_LED_ADDRESS] = base;
pru_buffer_comm[PRU_COMM_LED_PIN_MASK] = mask;
} else {
pru_buffer_comm[PRU_COMM_LED_ADDRESS] = 0;
pru_buffer_comm[PRU_COMM_LED_PIN_MASK] = 0;
}
if(analog_enabled) {
pru_buffer_comm[PRU_COMM_USE_SPI] = 1;
// TODO : a different number of channels for inputs and outputs
// is not yet supported
unsigned int analogChannels = context->analogInChannels;
pru_buffer_comm[PRU_COMM_SPI_NUM_CHANNELS] = analogChannels;
} else {
pru_buffer_comm[PRU_COMM_USE_SPI] = 0;
pru_buffer_comm[PRU_COMM_SPI_NUM_CHANNELS] = 0;
}
if(digital_enabled) {
pru_buffer_comm[PRU_COMM_USE_DIGITAL] = 1;
}
else {
pru_buffer_comm[PRU_COMM_USE_DIGITAL] = 0;
}
}
// Run the code image in the specified file
int PRU::start(char * const filename, const McaspRegisters& mcaspRegisters)
{
switch(belaHw)
{
case BelaHw_Bela:
//nobreak
case BelaHw_BelaMini:
//nobreak
case BelaHw_Salt:
pruUsesMcaspIrq = false;
break;
case BelaHw_BelaMiniMultiAudio:
//nobreak
case BelaHw_BelaMiniMultiTdm:
//nobreak
case BelaHw_BelaMultiTdm:
//nobreak
case BelaHw_BelaMiniMultiI2s:
//nobreak
case BelaHw_CtagFace:
//nobreak
case BelaHw_CtagBeast:
//nobreak
case BelaHw_CtagFaceBela:
//nobreak
case BelaHw_CtagBeastBela:
//nobreak
case BelaHw_BelaEs9080:
//nobreak
case BelaHw_BelaRevC:
pruUsesMcaspIrq = true;
break;
case BelaHw_NoHw:
default:
fprintf(stderr, "Error: unrecognized hardware\n");
return 1;
}
if(gRTAudioVerbose)
printf("%ssing McASP->PRU irq\n", pruUsesMcaspIrq ? "U" : "Not u");
#if RTDM_PRUSS_IRQ_VERSION < 1
if(pruUsesMcaspIrq)
{
fprintf(stderr, "Error: the installed rtdm_pruss_irq driver cannot be used in conjunction with McASP interrupts. Update the driver\n");
return -1;
}
#endif /* RTDM_PRUSS_IRQ_VERSION */
#ifdef BELA_USE_RTDM
// Open RTDM driver
// NOTE: if this is moved later on, (e.g.: at the beginning of loop())
// it will often hang the system (especially for small blocksizes).
// Not sure why this would happen, perhaps a race condition between the PRU
// and the rtdm_driver?
if ((rtdm_fd_pru_to_arm = __wrap_open(rtdm_driver, O_RDWR)) < 0) {
fprintf(stderr, "Failed to open the kernel driver: (%d) %s.\n", errno, strerror(errno));
if(errno == EBUSY) // Device or resource busy
{
fprintf(stderr, "Another program is already running?\n");
}
if(errno == ENOENT) // No such file or directory
{
fprintf(stderr, "Maybe try\n modprobe rtdm_pruss_irq\n?\n");
}
return 1;
}
#if RTDM_PRUSS_IRQ_VERSION >= 2
{
// From version 2 onwards we can set the verbose level
int ret = __wrap_ioctl(rtdm_fd_pru_to_arm, RTDM_PRUSS_IRQ_VERBOSE, 0);
if(ret == -1)
fprintf(stderr, "ioctl verbose failed: %d %s\n", errno, strerror(errno));
// do not fail
}
#endif // RTDM_PRUSS_IRQ_VERSION >= 2
#if RTDM_PRUSS_IRQ_VERSION >= 1
// From version 1 onwards, we need to specify the PRU system event we want to receive interrupts from (see rtdm_pruss_irq.h)
// For rtdm_fd_pru_to_arm we use the default mapping
int ret = __wrap_ioctl(rtdm_fd_pru_to_arm, RTDM_PRUSS_IRQ_REGISTER, pru_system_event_rtdm);
if(ret == -1)
{
fprintf(stderr, "ioctl failed: %d %s\n", errno, strerror(errno));
return 1;
}
if(pruUsesMcaspIrq)
{
if ((rtdm_fd_mcasp_to_pru = __wrap_open(rtdm_driver, O_RDWR)) < 0) {
fprintf(stderr, "Unable to open rtdm driver to register McASP interrupts: (%d) %s.\n", errno, strerror(errno));
return 1;
}
// For rtdm_fd_mcasp_to_pru we use an arbitrary mapping to set up
// the McASP to PRU interrupt.
// We use PRU-INTC channel 0, which will trigger the PRUs R31.t30
// This will not propagate to ARM (in
// fact we have to mask it from ARM elsewhere), so no Linux/rtdm
// IRQ is set up by the driver and we will not be able/need to
// call `read()` on `rtdm_fd_mcasp_to_pru`.
struct rtdm_pruss_irq_registration rtdm_struct;
rtdm_struct.pru_system_events = pru_system_events_mcasp;
rtdm_struct.pru_system_events_count = sizeof(pru_system_events_mcasp);
rtdm_struct.pru_intc_channel = mcasp_to_pru_channel;
rtdm_struct.pru_intc_host = mcasp_to_pru_channel;
int ret = __wrap_ioctl(rtdm_fd_mcasp_to_pru, RTDM_PRUSS_IRQ_REGISTER_FULL, &rtdm_struct);
if(ret == -1)
{
fprintf(stderr, "ioctl failed: %d %s\n", errno, strerror(errno));
return 1;
}
}
#endif /* RTDM_PRUSS_IRQ_VERSION >= 1 */
#endif /* BELA_USE_RTDM */
pru_buffer_comm = pruMemory->getPruBufferComm();
initialisePruCommon(mcaspRegisters);
// NOTE: we assume that something else has masked the McASP interrupts
// from ARM, or rather that no one else unmasked them.
// For instance, make sure the McASP driver does not get to get hold of them
// by NOT setting `interrupt-names = "rx", "tx";` in the overlay
/* Load and execute binary on PRU */
bool useEmbeddedPruCode = ("" == std::string(filename));
unsigned int pruManager_ret = 0;
if(gRTAudioVerbose)
printf("Using %s %s PRU firmware\n", pruUsesMcaspIrq ? "McASP IRQ" : "Non-McASP IRQ", useEmbeddedPruCode ? "embedded" : filename);
if(useEmbeddedPruCode)
pruManager_ret = pruManager->start(pruUsesMcaspIrq);
else
pruManager_ret = pruManager->start(std::string(filename)); // simply passing filename calls the wrong overload
if(pruManager_ret)
{
fprintf(stderr, "Failed to execute PRU code\n");
return 1;
}
running = true;
return 0;
}
int PRU::testPruError()
{
if (unsigned int errorCode = pru_buffer_comm[PRU_COMM_ERROR_OCCURRED])
{
// only print warnings if we have been running for a while, or forced to do so
bool verbose = (context->audioFramesElapsed > 5000) || gRTAudioVerbose;
verbose && rt_fprintf(stderr, "audio frame %llu, errorCode: %d\n", context->audioFramesElapsed, errorCode);
int ret;
switch(errorCode){
case ARM_ERROR_XUNDRUN:
verbose && rt_fprintf(stderr, "McASP transmitter underrun occurred\n");
ret = 1;
break;
case ARM_ERROR_XSYNCERR:
verbose && rt_fprintf(stderr, "McASP unexpected transmit frame sync occurred\n");
ret = 1;
break;
// Sometimes a transmit clock error arises after boot. If the PRU loop
// continues, the clock error is automatically solved. Hence, no additional
// error handling is required on ARM side.
case ARM_ERROR_XCKFAIL:
verbose && rt_fprintf(stderr, "McASP transmit clock failure occurred\n");
ret = 1;
break;
// Same for DMA error. No action needed on ARM side.
case ARM_ERROR_XDMAERR:
verbose && rt_fprintf(stderr, "McASP transmit DMA error occurred\n");
ret = 1;
break;
case ARM_ERROR_TIMEOUT:
verbose && rt_fprintf(stderr, "PRU event loop timed out\n");
ret = 1;
break;
case ARM_ERROR_INVALID_INIT:
fprintf(stderr, "Invalid PRU configuration settings\n");
ret = 2;
break;
default:
verbose && rt_fprintf(stderr, "Unknown PRU error: %d\n", errorCode);
ret = 1;
}
pru_buffer_comm[PRU_COMM_ERROR_OCCURRED] = 0;
return ret;
} else {
return 0;
}
}
static inline int16_t audioFloatToAudioRaw(float value)
{
int out = value * 32768.0f;
if(out < -32768) out = -32768;
else if(out > 32767) out = 32767;
return out;
}
static inline int16_t analogFloatToAudioRaw(float value)
{
#if 0
// Es9080Q EVB: FS output is scaled via inverting amp to 0V:5V, we use
// the full range
return audioFloatToAudioRaw((1.f - value) * 2.f - 1.f);
#else
// Cape Rev C: FS outupt is scaled via non-inverting amp to -5V:5V, but
// we are only using the positive range of that (15 bit) when not on Pepper
float out = audioFloatToAudioRaw(value);
if(out < 0)
out = 0;
return out;
#endif
}
// Main loop to read and write data from/to PRU
void PRU::loop(void *userData, void(*render)(BelaContext*, void*), bool highPerformanceMode, BelaCpuData* cpuData)
{
// these pointers will be constant throughout the lifetime of pruMemory
uint16_t* analogInRaw = pruMemory->getAnalogInPtr();
uint16_t* analogOutRaw = pruMemory->getAnalogOutPtr();
int16_t* audioInRaw = pruMemory->getAudioInPtr();
int16_t* audioOutRaw = pruMemory->getAudioOutPtr();
// Polling interval is 1/4 of the period
time_ns_t sleepTime = 1000000000 * (float)context->audioFrames / (context->audioSampleRate * 4);
if(highPerformanceMode) // sleep less, more CPU available for us
sleepTime /= 4;
#ifdef BELA_USE_RTDM
// If we have ultra-small block sizes, sleeping is just detrimental: disable it
// by enabling highPerformanceMode
if(context->audioFrames <= 2)
highPerformanceMode = true;
#endif
//sleepTime = context->audioFrames / context->audioSampleRate / 4.f * 1000000000.f;
// Before starting, look at the last state of the analog and digital outputs which might
// have been changed by the user during the setup() function. This lets us start with pin
// directions and output values at something other than defaults.
if(analog_enabled) {
if(context->flags & BELA_FLAG_ANALOG_OUTPUTS_PERSIST) {
// Remember the content of the last_analog_out_frame
for(unsigned int ch = 0; ch < context->analogOutChannels; ch++){
last_analog_out_frame[ch] = context->analogOut[context->analogOutChannels * (context->analogFrames - 1) + ch];
}
}
}
if(digital_enabled) {
for(unsigned int n = 0; n < context->digitalFrames; n++){
last_digital_buffer[n] = context->digital[n];
}
}
bool interleaved = context->flags & BELA_FLAG_INTERLEAVED;
int underrunLedCount = -1;
while(!Bela_stopRequested()) {
#if defined BELA_USE_POLL || defined BELA_USE_BUSYWAIT
// Which buffer the PRU was last processing
static uint32_t lastPRUBuffer = 0;
// Poll
while(pru_buffer_comm[PRU_COMM_CURRENT_BUFFER] == lastPRUBuffer && !Bela_stopRequested()) {
#ifdef BELA_USE_POLL
task_sleep_ns(sleepTime);
#endif /* BELA_USE_POLL */
if(testPruError())
break;
}
lastPRUBuffer = pru_buffer_comm[PRU_COMM_CURRENT_BUFFER];
#endif /* BELA_USE_POLL || BELA_USE_BUSYWAIT */
#ifdef BELA_USE_RTDM
// make sure we always sleep a tiny bit to prevent hanging the board
if(!highPerformanceMode) // unless the user requested us not to.
task_sleep_ns(sleepTime / 2);
int ret = __wrap_read(rtdm_fd_pru_to_arm, NULL, 0);
int error = testPruError();
if(2 == error) {
gShouldStop = true;
break;
}
if(ret < 0)
{
static int interruptTimeoutCount = 0;
++interruptTimeoutCount;
rt_fprintf(stderr, "PRU interrupt timeout, %d %d %s\n", ret, errno, strerror(errno));
if(interruptTimeoutCount >= 5)
{
fprintf(stderr, "McASP error, abort\n");
exit(1); // Quitting abruptly, purposedly skipping the cleanup so that we can inspect the PRU with prudebug.
}
task_sleep_ns(100000000);
}
#endif
if(cpuData)
Bela_cpuTic(cpuData);