/** * Initialize the PLL0 to get a better frequency out * of the internal oscillator. */ void initClock( void ) { // Do this here because of errata sheet of the processor, // you need to set the PCLKSEL before enabling PLL0 LPC_SC->PCLKSEL0 &= ~(3<<28); // Clear the 2 CAN clock divider bits for can peripheral 2 LPC_SC->PCLKSEL0 |= (1<<28); // Set the clock divider for CAN peripheral 2 to 1, // so the main clock is the CAN clock LPC_SC->PCLKSEL0 &= ~(3<<30); // Clear the 2 clock divider bits for the can acceptance filter LPC_SC->PCLKSEL0 |= (1<<30); // Set the clock divider for acceptance filter to 1 // Folow the setup sequence from the user manual // Disconnect PLL0 if it was connected if( LPC_SC->PLL0STAT & (1<<25) ) { LPC_SC->PLL0CON &= ~(1<<1); clockFeed(); } // Disable PLL0 LPC_SC->PLL0CON = 0; clockFeed(); // Set the CPU divider to 1 LPC_SC->CCLKCFG = 0; // Set the clock source for PLL0 to the IRC LPC_SC->CLKSRCSEL = 0; // Set the Multiplier for PLL0 and load it into // PLL0 via a feed LPC_SC->PLL0CFG = 59; clockFeed(); // Enable PLL0 LPC_SC->PLL0CON = (1<<0); // Enable the PLL0 clockFeed(); // Set the CPU divide to the divider you are going // to use with the PLL0 connected LPC_SC->CCLKCFG = 4; // Wait for the PLL0 to lock on the frequency while( !(LPC_SC->PLL0STAT & (1<<26)) ); // Connect PLL0 to the CPU block LPC_SC->PLL0CON = (1<<0) | (1<<1); // Connect the PLL0 (and keep enabled) clockFeed(); } |
/** * Setup the CAN peripheral. * * The pins for the 2th CAN peripheral are set to the right * mode, the power and clock are setup for the CAN peripheral * and CAN peripheral 2 is cleared and setup (timing and such). */ void initCan( void ) { // Set the processor pins to the correct mode LPC_PINCON->PINSEL0 &= ~(0x2<<8); // Reset the function of Pin 0.4 LPC_PINCON->PINSEL0 |= (0x2<<8); // Set the function of Pin 0.4 to RD2 LPC_PINCON->PINSEL0 &= ~(0x2<<10); // Reset the function of Pin 0.5 LPC_PINCON->PINSEL0 |= (0x2<<10); // Set the function of Pin 0.5 to TD2 // Setup the power and clock for the peripheral LPC_SC->PCONP |= (1<<14); // Set the power bit for CAN peripheral 2 // Setup peripheral related settings LPC_CAN2->MOD = 0x1; // Set the CAN peripheral in reset mode so you can change values LPC_CAN2->IER = 0x0; // Turn off all CAN related interrupts, they would not fire anyway LPC_CAN2->GSR = 0x0; // Set the error counters to 0 // Clear everything you can via the command register LPC_CAN2->CMR = (0x1<<1) | // Abort transmission bit (0x1<<2) | // Release receive buffer (0x1<<3); // Clear data overrun bit LPC_CAN2->BTR = 0xEFC027; // Set the bit rate of the CAN peripheral to 100kbit/s // BRP = 40 // SJW = 3 // TESG1 = 15 // TESG2 = 6 // SAM = 1 LPC_CAN2->MOD = 0; // Enable the CAN peripheral again LPC_CANAF->AFMR |= (1<<1); // Set the acceptance filter in bypass mode } |