upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
mcp2515.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 
28 #include "mcp2515.h"
29 
30 namespace upm {
31 
59  class MCP2515 {
60  public:
61 
70  MCP2515(int bus, int csPin);
71 
75  ~MCP2515();
76 
81  void reset();
82 
92  void setOpmode(MCP2515_OPMODE_T opmode);
93 
103  void setSpeed(MCP2515_SPEED_T speed);
104 
122  void loadTXBuffer(MCP2515_TX_BUFFER_T bufnum,
123  int id, bool ext, bool rtr,
124  std::string payload);
125 
134  void transmitBuffer(MCP2515_TX_BUFFER_T bufnum, bool wait);
135 
145  bool isTXBufferFree(MCP2515_TX_BUFFER_T bufnum);
146 
153  MCP2515_TX_BUFFER_T findFreeTXBuffer();
154 
166  void setTXBufferPriority(MCP2515_TX_BUFFER_T bufnum,
167  MCP2515_TXP_T priority);
168 
179  void abortTX(MCP2515_TX_BUFFER_T bufnum);
180 
194  void setRXBufferMode(MCP2515_RX_BUFFER_T bufnum,
195  MCP2515_RXMODE_T rxm);
196 
206  MCP2515_RXMSG_T rxStatusMsgs();
207 
217  MCP2515_MSGTYPE_T rxStatusMsgType();
218 
227  MCP2515_FILTERMATCH_T rxStatusFiltermatch();
228 
240  upm_result_t getRXMsg(MCP2515_RX_BUFFER_T bufnum);
241 
254  void printMsg();
255 
256 
263  int msgGetID()
264  {
265  return m_message.id;
266  }
267 
274  bool msgGetRTR()
275  {
276  return m_message.rtr;
277  }
278 
286  bool msgGetEXT()
287  {
288  return m_message.ext;
289  }
290 
299  {
300  return m_message.filter_num;
301  }
302 
311  {
312  return m_message.len;
313  }
314 
322  std::string msgGetPayload()
323  {
324  return std::string((char *)m_message.pkt.data, m_message.len);
325  }
326 
327 
337  void installISR(int pin, void (*isr)(void *), void *arg);
338 
339 
345  void uninstallISR();
346 
353  void setIntrEnables(uint8_t enables);
354 
362  uint8_t getIntrFlags();
363 
372  void setIntrFlags(uint8_t flags);
373 
382  void clearIntrFlags(uint8_t flags);
383 
390  uint8_t getErrorFlags();
391 
399  void clearErrorFlags(uint8_t flags);
400 
414  void setFilter(MCP2515_RX_FILTER_T filter, bool ext, int id);
415 
430  void setMask(MCP2515_RX_MASK_T mask, bool ext, int id);
431 
432 
433  protected:
434  mcp2515_context m_mcp2515;
435 
436  // We operate only on this message (for received messages) to
437  // simplify SWIG accesses.
438  MCP2515_MSG_T m_message;
439 
451  std::string busRead(uint8_t cmd, std::string args, int datalen);
452 
463  void busWrite(uint8_t cmd, std::string data);
464 
474  void writeReg(uint8_t reg, uint8_t value);
475 
485  void writeRegs(uint8_t reg, std::string buffer);
486 
496  uint8_t readReg(uint8_t reg);
497 
510  void bitModify(uint8_t addr, uint8_t mask, uint8_t value);
511 
512  private:
513  /* Disable implicit copy and assignment operators */
514  MCP2515(const MCP2515&) = delete;
515  MCP2515 &operator=(const MCP2515&) = delete;
516  };
517 }
void setIntrFlags(uint8_t flags)
Definition: mcp2515.cxx:171
void uninstallISR()
Definition: mcp2515.cxx:150
MCP2515(int bus, int csPin)
Definition: mcp2515.cxx:35
std::string busRead(uint8_t cmd, std::string args, int datalen)
Definition: mcp2515.cxx:215
void clearErrorFlags(uint8_t flags)
Definition: mcp2515.cxx:194
uint8_t getErrorFlags()
Definition: mcp2515.cxx:185
~MCP2515()
Definition: mcp2515.cxx:43
uint8_t readReg(uint8_t reg)
void reset()
Definition: mcp2515.cxx:48
bool isTXBufferFree(MCP2515_TX_BUFFER_T bufnum)
Definition: mcp2515.cxx:86
void setSpeed(MCP2515_SPEED_T speed)
Definition: mcp2515.cxx:62
void transmitBuffer(MCP2515_TX_BUFFER_T bufnum, bool wait)
Definition: mcp2515.cxx:79
int msgGetPayloadLen()
Definition: mcp2515.hpp:310
Definition: mcp2515.h:53
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
void setRXBufferMode(MCP2515_RX_BUFFER_T bufnum, MCP2515_RXMODE_T rxm)
Definition: mcp2515.cxx:111
void clearIntrFlags(uint8_t flags)
Definition: mcp2515.cxx:178
MCP2515_TX_BUFFER_T findFreeTXBuffer()
Definition: mcp2515.cxx:91
void busWrite(uint8_t cmd, std::string data)
Definition: mcp2515.cxx:226
void setMask(MCP2515_RX_MASK_T mask, bool ext, int id)
Definition: mcp2515.cxx:208
int msgGetID()
Definition: mcp2515.hpp:263
void setOpmode(MCP2515_OPMODE_T opmode)
Definition: mcp2515.cxx:55
void abortTX(MCP2515_TX_BUFFER_T bufnum)
Definition: mcp2515.cxx:104
MCP2515_MSGTYPE_T rxStatusMsgType()
Definition: mcp2515.cxx:123
void writeReg(uint8_t reg, uint8_t value)
Definition: mcp2515.cxx:233
void setFilter(MCP2515_RX_FILTER_T filter, bool ext, int id)
Definition: mcp2515.cxx:201
void bitModify(uint8_t addr, uint8_t mask, uint8_t value)
Definition: mcp2515.cxx:248
void installISR(int pin, void(*isr)(void *), void *arg)
Definition: mcp2515.cxx:143
bool msgGetEXT()
Definition: mcp2515.hpp:286
void loadTXBuffer(MCP2515_TX_BUFFER_T bufnum, int id, bool ext, bool rtr, std::string payload)
Definition: mcp2515.cxx:69
upm_result_t getRXMsg(MCP2515_RX_BUFFER_T bufnum)
Definition: mcp2515.cxx:133
MCP2515_FILTERMATCH_T rxStatusFiltermatch()
Definition: mcp2515.cxx:128
API for the MCP2515 CAN bus controller.
Definition: mcp2515.hpp:59
Definition: mcp2515_regs.h:92
void setTXBufferPriority(MCP2515_TX_BUFFER_T bufnum, MCP2515_TXP_T priority)
Definition: mcp2515.cxx:96
int msgGetFilterNum()
Definition: mcp2515.hpp:298
void printMsg()
Definition: mcp2515.cxx:138
std::string msgGetPayload()
Definition: mcp2515.hpp:322
void writeRegs(uint8_t reg, std::string buffer)
Definition: mcp2515.cxx:240
void setIntrEnables(uint8_t enables)
Definition: mcp2515.cxx:155
MCP2515_RXMSG_T rxStatusMsgs()
Definition: mcp2515.cxx:118
uint8_t getIntrFlags()
Definition: mcp2515.cxx:162
bool msgGetRTR()
Definition: mcp2515.hpp:274