Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/CAN.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <hal/CAN.h> |
| 10 | #include <hal/CANAPI.h> |
| 11 | #include <hal/Errors.h> |
| 12 | #include <hal/FRCUsageReporting.h> |
| 13 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 14 | #include "frc/Errors.h" |
| 15 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | using namespace frc; |
| 17 | |
| 18 | CAN::CAN(int deviceId) { |
| 19 | int32_t status = 0; |
| 20 | m_handle = |
| 21 | HAL_InitializeCAN(kTeamManufacturer, deviceId, kTeamDeviceType, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 22 | FRC_CheckErrorStatus(status, "device id {}", deviceId); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | |
| 24 | HAL_Report(HALUsageReporting::kResourceType_CAN, deviceId + 1); |
| 25 | } |
| 26 | |
| 27 | CAN::CAN(int deviceId, int deviceManufacturer, int deviceType) { |
| 28 | int32_t status = 0; |
| 29 | m_handle = HAL_InitializeCAN( |
| 30 | static_cast<HAL_CANManufacturer>(deviceManufacturer), deviceId, |
| 31 | static_cast<HAL_CANDeviceType>(deviceType), &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | FRC_CheckErrorStatus(status, "device id {} mfg {} type {}", deviceId, |
| 33 | deviceManufacturer, deviceType); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | |
| 35 | HAL_Report(HALUsageReporting::kResourceType_CAN, deviceId + 1); |
| 36 | } |
| 37 | |
| 38 | CAN::~CAN() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | if (m_handle != HAL_kInvalidHandle) { |
| 40 | HAL_CleanCAN(m_handle); |
| 41 | m_handle = HAL_kInvalidHandle; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void CAN::WritePacket(const uint8_t* data, int length, int apiId) { |
| 46 | int32_t status = 0; |
| 47 | HAL_WriteCANPacket(m_handle, data, length, apiId, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 48 | FRC_CheckErrorStatus(status, "{}", "WritePacket"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void CAN::WritePacketRepeating(const uint8_t* data, int length, int apiId, |
| 52 | int repeatMs) { |
| 53 | int32_t status = 0; |
| 54 | HAL_WriteCANPacketRepeating(m_handle, data, length, apiId, repeatMs, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 55 | FRC_CheckErrorStatus(status, "{}", "WritePacketRepeating"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void CAN::WriteRTRFrame(int length, int apiId) { |
| 59 | int32_t status = 0; |
| 60 | HAL_WriteCANRTRFrame(m_handle, length, apiId, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 61 | FRC_CheckErrorStatus(status, "{}", "WriteRTRFrame"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 64 | int CAN::WritePacketNoError(const uint8_t* data, int length, int apiId) { |
| 65 | int32_t status = 0; |
| 66 | HAL_WriteCANPacket(m_handle, data, length, apiId, &status); |
| 67 | return status; |
| 68 | } |
| 69 | |
| 70 | int CAN::WritePacketRepeatingNoError(const uint8_t* data, int length, int apiId, |
| 71 | int repeatMs) { |
| 72 | int32_t status = 0; |
| 73 | HAL_WriteCANPacketRepeating(m_handle, data, length, apiId, repeatMs, &status); |
| 74 | return status; |
| 75 | } |
| 76 | |
| 77 | int CAN::WriteRTRFrameNoError(int length, int apiId) { |
| 78 | int32_t status = 0; |
| 79 | HAL_WriteCANRTRFrame(m_handle, length, apiId, &status); |
| 80 | return status; |
| 81 | } |
| 82 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 83 | void CAN::StopPacketRepeating(int apiId) { |
| 84 | int32_t status = 0; |
| 85 | HAL_StopCANPacketRepeating(m_handle, apiId, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 86 | FRC_CheckErrorStatus(status, "{}", "StopPacketRepeating"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | bool CAN::ReadPacketNew(int apiId, CANData* data) { |
| 90 | int32_t status = 0; |
| 91 | HAL_ReadCANPacketNew(m_handle, apiId, data->data, &data->length, |
| 92 | &data->timestamp, &status); |
| 93 | if (status == HAL_ERR_CANSessionMux_MessageNotFound) { |
| 94 | return false; |
| 95 | } |
| 96 | if (status != 0) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 97 | FRC_CheckErrorStatus(status, "{}", "ReadPacketNew"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 98 | return false; |
| 99 | } else { |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | bool CAN::ReadPacketLatest(int apiId, CANData* data) { |
| 105 | int32_t status = 0; |
| 106 | HAL_ReadCANPacketLatest(m_handle, apiId, data->data, &data->length, |
| 107 | &data->timestamp, &status); |
| 108 | if (status == HAL_ERR_CANSessionMux_MessageNotFound) { |
| 109 | return false; |
| 110 | } |
| 111 | if (status != 0) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 112 | FRC_CheckErrorStatus(status, "{}", "ReadPacketLatest"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 113 | return false; |
| 114 | } else { |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | bool CAN::ReadPacketTimeout(int apiId, int timeoutMs, CANData* data) { |
| 120 | int32_t status = 0; |
| 121 | HAL_ReadCANPacketTimeout(m_handle, apiId, data->data, &data->length, |
| 122 | &data->timestamp, timeoutMs, &status); |
| 123 | if (status == HAL_CAN_TIMEOUT || |
| 124 | status == HAL_ERR_CANSessionMux_MessageNotFound) { |
| 125 | return false; |
| 126 | } |
| 127 | if (status != 0) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 128 | FRC_CheckErrorStatus(status, "{}", "ReadPacketTimeout"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 129 | return false; |
| 130 | } else { |
| 131 | return true; |
| 132 | } |
| 133 | } |