Line data Source code
1 : /* 2 : * This file is free software: you can redistribute it and/or modify it 3 : * under the terms of the GNU General Public License as published by the 4 : * Free Software Foundation, either version 3 of the License, or 5 : * (at your option) any later version. 6 : * 7 : * This file is distributed in the hope that it will be useful, but 8 : * WITHOUT ANY WARRANTY; without even the implied warranty of 9 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 10 : * See the GNU General Public License for more details. 11 : * 12 : * You should have received a copy of the GNU General Public License along 13 : * with this program. If not, see <http://www.gnu.org/licenses/>. 14 : * 15 : * Author: Oliver Walters / Currawong Engineering Pty Ltd 16 : */ 17 : 18 : #include "AP_PiccoloCAN_config.h" 19 : 20 : #if AP_PICCOLOCAN_ENABLED 21 : 22 : #include "AP_PiccoloCAN_ECU.h" 23 : 24 : // Protocol files for the ECU 25 : #include <AP_PiccoloCAN/piccolo_protocol/ECUProtocol.h> 26 : #include <AP_PiccoloCAN/piccolo_protocol/ECUPackets.h> 27 : 28 : 29 : /* Piccolo Glue Logic 30 : * The following functions are required by the auto-generated protogen code. 31 : */ 32 : 33 : 34 : //! \return the packet data pointer from the packet 35 0 : uint8_t* getECUPacketData(void* pkt) 36 : { 37 0 : AP_HAL::CANFrame* frame = (AP_HAL::CANFrame*) pkt; 38 : 39 0 : return (uint8_t*) frame->data; 40 : } 41 : 42 : //! \return the packet data pointer from the packet, const 43 0 : const uint8_t* getECUPacketDataConst(const void* pkt) 44 : { 45 0 : AP_HAL::CANFrame* frame = (AP_HAL::CANFrame*) pkt; 46 : 47 0 : return (const uint8_t*) frame->data; 48 : } 49 : 50 : //! Complete a packet after the data have been encoded 51 0 : void finishECUPacket(void* pkt, int size, uint32_t packetID) 52 : { 53 0 : AP_HAL::CANFrame* frame = (AP_HAL::CANFrame*) pkt; 54 : 55 0 : if (size > AP_HAL::CANFrame::MaxDataLen) { 56 0 : size = AP_HAL::CANFrame::MaxDataLen; 57 : } 58 : 59 0 : frame->dlc = size; 60 : 61 : /* Encode the CAN ID 62 : * 0x09mmdddd 63 : * - 07 = ECU_IN (to and ECU) group ID 64 : * - mm = Message ID 65 : * - dd = Device ID 66 : * 67 : * Note: The Device ID (lower 16 bits of the frame ID) will have to be inserted later 68 : */ 69 : 70 0 : uint32_t id = (((uint8_t) PiccoloCAN_MessageGroup::ECU_IN) << 24) | // CAN Group ID 71 0 : ((packetID & 0xFF) << 16); // Message ID 72 : 73 : // Extended frame format 74 0 : id |= AP_HAL::CANFrame::FlagEFF; 75 : 76 0 : frame->id = id; 77 0 : } 78 : 79 : //! \return the size of a packet from the packet header 80 0 : int getECUPacketSize(const void* pkt) 81 : { 82 0 : AP_HAL::CANFrame* frame = (AP_HAL::CANFrame*) pkt; 83 : 84 0 : return (int) frame->dlc; 85 : } 86 : 87 : //! \return the ID of a packet from the packet header 88 0 : uint32_t getECUPacketID(const void* pkt) 89 : { 90 0 : AP_HAL::CANFrame* frame = (AP_HAL::CANFrame*) pkt; 91 : 92 : // Extract the message ID field from the 29-bit ID 93 0 : return (uint32_t) ((frame->id >> 16) & 0xFF); 94 : } 95 : 96 : #endif // AP_PICCOLOCAN_ENABLED