2008-12-03 05:40:16 +00:00
|
|
|
#ifndef CAN_H
|
|
|
|
#define CAN_H
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Simple CAN Library
|
|
|
|
*
|
2012-05-06 18:45:43 +00:00
|
|
|
* #define CAN_INTERRUPT 1 // set this to enable interrupt driven
|
|
|
|
* // and buffering version
|
|
|
|
* #define CAN_RX_BUFFER_SIZE 2 // only used for Interrupt
|
|
|
|
* #define CAN_TX_BUFFER_SIZE 2 // only used for Interrupt
|
2008-12-03 05:40:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Types
|
|
|
|
*/
|
|
|
|
|
2012-05-06 18:45:43 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
typedef unsigned char can_addr_t;
|
|
|
|
typedef unsigned char can_port_t;
|
2008-12-03 05:40:16 +00:00
|
|
|
typedef uint16_t can_channel_t;
|
|
|
|
typedef uint8_t can_subchannel_t;
|
|
|
|
|
2012-05-06 18:45:43 +00:00
|
|
|
typedef struct {
|
2008-12-03 05:40:16 +00:00
|
|
|
uint32_t id;
|
|
|
|
uint8_t dlc;
|
|
|
|
uint8_t data[8];
|
|
|
|
} can_message_raw;
|
|
|
|
|
2012-05-06 18:45:43 +00:00
|
|
|
typedef struct {
|
|
|
|
can_addr_t addr_src;
|
|
|
|
can_addr_t addr_dst;
|
|
|
|
can_port_t port_src;
|
|
|
|
can_port_t port_dst;
|
2008-12-03 05:40:16 +00:00
|
|
|
unsigned char dlc;
|
|
|
|
unsigned char data[8];
|
2012-05-06 18:45:43 +00:00
|
|
|
} can_message;
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2012-05-06 18:45:43 +00:00
|
|
|
typedef struct {
|
|
|
|
can_channel_t channel;
|
2008-12-03 05:40:16 +00:00
|
|
|
can_subchannel_t subchannel;
|
2012-05-06 18:45:43 +00:00
|
|
|
can_addr_t addr_src;
|
|
|
|
can_addr_t addr_dst;
|
|
|
|
uint8_t dlc;
|
|
|
|
uint8_t data[8];
|
2008-12-03 05:40:16 +00:00
|
|
|
} can_message_v2;
|
|
|
|
|
2012-05-06 18:45:43 +00:00
|
|
|
typedef enum {
|
|
|
|
normal, mode_sleep, loopback, listenonly, config
|
|
|
|
} can_mode_t;
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Global variables
|
|
|
|
*/
|
|
|
|
#ifdef CAN_HANDLEERROR
|
2012-05-06 18:45:43 +00:00
|
|
|
extern unsigned char can_error;
|
2008-12-03 05:40:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Management
|
|
|
|
*/
|
|
|
|
|
|
|
|
void can_init();
|
|
|
|
void can_setfilter();
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_setmode( can_mode_t);
|
2008-12-03 05:40:16 +00:00
|
|
|
void can_setled(unsigned char led, unsigned char state);
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Sending
|
|
|
|
*/
|
|
|
|
|
|
|
|
can_message * can_buffer_get();
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_transmit(can_message *msg);
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Receiving
|
|
|
|
*/
|
|
|
|
|
|
|
|
can_message *can_get();
|
|
|
|
can_message *can_get_nb();
|
|
|
|
|
|
|
|
// this is only needed for Interrupt driven Version
|
|
|
|
#ifndef CAN_INTERRUPT
|
|
|
|
// # define can_free(m)
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_free(can_message * msg);
|
2008-12-03 05:40:16 +00:00
|
|
|
#else
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_free(can_message * msg);
|
2008-12-03 05:40:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Sending
|
|
|
|
*/
|
|
|
|
|
|
|
|
can_message_raw * can_buffer_get_raw();
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_transmit_raw(can_message_raw *msg);
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Receiving
|
|
|
|
*/
|
|
|
|
|
|
|
|
can_message_raw *can_get_raw();
|
|
|
|
can_message_raw *can_get_raw_nb();
|
|
|
|
|
|
|
|
// this is only needed for Interrupt driven Version
|
|
|
|
#ifndef CAN_INTERRUPT
|
|
|
|
// # define can_free(m)
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_free_raw(can_message_raw * msg);
|
2008-12-03 05:40:16 +00:00
|
|
|
#else
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_free_raw(can_message_raw * msg);
|
2008-12-03 05:40:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Sending
|
|
|
|
*/
|
|
|
|
|
2012-05-06 18:45:43 +00:00
|
|
|
void can_transmit_v2(can_message_v2 *msg);
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Receiving
|
|
|
|
*/
|
|
|
|
|
|
|
|
can_message_v2 *can_get_v2_nb();
|
|
|
|
|
|
|
|
void can_free_v2(can_message_v2 *msg);
|
|
|
|
|
|
|
|
#endif
|