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