📄 flexcan.c
字号:
printf("NO.%d transmit compelet!\n",mb_no);
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;
}
}
printf("NO.%d transmit overtime!\n",mb_no);
printf("MCF_CAN_IFLAG=%x\n",MCF_CAN_IFLAG);
printf("MCF_CAN_MB_CS(%d)=%x\n",mb_no,MCF_CAN_MB_CS(mb_no));
printf("MCF_CAN_ERRSTAT=%x\n",MCF_CAN_ERRSTAT);
printf("MCF_CAN_ERRCNT=%x\n",MCF_CAN_ERRCNT);
*err = 1;
MCF_CAN_MB_CS(mb_no) = (0x00000008)<<24;//set transmit inactive
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;
}
}
/****************************************************************
*function: read data from mb_no
*err : 0-done right 1-done wrong
**length :return the receive data of length
*******************************************************************/
void read_data(char mb_no,unsigned char *buf,unsigned char *length,char *err)
{
uint32 i;
*err = 0;
MCF_CAN_IFLAG = MCF_CAN_IFLAG;//clear the flag
//for debug
#ifdef debug_can
printf("enter read data function!\n");
#endif
if(mb_no > 15 || mb_no < 0)
{
printf("\nmb_no = %d is not valid!\n",mb_no);
*err = 1;
return;
}
printf("receive data form mb%d \n ",mb_no);
while( MCF_CAN_MB_CS(mb_no) & 0x01000000 );//wait for no busy
i = MCF_CAN_MB_CS(mb_no);//read C/S, to lock the mb
*length = (char)( (i & MCF_CAN_MB_LEN_MASK)>>16 );//get the length of mb
if( (MCF_CAN_MB_CS(mb_no) & MCF_CAN_MB_IDE_MASK) == 0)//if use standard frame
{
/*
if( (MCF_CAN_MB_ID(mb_no) & 0x1FFC0000) != id<< 18)//id not march
{
printf("standard ID don't match!\n");
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;
}
*/
printf("match standard format!\n");
//printf("the marched standard id = %x(16 format)\n",(MCF_CAN_MB_ID(mb_no) & 0x1FFC0000)>>18);
printf("the marched standard id = %x(16 format)\n",(MCF_CAN_MB_ID(mb_no) & 0x1FFC0000));
}
else//use extended frame format
{
/*
if( (MCF_CAN_MB_ID(mb_no) & 0x1FFFFFFF) != id )
{
printf("extended ID don't match!\n");
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;
}
*/
printf("match extend format!\n");
printf("the marched extend id = %x(16 format)\n",(MCF_CAN_MB_ID(mb_no) & 0x1FFC0000));
}
//release the mb,read the free-running timer
//i = MCF_CAN_TIMER;
//for debug
#ifdef debug_can
printf("begin to read the NO.%d mb of datafield\n",mb_no);
#endif
//id matched
for (i = 0;i<*length;i++)
{
*(buf+i) = MCF_CAN_MB_DATAFIELDS(mb_no,i);//copy the bytes from mb to buf
//for debug
#ifdef debug_can
printf("buf[%d]=%d\n",i,*(buf+i));
#endif
}
//for debug
#ifdef debug_can
printf("end of read datafield!\n");
#endif
MCF_CAN_IFLAG = 1<< mb_no;//receive compelet
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;
}
/****************************************************************
*function: change id
*
*******************************************************************/
void change_id(char mb_no,uint32 id)
{
MCF_CAN_CANMCR |= MCF_CAN_CANMCR_FRZ;//freeze mode enable
MCF_CAN_CANMCR |= MCF_CAN_CANMCR_HALT;//enter freeze mode
while( (MCF_CAN_CANMCR & MCF_CAN_CANMCR_FRZACK) == 0);//wait until have enter freeze mode
MCF_CAN_MB_ID(mb_no) = id;
MCF_CAN_CANMCR &= ~MCF_CAN_CANMCR_HALT;//exit freeze mode
}
/****************************************************************
*function: change C/S od mb_no
*
*******************************************************************/
void change_mb_cs(char mb_no,char code,char srr,char ide,char rtr)
{
uint32 i = 0;
//if mb is active ,we should disable the module before we change the C/S
if( ( (MCF_CAN_MB_CS(mb_no) & 0x0F000000) != MCF_CAN_MB_CODE_RX_INACTIVE)
&& ( (MCF_CAN_MB_CS(mb_no) & 0x0F000000) != MCF_CAN_MB_CODE_TX_INACTIVE))
{
if( (MCF_CAN_MB_CS(mb_no) & 0x0F700000) == (0 | code<<24 | srr<<22 | ide <<21 | rtr<<20))
{
printf("without change C/S \n");
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;//without change
}
else//change
{
MCF_CAN_CANMCR |= MCF_CAN_CANMCR_FRZ;//freeze mode enable
MCF_CAN_CANMCR |= MCF_CAN_CANMCR_HALT;//enter freeze mode
while( (MCF_CAN_CANMCR & MCF_CAN_CANMCR_FRZACK) == 0);//wait until have enter freeze mode
//change the C/S
MCF_CAN_MB_CS(mb_no) &= (~0x0FF0000);
MCF_CAN_MB_CS(mb_no) |= ( (code<<24) | (srr<<22) | (ide<<21) | (rtr<<20));
MCF_CAN_CANMCR &= ~MCF_CAN_CANMCR_HALT;//exit freeze mode
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;
}
}
else//is inactive ,so we can change it directly
{
//change the C/S
MCF_CAN_MB_CS(mb_no) &= ~0x0FF0000;
MCF_CAN_MB_CS(mb_no) |= ( (code<<24) | (srr<<22) | (ide<<21) | (rtr<<20));
//release the mb,read the free-running timer
i = MCF_CAN_TIMER;
return;
}
}
/*****************************************************************
*ucos-ii test task for FlexCAN
*******************************************************************/
OS_STK AppTaskFlexCANStk[256];
static unsigned char can_buf_data[10]=" 1 2 a b \0";
static unsigned char *can_buf = can_buf_data;
static unsigned char buf_rec[10]={0};
static unsigned char *rec_buf = buf_rec;
static char err = 0;
static unsigned char length = 0;
void AppTaskFlexCAN (void * pdata)
{
uint16 i=0;
uint16 flag = 0;
length = 8;
FlexCAN_init();
button_value_current = 0;
transmit_again:
printf("\nPlease select the TX and RX: ");
printf("SW2--- TX data; SW3--- RX data.\n");
while(button_value_current == 0);
switch(button_value_current)
{
case 2 ://pressed sw2 ,into transmit data
printf("the data length is= %d\n",length);
printf("the transmit data is :");
for(i=0; i<8; i++)
{
printf("%d ",can_buf[i]);
}
printf("END\n\n");
//void transmit_data(char mb_no,uint32 id,char *databuf,char data_length,char transmit_code,char rtr,char *err)
transmit_data(1,0b10101010101<<18,can_buf,length,0b1100,0,0,&err);//transmit 8bytes from mb1 to mb7 ,then into inactive
if(err == 1)
{
printf("transmit wrong!\n");
button_value_current = 0;
goto transmit_again;
break;
}
//for debug
printf("waiting for receiving data!\n\n\n");
#ifdef debug_can
printf("MCF_CAN_IFLAG=%x\n",MCF_CAN_IFLAG);
printf("MCF_CAN_CANCTRL=%x\n",MCF_CAN_CANCTRL);
printf("MCF_CAN_CANMCR=%x\n",MCF_CAN_CANMCR);
printf("MCF_CAN_MB_CS(1)=%x\n",MCF_CAN_MB_CS(1));
printf("MCF_CAN_MB_CS(7)=%x\n",MCF_CAN_MB_CS(7));
#endif
while( !MCF_CAN_IFLAG);//wait for receive
//get the flag number
for(flag = 0; (MCF_CAN_IFLAG>>flag) != 1;flag++);
err = 0;
printf("begin receive the data from MB%d!\n",flag);
//void read_data(char mb_no,uint32 id,char *buf,char *err)
read_data(flag,buf_rec,&length,&err);//receive mb7
printf("the data length is= %d\n",length);
printf("the received data is :");//printf the received mb
for(i = 0;i < 8 ;i++)
printf("%d ",buf_rec[i]);
printf("END\n");
button_value_current = 0;
goto transmit_again;
case 3://pressed sw3,into receive data
while(1)
{
printf("waiting for receiving data!\n");
while( MCF_CAN_IFLAG == 0)
{
}//wait for receive
//get the flag number
for(flag = 0; (MCF_CAN_IFLAG>>flag) != 1;flag++);
err = 0;
//void read_data(char mb_no,uint32 id,char *buf,char *err)
read_data(flag,rec_buf,&length,&err);//receive mb7
printf("the data length is= %d\n",length);
printf("\nthe received data is :");//printf the received mb
for(i = 0;i < 8 ;i++)
printf("%d ",*(rec_buf+i) );
printf("END\n\n");
printf("begin to transmit data!\n");
printf("the data length is= %d\n",length);
printf("the transmit data is :");
for(i=0; i<8; i++)
{
printf("%d ",can_buf[i]);
}
printf("END\n\n");
//void transmit_data(char mb_no,uint32 id,char *databuf,char data_length,char transmit_code,char rtr,char *err)
transmit_data(1,7<<18,can_buf,8,0b1100,0,0,&err);//transmit 8bytes from mb1 to mb7 ,then into inactive
if(err == 1)
{
printf("transmit wrong!\n");
button_value_current = 0;
goto transmit_again;
break;
}
}
default:
printf("pressed the wrong number!\n");
break;
}
while(1)
{
OSTimeDly(60);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -