CAN:
///////////////////////////////////////////////////////////////////////////////////////// // // Sample for Freescale EVB9S08DZ60 // //-------------------------------------------------------------------------------------- // // This project has been written for CodeWarrior 5.0 for HC(S)08 // //-------------------------------------------------------------------------------------- // // Copyright (c) 2006 SofTec Microsystems // http://www.softecmicro.com/ // /////////////////////////////////////////////////////////////////////////////////////////
#include "derivative.h" /* include peripheral declarations */ #include "mscan.h"
///////////////////////////////////////////////////////////////////////////////////////// // MSCANInit // -------------------------------------------------------------------------------------- // MSCAN Peripheral Initialization ///////////////////////////////////////////////////////////////////////////////////////// void MSCANInit(void) {
char sj,p; char t_seg1, t_seg2;
if (!CANCTL0&0x01) // Active MSCAN initialization mode CANCTL0 =0x01; // Wait until the MSCAN is in initialization mode while (!CANCTL1&0x01) ; sj = (SJW-1)<<6; p = (BRP-1); // Configures SJW and Tq clock Baud Rate Prescaler CANBTR0 = (sj|p); t_seg1 = (TSEG1-1); t_seg2 = (TSEG2-1)<<4; // Configure Time Segment 1 and 2, and one Sample per bit CANBTR1 = (t_seg1 | t_seg2); // Disables all the Filters CANIDMR0 = 0xFF; CANIDMR1 = 0xFF; CANIDMR2 = 0xFF; CANIDMR3 = 0xFF; CANIDMR4 = 0xFF; CANIDMR5 = 0xFF; CANIDMR6 = 0xFF; CANIDMR7 = 0xFF; // Enable MSCAN and normal operation and select the oscillator clock as MSCAN clock source CANCTL1 = 0x80; // Active MSCAN Normal Operation CANCTL0 = 0x00; // Wait until the MSCAN operates normally while(CANCTL1&0x01) ; // Wait until MSCAN is synchronized to the CAN bus while(!(CANCTL0&0x10)) ; }
///////////////////////////////////////////////////////////////////////////////////////// // MSCANSendMsg // -------------------------------------------------------------------------------------- // MSCAN Send Message Routine ///////////////////////////////////////////////////////////////////////////////////////// Bool MSCANSendMsg(struct can_msg msg) {
unsigned char n_tx_buf, i; // Checks len validity if(msg.len > MAX_LEN) return(FALSE);
// Checks synchronization to CAN bus if(!(CANCTL0&0x10)) return(FALSE);
n_tx_buf = 0; do { // Looks for a free buffer CANTBSEL=CANTFLG; n_tx_buf=CANTBSEL; }while(!n_tx_buf); // Write Identifier CANTIDR0 = (unsigned char)(msg.id>>3); CANTIDR1 = (unsigned char)(msg.id<<5); if(msg.RTR) // RTR=Recessive CANTIDR1 |= 0x10; // Write Data Segment for(i = 0; i < msg.len; i++) *((&CANTDSR0)+i) = msg.data; // Write Data Length CANTDLR = msg.len; // Write Priority CANTTBPR = msg.prty; // Clear TXx Flag (buffer ready to transmission) CANTFLG = n_tx_buf; return(TRUE); }
///////////////////////////////////////////////////////////////////////////////////////// // MSCANGetMsg // -------------------------------------------------------------------------------------- // MSCAN Get Message Routine ///////////////////////////////////////////////////////////////////////////////////////// Bool MSCANGetMsg(struct can_msg *msg) { unsigned char i;
// Checks for received messages if(!(CANRFLG&0x01)) return(FALSE); // Checks if message has standard identifier if(CANRIDR1&0x08) // IDE = Recessive (Extended Mode) return(FALSE);
// Reads message msg->id = (unsigned int)(CANRIDR0<<3) | (unsigned char)(CANRIDR1>>5); if(CANRIDR1&0x10) msg->RTR = TRUE; else msg->RTR = FALSE; // Read Data Length msg->len = CANRDLR; // Read Data Segment for(i = 0; i < msg->len; i++) msg->data = *((&CANRDSR0)+i);
// Clear RXF flag (buffer ready to be read) CANRFLG = 0x01; return(TRUE); }
///////////////////////////////////////////////////////////////////////////////////////// // MSCANCheckRcvdMsg // -------------------------------------------------------------------------------------- // Verify if a new message is available in the Receiver Buffer Register ///////////////////////////////////////////////////////////////////////////////////////// Bool MSCANCheckRcvdMsg(void) {
// Checks for received messages if(CANRFLG&0x01) return(TRUE); else return(FALSE); }
|