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 */
226 Kevin 23
#pragma config FWDTEN    = ON       // Watchdog Timer Enable
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"
193 Kevin 48
 
200 Kevin 49
void BTN1_Interrupt(void);
50
void BTN2_Interrupt(void);
206 Kevin 51
void BTN3_Interrupt(void);
199 Kevin 52
 
231 Kevin 53
void Delay_MS(uint32_t delay_ms) {
201 Kevin 54
    // Delays the CPU for the given amount of time.
55
    // Note: Watch out for integer overflow! (max delay_ms = 107374) ??
231 Kevin 56
    uint32_t delay = delay_ms * MS_TO_CT_TICKS;
57
    uint32_t startTime = ReadCoreTimer();
58
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 59
}
60
 
231 Kevin 61
void Delay_US(uint32_t delay_us) {
201 Kevin 62
    // Delays the CPU for the given amount of time.
63
    // Note: Watch out for integer overflow!
231 Kevin 64
    uint32_t delay = delay_us * US_TO_CT_TICKS;
65
    uint32_t startTime = ReadCoreTimer();
66
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 67
}
68
 
231 Kevin 69
int32_t main() {
226 Kevin 70
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
71
 
193 Kevin 72
    /* Configure the target for maximum performance at 80 MHz. */
199 Kevin 73
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
74
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 75
 
199 Kevin 76
    // Configure the interrupts for multiple vectors
193 Kevin 77
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
78
 
199 Kevin 79
    // Set all analog I/O pins to digital
80
    AD1PCFGSET = 0xFFFF;
193 Kevin 81
 
234 Kevin 82
    LED1_TRIS = 0;
83
    LED2_TRIS = 0;
84
    LED3_TRIS = 0;
85
    LED4_TRIS = 0;
86
    LED1_LAT = 0;
87
    LED2_LAT = 0;
88
    LED3_LAT = 0;
89
    LED4_LAT = 0;
90
 
212 Kevin 91
    // Initialize the SPI1 module
226 Kevin 92
    SPI1_DATA spi_1_data;
93
    SPI1_Init(&spi_1_data, NULL);
199 Kevin 94
 
226 Kevin 95
    // Initialize the SPI4 module
96
    SPI4_DATA spi_4_data;
97
    SPI4_Init(&spi_4_data);
98
 
234 Kevin 99
    I2C1_DATA i2c_1_data;
100
    I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
101
 
212 Kevin 102
    // Initialize the UART1 module
103
    UART1_DATA uart_data;
104
    UART1_Init(&uart_data, &Cube_Data_In);
105
 
106
    // Initializs the PWM2 output to 20MHz
199 Kevin 107
    PWM2_Init();
108
    PWM2_Start();
109
 
212 Kevin 110
    // Initialize the cube variables
199 Kevin 111
    CUBE_DATA cube_data;
206 Kevin 112
    Cube_Init(&cube_data, 0x40);
199 Kevin 113
 
207 Kevin 114
    // Start the cube update layer interrupt
199 Kevin 115
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
206 Kevin 116
    TIMER5_DATA timer_5_data;
117
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
199 Kevin 118
    TIMER5_Start();
119
 
207 Kevin 120
    // Start the overlay rotation interrupt
206 Kevin 121
    TIMER4_DATA timer_4_data;
226 Kevin 122
    TIMER4_Init(&timer_4_data, &Cube_Text_Interrupt, 90000);
206 Kevin 123
 
207 Kevin 124
    // Process button inputs
201 Kevin 125
    BTN_DATA btn_data;
208 Kevin 126
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, &BTN3_Interrupt);
234 Kevin 127
 
201 Kevin 128
    // Begin display
200 Kevin 129
 
208 Kevin 130
//    Cube_Set_All(RED);
226 Kevin 131
//    Delay_MS(2000);
208 Kevin 132
//    Cube_Set_All(GREEN);
226 Kevin 133
//    Delay_MS(2000);
208 Kevin 134
//    Cube_Set_All(BLUE);
226 Kevin 135
//    Delay_MS(2000);
136
//    Animation_Pseudo_Random_Colors(10,300);
207 Kevin 137
 
231 Kevin 138
//    int8_t start_text[] = "Cube Initialized\r\n";
215 Kevin 139
//    UART1_Write(start_text, 18);
212 Kevin 140
 
206 Kevin 141
    // Set the overlay text
231 Kevin 142
    uint8_t text_string[] = "Welcome to the AMP Lab     ";
226 Kevin 143
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
216 Kevin 144
    TIMER4_Start();
206 Kevin 145
 
199 Kevin 146
    // Loop through some preset animations
234 Kevin 147
    uint8_t buffer1[2];
148
    uint8_t buffer2[2];
149
    uint8_t result, length;
150
 
199 Kevin 151
    while(1) {
235 Kevin 152
        I2C1_Master_Restart(0x24, 0xA, 1);
153
        do {
154
            result = I2C1_Get_Status();
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
 
234 Kevin 165
 
166
        I2C1_Master_Restart(0x25, 0xA, 1);
167
        do {
168
            result = I2C1_Get_Status();
169
        } while (!result);
235 Kevin 170
        if (result == I2C1_RECV_OK) {
171
            LED2_LAT = 1;
172
            length = I2C1_Read_Buffer(buffer2);
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();
182
        } while (!result);
183
 
234 Kevin 184
        I2C1_Master_Send(0x25, buffer2, 2);
235 Kevin 185
        do {
186
            result = I2C1_Get_Status();
187
        } while (!result);
234 Kevin 188
 
189
        Delay_MS(1);
190
 
207 Kevin 191
//        Animation_Solid_Colors(2,300);
192
//        Animation_Layer_Alternate(2,300);
193
//        Animation_Pixel_Alternate(1,200);
194
//        Animation_Full_Color_Sweep(2,1000);
234 Kevin 195
//        Animation_Row_Column_Sweep(2,40);
196
//        Animation_Cube_In_Cube(4,300);
197
//        Animation_Double_Rotation(2,40);
226 Kevin 198
//        Animation_Pseudo_Random_Colors(10,300);
207 Kevin 199
//        Animation_Random_Colors(10,300);
226 Kevin 200
 
201
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
199 Kevin 202
    }
193 Kevin 203
}
204
 
200 Kevin 205
// Function call on button 1 press to change refresh rate
206
void BTN1_Interrupt(void) {
231 Kevin 207
    static uint8_t state;
206 Kevin 208
    state = (state == 4) ? 0 : state + 1;
200 Kevin 209
    TIMER5_Stop();
210
    switch (state) {
211
        case 0:
207 Kevin 212
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
200 Kevin 213
            break;
214
        case 1:
207 Kevin 215
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
200 Kevin 216
            break;
217
        case 2:
207 Kevin 218
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
200 Kevin 219
            break;
220
        case 3:
207 Kevin 221
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
200 Kevin 222
            break;
206 Kevin 223
        case 4:
207 Kevin 224
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
206 Kevin 225
            break;
200 Kevin 226
    }
227
    TIMER5_Start();
228
}
229
 
230
// Function call on button 2 press to change brightness
231
void BTN2_Interrupt(void) {
231 Kevin 232
    static uint8_t state;
205 Kevin 233
    state = (state == 6) ? 0 : state + 1;
200 Kevin 234
    TIMER5_Stop();
235
    Delay_MS(1); // Need to wait for all SPI writes to complete
231 Kevin 236
    uint8_t BC;
200 Kevin 237
    switch (state) {
238
        case 0:
239
            BC = 0x01;
240
            break;
241
        case 1:
242
            BC = 0x08;
243
            break;
244
        case 2:
245
            BC = 0x10;
246
            break;
247
        case 3:
248
            BC = 0x20;
249
            break;
250
        case 4:
251
            BC = 0x40;
252
            break;
253
        case 5:
205 Kevin 254
            BC = 0x80;
200 Kevin 255
            break;
205 Kevin 256
        case 6:
257
            BC = 0xFF;
258
            break;
200 Kevin 259
    }
260
    Cube_Write_DCS(BC);
261
    TIMER5_Start();
262
}
263
 
206 Kevin 264
// Function call on button 3 press to change text scroll speed
265
void BTN3_Interrupt(void)  {
231 Kevin 266
    static uint8_t state;
206 Kevin 267
    state = (state == 4) ? 0 : state + 1;
268
    TIMER4_Stop();
269
    switch (state) {
270
        case 0:
271
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
272
            break;
273
        case 1:
274
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
275
            break;
276
        case 2:
277
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
278
            break;
279
        case 3:
207 Kevin 280
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
206 Kevin 281
            break;
282
        case 4:
207 Kevin 283
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
206 Kevin 284
            break;
285
    }
286
    TIMER4_Start();
287
}