dSPACECANAPI20CReference
dSPACECANAPI20CReference
C Reference
For dSPACE CAN API Package 2024‑A
To access the Web version of this document, click the following link or scan the QR code:
https://www.dspace.com/dspace-help/zyFFe
To access the Web version, you must have a mydSPACE account. For more information on the
mydSPACE registration process, refer to https://www.dspace.com/faq?097.
How to Contact dSPACE
Mail: dSPACE GmbH
Rathenaustraße 26
33102 Paderborn
Germany
Tel.: +49 5251 1638-0
E-mail: [email protected]
Web: https://www.dspace.com
If possible, always provide the serial number of the hardware, the relevant dSPACE License
ID, or the serial number of the CmContainer in your support request.
Important Notice
This publication contains proprietary information that is protected by copyright. All rights
are reserved. The publication may be printed for personal or internal use provided all the
proprietary markings are retained on all printed copies. In all other cases, the publication
must not be copied, photocopied, reproduced, translated, or reduced to any electronic
medium or machine-readable form, in whole or in part, without the prior written consent
of dSPACE GmbH.
This publication and the contents hereof are subject to change without notice.
Contents
General Handling 23
Data Types, Constants............................................................................................ 24
Data Types........................................................................................ ............ 24
Constants......................................................................................... ............ 24
CAN Interface Bus Types................................................................................ 25
Functions............................................................................................................... 26
DSCAN_GetBusType...................................................................................... 26
Vendor Information 29
Data Types, Constants............................................................................................ 30
CAN Vendor Names...................................................................................... 30
Structures............................................................................................................... 31
DSSCanVendorInfo........................................................................................ 31
Functions............................................................................................................... 33
Basics on Vendor Information Functions........................................................ 33
DSCAN_GetSupportedVendorsCount............................................................ 34
DSCAN_GetVendorInformation..................................................................... 34
3
May 2024 dSPACE CAN API 2.0 C Reference
Contents
Channel Information 37
Data Types, Constants............................................................................................ 38
CAN Interface Names.................................................................................... 38
CAN Channel Capabilities............................................................................. 39
Enumerations................................................................................................ 39
Structures............................................................................................................... 40
DSSCanChannelInfo...................................................................................... 40
DSSCanChannelsSearchAttribute................................................................... 42
Functions............................................................................................................... 43
Basics on Channel Information Functions....................................................... 43
DSCAN_GetAvailableChannelsCount............................................................. 44
DSCAN_GetAvailableChannels...................................................................... 45
DSCAN_IsChannelAvailable........................................................................... 46
Configuration 49
Data Types, Constants............................................................................................ 50
CAN Baud Rates............................................................................................ 50
CAN Acceptance........................................................................................... 50
Enumerations................................................................................................ 51
Structures............................................................................................................... 52
DSSCanBitTimingParameters......................................................................... 52
Functions............................................................................................................... 53
Basics on Configuration Functions................................................................. 54
Basics on Bit Timing Parameters and Baud Rates............................................ 55
DSCAN_RegisterChannel............................................................................... 57
DSCAN_InitChannel...................................................................................... 59
DSCAN_UnregisterChannel........................................................................... 60
DSCAN_GetChannelInformation................................................................... 61
DSCAN_GetChannelCapabilities.................................................................... 63
DSCAN_IsChannelAccessible......................................................................... 64
DSCAN_SetBaudrate..................................................................................... 65
DSCAN_GetBaudrate.................................................................................... 66
DSCAN_SetTransmitAcknowledge................................................................. 67
DSCAN_SetChannelOutput........................................................................... 67
DSCAN_SetAcceptance................................................................................. 68
DSCAN_SetEventNotification......................................................................... 71
DSCAN_EnableBusStatistics........................................................................... 72
4
dSPACE CAN API 2.0 C Reference May 2024
Contents
Communication 73
Data Types, Constants............................................................................................ 74
CAN Message TX Flags.................................................................................. 74
CAN Message RX Flags..................................................................... ............ 74
CAN Bus Statistics Flags.................................................................... ............ 75
Enumerations................................................................................................ 75
Structures............................................................................................................... 77
DSSCanBusInfo............................................................................................. 77
DSSCanMessage........................................................................................... 78
DSSCanBusStatistics...................................................................................... 79
Functions............................................................................................................... 81
Basics on Communication Functions.............................................................. 82
DSCAN_ActivateChannel.................................................................. ............ 84
DSCAN_DeactivateChannel........................................................................... 84
DSCAN_GetHardwareTimeResolution............................................................ 85
DSCAN_GetHardwareTime............................................................................ 86
DSCAN_ResetHardwareTime......................................................................... 87
DSCAN_GetReceiveQueueLevel..................................................................... 87
DSCAN_ReadReceiveQueue.............................................................. ............ 88
DSCAN_ReadReceiveQueueAndDeactivateChannel....................................... 89
DSCAN_FlushReceiveQueue.......................................................................... 90
DSCAN_TransmitMessages............................................................................ 91
DSCAN_FlushTransmitQueue......................................................................... 92
DSCAN_GetBusInfo....................................................................................... 92
Error Handling 95
Data Types, Constants............................................................................................ 96
Error Codes................................................................................................... 96
Functions............................................................................................................... 97
Basics on Error Handling Functions................................................................ 97
DSCAN_GetErrorText..................................................................................... 97
DSCAN_GetLastVendorSpecificError.............................................................. 98
Auxiliary 101
Functions............................................................................................................. 102
DSCAN_ConvertBaudrateToBitTimingParameters......................................... 102
DSCAN_ConvertBaudratesToBitTimingParameters............................. .......... 104
DSCAN_ConvertBaudrateToBitTimingParametersWithSameSPAndBRP.......... 105
DSCAN_ConvertBusTimingRegistersToBitTimingParameters......................... 105
5
May 2024 dSPACE CAN API 2.0 C Reference
Contents
DSCAN_ConvertBitTimingParametersToBaudrate......................................... 106
DSCAN_ConvertBitTimingParametersToBusTimingRegisters......................... 107
DSCAN_ConvertByteCountToDlc................................................................. 108
DSCAN_ConvertDlcToByteCount................................................................. 109
DSCAN_CalculateAcceptanceFilter.............................................................. 110
DSCAN_MergeAcceptanceFilter................................................................... 111
DSCAN_EncodeBusStatistics........................................................................ 112
DSCAN_ConvertApiVersionToString............................................................. 113
Appendix 115
Basics on CAN FD........................................................................................ 115
Troubleshooting.......................................................................................... 117
Limitations.................................................................................................. 118
Index 119
6
dSPACE CAN API 2.0 C Reference May 2024
About this Reference
Content The reference gives you detailed information on the API functions, data
structures, and error codes of the dSPACE CAN API 2.0.
Symbol Description
Indicates a hazardous situation that, if not avoided,
V DANGER
will result in death or serious injury.
Indicates a hazardous situation that, if not avoided,
V WARNING could result in death or serious injury.
Indicates a hazardous situation that, if not avoided,
V CAUTION could result in minor or moderate injury.
Indicates a hazard that, if not avoided, could result in
NOTICE
property damage.
Indicates important information that you should take
Note
into account to avoid malfunctions.
Indicates tips that can make your work easier.
Tip
Indicates a link that refers to a definition in the
glossary, which you can find at the end of the
document unless stated otherwise.
Follows the document title in a link that refers to
another document.
Naming conventions dSPACE user documentation uses the following naming conventions:
7
May 2024 dSPACE CAN API 2.0 C Reference
About this Reference
Special Windows folders Windows‑based software products use the following special folders:
Accessing dSPACE Help and After you install and decrypt Windows‑based dSPACE software, the
PDF files documentation for the installed products is available in dSPACE Help and as PDF
files.
dSPACE Help (local) You can open your local installation of dSPACE Help:
§ On its home page via Windows Start Menu
§ On specific content using context-sensitive help via F1
PDF files You can access PDF files via the icon in dSPACE Help. The PDF
opens on the first page.
8
dSPACE CAN API 2.0 C Reference May 2024
Introduction and Overview
Migrating Scripts..................................................................................... 16
The data types of the dSPACE CAN API functions have been changed
with dSPACE CAN API Package 4.0.4 (dSPACE Release 2020‑A).
Discontinuation....................................................................................... 16
As of dSPACE Release 2022-B, the dSPACE CAN API and further
bus‑specific APIs are no longer available for 32-bit applications.
Support for the 64-bit versions of these APIs is not affected and will be
continued.
Introduction The dSPACE CAN API 2.0 lets you program custom applications for
CAN interfaces from dSPACE, Eberspächer GmbH, Kvaser, and Vector Informatik
GmbH.
9
May 2024 dSPACE CAN API 2.0 C Reference
Introduction and Overview
Supported CAN interfaces Currently, the dSPACE CAN API 2.0 supports the following CAN interfaces:
§ dSPACE CAN interfaces:
§ DCI‑CAN2
§ DCI‑CAN/LIN1
§ CAN interfaces from Eberspächer GmbH
For a detailed list, refer to CAN Interface Names on page 38.
§ CAN interfaces from Kvaser
For a detailed list, refer to CAN Interface Names on page 38.
§ CAN interfaces from Vector Informatik GmbH
For a detailed list, refer to CAN Interface Names on page 38.
§ 2 virtual CAN channels for testing purposes.
Support of CAN FD The dSPACE CAN API 2.0 supports CAN FD.
Accessing the The dSPACE CAN API 2.0 is a functional 64-bit Windows DLL.
dSPACE CAN API 2.0
Access via applications written in C, C++, or C# The dSPACE CAN API 2.0
interfaces are provided via export C functions, which you can use with your
application written in C or C++.
To use the dSPACE CAN API 2.0 in a C# application, you have to
reference the dSPACE CAN API 2.0 .NET DLL DsCanApi20DotNet.dll. The
DsCanApi20DotNet.dll is part of the installation of the dSPACE CAN API 2.0.
Access via Python scripts To use the dSPACE CAN API 2.0 in a Python script,
you have to import the dSPACE CAN API 2.0 Python module dscanapi20lib.
The dscanapi20lib Python module is part of the installation of the
dSPACE CAN API 2.0.
Multi-application and The dSPACE CAN API 2.0 supports both multi-application and multiclient.
multiclient support
Multi-application support Multi-application support means that several
application processes can connect to the same CAN interface channel.
10
dSPACE CAN API 2.0 C Reference May 2024
Basics of the dSPACE CAN API 2.0
The demo for getting started with dSPACE CAN API 2.0 shows an example
multiclient scenario. Refer to Steps Shown in the Demo on page 18.
Note
User application
CAN driver
CAN hardware
* Each RX/TX queue can store 32767 classic CAN messages. The number of CAN FD
messages per queue is lower and depends on the message payload.
dSPACE CAN API 2.0 versus dSPACE CAN API 2.0 was introduced with dSPACE Release 2016‑B. It is the
dSPACE CAN API 1.0 successor of dSPACE CAN API 1.0, includes all previous features, and additionally
supports CAN FD.
11
May 2024 dSPACE CAN API 2.0 C Reference
Introduction and Overview
Information on installation For information on installing dSPACE software and handling dSPACE licenses,
and licensing refer to What Do You Want To Do? (Installing dSPACE Software ).
Overview The following illustration shows an overview of the dSPACE CAN API 2.0
functions and the possible function sequences.
12
dSPACE CAN API 2.0 C Reference May 2024
Overview of the API Functions and Their Dependencies
DSCAN_GetSupportedVendorsCount DSCAN_GetAvailableChannelsCount
DSCAN_GetVendorInformation DSCAN_GetAvailableChannels
DSCAN_GetBusType DSCAN_IsChannelAvailable
DSCAN_RegisterChannel
DSCAN_InitChannel
yes
Access
DSCAN_SetBaudrate
permission
DSCAN_SetChannelOutput
no
DSCAN_SetTransmitAcknowledge DSCAN_GetBaudrate
DSCAN_SetAcceptance DSCAN_GetChannelInformation
DSCAN_SetEventNotification DSCAN_GetChannelCapabilities
DSCAN_EnableBusStatistics DSCAN_IsChannelAccessible
DSCAN_ActivateChannel
Access
DSCAN_FlushTransmitQueue yes
permission
no
DSCAN_GetReceiveQueueLevel DSCAN_GetHardwareTimeResolution
DSCAN_ReadReceiveQueue DSCAN_GetHardwareTime
DSCAN_FlushReceiveQueue DSCAN_ResetHardwareTime
DSCAN_TransmitMessages DSCAN_GetBusInfo
DSCAN_DeactivateChannel
DSCAN_UnregisterChannel
Error handling functions The following dSPACE CAN API 2.0 functions are
provided for error handling:
§ DSCAN_GetErrorText
§ DSCAN_GetLastVendorSpecificError
Auxiliary functions The dSPACE CAN API 2.0 provides the following auxiliary
functions:
§ DSCAN_ConvertBaudrateToBitTimingParameters
§ DSCAN_ConvertBaudratesToBitTimingParameters
13
May 2024 dSPACE CAN API 2.0 C Reference
Introduction and Overview
§ DSCAN_ConvertBaudrateToBitTimingParametersWithSameSPAndBRP
§ DSCAN_ConvertBusTimingRegistersToBitTimingParameters
§ DSCAN_ConvertBitTimingParametersToBaudrate
§ DSCAN_ConvertBitTimingParametersToBusTimingRegisters
§ DSCAN_ConvertByteCountToDlc
§ DSCAN_ConvertDlcToByteCount
§ DSCAN_CalculateAcceptanceFilter
§ DSCAN_MergeAcceptanceFilter
§ DSCAN_EncodeBusStatistics
§ DSCAN_ConvertApiVersionToString
Files and their locations The following table shows the locations of the important files of dSPACE CAN
API 2.0 after installation.
14
dSPACE CAN API 2.0 C Reference May 2024
Software Requirements for Working with dSPACE CAN API 2.0
C# Demo Application
DSCanApi20_GettingStarted.exe %CommonProgramFiles%\dSPACE\DSCanApi 2024‑A C# demo
\DSCanApi_22.2 application. Refer to
\Demos\DsCanApi20\GettingStarted\C#\bin\x64\Release\ Steps Shown in the
Demo on page 18.
DSCanApi20_GettingStarted.sln %CommonProgramFiles%\dSPACE\DSCanApi 2024‑A Source code for
\DSCanApi_22.2 the C# demo
\Demos\DsCanApi20\GettingStarted\C#\src\ application.
Software requirements Operating systems The dSPACE CAN API 2.0 supports the following
operating systems:
§ Windows 10 (64-bit version)
CAN driver software For information on the CAN driver software required
for CAN interfaces from Eberspächer GmbH, Kvaser or Vector Informatik
GmbH, refer to Overview of Required Third-Party Software (Installing dSPACE
Software ).
15
May 2024 dSPACE CAN API 2.0 C Reference
Introduction and Overview
Migrating Scripts
Migration to dSPACE CAN API The data types of the dSPACE CAN API functions have been changed with
Package 4.0.4 dSPACE CAN API Package 4.0.4 (dSPACE Release 2020‑A).
The following table shows the data type changes. You have to adapt your scripts
accordingly.
Discontinuation
Discontinuation of the 32-bit As of dSPACE Release 2022-B, the dSPACE CAN API and further bus‑specific APIs
version of the dSPACE CAN are no longer available for 32-bit applications.
API and further bus‑specific
Support for the 64-bit versions of these APIs is not affected and will be
APIs
continued.
16
dSPACE CAN API 2.0 C Reference May 2024
Demo for dSPACE CAN API 2.0
Introduction The dSPACE CAN API installation comprises a demo for getting started with
dSPACE CAN API 2.0.
Using physical or virtual The demo uses either physical or virtual CAN interface channels:
CAN interface channels § Physical CAN interface channels are used if at least two CAN interface
channels are connected to the host PC (e.g., one DCI‑CAN/LIN1 with
two CAN interface channels, or two DCI-CAN2 each of which has one
CAN interface channel).
§ Virtual CAN interface channels are used if less than two physical CAN interface
channels are connected to the host PC.
17
May 2024 dSPACE CAN API 2.0 C Reference
Demo for dSPACE CAN API 2.0
You can use the demo code (or parts of it) as the starting point for your own
applications and scripts.
Location of the demo files For the location of the demo files, refer to Files of dSPACE CAN API 2.0 on
page 14.
18
dSPACE CAN API 2.0 C Reference May 2024
Steps Shown in the Demo
For channels that have no access permission, the current baud rate of the
interface channel is returned by using the DSCAN_GetBaudrate function.
19
May 2024 dSPACE CAN API 2.0 C Reference
Demo for dSPACE CAN API 2.0
Messages to be Transmitted
Classic CAN § 3 messages with standard IDs 10, 20 and 30
§ 2 messages with extended IDs 10000 and 10100
CAN FD1) § 3 standard CAN messages with standard IDs 10, 20 and 30
§ 2 standard CAN messages with extended IDs 10000 and 10100
§ 3 CAN FD messages with standard IDs 10, 20 and 30 and with
baud rate switch
§ 2 CAN FD messages with extended IDs 10000 and 10100 and
without baud rate switch
1) If supported by the used interface channels
The following ways of message transmission are used:
20
dSPACE CAN API 2.0 C Reference May 2024
Steps Shown in the Demo
21
May 2024 dSPACE CAN API 2.0 C Reference
Demo for dSPACE CAN API 2.0
22
dSPACE CAN API 2.0 C Reference May 2024
General Handling
General Handling
Functions................................................................................................. 26
23
May 2024 dSPACE CAN API 2.0 C Reference
General Handling
Data Types............................................................................................... 24
General CAN data types
Constants................................................................................................ 24
General CAN constants
Data Types
Data types
DSTEventHandle
DSTCanHandle
DSTCanError
Constants
Constants
DSCAN_MAX_DATA_LENGTH
Value Description
64 Maximum data length of a CAN message
DSCAN_MAX_NAME_LENGTH
Value Description
256 Maximum length of any naming strings (e.g. vendor name, interface name, etc.)
24
dSPACE CAN API 2.0 C Reference May 2024
Data Types, Constants
DSCAN_INVALID_CAN_HANDLE
Value Description
-1 Invalid channel handle
DSCAN_INVALID_EVENT_HANDLE
Value Description
0 Invalid event handle
DSCAN_MAX_TEXT_LENGTH
Value Description
2048 Maximum length of any text strings (e.g. error text)
DSCAN_MAX_RX_QUEUE_SIZE
Value Description
32768 Maximum receive queue size
DSCAN_CRYSTAL_FREQUENCY_SJA1000
Value Description
16000000 16 MHz
DSCAN_CLOCK_FREQUENCY_SJA1000
Value Description
(DSCAN_CRYSTAL_FREQUENCY_SJA1000 / 2) 8 MHz (16 MHz crystal frequency with
the fixed prescaler of 2)
25
May 2024 dSPACE CAN API 2.0 C Reference
General Handling
Functions
DSCAN_GetBusType
Syntax
DSTCanError DSCAN_GetBusType(char szVendorName[DSCAN_MAX_NAME_LENGTH],
char szInterfaceName[DSCAN_MAX_NAME_LENGTH],
char* pszBusType);
26
dSPACE CAN API 2.0 C Reference May 2024
Functions
27
May 2024 dSPACE CAN API 2.0 C Reference
General Handling
28
dSPACE CAN API 2.0 C Reference May 2024
Vendor Information
Vendor Information
Structures................................................................................................ 31
Functions................................................................................................. 33
29
May 2024 dSPACE CAN API 2.0 C Reference
Vendor Information
30
dSPACE CAN API 2.0 C Reference May 2024
Structures
Structures
DSSCanVendorInfo
Syntax
typedef struct DSSCanVendorInfo
{
char szVendorName[DSCAN_MAX_NAME_LENGTH];
char szVendorApiDllName[DSCAN_MAX_NAME_LENGTH];
uint32_t ulVendorApiVersion;
uint32_t ulRequiredVendorApiVersion;
DSTCanError tVendorApiState;
} DSSCanVendorInfo;
The vendor CAN API version is available only if the vendor CAN API DLL could be
loaded successfully.
Use DSCAN_ConvertApiVersionToString to convert a CAN API version to
string.
31
May 2024 dSPACE CAN API 2.0 C Reference
Vendor Information
32
dSPACE CAN API 2.0 C Reference May 2024
Functions
Functions
Where to go from here Information in this section
DSCAN_GetSupportedVendorsCount...................................................... 34
To return the count of supported vendor CAN APIs.
DSCAN_GetVendorInformation............................................................... 34
To return information about supported vendor CAN APIs.
Steps to obtain vendor CAN To obtain information about supported vendor CAN APIs, perform the following
API information steps:
1. Call the DSCAN_GetSupportedVendorsCount function to get the count of
supported vendor CAN APIs.
2. Allocate memory for the vendor CAN API information.
3. Call the DSCAN_GetVendorInformation function to get the information
on supported vendor CAN APIs.
4. Free the allocated memory after processing the information on supported
vendor CAN APIs.
Example The following example shows how to obtain information about supported
vendor CAN APIs.
DSTCanError tErrorCode = DSCAN_ERR_NO_ERROR;
uint32_t ulVendorsCount = 0;
DSSCanVendorInfo* ptVendorsArray = NULL;
// get count of supported vendor CAN APIs
tErrorCode = DSCAN_GetSupportedVendorsCount(&ulVendorsCount);
if (DSCAN_ERR_NO_ERROR == tErrorCode)
{
// allocate memory for supported vendor CAN APIs information
ptVendorsArray = new DSSCanVendorInfo[ulVendorsCount];
// get supported vendor CAN APIs information
tErrorCode = DSCAN_GetVendorInformation(&ulVendorsCount, ptVendorsArray);
// process supported vendor CAN APIs information
for (uint32_t i = 0; i < ulVendorsCount; i++)
{
// do anything with ptVendorsArray[i]
}
33
May 2024 dSPACE CAN API 2.0 C Reference
Vendor Information
Vendor CAN API information Vendor CAN API information is described by the DSSCanVendorInfo structure.
Refer to DSSCanVendorInfo on page 31.
DSCAN_GetSupportedVendorsCount....................................................................................... 34
DSCAN_GetVendorInformation................................................................................................ 34
DSCAN_GetSupportedVendorsCount
Syntax
DSTCanError DSCAN_GetSupportedVendorsCount(uint32_t* pulVendorsCount);
DSCAN_GetVendorInformation
34
dSPACE CAN API 2.0 C Reference May 2024
Functions
Syntax
DSTCanError DSCAN_GetVendorInformation(uint32_t* pulVendorsCount,
DSSCanVendorInfo* ptVendorsArray);
Parameters (In, Out) pulVendorsCount Maximum number of supported vendor CAN APIs.
§ If used as In parameter:
Lets you specify the maximum number of supported vendor CAN APIs to
obtain. To obtain the information, call the function with pulVendorsCount
set to the maximum number of vendor CAN APIs to get. This is usually the size
of the ptVendorsArray parameter.
§ If used as Out parameter:
Lets you get the number of obtained vendor CAN APIs. The
pulVendorsCount parameter contains the actual number of supported
vendor CAN APIs which have been written to the ptVendorsArray parameter
(the actual number is always less than or equal to the initial maximum value).
You can call the function with the NULL pointer instead of ptVendorsArray.
In this case, only the number of supported vendor CAN APIs is obtained. This
function call is equal to a call of the DSCAN_GetSupportedVendorsCount
function.
The memory for ptVendorsArray must be allocated and freed by the caller.
35
May 2024 dSPACE CAN API 2.0 C Reference
Vendor Information
36
dSPACE CAN API 2.0 C Reference May 2024
Channel Information
Channel Information
Structures................................................................................................ 40
Functions................................................................................................. 43
37
May 2024 dSPACE CAN API 2.0 C Reference
Channel Information
Enumerations.......................................................................................... 39
CAN channel enumerations
38
dSPACE CAN API 2.0 C Reference May 2024
Data Types, Constants
Enumerations
39
May 2024 dSPACE CAN API 2.0 C Reference
Channel Information
Structures
Where to go from here Information in this section
DSSCanChannelInfo................................................................................ 40
Structure for CAN channel information
DSSCanChannelsSearchAttribute............................................................. 42
Structure for CAN channels search attribute
DSSCanChannelInfo
Syntax
typedef struct DSSCanChannelInfo
{
char szVendorName[DSCAN_MAX_NAME_LENGTH];
char szInterfaceName[DSCAN_MAX_NAME_LENGTH];
char szInterfaceSerialNumber[DSCAN_MAX_NAME_LENGTH];
char szChannelIdentifier[DSCAN_MAX_NAME_LENGTH];
uint32_t ulChannelCapabilities;
} DSSCanChannelInfo;
40
dSPACE CAN API 2.0 C Reference May 2024
Structures
41
May 2024 dSPACE CAN API 2.0 C Reference
Channel Information
DSSCanChannelsSearchAttribute
Syntax
typedef struct DSSCanChannelsSearchAttribute
{
DSECanChannelsSearchAttributeType tSearchAttributeType;
char szSearchAttribute[DSCAN_MAX_NAME_LENGTH];
} DSSCanChannelsSearchAttribute;
42
dSPACE CAN API 2.0 C Reference May 2024
Functions
Functions
Where to go from here Information in this section
DSCAN_GetAvailableChannelsCount....................................................... 44
To return the count of available CAN channels.
DSCAN_GetAvailableChannels................................................................ 45
To return information about available CAN channels.
DSCAN_IsChannelAvailable..................................................................... 46
To check whether the specified CAN channel is available.
Steps to obtain information To obtain information about available CAN channels, perform the following
about available CAN channels steps:
1. Call the DSCAN_GetAvailableChannelsCount function to get the count of
available CAN channels.
2. Allocate memory for the available CAN channels.
3. Call the DSCAN_GetAvailableChannels function to get the available CAN
channels.
4. Free the allocated memory after processing the available CAN channels.
Example The following example shows how to obtain information about available CAN
channels.
DSTCanError tErrorCode = DSCAN_ERR_NO_ERROR;
uint32_t ulChannelsCount = 0;
DSSCanChannelInfo* ptChannelsArray = NULL;
// get count of available CAN channelst
ErrorCode = DSCAN_GetAvailableChannelsCount(&ulChannelsCount, 0, NULL);
if (DSCAN_ERR_NO_ERROR == tErrorCode)
{
// allocate memory for available CAN channels
ptChannelsArray = new DSSCanChannelInfo[ulChannelsCount];
// get available CAN channels
tErrorCode = DSCAN_GetAvailableChannels(&ulChannelsCount, ptChannelsArray, 0, NULL);
// process available CAN channels
for (uint32_t i = 0; i < ulChannelsCount; i++)
{
// do anything with ptChannelsArray[i]
}
43
May 2024 dSPACE CAN API 2.0 C Reference
Channel Information
CAN channel information CAN channel information is described by the DSSCanChannelInfo structure. For
details, refer to DSSCanChannelInfo on page 40.
DSCAN_GetAvailableChannels................................................................................................. 45
DSCAN_GetAvailableChannelsCount........................................................................................ 44
DSCAN_GetAvailableChannelsCount
Syntax
DSTCanError DSCAN_GetAvailableChannelsCount(uint32_t* pulChannelsCount,
uint32_t ulAdditionalSearchAttributesCount,
DSSCanChannelsSearchAttribute* ptAdditionalSearchAttributesArray);
44
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_GetAvailableChannels
Syntax
DSTCanError DSCAN_GetAvailableChannels(uint32_t* pulChannelsCount,
DSSCanChannelInfo* ptChannelsArray,
uint32_t ulAdditionalSearchAttributesCount,
DSSCanChannelsSearchAttribute* ptAdditionalSearchAttributesArray)
You can call the function with the NULL pointer instead of ptChannelsArray. In
this case, only the number of available CAN channels is obtained. This function
call is equal to a call of the DSCAN_GetAvailableChannelsCount function.
The memory for ptChannelsArray must be allocated and freed by the caller.
45
May 2024 dSPACE CAN API 2.0 C Reference
Channel Information
DSCAN_IsChannelAvailable
Syntax
DSTCanError DSCAN_IsChannelAvailable(char szVendorName[DSCAN_MAX_NAME_LENGTH],
char szInterfaceName[DSCAN_MAX_NAME_LENGTH],
char szInterfaceSerialNumber[DSCAN_MAX_NAME_LENGTH],
char szChannelIdentifier[DSCAN_MAX_NAME_LENGTH],
bool* pbChannelIsAvailable,
uint32_t ulAdditionalSearchAttributesCount,
DSSCanChannelsSearchAttribute* ptAdditionalSearchAttributesArray);
46
dSPACE CAN API 2.0 C Reference May 2024
Functions
47
May 2024 dSPACE CAN API 2.0 C Reference
Channel Information
48
dSPACE CAN API 2.0 C Reference May 2024
Configuration
Configuration
Structures................................................................................................ 52
Functions................................................................................................. 53
49
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
CAN Acceptance..................................................................................... 50
CAN acceptance constants
Enumerations.......................................................................................... 51
CAN configuration enumerations
CAN Acceptance
50
dSPACE CAN API 2.0 C Reference May 2024
Data Types, Constants
Enumerations
51
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
Structures
DSSCanBitTimingParameters
52
dSPACE CAN API 2.0 C Reference May 2024
Functions
Functions
Where to go from here Information in this section
DSCAN_RegisterChannel......................................................................... 57
To register a channel.
DSCAN_InitChannel................................................................................ 59
To initialize a CAN channel and get access permission to it.
DSCAN_UnregisterChannel..................................................................... 60
To unregister a channel.
DSCAN_GetChannelInformation.............................................................. 61
To return information of a CAN channel.
DSCAN_GetChannelCapabilities.............................................................. 63
To return the capabilities of a CAN channel.
DSCAN_IsChannelAccessible................................................................... 64
To check if a CAN channel is accessible.
DSCAN_SetBaudrate............................................................................... 65
To set the baud rate of a CAN interface channel.
DSCAN_GetBaudrate............................................................................... 66
To return the baud rate of a CAN channel.
DSCAN_SetTransmitAcknowledge........................................................... 67
To set the transmit acknowledge state of a CAN channel.
DSCAN_SetChannelOutput..................................................................... 67
To set the output mode of a CAN channel.
DSCAN_SetAcceptance........................................................................... 68
To set the CAN message acceptance filter of a CAN channel.
DSCAN_SetEventNotification................................................................... 71
To set the event notification of a CAN channel.
DSCAN_EnableBusStatistics..................................................................... 72
To enable or disable the periodic generation of bus statistics messages for
a CAN channel.
53
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
Steps to configure a CAN To configure a CAN channel, perform the following steps:
channel 1. Call the DSCAN_RegisterChannel function to register a CAN channel.
Note
You must register a CAN channel before you can use it.
Tip
Note
Note
CAN bit timing parameters CAN bit timing parameters are described by the DSSCanBitTimingParameters
structure. For details, refer to DSSCanBitTimingParameters on page 52.
DSSCanBitTimingParameters..................................................................................................... 52
54
dSPACE CAN API 2.0 C Reference May 2024
Functions
CAN bit timing parameters CAN channels can be configured via the following bit timing parameters:
§ Synchronization jump width
§ Baud rate prescaler
§ Sample mode
§ Bit time segment 1
§ Bit time segment 2
The illustration below shows the meaning and the interdependencies of the
parameters:
Clock
frequency
CAN
Sample point(s)
Using dSPACE CAN API 2.0, bit timing parameters are described by the
DSSCanBitTimingParameters structure.
Baud rate The baud rate is determined by the CAN interface clock frequency and the bit
timing parameters.
Setting the baud rate With dSPACE CAN API 2.0, you can set the baud rate
of a CAN channel by using the DSCAN_SetBaudrate function.
Getting the baud rate With dSPACE CAN API 2.0, you can get the baud
rate of a CAN channel by using the DSCAN_GetBaudrate function.
55
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
Sample mode The sample mode specifies the number of sample points per bit that is used to
evaluate whether the bit is recessive or dominant,
Converting baud rates into bit To convert a baud rate value to bit timing parameters and vice versa, dSPACE
timing parameters and vice CAN API 2.0 provides the following functions:
versa § DSCAN_ConvertBaudrateToBitTimingParameters
§ DSCAN_ConvertBaudratesToBitTimingParameters
§ DSCAN_ConvertBaudrateToBitTimingParametersWithSameSPAndBRP
§ DSCAN_ConvertBitTimingParametersToBaudrate
Baud Rate Synch. Jump Baud Rate Sample Bit Time Bit Time Sample
Width Prescaler Mode Segment 1 Segment 2 Point
1000 kBit/s 2 1 0 5 2 75%
500 kBit/s 2 2 0 5 2 75%
250 kBit/s 2 4 0 5 2 75%
125 kBit/s 2 8 0 5 2 75%
100 kBit/s 2 10 0 5 2 75%
50 kBit/s 2 20 0 5 2 75%
20 kBit/s 2 50 0 5 2 75%
10 kBit/s 2 50 0 13 2 87%
§ For all other combinations of baud rate and clock frequency values, the bit
timing parameters are determined according to the following conditions:
§ Sample point is equal or close to:
§ 75 % for classic CAN baud rates (≤ 1 Mbit/s)
§ 80 % for CAN FD baud rates (> 1 Mbit/s)
§ Lowest nominal bit time for the desired sample point
§ Sample mode is 0
56
dSPACE CAN API 2.0 C Reference May 2024
Functions
Converting bit timing For classic CAN baud rates of up to 1 Mbit/s, bit timing parameters can be stored
parameters into bus timing in two bus timing registers.
registers and vice versa
To convert bit timing parameters to bus timing registers and vice versa, dSPACE
CAN API 2.0 provides the following functions:
§ DSCAN_ConvertBitTimingParametersToBusTimingRegisters
§ DSCAN_ConvertBusTimingRegistersToBitTimingParameters
DSCAN_RegisterChannel
Syntax
DSTCanError DSCAN_RegisterChannel(char szVendorName[DSCAN_MAX_NAME_LENGTH],
char szInterfaceName[DSCAN_MAX_NAME_LENGTH],
char szInterfaceSerialNumber [DSCAN_MAX_NAME_LENGTH],
char szChannelIdentifier[DSCAN_MAX_NAME_LENGTH],
DSTCanHandle* ptChannelHandle);
Description You must register a CAN channel before you can use it.
Tip
To register a CAN channel, the related CAN interface hardware does not
have to be connected to the host PC.
57
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
58
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_InitChannel
Syntax
DSTCanError DSCAN_InitChannel(DSTCanHandle tChannelHandle,
DSECanIdentifierType tIdentifierType,
uint32_t ulRxQueueSize,
bool bFD,
bool* pbAccessPermission);
Description You must initialize a CAN channel before you can use it. To initialize a CAN
channel, the related CAN interface hardware must be connected to the host PC.
You can use the same CAN channel hardware multiple times with the same
application or even for different applications simultaneously.
Note
59
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
DSCAN_UnregisterChannel
Syntax
DSTCanError DSCAN_UnregisterChannel(DSTCanHandle tChannelHandle);
Description After you have used a channel, you must unregister it.
Note
60
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_GetChannelInformation
Syntax
DSTCanError DSCAN_GetChannelInformation(DSTCanHandle tChannelHandle,
char* pszVendorName,
char* pszInterfaceName,
char* pszInterfaceSerialNumber,
char* pszChannelIdentifier);
61
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
62
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_GetChannelCapabilities
Syntax
DSTCanError DSCAN_GetChannelCapabilities(DSTCanHandle tChannelHandle,
uint32_t* pulChannelCapabilities);
63
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
DSCAN_IsChannelAccessible
Syntax
DSTCanError DSCAN_IsChannelAccessible(DSTCanHandle tChannelHandle,
bool* pbChannelIsAccessible);
Description If the function returns that the channel is not accessible, the channel cannot be
used any more since it has no longer connection to the corresponding hardware
CAN channel.
In this case:
1. Call DSCAN_UnregisterChannel to unregister the channel.
2. Use DSCAN_GetAvailableChannels to check whether the hardware CAN
channel is available.
3. If it is, call DSCAN_RegisterChannel to register the new channel.
64
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_SetBaudrate
Note
To call the function you must have access permission for the CAN interface
channel.
Syntax
DSTCanError DSCAN_SetBaudrate(DSTCanHandle tChannelHandle,
uint32_t ulClockFrequency,
DSSCanBitTimingParameters* ptBitTimingParameters,
DSSCanBitTimingParameters* ptBitTimingParameters_FD);
Description Depending on how the channel was initialized, the bit timing parameters
ptBitTimingParameters and ptBitTimingParameters_FD have different
meanings.
Meaning of ...
ptBitTimingParameters ptBitTimingParameters_FD
CAN FD is not Nominal baud rate Is ignored. Use the NULL pointer instead.
to be used
CAN FD is to CAN FD arbitration baud rate CAN FD data baud rate
be used
65
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
DSCAN_GetBaudrate
Syntax
DSTCanError DSCAN_GetBaudrate(DSTCanHandle tChannelHandle,
uint32_t* pulClockFrequency,
DSSCanBitTimingParameters* ptBitTimingParameters,
bool* pbFD,
DSSCanBitTimingParameters* ptBitTimingParameters_FD);
Meaning of ...
ptBitTimingParameters ptBitTimingParameters_FD
CAN FD is not used Nominal baud rate - (not relevant; can be ignored)
CAN FD is used CAN FD arbitration baud rate CAN FD data baud rate
66
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_SetTransmitAcknowledge
Syntax
DSTCanError DSCAN_SetTransmitAcknowledge(DSTCanHandle tChannelHandle,
bool bTransmitAcknowledge);
DSCAN_SetChannelOutput
Note
To call the function, you must have access permission for the channel.
67
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
Syntax
DSTCanError DSCAN_SetChannelOutput(DSTCanHandle tChannelHandle,
bool bSilentMode);
Description § If the silent mode is disabled, the hardware CAN channel generates a receive
acknowledge on the CAN bus whenever a CAN message was received
successfully.
The silent mode is disabled by default.
§ If the silent mode is enabled, the hardware CAN channel neither generates
receive acknowledges for incoming CAN messages nor transmits CAN
messages.
DSCAN_SetAcceptance
Syntax
DSTCanError DSCAN_SetAcceptance(DSTCanHandle tChannelHandle,
uint32_t ulCodeStd,
uint32_t ulMaskStd,
uint32_t ulCodeXtd,
uint32_t ulMaskXtd);
Description Via the mask and code filter, you can specify which bits of the received CAN IDs
are checked and which values are accepted:
§ A 1 in the mask filter specifies that the corresponding bit of the received CAN
ID is checked.
68
dSPACE CAN API 2.0 C Reference May 2024
Functions
§ If the checked bits of the CAN ID match the bits of the code filter, the CAN
message is passed to the receive buffer.
Note
§ Both CAN identifier types have their own code and mask filter definitions.
However, only the code and mask filters for the currently active CAN
identifier type are used. The active identifier type is specified via the
DSCAN_InitChannel function.
§ Suppose a CAN bus has messages of both the STD and the XTD
identifier type. dSPACE CAN interfaces can receive messages of both
identifier types regardless of the tIdentifierType parameter value that
was passed to the DSCAN_InitChannel function. This requires further
filtering in your application.
§ Every message that corresponds to the specified filter is accepted.
However, depending on the filter settings and the messages on the bus,
even more messages can pass the filter. This requires further filtering in
your application.
Example The following table shows you which mask and code filters you
have to specify in three example cases:
Note
If you want to filter for several specific CAN IDs, you might not be able to
define a mask and code filter that blocks all the undesired CAN IDs. This
is the case if the differences between the desired CAN IDs are located in
different bits. Below are rules for finding your optimal mask and code filters
to filter for 1 … n CAN IDs.
69
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
Calculating mask and code filters Use the following formulas to calculate
your mask and code filters if you want to receive the CAN IDs ID(0) … ID(n):
code = ID(0) | ID(1) | .... | ID(n)
mask = 0x7ff // for standard identifiers
mask = 0x1fff ffff // for extended identifiers
for (i = 0; i <= n; i++)
{
mask = (~(ID(i) & mask) ^ (code & mask)) & mask
}
Note
The total number of different CAN IDs that pass the acceptance filter can be
calculated via the number of 0’s in the mask (see example below):
Number of CAN IDs = 2 Number of 0’s
ID1:
mask = (~(000 0000 1010 & 111 1111 1111) XOR
(000 0101 1110 & 111 1111 1111)) & 111 1111 1111
mask = (-(000 0000 1010) XOR (000 0101 1110)) & 111 1111 1111
mask = ((111 1111 0101) XOR (000 0101 1110)) & 111 1111 1111
mask = (111 1010 1011) & 111 1111 1111
mask = 111 1010 1011
ID2:
mask = (~(000 0101 0110 & 111 1010 1011) XOR
(000 0101 1110 & 111 1010 1011)) & 111 1010 1011
mask = (-(000 0000 0010) XOR (000 0000 1010)) & 111 1010 1011
mask = ((111 1111 1101) XOR (000 0000 1010)) & 111 1010 1011
mask = (111 1111 0111) & 111 1010 1011
mask = 111 1010 0011
70
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_SetEventNotification
Syntax
DSTCanError DSCAN_SetEventNotification(DSTCanHandle tChannelHandle,
DSTEventHandle tEventHandle,
uint32_t ulReceiveQueueLevel);
Description To enable the event notification, call the function with a valid Windows event
handle. Call the function with a NULL pointer instead of the event handle to
disable the event notification.
If the event notification is enabled, the CAN channel informs the application via
the event when the specified number of CAN messages has been received.
Note
71
May 2024 dSPACE CAN API 2.0 C Reference
Configuration
DSCAN_EnableBusStatistics
Purpose To enable or disable the periodic generation of bus statistics messages for a CAN
channel.
Syntax
DSTCanError DSCAN_EnableBusStatistics(DSTCanHandle tChannelHandle,
bool bEnable);
Description If the option is enabled, the hardware CAN channel periodically generates a bus
statistics message in its receive queue.
72
dSPACE CAN API 2.0 C Reference May 2024
Communication
Communication
Structures................................................................................................ 77
Functions................................................................................................. 81
73
May 2024 dSPACE CAN API 2.0 C Reference
Communication
Enumerations.......................................................................................... 75
CAN communication enumerations
74
dSPACE CAN API 2.0 C Reference May 2024
Data Types, Constants
Enumerations
75
May 2024 dSPACE CAN API 2.0 C Reference
Communication
76
dSPACE CAN API 2.0 C Reference May 2024
Structures
Structures
Where to go from here Information in this section
DSSCanBusInfo........................................................................................ 77
Structure for CAN bus information
DSSCanMessage...................................................................................... 78
Structure for a CAN message
DSSCanBusStatistics................................................................................ 79
Structure for CAN bus statistics information
DSSCanBusInfo
Syntax
typedef struct DSSCanBusInfo
{
DSECanBusStatus tBusStatus;
uint16_t usRxErrorCounter;
uint16_t usTxErrorCounter;
uint8_t ucBusLoad;
} DSSCanBusInfo;
ucBusLoad CAN bus load in percent (only for channels having the
DSCAN_CHANNEL_CAPABILITY_BUS_LOAD_INFO channel capability)
77
May 2024 dSPACE CAN API 2.0 C Reference
Communication
DSSCanMessage
Syntax
typedef struct DSSCanMessage
{
DSECanMessageType tMessageType;
uint64_t ui64Timestamp;
uint32_t ulCanIdentifier;
DSECanIdentifierType tCanIdentifierType;
uint32_t ulFlags;
uint16_t usDLC;
uint8_t ucData[DSCAN_MAX_DATA_LENGTH];
DSSCanBusInfo tBusInfo;
} DSSCanMessage;
Note
ui64Timestamp Timestamp
For dSPACE CAN interfaces, you can specify the transmission time of a CAN
message by setting the CAN message timestamp.
To transmit a CAN message immediately, set the CAN message timestamp to '0'.
When you first pass a CAN message with specified CAN message timestamp to
the CAN channel and then one for immediate transmission, the latter message is
delayed until the first message is sent.
78
dSPACE CAN API 2.0 C Reference May 2024
Structures
ucData Specifies either data bytes (only for messages with the
DSCAN_MESSAGE_TYPE_DATA message type) or CAN bus statistics information
(only for messages with the DSCAN_MESSAGE_TYPE_BUS_STATISTICS message
type).
To encode the CAN bus statistics information from data bytes, use the
DSCAN_EncodeBusStatistics function.
tBusInfo Specifies CAN bus information (only for messages with the
DSCAN_MESSAGE_TYPE_BUS_INFO or DSCAN_MESSAGE_TYPE_BUS_STATISTICS
message type).
Refer to DSSCanBusInfo on page 77.
DSCAN_EncodeBusStatistics................................................................................................... 112
DSSCanBusStatistics
79
May 2024 dSPACE CAN API 2.0 C Reference
Communication
Syntax
typedef struct DSSCanBusStatistics
{
uint32_t ulFlags;
uint32_t ulErrorFrames;
uint32_t ulRxStdFrames;
uint32_t ulTxStdFrames;
uint32_t ulRxExtFrames;
uint32_t ulTxExtFrames;
uint32_t ulRxStdFDFrames;
uint32_t ulTxStdFDFrames;
uint32_t ulRxExtFDFrames;
uint32_t ulTxExtFDFrames;
} DSSCanBusStatistics;
80
dSPACE CAN API 2.0 C Reference May 2024
Functions
Functions
Where to go from here Information in this section
DSCAN_ActivateChannel......................................................................... 84
To activate CAN communication of a specific CAN channel.
DSCAN_DeactivateChannel..................................................................... 84
To deactivate CAN communication of a specific CAN channel.
DSCAN_GetHardwareTimeResolution...................................................... 85
To return the hardware time resolution of a CAN channel.
DSCAN_GetHardwareTime...................................................................... 86
To return the hardware time of a CAN channel.
DSCAN_ResetHardwareTime................................................................... 87
To reset the hardware time of a CAN channel.
DSCAN_GetReceiveQueueLevel............................................................... 87
To return the count of CAN messages in the receive queue of a CAN
channel.
DSCAN_ReadReceiveQueue..................................................................... 88
To read CAN messages from the receive queue of a CAN channel.
DSCAN_ReadReceiveQueueAndDeactivateChannel.................................. 89
To read CAN messages from the receive queue of a CAN channel, and
deactivate CAN communication of the channel.
DSCAN_FlushReceiveQueue..................................................................... 90
To clear the receive queue of a CAN channel.
DSCAN_TransmitMessages...................................................................... 91
To transmit CAN messages by the use of a CAN channel.
DSCAN_FlushTransmitQueue................................................................... 92
To clear the transmit queue of a CAN channel.
DSCAN_GetBusInfo................................................................................. 92
To return the bus communication state of a CAN channel.
81
May 2024 dSPACE CAN API 2.0 C Reference
Communication
Steps to perform To receive and transmit CAN messages with a channel, perform the following
CAN communication with a steps:
channel 1. Call the DSCAN_ActivateChannel function to activate the CAN channel.
Note
You must activate a channel before you can use it to transmit and
receive CAN messages.
Specifying the transmission For dSPACE CAN interfaces, you can specify the transmission time of a CAN
time message by setting the CAN message timestamp.
To transmit a CAN message immediately, set the CAN message timestamp to '0'.
When you first pass a CAN message with specified CAN message timestamp to
the CAN channel and then one for immediate transmission, the latter message is
delayed until the first message is sent.
Example The following example shows how to receive and transmit CAN messages with a
channel.
DSTCanError tErrorCode = DSCAN_ERR_NO_ERROR;
DSTCanHandle tChannelHandle = DSCAN_INVALID_CAN_HANDLE;
uint32_t ulMessagesCount = 0;
DSSCanMessage* ptMessagesArray = NULL;
DSSCanMessage tMessage;
uint64_t ui64TimeResolution = 0;
uint64_t ui64CurrentTime = 0;
// ...
// Receive CAN messages
// --------------------
// Get count of CAN messages
tErrorCode = DSCAN_GetReceiveQueueLevel(tChannelHandle, &ulMessagesCount);
if (DSCAN_ERR_NO_ERROR == tErrorCode)
{
// Allocate memory for CAN messages
ptMessagesArray = new DSSCanMessage[ulMessagesCount];
82
dSPACE CAN API 2.0 C Reference May 2024
Functions
83
May 2024 dSPACE CAN API 2.0 C Reference
Communication
DSCAN_ActivateChannel.......................................................................................................... 84
DSCAN_TransmitMessages....................................................................................................... 91
References
DSCAN_DeactivateChannel...................................................................................................... 84
DSCAN_ActivateChannel
Syntax
DSTCanError DSCAN_ActivateChannel(DSTCanHandle tChannelHandle);
Description If the CAN communication is activated, the CAN channel can send and receive
CAN messages.
DSCAN_DeactivateChannel
Syntax
DSTCanError DSCAN_DeactivateChannel(DSTCanHandle tChannelHandle);
84
dSPACE CAN API 2.0 C Reference May 2024
Functions
Description If the CAN communication is deactivated, the CAN channel cannot send and
receive CAN messages.
References
DSCAN_ReadReceiveQueueAndDeactivateChannel................................................................... 89
DSCAN_GetHardwareTimeResolution
Syntax
DSTCanError DSCAN_GetHardwareTimeResolution(DSTCanHandle tChannelHandle,
uint64_t* pui64TimeResolution);
Description The hardware time resolution defines the time unit of the hardware time which
can be obtained by DSCAN_GetHardwareTime.
85
May 2024 dSPACE CAN API 2.0 C Reference
Communication
DSCAN_GetHardwareTime
Syntax
DSTCanError DSCAN_GetHardwareTime(DSTCanHandle tChannelHandle,
uint64_t* pui64Time);
86
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_ResetHardwareTime
Note
This is not supported for CAN interfaces from dSPACE and Kvaser.
Syntax
DSTCanError DSCAN_ResetHardwareTime(DSTCanHandle tChannelHandle);
DSCAN_GetReceiveQueueLevel
Purpose To return the count of CAN messages in the receive queue of a CAN channel.
Syntax
DSTCanError DSCAN_GetReceiveQueueLevel(DSTCanHandle tChannelHandle,
uint32_t* pulCanMessagesCount);
87
May 2024 dSPACE CAN API 2.0 C Reference
Communication
DSCAN_ReadReceiveQueue
Purpose To read CAN messages from the receive queue of a CAN channel.
Syntax
DSTCanError DSCAN_ReadReceiveQueue(DSTCanHandle tChannelHandle,
uint32_t* pulCanMessagesCount,
DSSCanMessage* ptCanMessagesArray);
Description You can call the function with the NULL pointer instead of
ptCanMessagesArray. In this case only the count of CAN messages in
the receive queue is obtained. This function call is equal to a call of
DSCAN_GetReceiveQueueLevel.
Parameters (In, Out) pulCanMessagesCount Count of CAN messages in the receive queue.
§ If used as In parameter:
Lets you specify the maximum number of CAN messages to obtain. To obtain
the information, call the function with pulCanMessagesCount set to the
maximum number of CAN messages to get. This is usually the size of the
ptCanMessagesArray parameter.
§ If used as Out parameter:
Lets you get the number of CAN messages which have been written to the
ptCanMessagesArray parameter (the actual count is always less than or
equal to the initial maximum value).
88
dSPACE CAN API 2.0 C Reference May 2024
Functions
References
DSCAN_ReadReceiveQueueAndDeactivateChannel................................................................... 89
DSCAN_ReadReceiveQueueAndDeactivateChannel
Purpose To read CAN messages from the receive queue of a CAN channel, and deactivate
CAN communication of the channel.
Syntax
DSTCanError DSCAN_ReadReceiveQueueAndDeactivateChannel(DSTCanHandle tChannelHandle,
uint32_t* pulCanMessagesCount,
DSSCanMessage* ptCanMessagesArray);
Description Using the function has the same effect as sequentially calling the following
functions:
1. DSCAN_ReadReceiveQueue
2. DSCAN_DeactivateChannel
Tip
You can call the function with the NULL pointer instead of
ptCanMessagesArray. In this case only the count of CAN messages in the
receive queue is obtained.
89
May 2024 dSPACE CAN API 2.0 C Reference
Communication
Parameters (In, Out) pulCanMessagesCount Count of CAN messages in the receive queue.
§ If used as In parameter:
Lets you specify the maximum number of CAN messages to obtain. To obtain
the information, call the function with pulCanMessagesCount set to the
maximum number of CAN messages to get. This is usually the size of the
ptCanMessagesArray parameter.
§ If used as Out parameter:
Lets you get the number of CAN messages that were written to the
ptCanMessagesArray parameter (the actual count is always less than or
equal to the initial maximum value).
References
DSCAN_DeactivateChannel...................................................................................................... 84
DSCAN_EnableBusStatistics...................................................................................................... 72
DSCAN_ReadReceiveQueue...................................................................................................... 88
DSCAN_FlushReceiveQueue
Syntax
DSTCanError DSCAN_FlushReceiveQueue(DSTCanHandle tChannelHandle);
Description All CAN messages in the receive queue are deleted and cannot be read any
more.
90
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_TransmitMessages
Syntax
DSTCanError DSCAN_TransmitMessages(DSTCanHandle tChannelHandle,
uint32_t ulCanMessagesCount,
DSSCanMessage* ptCanMessagesArray);
91
May 2024 dSPACE CAN API 2.0 C Reference
Communication
DSCAN_FlushTransmitQueue
Note
To call the function you must have access permission for the channel.
Syntax
DSTCanError DSCAN_FlushTransmitQueue(DSTCanHandle tChannelHandle);
Description All CAN messages in the transmit queue are deleted and will not be transmitted.
DSCAN_GetBusInfo
Syntax
DSTCanError DSCAN_GetBusInfo(DSTCanHandle tChannelHandle,
DSSCanBusInfo* ptBusInfo);
92
dSPACE CAN API 2.0 C Reference May 2024
Functions
References
93
May 2024 dSPACE CAN API 2.0 C Reference
Communication
94
dSPACE CAN API 2.0 C Reference May 2024
Error Handling
Error Handling
Functions................................................................................................. 97
95
May 2024 dSPACE CAN API 2.0 C Reference
Error Handling
Error Codes
96
dSPACE CAN API 2.0 C Reference May 2024
Functions
Functions
Where to go from here Information in this section
DSCAN_GetErrorText............................................................................... 97
To get the error description for a dSPACE CAN API error code.
DSCAN_GetLastVendorSpecificError......................................................... 98
To get the code and description of a vendor‑specific CAN API error.
dSPACE CAN API errors You can get the error description for dSPACE CAN API error codes via the
DSCAN_GetErrorText function.
Vendor CAN API errors If a dSPACE CAN API function returns the DSCAN_ERR_VENDOR_SPECIFIC error
code, an error occurred in the vendor‑specific CAN API. In this case, you can
get the code and description of the vendor‑specific CAN API error via the
DSCAN_GetLastVendorSpecificError function.
DSCAN_GetLastVendorSpecificError.......................................................................................... 98
DSCAN_GetErrorText
Purpose To get the error description for a dSPACE CAN API error code.
Syntax
DSTCanError DSCAN_GetErrorText(DSTCanError tErrorCode,
char* pszErrorText);
97
May 2024 dSPACE CAN API 2.0 C Reference
Error Handling
DSCAN_GetLastVendorSpecificError
Purpose To get the code and description of a vendor‑specific CAN API error.
Syntax
DSTCanError DSCAN_GetLastVendorSpecificError(DSTCanHandle tChannelHandle,
int32_t* plVendorErrorCode,
char* pszVendorErrorText);
98
dSPACE CAN API 2.0 C Reference May 2024
Functions
References
DSCAN_GetLastVendorSpecificError.......................................................................................... 98
99
May 2024 dSPACE CAN API 2.0 C Reference
Error Handling
100
dSPACE CAN API 2.0 C Reference May 2024
Auxiliary
Auxiliary
101
May 2024 dSPACE CAN API 2.0 C Reference
Auxiliary
Functions
Where to go from here Information in this section
DSCAN_ConvertBaudrateToBitTimingParameters................................... 102
To convert the baud rate to bit timing parameters.
DSCAN_ConvertBaudratesToBitTimingParameters.................................. 104
To convert two baud rates to bit timing parameters with identical sample
point.
DSCAN_ConvertBaudrateToBitTimingParametersWithSameSPAndB
RP......................................................................................................... 105
To convert a baud rate to bit timing parameters whereof the same
sample point and the baud rate prescaler are set to reference values.
DSCAN_ConvertBusTimingRegistersToBitTimingParameters................... 105
To convert bit timing registers to bit timing parameters.
DSCAN_ConvertBitTimingParametersToBaudrate................................... 106
To convert bit timing parameters to baud rate.
DSCAN_ConvertBitTimingParametersToBusTimingRegisters................... 107
To convert bit timing parameters to bit timing registers.
DSCAN_ConvertByteCountToDlc........................................................... 108
To convert data byte count to DLC.
DSCAN_ConvertDlcToByteCount........................................................... 109
To convert DLC to data byte count.
DSCAN_CalculateAcceptanceFilter......................................................... 110
To calculate CAN acceptance code and mask for an array of CAN
identifiers.
DSCAN_MergeAcceptanceFilter............................................................. 111
To merge two CAN acceptance codes and masks.
DSCAN_EncodeBusStatistics.................................................................. 112
To encode CAN bus statistics information from data bytes of a CAN bus
statistics message.
DSCAN_ConvertApiVersionToString....................................................... 113
To convert API version to string.
DSCAN_ConvertBaudrateToBitTimingParameters
102
dSPACE CAN API 2.0 C Reference May 2024
Functions
Syntax
DSTCanError DSCAN_ConvertBaudrateToBitTimingParameters(uint32_t ulClockFrequency,
uint32_t ulBaudrate,
DSSCanBitTimingParameters* ptBitTimingParameters);
Description
Baud Rate Synch. Jump Baud Rate Sample Bit Time Bit Time Sample
Width Prescaler Mode Segment 1 Segment 2 Point
1000 kBit/s 2 1 0 5 2 75%
500 kBit/s 2 2 0 5 2 75%
250 kBit/s 2 4 0 5 2 75%
125 kBit/s 2 8 0 5 2 75%
100 kBit/s 2 10 0 5 2 75%
50 kBit/s 2 20 0 5 2 75%
20 kBit/s 2 50 0 5 2 75%
10 kBit/s 2 50 0 13 2 87%
§ For all other combinations of baud rate and clock frequency values, the bit
timing parameters are determined according to the following conditions:
§ Sample point is equal or close to:
§ 75 % for classic CAN baud rates (≤ 1 Mbit/s)
§ 80 % for CAN FD baud rates (> 1 Mbit/s)
§ Lowest nominal bit time for the desired sample point
§ Sample mode is 0
103
May 2024 dSPACE CAN API 2.0 C Reference
Auxiliary
DSCAN_ConvertBaudratesToBitTimingParameters
Purpose To convert two baud rates to bit timing parameters with identical sample point.
Syntax
DSTCanError DSCAN_ConvertBaudratesToBitTimingParameters(uint32_t ulClockFrequency,
uint32_t ulReferenceSamplePoint,
uint32_t ulBaudrate_1,
uint32_t ulBaudrate_2,
DSSCanBitTimingParameters* ptBitTimingParameters_1,
DSSCanBitTimingParameters* ptBitTimingParameters_2);
104
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSCAN_ConvertBaudrateToBitTimingParametersWithSameSPAndBRP
Purpose To convert a baud rate to bit timing parameters whereof the same sample point
and the baud rate prescaler are set to reference values.
Syntax
DSTCanError DSCAN_ConvertBaudrateToBitTimingParametersWithSameSPAndBRP(
uint32_t ulClockFrequency,
DSSCanBitTimingParameters* ptReferenceBitTimingParameters,
uint32_t ulBaudrate,
DSSCanBitTimingParameters* ptBitTimingParameters);
DSCAN_ConvertBusTimingRegistersToBitTimingParameters
105
May 2024 dSPACE CAN API 2.0 C Reference
Auxiliary
Syntax
DSTCanError DSCAN_ConvertBusTimingRegistersToBitTimingParameters(uint8_t ucBTR0,
uint8_t ucBTR1,
DSSCanBitTimingParameters* ptBitTimingParameters);
Description For classic CAN, bit timing parameters can be packed to bus timing registers
(BTRs):
BTR0
7 6 5 4 3 2 1 0
SJW1) -1 BRP2) -1
1) Synchronization jump width
2) Baud rate prescaler
BTR1
7 6 5 4 3 2 1 0
SAM1) TSEG22) -1 TSEG13) -1
1) Sample mode
2) Bit time segment 2
3) Bit time segment 1
DSCAN_ConvertBitTimingParametersToBaudrate
106
dSPACE CAN API 2.0 C Reference May 2024
Functions
Syntax
DSTCanError DSCAN_ConvertBitTimingParametersToBaudrate(uint32_t ulClockFrequency,
DSSCanBitTimingParameters* ptBitTimingParameters,
uint32_t* pulBaudrate);
Description The baud rate value is calculated via the following formula:
Clock frequency
Baud rate = Baud rate prescaler · 1 + Bit time segment 1 + Bit time segment 2
DSCAN_ConvertBitTimingParametersToBusTimingRegisters
Syntax
DSTCanError DSCAN_ConvertBitTimingParametersToBusTimingRegisters(DSSCanBitTimingParameters* ptBitTimingParameters,
uint8_t* pucBTR0,
uint8_t* pucBTR1);
Description For classic CAN, bit timing parameters can be packed to bus timing registers
(BTRs):
107
May 2024 dSPACE CAN API 2.0 C Reference
Auxiliary
BTR0
7 6 5 4 3 2 1 0
SJW1) -1 BRP2) -1
1) Synchronization jump width
2) Baud rate prescaler
BTR1
7 6 5 4 3 2 1 0
SAM1) TSEG22) -1 TSEG13) -1
1) Sample mode
2) Bit time segment 2
3) Bit time segment 1
DSCAN_ConvertByteCountToDlc
Syntax
uint16_t DSCAN_ConvertByteCountToDlc(uint16_t usByteCount);
108
dSPACE CAN API 2.0 C Reference May 2024
Functions
Description The conversion between the data length code (DLC) and data byte count is
performed according to the following mapping:
DSCAN_ConvertDlcToByteCount
Syntax
uint16_t DSCAN_ConvertDlcToByteCount(uint16_t usDLC);
109
May 2024 dSPACE CAN API 2.0 C Reference
Auxiliary
Description The conversion between the data length code (DLC) and data byte count is
performed according to the following mapping:
DSCAN_CalculateAcceptanceFilter
Purpose To calculate CAN acceptance code and mask for an array of CAN identifiers.
110
dSPACE CAN API 2.0 C Reference May 2024
Functions
Syntax
DSTCanError DSCAN_CalculateAcceptanceFilter(uint32_t* pulCanIdentifiersArray,
uint32_t ulCanIdentifiersCount,
DSECanIdentifierType tCanIdentifiersType,
uint32_t* pulCode,
uint32_t* pulMask);
References
DSCAN_SetAcceptance............................................................................................................ 68
DSCAN_MergeAcceptanceFilter
Syntax
DSTCanError DSCAN_MergeAcceptanceFilter(uint32_t ulCode1,
uint32_t ulMask1,
uint32_t ulCode2,
uint32_t ulMask2,
DSECanIdentifierType tCanIdentifiersType,
uint32_t* pulResultCode,
uint32_t* pulResultMask);
111
May 2024 dSPACE CAN API 2.0 C Reference
Auxiliary
DSCAN_SetAcceptance............................................................................................................ 68
DSCAN_EncodeBusStatistics
Purpose To encode CAN bus statistics information from data bytes of a CAN bus statistics
message.
Syntax
DSTCanError DSCAN_EncodeBusStatistics(uint8_t ucDataBytes[DSCAN_MAX_DATA_LENGTH],
DSSCanBusStatistics* ptBusStatistics);
112
dSPACE CAN API 2.0 C Reference May 2024
Functions
DSSCanBusStatistics................................................................................................................. 79
DSCAN_ConvertApiVersionToString
Syntax
DSTCanError DSCAN_ConvertApiVersionToString(uint32_t ulApiVersion,
char* pszApiVersion);
113
May 2024 dSPACE CAN API 2.0 C Reference
Auxiliary
114
dSPACE CAN API 2.0 C Reference May 2024
Appendix
Appendix
Troubleshooting..................................................................................... 117
To solve problems when using the dSPACE CAN API 2.0.
Limitations............................................................................................. 118
There are some limitations when you use the dSPACE CAN API 2.0.
Basics on CAN FD
Introduction Using the CAN FD protocol allows data rates higher than 1 MBit/s and payloads
longer than 8 bytes per message.
Basics on CAN FD CAN FD stands for CAN with Flexible Data Rate. The CAN FD protocol is
based on the CAN protocol as specified in ISO 11898‑1. Compared with the
classic CAN protocol, CAN FD comes with an increased bandwidth for the serial
communication. The improvement is based on two factors:
§ The CAN FD protocol allows you to use CAN messages with longer data fields
(up to 64 bytes).
§ The CAN FD protocol allows you to use a higher bit rate (typically higher by a
factor of 8). It is possible to switch inside the message to the faster bit rate.
115
May 2024 dSPACE CAN API 2.0 C Reference
Appendix
§ Data phase
The data phase spans the phase where the data bits, CRC and length
information are transferred. The data phase can be configured to have a
higher bit rate than the arbitration phase, so that data bits are transferred with
the preconfigured higher bit rate.
At the end of the data phase, CAN FD returns to the standard data rate.
The following illustration shows:
§ A classic CAN message
§ A CAN FD message using a higher bit rate during the data phase
§ A CAN FD message with longer payload using a higher bit rate
You can see the implications of the CAN FD features: The arbitration phases are
identical in all cases, because the standard bit rate is always used. The lengths of
the data phases differ depending on the payload length and bit rate used.
Arbitration Arbitration
phase Data phase phase
Arbitration Arbitration
phase Data phase phase
CAN FD protocols Currently, there are two CAN FD protocols on the market, which are not
compatible with each other.
§ The non‑ISO CAN FD protocol represents the original CAN FD protocol from
Bosch.
§ The ISO CAN FD protocol represents the CAN FD protocol according to the
ISO 11898‑1:2015 standard.
Compared to the non‑ISO CAN FD protocol, the ISO CAN FD protocol comes
with an improved failure detection capability.
116
dSPACE CAN API 2.0 C Reference May 2024
Troubleshooting
Troubleshooting
Problem in connection Currently, there are two CAN FD protocols on the market, which are not
with incompatible CAN FD compatible with each other.
protocols § The non‑ISO CAN FD protocol represents the original CAN FD protocol from
Bosch.
§ The ISO CAN FD protocol represents the CAN FD protocol according to the
ISO 11898‑1:2015 standard.
117
May 2024 dSPACE CAN API 2.0 C Reference
Appendix
Limitations
Maximum number of § When you use the dSPACE CAN API in connection with CAN interfaces from
CAN interface channels dSPACE, you can use at most 16 CAN interface channels simultaneously.
§ When you use the dSPACE CAN API in connection with CAN interfaces from
other vendors, the maximum number of CAN interface channels depends on
the vendor's CAN driver software.
Maximum number of clients § When you use the dSPACE CAN API in connection with CAN interfaces from
dSPACE, the number of clients is limited to 32.
This applies to both CAN and CAN FD.
§ When you use the dSPACE CAN API in connection with CAN interfaces from
other vendors, the maximum number of clients depends on the vendor's
CAN driver software.
118
dSPACE CAN API 2.0 C Reference May 2024
Index
Index
DSCAN_MAX_TEXT_LENGTH 24
B DSCAN_MergeAcceptanceFilter 111
DSCAN_ReadReceiveQueue 88
Bit timing parameters and baud rates
DSCAN_ReadReceiveQueueAndDeactivateChann
Basics 55
el 89
DSCAN_RegisterChannel 57
C DSCAN_ResetHardwareTime 87
channel information functions DSCAN_SetAcceptance 68
basics 43 DSCAN_SetBaudrate 65
Common Program Data folder 8 DSCAN_SetChannelOutput 67
communication functions DSCAN_SetEventNotification 71
basics 82 DSCAN_SetTransmitAcknowledge 67
Configuration Functions DSCAN_TransmitMessages 91
Basics 54 DSCAN_UnregisterChannel 60
DSECanBusStatus 75
DSECanChannelsSearchAttributeType 39
D
DSECanIdentifierType 51
Demo 17 DSECanMessageType 75
Documents folder 8 DSSCanBitTimingParameters 52
DSCAN_ActivateChannel 84 DSSCanBusInfo 77
DSCAN_CalculateAcceptanceFilter 110 DSSCanBusStatistics 79
DSCAN_CLOCK_FREQUENCY_SJA1000 24 DSSCanChannelInfo 40
DSCAN_ConvertApiVersionToString 113 DSSCanChannelsSearchAttribute 42
DSCAN_ConvertBaudratesToBitTimingParameters DSSCanMessage 78
104 DSSCanVendorInfo 31
DSCAN_ConvertBaudrateToBitTimingParameters DSTCanError 24
102 DSTCanHandle 24
DSCAN_ConvertBaudrateToBitTimingParameters DSTEventHandle 24
WithSameSPAndBRP 105
DSCAN_ConvertBitTimingParametersToBaudrate
E
106
DSCAN_ConvertBitTimingParametersToBusTimin Error Codes 96
gRegisters 107
DSCAN_ConvertBusTimingRegistersToBitTimingP L
arameters 105
Local Program Data folder 8
DSCAN_ConvertByteCountToDlc 108
DSCAN_ConvertDlcToByteCount 109
DSCAN_CRYSTAL_FREQUENCY_SJA1000 24 V
DSCAN_DeactivateChannel 84 vendor information functions
DSCAN_EnableBusStatistics 72 basics 33
DSCAN_FlushReceiveQueue 90
DSCAN_FlushTransmitQueue 92
DSCAN_GetAvailableChannels 45
DSCAN_GetAvailableChannelsCount 44
DSCAN_GetBaudrate 66
DSCAN_GetBusInfo 92
DSCAN_GetBusType 26
DSCAN_GetChannelCapabilities 63
DSCAN_GetChannelInformation 61
DSCAN_GetErrorText 97
DSCAN_GetHardwareTime 86
DSCAN_GetHardwareTimeResolution 85
DSCAN_GetLastVendorSpecificError 98
DSCAN_GetReceiveQueueLevel 87
DSCAN_GetSupportedVendorsCount 34
DSCAN_GetVendorInformation 34
DSCAN_InitChannel 59
DSCAN_INVALID_CAN_HANDLE 24
DSCAN_INVALID_EVENT_HANDLE 24
DSCAN_IsChannelAccessible 64
DSCAN_IsChannelAvailable 46
DSCAN_MAX_DATA_LENGTH 24
DSCAN_MAX_NAME_LENGTH 24
DSCAN_MAX_RX_QUEUE_SIZE 24
119
May 2024 dSPACE CAN API 2.0 C Reference
Index
120
dSPACE CAN API 2.0 C Reference May 2024