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"
206 Kevin 43
#include "TIMER4.h"
199 Kevin 44
#include "TIMER5.h"
45
#include "CUBE.h"
200 Kevin 46
#include "BTN.h"
226 Kevin 47
#include "ANIMATIONS.h"
237 Kevin 48
#include "CONTROLLERS.h"
49
#include "SNAKE.h"
241 Kevin 50
#include "TRON.h"
193 Kevin 51
 
200 Kevin 52
void BTN1_Interrupt(void);
53
void BTN2_Interrupt(void);
206 Kevin 54
void BTN3_Interrupt(void);
199 Kevin 55
 
231 Kevin 56
void Delay_MS(uint32_t delay_ms) {
201 Kevin 57
    // Delays the CPU for the given amount of time.
58
    // Note: Watch out for integer overflow! (max delay_ms = 107374) ??
231 Kevin 59
    uint32_t delay = delay_ms * MS_TO_CT_TICKS;
60
    uint32_t startTime = ReadCoreTimer();
61
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 62
}
63
 
231 Kevin 64
void Delay_US(uint32_t delay_us) {
201 Kevin 65
    // Delays the CPU for the given amount of time.
66
    // Note: Watch out for integer overflow!
231 Kevin 67
    uint32_t delay = delay_us * US_TO_CT_TICKS;
68
    uint32_t startTime = ReadCoreTimer();
69
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 70
}
71
 
237 Kevin 72
uint8_t Get_Reset_Condition(void) {
73
    uint8_t ret = 0;
74
    if (RCONbits.POR && RCONbits.BOR)
75
        ret = RESET_POR;
76
    else if (RCONbits.BOR)
77
        ret = RESET_BOR;
78
    else if (RCONbits.EXTR)
79
        ret = RESET_PIN;
80
    else if (RCONbits.SWR)
81
        ret = RESET_SWR;
82
    else if (RCONbits.CMR)
83
        ret = RESET_CFG;
84
    else if (RCONbits.WDTO)
85
        ret = RESET_WDT;
86
    // Clear the RCON register
87
    RCON = 0x0;
88
    return ret;
89
}
90
 
240 Kevin 91
// Initialize a persistent operational state machine
92
static BOARD_STATE op_state __attribute__((persistent));
93
 
94
uint8_t Get_Board_State(void) {
95
    return op_state.cube_mode;
96
}
97
 
98
void Reset_Board(uint8_t next_state) {
99
    op_state.cube_mode = next_state;
100
 
237 Kevin 101
    // Executes a software reset
102
    INTDisableInterrupts();
103
    SYSKEY = 0x00000000; // Write invalid key to force lock
104
    SYSKEY = 0xAA996655; // Write key1 to SYSKEY
105
    SYSKEY = 0x556699AA; // Write key2 to SYSKEY
106
    /* OSCCON is now unlocked */
107
    // Set SWRST bit to arm reset
108
    RSWRSTSET = 1;
109
    // Read RSWRST register to trigger reset
110
    uint32_t dummy;
111
    dummy = RSWRST;
112
    // Prevent any unwanted code execution until reset occurs
113
    while(1);
114
}
115
 
116
void main() {
226 Kevin 117
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
237 Kevin 118
 
119
    /* -------------------- BEGIN INITIALIZATION --------------------- */
226 Kevin 120
 
237 Kevin 121
    // Configure the target for maximum performance at 80 MHz.
199 Kevin 122
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
123
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 124
 
199 Kevin 125
    // Configure the interrupts for multiple vectors
193 Kevin 126
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
127
 
199 Kevin 128
    // Set all analog I/O pins to digital
129
    AD1PCFGSET = 0xFFFF;
193 Kevin 130
 
237 Kevin 131
    // Enable the watchdog timer with windowed mode disabled
132
    // WDT prescaler set to 1048576 (1048.576s) (see config bits)
133
    WDTCON = 0x00008000;
134
 
135
    // Configure onboard LEDs
234 Kevin 136
    LED1_TRIS = 0;
137
    LED2_TRIS = 0;
138
    LED3_TRIS = 0;
139
    LED4_TRIS = 0;
140
    LED1_LAT = 0;
141
    LED2_LAT = 0;
142
    LED3_LAT = 0;
143
    LED4_LAT = 0;
144
 
212 Kevin 145
    // Initialize the SPI1 module
226 Kevin 146
    SPI1_DATA spi_1_data;
147
    SPI1_Init(&spi_1_data, NULL);
199 Kevin 148
 
226 Kevin 149
    // Initialize the SPI4 module
150
    SPI4_DATA spi_4_data;
151
    SPI4_Init(&spi_4_data);
152
 
237 Kevin 153
    // Initialize the I2C1 module
234 Kevin 154
    I2C1_DATA i2c_1_data;
155
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
156
 
212 Kevin 157
    // Initialize the UART1 module
158
    UART1_DATA uart_data;
159
    UART1_Init(&uart_data, &Cube_Data_In);
160
 
161
    // Initializs the PWM2 output to 20MHz
199 Kevin 162
    PWM2_Init();
163
 
212 Kevin 164
    // Initialize the cube variables
199 Kevin 165
    CUBE_DATA cube_data;
206 Kevin 166
    Cube_Init(&cube_data, 0x40);
199 Kevin 167
 
207 Kevin 168
    // Start the cube update layer interrupt
199 Kevin 169
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
206 Kevin 170
    TIMER5_DATA timer_5_data;
171
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
199 Kevin 172
 
237 Kevin 173
    // Start the controller polling and overlay rotation interrupt
206 Kevin 174
    TIMER4_DATA timer_4_data;
237 Kevin 175
    TIMER4_Init(&timer_4_data, &Controller_Update, NULL, 0);
206 Kevin 176
 
207 Kevin 177
    // Process button inputs
201 Kevin 178
    BTN_DATA btn_data;
237 Kevin 179
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, NULL);
234 Kevin 180
 
240 Kevin 181
    // Initialize controllers
237 Kevin 182
    CONTROLLER_DATA ctrl_data;
241 Kevin 183
    Controller_Init(&ctrl_data, NULL);
240 Kevin 184
 
185
    SNAKE_DATA snake_data;
241 Kevin 186
    TRON_DATA tron_data;
200 Kevin 187
 
237 Kevin 188
    // Determine what to do at this point. We either choose to idle (on POR)
189
    // or go into a mode specified prior to the software reset event
190
    uint8_t last_reset = Get_Reset_Condition();
191
    switch (last_reset) {
192
        // If our last reset was a POR/BOR/PIN/WDT/CFG, go into idle mode
193
        case RESET_POR:
194
        case RESET_BOR:
195
        case RESET_PIN:
196
        case RESET_WDT:
197
        case RESET_CFG:
198
            op_state.cube_mode = BOARD_MODE_IDLE;
199
            break;
200
    }
201
 
202
    PWM2_Start();
203
    TIMER5_Start();
204
    TIMER4_Start();
205
 
206
    /* -------------------- END OF INITIALIZATION -------------------- */
207
    /* ------------------------ BEGIN DISPLAY ------------------------ */
208
 
209
    switch (op_state.cube_mode) {
241 Kevin 210
        case BOARD_MODE_SNAKE:;
211
            Controller_Init(&ctrl_data, &Snake_Update_Direction);
237 Kevin 212
            Snake_Init(&snake_data);
240 Kevin 213
            Snake_Main();
237 Kevin 214
            break;
215
        case BOARD_MODE_TRON:
241 Kevin 216
            Controller_Init(&ctrl_data, &Tron_Update_Direction);
217
            Tron_Init(&tron_data);
218
            Tron_Main();
237 Kevin 219
            break;
220
        case BOARD_MODE_IDLE:
221
        default:
222
            Idle_Animation_Sequence();
223
            break;
224
    }
225
 
226
}
227
 
228
void Idle_Animation_Sequence(void) {
240 Kevin 229
      Delay_MS(250);
237 Kevin 230
 
208 Kevin 231
//    Cube_Set_All(RED);
226 Kevin 232
//    Delay_MS(2000);
208 Kevin 233
//    Cube_Set_All(GREEN);
226 Kevin 234
//    Delay_MS(2000);
208 Kevin 235
//    Cube_Set_All(BLUE);
226 Kevin 236
//    Delay_MS(2000);
240 Kevin 237
//    Animation_Pseudo_Random_Colors(200);
238
//    Animation_Pseudo_Random_Colors(200);
239
//    Animation_Pseudo_Random_Colors(200);
240
//    Animation_Pseudo_Random_Colors(200);
241
//    Animation_Pseudo_Random_Colors(200);
242
//    Animation_Pseudo_Random_Colors(200);
207 Kevin 243
 
237 Kevin 244
    // Start the scrolling text
245
    TIMER4_Stop();
246
    TIMER4_Init(NULL, &Controller_Update, &Cube_Text_Interrupt, 100);
247
    TIMER4_Start();
248
 
231 Kevin 249
//    int8_t start_text[] = "Cube Initialized\r\n";
215 Kevin 250
//    UART1_Write(start_text, 18);
212 Kevin 251
 
206 Kevin 252
    // Set the overlay text
231 Kevin 253
    uint8_t text_string[] = "Welcome to the AMP Lab     ";
226 Kevin 254
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
206 Kevin 255
 
199 Kevin 256
    // Loop through some preset animations
257
    while(1) {
237 Kevin 258
//        Animation_Solid_Colors(300);
259
//        Animation_Layer_Alternate(300);
260
//        Animation_Pixel_Alternate(200);
261
//        Animation_Full_Color_Sweep(1000);
262
        Animation_Row_Column_Sweep(40);
263
        Animation_Row_Column_Sweep(40);
264
        Animation_Row_Column_Sweep(40);
265
        Animation_Cube_In_Cube(300);
266
        Animation_Cube_In_Cube(300);
267
        Animation_Cube_In_Cube(300);
268
        Animation_Double_Rotation(40);
269
        Animation_Double_Rotation(40);
270
        Animation_Double_Rotation(40);
271
//        Animation_Pseudo_Random_Colors(300);
272
//        Animation_Random_Colors(300);
234 Kevin 273
 
226 Kevin 274
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
199 Kevin 275
    }
193 Kevin 276
}
277
 
200 Kevin 278
// Function call on button 1 press to change refresh rate
279
void BTN1_Interrupt(void) {
231 Kevin 280
    static uint8_t state;
206 Kevin 281
    state = (state == 4) ? 0 : state + 1;
200 Kevin 282
    TIMER5_Stop();
283
    switch (state) {
284
        case 0:
207 Kevin 285
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
200 Kevin 286
            break;
287
        case 1:
207 Kevin 288
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
200 Kevin 289
            break;
290
        case 2:
207 Kevin 291
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
200 Kevin 292
            break;
293
        case 3:
207 Kevin 294
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
200 Kevin 295
            break;
206 Kevin 296
        case 4:
207 Kevin 297
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
206 Kevin 298
            break;
200 Kevin 299
    }
300
    TIMER5_Start();
301
}
302
 
303
// Function call on button 2 press to change brightness
304
void BTN2_Interrupt(void) {
231 Kevin 305
    static uint8_t state;
205 Kevin 306
    state = (state == 6) ? 0 : state + 1;
200 Kevin 307
    TIMER5_Stop();
308
    Delay_MS(1); // Need to wait for all SPI writes to complete
231 Kevin 309
    uint8_t BC;
200 Kevin 310
    switch (state) {
311
        case 0:
312
            BC = 0x01;
313
            break;
314
        case 1:
315
            BC = 0x08;
316
            break;
317
        case 2:
318
            BC = 0x10;
319
            break;
320
        case 3:
321
            BC = 0x20;
322
            break;
323
        case 4:
324
            BC = 0x40;
325
            break;
326
        case 5:
205 Kevin 327
            BC = 0x80;
200 Kevin 328
            break;
205 Kevin 329
        case 6:
330
            BC = 0xFF;
331
            break;
200 Kevin 332
    }
333
    Cube_Write_DCS(BC);
334
    TIMER5_Start();
335
}
336
 
237 Kevin 337
//// Function call on button 3 press to change text scroll speed
338
//void BTN3_Interrupt(void)  {
339
//    static uint8_t state;
340
//    state = (state == 4) ? 0 : state + 1;
341
//    TIMER4_Stop();
342
//    switch (state) {
343
//        case 0:
344
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
345
//            break;
346
//        case 1:
347
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
348
//            break;
349
//        case 2:
350
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
351
//            break;
352
//        case 3:
353
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
354
//            break;
355
//        case 4:
356
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
357
//            break;
358
//    }
359
//    TIMER4_Start();
360
//}