BOARD_nSPI2_SS_INACTIVE();
MCP2515_Read(0x30, 1, &read_out); // debug
}
uint8_t MCP2515_ReadStatus(void)
{
uint8_t status;
BOARD_nSPI2_SS_ACTIVE();
SPI_WriteByte(READ_STATUS);
status = SPI_ReadByte();
BOARD_nSPI2_SS_INACTIVE();
return status;
}
uint8_t MCP2515_RxStatus(void)
{
uint8_t status;
BOARD_nSPI2_SS_ACTIVE();
SPI_WriteByte(RX_STATUS);
status = SPI_ReadByte();
BOARD_nSPI2_SS_INACTIVE();
return status;
}
void MCP2515_BitModfye(uint8_t addr, uint8_t mask, uint8_t data)
{
BOARD_nSPI2_SS_ACTIVE();
SPI_WriteByte(BIT_MODIFY);
SPI_WriteByte(addr);
SPI_WriteByte(mask);
SPI_WriteByte(data);
BOARD_nSPI2_SS_INACTIVE();
}
void MCP2515_SetTxPriority(uint8_t txb, uint8_t priority)
{
uint8_t start_addr;
if(txb == 0)
{
start_addr = 0x30;
}
else if(txb == 1)
{
start_addr = 0x40;
}
else if(txb == 2)
{
start_addr = 0x50;
}
MCP2515_BitModfye(start_addr, 0x03, priority);
}
void MCP2515_InitiateTx(uint8_t txb)
{
uint8_t addr;
if(txb == 0)
{
addr = 0x30;
}
else if(txb == 1)
{
addr = 0x40;
}
else if(txb == 2)
{
addr = 0x50;
}
MCP2515_BitModfye(addr, 1<<3, 1<<3);
MCP2515_BitModfye(0x0f, 1<<4, 0<<4);
}
/*
* rxm : 11 = Turns mask/filters off; receives any message
* 00 = Receives all valid messages using either Standard or Extended Identifiers that meet filter criteria;
* Extended ID Filter registers, RXFnEID8:RXFnEID0, are applied to the first two bytes of data in
* the messages with standard IDs
*/
void MCP2515_SetRxMode(uint8_t rxb,uint8_t mode)
{
uint8_t addr;
if(rxb == 0)
{
addr = 0x60;
}
else if(rxb == 1)
{
addr = 0x70;
}
MCP2515_BitModfye(addr, 0x03<<6, mode);
}
void MCP2515_ConfigMode(void)
{
uint8_t byte;
MCP2515_BitModfye(0x0f, 0x7<<5, 4<<5);
do
{
MCP2515_Read(0x0e, 1, &byte);
byte &= (0x07 << 5);
}
while(byte != (4<<5));
}
void MCP2515_LoopBackMode(void)
{
uint8_t byte;
MCP2515_BitModfye(0x0f, 0x7<<5, 2<<5);
do
{
MCP2515_Read(0x0e, 1, &byte);
byte &= (0x07 << 5);
}
while(byte != (2<<5));
}
void MCP2515_NormalOpMode(void)
{
uint8_t byte;
MCP2515_BitModfye(0x0f, 0x7<<5, 0<<5);
do
{
MCP2515_Read(0x0e, 1, &byte);
byte &= (0x07 << 5);
}
while(byte != (0<<5));
}
void MCP2515_BitTimingConfig(void)
{
/*
MCP2515_ConfigMode();
MCP2515_BitModfye(0x2a, 0xff, (0<<6) | (1<<0)); // 125KHz @ 8MHz,SJW = 1T_q, T_q = 2*(1 + 1)/8MHz = 0.5us
MCP2515_BitModfye(0x29, 0xff, (6<<3) | (1<<0)); // PS1 = 7T_q, ProgSeg = 2T_q
MCP2515_BitModfye(0x28, 0xff, (5<<0)); // PS2 = 6T_q
MCP2515_NormalOpMode();
*/
MCP2515_ConfigMode();
MCP2515_BitModfye(0x2a, 0xff, (0<<6) | (3<<0)); // 125KHz @ 8MHz,SJW = 1T_q, T_q = 2*(3 + 1)/8MHz = 1us
MCP2515_BitModfye(0x29, 0xff, (1<<7) | (2<<3) | (0<<0)); // PS1 = 3T_q, ProgSeg = 1T_q
MCP2515_BitModfye(0x28, 0xff, (2<<0)); // PS2 = 3T_q
MCP2515_NormalOpMode();
}
void MCP2515_InterruptConfig(uint8_t bit_mask, uint8_t enable)
{
MCP2515_ConfigMode();
if(enable)
{
MCP2515_BitModfye(0x2b, bit_mask, 0xff);
}
else
{
MCP2515_BitModfye(0x2b, bit_mask, 0x00);
}
MCP2515_NormalOpMode();
}
void MCP2515_LoadTxSID(uint8_t txb, uint32_t sid)
{
uint8_t addr;
uint8_t sid_array[2];
sid_array[0] = sid>>3;
sid_array[1] = (sid<<5);
if(txb == 0)
{
addr = 0x31;
}
else if(txb == 1)
{
addr = 0x41;
}
else if(txb == 2)
{
addr = 0x51;
}
// MCP2515_ConfigMode();
MCP2515_Write(addr, 2, sid_array);
}
void MCP2515_LoadRxf(uint8_t rxf, uint32_t sid)
{
uint8_t addr;
uint8_t sid_array[2];
sid_array[0] = sid>>3;
sid_array[1] = (sid<<5);
MCP2515_ConfigMode();
if(rxf == 0)
{
addr = 0x00;
}
else if(rxf == 1)
{
addr = 0x04;
}
else if(rxf == 2)
{
addr = 0x08;
}
else if(rxf == 3)
{
addr = 0x10;
}
else if(rxf == 4)
{
addr = 0x14;
}
else if(rxf == 5)
{
addr = 0x18;
}
MCP2515_Write(addr, 2, sid_array);
MCP2515_NormalOpMode();
}
void MCP2515_LoadRxm(uint8_t rxm, uint32_t mask)
{
uint8_t addr;
uint8_t mask_array[2];
mask_array[0] = mask>>3;
mask_array[1] = mask<<5;
MCP2515_ConfigMode();
if(rxm == 0)
{
addr = 0x20;
}
else if(rxm == 1)
{
addr = 0x24;
}
MCP2515_Write(addr, 2, mask_array);
MCP2515_NormalOpMode();
}
uint8_t MCP2515_ReadICOD(void)
{
uint8_t ret_val = 0;
MCP2515_Read(0x0e, 1, &ret_val);
ret_val >>= 1;
ret_val &= 0x07;
return ret_val;
}
文章评论(0条评论)
登录后参与讨论