WPILibC++ 2024.3.2
CANAPI.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#include "hal/CANAPITypes.h"
10#include "hal/Types.h"
11
12/**
13 * @defgroup hal_canapi CAN API Functions
14 * @ingroup hal_capi
15 * @{
16 */
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/**
23 * Reads the current value of the millisecond-resolution timer that the CAN API
24 * functions use as a time base.
25 *
26 * @return Current value of timer used as a base time by the CAN API in
27 * milliseconds.
28 */
30
31/**
32 * Initializes a CAN device.
33 *
34 * These follow the FIRST standard CAN layout.
35 * https://docs.wpilib.org/en/stable/docs/software/can-devices/can-addressing.html
36 *
37 * @param[in] manufacturer the can manufacturer
38 * @param[in] deviceId the device ID (0-63)
39 * @param[in] deviceType the device type
40 * @param[out] status Error status variable. 0 on success.
41 * @return the created CAN handle
42 */
44 int32_t deviceId, HAL_CANDeviceType deviceType,
45 int32_t* status);
46
47/**
48 * Frees a CAN device
49 *
50 * @param[in,out] handle the CAN handle
51 */
53
54/**
55 * Writes a packet to the CAN device with a specific ID.
56 *
57 * This ID is 10 bits.
58 *
59 * @param[in] handle the CAN handle
60 * @param[in] data the data to write (0-8 bytes)
61 * @param[in] length the length of data (0-8)
62 * @param[in] apiId the ID to write (0-1023 bits)
63 * @param[out] status Error status variable. 0 on success.
64 */
65void HAL_WriteCANPacket(HAL_CANHandle handle, const uint8_t* data,
66 int32_t length, int32_t apiId, int32_t* status);
67
68/**
69 * Writes a repeating packet to the CAN device with a specific ID.
70 *
71 * This ID is 10 bits.
72 *
73 * The RoboRIO will automatically repeat the packet at the specified interval
74 *
75 * @param[in] handle the CAN handle
76 * @param[in] data the data to write (0-8 bytes)
77 * @param[in] length the length of data (0-8)
78 * @param[in] apiId the ID to write (0-1023)
79 * @param[in] repeatMs the period to repeat in ms
80 * @param[out] status Error status variable. 0 on success.
81 */
82void HAL_WriteCANPacketRepeating(HAL_CANHandle handle, const uint8_t* data,
83 int32_t length, int32_t apiId,
84 int32_t repeatMs, int32_t* status);
85
86/**
87 * Writes an RTR frame of the specified length to the CAN device with the
88 * specific ID.
89 *
90 * By spec, the length must be equal to the length sent by the other device,
91 * otherwise behavior is unspecified.
92 *
93 * @param[in] handle the CAN handle
94 * @param[in] length the length of data to request (0-8)
95 * @param[in] apiId the ID to write (0-1023)
96 * @param[out] status Error status variable. 0 on success.
97 */
98void HAL_WriteCANRTRFrame(HAL_CANHandle handle, int32_t length, int32_t apiId,
99 int32_t* status);
100
101/**
102 * Stops a repeating packet with a specific ID.
103 *
104 * This ID is 10 bits.
105 *
106 * @param[in] handle the CAN handle
107 * @param[in] apiId the ID to stop repeating (0-1023)
108 * @param[out] status Error status variable. 0 on success.
109 */
110void HAL_StopCANPacketRepeating(HAL_CANHandle handle, int32_t apiId,
111 int32_t* status);
112
113/**
114 * Reads a new CAN packet.
115 *
116 * This will only return properly once per packet received. Multiple calls
117 * without receiving another packet will return an error code.
118 *
119 * @param[in] handle the CAN handle
120 * @param[in] apiId the ID to read (0-1023)
121 * @param[out] data the packet data (8 bytes)
122 * @param[out] length the received length (0-8 bytes)
123 * @param[out] receivedTimestamp the packet received timestamp in ms (based off
124 * of CLOCK_MONOTONIC)
125 * @param[out] status Error status variable. 0 on success.
126 */
127void HAL_ReadCANPacketNew(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
128 int32_t* length, uint64_t* receivedTimestamp,
129 int32_t* status);
130
131/**
132 * Reads a CAN packet. The will continuously return the last packet received,
133 * without accounting for packet age.
134 *
135 * @param[in] handle the CAN handle
136 * @param[in] apiId the ID to read (0-1023)
137 * @param[out] data the packet data (8 bytes)
138 * @param[out] length the received length (0-8 bytes)
139 * @param[out] receivedTimestamp the packet received timestamp in ms (based off
140 * of CLOCK_MONOTONIC)
141 * @param[out] status Error status variable. 0 on success.
142 */
143void HAL_ReadCANPacketLatest(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
144 int32_t* length, uint64_t* receivedTimestamp,
145 int32_t* status);
146
147/**
148 * Reads a CAN packet. The will return the last packet received until the
149 * packet is older then the requested timeout. Then it will return an error
150 * code.
151 *
152 * @param[in] handle the CAN handle
153 * @param[in] apiId the ID to read (0-1023)
154 * @param[out] data the packet data (8 bytes)
155 * @param[out] length the received length (0-8 bytes)
156 * @param[out] receivedTimestamp the packet received timestamp in ms (based off
157 * of CLOCK_MONOTONIC)
158 * @param[out] timeoutMs the timeout time for the packet
159 * @param[out] status Error status variable. 0 on success.
160 */
161void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId,
162 uint8_t* data, int32_t* length,
163 uint64_t* receivedTimestamp, int32_t timeoutMs,
164 int32_t* status);
165
166#ifdef __cplusplus
167} // extern "C"
168#endif
169/** @} */
HAL_CANHandle HAL_InitializeCAN(HAL_CANManufacturer manufacturer, int32_t deviceId, HAL_CANDeviceType deviceType, int32_t *status)
Initializes a CAN device.
void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId, uint8_t *data, int32_t *length, uint64_t *receivedTimestamp, int32_t timeoutMs, int32_t *status)
Reads a CAN packet.
void HAL_ReadCANPacketNew(HAL_CANHandle handle, int32_t apiId, uint8_t *data, int32_t *length, uint64_t *receivedTimestamp, int32_t *status)
Reads a new CAN packet.
void HAL_WriteCANPacket(HAL_CANHandle handle, const uint8_t *data, int32_t length, int32_t apiId, int32_t *status)
Writes a packet to the CAN device with a specific ID.
void HAL_CleanCAN(HAL_CANHandle handle)
Frees a CAN device.
void HAL_WriteCANPacketRepeating(HAL_CANHandle handle, const uint8_t *data, int32_t length, int32_t apiId, int32_t repeatMs, int32_t *status)
Writes a repeating packet to the CAN device with a specific ID.
uint32_t HAL_GetCANPacketBaseTime(void)
Reads the current value of the millisecond-resolution timer that the CAN API functions use as a time ...
void HAL_ReadCANPacketLatest(HAL_CANHandle handle, int32_t apiId, uint8_t *data, int32_t *length, uint64_t *receivedTimestamp, int32_t *status)
Reads a CAN packet.
HAL_CANDeviceType
The CAN device type.
Definition: CANAPITypes.h:22
HAL_CANManufacturer
The CAN manufacturer ID.
Definition: CANAPITypes.h:56
void HAL_WriteCANRTRFrame(HAL_CANHandle handle, int32_t length, int32_t apiId, int32_t *status)
Writes an RTR frame of the specified length to the CAN device with the specific ID.
void HAL_StopCANPacketRepeating(HAL_CANHandle handle, int32_t apiId, int32_t *status)
Stops a repeating packet with a specific ID.
HAL_Handle HAL_CANHandle
Definition: Types.h:51