Subversion Repositories svn.mios32

Rev

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

Rev 2652 Rev 2657
1
// $Id: mios32_can.h 2403 2016-08-15 17:47:50Z tk $
1
// $Id: mios32_can.h 2403 2016-08-15 17:47:50Z tk $
2
/*
2
/*
3
 * Header file for CAN functions
3
 * Header file for CAN functions
4
 *
4
 *
5
 * ==========================================================================
5
 * ==========================================================================
6
 *
6
 *
7
 *  Copyright (C) 2008 Thorsten Klose (tk@midibox.org)
7
 *  Copyright (C) 2008 Thorsten Klose (tk@midibox.org)
8
 *  Licensed for personal non-commercial use only.
8
 *  Licensed for personal non-commercial use only.
9
 *  All other rights reserved.
9
 *  All other rights reserved.
10
 *
10
 *
11
 * ==========================================================================
11
 * ==========================================================================
12
 */
12
 */
13
13
14
#ifndef _MIOS32_CAN_H
14
#ifndef _MIOS32_CAN_H
15
#define _MIOS32_CAN_H
15
#define _MIOS32_CAN_H
16
16
17
/////////////////////////////////////////////////////////////////////////////
17
/////////////////////////////////////////////////////////////////////////////
18
// Global definitions
18
// Global definitions
19
/////////////////////////////////////////////////////////////////////////////
19
/////////////////////////////////////////////////////////////////////////////
20
20
21
// number of CAN interfaces (0..2)
21
// number of CAN interfaces (0..2)
22
#if defined(MIOS32_BOARD_STM32F4DISCOVERY) || defined(MIOS32_BOARD_MBHP_CORE_STM32F4)
22
#if defined(MIOS32_BOARD_STM32F4DISCOVERY) || defined(MIOS32_BOARD_MBHP_CORE_STM32F4)
23
#ifndef MIOS32_CAN_NUM
23
#ifndef MIOS32_CAN_NUM
24
#define MIOS32_CAN_NUM 1
24
#define MIOS32_CAN_NUM 1
25
#else
25
#else
26
#if MIOS32_CAN_NUM >1
26
#if MIOS32_CAN_NUM >1
27
#define MIOS32_CAN_NUM 1
27
#define MIOS32_CAN_NUM 1
28
#endif
28
#endif
29
#endif
29
#endif
30
#else
30
#else
31
#define MIOS32_CAN_NUM 0
31
#define MIOS32_CAN_NUM 0
32
# warning "Unsupported MIOS32_BOARD selected!"
32
# warning "Unsupported MIOS32_BOARD selected!"
33
// because of MIDI Area Network Id arbitration and filtering, only STM32F4 is supported.
33
// because of MIDI Area Network Id arbitration and filtering, only STM32F4 is supported.
34
#endif
34
#endif
35
35
36
// Tx buffer size (1..256)
36
// Tx buffer size (1..256)
37
#ifndef MIOS32_CAN_TX_BUFFER_SIZE
37
#ifndef MIOS32_CAN_TX_BUFFER_SIZE
38
#define MIOS32_CAN_TX_BUFFER_SIZE 128
38
#define MIOS32_CAN_TX_BUFFER_SIZE 128
39
// Should be enough for the 1024 bytes of Sysex buffer
39
// Should be enough for the 1024 bytes of Sysex buffer
40
// Note: One CAN packet transmits 8 bytes maximum.
40
// Note: One CAN packet transmits 8 bytes maximum.
41
// We need 1024/8 = 128 packet buffer.
41
// We need 1024/8 = 128 packet buffer.
42
#endif
42
#endif
43
43
44
// Rx buffer size (1..256)
44
// Rx buffer size (1..256)
45
#ifndef MIOS32_CAN_RX_BUFFER_SIZE
45
#ifndef MIOS32_CAN_RX_BUFFER_SIZE
46
#define MIOS32_CAN_RX_BUFFER_SIZE 128
46
#define MIOS32_CAN_RX_BUFFER_SIZE 128
47
#endif
47
#endif
48
48
49
// Interface assignment: 0 = disabled, 1 = MIDI, (2 = MBNET ?), (3 = other ?)
49
// Interface assignment: 0 = disabled, 1 = MIDI, (2 = MBNET ?), (3 = other ?)
50
#ifndef MIOS32_CAN1_ASSIGNMENT
50
#ifndef MIOS32_CAN1_ASSIGNMENT
51
#define MIOS32_CAN1_ASSIGNMENT    1
51
#define MIOS32_CAN1_ASSIGNMENT    1
52
#endif
52
#endif
53
53
54
// Interface assignment: 0 = disabled, 1 = MIDI, (2 = MBNET ?), (3 = other ?)
54
// Interface assignment: 0 = disabled, 1 = MIDI, (2 = MBNET ?), (3 = other ?)
55
#ifndef MIOS32_CAN2_ASSIGNMENT
55
#ifndef MIOS32_CAN2_ASSIGNMENT
56
#define MIOS32_CAN2_ASSIGNMENT    0
56
#define MIOS32_CAN2_ASSIGNMENT    0
57
#endif
57
#endif
58
58
59
// Alternate function pin assignement.
59
// Alternate function pin assignement.
60
//  0: CAN2.RX->PB5, CAN2.TX->PB6
60
//  0: CAN2.RX->PB5, CAN2.TX->PB6
61
//  1: CAN2.RX->PB12, CAN2.TX->PB13
61
//  1: CAN2.RX->PB12, CAN2.TX->PB13
62
#ifndef MIOS32_CAN2_ALTFUNC
62
#ifndef MIOS32_CAN2_ALTFUNC
63
#define MIOS32_CAN2_ALTFUNC       0
63
#define MIOS32_CAN2_ALTFUNC       0
64
#endif
64
#endif
65
65
66
#define MIOS32_CAN_VERBOSE        1
66
#define MIOS32_CAN_VERBOSE        1
67
67
68
68
69
69
70
/////////////////////////////////////////////////////////////////////////////
70
/////////////////////////////////////////////////////////////////////////////
71
// Global Types
71
// Global Types
72
/////////////////////////////////////////////////////////////////////////////
72
/////////////////////////////////////////////////////////////////////////////
73
73
74
// CAN Standard 11bits Id(16bis reg), used for 16bits filtering only!
74
// CAN Standard 11bits Id(16bis reg), used for 16bits filtering only!
75
typedef union {
75
typedef union {
76
  u16 ALL;
76
  u16 ALL;
77
  struct {
77
  struct {
78
    u16 :3;
78
    u16 :3;
79
    u16 ide:1;
79
    u16 ide:1;
80
    u16 rtr:1;
80
    u16 rtr:1;
81
    u16 :11;
81
    u16 :11;
82
  };
82
  };
83
 
83
 
84
} can_std_id_t;
84
} mios32_can_std_id_t;
85
85
86
// CAN Extended 29bits Id(32Bits reg), CAN_RIxR/CAN_TIxR
86
// CAN Extended 29bits Id(32Bits reg), CAN_RIxR/CAN_TIxR
87
typedef union {
87
typedef union {
88
  u32 ALL;
88
  u32 ALL;
89
  struct {
89
  struct {
90
    u16 data[2];
90
    u16 data[2];
91
  };
91
  };
92
  struct {
92
  struct {
93
    u16 data_l;
93
    u16 data_l;
94
    u16 data_h;
94
    u16 data_h;
95
  };
95
  };
96
  struct {
96
  struct {
97
    u32 TXRQ:1;
97
    u32 TXRQ:1;
98
    u32 RTR:1;
98
    u32 RTR:1;
99
    u32 IDE:1;
99
    u32 IDE:1;
100
    u32 :29;
100
    u32 :29;
101
  };
101
  };
102
  struct {
102
  struct {
103
    u32 txrq:1;
103
    u32 txrq:1;
104
    u32 rtr:1;
104
    u32 rtr:1;
105
    u32 ide:1;
105
    u32 ide:1;
106
    u32 :29;
106
    u32 :29;
107
  };
107
  };
108
  struct {
108
  struct {
109
    u32 :21;
109
    u32 :21;
110
    u32 std_id:11;
110
    u32 std_id:11;
111
  };
111
  };
112
  struct {
112
  struct {
113
    u32 :1;
113
    u32 :1;
114
    u32 lpc_ctrl:2; // special field for lpc,
114
    u32 lpc_ctrl:2; // special field for lpc,
115
    u32 ext_id:29;
115
    u32 ext_id:29;
116
  };
116
  };
117
} can_ext_id_t;
117
} mios32_can_ext_id_t;
118
118
119
// CAN control registers CAN_TDTxR/CAN_RDTxR
119
// CAN control registers CAN_TDTxR/CAN_RDTxR
120
typedef union {
120
typedef union {
121
  u32 ALL;
121
  u32 ALL;
122
  struct {
122
  struct {
123
    u32 dlc:4;
123
    u32 dlc:4;
124
    u32 :2;
124
    u32 :2;
125
    u32 fmi:10;
125
    u32 fmi:10;
126
    u32 time:16;
126
    u32 time:16;
127
  };
127
  };
128
  struct {
128
  struct {
129
    u32 DLC:4;
129
    u32 DLC:4;
130
    u32 :4;
130
    u32 :4;
131
    u32 tgt:1;
131
    u32 tgt:1;
132
    u32 :23;
132
    u32 :23;
133
  };
133
  };
134
} can_ctrl_t;
134
} mios32_can_ctrl_t;
135
135
136
// CAN data registers CAN_TDLxR/CAN_TDHxR/CAN_RDLxR/CAN_RDHxR
136
// CAN data registers CAN_TDLxR/CAN_TDHxR/CAN_RDLxR/CAN_RDHxR
137
typedef union {
137
typedef union {
138
  struct {
138
  struct {
139
    u32 ALL[2];
139
    u32 ALL[2];
140
  };
140
  };
141
  struct {
141
  struct {
142
    u32 data_l;
142
    u32 data_l;
143
    u32 data_h;
143
    u32 data_h;
144
  };
144
  };
145
  struct {
145
  struct {
146
    u16 data[4];
146
    u16 data[4];
147
  };
147
  };
148
  struct {
148
  struct {
149
    u8 bytes[8];
149
    u8 bytes[8];
150
  };
150
  };
151
} can_data_t;
151
} mios32_can_data_t;
152
152
153
// CAN mailboxes packet
153
// CAN mailboxes packet
154
typedef struct can_packet_t {
154
typedef struct mios32_can_packet_t {
155
  can_ext_id_t id;
155
  mios32_can_ext_id_t id;
156
  can_ctrl_t ctrl;
156
  mios32_can_ctrl_t ctrl;
157
  can_data_t data;
157
  mios32_can_data_t data;
158
} can_packet_t;
158
} mios32_can_packet_t;
159
159
160
// CAN 16bits filter
160
// CAN 16bits filter
161
typedef struct can_std_filter_t {
161
typedef struct mios32_can_std_filter_t {
162
  can_std_id_t   filt;
162
  mios32_can_std_id_t   filt;
163
  can_std_id_t   mask;
163
  mios32_can_std_id_t   mask;
164
} can_std_filter_t;
164
} mios32_can_std_filter_t;
165
165
166
// CAN 32bits filter
166
// CAN 32bits filter
167
typedef struct can_ext_filter_t {
167
typedef struct mios32_can_ext_filter_t {
168
  can_ext_id_t   filt;
168
  mios32_can_ext_id_t   filt;
169
  can_ext_id_t   mask;
169
  mios32_can_ext_id_t   mask;
170
} can_ext_filter_t;
170
} mios32_can_ext_filter_t;
171
171
172
// CAN bus state
172
// CAN bus state
173
typedef enum {
173
typedef enum {
174
  BUS_OK = 2,
174
  BUS_OK = 2,
175
  WARNING = 1,
175
  WARNING = 1,
176
  PASSIVE = 0,
176
  PASSIVE = 0,
177
  BUS_OFF = -1
177
  BUS_OFF = -1
178
} can_bus_stat_t;
178
} mios32_can_bus_stat_t;
179
179
180
// CAN error staus
180
// CAN error staus
181
typedef union {
181
typedef union {
182
  struct {
182
  struct {
183
    u32 ALL;
183
    u32 ALL;
184
  };
184
  };
185
//  struct {
185
//  struct {
186
//    u32 rec:8;
186
//    u32 rec:8;
187
//    u32 tec:8;
187
//    u32 tec:8;
188
//    u32 :9;
188
//    u32 :9;
189
//    u32 lec:3;
189
//    u32 lec:3;
190
//    u32 :1;
190
//    u32 :1;
191
//    u32 boff:1;
191
//    u32 boff:1;
192
//    u32 epvf:1;
192
//    u32 epvf:1;
193
//    u32 ewgf:1;
193
//    u32 ewgf:1;
194
//  };
194
//  };
195
  struct {
195
  struct {
196
    u32 ewgf:1;
196
    u32 ewgf:1;
197
    u32 epvf:1;
197
    u32 epvf:1;
198
    u32 boff:1;
198
    u32 boff:1;
199
    u32 :1;
199
    u32 :1;
200
    u32 lec:3;
200
    u32 lec:3;
201
    u32 :9;
201
    u32 :9;
202
    u32 tec:8;
202
    u32 tec:8;
203
    u32 rec:8;
203
    u32 rec:8;
204
  };
204
  };
205
} can_stat_err_t;
205
} mios32_can_stat_err_t;
206
206
207
// CAN status report
207
// CAN status report
208
typedef struct can_stat_report_t {
208
typedef struct mios32_can_stat_report_t {
209
  u32            rx_packets_err;
209
  u32            rx_packets_err;
210
  u32            tx_packets_ctr;
210
  u32            tx_packets_ctr;
211
  u32            rx_packets_ctr;
211
  u32            rx_packets_ctr;
212
  u32            rx_buff_err_ctr;
212
  u32            rx_buff_err_ctr;
213
  u8             rx_last_buff_err;
213
  u8             rx_last_buff_err;
214
  can_bus_stat_t bus_state;
214
  mios32_can_bus_stat_t bus_state;
215
  can_stat_err_t bus_curr_err;
215
  mios32_can_stat_err_t bus_curr_err;
216
  can_stat_err_t bus_last_err;
216
  mios32_can_stat_err_t bus_last_err;
217
} can_stat_report_t;
217
} mios32_can_stat_report_t;
218
218
219
/////////////////////////////////////////////////////////////////////////////
219
/////////////////////////////////////////////////////////////////////////////
220
// Prototypes
220
// Prototypes
221
/////////////////////////////////////////////////////////////////////////////
221
/////////////////////////////////////////////////////////////////////////////
222
222
223
extern s32 MIOS32_CAN_VerboseSet(u8 level);
223
extern s32 MIOS32_CAN_VerboseSet(u8 level);
224
extern s32 MIOS32_CAN_VerboseGet(void);
224
extern s32 MIOS32_CAN_VerboseGet(void);
225
extern s32 MIOS32_CAN_Init(u32 mode);
225
extern s32 MIOS32_CAN_Init(u32 mode);
226
226
227
extern s32 MIOS32_CAN_IsAssignedToMIDI(u8 can);
227
extern s32 MIOS32_CAN_IsAssignedToMIDI(u8 can);
228
228
229
extern s32 MIOS32_CAN_InitPort(u8 can, u8 is_midi);
229
extern s32 MIOS32_CAN_InitPort(u8 can, u8 is_midi);
230
extern s32 MIOS32_CAN_InitPortDefault(u8 can);
230
extern s32 MIOS32_CAN_InitPortDefault(u8 can);
231
extern s32 MIOS32_CAN_InitPeriph(u8 can);
231
extern s32 MIOS32_CAN_InitPeriph(u8 can);
232
extern s32 MIOS32_CAN_Init32bitFilter(u8 bank, u8 fifo, can_ext_filter_t filter, u8 enabled);
232
extern s32 MIOS32_CAN_Init32bitFilter(u8 bank, u8 fifo, mios32_can_ext_filter_t filter, u8 enabled);
233
extern s32 MIOS32_CAN_Init16bitFilter(u8 bank, u8 fifo, can_std_filter_t filter1, can_std_filter_t filter2, u8 enabled);
233
extern s32 MIOS32_CAN_Init16bitFilter(u8 bank, u8 fifo, mios32_can_std_filter_t filter1, mios32_can_std_filter_t filter2, u8 enabled);
234
extern s32 MIOS32_CAN_InitPacket(can_packet_t *packet);
234
extern s32 MIOS32_CAN_InitPacket(mios32_can_packet_t *packet);
235
235
236
extern s32 MIOS32_CAN_RxBufferFree(u8 can);
236
extern s32 MIOS32_CAN_RxBufferFree(u8 can);
237
extern s32 MIOS32_CAN_RxBufferUsed(u8 can);
237
extern s32 MIOS32_CAN_RxBufferUsed(u8 can);
238
extern s32 MIOS32_CAN_RxBufferGet(u8 can, can_packet_t *p);
238
extern s32 MIOS32_CAN_RxBufferGet(u8 can, mios32_can_packet_t *p);
239
extern s32 MIOS32_CAN_RxBufferPeek(u8 can, can_packet_t *p);
239
extern s32 MIOS32_CAN_RxBufferPeek(u8 can, mios32_can_packet_t *p);
240
extern s32 MIOS32_CAN_RxBufferRemove(u8 can);
240
extern s32 MIOS32_CAN_RxBufferRemove(u8 can);
241
extern s32 MIOS32_CAN_RxBufferPut(u8 can, can_packet_t p);
241
extern s32 MIOS32_CAN_RxBufferPut(u8 can, mios32_can_packet_t p);
242
242
243
extern s32 MIOS32_CAN_TxBufferFree(u8 can);
243
extern s32 MIOS32_CAN_TxBufferFree(u8 can);
244
extern s32 MIOS32_CAN_TxBufferUsed(u8 can);
244
extern s32 MIOS32_CAN_TxBufferUsed(u8 can);
245
extern s32 MIOS32_CAN_TxBufferGet(u8 can, can_packet_t *p);
245
extern s32 MIOS32_CAN_TxBufferGet(u8 can, mios32_can_packet_t *p);
246
extern s32 MIOS32_CAN_TxBufferPutMore_NonBlocking(u8 can, can_packet_t* p,u16 len);
246
extern s32 MIOS32_CAN_TxBufferPutMore_NonBlocking(u8 can, mios32_can_packet_t* p,u16 len);
247
extern s32 MIOS32_CAN_TxBufferPutMore(u8 can, can_packet_t *packets, u16 len);
247
extern s32 MIOS32_CAN_TxBufferPutMore(u8 can, mios32_can_packet_t *packets, u16 len);
248
extern s32 MIOS32_CAN_TxBufferPut_NonBlocking(u8 can, can_packet_t p);
248
extern s32 MIOS32_CAN_TxBufferPut_NonBlocking(u8 can, mios32_can_packet_t p);
249
extern s32 MIOS32_CAN_TxBufferPut(u8 can, can_packet_t p);
249
extern s32 MIOS32_CAN_TxBufferPut(u8 can, mios32_can_packet_t p);
250
250
251
extern s32 MIOS32_CAN_BusErrorCheck(u8 can);
251
extern s32 MIOS32_CAN_BusErrorCheck(u8 can);
252
252
253
extern s32 MIOS32_CAN_Transmit(u8 can, can_packet_t p, s16 block_time);
253
extern s32 MIOS32_CAN_Transmit(u8 can, mios32_can_packet_t p, s16 block_time);
254
254
255
extern s32 MIOS32_CAN_ReportLastErr(u8 can, can_stat_err_t* err);
255
extern s32 MIOS32_CAN_ReportLastErr(u8 can, mios32_can_stat_err_t* err);
256
extern s32 MIOS32_CAN_ReportGetCurr(u8 can, can_stat_report_t* report);
256
extern s32 MIOS32_CAN_ReportGetCurr(u8 can, mios32_can_stat_report_t* report);
257
extern s32 MIOS32_CAN_ReportReset(u8 can);
257
extern s32 MIOS32_CAN_ReportReset(u8 can);
258
/////////////////////////////////////////////////////////////////////////////
258
/////////////////////////////////////////////////////////////////////////////
259
// Export global variables
259
// Export global variables
260
/////////////////////////////////////////////////////////////////////////////
260
/////////////////////////////////////////////////////////////////////////////
261
extern u32 can_temp;
-
 
262
261
263
#endif /* _MIOS32_CAN_H */
262
#endif /* _MIOS32_CAN_H */
264
 
263