-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab5_PART2.c
449 lines (340 loc) · 9.1 KB
/
Lab5_PART2.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
#include <DSP28x_Project.h>
#include "string.h"
interrupt void test_isr(void);
interrupt void ADC_isr(void);
unsigned int ADC_get();
static unsigned int * SRAMaddress = (unsigned int *)0x200000; //pointer for SRAM address
static int a = 0; //test variable for the timer flag
int main(void){
DisableDog();
EALLOW;
unsigned int test = 0;
int laggingValue = 0xFF;
//unsigned int value = 0;
EALLOW;
CPUinit();
EALLOW;
DINT;
outputEnable();
initADC();
EALLOW;
DAC_init();
EALLOW;
outputEnable(); //having issues when not enabling twice
EALLOW;
timerINIT();
EALLOW;
while(1){
}
EALLOW;
return 0;
}
void setFreq(int keypadValue){
if(keypadValue == 0xF){
//default value
EALLOW;
ConfigCpuTimer(&CpuTimer1, 150, 1000000*1/10000*.5);
CpuTimer1Regs.TCR.bit.TSS = 0;
EALLOW;
}
else if(keypadValue == 1){ //
//second value not default
EALLOW;
ConfigCpuTimer(&CpuTimer1, 150, 1000000*1/5000*.5);
CpuTimer1Regs.TCR.bit.TSS = 0;
EALLOW;
}
else if(keypadValue == 2){
//third value
EALLOW;
ConfigCpuTimer(&CpuTimer1, 150, 1000000*1/20000*.5);
CpuTimer1Regs.TCR.bit.TSS = 0;
EALLOW;
}
else{
//do nothing
}
}
void outputEnable(){
//also enables the LCD
SysCtrlRegs.PCLKCR3.bit.XINTFENCLK = 1;
GpioCtrlRegs.GPAMUX2.all = 0x33000000;
GpioCtrlRegs.GPBMUX1.all = 0xFFFFFFFF; //
GpioCtrlRegs.GPCPUD.all = 0xFFFFFFFF;
GpioCtrlRegs.GPCMUX1.all = 0xFFFFFFFF; //contains xd[15:0]
GpioCtrlRegs.GPCMUX2.all = 0xFFFFFFFF; //enables A15:8
GpioCtrlRegs.GPAMUX2.all &= 0xFF00FFFF;
GpioCtrlRegs.GPAMUX2.all |= 0x00FC0000;
GpioCtrlRegs.GPAMUX2.all &= 0xFFFFFF00;
GpioCtrlRegs.GPAMUX2.all |= 0x00000051;
}
void CPUinit(){
InitSysCtrl();
EALLOW;
SysCtrlRegs.PLLSTS.bit.DIVSEL = 0;
SysCtrlRegs.PLLCR.bit.DIV = 0xA;
SysCtrlRegs.PLLSTS.bit.DIVSEL = 2;
}
void ADC_set(int a, int b){
McbspbRegs.DXR2.all = b;
McbspbRegs.DXR1.all = a;
}
void initADC(){
GpioCtrlRegs.GPAMUX2.all &= 0xFF00FFFF;
GpioCtrlRegs.GPAMUX2.all |= 0x00FC0000;
McbspbRegs.SPCR2.all= 0x0000;
McbspbRegs.SPCR1.all= 0x0000;
McbspbRegs.SPCR1.bit.CLKSTP = 2;
McbspbRegs.PCR.bit.CLKXP = 0;
McbspbRegs.PCR.bit.CLKRP = 0;
McbspbRegs.PCR.bit.CLKXM = 1;
McbspbRegs.PCR.bit.SCLKME = 0;
McbspbRegs.SRGR2.bit.CLKSM = 1;
McbspbRegs.SRGR1.bit.CLKGDV = 2;
McbspbRegs.PCR.bit.FSXM = 1;
McbspbRegs.SRGR2.bit.FSGM = 0;
McbspbRegs.PCR.bit.FSXP = 1;
McbspbRegs.XCR2.bit.XDATDLY = 1;
McbspbRegs.RCR2.bit.RDATDLY = 1;
McbspbRegs.XCR1.bit.XFRLEN1 = 0;
McbspbRegs.XCR1.bit.XWDLEN1 = 4;
McbspbRegs.RCR1.bit.RFRLEN1 = 0;
McbspbRegs.RCR1.bit.RWDLEN1 = 4;
McbspbRegs.SPCR2.bit.GRST=1;
//delay_loop();
McbspbRegs.SPCR2.bit.XRST=1;
McbspbRegs.SPCR1.bit.RRST=1;
//delay_loop();
McbspbRegs.SPCR2.bit.FRST=1;
}
void kp_enable(){
GpioCtrlRegs.GPADIR.all &= 0xFFFF00FF;
GpioCtrlRegs.GPADIR.all |= 0x000000FF;
}
int keypadScan(){
kp_enable();
int directionValue = 1;
GpioCtrlRegs.GPADIR.all |= 0x0100; //setting pin 8 as output
int i;
for(i=0;i<3;i++){
int data = GpioDataRegs.GPADAT.all;
data = data>>12;
data &= 0x0000000F;
;
GpioCtrlRegs.GPADIR.all |= directionValue*2;
if(data == 0xF){
return 0xF; //case 1
}
if(i == 0){
if(data == 0xF){
return 0xF; //case 1
}
else if(data == 0xE){
return 1;
}
else if(data == 0xD){
return 2;
}
else if(data == 0xB){
return 3;
}
else if(data == 0x7){
return 0xA;
}
}
else if(i == 1){
if(data == 0xF){
return 0xF; //case 1
}
else if(data == 0xE){
return 4;
}
else if(data == 0xD){
return 5;
}
else if(data == 0xB){
return 6;
}
else if(data == 0x7){
return 0xB;
}
}
else if(i == 2){
if(data == 0xF){
return 0xF; //case 1
}
else if(data == 0xE){
return 0x7;
}
else if(data == 0xD){
return 0x8;
}
else if(data == 0xB){
return 0x9;
}
else if(data == 0x7){
return 0xC;
}
}
else if(i == 3){
if(data == 0xF){
return 0xF; //case 1
}
else if(data == 0xE){
return 0xE;
}
else if(data == 0xD){
return 0;
}
else if(data == 0xB){
return 0xF;
}
else if(data == 0x7){
return 0xD;
}
}
}
}
unsigned int ADC_get(){
long fullIn;
ADC_set(0,0);
while(McbspbRegs.SPCR1.bit.RRDY == 0){
}
fullIn = McbspbRegs.DRR2.all;
fullIn = fullIn<<16;
fullIn |= McbspbRegs.DRR1.all;
fullIn &= 0x0003FFFC;
fullIn = fullIn>>2;
return((unsigned int)fullIn);
}
void DAC_init(){
GpioCtrlRegs.GPAMUX2.all &= 0xFFFFFF00;
GpioCtrlRegs.GPAMUX2.all |= 0x00000051;
SpiaRegs.SPICCR.bit.SPISWRESET= 0;
SpiaRegs.SPICCR.bit.CLKPOLARITY = 1;
SpiaRegs.SPICCR.bit.SPICHAR = 15;
SpiaRegs.SPICTL.bit.CLK_PHASE = 0;
SpiaRegs.SPICTL.bit.MASTER_SLAVE = 1;
SpiaRegs.SPICTL.bit.TALK = 1;
SpiaRegs.SPIBRR = 4;
SpiaRegs.SPICCR.bit.SPISWRESET = 1;
SpiaRegs.SPIPRI.bit.FREE = 1;
SpiaRegs.SPIFFTX.all = 0xE040;
SpiaRegs.SPIFFCT.all = 0x0;
}
void DAC_set(unsigned int data){
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG == 1){}
SpiaRegs.SPITXBUF = data;
}
void timerINIT(){
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
//InitPeripheralClocks();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
// PieVectTable.TINT0 = &cpu_timer0_isr;
PieVectTable.XINT13 = &ADC_isr;
//PieVectTable.TINT2 = &cpu_timer2_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize the Device Peripheral. This function can be
// found in DSP2833x_CpuTimers.c
InitCpuTimers(); // For this example, only initialize the Cpu Timers
EALLOW;
// Configure CPU-Timer 0, 1, and 2 to interrupt every second:
// 150MHz CPU Freq, 1 second Period (in uSeconds)
// ConfigCpuTimer(&CpuTimer0, 150, 1000000);
ConfigCpuTimer(&CpuTimer1, 150, 1000000*1/44000*.5); //1/10000 is the frequency we want to toggle timer1 to interrupt
//ConfigCpuTimer(&CpuTimer2, 150, 1000000);
#if (CPU_FRQ_100MHZ)
// Configure CPU-Timer 0, 1, and 2 to interrupt every second:
// 100MHz CPU Freq, 1 second Period (in uSeconds)
// ConfigCpuTimer(&CpuTimer0, 100, 1000000);
ConfigCpuTimer(&CpuTimer1, 150, 2000);
//ConfigCpuTimer(&CpuTimer2, 100, 1000000);
#endif
// To ensure precise timing, use write-only instructions to write to the entire register. Therefore, if any
// of the configuration bits are changed in ConfigCpuTimer and InitCpuTimers (in DSP2833x_CpuTimers.h), the
// below settings must also be updated.
// CpuTimer0Regs.TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0
CpuTimer1Regs.TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0
//CpuTimer2Regs.TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0
// Step 5. User specific code, enable interrupts:
// Enable CPU int1 which is connected to CPU-Timer 0, CPU int13
// which is connected to CPU-Timer 1, and CPU int 14, which is connected
// to CPU-Timer 2:
//IER |= M_INT1;
IER |= M_INT13;
//IER |= M_INT14;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
EALLOW;
// Step 6. IDLE loop. Just sit and loop forever (optional):
}
void SRAMwrite(unsigned int value){
//this assumes that SRAM has been intialized to 0x200000
*SRAMaddress = value;
SRAMaddress++;
}
void changeFunctions(){
EALLOW;
ConfigCpuTimer(&CpuTimer1, 150, 1000000*1/10000*.5);
CpuTimer1Regs.TCR.bit.TSS = 0;
EALLOW;
PieVectTable.XINT13 = &test_isr;
}
interrupt void ADC_isr(void){
unsigned int value;
initADC();
value = ADC_get();
DAC_set(value);
// if (a == 1){
// DAC_set(0xFFFF);
// a = 0;
// }
// else{
// DAC_set(0);
// a = 1;
// }
CpuTimer1.InterruptCount++;
EALLOW;
}
interrupt void test_isr(void){
if (a == 1){
EALLOW;
DAC_init();
a = 0;
}
if(SRAMaddress <= 0x2FFFFF){
DAC_set(*SRAMaddress);
SRAMaddress++;
}
CpuTimer1.InterruptCount++;
EALLOW;
}