-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrillRackApplicationStm32.cpp
1062 lines (983 loc) · 30.2 KB
/
TrillRackApplicationStm32.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
#include "TrillRackApplicationStm32.h"
#include <TrillRackApplication_bsp.h>
#include <main.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "Bela.h"
#include "../common_stuff/pinManager.h"
const float SAMPLE_RATE = 42500;
enum {
kCommandNone = 0,
kCommandMode = 1,
kCommandScanSettings = 2,
kCommandPrescaler = 3,
kCommandNoiseThreshold = 4,
kCommandIdac = 5,
kCommandBaselineUpdate = 6,
kCommandMinimumSize = 7,
kCommandAdjacentCentroidNoiseThreshold = 8,
kCommandAutoScanInterval = 16,
kCommandIdentify = 255
};
enum {
kModeCentroid = 0,
kModeRaw = 1,
kModeBaseline = 2,
kModeDiff = 3
};
enum {
kOffsetCommand = 0,
kOffsetData = 4
};
enum { kNumTouches = 5 };
// The device 7 bits address value in datasheet must be shifted to the left before calling the interface
// may be overridden below
uint8_t gI2cAddress = 0x20 << 1;
const unsigned int kTimeout = 10;
//#define TOGGLE_DEBUG_PINS
#define I2C_USE_DMA
//#define I2C_ON_EVT
#define I2C_MANUAL 5
#define DAC_USE_DMA
#define ADC_USE_DMA
#define GPIO_OUT_USE_DMA
#define GPIO_IN_USE_DMA
#define NEOPIXEL_USE_TIM
#define TRILL_RACK_INTERFACE
#ifdef TRILL_RACK_INTERFACE
#include "trill-neopixel/TrillRackInterface.h"
#endif // TRILL_RACK_INTERFACE
#ifdef I2C_USE_DMA
#ifndef I2C_MANUAL
static
#endif // I2C_MANUAL
int startScanning();
#ifdef I2C_ON_EVT
static volatile int gI2cOnEvtEnabled = false; // allows us to defer reading on EVT until we are fully ready
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(PSOC_EVENT_Pin == GPIO_Pin)
{
HAL_GPIO_WritePin(DEBUG0_GPIO_Port, DEBUG0_Pin, GPIO_PIN_SET);
if(gI2cOnEvtEnabled)
startScanning();
HAL_GPIO_WritePin(DEBUG0_GPIO_Port, DEBUG0_Pin, GPIO_PIN_RESET);
}
}
#endif // I2C_ON_EV
#define TRILL_USE_CLASS
#ifdef TRILL_USE_CLASS
int trillSetup();
void trillNewData(const uint8_t* data, size_t len);
int trillRead();
unsigned int trillNumTouches();
float trillTouchLocation(unsigned int n);
float trillTouchSize(unsigned int n);
#endif // TRILL_USE_CLASS
enum { gI2cDmaRecvSize = 61 };
uint8_t gI2cDmaRecv[gI2cDmaRecvSize];
#ifndef TRILL_RACK_INTERFACE
uint8_t gI2cLatestRecv[gI2cDmaRecvSize];
#endif // TRILL_RACK_INTERFACE
static volatile bool gIsScanning = false;
// In order for this to be called, the I2C1 EVENT interrupt must be manually enabled in CubeMx.
// See https://blog.shirtec.com/2019/10/stm32-hal-i2c-itdma-gotcha.html
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
// for(unsigned int n = 0; n < gI2cDmaRecvSize; n += 2)
// printf("%d ", gI2cDmaRecv[n] * 256 + gI2cDmaRecv[n + 1]);
// printf("\n\r");
gIsScanning = false;
#ifdef TRILL_RACK_INTERFACE
tr_newData(gI2cDmaRecv, gI2cDmaRecvSize);
#else // TRILL_RACK_INTERFACE
#ifdef TRILL_USE_CLASS
trillNewData(gI2cDmaRecv, gI2cDmaRecvSize);
#else // TRILL_USE_CLASS
memcpy(gI2cLatestRecv, gI2cDmaRecv, gI2cDmaRecvSize);
#endif // TRILL_USE_CLASS
#endif // TRILL_RACK_INTERFACE
#if !(defined(I2C_ON_EVT) || defined(I2C_MANUAL))
startScanning();
#endif // I2C_ON_EVT || I2C_MANUAL
}
#endif // I2C_USE_DMA
typedef enum {
#ifdef DAC_USE_DMA
kDAC0,
kDAC1,
#endif //DAC_USE_DMA
#ifdef ADC_USE_DMA
kADC,
#endif // ADC_USE_DMA
#ifdef GPIO_OUT_USE_DMA
kGpioOut,
#endif // GPIO_OUT_USE_DMA
#ifdef GPIO_IN_USE_DMA
kGpioIn,
#endif // GPIO_IN_USE_DMA
kNumStreams,
} Stream;
volatile bool gProcessingEnabled;
static void streamComplete(Stream stream, uint8_t end);
enum { kDoubleBufferSize = 128 };
#ifdef DAC_USE_DMA
enum { kDacNumChannels = 2 };
static uint16_t gDacOutputs[kDacNumChannels][kDoubleBufferSize];
static void dacCb(unsigned int channel, unsigned int end)
{
#ifdef TOGGLE_DEBUG_PINS
HAL_GPIO_WritePin(DEBUG3_GPIO_Port, DEBUG3_Pin, GPIO_PIN_SET);
if(0 == end)
HAL_GPIO_WritePin(DEBUG3_GPIO_Port, DEBUG3_Pin, GPIO_PIN_RESET);
#endif // TOGGLE_DEBUG_PINS
Stream stream = channel ? kDAC1 : kDAC0;
streamComplete(stream, end);
#ifdef TOGGLE_DEBUG_PINS
if(1 == end)
HAL_GPIO_WritePin(DEBUG3_GPIO_Port, DEBUG3_Pin, GPIO_PIN_RESET);
#endif // TOGGLE_DEBUG_PINS
}
void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
{
dacCb(0, 0);
}
void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
{
dacCb(0, 1);
}
void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac)
{
dacCb(1, 0);
}
void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
{
dacCb(1, 1);
}
static void dacError(unsigned int dac, unsigned int dma)
{
fprintf(stderr, "DAC error: %d %d\n\r", dac, dma);
}
void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
{
dacError(0, 0);
}
void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
{
dacError(0, 1);
}
void HAL_DAC_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
{
dacError(1, 0);
}
void HAL_DAC_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
{
dacError(1, 1);
}
#endif // DAC_USE_DMA
#ifdef ADC_USE_DMA
uint16_t gAdcInputs[kDoubleBufferSize];
static void adcCb(unsigned int end)
{
#ifdef TOGGLE_DEBUG_PINS
HAL_GPIO_WritePin(DEBUG1_GPIO_Port, DEBUG1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(DEBUG1_GPIO_Port, DEBUG1_Pin, GPIO_PIN_RESET);
#endif // TOGGLE_DEBUG_PINS
streamComplete(kADC, end);
}
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
{
adcCb(0);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
adcCb(1);
}
#endif // ADC_USE_DMA
#ifdef GPIO_OUT_USE_DMA
static uint32_t gGpioOut[kDoubleBufferSize];
GPIO_TypeDef* gGpioHighRateOutBank = GPIOB;
const unsigned int kGpioHighRateOutTimerChannel = gpioHtimChannelOut;
static const uint16_t kGpioOutMask = (DEBUG2_Pin | DEBUG3_Pin | SW_LED_A_Pin | SW_LED_B_Pin); // what GPIO bits to actually write to
static void digitalWriteInit(uint8_t end)
{
unsigned int off = end ? kDoubleBufferSize / 2 : 0;
memset(gGpioOut + off, 0, sizeof(gGpioOut) / 2);
}
static void digitalWriteLowLevel(uint8_t end, unsigned int frame, unsigned int channel, uint8_t val)
{
unsigned int off = end ? kDoubleBufferSize / 2 : 0;
// the word will be written to GPIOx_BSRR Bit Set Reset register
// if(val), set bit in the lower half-word(SET), else in the upper half-word(RESET)
gGpioOut[frame + off] |= 1 << (channel + (!val) * 16) & (kGpioOutMask | (kGpioOutMask << 16));
}
static void gpioHighRateOutDone(uint8_t end)
{
#ifdef TOGGLE_DEBUG_PINS
HAL_GPIO_WritePin(DEBUG2_GPIO_Port, DEBUG2_Pin, GPIO_PIN_SET);
#endif // TOGGLE_DEBUG_PINS
digitalWriteInit(end);
streamComplete(kGpioOut, end);
#ifdef TOGGLE_DEBUG_PINS
HAL_GPIO_WritePin(DEBUG2_GPIO_Port, DEBUG2_Pin, GPIO_PIN_RESET);
#endif // TOGGLE_DEBUG_PINS
}
static void gpioHighRateOutCpltCb(DMA_HandleTypeDef* hdma)
{
gpioHighRateOutDone(1);
}
static void gpioHighRateOutHalfCpltCb(DMA_HandleTypeDef* hdma)
{
gpioHighRateOutDone(0);
}
static void gpioErrorCb(DMA_HandleTypeDef* hdma)
{
fprintf(stderr, "GPIO error\n\r");
}
#endif // GPIO_OUT_USE_DMA
#ifdef GPIO_IN_USE_DMA
static uint16_t gGpioIn[kDoubleBufferSize];
const GPIO_TypeDef* gGpioHighRateInBank = GPIOB;
const unsigned int kGpioHighRateInTimerChannel = gpioHtimChannelIn;
static inline int digitalReadLowLevel(uint8_t end, unsigned int frame, unsigned int channel)
{
unsigned int off = end ? kDoubleBufferSize / 2 : 0;
uint16_t val = gGpioIn[frame + off] & (1 << (channel));
// make it 1 or 0
return !(!val);
}
static void gpioHighRateInDone(uint8_t end)
{
#ifdef TOGGLE_DEBUG_PINS
// HAL_GPIO_WritePin(DEBUG3_GPIO_Port, DEBUG3_Pin, 1);
#endif // TOGGLE_DEBUG_PINS
streamComplete(kGpioIn, end);
// printf("%x \n\r", digitalReadLowLevel(end, 0, 4));
#ifdef TOGGLE_DEBUG_PINS
// HAL_GPIO_WritePin(DEBUG3_GPIO_Port, DEBUG3_Pin, 0);
#endif // TOGGLE_DEBUG_PINS
}
static void gpioHighRateInCpltCb(DMA_HandleTypeDef* hdma)
{
gpioHighRateInDone(1);
}
static void gpioHighRateInHalfCpltCb(DMA_HandleTypeDef* hdma)
{
gpioHighRateInDone(0);
}
#endif // GPIO_IN_USE_DMA
#if defined(GPIO_OUT_USE_DMA) || defined(GPIO_IN_USE_DMA)
static uint32_t getTimChannel(unsigned int channel)
{
uint32_t vals[] = {TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4, TIM_CHANNEL_5, TIM_CHANNEL_6, TIM_CHANNEL_ALL};
return vals[channel];
}
static uint32_t getTimDmaId(unsigned int channel)
{
uint32_t vals[] = {TIM_DMA_ID_CC1, TIM_DMA_ID_CC2, TIM_DMA_ID_CC3, TIM_DMA_ID_CC4};
return vals[channel - 1];
}
static uint32_t getTimDmaRequestEnableBit(unsigned int channel)
{
uint32_t vals[] = {TIM_DMA_CC1, TIM_DMA_CC2, TIM_DMA_CC3, TIM_DMA_CC4};
return vals[channel - 1];
}
static int gpioHighRateInitDma(unsigned int timerChannel, const volatile uint32_t * source, volatile uint32_t* dest,
void (*halfCplt)(DMA_HandleTypeDef*), void (*cplt)(DMA_HandleTypeDef*))
{
uint32_t TIM_DMA_ID_x = getTimDmaId(timerChannel);
gpioHtim->hdma[TIM_DMA_ID_x]->XferHalfCpltCallback = halfCplt;
gpioHtim->hdma[TIM_DMA_ID_x]->XferCpltCallback = cplt;
/* Set the DMA error callback */
gpioHtim->hdma[TIM_DMA_ID_x]->XferErrorCallback = gpioErrorCb;
/* Enable the DMA channel */
if (HAL_DMA_Start_IT(gpioHtim->hdma[TIM_DMA_ID_x], (uint32_t)source, (uint32_t)dest, kDoubleBufferSize) != HAL_OK)
{
return -1;
}
/* Enable the TIM Capture/Compare DMA request */
uint32_t TIM_DMA_x = getTimDmaRequestEnableBit(timerChannel);
__HAL_TIM_ENABLE_DMA(gpioHtim, TIM_DMA_x);
return 0;
}
static int gpioHighRateInit()
{
if(gpioHighRateInitDma(kGpioHighRateOutTimerChannel, gGpioOut, &gGpioHighRateOutBank->BSRR, gpioHighRateOutHalfCpltCb, gpioHighRateOutCpltCb))
return -1;
if(gpioHighRateInitDma(kGpioHighRateInTimerChannel, &gGpioHighRateInBank->IDR, (uint32_t*)gGpioIn, gpioHighRateInHalfCpltCb, gpioHighRateInCpltCb))
return -1;
return 0;
}
static int gpioHighRateStart()
{
// finally, enable the timer without DMA (DMA initialised above)
// TODO: there is a bit of overhead in HAL_TIM_PWM_Start and they are actually both enabling the clock in EN1
// Could be refactored to make it start earlier, or even better look at other ways of starting it.
#ifdef GPIO_OUT_USE_DMA
if(HAL_TIM_PWM_Start(gpioHtim, getTimChannel(kGpioHighRateOutTimerChannel)))
return -1;
#endif // GPIO_OUT_USE_DMA
#ifdef GPIO_IN_USE_DMA
if(HAL_TIM_PWM_Start(gpioHtim, getTimChannel(kGpioHighRateInTimerChannel)))
return -1;
#endif // GPIO_IN_USE_DMA
return 0;
}
#endif // defined(GPIO_OUT_USE_DMA) || defined (GPIO_IN_USE_DMA)
static void processingCallback(uint8_t end);
#if defined(DAC_USE_DMA) || defined(ADC_USE_DMA) || defined(GPIO_OUT_USE_DMA) || defined(GPIO_IN_USE_DMA)
static void streamComplete(Stream stream, uint8_t end)
{
if(!gProcessingEnabled)
return;
static uint8_t streamStates[kNumStreams];
streamStates[stream] = end;
uint8_t ready = 1;
for(unsigned int n = 1; n < kNumStreams; ++n)
{
if(streamStates[n] != streamStates[0])
{
ready = 0;
break;
}
}
if(ready)
processingCallback(end);
}
static void processingCallback(uint8_t end)
{
#ifdef TOGGLE_DEBUG_PINS
HAL_GPIO_WritePin(DEBUG0_GPIO_Port, DEBUG0_Pin, GPIO_PIN_SET);
#endif // TOGGLE_DEBUG_PINS
#ifdef I2C_MANUAL
static unsigned int counter = 0;
if((counter++ % I2C_MANUAL) == 0)
startScanning();
#endif
enum { frames = kDoubleBufferSize / 2 };
static float analogIn[frames];
static float analogOut[frames * kDacNumChannels];
static uint32_t digital[frames];
static BelaContext ctx =
{
.analogIn = analogIn,
.analogOut = analogOut,
.analogInChannels = 1,
.analogOutChannels = kDacNumChannels,
.analogFrames = frames,
.analogSampleRate = SAMPLE_RATE,
.digital = digital,
.digitalChannels = 16,
.digitalFrames = frames,
.digitalSampleRate = SAMPLE_RATE,
.audioFramesElapsed = 0,
};
size_t dmaBufferOffset = end * kDoubleBufferSize / 2;
#ifdef GPIO_IN_USE_DMA
for(size_t n = 0; n < ctx.digitalFrames; ++n)
{
// lower word is direction, upper word is input value
// For the pins set as an input, we set the upper word according to the GPIO status.
uint32_t inputChannels = digital[n] & 0xffff;
digital[n] = ((gGpioIn[dmaBufferOffset + n] & inputChannels) << 16) | inputChannels;
}
#endif // GPIO_IN_USE_DMA
#ifdef ADC_USE_DMA
assert(1 == ctx.analogInChannels); // loop below assumes 1 channel
for(size_t n = 0; n < ctx.analogFrames; ++n)
ctx.analogIn[n] = 1.f - gAdcInputs[dmaBufferOffset + n] / (4096.f * 8.f); // invert input because of inverting opamp
#endif // ADC_USE_DMA
*((size_t*)&(ctx.audioFramesElapsed)) += ctx.analogFrames;
render(&ctx, nullptr);
const float dacMax = 4095.f / 4096.f;
const float dacMin = 0;
#ifdef DAC_USE_DMA
for(size_t n = 0; n < ctx.analogFrames; ++n)
{
for(size_t c = 0; c < ctx.analogOutChannels; ++c)
{
float val = ctx.analogOut[n * ctx.analogOutChannels + c];
val = val > dacMax ? dacMax : val;
val = val < dacMin ? dacMin : val;
gDacOutputs[c][dmaBufferOffset + n] = val * 4096.f;
}
}
#endif // DAC_USE_DMA
#ifdef GPIO_OUT_USE_DMA
for(size_t n = 0; n < ctx.digitalFrames; ++n)
{
uint32_t d = digital[n];
uint16_t outputChannels = ~(d & 0xffff) & kGpioOutMask; // lower word is direction: output channels are set to 0
uint16_t setValues = outputChannels & (d >> 16); // upper word is output value
uint16_t resetValues = outputChannels & (~(d >> 16));
// the word will be written to GPIOx_BSRR Bit Set Reset register
// if(val), set bit in the lower half-word(SET), else in the upper half-word(RESET)
gGpioOut[dmaBufferOffset + n] = ((resetValues << 16) | setValues);
}
#endif // GPIO_OUT_USE_DMA
#ifdef TOGGLE_DEBUG_PINS
HAL_GPIO_WritePin(DEBUG0_GPIO_Port, DEBUG0_Pin, GPIO_PIN_RESET);
#endif // TOGGLE_DEBUG_PINS
}
void render(BelaContext *context, void *userData)
{
#ifdef TRILL_RACK_INTERFACE
tr_process(context);
#endif // TRILL_RACK_INTERFACE
#if 0
{ // output a clock on kTestChannel
enum { kOnes = 5, kZeros = 1};
enum { kTestChannel = 6 };
for(unsigned int n = 0; n < kOnes; ++n)
digitalWrite(context, n, kTestChannel, 1);
for(unsigned int n = kOnes; n < kOnes + kZeros; ++n)
digitalWrite(context, n, kTestChannel, 0);
const int kNumBits = context->digitalFrames - kOnes - kZeros;
static int count = 0;
for(int n = 0; n < kNumBits; ++n)
{
unsigned int frame = n + kOnes + kZeros;
digitalWrite(context, frame, kTestChannel, (count >> n) & 1);
}
++count;
if(count >= (1 << kNumBits))
count = 0;
}
#endif
#if 0
{
// set DACx to echo ADC
unsigned int outChannel = 1;
for(unsigned int n = 0; n < context->analogFrames; ++n)
{
analogWriteOnce(context, n, outChannel, analogRead(context, n, 0));
}
}
#endif
#if 0
// generate sawtooths
for(unsigned int n = 0; n < context->analogFrames; ++n)
{
static unsigned int count = 0;
float out = count / 4096.f;
for(unsigned int c = 0; c < context->analogOutChannels; ++c)
analogWriteOnce(context, n, c, out);
count++;
if(count == 4096)
count = 0;
}
#endif
#if 0
{
//set DAC to output a sinewave
int channel = 0;
static float amp = 0.99;
static float ampSig = 1;
static float phase = 0;
const float kAmpMax = 0.1;
const float kAmpMin = 0;
for(unsigned int n = 0; n < context->analogFrames; ++n)
{
phase += 2.f * (float)M_PI * 400.f / context->analogSampleRate;
if(phase > M_PI)
phase -= 2.f * (float)M_PI;
static int count = 0;
count += 512;
float out = count / 4096.f;
if(count >= 4096)
count = 0; // hard sync
// float out = sinf(phase) * amp;
// amp += ampSig * 0.1f / context->analogSampleRate;
// if(amp > kAmpMax) {
// ampSig = -1;
// amp = kAmpMax;
// }
// if(amp < kAmpMin) {
// ampSig = 1;
// amp = 0;
// }
analogWriteOnce(context, n, channel, out * 0.5f + 0.5f);
}
}
#endif
#if 0
for(size_t n = 0; n < context->digitalFrames; ++n)
{
context->digital[n] = (0xffff << 16) * (n & 1);
}
#endif
}
#endif // any DMA
#ifdef NEOPIXEL_USE_TIM
#define NEOPIXEL_USE_CLASS
#include <stdlib.h> // atoi below
#ifdef NEOPIXEL_USE_CLASS
extern "C" {
int npSetup(void);
ssize_t npSend(const uint8_t* rgb, size_t length);
void npSendColorToAll(uint32_t val);
void npDone();
uint8_t npReady();
};
#else // NEOPIXEL_USE_CLASS
volatile uint8_t gNpBusyFlag = 0;
void WS2812_Send (uint32_t color)
{
enum { kNumPixels = 16};
enum { kNumBitsPerPixel = 24 };
enum { kNumBits = kNumPixels * kNumBitsPerPixel };
enum { kNumData = kNumBits + 2 }; // one leading and one trailing 0-valued
enum { kDataStart = 1 }; // leading 0 byte
static uint32_t pwmData[kNumData]; // this should be uint16_t if using a timer other than TIM2 and the DMA settings should be changed to half-word in that case, too
// see https://controllerstech.com/pwm-with-dma-in-stm32/, but adapted for TIM2 (32 bit CCR).
if(gNpBusyFlag)
return;
gNpBusyFlag = 1;
for(unsigned int n = 0; n < kDataStart; ++n)
pwmData[n] = 0;
for(unsigned int n = 0; n < kNumPixels; ++n)
{
for (int i = kNumBitsPerPixel; i >= 0; --i)
{
unsigned int idx = kNumBitsPerPixel * n + i + kDataStart;
if (color & (1 << ((7 - (i % 8)) + ((i / 8) * 8) ))) // don't ask
pwmData[idx] = 66;
else
pwmData[idx] = 33;
}
}
pwmData[kNumData - 1] = 0; // ensure the last PWM value is 0, so if the DMA callback arrives late, we have stopped sending out stuff already.
HAL_Delay(1); // min delay between repetitions
neoPixelHtim.Instance->CCR2 = 0;
HAL_TIM_PWM_Start_DMA(&neoPixelHtim, neoPixelHtim_TIM_CHANNEL_x, (uint32_t *)pwmData, kNumData);
}
#endif // NEOPIXEL_USE_CLASS
void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
{
#ifdef TRILL_RACK_INTERFACE
tr_snpDone();
#else // TRILL_RACK_INTERFACE
#ifdef NEOPIXEL_USE_CLASS
npDone();
#else // NEOPIXEL_USE_CLASS
gNpBusyFlag = 0;
#endif // NEOPIXEL_USE_CLASS
#endif // TRILL_RACK_INTERFACE
fprintf(stderr, "TIM error %p\n", htim);
}
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
if(&neoPixelHtim == htim)
{
#ifdef TRILL_RACK_INTERFACE
tr_snpDone();
#else // TRILL_RACK_INTERFACE
#ifdef NEOPIXEL_USE_CLASS
npDone();
#else // NEOPIXEL_USE_CLASS
HAL_TIM_PWM_Stop_DMA(htim, neoPixelHtim_TIM_CHANNEL_x);
gNpBusyFlag = 0;
#endif // NEOPIXEL_USE_CLASS
#endif // TRILL_RACK_INTERFACE
}
}
#endif // NEOPIXEL_USE_TIM
#if __has_include("psoc-programmer-stm32/issp.h")
#include "psoc-programmer-stm32/issp.h"
extern uint8_t trill_program_start;
extern uint8_t trill_program_end;
#define DO_PSOC_PROGRAMMING
#endif // has_include
#ifdef DO_PSOC_PROGRAMMING
int programTrillHelper(uint8_t* program, size_t size){
uint8_t security[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int result = 0;
uint16_t checksumRead, checksumWrite;
// int ISSP_setup(void* sdataBank, uint16_t sdataPin, void* sclkBank, uint16_t sclkPin, void* pwrBank, uint16_t pwrPin, void* tpBank, uint16_t tpPin)
struct issp_init init = { 0 };
init.sdataPort = GPIOA;
init.sdataPin = GPIO_PIN_8;
init.sclkPort = GPIOA;
init.sclkPin = GPIO_PIN_9;
init.pwrPort = TRILL_3V3_GPIO_Port;
init.pwrPin = TRILL_3V3_Pin;
init.sclk_delay = 3;
result = ISSP_setup(&init);
printf("Setup returned %d\n\r", result);
if(result)
return 1;
result = ISSP_verify(program, size);
if(!result) {
printf("verify returned 0. No need to reflash\n\r");
return 0;
}
printf("verify returned %d. Reflashing\n\r", result);
result = ISSP_erase();
printf("Erase returned %d\n\r", result);
if(result)
return 2;
result = ISSP_program(program, size, &checksumRead);
printf("Program returned %d\n\r", result);
printf("Checksum %d\n\r", checksumRead);
if(result)
return 3;
result = ISSP_verify(program, size);
printf("Verify returned %d\n\r", result);
if(result)
return 4;
result = ISSP_security(security, 16, &checksumWrite);
printf("Security returned %d\n\r", result);
printf("Checksum %d\n\r", checksumWrite);
if(result)
return 5;
result = ISSP_restart(0);
if(result)
return 6;
return checksumRead == checksumWrite ? 0 : 7;
}
#endif // DO_PSOC_PROGRAMMING
int startScanning()
{
if(tr_scanRequested()) {
if(!gIsScanning) {
int ret = HAL_I2C_Master_Receive_DMA(&trillHi2c, gI2cAddress, gI2cDmaRecv, gI2cDmaRecvSize);
if(HAL_OK != ret)
{
fprintf(stderr, "I2C_Master_Receive_DMA failed: %d\n", ret);
gIsScanning = false;
return 1;
}
gIsScanning = true;
}
}
return 0;
}
// call this as soon as possible to try and counteract the case where
// a PSU misbehaves because the LEDs are drawing too much current
void TrillRackApplication_earlyInit()
{
tr_clearLeds();
}
static void setDacTo0V()
{
// don't call this before the final MX_DACx_Init() call has been made
// (i.e.: cannot go in _earlyInit())
gProcessingEnabled = false;
for(size_t c = 0; c < kDacNumChannels; ++c)
{
for(size_t n = 0; n < kDoubleBufferSize; ++n)
gDacOutputs[c][n] = 2744; // write approx 0V
}
HAL_DAC_Start_DMA(&dac0Handle, dac0Channel, (uint32_t*)gDacOutputs[0], kDoubleBufferSize, DAC_ALIGN_12B_R);
HAL_DAC_Start_DMA(&dac1Handle, dac1Channel, (uint32_t*)gDacOutputs[1], kDoubleBufferSize, DAC_ALIGN_12B_R);
HAL_TIM_Base_Start(&dacAdcHtim);
for(volatile unsigned int i = 0; i < 100 * 1000; ++i)
; // waste some time, while some samples are written
HAL_TIM_Base_Stop(&dacAdcHtim);
}
int TrillRackApplication()
{
setDacTo0V();
HAL_Delay(10); // wait for any callbacks to complete
printf("Booted\n\r");
#ifdef I2C_ON_EVT
// do not start reading I2C on EVT yet.
#endif // I2C_ON_EVT
HAL_NVIC_DisableIRQ(EXTI3_IRQn);
// Calibrate The ADC On Power-Up For Better Accuracy
HAL_ADCEx_Calibration_Start(&adcHandle, ADC_SINGLE_ENDED);
#ifdef DO_PSOC_PROGRAMMING
// set pins in programming mode
HAL_GPIO_WritePin(TRILL_3V3_GPIO_Port, TRILL_3V3_Pin, GPIO_PIN_SET);
i2cPinsMode(kI2cPinsModeProgramming);
uint8_t* program = &trill_program_start;
size_t programSize = &trill_program_end - &trill_program_start;
// program Trill
programTrillHelper(program, programSize);
#endif // DO_PSOC_PROGRAMMING
// set pins in I2C mode
i2cPinsMode(kI2cPinsModeI2c);
// powercycle the Trill
psocPower(false);
HAL_Delay(200);
psocPower(true);
HAL_Delay(250); // wait for device to be receptive to I2C transactions
#ifdef TRILL_RACK_INTERFACE
int ret = tr_setup();
printf("tr_setup: %x\n\r", ret);
// if the PSoC doesn't respond, or in case of other error, we'd get ret <= 0
// however, we ignore that and keep running, so that the regular behaviour
// continues. This allows, e.g.: the factory test mode to display the regular
// error messages and allow an operator to validate errors.
gI2cAddress = ret << 1;
#else // TRILL_RACK_INTERFACE
#ifdef NEOPIXEL_USE_CLASS
npSetup();
#endif // NEOPIXEL_USE_CLASS
#ifdef TRILL_USE_CLASS
int ret;
if((ret = trillSetup()))
{
fprintf(stderr, "Error setting up Trill: %d\n", ret);
return 1;
}
#else // TRILL_USE_CLASS
uint8_t identifyBuf[] = {kOffsetCommand, kCommandIdentify};
int ret = HAL_I2C_Master_Transmit(&trillHi2c, gI2cAddress, identifyBuf, sizeof(identifyBuf), kTimeout);
if(HAL_OK != ret) {
fprintf(stderr, "Error: send identify command\n\r");
return 1;
}
HAL_Delay(10);
uint8_t receiveBuffer[4];
ret = HAL_I2C_Master_Receive(&trillHi2c, gI2cAddress, receiveBuffer, sizeof(receiveBuffer), kTimeout);
if(HAL_OK != ret) {
fprintf(stderr, "Error: receive identify command\n\r");
return 1;
}
printf("identify: %#4x %#4x %#4x %#4x\n\r", receiveBuffer[0], receiveBuffer[1], receiveBuffer[2], receiveBuffer[3]);
HAL_Delay(10);
uint8_t diffBuf[] = {kOffsetCommand, kCommandMode, kModeCentroid};
ret = HAL_I2C_Master_Transmit(&trillHi2c, gI2cAddress, diffBuf, sizeof(diffBuf), kTimeout);
if(HAL_OK != ret) {
fprintf(stderr, "Error: send mode command\n\r");
return 1;
}
// update baseline
uint8_t updateBaselineBuffer[] = {kOffsetCommand, kCommandBaselineUpdate};
ret = HAL_I2C_Master_Transmit(&trillHi2c, gI2cAddress, updateBaselineBuffer, sizeof(updateBaselineBuffer), kTimeout);
if(HAL_OK != ret) {
fprintf(stderr, "Error: send update baseline command\n\r");
return 1;
}
// prepare to read data
uint8_t transmitBuffer[] = {kOffsetData};
ret = HAL_I2C_Master_Transmit(&trillHi2c, gI2cAddress, transmitBuffer, sizeof(transmitBuffer), kTimeout);
if(HAL_OK != ret) {
fprintf(stderr, "Error: prepare to read command\n\r");
return 1;
}
#endif// TRILL_USE_CLASS
#endif // TRILL_RACK_INTERFACE
#ifdef I2C_USE_DMA
#ifdef I2C_ON_EVT
// Start reading I2C on evt
gI2cOnEvtEnabled = true;
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
#else // I2C_ON_EVT
#ifndef I2C_MANUAL
ret = startScanning();
if(ret)
return 1;
#endif // !I2C_MANUAL
#endif // I2C_ON_EVT
#endif // I2C_USE_DMA
#ifdef DAC_USE_DMA
// stop DMA as it may have been started in setDacTo0V()
HAL_DAC_Stop_DMA(&dac0Handle, dac0Channel);
ret = HAL_DAC_Start_DMA(&dac0Handle, dac0Channel, (uint32_t*)gDacOutputs[0], kDoubleBufferSize, DAC_ALIGN_12B_R);
if(HAL_OK != ret)
{
fprintf(stderr, "DAC_Start_DMA 0 failed: %d\n", ret);
return 1;
}
HAL_DAC_Stop_DMA(&dac1Handle, dac1Channel);
ret = HAL_DAC_Start_DMA(&dac1Handle, dac1Channel, (uint32_t*)gDacOutputs[1], kDoubleBufferSize, DAC_ALIGN_12B_R);
if(HAL_OK != ret)
{
fprintf(stderr, "DAC_Start_DMA 1 failed: %d\n", ret);
return 1;
}
#else // DAC_USE_DMA
ret = HAL_DAC_Start(&dac0Handle, dac0Channel);
if(HAL_OK != ret)
{
fprintf(stderr, "Error: HAL_DAC_Start()\n\r");
return 1;
}
ret = HAL_DAC_Start(&dac1Handle, dac1Channel);
if(HAL_OK != ret)
{
fprintf(stderr, "Error: HAL_DAC_Start()\n\r");
return 1;
}
#endif // DAC_USE_DMA
#ifdef ADC_USE_DMA
ret = HAL_ADC_Start_DMA(&adcHandle, (uint32_t*)gAdcInputs, kDoubleBufferSize);
if(HAL_OK != ret)
{
fprintf(stderr, "ADC_Start_DMA failed: %d\n", ret);
return 1;
}
#else // ADC_USE_DMA
HAL_ADC_Start(&adcHandle);
#endif// ADC_USE_DMA
#if defined(GPIO_OUT_USE_DMA) || defined(GPIO_IN_USE_DMA)
if(gpioHighRateInit())
{
fprintf(stderr, "gpioHighRateInit failed\n");
return 1;
}
#endif // defined(GPIO_OUT_USE_DMA) || defined(GPIO_IN_USE_DMA)
#if defined(GPIO_OUT_USE_DMA) || defined(GPIO_IN_USE_DMA)
if(gpioHighRateStart())
{
fprintf(stderr, "gpioHighRateStart failed\n");
return 1;
}
#endif // defined(GPIO_OUT_USE_DMA) || defined(GPIO_IN_USE_DMA)
#if defined(DAC_USE_DMA) || defined (ADC_USE_DMA)
gProcessingEnabled = true; // tell the callbacks to do their job
ret = HAL_TIM_Base_Start(&dacAdcHtim);
if(HAL_OK != ret)
{
fprintf(stderr, "TIM_Base_Start for DAC/ADC failed: %d\n", ret);
return 1;
}
#endif // DAC_USE_DMA || ADC_USE_DMA
while (1)
{
#ifdef TRILL_RACK_INTERFACE
tr_mainLoop();
#if !(defined(I2C_ON_EVT) || defined(I2C_MANUAL))
startScanning();
#endif // I2C_ON_EVT
// not much to do here ...
#else // TRILL_RACK_INTERFACE
#ifdef NEOPIXEL_USE_TIM
#ifdef NEOPIXEL_USE_CLASS
if(npReady())
#else // NEOPIXEL_USE_CLASS
if(!gNpBusyFlag)
#endif // NEOPIXEL_USE_CLASS
{
// updated neopixels
#if 1 // interactive LEDs
static int val = 0;
char data[2] = {0};
HAL_StatusTypeDef status = HAL_UART_Receive(&dbgHuart, (uint8_t*)data, 1, 0);
if(HAL_OK == status)
{
printf("%s", data);
char mybuf[10];
static int i = 0;
if(data[0] == '\n' || data[0] == '\r' || i >= sizeof(mybuf) - 1)
{
mybuf[i] = 0;
val = atoi(mybuf);
printf("Sending %d\n\r", val);
i = 0;
#ifdef NEOPIXEL_USE_CLASS
npSendColorToAll(val);
#else // NEOPIXEL_USE_CLASS
WS2812_Send(val);
#endif // NEOPIXEL_USE_CLASS
} else {
mybuf[i] = data[0];
++i;
}
}
#else
static uint8_t count = 1;
static uint8_t shift = 0;
static uint8_t inc = 1;
#ifdef NEOPIXEL_USE_CLASS
uint32_t val = count << shift;
npSendColorToAll(val);
#else // NEOPIXEL_USE_CLASS
WS2812_Send((count << shift));
#endif// NEOPIXEL_USE_CLASS
if(count == 255)
inc = 0;
if(count == 0)
{
inc = 1;
shift += 8;
}
if(shift >= 24)
shift = 0;
if(inc)
++count;
else
--count;
#endif
}
#endif // NEOPIXEL_USE_TIM
#ifndef TRILL_USE_CLASS
uint8_t receiveBuffer[kNumTouches * 2 * 2];
#endif // TRILL_USE_CLASS
#ifdef I2C_USE_DMA
// TODO: does the below make sense?
memcpy(receiveBuffer, gI2cLatestRecv, sizeof(receiveBuffer));
#else // I2C_USE_DMA
#ifdef TRILL_USE_CLASS
ret = trillRead();
#else // TRILL_USE_CLASS
ret = HAL_I2C_Master_Receive(&trillHi2c, gI2cAddress, receiveBuffer, sizeof(receiveBuffer), kTimeout);
#endif // TRILL_USE_CLASS
if(HAL_OK != ret) {
fprintf(stderr, "Error: blocking receive\n\r");
return 1;
}
#endif // I2C_USE_DMA