00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00033
00034
00035 #ifndef CAN_H
00036 #define CAN_H
00037
00038 #if defined (__cplusplus)
00039 extern "C" {
00040 #endif
00041
00042
00055
00056
00057 #include <avr/pgmspace.h>
00058 #include <stdint.h>
00059 #include <stdbool.h>
00060
00061 #include "config.h"
00062
00063
00068 #define ONLY_NON_RTR 2
00069 #define ONLY_RTR 3
00070
00071
00075 typedef enum {
00076 BITRATE_10_KBPS = 0,
00077 BITRATE_20_KBPS = 1,
00078 BITRATE_50_KBPS = 2,
00079 BITRATE_100_KBPS = 3,
00080 BITRATE_125_KBPS = 4,
00081 BITRATE_250_KBPS = 5,
00082 BITRATE_500_KBPS = 6,
00083 BITRATE_1_MBPS = 7,
00084 } can_bitrate_t;
00085
00090 #define CAN_ALL_FILTER 0xff
00091
00096 #ifndef SUPPORT_EXTENDED_CANID
00097 #define SUPPORT_EXTENDED_CANID 1
00098 #endif
00099
00105 #ifndef SUPPORT_TIMESTAMPS
00106 #define SUPPORT_TIMESTAMPS 0
00107 #endif
00108
00141 #if defined(__DOXYGEN__)
00142
00143 #define MCP2515_FILTER_EXTENDED(id)
00144 #define MCP2515_FILTER(id)
00145
00146 #else
00147
00148 #if SUPPORT_EXTENDED_CANID
00149 #define MCP2515_FILTER_EXTENDED(id) \
00150 (uint8_t) ((uint32_t) (id) >> 21), \
00151 (uint8_t)((((uint32_t) (id) >> 13) & 0xe0) | (1<<3) | \
00152 (((uint32_t) (id) >> 16) & 0x3)), \
00153 (uint8_t) ((uint32_t) (id) >> 8), \
00154 (uint8_t) ((uint32_t) (id))
00155 #endif
00156
00157 #define MCP2515_FILTER(id) \
00158 (uint8_t)((uint32_t) id >> 3), \
00159 (uint8_t)((uint32_t) id << 5), \
00160 0, \
00161 0
00162 #endif
00163
00164
00169 typedef struct
00170 {
00171 #if SUPPORT_EXTENDED_CANID
00172 uint32_t id;
00173 struct {
00174 int rtr : 1;
00175 int extended : 1;
00176 } flags;
00177 #else
00178 uint16_t id;
00179 struct {
00180 int rtr : 1;
00181 } flags;
00182 #endif
00183
00184 uint8_t length;
00185 uint8_t data[8];
00186
00187 #if SUPPORT_TIMESTAMPS
00188 uint16_t timestamp;
00189 #endif
00190 } can_t;
00191
00192
00193
00194
00225 typedef struct
00226 {
00227 #if SUPPORT_EXTENDED_CANID
00228 uint32_t id;
00229 uint32_t mask;
00230 struct {
00231 uint8_t rtr : 2;
00232 uint8_t extended : 2;
00233 } flags;
00234 #else
00235 uint16_t id;
00236 uint16_t mask;
00237 struct {
00238 uint8_t rtr : 2;
00239 } flags;
00240 #endif
00241 } can_filter_t;
00242
00243
00244
00249 typedef struct {
00250 uint8_t rx;
00251 uint8_t tx;
00252 } can_error_register_t;
00253
00254
00259 typedef enum {
00260 LISTEN_ONLY_MODE,
00261 LOOPBACK_MODE,
00262 NORMAL_MODE,
00263 SLEEP_MODE
00264 } can_mode_t;
00265
00266
00276 extern bool
00277 can_init(can_bitrate_t bitrate);
00278
00279
00323 extern void
00324 can_sleep(void);
00325
00326 extern void
00327 can_wakeup(void);
00328
00329
00341 extern bool
00342 can_set_filter(uint8_t number, const can_filter_t *filter);
00343
00344
00355 extern bool
00356 can_disable_filter(uint8_t number);
00357
00358
00391 extern void
00392 can_static_filter(const prog_uint8_t *filter_array);
00393
00394
00420 extern uint8_t
00421 can_get_filter(uint8_t number, can_filter_t *filter);
00422
00423
00430 extern bool
00431 can_check_message(void);
00432
00433
00440 extern bool
00441 can_check_free_buffer(void);
00442
00443
00452 extern uint8_t
00453 can_send_message(const can_t *msg);
00454
00455
00464 extern uint8_t
00465 can_get_message(can_t *msg);
00466
00467
00477 extern can_error_register_t
00478 can_read_error_register(void);
00479
00480
00491 extern bool
00492 can_check_bus_off(void);
00493
00494
00504 extern void
00505 can_reset_bus_off(void);
00506
00507
00514 extern void
00515 can_set_mode(can_mode_t mode);
00516
00517 #if defined (__cplusplus)
00518 }
00519 #endif
00520
00521 #endif // CAN_H