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
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/CAN.h"
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2018-10-29 12:49:17 -07:00
|
|
|
#include <hal/CAN.h>
|
|
|
|
|
#include <hal/CANAPI.h>
|
|
|
|
|
#include <hal/Errors.h>
|
|
|
|
|
#include <hal/FRCUsageReporting.h>
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
|
|
|
|
|
2018-05-21 16:09:38 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
CAN::CAN(int deviceId) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_handle =
|
|
|
|
|
HAL_InitializeCAN(kTeamManufacturer, deviceId, kTeamDeviceType, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "device id {}", deviceId);
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_CAN, deviceId + 1);
|
2018-05-21 16:09:38 -07:00
|
|
|
}
|
|
|
|
|
|
2018-09-19 21:40:47 -07:00
|
|
|
CAN::CAN(int deviceId, int deviceManufacturer, int deviceType) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_handle = HAL_InitializeCAN(
|
|
|
|
|
static_cast<HAL_CANManufacturer>(deviceManufacturer), deviceId,
|
|
|
|
|
static_cast<HAL_CANDeviceType>(deviceType), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "device id {} mfg {} type {}", deviceId,
|
|
|
|
|
deviceManufacturer, deviceType);
|
2018-09-19 21:40:47 -07:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_CAN, deviceId + 1);
|
2018-09-19 21:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-21 16:09:38 -07:00
|
|
|
CAN::~CAN() {
|
|
|
|
|
if (m_handle != HAL_kInvalidHandle) {
|
|
|
|
|
HAL_CleanCAN(m_handle);
|
|
|
|
|
m_handle = HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CAN::WritePacket(const uint8_t* data, int length, int apiId) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacket(m_handle, data, length, apiId, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "WritePacket");
|
2018-05-21 16:09:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CAN::WritePacketRepeating(const uint8_t* data, int length, int apiId,
|
|
|
|
|
int repeatMs) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacketRepeating(m_handle, data, length, apiId, repeatMs, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "WritePacketRepeating");
|
2018-05-21 16:09:38 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-28 16:49:34 -07:00
|
|
|
void CAN::WriteRTRFrame(int length, int apiId) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANRTRFrame(m_handle, length, apiId, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "WriteRTRFrame");
|
2019-09-28 16:49:34 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-29 23:07:22 -07:00
|
|
|
int CAN::WritePacketNoError(const uint8_t* data, int length, int apiId) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacket(m_handle, data, length, apiId, &status);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CAN::WritePacketRepeatingNoError(const uint8_t* data, int length, int apiId,
|
|
|
|
|
int repeatMs) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacketRepeating(m_handle, data, length, apiId, repeatMs, &status);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CAN::WriteRTRFrameNoError(int length, int apiId) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANRTRFrame(m_handle, length, apiId, &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);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "StopPacketRepeating");
|
2018-05-21 16:09:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CAN::ReadPacketNew(int apiId, CANData* data) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ReadCANPacketNew(m_handle, apiId, data->data, &data->length,
|
|
|
|
|
&data->timestamp, &status);
|
|
|
|
|
if (status == HAL_ERR_CANSessionMux_MessageNotFound) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (status != 0) {
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "ReadPacketNew");
|
2018-05-21 16:09:38 -07:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CAN::ReadPacketLatest(int apiId, CANData* data) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ReadCANPacketLatest(m_handle, apiId, data->data, &data->length,
|
|
|
|
|
&data->timestamp, &status);
|
|
|
|
|
if (status == HAL_ERR_CANSessionMux_MessageNotFound) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (status != 0) {
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "ReadPacketLatest");
|
2018-05-21 16:09:38 -07:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CAN::ReadPacketTimeout(int apiId, int timeoutMs, CANData* data) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ReadCANPacketTimeout(m_handle, apiId, data->data, &data->length,
|
|
|
|
|
&data->timestamp, timeoutMs, &status);
|
|
|
|
|
if (status == HAL_CAN_TIMEOUT ||
|
|
|
|
|
status == HAL_ERR_CANSessionMux_MessageNotFound) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (status != 0) {
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "ReadPacketTimeout");
|
2018-05-21 16:09:38 -07:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 12:54:03 -04:00
|
|
|
|
|
|
|
|
uint64_t CAN::GetTimestampBaseTime() {
|
|
|
|
|
return HAL_GetCANPacketBaseTime();
|
|
|
|
|
}
|