now I've got the following code (using a 4MHz crystal installed on the eval-board, CAN-baud = 500kbit/s) which seems to be working fine in the full-chip-simulation mode:
Code:
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations *//*----------------------------------------------------------definitions ------------------------------------------------ -----------------------------------------------------------*/typedef unsigned char UINT8;typedef signed char SINT8;typedef unsigned int UINT16;typedef int SINT16;typedef unsigned long int UINT32;typedef long int SINT32;/*----------------------------------------------------------Interrupt Vector Numbers definitions---------------------------------------------------------------------------------- */#define ISR_VEC_TPM1OVF 11#define ISR_VEC_ADC 23#define ISR_VEC_CANTX 29/*----------------------------------------------------------define LED's --------------------------------------------------------------------------------------------------------- */#define LED2 PTBD_PTBD3#define LED1 PTCD_PTCD5/*----------------------------------------------------------globals -------------------------------------------------------------------------------------------------------------- */UINT8 tx_buf;/*----------------------------------------------------------ISR CAN_TX------------------------------------------------------------------------------------------------------------ */interrupt ISR_VEC_CANTX void intCANtx(void) {LED1 ^= 1; //toggle LED1CANTIER_TXEIE0 = 0;}/*----------------------------------------------------------init CAN-------------------------------------------------------------------------------------------------------------- */void initCAN(void){UINT32 Count;UINT16 idmask; /* ### Init_MSCAN init code */ /* CANCTL0: INITRQ=1 */ CANCTL0 |= (unsigned char)0x01; while(!CANCTL1_INITAK) { /* Wait for init acknowledge */ } /* CANCTL1: CANE=1,CLKsrc=0,LOOPB=0,LISTEN=0,BORM=0,WUPM=0,SLPAK=0,INITAK=1 */ CANCTL1 = 0x91; CANCTL1_LISTEN = 0; /* CANBTR1: SAMP=0,TSEG22=0,TSEG21=0,TSEG20=0,TSEG13=0,TSEG12=0,TSEG11=0,TSEG10=1 */ CANBTR1 = 0x01; /* CANBTR0: SJW1=0,SJW0=0,BRP5=0,BRP4=0,BRP3=0,BRP2=0,BRP1=0,BRP0=1 */ CANBTR0 = 0x01; /* CANIDAC: IDAM1=0,IDAM0=1,IDHIT2=0,IDHIT1=0,IDHIT0=0 */ CANIDAC = 0x10; CANIDAC_IDAM0=1; /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DMR5 = 0x00; /* CANIDMR6: AM7=0,AM6=0,AM5=0,AM4=0,AM3=0,AM2=0,AM1=0,AM0=0 */ CANIDMR6 = 0x00; /* CANIDMR7: AM7=0,AM6=0,AM5=0,AM4=0,AM3=0,AM2=0,AM1=0,AM0=0 */ CANIDMR7 = 0x00; /* CANCTL0: INITRQ=0 */ CANCTL0 &= (unsigned char)~0x01; while(CANCTL1_INITAK) { /* Wait for init exit */ } /* CANCTL0: RXFRM=0,RXACT=0,CSWAI=0,SYNCH=0,TIME=0,WUPE=0,SLPRQ=0,INITRQ=0 */ CANCTL0 = 0x00; /* CANRIER: WUPIE=0,CSCIE=0,RSTATE1=0,RSTATE0=0,TSTATE1=0,TSTATE0=0,OVRIE=0,RXFIE=0 */ CANRIER = 0x00; /* CANTIER: TXEIE2=0,TXEIE1=0,TXEIE0=1 */ CANTIER = 0x01; /* ### */ } //end initCAN/*----------------------------------------------------------send a messagevia CAN------------------------------------------------------------------------------------------------- */void sendCAN_message(void){ UINT8 data[8],dlen; //tx_buf; UINT16 id; //configure data of CAN-frame id = 0x0301; data[0]= 0x0A; data[1]= 0x0B; data[2]= 0x0C; data[3]= 0x0D; data[4]= 0x0E; data[5]= 0x0F; data[6]= 0xEE; data[7]= 0xFF; dlen = 8; //select background buffer CANTBSEL=CANTFLG; tx_buf = CANTBSEL; //set ID for standard frames CANTIDR0 = id>>3; CANTIDR1 = id<<5; //RTR = 0; IDE = 0; ==> standard frames CANTIDR1_IDE = 0; //standard frame CANTIDR1_SRR = 0; //SRR = RTR for standard frames CANTIDR2;// = 0x00; CANTIDR3;// = 0x00; //set frame-data CANTDSR0 = data[0]; CANTDSR1 = data[1]; CANTDSR2 = data[2]; CANTDSR3 = data[3]; CANTDSR4 = data[4]; CANTDSR5 = data[5]; CANTDSR6 = data[6]; CANTDSR7 = data[7]; //set data length code CANTDLR = dlen; //set buffer priority CANTTBPR = 0x01; //enable TX-Interrupt for this buffer //CANTIER |= tx_buf; //start transmit data by clearing TXE0 bit CANTFLG_TXE0 = 0; //enabling transmit interrupt
CANTIER_TXEIE0 = 1;}/*----------------------------------------------------------main entry point ----------------------------------------------------------------------------------------------------- */void main(void) { UINT16 i; UINT8 aux; // ---------------- MCU init ------------------------------------------- /* ### MC9S08DZ60_64 "Cpu" init code ... */ /* PE initialization code after reset */ /* Common initialization of the write once registers */ /* SOPT1: COPT=0,STOPE=0,SCI2PS=0,IICPS=0 */ SOPT1 = 0x00; /* SOPT2: COPCLKS=0,COPW=0,ADHTS=0,MCSEL=0 */ SOPT2 = 0x00; /* SPMSC1: LVWF=0,LVWACK=0,LVWIE=0,LVDRE=1,LVDSE=1,LVDE=1,BGBE=0 */ SPMSC1 = 0x1C; /* SPMSC2: LVDV=0,LVWV=0,PPDF=0,PPDACK=0,PPDC=0 */ SPMSC2 = 0x00; /* System clock initialization */ MCGTRM = *(unsigned char*far)0xFFAF; /* Initialize MCGTRM register from a non volatile memory */ MCGSC = *(unsigned char*far)0xFFAE; /* Initialize MCGSC register from a non volatile memory */ /* MCGC2: BDIV=3,RANGE=1,HGO=0,LP=0,EREFS=0,ERCLKEN=1,EREFSTEN=0 */ MCGC2 = 0xE2; /* Set MCGC2 register */ /* MCGC1: CLKS=0,RDIV=0,IREFS=1,IRCLKEN=0,IREFSTEN=0 */ MCGC1 = 0x04; /* Set MCGC1 register */ /* MCGC3: LOLIE=0,PLLS=0,CME=0,VDIV=1 */ MCGC3 = 0x01; /* Set MCGC3 register */ while(!MCGSC_LOCK) { /* Wait until FLL is locked */ } // ---------------- MCU init ------------------------------------------- PTBDD_PTBDD3 = 1; // configure Ports with green led as outputs PTCDD_PTCDD5 = 1; LED1 = 1; //turn off leds LED2 = 1; initCAN(); EnableInterrupts; /* enable interrupts */ /* include your code here */ for(;;) { for (i=8000; i!=0; i--) { //dummy action aux = i>>4; } sendCAN_message(); __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave main */}
1. In debug-mode with the P&E Multilink interface, when stepping through the code, the INITAK-bit is never cleared, so the programm is hanging up in this line of the initialization function:
Is that only because of the BDM running mode? Does it clear in normal running mode?
2. Does the interrupt enabled in the following line occur upon transmission complete?
Tomorrow I will test if the uC is really sending any message on the bus (using the CANalyzer). With the above code the simulation works. Let's see what the hardware is doing...