Subversion Repositories svn.mios32

Rev

Rev 2646 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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