2020-12-26 14:12:05 -08:00
|
|
|
// 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
|
|
|
|
2018-09-24 00:08:25 -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"
|
2021-04-18 20:35:29 -07:00
|
|
|
|
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) {
|
2018-09-19 21:40:47 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
m_handle = HAL_InitializeCAN(
|
2025-02-25 19:07:01 -08:00
|
|
|
busId, static_cast<HAL_CANManufacturer>(deviceManufacturer), deviceId,
|
2018-09-19 21:40:47 -07:00
|
|
|
static_cast<HAL_CANDeviceType>(deviceType), &status);
|
2025-11-07 20:00:43 -05:00
|
|
|
WPILIB_CheckErrorStatus(status, "device id {} mfg {} type {}", deviceId,
|
2025-11-07 20:01:58 -05:00
|
|
|
deviceManufacturer, deviceType);
|
2018-09-19 21:40:47 -07:00
|
|
|
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage(
|
|
|
|
|
fmt::format("CAN[{}][{}][{}]", deviceType, deviceManufacturer, deviceId),
|
|
|
|
|
"");
|
2018-09-19 21:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2019-09-28 16:49:34 -07:00
|
|
|
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");
|
2019-09-28 16:49:34 -07:00
|
|
|
}
|
|
|
|
|
|
2025-02-25 19:07:01 -08:00
|
|
|
int CAN::WritePacketNoError(int apiId, const HAL_CANMessage& message) {
|
2020-08-29 23:07:22 -07:00
|
|
|
int32_t status = 0;
|
2025-02-25 19:07:01 -08:00
|
|
|
HAL_WriteCANPacket(m_handle, apiId, &message, &status);
|
2020-08-29 23:07:22 -07:00
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 19:07:01 -08:00
|
|
|
int CAN::WritePacketRepeatingNoError(int apiId, const HAL_CANMessage& message,
|
2020-08-29 23:07:22 -07:00
|
|
|
int repeatMs) {
|
|
|
|
|
int32_t status = 0;
|
2025-02-25 19:07:01 -08:00
|
|
|
HAL_WriteCANPacketRepeating(m_handle, apiId, &message, repeatMs, &status);
|
2020-08-29 23:07:22 -07:00
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 19:07:01 -08:00
|
|
|
int CAN::WriteRTRFrameNoError(int apiId, const HAL_CANMessage& message) {
|
2020-08-29 23:07:22 -07:00
|
|
|
int32_t status = 0;
|
2025-02-25 19:07:01 -08:00
|
|
|
HAL_WriteCANRTRFrame(m_handle, apiId, &message, &status);
|
2020-08-29 23:07:22 -07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|