[hal] Expose CAN timestamp base clock (#5357)

This commit is contained in:
Ryan Blue
2023-06-07 12:54:03 -04:00
committed by GitHub
parent 91cbcea841
commit 1c00a52b67
9 changed files with 81 additions and 28 deletions

View File

@@ -35,15 +35,6 @@ struct CANStorage {
static UnlimitedHandleResource<HAL_CANHandle, CANStorage, HAL_HandleEnum::CAN>*
canHandles;
static uint32_t GetPacketBaseTime() {
timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
// Convert t to milliseconds
uint64_t ms = t.tv_sec * 1000ull + t.tv_nsec / 1000000ull;
return ms & 0xFFFFFFFF;
}
namespace hal::init {
void InitializeCANAPI() {
static UnlimitedHandleResource<HAL_CANHandle, CANStorage, HAL_HandleEnum::CAN>
@@ -63,6 +54,15 @@ static int32_t CreateCANId(CANStorage* storage, int32_t apiId) {
extern "C" {
uint32_t HAL_GetCANPacketBaseTime(void) {
timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
// Convert t to milliseconds
uint64_t ms = t.tv_sec * 1000ull + t.tv_nsec / 1000000ull;
return ms & 0xFFFFFFFF;
}
HAL_CANHandle HAL_InitializeCAN(HAL_CANManufacturer manufacturer,
int32_t deviceId, HAL_CANDeviceType deviceType,
int32_t* status) {
@@ -267,7 +267,7 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId,
auto i = can->receives.find(messageId);
if (i != can->receives.end()) {
// Found, check if new enough
uint32_t now = GetPacketBaseTime();
uint32_t now = HAL_GetCANPacketBaseTime();
if (now - i->second.lastTimeStamp > static_cast<uint32_t>(timeoutMs)) {
// Timeout, return bad status
*status = HAL_CAN_TIMEOUT;