Subversion Repositories Code-Repo

Rev

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;
237 Kevin 185
    TIMER4_Init(&timer_4_data, &Controller_Update, 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;
241 Kevin 193
    Controller_Init(&ctrl_data, NULL);
261 Kevin 194
 
195
    // Initialize the Ethernet module
196
    ETH_DATA eth_data;
197
    ETH_Init(&eth_data, NULL, &Cube_Ethernet_Frame_In);
240 Kevin 198
 
199
    SNAKE_DATA snake_data;
241 Kevin 200
    TRON_DATA tron_data;
200 Kevin 201
 
237 Kevin 202
    PWM2_Start();
203
    TIMER5_Start();
204
    TIMER4_Start();
205
 
206
    /* -------------------- END OF INITIALIZATION -------------------- */
207
    /* ------------------------ BEGIN DISPLAY ------------------------ */
263 Kevin 208
 
209
    // Figure out what state to go into here
261 Kevin 210
    switch (op_state) {
211
        case BOARD_MODE_IDLE:
212
            Idle_Animation_Sequence();
213
            break;
241 Kevin 214
        case BOARD_MODE_SNAKE:;
215
            Controller_Init(&ctrl_data, &Snake_Update_Direction);
237 Kevin 216
            Snake_Init(&snake_data);
240 Kevin 217
            Snake_Main();
237 Kevin 218
            break;
219
        case BOARD_MODE_TRON:
241 Kevin 220
            Controller_Init(&ctrl_data, &Tron_Update_Direction);
221
            Tron_Init(&tron_data);
222
            Tron_Main();
237 Kevin 223
            break;
255 Kevin 224
        case BOARD_MODE_ETHERNET:
261 Kevin 225
            TIMER4_Stop();
255 Kevin 226
            while(1);
227
            break;
237 Kevin 228
    }
229
 
230
}
231
 
232
void Idle_Animation_Sequence(void) {
240 Kevin 233
      Delay_MS(250);
237 Kevin 234
 
208 Kevin 235
//    Cube_Set_All(RED);
226 Kevin 236
//    Delay_MS(2000);
208 Kevin 237
//    Cube_Set_All(GREEN);
226 Kevin 238
//    Delay_MS(2000);
208 Kevin 239
//    Cube_Set_All(BLUE);
226 Kevin 240
//    Delay_MS(2000);
240 Kevin 241
//    Animation_Pseudo_Random_Colors(200);
242
//    Animation_Pseudo_Random_Colors(200);
243
//    Animation_Pseudo_Random_Colors(200);
244
//    Animation_Pseudo_Random_Colors(200);
245
//    Animation_Pseudo_Random_Colors(200);
246
//    Animation_Pseudo_Random_Colors(200);
207 Kevin 247
 
237 Kevin 248
    // Start the scrolling text
249
    TIMER4_Stop();
250
    TIMER4_Init(NULL, &Controller_Update, &Cube_Text_Interrupt, 100);
263 Kevin 251
//    TIMER4_Start();
237 Kevin 252
 
231 Kevin 253
//    int8_t start_text[] = "Cube Initialized\r\n";
215 Kevin 254
//    UART1_Write(start_text, 18);
212 Kevin 255
 
206 Kevin 256
    // Set the overlay text
255 Kevin 257
    uint8_t text_string[] = "Welcome to the CCM Lab     ";
226 Kevin 258
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
206 Kevin 259
 
199 Kevin 260
    // Loop through some preset animations
261
    while(1) {
237 Kevin 262
//        Animation_Solid_Colors(300);
263
//        Animation_Layer_Alternate(300);
264
//        Animation_Pixel_Alternate(200);
265
//        Animation_Full_Color_Sweep(1000);
266
        Animation_Row_Column_Sweep(40);
267
        Animation_Row_Column_Sweep(40);
268
        Animation_Row_Column_Sweep(40);
269
        Animation_Cube_In_Cube(300);
270
        Animation_Cube_In_Cube(300);
271
        Animation_Cube_In_Cube(300);
272
        Animation_Double_Rotation(40);
273
        Animation_Double_Rotation(40);
274
        Animation_Double_Rotation(40);
275
//        Animation_Pseudo_Random_Colors(300);
276
//        Animation_Random_Colors(300);
234 Kevin 277
 
226 Kevin 278
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
199 Kevin 279
    }
193 Kevin 280
}
281
 
200 Kevin 282
// Function call on button 1 press to change refresh rate
283
void BTN1_Interrupt(void) {
231 Kevin 284
    static uint8_t state;
206 Kevin 285
    state = (state == 4) ? 0 : state + 1;
200 Kevin 286
    TIMER5_Stop();
287
    switch (state) {
288
        case 0:
207 Kevin 289
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
200 Kevin 290
            break;
291
        case 1:
207 Kevin 292
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
200 Kevin 293
            break;
294
        case 2:
207 Kevin 295
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
200 Kevin 296
            break;
297
        case 3:
207 Kevin 298
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
200 Kevin 299
            break;
206 Kevin 300
        case 4:
207 Kevin 301
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
206 Kevin 302
            break;
200 Kevin 303
    }
304
    TIMER5_Start();
305
}
306
 
307
// Function call on button 2 press to change brightness
308
void BTN2_Interrupt(void) {
231 Kevin 309
    static uint8_t state;
205 Kevin 310
    state = (state == 6) ? 0 : state + 1;
200 Kevin 311
    TIMER5_Stop();
312
    Delay_MS(1); // Need to wait for all SPI writes to complete
231 Kevin 313
    uint8_t BC;
200 Kevin 314
    switch (state) {
315
        case 0:
316
            BC = 0x01;
317
            break;
318
        case 1:
319
            BC = 0x08;
320
            break;
321
        case 2:
322
            BC = 0x10;
323
            break;
324
        case 3:
325
            BC = 0x20;
326
            break;
327
        case 4:
328
            BC = 0x40;
329
            break;
330
        case 5:
205 Kevin 331
            BC = 0x80;
200 Kevin 332
            break;
205 Kevin 333
        case 6:
334
            BC = 0xFF;
335
            break;
200 Kevin 336
    }
337
    Cube_Write_DCS(BC);
338
    TIMER5_Start();
339
}
340
 
237 Kevin 341
//// Function call on button 3 press to change text scroll speed
342
//void BTN3_Interrupt(void)  {
343
//    static uint8_t state;
344
//    state = (state == 4) ? 0 : state + 1;
345
//    TIMER4_Stop();
346
//    switch (state) {
347
//        case 0:
348
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
349
//            break;
350
//        case 1:
351
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
352
//            break;
353
//        case 2:
354
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
355
//            break;
356
//        case 3:
357
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
358
//            break;
359
//        case 4:
360
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
361
//            break;
362
//    }
363
//    TIMER4_Start();
364
//}