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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/*
* Copyright (c) 2015-2018 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Driver_CAN.h"
#define ARM_CAN_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(1,0) // CAN driver version
// Driver Version
static const ARM_DRIVER_VERSION can_driver_version = { ARM_CAN_API_VERSION, ARM_CAN_DRV_VERSION };
// Driver Capabilities
static const ARM_CAN_CAPABILITIES can_driver_capabilities = {
32U, // Number of CAN Objects available
1U, // Supports reentrant calls to ARM_CAN_MessageSend, ARM_CAN_MessageRead, ARM_CAN_ObjectConfigure and abort message sending used by ARM_CAN_Control.
0U, // Does not support CAN with Flexible Data-rate mode (CAN_FD)
0U, // Does not support restricted operation mode
1U, // Supports bus monitoring mode
1U, // Supports internal loopback mode
1U, // Supports external loopback mode
};
// Object Capabilities
static const ARM_CAN_OBJ_CAPABILITIES can_object_capabilities = {
1U, // Object supports transmission
1U, // Object supports reception
0U, // Object does not support RTR reception and automatic Data transmission
0U, // Object does not support RTR transmission and automatic Data reception
1U, // Object allows assignment of multiple filters to it
1U, // Object supports exact identifier filtering
0U, // Object does not support range identifier filtering
1U, // Object supports mask identifier filtering
3U // Object can buffer 3 messages
};
static uint8_t can_driver_powered = 0U;
static uint8_t can_driver_initialized = 0U;
static ARM_CAN_SignalUnitEvent_t CAN_SignalUnitEvent = NULL;
static ARM_CAN_SignalObjectEvent_t CAN_SignalObjectEvent = NULL;
//
// Functions
//
static ARM_DRIVER_VERSION CAN_GetVersion (void) {
// Return driver version
return can_driver_version;
}
static ARM_CAN_CAPABILITIES CAN_GetCapabilities (void) {
// Return driver capabilities
return can_driver_capabilities;
}
static int32_t CAN_Initialize (ARM_CAN_SignalUnitEvent_t cb_unit_event,
ARM_CAN_SignalObjectEvent_t cb_object_event) {
if (can_driver_initialized != 0U) { return ARM_DRIVER_OK; }
CAN_SignalUnitEvent = cb_unit_event;
CAN_SignalObjectEvent = cb_object_event;
// Add code for pin, memory, RTX objects initialization
// ..
can_driver_initialized = 1U;
return ARM_DRIVER_OK;
}
static int32_t CAN_Uninitialize (void) {
// Add code for pin, memory, RTX objects de-initialization
// ..
can_driver_initialized = 0U;
return ARM_DRIVER_OK;
}
static int32_t CAN_PowerControl (ARM_POWER_STATE state) {
switch (state) {
case ARM_POWER_OFF:
can_driver_powered = 0U;
// Add code to disable interrupts and put peripheral into reset mode,
// and if possible disable clock
// ..
case ARM_POWER_FULL:
if (can_driver_initialized == 0U) { return ARM_DRIVER_ERROR; }
if (can_driver_powered != 0U) { return ARM_DRIVER_OK; }
// Add code to enable clocks, reset variables enable interrupts
// and put peripheral into operational
// ..
can_driver_powered = 1U;
break;
default:
// Other states are not supported
return ARM_DRIVER_ERROR_UNSUPPORTED;
}
return ARM_DRIVER_OK;
}
uint32_t CAN_GetClock (void) {
// Add code to return peripheral clock frequency
// ..
}
static int32_t CAN_SetBitrate (ARM_CAN_BITRATE_SELECT select, uint32_t bitrate, uint32_t bit_segments) {
if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
// Add code to setup peripheral parameters to generate specified bitrate
// with specified bit segments
// ..
return ARM_DRIVER_OK;
}
static int32_t CAN_SetMode (ARM_CAN_MODE mode) {
if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
switch (mode) {
case ARM_CAN_MODE_INITIALIZATION:
// Add code to put peripheral into initialization mode
// ..
break;
case ARM_CAN_MODE_NORMAL:
// Add code to put peripheral into normal operation mode
// ..
break;
case ARM_CAN_MODE_RESTRICTED:
// Add code to put peripheral into restricted operation mode
// ..
break;
case ARM_CAN_MODE_MONITOR:
// Add code to put peripheral into bus monitoring mode
// ..
break;
case ARM_CAN_MODE_LOOPBACK_INTERNAL:
// Add code to put peripheral into internal loopback mode
// ..
break;
case ARM_CAN_MODE_LOOPBACK_EXTERNAL:
// Add code to put peripheral into external loopback mode
// ..
break;
default:
// Handle unknown mode code
return ARM_DRIVER_ERROR_UNSUPPORTED;
}
return ARM_DRIVER_OK;
}
ARM_CAN_OBJ_CAPABILITIES CAN_ObjectGetCapabilities (uint32_t obj_idx) {
// Return object capabilities
return can_object_capabilities;
}
static int32_t CAN_ObjectSetFilter (uint32_t obj_idx, ARM_CAN_FILTER_OPERATION operation, uint32_t id, uint32_t arg) {
if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
switch (operation) {
case ARM_CAN_FILTER_ID_EXACT_ADD:
// Add code to setup peripheral to receive messages with specified exact ID
break;
case ARM_CAN_FILTER_ID_MASKABLE_ADD:
// Add code to setup peripheral to receive messages with specified maskable ID
break;
case ARM_CAN_FILTER_ID_RANGE_ADD:
// Add code to setup peripheral to receive messages within specified range of IDs
break;
case ARM_CAN_FILTER_ID_EXACT_REMOVE:
// Add code to remove specified exact ID from being received by peripheral
break;
case ARM_CAN_FILTER_ID_MASKABLE_REMOVE:
// Add code to remove specified maskable ID from being received by peripheral
break;
case ARM_CAN_FILTER_ID_RANGE_REMOVE:
// Add code to remove specified range of IDs from being received by peripheral
break;
default:
// Handle unknown operation code
return ARM_DRIVER_ERROR_UNSUPPORTED;
}
return ARM_DRIVER_OK;
}
static int32_t CAN_ObjectConfigure (uint32_t obj_idx, ARM_CAN_OBJ_CONFIG obj_cfg) {
if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
switch (obj_cfg) {
case ARM_CAN_OBJ_INACTIVE:
// Deactivate object
// ..
break;
case ARM_CAN_OBJ_RX_RTR_TX_DATA:
// Setup object to automatically return data when RTR with it's ID is received
// ..
break;
case ARM_CAN_OBJ_TX_RTR_RX_DATA:
// Setup object to send RTR and receive data response
// ..
break;
case ARM_CAN_OBJ_TX:
// Setup object to be used for sending messages
// ..
break;
case ARM_CAN_OBJ_RX:
// Setup object to be used for receiving messages
// ..
break;
default:
// Handle unknown object configuration code
return ARM_DRIVER_ERROR_UNSUPPORTED;
}
return ARM_DRIVER_OK;
}
static int32_t CAN_MessageSend (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, const uint8_t *data, uint8_t size) {
if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
// Add code to send requested message
// ..
return ((int32_t)size);
}
static int32_t CAN_MessageRead (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, uint8_t *data, uint8_t size) {
if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
// Add code to read previously received message
// (reception was started when object was configured for reception)
// ..
return ((int32_t)size);
}
static int32_t CAN_Control (uint32_t control, uint32_t arg) {
if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
switch (control & ARM_CAN_CONTROL_Msk) {
case ARM_CAN_ABORT_MESSAGE_SEND:
// Add code to abort message pending to be sent
// ..
break;
case ARM_CAN_SET_FD_MODE:
// Add code to enable Flexible Data-rate mode
// ..
break;
case ARM_CAN_SET_TRANSCEIVER_DELAY:
// Add code to set transceiver delay
// ..
break;
default:
// Handle unknown control code
return ARM_DRIVER_ERROR_UNSUPPORTED;
}
return ARM_DRIVER_OK;
}
static ARM_CAN_STATUS CAN_GetStatus (void) {
// Add code to return device bus and error status
// ..
}
// IRQ handlers
// Add interrupt routines to handle transmission, reception, error and status interrupts
// ..
// CAN driver functions structure
ARM_DRIVER_CAN Driver_CAN = {
CAN_GetVersion,
CAN_GetCapabilities,
CAN_Initialize,
CAN_Uninitialize,
CAN_PowerControl,
CAN_GetClock,
CAN_SetBitrate,
CAN_SetMode,
CAN_ObjectGetCapabilities,
CAN_ObjectSetFilter,
CAN_ObjectConfigure,
CAN_MessageSend,
CAN_MessageRead,
CAN_Control,
CAN_GetStatus
};
|