WPILibC++ 2025.3.2
Loading...
Searching...
No Matches
CAN.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9/**
10 * @defgroup hal_can CAN Functions
11 * @ingroup hal_capi
12 * @{
13 */
14// These are copies of defines located in CANSessionMux.h prepended with HAL_
15
16/**
17 * Flag for sending a CAN message once.
18 */
19#define HAL_CAN_SEND_PERIOD_NO_REPEAT 0
20
21/**
22 * Flag for stopping periodic CAN message sends.
23 */
24#define HAL_CAN_SEND_PERIOD_STOP_REPEATING -1
25
26/**
27 * Mask for "is frame remote" in message ID.
28 */
29#define HAL_CAN_IS_FRAME_REMOTE 0x80000000
30
31/**
32 * Mask for "is frame 11 bits" in message ID.
33 */
34#define HAL_CAN_IS_FRAME_11BIT 0x40000000
35
36#define HAL_ERR_CANSessionMux_InvalidBuffer -44086
37#define HAL_ERR_CANSessionMux_MessageNotFound -44087
38#define HAL_WARN_CANSessionMux_NoToken 44087
39#define HAL_ERR_CANSessionMux_NotAllowed -44088
40#define HAL_ERR_CANSessionMux_NotInitialized -44089
41#define HAL_ERR_CANSessionMux_SessionOverrun 44050
42/** @} */
43
44/**
45 * @defgroup hal_canstream CAN Stream Functions
46 * @ingroup hal_capi
47 * @{
48 */
49/**
50 * Storage for CAN Stream Messages.
51 */
53 /** The message ID */
54 uint32_t messageID;
55 /** The packet received timestamp (based off of CLOCK_MONOTONIC) */
56 uint32_t timeStamp;
57 /** The message data */
58 uint8_t data[8];
59 /** The size of the data received (0-8 bytes) */
60 uint8_t dataSize;
61};
62/** @} */
63#ifdef __cplusplus
64extern "C" {
65#endif
66/**
67 * @ingroup hal_can
68 * @{
69 */
70/**
71 * Sends a CAN message.
72 *
73 * @param[in] messageID the CAN ID to send
74 * @param[in] data the data to send (0-8 bytes)
75 * @param[in] dataSize the size of the data to send (0-8 bytes)
76 * @param[in] periodMs the period to repeat the packet at. Use
77 * HAL_CAN_SEND_PERIOD_NO_REPEAT to not repeat.
78 * @param[out] status Error status variable. 0 on success.
79 */
80void HAL_CAN_SendMessage(uint32_t messageID, const uint8_t* data,
81 uint8_t dataSize, int32_t periodMs, int32_t* status);
82
83/**
84 * Receives a CAN message.
85 *
86 * @param[out] messageID store for the received message ID
87 * @param[in] messageIDMask the message ID mask to look for
88 * @param[out] data data output (8 bytes)
89 * @param[out] dataSize data length (0-8 bytes)
90 * @param[out] timeStamp the packet received timestamp (based off of
91 * CLOCK_MONOTONIC)
92 * @param[out] status Error status variable. 0 on success.
93 */
94void HAL_CAN_ReceiveMessage(uint32_t* messageID, uint32_t messageIDMask,
95 uint8_t* data, uint8_t* dataSize,
96 uint32_t* timeStamp, int32_t* status);
97/**
98 * Gets CAN status information.
99 *
100 * @param[out] percentBusUtilization the bus utilization
101 * @param[out] busOffCount the number of bus off errors
102 * @param[out] txFullCount the number of tx full errors
103 * @param[out] receiveErrorCount the number of receive errors
104 * @param[out] transmitErrorCount the number of transmit errors
105 * @param[out] status Error status variable. 0 on success.
106 */
107void HAL_CAN_GetCANStatus(float* percentBusUtilization, uint32_t* busOffCount,
108 uint32_t* txFullCount, uint32_t* receiveErrorCount,
109 uint32_t* transmitErrorCount, int32_t* status);
110/** @} */
111/**
112 * @ingroup hal_canstream
113 * @{
114 */
115/**
116 * Opens a CAN stream.
117 *
118 * @param[out] sessionHandle output for the session handle
119 * @param[in] messageID the message ID to read
120 * @param[in] messageIDMask the message ID mask
121 * @param[in] maxMessages the maximum number of messages to stream
122 * @param[out] status Error status variable. 0 on success.
123 */
124void HAL_CAN_OpenStreamSession(uint32_t* sessionHandle, uint32_t messageID,
125 uint32_t messageIDMask, uint32_t maxMessages,
126 int32_t* status);
127
128/**
129 * Closes a CAN stream.
130 *
131 * @param sessionHandle the session to close
132 */
133void HAL_CAN_CloseStreamSession(uint32_t sessionHandle);
134
135/**
136 * Reads a CAN stream message.
137 *
138 * @param[in] sessionHandle the session handle
139 * @param[in] messages array of messages
140 * @param[in] messagesToRead the max number of messages to read
141 * @param[out] messagesRead the number of messages actually read
142 * @param[out] status Error status variable. 0 on success.
143 */
144void HAL_CAN_ReadStreamSession(uint32_t sessionHandle,
145 struct HAL_CANStreamMessage* messages,
146 uint32_t messagesToRead, uint32_t* messagesRead,
147 int32_t* status);
148#ifdef __cplusplus
149} // extern "C"
150#endif
151/** @} */
void HAL_CAN_GetCANStatus(float *percentBusUtilization, uint32_t *busOffCount, uint32_t *txFullCount, uint32_t *receiveErrorCount, uint32_t *transmitErrorCount, int32_t *status)
Gets CAN status information.
void HAL_CAN_SendMessage(uint32_t messageID, const uint8_t *data, uint8_t dataSize, int32_t periodMs, int32_t *status)
Sends a CAN message.
void HAL_CAN_ReceiveMessage(uint32_t *messageID, uint32_t messageIDMask, uint8_t *data, uint8_t *dataSize, uint32_t *timeStamp, int32_t *status)
Receives a CAN message.
void HAL_CAN_ReadStreamSession(uint32_t sessionHandle, struct HAL_CANStreamMessage *messages, uint32_t messagesToRead, uint32_t *messagesRead, int32_t *status)
Reads a CAN stream message.
void HAL_CAN_CloseStreamSession(uint32_t sessionHandle)
Closes a CAN stream.
void HAL_CAN_OpenStreamSession(uint32_t *sessionHandle, uint32_t messageID, uint32_t messageIDMask, uint32_t maxMessages, int32_t *status)
Opens a CAN stream.
Storage for CAN Stream Messages.
Definition CAN.h:52
uint32_t messageID
The message ID.
Definition CAN.h:54
uint32_t timeStamp
The packet received timestamp (based off of CLOCK_MONOTONIC)
Definition CAN.h:56
uint8_t data[8]
The message data.
Definition CAN.h:58
uint8_t dataSize
The size of the data received (0-8 bytes)
Definition CAN.h:60