【MSPM0L1306 LaunchPad】CAN收发测试
[复制链接]
这篇来测试下SPI收发,使用SPI转CAN通信模块测试。
一、硬件部分
1.1、测试连接图
1.2、CAN芯片框图
测试使用了XL2515芯片,来实现SPI转CAN通信的功能
二、端口配置
2.1、SPI配置
SPI选择的端口
2.2、CAN片选引脚定义
2.3、CAN中断引脚定义
三、代码
3.1、x2515.c
#include "ti_msp_dl_config.h"
#include "systick/systick.h"
#include "x2515/x2515.h"
#include "spi/spi.h"
#include "led/led.h"
#include <stdio.h>
uint8_t rxbuf[20]={0};
#define x2515_cs_low() (DL_GPIO_clearPins(GPIO_X2515_PORT, GPIO_X2515_CS_PIN))
#define x2515_cs_high() (DL_GPIO_setPins(GPIO_X2515_PORT, GPIO_X2515_CS_PIN))
uint8_t SPI_Read(void)
{
uint8_t tdata=0;
tdata=spi_senddat(0xff);
return tdata;
}
void SPI_Write(uint8_t ch)
{
spi_senddat(ch);
}
void SPI_ByteWrite(uint8_t addr, uint8_t value)
{
x2515_cs_low() ;
SPI_Write(CAN_WRITE);
SPI_Write(addr);
SPI_Write(value);
x2515_cs_high() ;
}
uint8_t SPI_ByteRead(uint8_t addr)
{
uint8_t tempdata;
x2515_cs_low() ;
SPI_Write(CAN_READ);
SPI_Write(addr);
tempdata=SPI_Read();
x2515_cs_high();
return tempdata;
}
void SPI_Reset(void)
{
x2515_cs_low();
SPI_Write(CAN_RESET);
x2515_cs_high();
}
void SPI_BitModityx2515(uint8_t addr, uint8_t mask, uint8_t udata)
{
uint8_t i;
x2515_cs_low();
for(i = 10;i > 0;i--) ;
SPI_Write(CAN_BIT_MODIFY);
SPI_Write(addr);
SPI_Write(mask);
SPI_Write(udata);
x2515_cs_high();
}
void SPI_WriteRegister(uint8_t addr, uint8_t*dat, uint8_t length)
{
uint8_t i;
x2515_cs_low();
SPI_Write(CAN_WRITE);
SPI_Write(addr);
for (i=0; i<length ;i++ )
SPI_Write(*dat++);
x2515_cs_high();
}
void x2515_init(void)
{
uint8_t temp[4] = { 0, 0, 0, 0 };
NVIC_EnableIRQ(GPIO_X2515_INT_INT_IRQN);
SPI_Reset();
SysTick_Delay_ms(200);
SPI_BitModityx2515(CANCTRL,0xE0,0x80);
SPI_ByteWrite(CNF1,CAN_100Kbps);
SPI_ByteWrite(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
SPI_ByteWrite(CNF3,PHSEG2_3TQ);
SPI_ByteWrite(CANINTF,0x00);
SPI_ByteWrite(CANINTE,0x01);
SPI_ByteWrite(RXB0CTRL,0x60);
SPI_ByteWrite(RXB1CTRL,0x60);
SPI_ByteWrite(RXM0SIDH,0xff);
SPI_ByteWrite(RXM0SIDL,0xff);
// SPI_ByteWrite(RXM0EID8,0xff);
// SPI_ByteWrite(RXM0EID0,0xff);
//SPI_WriteRegister( RXF0SIDH, temp, 4 );
SPI_WriteRegister( RXF1SIDH, temp, 4 );
SPI_WriteRegister( RXF2SIDH, temp, 4 );
SPI_WriteRegister( RXF3SIDH, temp, 4 );
SPI_WriteRegister( RXF4SIDH, temp, 4 );
SPI_WriteRegister( RXF5SIDH, temp, 4 );
// SPI_WriteRegister( RXM0SIDH, temp, 4 );
SPI_WriteRegister( RXM1SIDH, temp, 4 );
SPI_ByteWrite( BFPCTRL, 0 );
SPI_ByteWrite( TXRTSCTRL, 0 );
SPI_BitModityx2515(CANCTRL,0xE0,0x00);
}
void CAN_Send(uint8_t *CAN_TX_Buf)
{
SPI_BitModityx2515(TXB0CTRL,0x08,0x00);
SPI_ByteWrite(TXB0SIDH,0x01);
SPI_ByteWrite(TXB0SIDL,0x08);
SPI_ByteWrite(TXB0EID8,0x60);
SPI_ByteWrite(TXB0EID0,0x11);
SPI_ByteWrite(TXB0DLC,DLC_8);
SPI_ByteWrite(TXB0D0,CAN_TX_Buf[0]);
SPI_ByteWrite(TXB0D1,CAN_TX_Buf[1]);
SPI_ByteWrite(TXB0D2,CAN_TX_Buf[2]);
SPI_ByteWrite(TXB0D3,CAN_TX_Buf[3]);
SPI_ByteWrite(TXB0D4,CAN_TX_Buf[4]);
SPI_ByteWrite(TXB0D5,CAN_TX_Buf[5]);
SPI_ByteWrite(TXB0D6,CAN_TX_Buf[6]);
SPI_ByteWrite(TXB0D7,CAN_TX_Buf[7]);
x2515_cs_low();
SPI_Write(CAN_RTS | 0x01);
x2515_cs_high();
}
void CAN_Receive(uint8_t *CAN_RX_Buf)
{
CAN_RX_Buf[0]=SPI_ByteRead(RXB0D0); //
CAN_RX_Buf[1]=SPI_ByteRead(RXB0D1);
CAN_RX_Buf[2]=SPI_ByteRead(RXB0D2);
CAN_RX_Buf[3]=SPI_ByteRead(RXB0D3);
CAN_RX_Buf[4]=SPI_ByteRead(RXB0D4);
CAN_RX_Buf[5]=SPI_ByteRead(RXB0D5);
CAN_RX_Buf[6]=SPI_ByteRead(RXB0D6);
CAN_RX_Buf[7]=SPI_ByteRead(RXB0D7);
CAN_RX_Buf[8]=SPI_ByteRead(RXB0SIDH);
CAN_RX_Buf[9]=SPI_ByteRead(RXB0SIDL);
CAN_RX_Buf[10]=SPI_ByteRead(RXB0EID8);
CAN_RX_Buf[11]=SPI_ByteRead(RXB0EID0);
CAN_RX_Buf[12]=SPI_ByteRead(RXB0DLC);
SPI_ByteWrite(CANINTF,0);
}
void GROUP1_IRQHandler(void)
{
switch (DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_1))
{
case GPIO_X2515_INT_INT_IIDX:
SPI_ByteWrite(CANINTF, 0);
CAN_Receive(rxbuf);
printf("rxdat: ");
for(uint8_t i=0;i<8;i++)
{
printf("0x%02x ",rxbuf[i]);
}
printf("\r\n");
led2_r_tog();
break;
}
}
3.2、main.c
#include "ti_msp_dl_config.h"
#include "systick/systick.h"
#include "led/led.h"
#include "uart/uart.h"
#include <string.h>
#include <stdio.h>
#include "adc/adc.h"
#include "spi/spi.h"
#include "x2515/x2515.h"
int main(void)
{
uint8_t can_txbuf[8]={0};
uint8_t js=0;
SYSCFG_DL_init();
init_adc();
x2515_init();
led2_r_off();
led2_g_off();
led2_b_off();
can_txbuf[0]=0x01;
can_txbuf[1]=0xff;
while (1)
{
led1_r_tog();
CAN_Send(can_txbuf);
SysTick_Delay_ms(100);
can_txbuf[0]++;
}
}
四、程序运行
CAN调试助手收到开发板的数据和开发板收到CAN调试助手发送的数据。
|