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 "hal/CANAPI.h"
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
#include <wpi/DenseMap.h>
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
#include "CANAPIInternal.h"
|
2018-05-24 17:07:03 -07:00
|
|
|
#include "HALInitializer.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/CAN.h"
|
|
|
|
|
#include "hal/Errors.h"
|
2020-10-03 12:21:03 -04:00
|
|
|
#include "hal/HALBase.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/handles/UnlimitedHandleResource.h"
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
struct Receives {
|
|
|
|
|
uint64_t lastTimeStamp;
|
|
|
|
|
uint8_t data[8];
|
|
|
|
|
uint8_t length;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CANStorage {
|
|
|
|
|
HAL_CANManufacturer manufacturer;
|
|
|
|
|
HAL_CANDeviceType deviceType;
|
|
|
|
|
uint8_t deviceId;
|
|
|
|
|
wpi::mutex mapMutex;
|
|
|
|
|
wpi::SmallDenseMap<int32_t, int32_t> periodicSends;
|
|
|
|
|
wpi::SmallDenseMap<int32_t, Receives> receives;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
static UnlimitedHandleResource<HAL_CANHandle, CANStorage, HAL_HandleEnum::CAN>*
|
|
|
|
|
canHandles;
|
|
|
|
|
|
|
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeCANAPI() {
|
|
|
|
|
static UnlimitedHandleResource<HAL_CANHandle, CANStorage, HAL_HandleEnum::CAN>
|
|
|
|
|
cH;
|
|
|
|
|
canHandles = &cH;
|
|
|
|
|
}
|
|
|
|
|
} // namespace init
|
2018-06-07 22:31:26 -07:00
|
|
|
namespace can {
|
|
|
|
|
int32_t GetCANModuleFromHandle(HAL_CANHandle handle, int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return can->deviceId;
|
|
|
|
|
}
|
|
|
|
|
} // namespace can
|
2018-05-21 16:09:38 -07:00
|
|
|
} // namespace hal
|
|
|
|
|
|
|
|
|
|
static int32_t CreateCANId(CANStorage* storage, int32_t apiId) {
|
|
|
|
|
int32_t createdId = 0;
|
|
|
|
|
createdId |= (static_cast<int32_t>(storage->deviceType) & 0x1F) << 24;
|
|
|
|
|
createdId |= (static_cast<int32_t>(storage->manufacturer) & 0xFF) << 16;
|
|
|
|
|
createdId |= (apiId & 0x3FF) << 6;
|
|
|
|
|
createdId |= (storage->deviceId & 0x3F);
|
|
|
|
|
return createdId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 12:54:03 -04:00
|
|
|
uint32_t HAL_GetCANPacketBaseTime() {
|
|
|
|
|
int status = 0;
|
|
|
|
|
auto basetime = HAL_GetFPGATime(&status);
|
|
|
|
|
// us to ms
|
|
|
|
|
return (basetime / 1000ull) & 0xFFFFFFFF;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 16:09:38 -07:00
|
|
|
HAL_CANHandle HAL_InitializeCAN(HAL_CANManufacturer manufacturer,
|
|
|
|
|
int32_t deviceId, HAL_CANDeviceType deviceType,
|
|
|
|
|
int32_t* status) {
|
2018-05-24 17:07:03 -07:00
|
|
|
hal::init::CheckInit();
|
2018-05-21 16:09:38 -07:00
|
|
|
auto can = std::make_shared<CANStorage>();
|
|
|
|
|
|
|
|
|
|
auto handle = canHandles->Allocate(can);
|
|
|
|
|
|
|
|
|
|
if (handle == HAL_kInvalidHandle) {
|
|
|
|
|
*status = NO_AVAILABLE_RESOURCES;
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
can->deviceId = deviceId;
|
|
|
|
|
can->deviceType = deviceType;
|
|
|
|
|
can->manufacturer = manufacturer;
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_CleanCAN(HAL_CANHandle handle) {
|
|
|
|
|
auto data = canHandles->Free(handle);
|
2023-01-02 02:19:04 -05:00
|
|
|
if (data == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(data->mapMutex);
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
for (auto&& i : data->periodicSends) {
|
|
|
|
|
int32_t s = 0;
|
2019-04-27 20:24:40 -07:00
|
|
|
auto id = CreateCANId(data.get(), i.first);
|
|
|
|
|
HAL_CAN_SendMessage(id, nullptr, 0, HAL_CAN_SEND_PERIOD_STOP_REPEATING, &s);
|
2018-05-21 16:09:38 -07:00
|
|
|
i.second = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_WriteCANPacket(HAL_CANHandle handle, const uint8_t* data,
|
|
|
|
|
int32_t length, int32_t apiId, int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto id = CreateCANId(can.get(), apiId);
|
|
|
|
|
|
|
|
|
|
HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status);
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-05-21 16:09:38 -07:00
|
|
|
can->periodicSends[apiId] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_WriteCANPacketRepeating(HAL_CANHandle handle, const uint8_t* data,
|
|
|
|
|
int32_t length, int32_t apiId,
|
|
|
|
|
int32_t repeatMs, int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto id = CreateCANId(can.get(), apiId);
|
|
|
|
|
|
|
|
|
|
HAL_CAN_SendMessage(id, data, length, repeatMs, status);
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-05-21 16:09:38 -07:00
|
|
|
can->periodicSends[apiId] = repeatMs;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 16:49:34 -07:00
|
|
|
void HAL_WriteCANRTRFrame(HAL_CANHandle handle, int32_t length, int32_t apiId,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto id = CreateCANId(can.get(), apiId);
|
|
|
|
|
id |= HAL_CAN_IS_FRAME_REMOTE;
|
|
|
|
|
uint8_t data[8];
|
|
|
|
|
std::memset(data, 0, sizeof(data));
|
|
|
|
|
|
|
|
|
|
HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status);
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
std::scoped_lock lock(can->mapMutex);
|
|
|
|
|
can->periodicSends[apiId] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 16:09:38 -07:00
|
|
|
void HAL_StopCANPacketRepeating(HAL_CANHandle handle, int32_t apiId,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto id = CreateCANId(can.get(), apiId);
|
|
|
|
|
|
|
|
|
|
HAL_CAN_SendMessage(id, nullptr, 0, HAL_CAN_SEND_PERIOD_STOP_REPEATING,
|
|
|
|
|
status);
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-05-21 16:09:38 -07:00
|
|
|
can->periodicSends[apiId] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ReadCANPacketNew(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
|
|
|
|
|
int32_t* length, uint64_t* receivedTimestamp,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 20:49:03 -07:00
|
|
|
uint32_t messageId = CreateCANId(can.get(), apiId);
|
2018-05-21 16:09:38 -07:00
|
|
|
uint8_t dataSize = 0;
|
|
|
|
|
uint32_t ts = 0;
|
2018-06-07 20:49:03 -07:00
|
|
|
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
if (*status == 0) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-06-07 20:49:03 -07:00
|
|
|
auto& msg = can->receives[messageId];
|
2018-05-21 16:09:38 -07:00
|
|
|
msg.length = dataSize;
|
2018-12-29 13:57:23 -08:00
|
|
|
msg.lastTimeStamp = ts;
|
2018-12-14 17:43:48 -08:00
|
|
|
// The NetComm call placed in data, copy into the msg
|
2018-05-21 16:09:38 -07:00
|
|
|
std::memcpy(msg.data, data, dataSize);
|
|
|
|
|
}
|
|
|
|
|
*length = dataSize;
|
2018-12-29 13:57:23 -08:00
|
|
|
*receivedTimestamp = ts;
|
2018-05-21 16:09:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ReadCANPacketLatest(HAL_CANHandle handle, int32_t apiId, uint8_t* data,
|
|
|
|
|
int32_t* length, uint64_t* receivedTimestamp,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 20:49:03 -07:00
|
|
|
uint32_t messageId = CreateCANId(can.get(), apiId);
|
2018-05-21 16:09:38 -07:00
|
|
|
uint8_t dataSize = 0;
|
|
|
|
|
uint32_t ts = 0;
|
2018-06-07 20:49:03 -07:00
|
|
|
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-05-21 16:09:38 -07:00
|
|
|
if (*status == 0) {
|
|
|
|
|
// fresh update
|
2018-06-07 20:49:03 -07:00
|
|
|
auto& msg = can->receives[messageId];
|
2018-05-21 16:09:38 -07:00
|
|
|
msg.length = dataSize;
|
|
|
|
|
*length = dataSize;
|
2018-12-29 13:57:23 -08:00
|
|
|
msg.lastTimeStamp = ts;
|
|
|
|
|
*receivedTimestamp = ts;
|
2018-12-14 17:43:48 -08:00
|
|
|
// The NetComm call placed in data, copy into the msg
|
2018-05-21 16:09:38 -07:00
|
|
|
std::memcpy(msg.data, data, dataSize);
|
|
|
|
|
} else {
|
2018-06-07 20:49:03 -07:00
|
|
|
auto i = can->receives.find(messageId);
|
2018-05-21 16:09:38 -07:00
|
|
|
if (i != can->receives.end()) {
|
2018-12-14 17:43:48 -08:00
|
|
|
// Read the data from the stored message into the output
|
|
|
|
|
std::memcpy(data, i->second.data, i->second.length);
|
2018-05-21 16:09:38 -07:00
|
|
|
*length = i->second.length;
|
|
|
|
|
*receivedTimestamp = i->second.lastTimeStamp;
|
|
|
|
|
*status = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId,
|
|
|
|
|
uint8_t* data, int32_t* length,
|
|
|
|
|
uint64_t* receivedTimestamp, int32_t timeoutMs,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 20:49:03 -07:00
|
|
|
uint32_t messageId = CreateCANId(can.get(), apiId);
|
2018-05-21 16:09:38 -07:00
|
|
|
uint8_t dataSize = 0;
|
|
|
|
|
uint32_t ts = 0;
|
2018-06-07 20:49:03 -07:00
|
|
|
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-05-21 16:09:38 -07:00
|
|
|
if (*status == 0) {
|
|
|
|
|
// fresh update
|
2018-06-07 20:49:03 -07:00
|
|
|
auto& msg = can->receives[messageId];
|
2018-05-21 16:09:38 -07:00
|
|
|
msg.length = dataSize;
|
|
|
|
|
*length = dataSize;
|
2018-12-29 13:57:23 -08:00
|
|
|
msg.lastTimeStamp = ts;
|
|
|
|
|
*receivedTimestamp = ts;
|
2018-12-14 17:43:48 -08:00
|
|
|
// The NetComm call placed in data, copy into the msg
|
2018-05-21 16:09:38 -07:00
|
|
|
std::memcpy(msg.data, data, dataSize);
|
|
|
|
|
} else {
|
2018-06-07 20:49:03 -07:00
|
|
|
auto i = can->receives.find(messageId);
|
2018-05-21 16:09:38 -07:00
|
|
|
if (i != can->receives.end()) {
|
|
|
|
|
// Found, check if new enough
|
2023-06-07 12:54:03 -04:00
|
|
|
uint32_t now = HAL_GetCANPacketBaseTime();
|
2018-12-29 13:57:23 -08:00
|
|
|
if (now - i->second.lastTimeStamp > static_cast<uint32_t>(timeoutMs)) {
|
2018-05-21 16:09:38 -07:00
|
|
|
// Timeout, return bad status
|
|
|
|
|
*status = HAL_CAN_TIMEOUT;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-14 17:43:48 -08:00
|
|
|
// Read the data from the stored message into the output
|
|
|
|
|
std::memcpy(data, i->second.data, i->second.length);
|
2018-05-21 16:09:38 -07:00
|
|
|
*length = i->second.length;
|
|
|
|
|
*receivedTimestamp = i->second.lastTimeStamp;
|
|
|
|
|
*status = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ReadCANPeriodicPacket(HAL_CANHandle handle, int32_t apiId,
|
|
|
|
|
uint8_t* data, int32_t* length,
|
|
|
|
|
uint64_t* receivedTimestamp, int32_t timeoutMs,
|
|
|
|
|
int32_t periodMs, int32_t* status) {
|
|
|
|
|
auto can = canHandles->Get(handle);
|
|
|
|
|
if (!can) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-14 17:43:48 -08:00
|
|
|
|
2018-06-07 20:49:03 -07:00
|
|
|
uint32_t messageId = CreateCANId(can.get(), apiId);
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-06-07 20:49:03 -07:00
|
|
|
auto i = can->receives.find(messageId);
|
2018-05-21 16:09:38 -07:00
|
|
|
if (i != can->receives.end()) {
|
|
|
|
|
// Found, check if new enough
|
2023-06-07 12:54:03 -04:00
|
|
|
uint32_t now = HAL_GetCANPacketBaseTime();
|
2018-12-29 13:57:23 -08:00
|
|
|
if (now - i->second.lastTimeStamp < static_cast<uint32_t>(periodMs)) {
|
2018-05-21 16:09:38 -07:00
|
|
|
*status = 0;
|
2018-12-14 17:43:48 -08:00
|
|
|
// Read the data from the stored message into the output
|
|
|
|
|
std::memcpy(data, i->second.data, i->second.length);
|
2018-05-21 16:09:38 -07:00
|
|
|
*length = i->second.length;
|
|
|
|
|
*receivedTimestamp = i->second.lastTimeStamp;
|
2018-12-14 17:43:48 -08:00
|
|
|
return;
|
2018-05-21 16:09:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t dataSize = 0;
|
|
|
|
|
uint32_t ts = 0;
|
2018-06-07 20:49:03 -07:00
|
|
|
HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status);
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(can->mapMutex);
|
2018-05-21 16:09:38 -07:00
|
|
|
if (*status == 0) {
|
|
|
|
|
// fresh update
|
2018-06-07 20:49:03 -07:00
|
|
|
auto& msg = can->receives[messageId];
|
2018-05-21 16:09:38 -07:00
|
|
|
msg.length = dataSize;
|
|
|
|
|
*length = dataSize;
|
2018-12-29 13:57:23 -08:00
|
|
|
msg.lastTimeStamp = ts;
|
|
|
|
|
*receivedTimestamp = ts;
|
2018-12-14 17:43:48 -08:00
|
|
|
// The NetComm call placed in data, copy into the msg
|
2018-05-21 16:09:38 -07:00
|
|
|
std::memcpy(msg.data, data, dataSize);
|
|
|
|
|
} else {
|
2018-06-07 20:49:03 -07:00
|
|
|
auto i = can->receives.find(messageId);
|
2018-05-21 16:09:38 -07:00
|
|
|
if (i != can->receives.end()) {
|
|
|
|
|
// Found, check if new enough
|
2023-06-07 12:54:03 -04:00
|
|
|
uint32_t now = HAL_GetCANPacketBaseTime();
|
2018-12-29 13:57:23 -08:00
|
|
|
if (now - i->second.lastTimeStamp > static_cast<uint32_t>(timeoutMs)) {
|
2018-05-21 16:09:38 -07:00
|
|
|
// Timeout, return bad status
|
|
|
|
|
*status = HAL_CAN_TIMEOUT;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-14 17:43:48 -08:00
|
|
|
// Read the data from the stored message into the output
|
|
|
|
|
std::memcpy(data, i->second.data, i->second.length);
|
2018-05-21 16:09:38 -07:00
|
|
|
*length = i->second.length;
|
|
|
|
|
*receivedTimestamp = i->second.lastTimeStamp;
|
|
|
|
|
*status = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|