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