/ Adafruit_ZeroCAN.h
Adafruit_ZeroCAN.h
 1  /*!
 2   * @file Adafruit_ZeroCAN.h
 3   *
 4   * This is a library for the CAN peripheral on SAME51 devices
 5   *
 6   * Adafruit invests time and resources providing this open source code,
 7   * please support Adafruit and open-source hardware by purchasing
 8   * products from Adafruit!
 9   *
10   * Written by Jeff Epler for Adafruit Industries.
11   *
12   * BSD license, all text here must be included in any redistribution.
13   *
14   */
15  
16  #pragma once
17  
18  #include <Arduino.h>
19  
20  struct _adafruit_ZeroCAN_state;
21  
22  class Adafruit_ZeroCAN {
23  public:
24    enum BusState { ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE, BUS_OFF };
25  
26    struct Message {
27      uint32_t id;
28      bool extended;
29      bool rtr;
30      uint8_t size;
31      uint8_t data[8];
32    };
33  
34    struct Match {
35      uint32_t id;
36      uint32_t mask;
37      bool extended;
38    };
39  
40    class Listener {
41    public:
42      Listener();
43      bool begin(Adafruit_ZeroCAN &can, Match *matches, size_t nmatch);
44      ~Listener();
45      bool in_waiting() const;
46      bool read(Message &);
47    };
48  
49    Adafruit_ZeroCAN(uint8_t TX_PIN, uint8_t RX_PIN);
50    Adafruit_ZeroCAN();
51    ~Adafruit_ZeroCAN() {}
52  
53    bool begin(int baudrate, bool loopback, bool silent);
54  
55    int transmitErrorCount() const;
56    int receiveErrorCount() const;
57    BusState busState() const;
58  
59    bool send(const Message &m);
60    void restart();
61  
62  private:
63    struct State;
64    int8_t _tx, _rx;
65    void *_hw;
66    _adafruit_ZeroCAN_state *state;
67  };