Arduino C++ Library for VLCB
Loading...
Searching...
No Matches
CAN2515.h
Go to the documentation of this file.
1// Copyright (C) Sven Rosvall (sven@rosvall.ie)
2// This file is part of VLCB-Arduino project on https://github.com/SvenRosvall/VLCB-Arduino
3// Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
4// The full licence can be found at: http://creativecommons.org/licenses/by-nc-sa/4.0/
5
6#pragma once
7
8// header files
9
10#include <Controller.h>
11#include <CanTransport.h>
12#include <ACAN2515.h> // ACAN2515 library
13#include <SPI.h>
14
15namespace VLCB
16{
17
18// constants
19
20static const byte MCP2515_CS = 10; // SPI chip select pin
21static const byte MCP2515_INT = 2; // interrupt pin
22static const byte NUM_RX_BUFFS = 4; // default value
23static const byte NUM_TX_BUFFS = 2; // default value
24static const uint32_t CANBITRATE = 125000UL; // 125Kb/s - fixed for VLCB
25static const uint32_t OSCFREQ = 16000000UL; // crystal frequency default
26
28class CAN2515 : public CanTransport
29{
30public:
31
33
35 // these methods are declared virtual in the base class and must be implemented by the derived class
36#ifdef ARDUINO_ARCH_RP2040
37 bool begin(bool poll = false, SPIClassRP2040 & spi = SPI); // note default args
38#else
39 bool begin(bool poll = false, SPIClass & spi = SPI);
40#endif
41 virtual bool available() override;
42 virtual CANFrame getNextCanFrame() override;
43 virtual bool sendCanFrame(CANFrame *frame) override;
44 virtual void reset() override;
45 virtual byte getHardwareType() override { return CAN_HW_MCP2515; };
46
47 // these methods are specific to this implementation
48 // they are not declared or implemented by the Transport interface class
49 void setNumBuffers(byte num_rx_buffers, byte num_tx_buffers = 0); // note default arg
50 void printStatus();
51 void setOscFreq(unsigned long freq);
52
53#ifdef ARDUINO_ARCH_RP2040
54 void setPins(byte cs_pin, byte int_pin, byte mosi_pin, byte miso_pin, byte sck_pin);
55#else
56 void setPins(byte cs_pin, byte int_pin);
57#endif
58
59 virtual unsigned int receiveCounter() override { return _numMsgsRcvd; }
60 virtual unsigned int transmitCounter() override { return _numMsgsSent; }
61 virtual unsigned int receiveErrorCounter() override { return canp->receiveErrorCounter(); }
62 virtual unsigned int transmitErrorCounter() override { return canp->transmitErrorCounter(); }
63 virtual unsigned int receiveBufferSize() override { return canp->receiveBufferSize(); };
64 virtual unsigned int transmitBufferSize() override { return canp->transmitBufferSize(0); };
65 virtual unsigned int receiveBufferUsage() override { return canp->receiveBufferCount(); };
66 virtual unsigned int transmitBufferUsage() override { return canp->transmitBufferCount(0); };
67 virtual unsigned int receiveBufferPeak() override { return canp->receiveBufferPeakCount(); };
68 virtual unsigned int transmitBufferPeak() override { return canp->transmitBufferPeakCount(0); };
69 virtual unsigned int errorStatus() override { return canp->errorFlagRegister(); }
71
72private:
73 ACAN2515 *canp; // pointer to CAN object
74 unsigned int _numMsgsSent, _numMsgsRcvd;
75 unsigned long _osc_freq;
76 byte _csPin, _intPin;
77 byte _num_rx_buffers, _num_tx_buffers;
78 bool _poll;
79
80#ifdef ARDUINO_ARCH_RP2040
81 byte _mosi_pin, _miso_pin, _sck_pin;
82#endif
83};
84
85}
Interface base class for CAN transport implementations.
Definition CanTransport.h:35
Definition VLCB.h:50
void begin()
@ CAN_HW_MCP2515
Definition vlcbdefs.hpp:668