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