-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathECANLib.h
More file actions
179 lines (140 loc) · 6.9 KB
/
Copy pathECANLib.h
File metadata and controls
179 lines (140 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef _ECAN_LIB_
#define _ECAN_LIB_
//////////////////////////////////////////////////////////////////////////
// CAN_OP_MODE
//////////////////////////////////////////////////////////////////////////
const char
_CAN_MODE_BITS = 0xE0, // Use this to access opmode bits
_CAN_MODE_NORMAL = 0x00,
_CAN_MODE_SLEEP = 0x20,
_CAN_MODE_LOOP = 0x40,
_CAN_MODE_LISTEN = 0x60,
_CAN_MODE_CONFIG = 0x80;
//////////////////////////////////////////////////////////////////////////
// CAN_CONFIG_FLAGS
//////////////////////////////////////////////////////////////////////////
//CAN_CONFIG_FLAGS constants define flags related to CAN module configuration.
//Functions ECANInitialize and ECANSetBaudRate expect one of these (or a bitwise combination) as their argument
const char
_CAN_CONFIG_DEFAULT = 0xFF, // 11111111
_CAN_CONFIG_PHSEG2_PRG_BIT = 0x01,
_CAN_CONFIG_PHSEG2_PRG_ON = 0xFF, // XXXXXXX1
_CAN_CONFIG_PHSEG2_PRG_OFF = 0xFE, // XXXXXXX0
_CAN_CONFIG_LINE_FILTER_BIT = 0x02,
_CAN_CONFIG_LINE_FILTER_ON = 0xFF, // XXXXXX1X
_CAN_CONFIG_LINE_FILTER_OFF = 0xFD, // XXXXXX0X
_CAN_CONFIG_SAMPLE_BIT = 0x04,
_CAN_CONFIG_SAMPLE_ONCE = 0xFF, // XXXXX1XX
_CAN_CONFIG_SAMPLE_THRICE = 0xFB, // XXXXX0XX
_CAN_CONFIG_MSG_TYPE_BIT = 0x08,
_CAN_CONFIG_STD_MSG = 0xFF, // XXXX1XXX
_CAN_CONFIG_XTD_MSG = 0xF7, // XXXX0XXX
_CAN_CONFIG_DBL_BUFFER_BIT = 0x10,
_CAN_CONFIG_DBL_BUFFER_ON = 0xFF, // XXX1XXXX
_CAN_CONFIG_DBL_BUFFER_OFF = 0xEF, // XXX0XXXX
_CAN_CONFIG_MSG_BITS = 0x60,
_CAN_CONFIG_ALL_MSG = 0xFF, // X11XXXXX
_CAN_CONFIG_VALID_XTD_MSG = 0xDF, // X10XXXXX
_CAN_CONFIG_VALID_STD_MSG = 0xBF, // X01XXXXX
_CAN_CONFIG_ALL_VALID_MSG = 0x9F; // X00XXXXX
//////////////////////////////////////////////////////////////////////////
// CAN_TX_MSG_FLAGS
//////////////////////////////////////////////////////////////////////////
const char
_CAN_TX_PRIORITY_BITS = 0x03,
_CAN_TX_PRIORITY_0 = 0xFC, // XXXXXX00
_CAN_TX_PRIORITY_1 = 0xFD, // XXXXXX01
_CAN_TX_PRIORITY_2 = 0xFE, // XXXXXX10
_CAN_TX_PRIORITY_3 = 0xFF, // XXXXXX11
_CAN_TX_FRAME_BIT = 0x08,
_CAN_TX_STD_FRAME = 0xFF, // XXXXX1XX
_CAN_TX_XTD_FRAME = 0xF7, // XXXXX0XX
_CAN_TX_RTR_BIT = 0x40,
_CAN_TX_NO_RTR_FRAME = 0xFF, // X1XXXXXX
_CAN_TX_RTR_FRAME = 0xBF; // X0XXXXXX
//////////////////////////////////////////////////////////////////////////
// CAN_RX_MSG_FLAGS
//////////////////////////////////////////////////////////////////////////
const char
_CAN_RX_FILTER_BITS = 0x07, // Use this to access filter bits
_CAN_RX_FILTER_1 = 0x00,
_CAN_RX_FILTER_2 = 0x01,
_CAN_RX_FILTER_3 = 0x02,
_CAN_RX_FILTER_4 = 0x03,
_CAN_RX_FILTER_5 = 0x04,
_CAN_RX_FILTER_6 = 0x05,
_CAN_RX_OVERFLOW = 0x08, // Set if Overflowed else cleared
_CAN_RX_INVALID_MSG = 0x10, // Set if invalid else cleared
_CAN_RX_XTD_FRAME = 0x20, // Set if XTD message else cleared
_CAN_RX_RTR_FRAME = 0x40, // Set if RTR message else cleared
_CAN_RX_DBL_BUFFERED = 0x80; // Set if this message was hardware double-buffered
//////////////////////////////////////////////////////////////////////////
// CAN_MASK
//////////////////////////////////////////////////////////////////////////
const char
_CAN_MASK_B1 = 0,
_CAN_MASK_B2 = 1;
//////////////////////////////////////////////////////////////////////////
// CAN_FILTER
//////////////////////////////////////////////////////////////////////////
const char
_CAN_FILTER_B1_F1 = 0,
_CAN_FILTER_B1_F2 = 1,
_CAN_FILTER_B2_F1 = 2,
_CAN_FILTER_B2_F2 = 3,
_CAN_FILTER_B2_F3 = 4,
_CAN_FILTER_B2_F4 = 5;
//////////////////////////////////////////////////////////////////////////
// CAN_TX_IDLE_LEVEL
//////////////////////////////////////////////////////////////////////////
const char
_CAN_DRIVE_HIGH_STATE_ENABLE = 0x00,
_CAN_DRIVE_HIGH_STATE_DISABLE = 0x01;
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/**
* @brief Initializes CAN. All pending transmissions are aborted. Sets all mask registers to 0 to allow all messages.
* @param SJW as defined in datasheet
* @param BRP as defined in datasheet
* @param PHSEG1 as defined in datasheet
* @param PHSEG2 as defined in datasheet
* @param PROPSEG as defined in datasheet
* @param CAN_CONFIG_FLAGS
*/
void ECANInitialize(char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CAN_CONFIG_FLAGS);
void ECANSetBaudRate(char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CAN_CONFIG_FLAGS);
/**
* @brief Sets ECAN to requested mode, i.e. copies mode to CANSTAT. Parameter mode needs to be one of CAN_OP_MODE constants (see CAN constants).
Parameter wait_flag needs to be either 0 or 0xFF:
- If set to 0xFF, this is a blocking call – the function won’t “return” until the requested mode is set.
- If 0, this is a non-blocking call. It does not verify if CAN module is switched to requested mode or not.
Caller must use CANGetOperationMode to verify correct operation mode before performing mode specific operation.
* @param mode Can be
* There are three modes:
* Mode 1: Fully backward compatible Legacy mode
* Mode 2: Enhanced Legacy mode
* Mode 3: Hardware FIFO mode
* @param wait_flag
*/
void ECANSetOperationMode(unsigned short mode, unsigned short wait_flag);
void ECANSetMask(char CAN_MASK, long value, char CAN_CONFIG_FLAGS);
void ECANSetFilter(char CAN_FILTER, long value, char CAN_CONFIG_FLAGS);
/**
* @brief This function sets the state of CANTX pin when recessive.
* @param driveHighState State of the CANTX pin. Valid values:
* - CAN_DRIVE_HIGH_STATE_ENABLE: CANTX pin will drive VDD when recessive.
Use it when using a differential bus to avoid signal crosstalk in CANTX from other nearby pins.
* - _CAN_DRIVE_HIGH_STATE_DISABLE: CANTX pin will be tri-state when recessive.
* @req Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus.
*/
void ECANSetTxIdleLevel(char driveHighState);
void ECANRead(long *id, char *data, char *datalen, char *CAN_RX_MSG_FLAGS);
/**
*
* @param id is CAN message identifier. Only 11 or 29 bits may be used depending on message type (standard or extended)
* @param data is array of bytes up to 8 bytes in length
* @param datalen is data length from 1–8
* @param CAN_TX_MSG_FLAGS is value formed from constants (see CAN constants)
*/
void ECANWrite(long id, char *data, char datalen, char CAN_TX_MSG_FLAGS);
#endif