Fully-Commented Main Code Module
1 ////////////////////////////////////
2
3
4 #include "config.h"
5
6 #include "pt_cornell_1_2.h"
7
8
9 #define Digipot_config_chan_A 0b0000000000000000
10
11
12
13 static struct pt pt_cmd, pt_input, pt_output, pt_DMA_output ;
14
15 volatile SpiChannel spiChn = SPI_CHANNEL1 ;
16 volatile int spiClkDiv = 4 ;
17
18
19
20
21
22
23 static char cmd[16];
24 static int value;
25
26 static PT_THREAD (protothread_cmd(struct pt *pt))
27 {
28 PT_BEGIN(pt);
29 while(1) {
30
31
32 sprintf(PT_send_buffer,"cmd>");
33 // by spawning a print thread
34 PT_SPAWN(pt, &pt_DMA_output, PT_DMA_PutSerialBuffer(&pt_DMA_output) );
35
36 //spawn a thread to handle terminal input
37 // the input thread waits for input
38 // -- BUT does NOT block other threads
39 // string is returned in "PT_term_buffer"
40 PT_SPAWN(pt, &pt_input, PT_GetSerialBuffer(&pt_input) );
41 // returns when the thread dies
42 // in this case, when <enter> is pushed
43 // now parse the string
44 sscanf(PT_term_buffer, "%s %d", cmd, &value);
45
46
47
48 if (cmd[0]=='d' ) {
49 //set mux to DSP chip
50 mPORTAClearBits(BIT_4);
51 mPORTBClearBits(BIT_13);
52 mPORTASetBits(BIT_4);
53 mPORTBClearBits(BIT_3 | BIT_7 | BIT_8 | BIT_9);
54
55 if(((value << 6) & (0x0040))){
56 mPORTBSetBits(BIT_3);
57 }
58 //select sound effect
59 mPORTBSetBits(value << 6);
60 }
61 if (cmd[0] == 's'){ //distortion
62 //set mux to Distortion
63 mPORTAClearBits(BIT_4);
64 mPORTBClearBits(BIT_13);
65 }
66 if (cmd[0] == 'n'){ //normal: only tone stack
67 //set mux to channel 0
68 mPORTAClearBits(BIT_4);
69 mPORTBClearBits(BIT_13);
70 mPORTBSetBits(BIT_13);
71 }
72 //pots
73 if (cmd[0] == 't'){ //treble
74 // CS low to start transaction
75 mPORTBClearBits(BIT_4); // start transaction
76 deliverSPICh1Datum(value);
77 // CS high
78 mPORTBSetBits(BIT_4); // end transaction
79 }
80 if (cmd[0] == 'b'){ //bass
81 // CS low to start transaction
82 mPORTAClearBits(BIT_0); // start transaction
83 deliverSPICh1Datum(value);
84 // CS high
85 mPORTASetBits(BIT_0); // end transaction
86 }
87 if (cmd[0] == 'v'){ //level
88 // CS low to start transaction
89 mPORTAClearBits(BIT_2); // start transaction
90 deliverSPICh1Datum(value);
91 // CS high
92 mPORTASetBits(BIT_2); // end transaction
93 }
94 if (cmd[0] == 'm'){ //mid
95 // CS low to start transaction
96 mPORTAClearBits(BIT_3); // start transaction
97 deliverSPICh1Datum(value);
98 // CS high
99 mPORTASetBits(BIT_3); // end transaction
100 }
101
102 // never exit while
103 } // END WHILE(1)
104 PT_END(pt);
105 } // thread 3
106
107 //instead of writing SPI code repeatedly, made a function.
108 //Used various chip selects on the same channel to change
109 //values of various different digital potentiometers
110 void deliverSPICh1Datum(int datum){
111 // test for ready
112 while (TxBufFullSPI1());
113 // write to spi2
114 WriteSPI1(Digipot_config_chan_A | datum);
115 // test for done
116 while (SPI1STATbits.SPIBUSY); // wait for end of transaction
117 // CS high
118 // mPORTBSetBits(BIT_4); // end transaction
119 }
120
121 // === Main ======================================================
122
123 int main(void)
124 {
125
126 // === config the uart, DMA, SPI ===========
127 PT_setup();
128
129 // == SPI ==
130 //enable SPI at 10 MHz clock to meet digital potentiometer specifications
131 SpiChnOpen(spiChn, SPI_OPEN_ON | SPI_OPEN_MODE16 | SPI_OPEN_MSTEN | SPI_OPEN_CKE_REV , spiClkDiv);
132
133 // === setup system wide interrupts ====================
134 INTEnableSystemMultiVectoredInt();
135
136 // === set up i/o port pin ===============================
137 //Port B bits, 3,7,8, and 9 are used to select digital output
138 //Port B bit 4 is used as chip select for treble digital potentiometer
139 //Port B bit 13 is used as a select signal for output effect selection multiplexer
140 //Additional functionality would use the TFT to display which output was being
141 //selected
142 mPORTBSetPinsDigitalOut(BIT_4 | BIT_3|BIT_7 | BIT_8 | BIT_9 |BIT_13); //Set port as output
143
144 //Port A Bits 0,2,and 3 are used to configure digital potentiometers (chip selects). Port A bit 4 is used
145 //for multiplexing whether to have input from Digital effector chip, distortion,
146 //or pure tonestack sound
147 mPORTASetPinsDigitalOut(BIT_0 | BIT_2 | BIT_3 | BIT_4);
148 // === now the threads ===================================
149
150 // init the threads
151 PT_INIT(&pt_cmd);
152 PT_INIT(&pt_time);
153
154 //==Digipot spi stuff
155 // SCK2 is pin 26
156 // SDO1 (MOSI) is in PPS output group 1, could be connected to RB5 which is pin 14
157 PPSOutput(2, RPB5, SDO1);
158 // control CS for DAC
159 //mPORTBSetPinsDigitalOut(BIT_4); //CS
160 mPORTBSetBits(BIT_4 | BIT_6);
161 //===
162
163 mPORTASetBits(BIT_0 | BIT_2 | BIT_3 | BIT_4); //CS pins active high
164 mPORTAClearBits(BIT_4);
165 mPORTBClearBits(BIT_13);
166 mPORTBSetBits(BIT_13);
167
168
169 // schedule the threads
170 while(1) {
171 //cmd used as command center for all effects
172 PT_SCHEDULE(protothread_cmd(&pt_cmd));
173 }
174 } // main