giuliomoro@301
|
1 #include <Bela.h>
|
giuliomoro@189
|
2 #include <cmath>
|
giuliomoro@189
|
3 #include <rtdk.h>
|
giuliomoro@189
|
4 #include <sys/time.h>
|
giuliomoro@189
|
5 #include <sys/types.h>
|
giuliomoro@189
|
6 #include <unistd.h>
|
giuliomoro@189
|
7 #include <stats.hpp>
|
giuliomoro@189
|
8 // setup() is called once before the audio rendering starts.
|
giuliomoro@189
|
9 // Use it to perform any initialisation and allocation which is dependent
|
giuliomoro@189
|
10 // on the period size or sample rate.
|
giuliomoro@189
|
11 //
|
giuliomoro@189
|
12 // userData holds an opaque pointer to a data structure that was passed
|
giuliomoro@189
|
13 // in from the call to initAudio().
|
giuliomoro@189
|
14 //
|
giuliomoro@189
|
15 // Return true on success; returning false halts the program.
|
giuliomoro@189
|
16
|
giuliomoro@189
|
17
|
giuliomoro@189
|
18 // digital inputs can be changed at will (as they are all being processed at the same time)
|
giuliomoro@189
|
19 // analog channels must be as per below
|
giuliomoro@189
|
20 int gAnalogOutCh = 1;
|
giuliomoro@189
|
21 int gDigitalOutCh = 2;
|
giuliomoro@189
|
22 int gDigitalInACh = 0;
|
giuliomoro@189
|
23 int gDigitalInDCh = 3;
|
giuliomoro@189
|
24 int gAnalogOutLoopDelay;
|
giuliomoro@189
|
25 int gDigitalOutLoopDelay;
|
giuliomoro@301
|
26 bool setup(BelaContext *context, void *userData)
|
giuliomoro@189
|
27 {
|
giuliomoro@189
|
28 rt_printf("For this test you need the following connections:\n"
|
giuliomoro@189
|
29 "analog%d out->digital%d in, analog%d out->analog%d in, "
|
giuliomoro@189
|
30 "digital%d out -> digital%d in, digital%d out-> analog%d in\n",
|
giuliomoro@189
|
31 gAnalogOutCh, gDigitalInACh, gAnalogOutCh, 0, gDigitalOutCh, gDigitalInDCh, gDigitalOutCh, 0);
|
giuliomoro@189
|
32 rt_printf("Running test with %d analog channels and a buffer size of %d\n",
|
giuliomoro@189
|
33 context->analogChannels, context->audioFrames);
|
giuliomoro@189
|
34
|
giuliomoro@189
|
35 for(unsigned int n = 0; n < context->digitalFrames; n++){
|
andrewm@310
|
36 pinMode(context, n, gDigitalInACh, INPUT);
|
andrewm@310
|
37 pinMode(context, n, gDigitalInDCh, INPUT);
|
andrewm@310
|
38 pinMode(context, n, gDigitalOutCh, OUTPUT);
|
giuliomoro@189
|
39 }
|
giuliomoro@189
|
40 switch (context->analogChannels){
|
giuliomoro@189
|
41 case 2:
|
giuliomoro@189
|
42 gAnalogOutLoopDelay = context->audioFrames*2 + 3;
|
giuliomoro@189
|
43 gDigitalOutLoopDelay = context->audioFrames*2 + 2;
|
giuliomoro@189
|
44 break;
|
giuliomoro@189
|
45 case 4:
|
giuliomoro@189
|
46 gAnalogOutLoopDelay = context->audioFrames*2 + 3;
|
giuliomoro@189
|
47 gDigitalOutLoopDelay = context->audioFrames*2 + 2;
|
giuliomoro@189
|
48 break;
|
giuliomoro@189
|
49 case 8:
|
giuliomoro@189
|
50 gAnalogOutLoopDelay = context->audioFrames + 3;
|
giuliomoro@189
|
51 gDigitalOutLoopDelay = context->audioFrames + 1;
|
giuliomoro@189
|
52 break;
|
giuliomoro@189
|
53 default:
|
giuliomoro@189
|
54 exit(2);
|
giuliomoro@189
|
55 }
|
giuliomoro@189
|
56
|
giuliomoro@189
|
57 return true;
|
giuliomoro@189
|
58 }
|
giuliomoro@189
|
59
|
giuliomoro@189
|
60 // render() is called regularly at the highest priority by the audio engine.
|
giuliomoro@189
|
61 // Input and output are given from the audio hardware and the other
|
giuliomoro@189
|
62 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
|
giuliomoro@189
|
63 // will be 0.
|
giuliomoro@189
|
64
|
giuliomoro@189
|
65 const int patternLength = 31;
|
giuliomoro@189
|
66 static int anaErrorCount = 0;
|
giuliomoro@189
|
67 static int digErrorCount = 0;
|
giuliomoro@301
|
68 void render(BelaContext *context, void *userData)
|
giuliomoro@189
|
69 {
|
giuliomoro@189
|
70 static bool writePattern[patternLength] = {
|
giuliomoro@189
|
71 0,1,0,1,0,0,1,1,
|
giuliomoro@189
|
72 0,0,0,1,1,1,0,0,
|
giuliomoro@189
|
73 1,1,1,1,0,0,0,0,
|
giuliomoro@189
|
74 1,1,1,1,1,0,0};
|
giuliomoro@189
|
75 // for(int n = 0; n < patternLength; n++){
|
giuliomoro@189
|
76 // writePattern[n]=1;
|
giuliomoro@189
|
77 // }
|
giuliomoro@189
|
78 static int inPointer = 0;
|
giuliomoro@189
|
79 static int outPointer = 0;
|
giuliomoro@189
|
80 static int digitalOutPointer = 0;
|
giuliomoro@189
|
81 static int digitalInPointer = 0;
|
giuliomoro@189
|
82 static int analogOut = 1;
|
giuliomoro@189
|
83 /** Checking offset between analog and digital
|
giuliomoro@189
|
84 * how it should be :
|
giuliomoro@189
|
85 * The PRU loop does the following (the loop runs at 88.2kHz):
|
giuliomoro@189
|
86 * - Read/write audio sample (once for the left channel, once for the right channel)
|
giuliomoro@189
|
87 * - Write DAC 0 or 0/2 or 0/2/4/6
|
giuliomoro@189
|
88 * - Read ADC 0 or 0/2 or 0/2/4/6, 2 samples (@176.4) older than NOW
|
giuliomoro@189
|
89 * - /During/ the line above, every two loops we also Read/Write GPIO,
|
giuliomoro@189
|
90 * therefore reading on ADC 0/2/4/6 a value that is being output from GPIO will lead to undefined results
|
giuliomoro@189
|
91 * - Write DAC 1 or 1/3 or 1/3/5/7
|
giuliomoro@189
|
92 * - Read ADC 1 or 1/3 or 1/3/5/7, 2 samples (@176.4) older than NOW
|
giuliomoro@189
|
93 */
|
giuliomoro@189
|
94 if(1)
|
giuliomoro@189
|
95 for(unsigned int n = 0; n < context->audioFrames; n++){
|
giuliomoro@189
|
96 static bool analog0In = false;
|
giuliomoro@189
|
97 static bool digitalAIn = false;
|
giuliomoro@189
|
98 static int count = 0;
|
giuliomoro@189
|
99 bool doReadWrite = context->analogChannels<=4 ? true : ((context->analogChannels == 8) && (n&1)==0);
|
giuliomoro@189
|
100 if(doReadWrite){
|
andrewm@308
|
101 digitalAIn = digitalRead(context, n, gDigitalInACh);
|
giuliomoro@189
|
102 switch(context->analogChannels){
|
giuliomoro@189
|
103 case 8:
|
andrewm@308
|
104 analog0In = analogRead(context, n/2, 0) > 0.5;
|
andrewm@308
|
105 analogWrite(context, n/2, analogOut, writePattern[outPointer]);
|
giuliomoro@189
|
106 break;
|
giuliomoro@189
|
107 case 4:
|
andrewm@308
|
108 analog0In = analogRead(context, n, 0) > 0.5;
|
andrewm@308
|
109 analogWrite(context, n, analogOut, writePattern[outPointer]);
|
giuliomoro@189
|
110 break;
|
giuliomoro@189
|
111 case 2:
|
andrewm@308
|
112 analog0In = analogRead(context, n * 2 + 1, 0) > 0.5;
|
andrewm@308
|
113 analogWrite(context, 2 * n, analogOut, writePattern[outPointer]);
|
andrewm@308
|
114 analogWrite(context, 2 * n + 1, analogOut, writePattern[outPointer]);
|
giuliomoro@189
|
115 break;
|
giuliomoro@189
|
116 }
|
giuliomoro@189
|
117 gAnalogOutLoopDelay--;
|
giuliomoro@189
|
118 outPointer++;
|
giuliomoro@189
|
119 if(gAnalogOutLoopDelay <= 0){
|
giuliomoro@189
|
120 if(++inPointer == patternLength){
|
giuliomoro@189
|
121 inPointer = 0;
|
giuliomoro@189
|
122 }
|
giuliomoro@189
|
123 }
|
giuliomoro@189
|
124 }
|
giuliomoro@189
|
125 bool expectedIn = writePattern[inPointer];
|
giuliomoro@189
|
126 if(gAnalogOutLoopDelay <= 0 && doReadWrite == true){
|
giuliomoro@189
|
127 if(analog0In != expectedIn || digitalAIn != expectedIn){
|
giuliomoro@189
|
128 rt_printf("expected: %d, received: %d %d, pointer: %d, delay: %d, count: %d\n",
|
giuliomoro@189
|
129 expectedIn, analog0In, digitalAIn, inPointer, gAnalogOutLoopDelay, count);
|
giuliomoro@189
|
130 anaErrorCount++;
|
giuliomoro@189
|
131 }
|
giuliomoro@189
|
132 }
|
giuliomoro@189
|
133 count++;
|
giuliomoro@189
|
134 if(analog0In != digitalAIn){ // at any time the analog and digital in should be the same
|
giuliomoro@189
|
135 rt_printf("ana %d_%d %d,\n", analog0In, digitalAIn, n);
|
giuliomoro@189
|
136 }
|
giuliomoro@189
|
137 if(outPointer == patternLength){
|
giuliomoro@189
|
138 outPointer = 0;
|
giuliomoro@189
|
139 }
|
giuliomoro@189
|
140 }
|
giuliomoro@189
|
141 if(1)
|
giuliomoro@189
|
142 for(unsigned int n = 0; n < context->audioFrames; n++){
|
giuliomoro@189
|
143 static int count = 0;
|
giuliomoro@189
|
144 static bool analog1In = false;
|
giuliomoro@189
|
145 static bool digitalDIn = false;
|
giuliomoro@189
|
146 /* we need to remember the pastAnalog1In because
|
giuliomoro@189
|
147 * reading GPIO takes place before writing to it, therefore
|
giuliomoro@189
|
148 * when reading a GPIOout, the GPIOin samples will always be one sample late
|
giuliomoro@189
|
149 */
|
giuliomoro@189
|
150 bool doReadWrite = false;
|
giuliomoro@189
|
151 static bool pastAnalog1In = false;
|
andrewm@308
|
152 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]);
|
giuliomoro@189
|
153 if(context->analogChannels == 8){
|
giuliomoro@189
|
154 if((n&1) == 0){ //do it every other sample
|
andrewm@308
|
155 pastAnalog1In = analogRead(context, n/2, 1) > 0.5;
|
andrewm@308
|
156 digitalDIn = digitalRead(context, n, gDigitalInDCh);
|
giuliomoro@189
|
157 doReadWrite = true;
|
giuliomoro@189
|
158 }
|
giuliomoro@189
|
159 }
|
giuliomoro@189
|
160 if(context->analogChannels == 4){
|
andrewm@308
|
161 pastAnalog1In = analogRead(context, n, 1) > 0.5;
|
andrewm@308
|
162 digitalDIn = digitalRead(context, n, gDigitalInDCh);
|
andrewm@308
|
163 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]);
|
giuliomoro@189
|
164 doReadWrite = true;
|
giuliomoro@189
|
165 }
|
giuliomoro@189
|
166 if(context->analogChannels == 2){
|
andrewm@308
|
167 pastAnalog1In = analogRead(context, n * 2, 1) > 0.5;
|
andrewm@308
|
168 digitalDIn = digitalRead(context, n, gDigitalInDCh);
|
andrewm@308
|
169 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]);
|
giuliomoro@189
|
170 doReadWrite = true;
|
giuliomoro@189
|
171 }
|
giuliomoro@189
|
172 bool expectedDigitalIn = writePattern[digitalInPointer];
|
giuliomoro@189
|
173 if(doReadWrite == true){
|
giuliomoro@189
|
174 gDigitalOutLoopDelay--;
|
giuliomoro@189
|
175 if(gDigitalOutLoopDelay <= 0){
|
giuliomoro@189
|
176 if(expectedDigitalIn != pastAnalog1In || expectedDigitalIn != digitalDIn){
|
giuliomoro@189
|
177 rt_printf("D expected: %d, received: %d %d, pointer: %d, delay: %d, count: %d\n",
|
giuliomoro@189
|
178 expectedDigitalIn, pastAnalog1In, digitalDIn, inPointer, gDigitalOutLoopDelay, count);
|
giuliomoro@189
|
179 digErrorCount++;
|
giuliomoro@189
|
180 }
|
giuliomoro@189
|
181 if(++digitalInPointer == patternLength){
|
giuliomoro@189
|
182 digitalInPointer = 0;
|
giuliomoro@189
|
183 }
|
giuliomoro@189
|
184 }
|
giuliomoro@189
|
185 pastAnalog1In = analog1In;
|
giuliomoro@189
|
186 if(++digitalOutPointer == patternLength){
|
giuliomoro@189
|
187 digitalOutPointer = 0;
|
giuliomoro@189
|
188 }
|
giuliomoro@189
|
189 }
|
giuliomoro@189
|
190 count++;
|
giuliomoro@189
|
191 }
|
andrewm@311
|
192 if(context->audioFramesElapsed > 30000){
|
giuliomoro@189
|
193 gShouldStop = true;
|
giuliomoro@189
|
194 }
|
giuliomoro@189
|
195 }
|
giuliomoro@189
|
196
|
giuliomoro@189
|
197
|
giuliomoro@301
|
198 void cleanup(BelaContext *context, void *userData)
|
giuliomoro@189
|
199 {
|
giuliomoro@189
|
200 if(anaErrorCount == 0 && digErrorCount == 0){
|
giuliomoro@189
|
201 rt_printf("Test was succesful with %d analog channels and a buffer size of %d\n", context->analogChannels, context->audioFrames);
|
giuliomoro@189
|
202 } else {
|
giuliomoro@189
|
203 rt_printf("------------------------\n%danalog %ddigital errors over %dsamples while running test with ",
|
andrewm@311
|
204 anaErrorCount, digErrorCount, context->audioFramesElapsed);
|
giuliomoro@189
|
205 rt_printf("%d analog channels and a buffer size of %d \n\n\n",
|
giuliomoro@189
|
206 context->analogChannels, context->audioFrames);
|
giuliomoro@189
|
207 exit(1);
|
giuliomoro@189
|
208 }
|
giuliomoro@189
|
209 }
|