COSA
An Object-Oriented Platform for Arduino Programming
RS485.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_RS485_HH
22 #define COSA_RS485_HH
23 
24 #if !defined(BOARD_ATTINY)
25 #include "Cosa/UART.hh"
26 #include "Cosa/IOBuffer.hh"
27 #include "Cosa/OutputPin.hh"
28 
29 // Default buffer size
30 #ifndef COSA_RS485_BUFFER_MAX
31 # define COSA_RS485_BUFFER_MAX 64
32 #endif
33 
44 class RS485 : public UART {
45 public:
47  static const uint8_t SOT = 0x01;
48 
50  struct header_t {
51  uint8_t length;
52  uint8_t dest;
53  uint8_t src;
54  uint8_t crc;
55  };
56 
58  static const uint8_t FRAME_MAX = sizeof(header_t) + sizeof(uint16_t) + 1;
59 
61  static const uint16_t BUFFER_MAX = COSA_RS485_BUFFER_MAX;
62 
64  static const uint8_t BROADCAST = 0;
65 
67  static const uint8_t MASTER = 255;
68 
80  uint16_t size, Board::DigitalPin de, uint8_t addr = MASTER) :
81  UART(port, ibuf, obuf),
82  PAYLOAD_MAX(size - FRAME_MAX),
83  m_de(de),
84  m_addr(addr),
85  m_state(0)
86  {}
87 
96  virtual int putchar(char c);
97 
102  void address(uint8_t addr = MASTER)
103  {
104  m_addr = addr;
105  }
106 
116  int send(const void* buf, size_t len, uint8_t dest = MASTER);
117 
125  int broadcast(const void* buf, size_t len)
126  __attribute__((always_inline))
127  {
128  return (send(buf, len, BROADCAST));
129  }
130 
141  int recv(void* buf, size_t len, uint32_t ms = 0L);
142 
143 protected:
145  const uint16_t PAYLOAD_MAX;
146 
149 
152 
154  uint8_t m_addr;
155 
157  uint8_t m_state;
158 
164  virtual void on_udre_interrupt();
165 
171  virtual void on_tx_interrupt();
172 
177  virtual void on_transmit_completed()
178  {
179  m_de.clear();
180  }
181 };
182 #endif
183 #endif
uint8_t dest
Destination node address.
Definition: RS485.hh:52
virtual void on_udre_interrupt()
Definition: RS485.cpp:65
virtual void on_transmit_completed()
Definition: RS485.hh:177
virtual int putchar(char c)
Definition: RS485.cpp:53
uint8_t src
Source node address.
Definition: RS485.hh:53
const uint16_t PAYLOAD_MAX
Definition: RS485.hh:145
int broadcast(const void *buf, size_t len)
Definition: RS485.hh:125
int send(const void *buf, size_t len, uint8_t dest=MASTER)
Definition: RS485.cpp:86
uint8_t m_state
Definition: RS485.hh:157
static const uint8_t SOT
Definition: RS485.hh:47
header_t m_header
Definition: RS485.hh:148
Definition: RS485.hh:44
static IOBuffer< UART::TX_BUFFER_MAX > obuf
Definition: UART.cpp:38
Definition: UART.hh:59
static const uint8_t FRAME_MAX
Definition: RS485.hh:58
static const uint16_t BUFFER_MAX
Definition: RS485.hh:61
static const uint8_t MASTER
Definition: RS485.hh:67
static const uint8_t BROADCAST
Definition: RS485.hh:64
void address(uint8_t addr=MASTER)
Definition: RS485.hh:102
#define COSA_RS485_BUFFER_MAX
Definition: RS485.hh:31
int recv(void *buf, size_t len, uint32_t ms=0L)
Definition: RS485.cpp:113
virtual void on_tx_interrupt()
Definition: RS485.cpp:79
uint8_t crc
Header check-sum.
Definition: RS485.hh:54
uint8_t m_addr
Definition: RS485.hh:154
uint8_t length
Number of bytes in payload.
Definition: RS485.hh:51
void clear() const
Definition: OutputPin.hh:124
static IOBuffer< UART::RX_BUFFER_MAX > ibuf
Definition: UART.cpp:37
RS485(uint8_t port, IOStream::Device *ibuf, IOStream::Device *obuf, uint16_t size, Board::DigitalPin de, uint8_t addr=MASTER)
Definition: RS485.hh:79
OutputPin m_de
Definition: RS485.hh:151