Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | 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
 
117
void main() {
226 Kevin 118
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
237 Kevin 119
 
120
    /* -------------------- BEGIN INITIALIZATION --------------------- */
226 Kevin 121
 
237 Kevin 122
    // Configure the target for maximum performance at 80 MHz.
199 Kevin 123
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
124
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 125
 
199 Kevin 126
    // Configure the interrupts for multiple vectors
193 Kevin 127
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
128
 
199 Kevin 129
    // Set all analog I/O pins to digital
130
    AD1PCFGSET = 0xFFFF;
193 Kevin 131
 
237 Kevin 132
    // Enable the watchdog timer with windowed mode disabled
133
    // WDT prescaler set to 1048576 (1048.576s) (see config bits)
134
    WDTCON = 0x00008000;
135
 
136
    // Configure onboard LEDs
234 Kevin 137
    LED1_TRIS = 0;
138
    LED2_TRIS = 0;
139
    LED3_TRIS = 0;
140
    LED4_TRIS = 0;
141
    LED1_LAT = 0;
142
    LED2_LAT = 0;
143
    LED3_LAT = 0;
144
    LED4_LAT = 0;
145
 
263 Kevin 146
    // Determine what to do at this point. We either choose to idle (on POR)
147
    // or go into a mode specified prior to the software reset event
148
    uint8_t last_reset = Get_Reset_Condition();
149
    if (last_reset == RESET_POR || last_reset == RESET_BOR ||
150
            last_reset == RESET_PIN || last_reset == RESET_WDT ||
151
            last_reset == RESET_CFG) {
152
        op_state = BOARD_MODE_IDLE;
153
    }
154
 
212 Kevin 155
    // Initialize the SPI1 module
226 Kevin 156
    SPI1_DATA spi_1_data;
157
    SPI1_Init(&spi_1_data, NULL);
199 Kevin 158
 
226 Kevin 159
    // Initialize the SPI4 module
160
    SPI4_DATA spi_4_data;
161
    SPI4_Init(&spi_4_data);
162
 
237 Kevin 163
    // Initialize the I2C1 module
234 Kevin 164
    I2C1_DATA i2c_1_data;
165
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
166
 
261 Kevin 167
//    // Initialize the UART1 module
168
//    UART1_DATA uart_data;
169
//    UART1_Init(&uart_data, &Cube_Data_In);
212 Kevin 170
 
171
    // Initializs the PWM2 output to 20MHz
199 Kevin 172
    PWM2_Init();
173
 
212 Kevin 174
    // Initialize the cube variables
199 Kevin 175
    CUBE_DATA cube_data;
206 Kevin 176
    Cube_Init(&cube_data, 0x40);
199 Kevin 177
 
207 Kevin 178
    // Start the cube update layer interrupt
199 Kevin 179
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
206 Kevin 180
    TIMER5_DATA timer_5_data;
181
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
199 Kevin 182
 
237 Kevin 183
    // Start the controller polling and overlay rotation interrupt
206 Kevin 184
    TIMER4_DATA timer_4_data;
264 Kevin 185
    TIMER4_Init(&timer_4_data, NULL, NULL, 0);
206 Kevin 186
 
207 Kevin 187
    // Process button inputs
201 Kevin 188
    BTN_DATA btn_data;
237 Kevin 189
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, NULL);
234 Kevin 190
 
240 Kevin 191
    // Initialize controllers
237 Kevin 192
    CONTROLLER_DATA ctrl_data;
274 Kevin 193
    Controller_Init(&ctrl_data);
261 Kevin 194
 
195
    // Initialize the Ethernet module
264 Kevin 196
    if (op_state == BOARD_MODE_ETHERNET) {
266 Kevin 197
        LED1_LAT = 1;
264 Kevin 198
        ETH_DATA eth_data;
199
        ETH_Init(&eth_data, NULL, &Cube_Ethernet_Frame_In);
200
    }
240 Kevin 201
 
202
    SNAKE_DATA snake_data;
241 Kevin 203
    TRON_DATA tron_data;
200 Kevin 204
 
237 Kevin 205
    PWM2_Start();
206
    TIMER5_Start();
207
 
208
    /* -------------------- END OF INITIALIZATION -------------------- */
209
    /* ------------------------ BEGIN DISPLAY ------------------------ */
263 Kevin 210
 
264 Kevin 211
    // Figure out what to do at this point (depending on current state)
261 Kevin 212
    switch (op_state) {
213
        case BOARD_MODE_IDLE:
214
            Idle_Animation_Sequence();
215
            break;
241 Kevin 216
        case BOARD_MODE_SNAKE:;
274 Kevin 217
            Controller_Init(&ctrl_data);
264 Kevin 218
            TIMER4_Init(NULL, &Controller_Update, NULL, 0);
219
            TIMER4_Start();
237 Kevin 220
            Snake_Init(&snake_data);
240 Kevin 221
            Snake_Main();
237 Kevin 222
            break;
223
        case BOARD_MODE_TRON:
274 Kevin 224
            Controller_Init(&ctrl_data);
264 Kevin 225
            TIMER4_Init(NULL, &Controller_Update, NULL, 0);
226
            TIMER4_Start();
241 Kevin 227
            Tron_Init(&tron_data);
228
            Tron_Main();
237 Kevin 229
            break;
255 Kevin 230
        case BOARD_MODE_ETHERNET:
261 Kevin 231
            TIMER4_Stop();
266 Kevin 232
            LED2_LAT = 1;
255 Kevin 233
            while(1);
234
            break;
237 Kevin 235
    }
236
 
237
}
238
 
239
void Idle_Animation_Sequence(void) {
240
 
208 Kevin 241
//    Cube_Set_All(RED);
226 Kevin 242
//    Delay_MS(2000);
208 Kevin 243
//    Cube_Set_All(GREEN);
226 Kevin 244
//    Delay_MS(2000);
208 Kevin 245
//    Cube_Set_All(BLUE);
226 Kevin 246
//    Delay_MS(2000);
240 Kevin 247
//    Animation_Pseudo_Random_Colors(200);
248
//    Animation_Pseudo_Random_Colors(200);
249
//    Animation_Pseudo_Random_Colors(200);
250
//    Animation_Pseudo_Random_Colors(200);
251
//    Animation_Pseudo_Random_Colors(200);
252
//    Animation_Pseudo_Random_Colors(200);
207 Kevin 253
 
237 Kevin 254
    // Start the scrolling text
255
    TIMER4_Stop();
264 Kevin 256
    TIMER4_Init(NULL, NULL, &Cube_Text_Interrupt, 100);
263 Kevin 257
//    TIMER4_Start();
237 Kevin 258
 
231 Kevin 259
//    int8_t start_text[] = "Cube Initialized\r\n";
215 Kevin 260
//    UART1_Write(start_text, 18);
212 Kevin 261
 
206 Kevin 262
    // Set the overlay text
255 Kevin 263
    uint8_t text_string[] = "Welcome to the CCM Lab     ";
226 Kevin 264
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
268 Kevin 265
//    TIMER4_Start();
206 Kevin 266
 
199 Kevin 267
    // Loop through some preset animations
268
    while(1) {
264 Kevin 269
 
270
        Animation_Sawtooth(100);
271
        Animation_Sawtooth(100);
272
        Animation_Sawtooth(100);
273
        Animation_Sphere(100);
274
        Animation_Sphere(100);
275
        Animation_Sphere(100);
276
        Animation_Sphere(100);
277
        Animation_Wave1(100);
278
        Animation_Wave1(100);
279
        Animation_Wave1(100);
280
        Animation_Wave1(100);
281
        Animation_Wave2(100);
282
        Animation_Wave2(100);
283
        Animation_Wave2(100);
284
        Animation_Wave2(100);
237 Kevin 285
//        Animation_Solid_Colors(300);
286
//        Animation_Layer_Alternate(300);
287
//        Animation_Pixel_Alternate(200);
288
//        Animation_Full_Color_Sweep(1000);
289
        Animation_Row_Column_Sweep(40);
290
        Animation_Row_Column_Sweep(40);
291
        Animation_Cube_In_Cube(300);
292
        Animation_Cube_In_Cube(300);
293
        Animation_Cube_In_Cube(300);
264 Kevin 294
        Animation_Double_Rotation(30);
295
        Animation_Double_Rotation(30);
274 Kevin 296
        Animation_Double_Rotation(30);
297
        Animation_Double_Rotation(30);
264 Kevin 298
        Animation_Pseudo_Random_Colors(300);
299
        Animation_Pseudo_Random_Colors(300);
300
        Animation_Pseudo_Random_Colors(300);
301
        Animation_Pseudo_Random_Colors(300);
302
        Animation_Pseudo_Random_Colors(300);
303
        Animation_Pseudo_Random_Colors(300);
304
        Animation_Pseudo_Random_Colors(300);
305
        Animation_Pseudo_Random_Colors(300);
306
        Animation_Pseudo_Random_Colors(300);
307
        Animation_Pseudo_Random_Colors(300);
237 Kevin 308
//        Animation_Random_Colors(300);
234 Kevin 309
 
226 Kevin 310
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
199 Kevin 311
    }
193 Kevin 312
}
313
 
264 Kevin 314
// Function call on button 1 press to change cube operation
200 Kevin 315
void BTN1_Interrupt(void) {
264 Kevin 316
    switch (op_state) {
317
        case BOARD_MODE_IDLE:
318
            Reset_Board(BOARD_MODE_SNAKE);
200 Kevin 319
            break;
264 Kevin 320
        case BOARD_MODE_SNAKE:
321
            Reset_Board(BOARD_MODE_TRON);
200 Kevin 322
            break;
264 Kevin 323
        case BOARD_MODE_TRON:
324
            Reset_Board(BOARD_MODE_ETHERNET);
200 Kevin 325
            break;
264 Kevin 326
        case BOARD_MODE_ETHERNET:
327
            Reset_Board(BOARD_MODE_IDLE);
200 Kevin 328
            break;
329
    }
264 Kevin 330
 
331
    // Code to change refresh rate on button press
332
//    static uint8_t state;
333
//    state = (state == 4) ? 0 : state + 1;
334
//    TIMER5_Stop();
335
//    switch (state) {
336
//        case 0:
337
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
338
//            break;
339
//        case 1:
340
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
341
//            break;
342
//        case 2:
343
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
344
//            break;
345
//        case 3:
346
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
347
//            break;
348
//        case 4:
349
//            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
350
//            break;
351
//    }
352
//    TIMER5_Start();
200 Kevin 353
}
354
 
355
// Function call on button 2 press to change brightness
356
void BTN2_Interrupt(void) {
264 Kevin 357
    static uint8_t state = 6;
205 Kevin 358
    state = (state == 6) ? 0 : state + 1;
200 Kevin 359
    TIMER5_Stop();
360
    Delay_MS(1); // Need to wait for all SPI writes to complete
231 Kevin 361
    uint8_t BC;
200 Kevin 362
    switch (state) {
363
        case 0:
364
            BC = 0x01;
365
            break;
366
        case 1:
367
            BC = 0x08;
368
            break;
369
        case 2:
370
            BC = 0x10;
371
            break;
372
        case 3:
373
            BC = 0x20;
374
            break;
375
        case 4:
376
            BC = 0x40;
377
            break;
378
        case 5:
205 Kevin 379
            BC = 0x80;
200 Kevin 380
            break;
205 Kevin 381
        case 6:
382
            BC = 0xFF;
383
            break;
200 Kevin 384
    }
385
    Cube_Write_DCS(BC);
386
    TIMER5_Start();
387
}
388
 
237 Kevin 389
//// Function call on button 3 press to change text scroll speed
390
//void BTN3_Interrupt(void)  {
391
//    static uint8_t state;
392
//    state = (state == 4) ? 0 : state + 1;
393
//    TIMER4_Stop();
394
//    switch (state) {
395
//        case 0:
396
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
397
//            break;
398
//        case 1:
399
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
400
//            break;
401
//        case 2:
402
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
403
//            break;
404
//        case 3:
405
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
406
//            break;
407
//        case 4:
408
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
409
//            break;
410
//    }
411
//    TIMER4_Start();
412
//}