-
Notifications
You must be signed in to change notification settings - Fork 0
/
irtest.c
276 lines (215 loc) · 7.89 KB
/
irtest.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
// graphics libraries
#include "config.h"
#include "tft_master.h"
#include "tft_gfx.h"
// need for rand function
#include <stdlib.h>
// serial stuff
#include <stdio.h>
#include <math.h>
// threading library
#define SYS_FREQ 64000000 //40000000
#include "pt_cornell_TFT.h"
#include "plib.h"
#define SYS_FREQ 64000000
#define SHOOT_LED BIT_0
#define LIFE_LED BIT_1
#define RECEIVER BIT_6
#define dmaChn 0
#define LATCH LATBbits.LATB4
#define spi_channel 1
// Use channel 2 since channel 1 is used by TFT display
#define spi_divider 10
// PIN Setup
// Latch Pin <-- RB4 (pin 11)
// ClockPin (SHCP) <-- SCK1(pin 25)
// DataPin (SPI1) <-- RB5 (pin 14)
static struct pt pt3, pt_input, pt_output, pt_DMA_output ;
// turn threads 1 and 2 on/off and set thread timing
int sys_time_seconds ;
int alive = 1;
static int timer_limit_1;
static int timer_limit_2;
static int timer_limit_3;
volatile unsigned char sine_table[256];
// === thread structures ============================================
// thread control structs
// note that UART input and output are threads
static struct pt pt_timer, pt_rpm, pt_pid ;
volatile static int lives = 0xFF;
void SPI_setup()
{
TRISBbits.TRISB4 = 0; // configure pin RB as an output
PPSOutput(2, RPB5, SDO1); // map SDO1 to RA1
}
void SPI1_transfer( int data)
{
SpiChnOpen(spi_channel, SPI_OPEN_MSTEN | SPI_OPEN_MODE8 | SPI_OPEN_DISSDI | SPI_OPEN_CKE_REV, spi_divider);
LATCH = 0; // set pin RB0 low / disable latch
while (TxBufFullSPI1()); // ensure buffer is free before writing
WriteSPI1(data); // send the data through SPI
while (SPI1STATbits.SPIBUSY); // blocking wait for end of transaction
LATCH = 1; // set pin RB0 high / enable latch
ReadSPI1();
//SpiChnClose(spi_channel);
CloseSPI1();
}
// Play game over sound
void playSound1(){
DmaChnEnable(dmaChn);
OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_1, timer_limit_1);
OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_256, 50000);
ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_2);
// Clear interrupt flag
mT5ClearIntFlag();
}
// Play score decrease sound
void playSound2(){
DmaChnEnable(dmaChn);
OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_1, timer_limit_2);
OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_256, 50000);
ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_2);
// Clear interrupt flag
mT5ClearIntFlag();
}
// Play score increase sound
void playSound3(){
DmaChnEnable(dmaChn);
OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_1, timer_limit_3);
OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_256, 50000);
ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_2);
// Clear interrupt flag
mT5ClearIntFlag();
}
void __ISR(_TIMER_5_VECTOR, ipl2) T5Handler(void){
mT3ClearIntFlag();
DmaChnDisable(dmaChn);
//CloseTimer2();
CloseTimer5();
}
static PT_THREAD (protothread_timer(struct pt *pt))
{
PT_BEGIN(pt);
while(1) {
//Trigger pressed, fire IR
if(alive == 1){
if(mPORTAReadBits(BIT_1)){
OC1RS = 842;
mPORTBSetBits(SHOOT_LED);
CVREFOpen( CVREF_ENABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
playSound1();
PT_YIELD_TIME_msec(100);
OC1RS = 0;
mPORTBClearBits(SHOOT_LED);
//CVREFOpen(CVREF_DISABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
//CVREFClose();
CVRCON = 0;
PT_YIELD_TIME_msec(200);
}else{
OC1RS = 0;
}
}else{
mPORTBClearBits(LIFE_LED);
CVRCON = 0;
PT_YIELD_TIME_msec(10);
SPI1_transfer(lives);
CVREFOpen( CVREF_ENABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
playSound2();
PT_YIELD_TIME_msec(2000);
playSound3();
PT_YIELD_TIME_msec(200);
mPORTBSetBits(LIFE_LED);
alive = 1;
//CVREFClose();
//CVREFOpen(CVREF_DISABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
CVRCON = 0;
PT_YIELD_TIME_msec(100);
}
} // END WHILE(1)
PT_END(pt);
} // timer thread
// === Main ======================================================
void main(void) {
// === config threads ==========
// turns OFF UART support and debugger pin
PT_setup();
// === setup system wide interrupts ========
INTEnableSystemMultiVectoredInt();
OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, 65535);
// set up the timer interrupt with a priority of 2
ConfigIntTimer2(T2_INT_OFF | T2_INT_PRIOR_2);
//period of 64000 should make the timer overflow frequency 38 kHz, the desired PWM frequency
OpenTimer3(T3_ON | T3_SOURCE_INT | T3_PS_1_1, 1684);
ConfigIntTimer2(T3_INT_OFF | T3_INT_PRIOR_2);
//OC STUFF
// set pulse to go high at 1/4 of the timer period and drop again at 1/2 the timer period
//ONLY TIMER 2 AND 3 CAN BE USED WITH OC. CAN CHANGE THE PROTOTHREADS TIMER COUNTER
//TO LET US USE 2 HERE AND TIMER 1 FOR PROTOTHREADS AND 45 FOR RPM
OpenOC1(OC_ON | OC_TIMER3_SRC | OC_PWM_FAULT_PIN_DISABLE , 0, 0);
// OC1 is PPS group 1, map to RPA0 (pin 2)
PPSOutput(1, RPA0, OC1);
OC1RS = 842;
PT_INIT(&pt_timer);
timer_limit_1 = SYS_FREQ/(256*900);
timer_limit_2 = SYS_FREQ/(256*200);
timer_limit_3 = SYS_FREQ/(256*300);
// set up the Vref pin and use as a DAC
// enable module| eanble output | use low range output | use internal reference | desired step
//CVREFOpen( CVREF_ENABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
// And read back setup from CVRCON for speed later
// 0x8060 is enabled with output enabled, Vdd ref, and 0-0.6(Vdd) range
int i;
for (i=0; i <256; i++){
sine_table[i] = (signed char) (8.0 * (sin((float)i*6.283/(float)256) + sin((float)(4.5*i*6.283)/(float)256)));
sine_table[i] = (sine_table[i] & 0x0F) | 0x8060;
}
// Open the desired DMA channel.
// We enable the AUTO option, we'll keep repeating the sam transfer over and over.
DmaChnOpen(dmaChn, 0, DMA_OPEN_AUTO);
// set the transfer parameters: source & destination address, source & destination size, number of bytes per event
// Setting the last parameter to one makes the DMA output one byte/interrupt
DmaChnSetTxfer(dmaChn, sine_table, (void*)&CVRCON, 256, 1, 1);
// set the transfer event control: what event is to start the DMA transfer
// In this case, timer2
DmaChnSetEventControl(dmaChn, DMA_EV_START_IRQ(_TIMER_4_IRQ));
// init the display
tft_init_hw();
tft_begin();
tft_fillScreen(ILI9340_BLACK);
//240x320 vertical display
tft_setRotation(0); // Use tft_setRotation(1) for 320x240
ConfigINT0(EXT_INT_ENABLE | FALLING_EDGE_INT |EXT_INT_PRI_2);
EnableINT0;
mPORTASetPinsDigitalIn(BIT_1);
mPORTBSetPinsDigitalOut(SHOOT_LED| LIFE_LED); //Shoot and life LEDs
mPORTBClearBits(SHOOT_LED);
mPORTBSetBits(LIFE_LED);
mPORTBSetPinsDigitalIn(RECEIVER);
INTEnableSystemMultiVectoredInt();
SPI_setup();
SPI1_transfer(lives);
// round-robin scheduler for threads
while (1){
PT_SCHEDULE(protothread_timer(&pt_timer));
}
} // main
// === end ======================================================
void __ISR(_EXTERNAL_0_VECTOR, ipl2) INT0Interrupt()
{
int cnt = 0;
//Debounce
while(cnt<1000){
if(mPORTBReadBits(RECEIVER) == 0){
cnt++;
}else{
break;
}
}
if(cnt == 1000){
alive = 0;
lives = lives << 1;
}else{
alive = 1;
}
mINT0ClearIntFlag();
}