123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- #ifndef CAN_H
- #define CAN_H
- #if defined (__cplusplus)
- extern "C" {
- #endif
- #include <avr/pgmspace.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include "config.h"
- #define ONLY_NON_RTR 2
- #define ONLY_RTR 3
- typedef enum {
- BITRATE_10_KBPS = 0,
- BITRATE_20_KBPS = 1,
- BITRATE_50_KBPS = 2,
- BITRATE_100_KBPS = 3,
- BITRATE_125_KBPS = 4,
- BITRATE_250_KBPS = 5,
- BITRATE_500_KBPS = 6,
- BITRATE_1_MBPS = 7,
- } can_bitrate_t;
- #define CAN_ALL_FILTER 0xff
- #ifndef SUPPORT_EXTENDED_CANID
- #define SUPPORT_EXTENDED_CANID 1
- #endif
- #ifndef SUPPORT_TIMESTAMPS
- #define SUPPORT_TIMESTAMPS 0
- #endif
- #if defined(__DOXYGEN__)
- #define MCP2515_FILTER_EXTENDED(id)
- #define MCP2515_FILTER(id)
- #else
- #if SUPPORT_EXTENDED_CANID
- #define MCP2515_FILTER_EXTENDED(id) \
- (uint8_t) ((uint32_t) (id) >> 21), \
- (uint8_t)((((uint32_t) (id) >> 13) & 0xe0) | (1<<3) | \
- (((uint32_t) (id) >> 16) & 0x3)), \
- (uint8_t) ((uint32_t) (id) >> 8), \
- (uint8_t) ((uint32_t) (id))
- #endif
-
- #define MCP2515_FILTER(id) \
- (uint8_t)((uint32_t) id >> 3), \
- (uint8_t)((uint32_t) id << 5), \
- 0, \
- 0
- #endif
- typedef struct
- {
- #if SUPPORT_EXTENDED_CANID
- uint32_t id;
- struct {
- int rtr : 1;
- int extended : 1;
- } flags;
- #else
- uint16_t id;
- struct {
- int rtr : 1;
- } flags;
- #endif
-
- uint8_t length;
- uint8_t data[8];
-
- #if SUPPORT_TIMESTAMPS
- uint16_t timestamp;
- #endif
- } can_t;
- typedef struct
- {
- #if SUPPORT_EXTENDED_CANID
- uint32_t id;
- uint32_t mask;
- struct {
- uint8_t rtr : 2;
- uint8_t extended : 2;
- } flags;
- #else
- uint16_t id;
- uint16_t mask;
- struct {
- uint8_t rtr : 2;
- } flags;
- #endif
- } can_filter_t;
- typedef struct {
- uint8_t rx;
- uint8_t tx;
- } can_error_register_t;
- typedef enum {
- LISTEN_ONLY_MODE,
- LOOPBACK_MODE,
- NORMAL_MODE,
- SLEEP_MODE
- } can_mode_t;
- extern bool
- can_init(can_bitrate_t bitrate);
- extern void
- can_sleep(void);
- extern void
- can_wakeup(void);
- extern bool
- can_set_filter(uint8_t number, const can_filter_t *filter);
- extern bool
- can_disable_filter(uint8_t number);
- extern void
- can_static_filter(const uint8_t *filter_array);
- extern uint8_t
- can_get_filter(uint8_t number, can_filter_t *filter);
- extern bool
- can_check_message(void);
- extern bool
- can_check_free_buffer(void);
- extern uint8_t
- can_send_message(const can_t *msg);
- extern uint8_t
- can_get_message(can_t *msg);
- extern can_error_register_t
- can_read_error_register(void);
- extern bool
- can_check_bus_off(void);
- extern void
- can_reset_bus_off(void);
- extern void
- can_set_mode(can_mode_t mode);
- #if defined (__cplusplus)
- }
- #endif
- #endif
|