#include "mcp2515.h" const struct MCP2515::TXBn_REGS MCP2515::TXB[MCP2515::N_TXBUFFERS] = { {MCP_TXB0CTRL, MCP_TXB0SIDH, MCP_TXB0DATA}, {MCP_TXB1CTRL, MCP_TXB1SIDH, MCP_TXB1DATA}, {MCP_TXB2CTRL, MCP_TXB2SIDH, MCP_TXB2DATA} }; const struct MCP2515::RXBn_REGS MCP2515::RXB[N_RXBUFFERS] = { {MCP_RXB0CTRL, MCP_RXB0SIDH, MCP_RXB0DATA, CANINTF_RX0IF}, {MCP_RXB1CTRL, MCP_RXB1SIDH, MCP_RXB1DATA, CANINTF_RX1IF} }; MCP2515::MCP2515(const uint8_t _CS) { SPI.begin(); SPICS = _CS; pinMode(SPICS, OUTPUT); endSPI(); } void MCP2515::startSPI() { SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0)); digitalWrite(SPICS, LOW); } void MCP2515::endSPI() { digitalWrite(SPICS, HIGH); SPI.endTransaction(); } MCP2515::ERROR MCP2515::reset(void) { startSPI(); SPI.transfer(INSTRUCTION_RESET); endSPI(); delay(10); uint8_t zeros[14]; memset(zeros, 0, sizeof(zeros)); setRegisters(MCP_TXB0CTRL, zeros, 14); setRegisters(MCP_TXB1CTRL, zeros, 14); setRegisters(MCP_TXB2CTRL, zeros, 14); setRegister(MCP_RXB0CTRL, 0); setRegister(MCP_RXB1CTRL, 0); setRegister(MCP_CANINTE, CANINTF_RX0IF | CANINTF_RX1IF | CANINTF_ERRIF | CANINTF_MERRF); modifyRegister(MCP_RXB0CTRL, RXBnCTRL_RXM_MASK | RXB0CTRL_BUKT, RXBnCTRL_RXM_STDEXT | RXB0CTRL_BUKT); modifyRegister(MCP_RXB1CTRL, RXBnCTRL_RXM_MASK, RXBnCTRL_RXM_STDEXT); // clear filters and masks /*RXF filters[] = {RXF0, RXF1, RXF2, RXF3, RXF4, RXF5}; for (int i=0; i<6; i++) { ERROR result = setFilter(filters[i], true, 0); if (result != ERROR_OK) { return result; } } MASK masks[] = {MASK0, MASK1}; for (int i=0; i<2; i++) { ERROR result = setFilterMask(masks[i], true, 0); if (result != ERROR_OK) { return result; } }*/ return ERROR_OK; } uint8_t MCP2515::readRegister(const REGISTER reg) { startSPI(); SPI.transfer(INSTRUCTION_READ); SPI.transfer(reg); uint8_t ret = SPI.transfer(0x00); endSPI(); return ret; } void MCP2515::readRegisters(const REGISTER reg, uint8_t values[], const uint8_t n) { startSPI(); SPI.transfer(INSTRUCTION_READ); SPI.transfer(reg); // mcp2515 has auto-increment of address-pointer for (uint8_t i=0; i> 8); canid = (uint16_t)(id >> 16); buffer[MCP_SIDL] = (uint8_t) (canid & 0x03); buffer[MCP_SIDL] += (uint8_t) ((canid & 0x1C) << 3); buffer[MCP_SIDL] |= TXB_EXIDE_MASK; buffer[MCP_SIDH] = (uint8_t) (canid >> 5); } else { buffer[MCP_SIDH] = (uint8_t) (canid >> 3); buffer[MCP_SIDL] = (uint8_t) ((canid & 0x07 ) << 5); buffer[MCP_EID0] = 0; buffer[MCP_EID8] = 0; } } MCP2515::ERROR MCP2515::setFilterMask(const MASK mask, const bool ext, const uint32_t ulData) { ERROR res = setConfigMode(); if (res != ERROR_OK) { return res; } uint8_t tbufdata[4]; prepareId(tbufdata, ext, ulData); REGISTER reg; switch (mask) { case MASK0: reg = MCP_RXM0SIDH; break; case MASK1: reg = MCP_RXM1SIDH; break; default: return ERROR_FAIL; } setRegisters(reg, tbufdata, 4); return ERROR_OK; } MCP2515::ERROR MCP2515::setFilter(const RXF num, const bool ext, const uint32_t ulData) { ERROR res = setConfigMode(); if (res != ERROR_OK) { return res; } REGISTER reg; switch (num) { case RXF0: reg = MCP_RXF0SIDH; break; case RXF1: reg = MCP_RXF1SIDH; break; case RXF2: reg = MCP_RXF2SIDH; break; case RXF3: reg = MCP_RXF3SIDH; break; case RXF4: reg = MCP_RXF4SIDH; break; case RXF5: reg = MCP_RXF5SIDH; break; default: return ERROR_FAIL; } uint8_t tbufdata[4]; prepareId(tbufdata, ext, ulData); setRegisters(reg, tbufdata, 4); return ERROR_OK; } MCP2515::ERROR MCP2515::sendMessage(const TXBn txbn, const struct can_frame *frame) { const struct TXBn_REGS *txbuf = &TXB[txbn]; uint8_t data[13]; bool ext = (frame->can_id & CAN_EFF_FLAG); bool rtr = (frame->can_id & CAN_RTR_FLAG); uint32_t id = (frame->can_id & (ext ? CAN_EFF_MASK : CAN_SFF_MASK)); prepareId(data, ext, id); data[MCP_DLC] = rtr ? (frame->can_dlc | RTR_MASK) : frame->can_dlc; memcpy(&data[MCP_DATA], frame->data, frame->can_dlc); setRegisters(txbuf->SIDH, data, 5 + frame->can_dlc); modifyRegister(txbuf->CTRL, TXB_TXREQ, TXB_TXREQ); return ERROR_OK; } MCP2515::ERROR MCP2515::sendMessage(const struct can_frame *frame) { if (frame->can_dlc > CAN_MAX_DLEN) { return ERROR_FAILTX; } TXBn txBuffers[N_TXBUFFERS] = {TXB0, TXB1, TXB2}; for (int i=0; iCTRL); if ( (ctrlval & TXB_TXREQ) == 0 ) { return sendMessage(txBuffers[i], frame); } } return ERROR_FAILTX; } MCP2515::ERROR MCP2515::readMessage(const RXBn rxbn, struct can_frame *frame) { const struct RXBn_REGS *rxb = &RXB[rxbn]; uint8_t tbufdata[5]; readRegisters(rxb->SIDH, tbufdata, 5); uint32_t id = (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5); if ( (tbufdata[MCP_SIDL] & TXB_EXIDE_MASK) == TXB_EXIDE_MASK ) { id = (id<<2) + (tbufdata[MCP_SIDL] & 0x03); id = (id<<8) + tbufdata[MCP_EID8]; id = (id<<8) + tbufdata[MCP_EID0]; id |= CAN_EFF_FLAG; } uint8_t dlc = (tbufdata[MCP_DLC] & DLC_MASK); if (dlc > CAN_MAX_DLEN) { return ERROR_FAIL; } uint8_t ctrl = readRegister(rxb->CTRL); if (ctrl & RXBnCTRL_RTR) { id |= CAN_RTR_FLAG; } frame->can_id = id; frame->can_dlc = dlc; readRegisters(rxb->DATA, frame->data, dlc); modifyRegister(MCP_CANINTF, rxb->CANINTF_RXnIF, 0); return ERROR_OK; } MCP2515::ERROR MCP2515::readMessage(struct can_frame *frame) { ERROR rc; uint8_t stat = getStatus(); if ( stat & STAT_RX0IF ) { rc = readMessage(RXB0, frame); } else if ( stat & STAT_RX1IF ) { rc = readMessage(RXB1, frame); } else { rc = ERROR_NOMSG; } return rc; } bool MCP2515::checkReceive(void) { uint8_t res = getStatus(); if ( res & STAT_RXIF_MASK ) { return true; } else { return false; } } bool MCP2515::checkError(void) { uint8_t eflg = getErrorFlags(); if ( eflg & EFLG_ERRORMASK ) { return true; } else { return false; } } uint8_t MCP2515::getErrorFlags(void) { return readRegister(MCP_EFLG); } void MCP2515::clearRXnOVRFlags(void) { modifyRegister(MCP_EFLG, EFLG_RX0OVR | EFLG_RX1OVR, 0); } uint8_t MCP2515::getInterrupts(void) { return readRegister(MCP_CANINTF); } void MCP2515::clearInterrupts(void) { setRegister(MCP_CANINTF, 0); } uint8_t MCP2515::getInterruptMask(void) { return readRegister(MCP_CANINTE); } void MCP2515::clearTXInterrupts(void) { modifyRegister(MCP_CANINTF, (CANINTF_TX0IF | CANINTF_TX1IF | CANINTF_TX2IF), 0); } void MCP2515::clearRXnOVR(void) { uint8_t eflg = getErrorFlags(); if (eflg != 0) { clearRXnOVRFlags(); clearInterrupts(); //modifyRegister(MCP_CANINTF, CANINTF_ERRIF, 0); } } void MCP2515::clearMERR() { //modifyRegister(MCP_EFLG, EFLG_RX0OVR | EFLG_RX1OVR, 0); //clearInterrupts(); modifyRegister(MCP_CANINTF, CANINTF_MERRF, 0); }