Subversion Repositories Code-Repo

Rev

Rev 235 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 235 Rev 237
Line 18... Line 18...
18
#pragma config UPLLEN    = ON       // USB PLL Enable
18
#pragma config UPLLEN    = ON       // USB PLL Enable
19
#pragma config UPLLIDIV  = DIV_2    // USB PLL Input Divider
19
#pragma config UPLLIDIV  = DIV_2    // USB PLL Input Divider
20
#pragma config FVBUSONIO = OFF      // USB VBUS ON Selection
20
#pragma config FVBUSONIO = OFF      // USB VBUS ON Selection
21
#pragma config FUSBIDIO  = OFF      // USB USID Selection
21
#pragma config FUSBIDIO  = OFF      // USB USID Selection
22
/* Other Peripheral Device Settings */
22
/* Other Peripheral Device Settings */
23
#pragma config FWDTEN    = ON       // Watchdog Timer Enable
23
#pragma config FWDTEN    = OFF      // Watchdog Timer Enable
24
#pragma config WDTPS     = PS1048576    // Watchdog Timer Postscaler (1048.576s)
24
#pragma config WDTPS     = PS1048576    // Watchdog Timer Postscaler (1048.576s)
25
#pragma config FSRSSEL   = PRIORITY_7   // SRS Interrupt Priority
25
#pragma config FSRSSEL   = PRIORITY_7   // SRS Interrupt Priority
26
#pragma config FCANIO    = OFF      // CAN I/O Pin Select (default/alternate)
26
#pragma config FCANIO    = OFF      // CAN I/O Pin Select (default/alternate)
27
#pragma config FETHIO    = ON       // Ethernet 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)
28
#pragma config FMIIEN    = OFF      // Ethernet MII/RMII select (OFF=RMII)
Line 43... Line 43...
43
#include "TIMER4.h"
43
#include "TIMER4.h"
44
#include "TIMER5.h"
44
#include "TIMER5.h"
45
#include "CUBE.h"
45
#include "CUBE.h"
46
#include "BTN.h"
46
#include "BTN.h"
47
#include "ANIMATIONS.h"
47
#include "ANIMATIONS.h"
-
 
48
#include "CONTROLLERS.h"
-
 
49
#include "SNAKE.h"
48
 
50
 
49
void BTN1_Interrupt(void);
51
void BTN1_Interrupt(void);
50
void BTN2_Interrupt(void);
52
void BTN2_Interrupt(void);
51
void BTN3_Interrupt(void);
53
void BTN3_Interrupt(void);
52
 
54
 
Line 64... Line 66...
64
    uint32_t delay = delay_us * US_TO_CT_TICKS;
66
    uint32_t delay = delay_us * US_TO_CT_TICKS;
65
    uint32_t startTime = ReadCoreTimer();
67
    uint32_t startTime = ReadCoreTimer();
66
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
68
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
67
}
69
}
68
 
70
 
-
 
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
 
69
int32_t main() {
106
void main() {
70
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
107
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
-
 
108
 
-
 
109
    /* -------------------- BEGIN INITIALIZATION --------------------- */
71
    
110
    
72
    /* Configure the target for maximum performance at 80 MHz. */
111
    // Configure the target for maximum performance at 80 MHz.
73
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
112
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
74
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
113
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
75
 
114
 
76
    // Configure the interrupts for multiple vectors
115
    // Configure the interrupts for multiple vectors
77
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
116
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
78
 
117
 
79
    // Set all analog I/O pins to digital
118
    // Set all analog I/O pins to digital
80
    AD1PCFGSET = 0xFFFF;
119
    AD1PCFGSET = 0xFFFF;
81
 
120
 
-
 
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
82
    LED1_TRIS = 0;
126
    LED1_TRIS = 0;
83
    LED2_TRIS = 0;
127
    LED2_TRIS = 0;
84
    LED3_TRIS = 0;
128
    LED3_TRIS = 0;
85
    LED4_TRIS = 0;
129
    LED4_TRIS = 0;
86
    LED1_LAT = 0;
130
    LED1_LAT = 0;
87
    LED2_LAT = 0;
131
    LED2_LAT = 0;
88
    LED3_LAT = 0;
132
    LED3_LAT = 0;
89
    LED4_LAT = 0;
133
    LED4_LAT = 0;
90
 
134
 
-
 
135
    // Initialize a persistent operational state machine
-
 
136
    BOARD_STATE op_state __attribute__((persistent));
-
 
137
 
91
    // Initialize the SPI1 module
138
    // Initialize the SPI1 module
92
    SPI1_DATA spi_1_data;
139
    SPI1_DATA spi_1_data;
93
    SPI1_Init(&spi_1_data, NULL);
140
    SPI1_Init(&spi_1_data, NULL);
94
 
141
 
95
    // Initialize the SPI4 module
142
    // Initialize the SPI4 module
96
    SPI4_DATA spi_4_data;
143
    SPI4_DATA spi_4_data;
97
    SPI4_Init(&spi_4_data);
144
    SPI4_Init(&spi_4_data);
98
 
145
 
-
 
146
    // Initialize the I2C1 module
99
    I2C1_DATA i2c_1_data;
147
    I2C1_DATA i2c_1_data;
100
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
148
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
101
 
149
 
102
    // Initialize the UART1 module
150
    // Initialize the UART1 module
103
    UART1_DATA uart_data;
151
    UART1_DATA uart_data;
104
    UART1_Init(&uart_data, &Cube_Data_In);
152
    UART1_Init(&uart_data, &Cube_Data_In);
105
 
153
 
106
    // Initializs the PWM2 output to 20MHz
154
    // Initializs the PWM2 output to 20MHz
107
    PWM2_Init();
155
    PWM2_Init();
108
    PWM2_Start();
-
 
109
 
156
 
110
    // Initialize the cube variables
157
    // Initialize the cube variables
111
    CUBE_DATA cube_data;
158
    CUBE_DATA cube_data;
112
    Cube_Init(&cube_data, 0x40);
159
    Cube_Init(&cube_data, 0x40);
113
 
160
 
114
    // Start the cube update layer interrupt
161
    // Start the cube update layer interrupt
115
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
162
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
116
    TIMER5_DATA timer_5_data;
163
    TIMER5_DATA timer_5_data;
117
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
164
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
118
    TIMER5_Start();
-
 
119
 
165
 
120
    // Start the overlay rotation interrupt
166
    // Start the controller polling and overlay rotation interrupt
121
    TIMER4_DATA timer_4_data;
167
    TIMER4_DATA timer_4_data;
122
    TIMER4_Init(&timer_4_data, &Cube_Text_Interrupt, 90000);
168
    TIMER4_Init(&timer_4_data, &Controller_Update, NULL, 0);
123
 
169
 
124
    // Process button inputs
170
    // Process button inputs
125
    BTN_DATA btn_data;
171
    BTN_DATA btn_data;
126
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, &BTN3_Interrupt);
172
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, NULL);
-
 
173
 
-
 
174
    CONTROLLER_DATA ctrl_data;
-
 
175
    Controller_Init(&ctrl_data, &op_state, &Controller_Set_Leds);
-
 
176
 
-
 
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
}
127
 
218
 
128
    // Begin display
219
void Idle_Animation_Sequence(void) {
129
 
220
 
130
//    Cube_Set_All(RED);
221
//    Cube_Set_All(RED);
131
//    Delay_MS(2000);
222
//    Delay_MS(2000);
132
//    Cube_Set_All(GREEN);
223
//    Cube_Set_All(GREEN);
133
//    Delay_MS(2000);
224
//    Delay_MS(2000);
134
//    Cube_Set_All(BLUE);
225
//    Cube_Set_All(BLUE);
135
//    Delay_MS(2000);
226
//    Delay_MS(2000);
-
 
227
    Animation_Pseudo_Random_Colors(200);
136
//    Animation_Pseudo_Random_Colors(10,300);
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);
-
 
233
 
-
 
234
    // Start the scrolling text
-
 
235
    TIMER4_Stop();
-
 
236
    TIMER4_Init(NULL, &Controller_Update, &Cube_Text_Interrupt, 100);
-
 
237
    TIMER4_Start();
137
 
238
 
138
//    int8_t start_text[] = "Cube Initialized\r\n";
239
//    int8_t start_text[] = "Cube Initialized\r\n";
139
//    UART1_Write(start_text, 18);
240
//    UART1_Write(start_text, 18);
140
 
241
 
141
    // Set the overlay text
242
    // Set the overlay text
142
    uint8_t text_string[] = "Welcome to the AMP Lab     ";
243
    uint8_t text_string[] = "Welcome to the AMP Lab     ";
143
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
244
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
144
    TIMER4_Start();
-
 
145
 
245
 
146
    // Loop through some preset animations
246
    // Loop through some preset animations
147
    uint8_t buffer1[2];
-
 
148
    uint8_t buffer2[2];
-
 
149
    uint8_t result, length;
-
 
150
    
-
 
151
    while(1) {
247
    while(1) {
152
        I2C1_Master_Restart(0x24, 0xA, 1);
248
//        Animation_Solid_Colors(300);
153
        do {
-
 
154
            result = I2C1_Get_Status();
249
//        Animation_Layer_Alternate(300);
155
        } while (!result);
-
 
156
        if (result == I2C1_RECV_OK) {
-
 
157
            LED1_LAT = 1;
-
 
158
            length = I2C1_Read_Buffer(buffer1);
-
 
159
            buffer1[1] = ~buffer1[0];
-
 
160
            buffer1[0] = 0xB;
-
 
161
        } else {
-
 
162
            LED1_LAT = 0;
-
 
163
        }
-
 
164
        
-
 
165
 
-
 
166
        I2C1_Master_Restart(0x25, 0xA, 1);
-
 
167
        do {
-
 
168
            result = I2C1_Get_Status();
250
//        Animation_Pixel_Alternate(200);
169
        } while (!result);
-
 
170
        if (result == I2C1_RECV_OK) {
-
 
171
            LED2_LAT = 1;
-
 
172
            length = I2C1_Read_Buffer(buffer2);
251
//        Animation_Full_Color_Sweep(1000);
173
            buffer2[1] = ~buffer2[0];
-
 
174
            buffer2[0] = 0xB;
-
 
175
        } else {
-
 
176
            LED2_LAT = 0;
-
 
177
        }
-
 
178
        
-
 
179
        I2C1_Master_Send(0x24, buffer1, 2);
-
 
180
        do {
-
 
181
            result = I2C1_Get_Status();
252
        Animation_Row_Column_Sweep(40);
182
        } while (!result);
-
 
183
        
-
 
184
        I2C1_Master_Send(0x25, buffer2, 2);
-
 
185
        do {
-
 
186
            result = I2C1_Get_Status();
253
        Animation_Row_Column_Sweep(40);
187
        } while (!result);
-
 
188
 
-
 
189
        Delay_MS(1);
-
 
190
        
-
 
191
//        Animation_Solid_Colors(2,300);
254
        Animation_Row_Column_Sweep(40);
192
//        Animation_Layer_Alternate(2,300);
255
        Animation_Cube_In_Cube(300);
193
//        Animation_Pixel_Alternate(1,200);
256
        Animation_Cube_In_Cube(300);
194
//        Animation_Full_Color_Sweep(2,1000);
257
        Animation_Cube_In_Cube(300);
195
//        Animation_Row_Column_Sweep(2,40);
258
        Animation_Double_Rotation(40);
196
//        Animation_Cube_In_Cube(4,300);
259
        Animation_Double_Rotation(40);
197
//        Animation_Double_Rotation(2,40);
260
        Animation_Double_Rotation(40);
198
//        Animation_Pseudo_Random_Colors(10,300);
261
//        Animation_Pseudo_Random_Colors(300);
199
//        Animation_Random_Colors(10,300);
262
//        Animation_Random_Colors(300);
200
 
263
 
201
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
264
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
202
    }
265
    }
203
}
266
}
204
 
267
 
Line 259... Line 322...
259
    }
322
    }
260
    Cube_Write_DCS(BC);
323
    Cube_Write_DCS(BC);
261
    TIMER5_Start();
324
    TIMER5_Start();
262
}
325
}
263
 
326
 
264
// Function call on button 3 press to change text scroll speed
327
//// Function call on button 3 press to change text scroll speed
265
void BTN3_Interrupt(void)  {
328
//void BTN3_Interrupt(void)  {
266
    static uint8_t state;
329
//    static uint8_t state;
267
    state = (state == 4) ? 0 : state + 1;
330
//    state = (state == 4) ? 0 : state + 1;
268
    TIMER4_Stop();
331
//    TIMER4_Stop();
269
    switch (state) {
332
//    switch (state) {
270
        case 0:
333
//        case 0:
271
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
334
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
272
            break;
335
//            break;
273
        case 1:
336
//        case 1:
274
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
337
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
275
            break;
338
//            break;
276
        case 2:
339
//        case 2:
277
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
340
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
278
            break;
341
//            break;
279
        case 3:
342
//        case 3:
280
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
343
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
281
            break;
344
//            break;
282
        case 4:
345
//        case 4:
283
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
346
//            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
284
            break;
347
//            break;
285
    }
348
//    }
286
    TIMER4_Start();
349
//    TIMER4_Start();
287
}
350
//}