Subversion Repositories HomeAutomation

Rev

Rev 63 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 63 Rev 69
1
#ifndef CAN_H_
1
#ifndef CAN_H_
2
#define CAN_H_
2
#define CAN_H_
3
 
3
 
4
/*-----------------------------------------------------------------------------
4
/*-----------------------------------------------------------------------------
5
 * Includes
5
 * Includes
6
 *---------------------------------------------------------------------------*/
6
 *---------------------------------------------------------------------------*/
7
#include <inttypes.h>
7
#include <inttypes.h>
8
#include <can_cfg.h>
8
#include <can_cfg.h>
9
 
9
 
10
 
10
 
11
/*-----------------------------------------------------------------------------
11
/*-----------------------------------------------------------------------------
12
 * Type Definitions
12
 * Type Definitions
13
 *---------------------------------------------------------------------------*/
13
 *---------------------------------------------------------------------------*/
14
/**
14
/**
15
 * CAN Bitrate type.
15
 * CAN Bitrate type.
16
 */
16
 */
17
typedef enum {
17
typedef enum {
18
    CAN_BITRATE_125K = 0,
18
    CAN_BITRATE_125K = 0,
19
    CAN_BITRATE_250K = 1,
19
    CAN_BITRATE_250K = 1,
20
    CAN_BITRATE_500K = 2,
20
    CAN_BITRATE_500K = 2,
21
    CAN_BITRATE_1M = 3
21
    CAN_BITRATE_1M = 3
22
} CanBitrate_t;
22
} CanBitrate_t;
23
 
23
 
24
/**
24
/**
25
 * CAN Return Type. Most CAN functions return one of these.
25
 * CAN Return Type. Most CAN functions return one of these.
26
 */
26
 */
27
typedef enum {
27
typedef enum {
28
    CAN_OK = 0,
28
    CAN_OK = 0,
29
    CAN_FAILINIT = 1,
29
    CAN_FAILINIT = 1,
30
    CAN_FAILTX = 2,
30
    CAN_FAILTX = 2,
31
    CAN_MSGAVAIL = 3,
31
    CAN_MSGAVAIL = 3,
32
    CAN_NOMSG = 4,
32
    CAN_NOMSG = 4,
33
    CAN_CTRLERROR = 5,
33
    CAN_CTRLERROR = 5,
34
    CAN_FAIL = 0xFF
34
    CAN_FAIL = 0xFF
35
} CanReturn_t;
35
} CanReturn_t;
36
 
36
 
37
/**
37
/**
38
 * CAN Message Type. Stores a complete CAN frame.
38
 * CAN Message Type. Stores a complete CAN frame.
39
 */
39
 */
40
typedef struct {
40
typedef struct {
41
    /*
41
    /*
42
     * ID section. Contains either 29bit ID or 11bit ID (depending on ExtFlag).
42
     * ID section. Contains either 29bit ID or 11bit ID (depending on ExtendedFlag).
43
     */
43
     */
44
    uint32_t Id;
44
    uint32_t Id;
45
   
45
   
46
    /*
46
    /*
47
     * DLC section. Contains the data length expressed in number of bytes. Range [0,8].
47
     * DLC section. Contains the data length expressed in number of bytes. Range [0,8].
48
     */
48
     */
49
    uint8_t DataLength;
49
    uint8_t DataLength;
50
   
50
   
51
    /**
51
    /**
52
     * Extended frame flag. Set to 1 if the ID is 29bit, or 0 if it is 11bit.
52
     * Extended frame flag. Set to 1 if the ID is 29bit, or 0 if it is 11bit.
53
     */
53
     */
54
    uint8_t ExtendedFlag;
54
    uint8_t ExtendedFlag;
55
   
55
   
56
    /**
56
    /**
57
     * Remote frame flag. Set to 1 if the frame is a remote frame. 0 otherwise.
57
     * Remote frame flag. Set to 1 if the frame is a remote frame. 0 otherwise.
58
     */
58
     */
59
    uint8_t RemoteFlag;
59
    uint8_t RemoteFlag;
60
   
60
   
61
    /*
61
    /*
62
     * DATA section. Contains the 8 data bytes.
62
     * DATA section. Contains the 8 data bytes.
63
     */
63
     */
64
    union {
64
    union {
65
        /* read as 8bit unsigned data */
65
        /* read as 8bit unsigned data */
66
        uint8_t bytes[8];
66
        uint8_t bytes[8];
67
       
67
       
68
        /* read as 16bit unsigned data */
68
        /* read as 16bit unsigned data */
69
        uint16_t words[4];
69
        uint16_t words[4];
70
       
70
       
71
        /* read as 32bit unsigned data */
71
        /* read as 32bit unsigned data */
72
        uint32_t dwords[2];
72
        uint32_t dwords[2];
73
    } Data;
73
    } Data;
74
} CanMessage_t;
74
} CanMessage_t;
75
 
75
 
76
 
76
 
77
/*-----------------------------------------------------------------------------
77
/*-----------------------------------------------------------------------------
78
 * Public Function Prototypes
78
 * Public Function Prototypes
79
 *---------------------------------------------------------------------------*/
79
 *---------------------------------------------------------------------------*/
80
 
80
 
81
CanReturn_t CanInit(CanBitrate_t bitrate);
81
CanReturn_t CanInit(CanBitrate_t bitrate);
82
void can_initMessageStruct(CanMessage_t* msg);
82
void can_initMessageStruct(CanMessage_t* msg);
83
CanReturn_t CanSend(CanMessage_t* msg);
83
CanReturn_t CanSend(CanMessage_t* msg);
84
CanReturn_t CanReceiveAvailable(void);
84
CanReturn_t CanReceiveAvailable(void);
85
CanReturn_t CanReceive(CanMessage_t *msg);
85
CanReturn_t CanReceive(CanMessage_t *msg);
86
CanReturn_t CanCheckError(void);
86
CanReturn_t CanCheckError(void);
87
 
87
 
88
#if (CANDEBUG)
88
#if (CANDEBUG)
89
void can_debug1(void);
89
void can_debug1(void);
90
void can_dumpMessage(CanMessage *msg);
90
void can_dumpMessage(CanMessage *msg);
91
uint8_t can_testTransmit(const uint8_t ext, const uint8_t data2);
91
uint8_t can_testTransmit(const uint8_t ext, const uint8_t data2);
92
void can_dumpStatus(void);
92
void can_dumpStatus(void);
93
#endif
93
#endif
94
 
94
 
95
#endif
95
#endif
96
 
96