Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
arduino/Can.hxx
Go to the documentation of this file.
1
36// This include is exclusive against freertos_drivers/common/Can.hxx
37#ifndef _FREERTOS_DRIVERS_ARDUINO_CAN_HXX_
38#define _FREERTOS_DRIVERS_ARDUINO_CAN_HXX_
39
40#include "DeviceBuffer.hxx"
41#include "can_frame.h"
42#include "nmranet_config.h"
43#include "os/OS.hxx"
44#include "utils/Atomic.hxx"
45
47class Can : protected Atomic
48{
49public:
50 static unsigned numReceivedPackets_;
51 static unsigned numTransmittedPackets_;
52
55 {
56 return rxBuf->pending();
57 }
58
62 {
63 return txBuf->space();
64 }
65
69 int read(struct can_frame *frame)
70 {
71 AtomicHolder h(this);
72 auto ret = rxBuf->get(frame, 1);
73 return ret;
74 }
75
79 int write(const struct can_frame *frame)
80 {
81 AtomicHolder h(this);
82 auto ret = txBuf->put(frame, 1);
83 if (ret)
84 {
85 tx_msg();
86 }
87 return ret;
88 }
89
90 virtual void enable() = 0;
91 virtual void disable() = 0;
93protected:
97 Can(const char *ignored)
98 : txBuf(DeviceBuffer<struct can_frame>::create(
99 config_can_tx_buffer_size(), config_can_tx_buffer_size() / 2))
100 , rxBuf(DeviceBuffer<struct can_frame>::create(
101 config_can_rx_buffer_size()))
102 , overrunCount(0)
103 , busOffCount(0)
104 , softErrorCount(0)
105 {
106 }
107
111 {
112 txBuf->destroy();
113 rxBuf->destroy();
114 }
115
116 virtual void tx_msg() = 0;
120 unsigned int overrunCount;
121 unsigned int busOffCount;
122 unsigned int softErrorCount;
124private:
126};
127
128#endif /* _FREERTOS_DRIVERS_ARDUINO_CAN_HXX_ */
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
Base class for a CAN device for the Arduino environment.
unsigned int overrunCount
overrun count
unsigned int softErrorCount
soft error count
DeviceBuffer< struct can_frame > * txBuf
transmit buffer
int read(struct can_frame *frame)
Read a frame if there is one available.
unsigned int busOffCount
bus-off count
~Can()
Destructor.
virtual void enable()=0
function to enable device
Can(const char *ignored)
Constructor.
int available()
virtual void disable()=0
function to disable device
DeviceBuffer< struct can_frame > * rxBuf
receive buffer
int availableForWrite()
virtual void tx_msg()=0
function to try and transmit a message
int write(const struct can_frame *frame)
Send a frame if there is space available.
size_t space()
Return the number of items for which space is available.
size_t pending()
Return the number of items in the queue.
Implements a smart buffer specifically designed for character device drivers.
size_t get(T *buf, size_t items)
remove a number of items from the buffer.
size_t put(const T *buf, size_t items)
Insert a number of items to the buffer.
void destroy()
Destroy an existing DeviceBuffer instance.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171