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
 
212 Kevin 146
    // Initialize the SPI1 module
226 Kevin 147
    SPI1_DATA spi_1_data;
148
    SPI1_Init(&spi_1_data, NULL);
199 Kevin 149
 
226 Kevin 150
    // Initialize the SPI4 module
151
    SPI4_DATA spi_4_data;
152
    SPI4_Init(&spi_4_data);
153
 
237 Kevin 154
    // Initialize the I2C1 module
234 Kevin 155
    I2C1_DATA i2c_1_data;
156
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
157
 
261 Kevin 158
//    // Initialize the UART1 module
159
//    UART1_DATA uart_data;
160
//    UART1_Init(&uart_data, &Cube_Data_In);
212 Kevin 161
 
162
    // Initializs the PWM2 output to 20MHz
199 Kevin 163
    PWM2_Init();
164
 
212 Kevin 165
    // Initialize the cube variables
199 Kevin 166
    CUBE_DATA cube_data;
206 Kevin 167
    Cube_Init(&cube_data, 0x40);
199 Kevin 168
 
207 Kevin 169
    // Start the cube update layer interrupt
199 Kevin 170
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
206 Kevin 171
    TIMER5_DATA timer_5_data;
172
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
199 Kevin 173
 
237 Kevin 174
    // Start the controller polling and overlay rotation interrupt
206 Kevin 175
    TIMER4_DATA timer_4_data;
237 Kevin 176
    TIMER4_Init(&timer_4_data, &Controller_Update, NULL, 0);
261 Kevin 177
//    TIMER4_Init(&timer_4_data, NULL, NULL, 0);
206 Kevin 178
 
207 Kevin 179
    // Process button inputs
201 Kevin 180
    BTN_DATA btn_data;
237 Kevin 181
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, NULL);
234 Kevin 182
 
240 Kevin 183
    // Initialize controllers
237 Kevin 184
    CONTROLLER_DATA ctrl_data;
241 Kevin 185
    Controller_Init(&ctrl_data, NULL);
261 Kevin 186
 
187
    // Initialize the Ethernet module
188
    ETH_DATA eth_data;
189
    ETH_Init(&eth_data, NULL, &Cube_Ethernet_Frame_In);
240 Kevin 190
 
191
    SNAKE_DATA snake_data;
241 Kevin 192
    TRON_DATA tron_data;
200 Kevin 193
 
237 Kevin 194
    // Determine what to do at this point. We either choose to idle (on POR)
195
    // or go into a mode specified prior to the software reset event
196
    uint8_t last_reset = Get_Reset_Condition();
242 Kevin 197
    if (last_reset == RESET_POR || last_reset == RESET_BOR ||
198
            last_reset == RESET_PIN || last_reset == RESET_WDT ||
199
            last_reset == RESET_CFG) {
261 Kevin 200
        op_state = BOARD_MODE_IDLE;
237 Kevin 201
    }
202
 
203
    PWM2_Start();
204
    TIMER5_Start();
205
    TIMER4_Start();
206
 
207
    /* -------------------- END OF INITIALIZATION -------------------- */
208
    /* ------------------------ BEGIN DISPLAY ------------------------ */
209
 
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
 
261 Kevin 230
    while(1);
231
 
237 Kevin 232
}
233
 
234
void Idle_Animation_Sequence(void) {
240 Kevin 235
      Delay_MS(250);
237 Kevin 236
 
208 Kevin 237
//    Cube_Set_All(RED);
226 Kevin 238
//    Delay_MS(2000);
208 Kevin 239
//    Cube_Set_All(GREEN);
226 Kevin 240
//    Delay_MS(2000);
208 Kevin 241
//    Cube_Set_All(BLUE);
226 Kevin 242
//    Delay_MS(2000);
240 Kevin 243
//    Animation_Pseudo_Random_Colors(200);
244
//    Animation_Pseudo_Random_Colors(200);
245
//    Animation_Pseudo_Random_Colors(200);
246
//    Animation_Pseudo_Random_Colors(200);
247
//    Animation_Pseudo_Random_Colors(200);
248
//    Animation_Pseudo_Random_Colors(200);
207 Kevin 249
 
237 Kevin 250
    // Start the scrolling text
251
    TIMER4_Stop();
252
    TIMER4_Init(NULL, &Controller_Update, &Cube_Text_Interrupt, 100);
253
    TIMER4_Start();
254
 
231 Kevin 255
//    int8_t start_text[] = "Cube Initialized\r\n";
215 Kevin 256
//    UART1_Write(start_text, 18);
212 Kevin 257
 
206 Kevin 258
    // Set the overlay text
255 Kevin 259
    uint8_t text_string[] = "Welcome to the CCM Lab     ";
226 Kevin 260
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
206 Kevin 261
 
199 Kevin 262
    // Loop through some preset animations
263
    while(1) {
237 Kevin 264
//        Animation_Solid_Colors(300);
265
//        Animation_Layer_Alternate(300);
266
//        Animation_Pixel_Alternate(200);
267
//        Animation_Full_Color_Sweep(1000);
268
        Animation_Row_Column_Sweep(40);
269
        Animation_Row_Column_Sweep(40);
270
        Animation_Row_Column_Sweep(40);
271
        Animation_Cube_In_Cube(300);
272
        Animation_Cube_In_Cube(300);
273
        Animation_Cube_In_Cube(300);
274
        Animation_Double_Rotation(40);
275
        Animation_Double_Rotation(40);
276
        Animation_Double_Rotation(40);
277
//        Animation_Pseudo_Random_Colors(300);
278
//        Animation_Random_Colors(300);
234 Kevin 279
 
226 Kevin 280
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
199 Kevin 281
    }
193 Kevin 282
}
283
 
200 Kevin 284
// Function call on button 1 press to change refresh rate
285
void BTN1_Interrupt(void) {
231 Kevin 286
    static uint8_t state;
206 Kevin 287
    state = (state == 4) ? 0 : state + 1;
200 Kevin 288
    TIMER5_Stop();
289
    switch (state) {
290
        case 0:
207 Kevin 291
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
200 Kevin 292
            break;
293
        case 1:
207 Kevin 294
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
200 Kevin 295
            break;
296
        case 2:
207 Kevin 297
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
200 Kevin 298
            break;
299
        case 3:
207 Kevin 300
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
200 Kevin 301
            break;
206 Kevin 302
        case 4:
207 Kevin 303
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
206 Kevin 304
            break;
200 Kevin 305
    }
306
    TIMER5_Start();
307
}
308
 
309
// Function call on button 2 press to change brightness
310
void BTN2_Interrupt(void) {
231 Kevin 311
    static uint8_t state;
205 Kevin 312
    state = (state == 6) ? 0 : state + 1;
200 Kevin 313
    TIMER5_Stop();
314
    Delay_MS(1); // Need to wait for all SPI writes to complete
231 Kevin 315
    uint8_t BC;
200 Kevin 316
    switch (state) {
317
        case 0:
318
            BC = 0x01;
319
            break;
320
        case 1:
321
            BC = 0x08;
322
            break;
323
        case 2:
324
            BC = 0x10;
325
            break;
326
        case 3:
327
            BC = 0x20;
328
            break;
329
        case 4:
330
            BC = 0x40;
331
            break;
332
        case 5:
205 Kevin 333
            BC = 0x80;
200 Kevin 334
            break;
205 Kevin 335
        case 6:
336
            BC = 0xFF;
337
            break;
200 Kevin 338
    }
339
    Cube_Write_DCS(BC);
340
    TIMER5_Start();
341
}
342
 
237 Kevin 343
//// Function call on button 3 press to change text scroll speed
344
//void BTN3_Interrupt(void)  {
345
//    static uint8_t state;
346
//    state = (state == 4) ? 0 : state + 1;
347
//    TIMER4_Stop();
348
//    switch (state) {
349
//        case 0:
350
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
351
//            break;
352
//        case 1:
353
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
354
//            break;
355
//        case 2:
356
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
357
//            break;
358
//        case 3:
359
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
360
//            break;
361
//        case 4:
362
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
363
//            break;
364
//    }
365
//    TIMER4_Start();
366
//}