-
Notifications
You must be signed in to change notification settings - Fork 2
/
peg.c
executable file
·2371 lines (1813 loc) · 57.6 KB
/
peg.c
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
/*
Copyright (c) 2015 4ms Company
Author: Dan Green - danngreen1@gmail.com
LICENSE:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
See http://creativecommons.org/licenses/MIT/ for more information.
*/
/*
For use with PEG PCB v2.0
(see peg_v1 project in github for PEG PCB 1.0.x)
*/
/*****************
* GLOBAL MODES *
*****************/
//Enable to save code space by skipping the log lookup table. Useful for compiling with no optimization when using the debugger.
//#define OMITLOGCURVES
//Enables the UART, for communication with a PEG parameter controller
#define ENABLE_UART
#define F_CPU 16000000
/*****************
* INCLUDES *
*****************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include "uart.h"
#include "timer.h"
#include "dac_mcp4921.h"
#ifndef OMITLOGCURVES
#include "log4096.h"
#endif
FUSES =
{
.extended = 0x04, /*BOD 4.3V*/
.high = 0xd9, /*d1 to preserve EEPROM*/
.low = 0xd6 /*or 0xce*/
};
/*************************
* GLOBAL VARIABLES *
*************************/
//udiv32() is located in the div32.S file
extern uint32_t udiv32(uint32_t divisor);
extern volatile uint32_t tapintmr;
extern volatile uint32_t pingtmr;
extern volatile uint32_t divpingtmr;
extern volatile uint32_t eo1tmr;
extern volatile uint32_t eo2tmr;
volatile uint32_t ping_irq_timestamp=0, trigq_irq_timestamp=0;
volatile char reset_nextping_flag=0;
volatile char sync_to_ping_mode=1;
volatile uint8_t trigq_jack_down=0;
volatile char using_tap_clock=0;
volatile char timer_overflowed=0;
char eo1_high=0,eo2_high=0;
/***************
* SETTINGS *
***************/
//3000000 is about 1.25s
#define HOLDTIMECLEAR 4800000
#define SKEW_ADC_DRIFT 1
#define USER_INPUT_POLL_TIME 400
//2400 is 1ms
//12000 is 5ms
#define LIMIT_SKEW_TIME 15000
//10 is about 100ms
#define NUM_ADC_CYCLES_BEFORE_TRANSITION 10
#define DIV_ADC_HYSTERESIS 1
//SYSTEM_MODE_HOLD_TIME: how long the ping button must be held down to enter System Mode
//with HOLDTIMECLEAR at 4800000:
//200000 is about 5s
//150000 is about 3.75s
//103000 is about 2.5
#define SYSTEM_MODE_HOLD_TIME 130000
#define SYSTEM_MODE_EXIT_TIME 0x00080000
#define SYSTEM_MODE_CYCLE_FLASH_BRIGHT_ON 15000
#define SYSTEM_MODE_CYCLE_FLASH_BRIGHT_OFF 25000
#define SYSTEM_MODE_CYCLE_FLASH_DIM_ON 5000
#define SYSTEM_MODE_CYCLE_FLASH_DIM_OFF 50000
//119 is 13.15ms
//91 is 10.0ms
//46 is 5ms
//37 is 4ms
//9 is 1.1ms
//4 is 500us
//1 is 212us
#define EO_TRIG_TIME 91
#define EO_GATE_TIME 37
/*******************
* COMM *
*******************/
//OR these with BLUE_DETECT
#define CMD_WRITE 0b11111100
#define CMD_ARE_YOU_HERE 0b11110000
#define CMD_ARE_YOU_HERE_PING 0b11111000
#define CMD_HOW_ARE_YOU 0b11110100
#define REPLY_STATE 0b11000010
#define REPLY_HI_MY_NAME_IS 0b11001100
#define REPLY_HI_I_AM_PINGING 0b11000100
/*******************
* EEPROM *
*******************/
#define EEPROM_SET 0b10101010
#define EEPROM_CLEAR 0b00000000
#define EEPROM_UNPROGRAMMED 0b11111111
#define TAPCLK_EEPROMADDR 16
#define PING_EEPROMADDR 17
#define LIMIT_SKEW_EEPROMADDR 18
#define HALFRISE_EEPROMADDR 19
#define EO1_TRIG_EEPROMADDR 21
#define ASYNC_SUSTAIN_EEPROMADDR 20
#define EO2_TRIG_EEPROMADDR 22
char EOF_IS_TAPCLKOUT=0;
char NO_FREERUNNING_PING=0;
char EOR_IS_HALFRISE=0;
char EO1_IS_TRIG=0;
char EO2_IS_TRIG=0;
char LIMIT_SKEW=0;
char ROLLOFF_PING=1;
char ASYNC_CAN_SUSTAIN=1;
//The following are not user modifiable, change at your own risk!
#define QNT_REPHASES_WHEN_CYCLE_OFF 0
#define CYCLE_REPHASES_DIV_PING 1
/************************
* Mnemonics *
************************/
#define WAIT 0
#define RISE 1
#define SUSTAIN 2
#define FALL 3
#define TRANSITION 4
#define EXP 0
#define EXP25 1
#define EXP50 2
#define EXP75 3
#define LIN 4
#define LIN25 5
#define LIN50 6
#define LIN75 7
#define LOG 8
/*******************
* PIN DEFINITIONS *
*******************/
#define DEBUG_pin PC4
#define DEBUG_init
//#define DEBUG_init DDRC |= (1<<DEBUG_pin)
#define DEBUGFLIP PORTC ^= (1<<DEBUG_pin)
#define DEBUGHIGH PORTC |= (1<<DEBUG_pin)
#define DEBUGLOW PORTC &= ~(1<<DEBUG_pin)
#define DEBUG2_pin PD0
#define DEBUG2_init
//#define DEBUG2_init DDRD |= (1<<DEBUG2_pin)
#define DEBUG2FLIP PORTD ^= (1<<DEBUG2_pin)
#define DEBUG2HIGH PORTD |= (1<<DEBUG2_pin)
#define DEBUG2LOW PORTD &= ~(1<<DEBUG2_pin)
#define PINGLED_pin PB1
#define PINGLED_init DDRB |= (1<<PINGLED_pin)
#define PINGLEDFLIP PORTB ^= (1<<PINGLED_pin)
#define PINGLEDHIGH PORTB |= (1<<PINGLED_pin)
#define PINGLEDLOW PORTB &= ~(1<<PINGLED_pin)
#define PING_pin PD2
#define PING_init DDRD &= ~(1<<PING_pin); PORTD &= ~(1<<PING_pin)
#define PING (PIND & (1<<PING_pin))
#define TRIGA_pin PD4
#define TRIGA_init DDRD &= ~(1<<TRIGA_pin); PORTD &= ~(1<<TRIGA_pin)
#define TRIGA (PIND & (1<<TRIGA_pin))
#define TRIGQ_pin PD3
#define TRIGQ_init DDRD &= ~(1<<TRIGQ_pin); PORTD &= ~(1<<TRIGQ_pin)
#define TRIGQ (PIND & (1<<TRIGQ_pin))
#define CYCLE_BUT_pin PC3
#define CYCLE_BUT_init DDRC &= ~(1<<CYCLE_BUT_pin); PORTC |= (1<<CYCLE_BUT_pin)
#define CYCLE_BUT_RAW (!(PINC & (1<<CYCLE_BUT_pin)))
#define CYCLE_LED_pin PC4
#define CYCLE_LED_init DDRC |= (1<<CYCLE_LED_pin)
#define CYCLE_LED_ON PORTC |= (1<<CYCLE_LED_pin)
#define CYCLE_LED_OFF PORTC &= ~(1<<CYCLE_LED_pin)
#define CYCLE_INVERT_pin PC5
#define CYCLE_INVERT_init DDRC &= ~(1<<CYCLE_INVERT_pin); PORTC &= ~(1<<CYCLE_INVERT_pin)
#define CYCLE_INVERT (PINC & (1<<CYCLE_INVERT_pin))
#define CYCLE_BUT ((CYCLE_BUT_RAW && !CYCLE_INVERT) || (!CYCLE_BUT_RAW && CYCLE_INVERT))
#define EO1_pin PD6
#define EO2_pin PD5
#define EO1_OFF PORTD |= (1<<EO1_pin)
#define EO1_ON PORTD &= ~(1<<EO1_pin)
#define EO2_OFF PORTD |= (1<<EO2_pin)
#define EO2_ON PORTD &= ~(1<<EO2_pin)
#define EO_init DDRD |= (1<<EO1_pin) | (1<<EO2_pin)
#define CHANNEL_ID_OUT_pin PC0
#define CHANNEL_ID_OUT_init DDRC |= (1 << CHANNEL_ID_OUT_pin)
#define CHANNEL_ID_OUT_ON PORTC |= (1 << CHANNEL_ID_OUT_pin)
#define CHANNEL_ID_OUT_OFF PORTC &= ~(1 << CHANNEL_ID_OUT_pin)
#define TAPIN_pin PD7
#define TAPIN_init DDRD &= ~(1<<TAPIN_pin); \
PORTD |=(1<<TAPIN_pin)
#define TAPIN (!(PIND & (1<<TAPIN_pin)))
#define ADC_DDR DDRC
#define ADC_PORT PORTC
#define ADC_mask 0b00000111
#define NUM_ADC 3
#define CURVE_adc 6
#define CLKDIV_adc PC2
#define SKEW_adc PC1
#define SPI_PIN PINB
#define SPI_PORT PORTB
#define SPI_DDR DDRB
#define SPI_MOSI PB3
#define SPI_MISO PB4
#define SPI_SCLK PB5
#define BLUE_DETECT_PIN PB0
#define READ_BLUE_DETECT (PINB & (1<<BLUE_DETECT_PIN))
#define BLUE_DETECT_init DDRD &= ~(1<<BLUE_DETECT_PIN)
char BLUE_DETECT=0;
/**************
* FUNCTIONS *
**************/
uint8_t diff(uint8_t a, uint8_t b);
inline uint8_t diff(uint8_t a, uint8_t b){
if (a>b) return (a-b);
else return (b-a);
}
uint32_t diff32(uint32_t a, uint32_t b);
inline uint32_t diff32(uint32_t a, uint32_t b){
if (a>b) return (a-b);
else return(b-a);
}
void init_spi(void){
SPI_DDR |= (1<<SPI_MOSI) | (1<<SPI_SCLK); //SCK, MOSI are output
DAC_CS_DDR |= (1<<DAC_CS); //CS for MCP4922 DAC
DAC_CS_PORT |= (1<<DAC_CS); //pull CS high to intialize
SPI_DDR &= ~(1<<SPI_MISO); // MISO is input
SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1)| (0<<SPR0); //SPI enable, Master Mode, F_OSC/4, interrupt enabled
SPSR = (1<<SPI2X); //SPI double speed = 2MHz
output_dac(0);
}
void init_pins(void){
init_spi();
EO_init;
PING_init;
TRIGA_init;
TRIGQ_init;
CYCLE_BUT_init;
CYCLE_LED_init;
TAPIN_init;
BLUE_DETECT_init ;
CHANNEL_ID_OUT_init;
PINGLED_init;
CYCLE_INVERT_init;
DEBUG_init;
DEBUG2_init;
}
int8_t get_clk_div_nominal(uint8_t adc_val){
if (adc_val<=1) // /8 <=0..2 (3)
return(8);
else if (adc_val<=10) // /7 <= 3..15 (13)
return(7);
else if (adc_val<=26) // /6 <= 16..33 (18)
return(6);
else if (adc_val<=46) // /5 <= 34..53 (20)
return(5);
else if (adc_val<=65) // /4 <= 54..78 (27)
return(4);
else if (adc_val<=85) // /3 <= 79..97 (19)
return(3);
else if (adc_val<=100) // /2 <= 98..114 (19)
return(2);
else if (adc_val<=118) // =1 <= 117..134 (18)
return(1);
else if (adc_val<=128) // x2 <= 135..154 (20)
return(-2);
else if (adc_val<=150) // x3 <= 155..177 (23)
return(-3);
else if (adc_val<=170) // x4 was 196 <= 178..200 (23)
return(-4);
else if (adc_val<=188) // x5 <= 201..220 (20)
return(-5);
else if (adc_val<=207) // x6 was 246 <= 221..242 ()
return(-6);
else if (adc_val<=220) // x7 was 251 <= 243..253 ()
return(-7);
else /*if (adc_val<=255)*/ // x8 <=253..255 (3)
return(-8);
// else
// return(1);
}
uint32_t get_clk_div_time(int8_t clock_divide_amount, uint32_t clk_time){
if (clock_divide_amount==8) // /8
return(clk_time<<3);
else if (clock_divide_amount==7) // /7
return(clk_time*7);
else if (clock_divide_amount==6) // /6
return(clk_time*6);
else if (clock_divide_amount==5) // /5
return(clk_time*5);
else if (clock_divide_amount==4) // /4
return(clk_time<<2);
else if (clock_divide_amount==3) // /3
return(clk_time*3);
else if (clock_divide_amount==2) // /2
return(clk_time<<1);
else if (clock_divide_amount==1) // =1
return(clk_time);
else if (clock_divide_amount==-1) // =1
return(clk_time);
else if (clock_divide_amount==-2) // *2
return(clk_time>>1);
else if (clock_divide_amount==-3) // *3
return(clk_time/3);
else if (clock_divide_amount==-4) // *4
return(clk_time>>2);
else if (clock_divide_amount==-5) // *5
return(clk_time/5);
else if (clock_divide_amount==-6) // *6
return(clk_time/6);
else if (clock_divide_amount==-7) // *7
return(clk_time/7);
else if (clock_divide_amount==-8) // *8
return(clk_time>>3);
else return(clk_time);
}
uint32_t get_fall_time(uint8_t skew_adc, uint32_t div_clk_time){
uint32_t t,u;
uint8_t rev_skew;
if (!LIMIT_SKEW || (div_clk_time<(LIMIT_SKEW_TIME>>1)) ){
if (skew_adc==0)
return (768);
else if (skew_adc==1)
return(1024);
else if (skew_adc==2)
return(1280);
else if (skew_adc<=25){
t=(skew_adc) * (div_clk_time >> 8);
u=(skew_adc*skew_adc*64)+960;
if (t<1280) t=1280;
if (t<u) return t;
else
return u;
}
else if (skew_adc>=220)
return(div_clk_time-256);
else if (skew_adc>200){
t=(skew_adc) * (div_clk_time >> 8);
if (t>(div_clk_time-256)) t= div_clk_time-256;
rev_skew=255-skew_adc;
u=rev_skew*rev_skew*64;
if (u>(div_clk_time-256)){
return t;
} else {
u=div_clk_time-u;
if(t>u) return t;
else return u;
}
}
else if ((skew_adc>101) && (skew_adc<=114))
return(div_clk_time>>1);
else
return ((skew_adc) * (div_clk_time >> 8));
}
else { //LIMIT_SKEW
if ((skew_adc>101) && (skew_adc<=114)){
return(div_clk_time>>1);
} else {
t=(skew_adc) * (div_clk_time >> 8);
if (t<LIMIT_SKEW_TIME) t=LIMIT_SKEW_TIME;
if (t>(div_clk_time-LIMIT_SKEW_TIME)) t=div_clk_time-LIMIT_SKEW_TIME;
return(t);
}
}
}
int16_t calc_curve(int16_t t_dacout, char cur_curve){
uint16_t t_loga, t_inv_loga;
#ifndef OMITLOGCURVES
t_loga=pgm_read_word_near(&(loga[t_dacout]));
t_inv_loga=4095-pgm_read_word_near(&(loga[4095-t_dacout]));
#else
t_inv_loga=(((int32_t)t_dacout)*((int32_t)t_dacout))>>12;
t_loga=4095-(
(
((int32_t)(4095-t_dacout)) * ((int32_t)(4095-t_dacout))
)
>>12
);
#endif
if (cur_curve==LIN)
return (t_dacout);
else if (cur_curve==LOG)
return(t_loga);
else if (cur_curve==EXP)
return(t_inv_loga);
else if (cur_curve==EXP25){ //25% exp 75% lin
return((t_dacout >> 1) + (t_dacout >> 2) + (t_inv_loga >> 2));
}
else if (cur_curve==EXP50) //50% exp 50% lin
return((t_inv_loga >> 1) + (t_dacout>>1));
else if (cur_curve==EXP75){ //75% exp 25% lin
return((t_inv_loga >> 1) + (t_inv_loga >> 2) + (t_dacout >> 2));
}
else if (cur_curve==LIN25){ //25% lin 75% log
return((t_loga >> 1) + (t_loga >> 2) + (t_dacout >> 2));
}
else if (cur_curve==LIN50) //50% lin 50% log
return((t_loga >> 1) + (t_dacout>>1));
else if (cur_curve==LIN75){ //75% lin 25% log
return((t_dacout >> 1) + (t_dacout >> 2) + (t_loga >> 2));
}
else return(t_dacout);
}
void init_adc(void){
//init the ADC:
ADC_DDR &= ~(ADC_mask); //adc input
ADC_PORT &= ~(ADC_mask); //disable pullup
ADCSRA = (1<<ADEN); //Enable ADC
ADMUX = (1<<ADLAR); //Left-Adjust
ADCSRA |= (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); //prescale = clk/128 = 125kHz
ADCSRA |= (1<<ADSC);//set the Start Conversion Flag in the ADC Status Register
}
/*
TIMERS:
TCNT0 increments every 0.5us at 16MHz
overflows every 128us
So a timer value is expressed in 0.5us
e.g. if get_pingtmr() returns 8000, that's 4ms
*/
ISR (INT0_vect){
if (PING){
ping_irq_timestamp=(pingtmr << 8) | TCNT0;
pingtmr=0;
using_tap_clock=0;
}
}
ISR (INT1_vect){
if (TRIGQ){
trigq_irq_timestamp=1;
sync_to_ping_mode=1;
reset_nextping_flag=1;
trigq_jack_down=1;
} else {
trigq_jack_down=0;
}
}
void pcint_init(void){
//interrupt on any change on INT1 (PD3/triga), or rising-edge of INT0 (PD2/ping)
EICRA = (1<<ISC10) | (0<<ISC11) | (1<<ISC00) | (0<<ISC01);
EIMSK = (1<<INT1) | (1<<INT0);
PCMSK2 = 0;
PCMSK1 = 0;
}
void read_blue_detect(void){
BLUE_DETECT=READ_BLUE_DETECT;
}
void eo1_on(void);
void eo2_on(void);
void eo1_off(void);
void eo2_off(void);
void eor_on(void);
void eor_off(void);
void eof_on(void);
void eof_off(void);
void hr_on(void);
void hr_off(void);
void tapclkout_off(void);
void tapclkout_on(void);
void eo1_on(void){
if (!eo1_high){
EO1_ON;
eo1_high=1;
reset_eo1tmr();
}
}
void eo2_on(void){
if (!eo2_high){
EO2_ON;
eo2_high=1;
reset_eo2tmr();
}
}
void eo1_off(void){
if (!EO1_IS_TRIG && (eo1tmr>EO_GATE_TIME)) {
EO1_OFF;
reset_eo1tmr();
}
eo1_high=0;
}
void eo2_off(void){
if (!EO2_IS_TRIG && (eo2tmr>EO_GATE_TIME)) {
EO2_OFF;
reset_eo2tmr();
}
eo2_high=0;
}
inline void eor_on(void){
if (!EOR_IS_HALFRISE)
eo1_on();
}
inline void eor_off(void){
if (!EOR_IS_HALFRISE)
eo1_off();
}
inline void eof_on(void){
if (!EOF_IS_TAPCLKOUT)
eo2_on();
}
inline void eof_off(void){
if (!EOF_IS_TAPCLKOUT)
eo2_off();
}
inline void hr_on(void){
if (EOR_IS_HALFRISE)
eo1_on();
}
inline void hr_off(void){
if (EOR_IS_HALFRISE)
eo1_off();
}
inline void tapclkout_on(void){
if (EOF_IS_TAPCLKOUT)
eo2_on();
}
inline void tapclkout_off(void){
if (EOF_IS_TAPCLKOUT)
eo2_off();
}
/***************************************************
* MAIN() FUNCTION *
* *
***************************************************/
int main(void){
uint16_t timer_overflowed_running_total=0;
char use_timer_overflowed=0;
uint32_t now=0;
char env_state=WAIT;
char reset_now_flag=0;
char end_env_flag=0;
char end_segment_flag=0;
uint8_t triga_jack_down=0;
uint8_t cycle_down=0;
uint32_t div_clk_time=0;
uint32_t async_phase_diff=0;
uint8_t async_env_changed_shape=1;
uint32_t elapsed_time=0;
uint32_t clk_time=0;
uint32_t rise_time=0;
uint32_t fall_time=0;
uint64_t time_t=0;
uint32_t tapout_clk_time=0;
uint8_t skew_adc=127;
uint8_t clock_div_adc=127;
uint8_t curve_adc=127;
int8_t clock_divider_amount=1;
int8_t t_clock_divider_amount=1;
int8_t c_d_a=1;
int8_t new_clock_divider_amount=1;
int8_t hys_clock_divider_amount=0;
char curve_rise=LIN;
char curve_fall=LIN;
char cur_curve=LIN;
char next_curve_rise=LIN, next_curve_fall=LIN;
char current_adc_channel=CLKDIV_adc,last_adc_channel;
unsigned char adch=0;
uint16_t poll_user_input=0;
char tapin_down=0;
char tapin_up=0;
uint32_t last_tapin_time=0;
char envelope_running=0;
int16_t t_dacout=0;
//uint16_t t_loga, t_inv_loga;
uint32_t rise_inc=0;
uint32_t fall_inc=0;
int32_t accum=0;
uint8_t update_risefallincs=0;
char divmult_changed=0;
uint8_t didnt_change_divmult=0;
char tracking_changedrisefalls=0;
int32_t new_accum=0;
int16_t new_dacout=0;
int32_t transition_inc=0;
char next_env_state=WAIT;
int8_t transition_ctr=0;
char outta_sync=0;
uint32_t rise_per_clk=0;
int8_t ping_div_ctr=0;
char div_ping_led=0;
char ready_to_start_async=0;
char got_tap_clock=0;
uint8_t temp_u8=0,d,data1,data2;
uint16_t temp_u16=0;
uint32_t temp_u32=0;
uint8_t flash_cycle_led=0;
uint32_t entering_system_mode=0;
uint8_t system_mode_cur=0;
uint8_t initial_cycle_button_state=0;
char update_cycle_button_now=0;
//uint8_t ping_link_mode_master=0;
//uint8_t send_ping_link_now=0;
uint8_t rx_data=0, rx_ptr=0,rx_checksum=0;
uint8_t rx_buff[3]={0,0,0};
/** Initialize **/
accum=0;
inittimer();
init_pins();
CHANNEL_ID_OUT_ON;
init_adc();
pcint_init();
init_uart(64);
eor_off();
eof_on();
if (CYCLE_BUT) CYCLE_LED_ON;
else CYCLE_LED_OFF;
_delay_ms(5);
read_blue_detect();
temp_u8=eeprom_read_byte((const uint8_t *)(TAPCLK_EEPROMADDR));
if (temp_u8==EEPROM_SET)
EOF_IS_TAPCLKOUT=1;
else
EOF_IS_TAPCLKOUT=0;
temp_u8=eeprom_read_byte((const uint8_t *)(PING_EEPROMADDR));
if (temp_u8==EEPROM_SET)
NO_FREERUNNING_PING=1;
else
NO_FREERUNNING_PING=0;
temp_u8=eeprom_read_byte((const uint8_t *)(HALFRISE_EEPROMADDR));
if (temp_u8==EEPROM_SET)
EOR_IS_HALFRISE=1;
else
if (temp_u8==EEPROM_UNPROGRAMMED && BLUE_DETECT) {//a new chip with the blue jumper should default to HALFRISE mode
EOR_IS_HALFRISE=1;
eeprom_busy_wait();
eeprom_write_byte ((uint8_t *)(HALFRISE_EEPROMADDR),EEPROM_SET);
eeprom_busy_wait();
}
else EOR_IS_HALFRISE=0;
temp_u8=eeprom_read_byte((const uint8_t *)(EO1_TRIG_EEPROMADDR));
if (temp_u8==EEPROM_SET)
EO1_IS_TRIG=1;
else
EO1_IS_TRIG=0;
temp_u8=eeprom_read_byte((const uint8_t *)(EO2_TRIG_EEPROMADDR));
if (temp_u8==EEPROM_SET)
EO2_IS_TRIG=1;
else
EO2_IS_TRIG=0;
temp_u8=eeprom_read_byte((const uint8_t *)(LIMIT_SKEW_EEPROMADDR));
if (temp_u8==EEPROM_SET)
LIMIT_SKEW=1;
else
LIMIT_SKEW=0;
temp_u8=eeprom_read_byte((const uint8_t *)(ASYNC_SUSTAIN_EEPROMADDR));
if (temp_u8==EEPROM_SET)
ASYNC_CAN_SUSTAIN=0;
else
ASYNC_CAN_SUSTAIN=1;
#ifdef OMITLOGCURVES
BLUE_DETECT=0;
EOF_IS_TAPCLKOUT=1;
NO_FREERUNNING_PING=1;
LIMIT_SKEW=0;
ASYNC_CAN_SUSTAIN=1;
EO1_IS_TRIG=0;
EO2_IS_TRIG=0;
EOR_IS_HALFRISE=0;
EOF_IS_TAPCLKOUT=0;
#endif
_delay_ms(5);
clk_time=0;
div_clk_time=0;
last_tapin_time=0;
sync_to_ping_mode=1;
//Read all analog inputs
/** Main loop: about 19uS, but +5uS if some interrupts run **/
while(1){
/***************** TAP TEMPO ********************
*
* Read tap tempo buttons, and record
* time between taps.
*
* Clear the tap clock if button is held
* down for more than HOLDTIMECLEAR
*
*************************************************/
if (TAPIN){
tapin_down=0;
now=get_tapintmr();
if (!(tapin_up)){
tapin_up=1;
using_tap_clock=1;
#ifndef OMITLOGCURVES
if (last_tapin_time && (diff32(last_tapin_time,now)<(last_tapin_time>>1)) ) {
clk_time=(now>>1) + (last_tapin_time>>1);
} else {
clk_time=now;
last_tapin_time=now;
}
#else
clk_time=now;
last_tapin_time=now;
#endif
tapout_clk_time=clk_time;
reset_tapintmr();
reset_tapouttmr();
div_clk_time=get_clk_div_time(clock_divider_amount,clk_time);
fall_time=get_fall_time(skew_adc, div_clk_time);
rise_time=div_clk_time-fall_time;
rise_inc=udiv32(rise_time>>5);
fall_inc=udiv32(fall_time>>5);
didnt_change_divmult=NUM_ADC_CYCLES_BEFORE_TRANSITION; //force us to do a transition mode as if divmult or skew was changed
poll_user_input=USER_INPUT_POLL_TIME; //force us to enter the ADC block
} else {
if (now > HOLDTIMECLEAR){ //button has been down for more than 2 seconds
if (using_tap_clock){
env_state=WAIT;
envelope_running=0;
reset_divpingtmr();
clk_time=0; //added v4.2
div_clk_time=0; //added v4.2
accum=0;
using_tap_clock=0;
timer_overflowed=1;
}
tapout_clk_time=0;
last_tapin_time=0;
reset_tapouttmr();
PINGLEDLOW;
} else {
PINGLEDHIGH;
}
}
}
else {
tapin_up=0;
if (!(tapin_down)){
PINGLEDLOW;
tapin_down=1;
}
}
/******************** READ TRIG JACKS ****************
*
* Start and/or sync the envelope when receiveing a trigger
*
* -Quantized Trigger
* -Async Trigger
******************************************************/
// Read Trig jacks:
// (Re)start the envelope if triggere
if (TRIGA){
if (!triga_jack_down){
sync_to_ping_mode=0;
triga_jack_down=1;
reset_now_flag=1;
ready_to_start_async=0;
if (rise_time>0x1000) //do an immediate fall if rise_time is fast
outta_sync=1; //otherwise set the outta_sync flag which works to force a slew limited transition to zero
//start each envelope the same time after the divided clock
async_phase_diff=get_divpingtmr();
}
} else {
triga_jack_down=0;
}
if (trigq_irq_timestamp){ //this is set when the pinchange interrupt detects a TRIGQ jack going high
trigq_irq_timestamp=0; //clear it so we only run this once per TRIGQ
/*
When Cycle is on, a TRIGQ should always re-phase (re-start) a divided-ping env.
When Cycle is off, if and only if QNT_REPHASES_WHEN_CYCLE_OFF is set, then a TRIGQ should rephase