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