1 void SystemSetup(void) 2 { 3 //disable the interrupts 4 __builtin_disable_interrupts(); 5 6 // System unlock Sequence 7 SYSKEY = 0xAA996655; 8 SYSKEY = 0x556699AA; 9 10 //Init USB module 11 USB_init(); 12 13 // Set multi vector interrupt mode 14 INTCONbits.MVEC = 1; 15 16 unsigned int cp0; 17 18 ANSELA = 0; 19 ANSELB = 0; 20 ANSELC = 0; 21 ANSELD = 0; 22 ANSELE = 0; 23 ANSELF = 0; 24 ANSELG = 0; 25 26 //Buzzer control pin 27 TRISAbits.TRISA1 = 0; 28 29 //This pin is used by the peripherals to indicate they need serviced 30 TRISAbits.TRISA7 = 1; 31 32 //LED Port 33 TRISFbits.TRISF3 = 0; 34 TRISFbits.TRISF2 = 0; 35 TRISFbits.TRISF8 = 0; 36 TRISAbits.TRISA6 = 0; 37 TRISAbits.TRISA14 = 0; 38 TRISAbits.TRISA15 = 0; 39 TRISDbits.TRISD10 = 0; 40 TRISDbits.TRISD11 = 0; 41 42 //RC13 - Secondary OSC in 43 TRISCbits.TRISC13 = 1; 44 45 //RC15 - Pin 50 46 //Heartbeat out (TP1)) 47 TRISCbits.TRISC15 = 0; 48 49 //RG13 50 // /ACK from peripherals 51 TRISGbits.TRISG13 = 1; 52 53 //RG6 54 //TP2 55 TRISGbits.TRISG6 = 0; 56 57 //RA7 58 // /INT from peripheral 59 TRISAbits.TRISA7 = 1; 60 61 //Peripherals address setup 62 // A0 - PORTD.15 63 TRISDbits.TRISD15 = 0; 64 PORTDbits.RD15 = 0; 65 66 // A1 - PORTF.13 67 TRISFbits.TRISF13 = 0; 68 PORTFbits.RF13 = 0; 69 70 TRISDbits.TRISD14 = 0; 71 // A2 - PORTD.14 72 PORTDbits.RD14 = 0; 73 74 /********************************************************* 75 / I/O cycle change notice for RG13 76 /*********************************************************/ 77 IPC31bits.CNGIP = 7; 78 IPC31bits.CNGIS = 2; 79 80 CNCONGbits.ON = 1; 81 CNCONGbits.EDGEDETECT = 1; 82 CNNEGbits.CNNEG13 = 1; 83 IEC3bits.CNGIE = 1; 84 85 86 IFS3bits.CNGIF = 0; 87 88 //Pin Mapping 89 //Maps OC5 to pin 90 RPD4R = 0x0b; 91 92 //USB 93 //This allows the usage of RF3 as I/O while still using the USB module 94 //Device mode only 95 USBCRCONbits.USBIDOVEN = 1; 96 USBCRCONbits.USBIDVAL = 1; 97 98 PRISS = 0x76543210; 99 100 //PBCLK1 - System Clock 101 // Peripheral Bus 1 cannot be turned off, so there's no need to turn it on 102 PB1DIVbits.PBDIV = 0; // Peripheral Bus 1 Clock Divisor Control (PBCLK1 is SYSCLK divided by 1) 103 104 //PBCLK2 - PMP 105 PB2DIVbits.PBDIV = 1; // Peripheral Bus 2 Clock Divisor Control (PBCLK2 is SYSCLK divided by 1) 106 PB2DIVbits.ON = 1; // Peripheral Bus 2 Output Clock Enable (Output clock is enabled) 107 while (!PB2DIVbits.PBDIVRDY); 108 109 //PBCLK3 - Feeds TMR1 110 PB3DIVbits.PBDIV = 0; 111 PB3DIVbits.ON = 1; 112 while (!PB3DIVbits.PBDIVRDY); 113 114 //PB4DIV 115 PB4DIVbits.PBDIV = 1; // Peripheral Bus 4 Clock Divisor Control (PBCLK4 is SYSCLK divided by 1) 116 PB4DIVbits.ON = 1; // Peripheral Bus 4 Output Clock Enable (Output clock is enabled) 117 while (!PB4DIVbits.PBDIVRDY); // Wait until it is ready to write to 118 119 //PB5DIV 120 PB5DIVbits.PBDIV = 1; // Peripheral Bus 5 Clock Divisor Control (PBCLK5 is SYSCLK divided by 1) 121 PB5DIVbits.ON = 1; // Peripheral Bus 5 Output Clock Enable (Output clock is enabled) 122 while (!PB5DIVbits.PBDIVRDY); // Wait until it is ready to write to 123 124 // PB7DIV 125 PB7DIVbits.PBDIV = 0; // Peripheral Bus 7 Clock Divisor Control (PBCLK7 is SYSCLK divided by 1) 126 PB7DIVbits.ON = 1; // Peripheral Bus 7 Output Clock Enable (Output clock is enabled) 127 while (!PB7DIVbits.PBDIVRDY); // Wait until it is ready to write to 128 129 //PB8DIV 130 PB8DIVbits.PBDIV = 1; // Peripheral Bus 8 Clock Divisor Control (PBCLK8 is SYSCLK divided by 1) 131 PB8DIVbits.ON = 1; // Peripheral Bus 8 Output Clock Enable (Output clock is enabled) 132 while (!PB8DIVbits.PBDIVRDY); // Wait until it is ready to write to 133 134 //PRECON - Set up prefetch 135 PRECONbits.PFMSECEN = 0; // Flash SEC Interrupt Enable (Do not generate an interrupt when the PFMSEC bit is set) 136 PRECONbits.PREFEN = 3; // Predictive Prefetch Enable (Enable predictive prefetch for any address) 137 PRECONbits.PFMWS = 2; // PFM Access Time Defined in Terms of SYSCLK Wait States (Two wait states) 138 139 CFGCONbits.USBSSEN = 1; 140 141 // Set up caching 142 cp0 = _mfc0(16, 0); 143 cp0 &= ~0x07; 144 cp0 |= 0b011; // K0 = Cacheable, non-coherent, write-back, write allocate 145 _mtc0(16, 0, cp0); 146 147 while(CLKSTATbits.DIVSPLLRDY == 0); 148 149 //Real time clock init 150 RTCC_init(); 151 152 //Increase POSC freq 153 //56 seems to be the highest stable value 154 SetFreqPOSC(57); 155 156 //Lock system 157 SYSKEY = 0x33333333; 158 }