Files
allwpilib/wpilibc/src/main/native/cpp/hardware/bus/CAN.cpp

120 lines
3.6 KiB
C++
Raw Normal View History

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
2018-05-21 16:09:38 -07:00
2025-11-07 19:57:55 -05:00
#include "wpi/hal/CAN.h"
2025-11-07 19:56:21 -05:00
#include "wpi/hardware/bus/CAN.hpp"
2018-05-21 16:09:38 -07:00
#include <utility>
2025-11-07 19:56:21 -05:00
#include "wpi/hal/CANAPI.h"
#include "wpi/hal/Errors.h"
#include "wpi/hal/UsageReporting.h"
#include "wpi/system/Errors.hpp"
2025-11-07 20:00:05 -05:00
using namespace wpi;
2018-05-21 16:09:38 -07:00
2025-02-25 19:07:01 -08:00
CAN::CAN(int busId, int deviceId)
: CAN{busId, deviceId, kTeamManufacturer, kTeamDeviceType} {}
2018-05-21 16:09:38 -07:00
2025-02-25 19:07:01 -08:00
CAN::CAN(int busId, int deviceId, int deviceManufacturer, int deviceType) {
int32_t status = 0;
m_handle = HAL_InitializeCAN(
2025-02-25 19:07:01 -08:00
busId, static_cast<HAL_CANManufacturer>(deviceManufacturer), deviceId,
static_cast<HAL_CANDeviceType>(deviceType), &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "device id {} mfg {} type {}", deviceId,
2021-05-23 19:33:33 -07:00
deviceManufacturer, deviceType);
HAL_ReportUsage(
fmt::format("CAN[{}][{}][{}]", deviceType, deviceManufacturer, deviceId),
"");
}
2025-02-25 19:07:01 -08:00
void CAN::WritePacket(int apiId, const HAL_CANMessage& message) {
2018-05-21 16:09:38 -07:00
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_WriteCANPacket(m_handle, apiId, &message, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "WritePacket");
2018-05-21 16:09:38 -07:00
}
2025-02-25 19:07:01 -08:00
void CAN::WritePacketRepeating(int apiId, const HAL_CANMessage& message,
2018-05-21 16:09:38 -07:00
int repeatMs) {
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_WriteCANPacketRepeating(m_handle, apiId, &message, repeatMs, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "WritePacketRepeating");
2018-05-21 16:09:38 -07:00
}
2025-02-25 19:07:01 -08:00
void CAN::WriteRTRFrame(int apiId, const HAL_CANMessage& message) {
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_WriteCANRTRFrame(m_handle, apiId, &message, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "WriteRTRFrame");
}
2025-02-25 19:07:01 -08:00
int CAN::WritePacketNoError(int apiId, const HAL_CANMessage& message) {
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_WriteCANPacket(m_handle, apiId, &message, &status);
return status;
}
2025-02-25 19:07:01 -08:00
int CAN::WritePacketRepeatingNoError(int apiId, const HAL_CANMessage& message,
int repeatMs) {
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_WriteCANPacketRepeating(m_handle, apiId, &message, repeatMs, &status);
return status;
}
2025-02-25 19:07:01 -08:00
int CAN::WriteRTRFrameNoError(int apiId, const HAL_CANMessage& message) {
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_WriteCANRTRFrame(m_handle, apiId, &message, &status);
return status;
}
2018-05-21 16:09:38 -07:00
void CAN::StopPacketRepeating(int apiId) {
int32_t status = 0;
HAL_StopCANPacketRepeating(m_handle, apiId, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "StopPacketRepeating");
2018-05-21 16:09:38 -07:00
}
2025-02-25 19:07:01 -08:00
bool CAN::ReadPacketNew(int apiId, HAL_CANReceiveMessage* data) {
2018-05-21 16:09:38 -07:00
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_ReadCANPacketNew(m_handle, apiId, data, &status);
2018-05-21 16:09:38 -07:00
if (status == HAL_ERR_CANSessionMux_MessageNotFound) {
return false;
}
if (status != 0) {
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "ReadPacketNew");
2018-05-21 16:09:38 -07:00
return false;
} else {
return true;
}
}
2025-02-25 19:07:01 -08:00
bool CAN::ReadPacketLatest(int apiId, HAL_CANReceiveMessage* data) {
2018-05-21 16:09:38 -07:00
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_ReadCANPacketLatest(m_handle, apiId, data, &status);
2018-05-21 16:09:38 -07:00
if (status == HAL_ERR_CANSessionMux_MessageNotFound) {
return false;
}
if (status != 0) {
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "ReadPacketLatest");
2018-05-21 16:09:38 -07:00
return false;
} else {
return true;
}
}
2025-02-25 19:07:01 -08:00
bool CAN::ReadPacketTimeout(int apiId, int timeoutMs,
HAL_CANReceiveMessage* data) {
2018-05-21 16:09:38 -07:00
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_ReadCANPacketTimeout(m_handle, apiId, data, timeoutMs, &status);
2018-05-21 16:09:38 -07:00
if (status == HAL_CAN_TIMEOUT ||
status == HAL_ERR_CANSessionMux_MessageNotFound) {
return false;
}
if (status != 0) {
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "ReadPacketTimeout");
2018-05-21 16:09:38 -07:00
return false;
} else {
return true;
}
}