【BIGTREETECH PI开发板】 CAN收发测试
[复制链接]
测试下使用BIGTREETECH PI开发板测试CAN模块收发通信测试。
一、硬件连接
开发板连接CAN模块使用了下面接口
硬件连接图
二、程序代码
mcp2515.c
#include <stdio.h>
#include <wiringPi.h>
#include <mcp2515.h>
#include <unistd.h>
#include <stdint.h>
#define SCK 0 //PC3
#define SI 1 //pc0
#define SO 2 //PC7
#define CAN_CS 5 //PC14
#define CAN_RST 7 //PC12
#define CAN_INT 8 //PC10
uint8_t can_tx_flag=0;
uint8_t can_rx_flag=0;
uint8_t spi_read(void)
{
uint8_t n=8;
uint8_t tdata=0;
digitalWrite (SCK, LOW) ;
while(n--)
{
usleep(1);
digitalWrite (SCK, HIGH) ;
usleep(1);
tdata=(tdata<<1);
if(digitalRead(SO)==HIGH)
tdata=tdata|0x01;
else
tdata=tdata&0xfe;
digitalWrite (SCK, LOW) ;
}
return tdata;
}
void spi_write(uint8_t ch)
{
int n=8;
digitalWrite (SCK, LOW) ;
while(n--)
{
digitalWrite (SCK, LOW) ;
if((ch&0x80)==0x80)
{
digitalWrite (SI, HIGH) ;
}
else
{
digitalWrite (SI, LOW) ;
}
digitalWrite (SCK, HIGH) ;
ch=ch<<1;
}
}
void mcp2515_reset(void)
{
digitalWrite (CAN_RST, LOW) ;
spi_write(CAN_RESET);
digitalWrite (CAN_RST, HIGH) ;
}
void mcp2515_bitmodity(uint8_t addr, uint8_t msk, uint8_t dat)
{
digitalWrite (CAN_CS, LOW);
spi_write(CAN_BIT_MODIFY);
spi_write(addr);
spi_write(msk);
spi_write(dat);
digitalWrite (CAN_CS, HIGH);
}
void spi_writebyte(uint8_t adr, uint8_t dat)
{
digitalWrite (CAN_CS, LOW) ;
spi_write(CAN_WRITE);
spi_write(adr);
spi_write(dat);
digitalWrite (CAN_CS, HIGH) ;
}
int spi_readbyte(uint8_t adr)
{
int rxdat;
digitalWrite (CAN_CS, LOW);
spi_write(CAN_READ);
spi_write(adr);
rxdat=spi_read();
digitalWrite (CAN_CS, HIGH);
return rxdat;
}
void can_recrxb(uint8_t *CAN_RX_Buf)
{
CAN_RX_Buf[0]=spi_readbyte(RXB0D0);
CAN_RX_Buf[1]=spi_readbyte(RXB0D1);
CAN_RX_Buf[2]=spi_readbyte(RXB0D2);
CAN_RX_Buf[3]=spi_readbyte(RXB0D3);
CAN_RX_Buf[4]=spi_readbyte(RXB0D4);
CAN_RX_Buf[5]=spi_readbyte(RXB0D5);
CAN_RX_Buf[6]=spi_readbyte(RXB0D6);
CAN_RX_Buf[7]=spi_readbyte(RXB0D7);
spi_writebyte(CANINTF,0);
}
void can_send(uint8_t *CAN_TX_Buf)
{
mcp2515_bitmodity(TXB0CTRL,0x08,0x00);
spi_writebyte(TXB0SIDH,0x55);
spi_writebyte(TXB0SIDL,0x08);
spi_writebyte(TXB0EID8,0x55);
spi_writebyte(TXB0EID0,0x88);
spi_writebyte(TXB0DLC,DLC_8);
spi_writebyte(TXB0D0,CAN_TX_Buf[0]);
spi_writebyte(TXB0D1,CAN_TX_Buf[1]);
spi_writebyte(TXB0D2,CAN_TX_Buf[2]);
spi_writebyte(TXB0D3,CAN_TX_Buf[3]);
spi_writebyte(TXB0D4,CAN_TX_Buf[4]);
spi_writebyte(TXB0D5,CAN_TX_Buf[5]);
spi_writebyte(TXB0D6,CAN_TX_Buf[6]);
spi_writebyte(TXB0D7,CAN_TX_Buf[7]);
digitalWrite (CAN_CS, LOW);
spi_write(CAN_RTS | 0x01);
digitalWrite (CAN_CS, HIGH);
}
uint8_t can_readintf(void)
{
uint8_t intf=0;
intf=spi_readbyte(CANINTF);
return intf;
}
void can_setintf(uint8_t cmd)
{
spi_writebyte(CANINTF,cmd);
}
uint8_t rxbuf[8];
void mcp2515_isr(void)
{
uint8_t rx_sta=0;
can_tx_flag=1;
rx_sta=can_readintf();
if(rx_sta&0x01)
{
can_recrxb(rxbuf);
can_setintf(0x00);
can_rx_flag=1;
}
else
{
can_setintf(0x00);
}
can_tx_flag=0;
}
void init_mcp2515(void)
{
pinMode(SCK, OUTPUT);
pinMode(SI, OUTPUT);
pinMode(SO, INPUT);
pinMode(CAN_CS, OUTPUT);
pinMode(CAN_RST, OUTPUT);
pinMode(CAN_INT, INPUT);
pullUpDnControl (CAN_INT, PUD_UP);
wiringPiISR(CAN_INT,INT_EDGE_FALLING,&mcp2515_isr);
mcp2515_reset();
delay(2);
mcp2515_bitmodity(CANCTRL,0xE0,0x80);
spi_writebyte(CNF1,0x04); //100k
spi_writebyte(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
spi_writebyte(CNF3,PHSEG2_3TQ);
spi_writebyte(CANINTF,0x00);
spi_writebyte(CANINTE,0x03);
spi_writebyte(RXB0CTRL,0x60);
//spi_writebyte(RXB1CTRL,0x40);
spi_writebyte(RXM0SIDH,0xff);
spi_writebyte(RXM0SIDL,0xff);
spi_writebyte(RXM0EID8,0xff);
spi_writebyte(RXM0EID0,0xff);
// spi_writebyte(RXM1SIDH,0xff);
// spi_writebyte(RXM1SIDL,0xff);
// spi_writebyte(RXM1EID8,0xff);
// spi_writebyte(RXM1EID0,0xff);
spi_writebyte( BFPCTRL, 0 );
spi_writebyte( TXRTSCTRL, 0 );
mcp2515_bitmodity(CANCTRL,0xE0,0x00);
}
int main (void)
{
uint8_t txbuf[8];
uint8_t i=0;
wiringPiSetup();
init_mcp2515();
for(i=0;i<8;i++)
{
txbuf[i]=0;
}
while(1)
{
delay(100);
txbuf[0]=i;
printf("mcp2515 txbuf[0]=%x!\r\n",txbuf[0]);
if(can_tx_flag==0)
{
can_send(txbuf);
}
if(can_rx_flag==1)
{
can_rx_flag=0;
printf("can rxdat: %x %x %x %x %x %x %x %x\r\n",rxbuf[0],rxbuf[1],rxbuf[2],rxbuf[3],rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7]);
}
i++;
}
return 0;
}
三、测试结果
开发板与CAN卡调试助手之间收发通信
|