Subversion Repositories Code-Repo

Rev

Rev 278 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
199 Kevin 1
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
193 Kevin 2
/* ------------------------------------------------------------ */
3
/* PIC32 Configuration Settings */
4
/* ------------------------------------------------------------ */
5
/* Oscillator Settings */
6
#pragma config FNOSC     = PRIPLL   // Oscillator Selection Bits
7
#pragma config POSCMOD   = EC       // Primary Oscillator Configuration
8
#pragma config FPLLIDIV  = DIV_2    // PLL Input Divider
9
#pragma config FPLLMUL   = MUL_20   // PLL Multiplier
10
#pragma config FPLLODIV  = DIV_1    // PLL Output Divider
199 Kevin 11
#pragma config FPBDIV    = DIV_1    // Peripheral Clock Divisor (timers/UART/SPI/I2C)
193 Kevin 12
#pragma config FSOSCEN   = OFF      // Secondary Oscillator Enable
13
/* Clock Control Settings */
14
#pragma config IESO      = OFF      // Internal/External Clock Switch Over
15
#pragma config FCKSM     = CSDCMD   // Clock Switching and Monitor Selection
16
#pragma config OSCIOFNC  = OFF      // CLKO Output Signal Active on the OSCO Pin
17
/* USB Settings */
18
#pragma config UPLLEN    = ON       // USB PLL Enable
19
#pragma config UPLLIDIV  = DIV_2    // USB PLL Input Divider
20
#pragma config FVBUSONIO = OFF      // USB VBUS ON Selection
21
#pragma config FUSBIDIO  = OFF      // USB USID Selection
22
/* Other Peripheral Device Settings */
237 Kevin 23
#pragma config FWDTEN    = OFF      // Watchdog Timer Enable
226 Kevin 24
#pragma config WDTPS     = PS1048576    // Watchdog Timer Postscaler (1048.576s)
193 Kevin 25
#pragma config FSRSSEL   = PRIORITY_7   // SRS Interrupt Priority
26
#pragma config FCANIO    = OFF      // CAN I/O Pin Select (default/alternate)
27
#pragma config FETHIO    = ON       // Ethernet I/O Pin Select (default/alternate)
28
#pragma config FMIIEN    = OFF      // Ethernet MII/RMII select (OFF=RMII)
29
/* Code Protection Settings */
30
#pragma config CP        = OFF      // Code Protect
31
#pragma config BWP       = OFF      // Boot Flash Write Protect
32
#pragma config PWP       = OFF      // Program Flash Write Protect
33
/* Debug Settings */
34
#pragma config ICESEL = ICS_PGx1    // ICE/ICD Comm Channel Select (on-board debugger)
35
/* ------------------------------------------------------------ */
199 Kevin 36
// </editor-fold>
193 Kevin 37
 
38
#include "defines.h"
212 Kevin 39
#include "UART1.h"
199 Kevin 40
#include "SPI1.h"
226 Kevin 41
#include "SPI4.h"
234 Kevin 42
#include "I2C1.h"
255 Kevin 43
#include "ETHERNET.h"
206 Kevin 44
#include "TIMER4.h"
199 Kevin 45
#include "TIMER5.h"
46
#include "CUBE.h"
200 Kevin 47
#include "BTN.h"
226 Kevin 48
#include "ANIMATIONS.h"
237 Kevin 49
#include "CONTROLLERS.h"
50
#include "SNAKE.h"
241 Kevin 51
#include "TRON.h"
193 Kevin 52
 
200 Kevin 53
void BTN1_Interrupt(void);
54
void BTN2_Interrupt(void);
206 Kevin 55
void BTN3_Interrupt(void);
199 Kevin 56
 
231 Kevin 57
void Delay_MS(uint32_t delay_ms) {
201 Kevin 58
    // Delays the CPU for the given amount of time.
59
    // Note: Watch out for integer overflow! (max delay_ms = 107374) ??
231 Kevin 60
    uint32_t delay = delay_ms * MS_TO_CT_TICKS;
61
    uint32_t startTime = ReadCoreTimer();
62
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 63
}
64
 
231 Kevin 65
void Delay_US(uint32_t delay_us) {
201 Kevin 66
    // Delays the CPU for the given amount of time.
67
    // Note: Watch out for integer overflow!
231 Kevin 68
    uint32_t delay = delay_us * US_TO_CT_TICKS;
69
    uint32_t startTime = ReadCoreTimer();
70
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 71
}
72
 
237 Kevin 73
uint8_t Get_Reset_Condition(void) {
74
    uint8_t ret = 0;
75
    if (RCONbits.POR && RCONbits.BOR)
76
        ret = RESET_POR;
77
    else if (RCONbits.BOR)
78
        ret = RESET_BOR;
79
    else if (RCONbits.EXTR)
80
        ret = RESET_PIN;
81
    else if (RCONbits.SWR)
82
        ret = RESET_SWR;
83
    else if (RCONbits.CMR)
84
        ret = RESET_CFG;
85
    else if (RCONbits.WDTO)
86
        ret = RESET_WDT;
87
    // Clear the RCON register
88
    RCON = 0x0;
89
    return ret;
90
}
91
 
240 Kevin 92
// Initialize a persistent operational state machine
261 Kevin 93
volatile static uint8_t op_state __attribute__((persistent));
240 Kevin 94
 
95
uint8_t Get_Board_State(void) {
261 Kevin 96
    return op_state;
240 Kevin 97
}
98
 
99
void Reset_Board(uint8_t next_state) {
261 Kevin 100
    op_state = next_state;
240 Kevin 101
 
237 Kevin 102
    // Executes a software reset
103
    INTDisableInterrupts();
104
    SYSKEY = 0x00000000; // Write invalid key to force lock
105
    SYSKEY = 0xAA996655; // Write key1 to SYSKEY
106
    SYSKEY = 0x556699AA; // Write key2 to SYSKEY
107
    /* OSCCON is now unlocked */
108
    // Set SWRST bit to arm reset
109
    RSWRSTSET = 1;
110
    // Read RSWRST register to trigger reset
111
    uint32_t dummy;
112
    dummy = RSWRST;
113
    // Prevent any unwanted code execution until reset occurs
114
    while(1);
115
}
116
 
276 Kevin 117
void Test_Callback(uint8_t controller, CTRL_BTN_STATUS value) {
118
    LED1_LAT = 0;
119
    LED2_LAT = 0;
120
    LED3_LAT = 0;
121
    LED4_LAT = 0;
122
    if (value.BTN_R_N)
123
        LED1_LAT = 1;
124
    if (value.BTN_R_E)
125
        LED2_LAT = 1;
126
    if (value.BTN_R_S)
127
        LED3_LAT = 1;
128
    if (value.BTN_R_W)
129
        LED4_LAT = 1;
130
}
131
 
237 Kevin 132
void main() {
226 Kevin 133
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
237 Kevin 134
 
135
    /* -------------------- BEGIN INITIALIZATION --------------------- */
226 Kevin 136
 
237 Kevin 137
    // Configure the target for maximum performance at 80 MHz.
199 Kevin 138
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
139
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 140
 
199 Kevin 141
    // Configure the interrupts for multiple vectors
193 Kevin 142
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
143
 
199 Kevin 144
    // Set all analog I/O pins to digital
145
    AD1PCFGSET = 0xFFFF;
193 Kevin 146
 
237 Kevin 147
    // Enable the watchdog timer with windowed mode disabled
148
    // WDT prescaler set to 1048576 (1048.576s) (see config bits)
276 Kevin 149
//    WDTCON = 0x00008000;
150
    WDTCON = 0x00000000;
237 Kevin 151
 
152
    // Configure onboard LEDs
234 Kevin 153
    LED1_TRIS = 0;
154
    LED2_TRIS = 0;
155
    LED3_TRIS = 0;
156
    LED4_TRIS = 0;
157
    LED1_LAT = 0;
158
    LED2_LAT = 0;
159
    LED3_LAT = 0;
160
    LED4_LAT = 0;
161
 
263 Kevin 162
    // Determine what to do at this point. We either choose to idle (on POR)
163
    // or go into a mode specified prior to the software reset event
164
    uint8_t last_reset = Get_Reset_Condition();
165
    if (last_reset == RESET_POR || last_reset == RESET_BOR ||
166
            last_reset == RESET_PIN || last_reset == RESET_WDT ||
167
            last_reset == RESET_CFG) {
168
        op_state = BOARD_MODE_IDLE;
169
    }
170
 
212 Kevin 171
    // Initialize the SPI1 module
226 Kevin 172
    SPI1_DATA spi_1_data;
173
    SPI1_Init(&spi_1_data, NULL);
199 Kevin 174
 
226 Kevin 175
    // Initialize the SPI4 module
276 Kevin 176
//    SPI4_DATA spi_4_data;
177
//    SPI4_Init(&spi_4_data);
226 Kevin 178
 
237 Kevin 179
    // Initialize the I2C1 module
234 Kevin 180
    I2C1_DATA i2c_1_data;
278 Kevin 181
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
234 Kevin 182
 
261 Kevin 183
//    // Initialize the UART1 module
184
//    UART1_DATA uart_data;
185
//    UART1_Init(&uart_data, &Cube_Data_In);
212 Kevin 186
 
187
    // Initializs the PWM2 output to 20MHz
199 Kevin 188
    PWM2_Init();
189
 
212 Kevin 190
    // Initialize the cube variables
199 Kevin 191
    CUBE_DATA cube_data;
206 Kevin 192
    Cube_Init(&cube_data, 0x40);
199 Kevin 193
 
207 Kevin 194
    // Start the cube update layer interrupt
276 Kevin 195
    // 2084 = 60Hz, 500 = 250Hz, 250 = 500Hz
206 Kevin 196
    TIMER5_DATA timer_5_data;
197
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
199 Kevin 198
 
276 Kevin 199
    // Initialize timer for controller polling and overlay rotation interrupt
206 Kevin 200
    TIMER4_DATA timer_4_data;
264 Kevin 201
    TIMER4_Init(&timer_4_data, NULL, NULL, 0);
206 Kevin 202
 
207 Kevin 203
    // Process button inputs
201 Kevin 204
    BTN_DATA btn_data;
237 Kevin 205
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, NULL);
234 Kevin 206
 
240 Kevin 207
    // Initialize controllers
237 Kevin 208
    CONTROLLER_DATA ctrl_data;
276 Kevin 209
    Controller_Init(&ctrl_data, NULL);
261 Kevin 210
 
211
    // Initialize the Ethernet module
264 Kevin 212
    if (op_state == BOARD_MODE_ETHERNET) {
266 Kevin 213
        LED1_LAT = 1;
264 Kevin 214
        ETH_DATA eth_data;
215
        ETH_Init(&eth_data, NULL, &Cube_Ethernet_Frame_In);
216
    }
240 Kevin 217
 
218
    SNAKE_DATA snake_data;
241 Kevin 219
    TRON_DATA tron_data;
200 Kevin 220
 
237 Kevin 221
    PWM2_Start();
222
 
223
    /* -------------------- END OF INITIALIZATION -------------------- */
224
    /* ------------------------ BEGIN DISPLAY ------------------------ */
263 Kevin 225
 
264 Kevin 226
    // Figure out what to do at this point (depending on current state)
261 Kevin 227
    switch (op_state) {
228
        case BOARD_MODE_IDLE:
276 Kevin 229
            TIMER5_Start(); // Use the default refresh rate (250Hz)
261 Kevin 230
            Idle_Animation_Sequence();
231
            break;
276 Kevin 232
        case BOARD_MODE_SNAKE:
233
            // Change refresh rate to ~60Hz
234
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2000);
235
            TIMER5_Start();
236
            // Poll the controllers at 1kHz
237
            Controller_Init(NULL, &Snake_Update_Direction);
264 Kevin 238
            TIMER4_Init(NULL, &Controller_Update, NULL, 0);
276 Kevin 239
            // Initialize and start the game
237 Kevin 240
            Snake_Init(&snake_data);
240 Kevin 241
            Snake_Main();
237 Kevin 242
            break;
243
        case BOARD_MODE_TRON:
276 Kevin 244
            // Change refresh rate to ~60Hz
245
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2000);
246
            TIMER5_Start();
247
            // Poll the controllers at 1kHz
278 Kevin 248
            Controller_Init(NULL, &Tron_Update_Direction);
264 Kevin 249
            TIMER4_Init(NULL, &Controller_Update, NULL, 0);
276 Kevin 250
            // Initialize and start the game
251
            Tron_Init(&tron_data);
241 Kevin 252
            Tron_Main();
237 Kevin 253
            break;
255 Kevin 254
        case BOARD_MODE_ETHERNET:
261 Kevin 255
            TIMER4_Stop();
276 Kevin 256
            TIMER5_Start();
266 Kevin 257
            LED2_LAT = 1;
255 Kevin 258
            while(1);
259
            break;
237 Kevin 260
    }
261
 
262
}
263
 
264
void Idle_Animation_Sequence(void) {
265
 
208 Kevin 266
//    Cube_Set_All(RED);
226 Kevin 267
//    Delay_MS(2000);
208 Kevin 268
//    Cube_Set_All(GREEN);
226 Kevin 269
//    Delay_MS(2000);
208 Kevin 270
//    Cube_Set_All(BLUE);
226 Kevin 271
//    Delay_MS(2000);
240 Kevin 272
//    Animation_Pseudo_Random_Colors(200);
273
//    Animation_Pseudo_Random_Colors(200);
274
//    Animation_Pseudo_Random_Colors(200);
275
//    Animation_Pseudo_Random_Colors(200);
276
//    Animation_Pseudo_Random_Colors(200);
277
//    Animation_Pseudo_Random_Colors(200);
276 Kevin 278
 
279
    uint8_t connected, i;
280
    connected = Controller_Get_Connected();
281
    for (i = 0; i < connected; i++) {
282
        Controller_Set_Idle(i);
283
    }
207 Kevin 284
 
237 Kevin 285
    // Start the scrolling text
286
    TIMER4_Stop();
264 Kevin 287
    TIMER4_Init(NULL, NULL, &Cube_Text_Interrupt, 100);
263 Kevin 288
//    TIMER4_Start();
237 Kevin 289
 
231 Kevin 290
//    int8_t start_text[] = "Cube Initialized\r\n";
215 Kevin 291
//    UART1_Write(start_text, 18);
212 Kevin 292
 
206 Kevin 293
    // Set the overlay text
255 Kevin 294
    uint8_t text_string[] = "Welcome to the CCM Lab     ";
226 Kevin 295
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
268 Kevin 296
//    TIMER4_Start();
206 Kevin 297
 
199 Kevin 298
    // Loop through some preset animations
299
    while(1) {
264 Kevin 300
 
301
        Animation_Sawtooth(100);
302
        Animation_Sawtooth(100);
303
        Animation_Sawtooth(100);
304
        Animation_Sphere(100);
305
        Animation_Sphere(100);
306
        Animation_Sphere(100);
307
        Animation_Sphere(100);
308
        Animation_Wave1(100);
309
        Animation_Wave1(100);
310
        Animation_Wave1(100);
311
        Animation_Wave1(100);
312
        Animation_Wave2(100);
313
        Animation_Wave2(100);
314
        Animation_Wave2(100);
315
        Animation_Wave2(100);
237 Kevin 316
//        Animation_Solid_Colors(300);
317
//        Animation_Layer_Alternate(300);
318
//        Animation_Pixel_Alternate(200);
319
//        Animation_Full_Color_Sweep(1000);
276 Kevin 320
//        Animation_Row_Column_Sweep(40);
237 Kevin 321
        Animation_Row_Column_Sweep(40);
322
        Animation_Cube_In_Cube(300);
323
        Animation_Cube_In_Cube(300);
324
        Animation_Cube_In_Cube(300);
264 Kevin 325
        Animation_Double_Rotation(30);
326
        Animation_Double_Rotation(30);
274 Kevin 327
        Animation_Double_Rotation(30);
328
        Animation_Double_Rotation(30);
264 Kevin 329
        Animation_Pseudo_Random_Colors(300);
330
        Animation_Pseudo_Random_Colors(300);
331
        Animation_Pseudo_Random_Colors(300);
332
        Animation_Pseudo_Random_Colors(300);
333
        Animation_Pseudo_Random_Colors(300);
334
        Animation_Pseudo_Random_Colors(300);
335
        Animation_Pseudo_Random_Colors(300);
336
        Animation_Pseudo_Random_Colors(300);
337
        Animation_Pseudo_Random_Colors(300);
338
        Animation_Pseudo_Random_Colors(300);
237 Kevin 339
//        Animation_Random_Colors(300);
234 Kevin 340
 
226 Kevin 341
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
199 Kevin 342
    }
193 Kevin 343
}
344
 
264 Kevin 345
// Function call on button 1 press to change cube operation
200 Kevin 346
void BTN1_Interrupt(void) {
264 Kevin 347
    switch (op_state) {
348
        case BOARD_MODE_IDLE:
349
            Reset_Board(BOARD_MODE_SNAKE);
200 Kevin 350
            break;
264 Kevin 351
        case BOARD_MODE_SNAKE:
352
            Reset_Board(BOARD_MODE_TRON);
200 Kevin 353
            break;
264 Kevin 354
        case BOARD_MODE_TRON:
355
            Reset_Board(BOARD_MODE_ETHERNET);
200 Kevin 356
            break;
264 Kevin 357
        case BOARD_MODE_ETHERNET:
358
            Reset_Board(BOARD_MODE_IDLE);
200 Kevin 359
            break;
360
    }
264 Kevin 361
 
362
    // Code to change refresh rate on button press
363
//    static uint8_t state;
364
//    state = (state == 4) ? 0 : state + 1;
365
//    TIMER5_Stop();
366
//    switch (state) {
367
//        case 0:
368
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
369
//            break;
370
//        case 1:
371
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
372
//            break;
373
//        case 2:
374
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
375
//            break;
376
//        case 3:
377
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
378
//            break;
379
//        case 4:
380
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
381
//            break;
382
//    }
383
//    TIMER5_Start();
200 Kevin 384
}
385
 
386
// Function call on button 2 press to change brightness
387
void BTN2_Interrupt(void) {
264 Kevin 388
    static uint8_t state = 6;
205 Kevin 389
    state = (state == 6) ? 0 : state + 1;
200 Kevin 390
    TIMER5_Stop();
391
    Delay_MS(1); // Need to wait for all SPI writes to complete
231 Kevin 392
    uint8_t BC;
200 Kevin 393
    switch (state) {
394
        case 0:
395
            BC = 0x01;
396
            break;
397
        case 1:
398
            BC = 0x08;
399
            break;
400
        case 2:
401
            BC = 0x10;
402
            break;
403
        case 3:
404
            BC = 0x20;
405
            break;
406
        case 4:
407
            BC = 0x40;
408
            break;
409
        case 5:
205 Kevin 410
            BC = 0x80;
200 Kevin 411
            break;
205 Kevin 412
        case 6:
413
            BC = 0xFF;
414
            break;
200 Kevin 415
    }
416
    Cube_Write_DCS(BC);
417
    TIMER5_Start();
418
}
419
 
237 Kevin 420
//// Function call on button 3 press to change text scroll speed
421
//void BTN3_Interrupt(void)  {
422
//    static uint8_t state;
423
//    state = (state == 4) ? 0 : state + 1;
424
//    TIMER4_Stop();
425
//    switch (state) {
426
//        case 0:
427
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
428
//            break;
429
//        case 1:
430
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
431
//            break;
432
//        case 2:
433
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
434
//            break;
435
//        case 3:
436
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
437
//            break;
438
//        case 4:
439
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
440
//            break;
441
//    }
442
//    TIMER4_Start();
443
//}