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"
193 Kevin 50
 
200 Kevin 51
void BTN1_Interrupt(void);
52
void BTN2_Interrupt(void);
206 Kevin 53
void BTN3_Interrupt(void);
199 Kevin 54
 
231 Kevin 55
void Delay_MS(uint32_t delay_ms) {
201 Kevin 56
    // Delays the CPU for the given amount of time.
57
    // Note: Watch out for integer overflow! (max delay_ms = 107374) ??
231 Kevin 58
    uint32_t delay = delay_ms * MS_TO_CT_TICKS;
59
    uint32_t startTime = ReadCoreTimer();
60
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 61
}
62
 
231 Kevin 63
void Delay_US(uint32_t delay_us) {
201 Kevin 64
    // Delays the CPU for the given amount of time.
65
    // Note: Watch out for integer overflow!
231 Kevin 66
    uint32_t delay = delay_us * US_TO_CT_TICKS;
67
    uint32_t startTime = ReadCoreTimer();
68
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 69
}
70
 
237 Kevin 71
uint8_t Get_Reset_Condition(void) {
72
    uint8_t ret = 0;
73
    if (RCONbits.POR && RCONbits.BOR)
74
        ret = RESET_POR;
75
    else if (RCONbits.BOR)
76
        ret = RESET_BOR;
77
    else if (RCONbits.EXTR)
78
        ret = RESET_PIN;
79
    else if (RCONbits.SWR)
80
        ret = RESET_SWR;
81
    else if (RCONbits.CMR)
82
        ret = RESET_CFG;
83
    else if (RCONbits.WDTO)
84
        ret = RESET_WDT;
85
    // Clear the RCON register
86
    RCON = 0x0;
87
    return ret;
88
}
89
 
240 Kevin 90
// Initialize a persistent operational state machine
91
static BOARD_STATE op_state __attribute__((persistent));
92
 
93
uint8_t Get_Board_State(void) {
94
    return op_state.cube_mode;
95
}
96
 
97
void Reset_Board(uint8_t next_state) {
98
    op_state.cube_mode = next_state;
99
 
237 Kevin 100
    // Executes a software reset
101
    INTDisableInterrupts();
102
    SYSKEY = 0x00000000; // Write invalid key to force lock
103
    SYSKEY = 0xAA996655; // Write key1 to SYSKEY
104
    SYSKEY = 0x556699AA; // Write key2 to SYSKEY
105
    /* OSCCON is now unlocked */
106
    // Set SWRST bit to arm reset
107
    RSWRSTSET = 1;
108
    // Read RSWRST register to trigger reset
109
    uint32_t dummy;
110
    dummy = RSWRST;
111
    // Prevent any unwanted code execution until reset occurs
112
    while(1);
113
}
114
 
115
void main() {
226 Kevin 116
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
237 Kevin 117
 
118
    /* -------------------- BEGIN INITIALIZATION --------------------- */
226 Kevin 119
 
237 Kevin 120
    // Configure the target for maximum performance at 80 MHz.
199 Kevin 121
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
122
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 123
 
199 Kevin 124
    // Configure the interrupts for multiple vectors
193 Kevin 125
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
126
 
199 Kevin 127
    // Set all analog I/O pins to digital
128
    AD1PCFGSET = 0xFFFF;
193 Kevin 129
 
237 Kevin 130
    // Enable the watchdog timer with windowed mode disabled
131
    // WDT prescaler set to 1048576 (1048.576s) (see config bits)
132
    WDTCON = 0x00008000;
133
 
134
    // Configure onboard LEDs
234 Kevin 135
    LED1_TRIS = 0;
136
    LED2_TRIS = 0;
137
    LED3_TRIS = 0;
138
    LED4_TRIS = 0;
139
    LED1_LAT = 0;
140
    LED2_LAT = 0;
141
    LED3_LAT = 0;
142
    LED4_LAT = 0;
143
 
212 Kevin 144
    // Initialize the SPI1 module
226 Kevin 145
    SPI1_DATA spi_1_data;
146
    SPI1_Init(&spi_1_data, NULL);
199 Kevin 147
 
226 Kevin 148
    // Initialize the SPI4 module
149
    SPI4_DATA spi_4_data;
150
    SPI4_Init(&spi_4_data);
151
 
237 Kevin 152
    // Initialize the I2C1 module
234 Kevin 153
    I2C1_DATA i2c_1_data;
154
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
155
 
212 Kevin 156
    // Initialize the UART1 module
157
    UART1_DATA uart_data;
158
    UART1_Init(&uart_data, &Cube_Data_In);
159
 
160
    // Initializs the PWM2 output to 20MHz
199 Kevin 161
    PWM2_Init();
162
 
212 Kevin 163
    // Initialize the cube variables
199 Kevin 164
    CUBE_DATA cube_data;
206 Kevin 165
    Cube_Init(&cube_data, 0x40);
199 Kevin 166
 
207 Kevin 167
    // Start the cube update layer interrupt
199 Kevin 168
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
206 Kevin 169
    TIMER5_DATA timer_5_data;
170
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
199 Kevin 171
 
237 Kevin 172
    // Start the controller polling and overlay rotation interrupt
206 Kevin 173
    TIMER4_DATA timer_4_data;
237 Kevin 174
    TIMER4_Init(&timer_4_data, &Controller_Update, NULL, 0);
206 Kevin 175
 
207 Kevin 176
    // Process button inputs
201 Kevin 177
    BTN_DATA btn_data;
237 Kevin 178
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, NULL);
234 Kevin 179
 
240 Kevin 180
    // Initialize controllers
237 Kevin 181
    CONTROLLER_DATA ctrl_data;
240 Kevin 182
    Controller_Init(&ctrl_data, &Snake_Update_Direction);
183
 
184
    SNAKE_DATA snake_data;
200 Kevin 185
 
237 Kevin 186
    // Determine what to do at this point. We either choose to idle (on POR)
187
    // or go into a mode specified prior to the software reset event
188
    uint8_t last_reset = Get_Reset_Condition();
189
    switch (last_reset) {
190
        // If our last reset was a POR/BOR/PIN/WDT/CFG, go into idle mode
191
        case RESET_POR:
192
        case RESET_BOR:
193
        case RESET_PIN:
194
        case RESET_WDT:
195
        case RESET_CFG:
196
            op_state.cube_mode = BOARD_MODE_IDLE;
197
            break;
198
    }
199
 
200
    PWM2_Start();
201
    TIMER5_Start();
202
    TIMER4_Start();
203
 
204
    /* -------------------- END OF INITIALIZATION -------------------- */
205
    /* ------------------------ BEGIN DISPLAY ------------------------ */
206
 
207
    switch (op_state.cube_mode) {
208
        case BOARD_MODE_SNAKE:
209
            Snake_Init(&snake_data);
240 Kevin 210
            Snake_Main();
237 Kevin 211
            break;
212
        case BOARD_MODE_TRON:
213
            while(1);
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
//}