Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork140
Add HardwareCAN - a abstract base class for implementing CAN interfaces across Arduino cores.#185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletionsapi/CanMsg.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * This file is free software; you can redistribute it and/or modify | ||
| * it under the terms of either the GNU General Public License version 2 | ||
| * or the GNU Lesser General Public License version 2.1, both as | ||
| * published by the Free Software Foundation. | ||
| */ | ||
| #ifndef ARDUINOCORE_API_CAN_MSG_H_ | ||
| #define ARDUINOCORE_API_CAN_MSG_H_ | ||
| /************************************************************************************** | ||
| * INCLUDE | ||
| **************************************************************************************/ | ||
| #include <cstdlib> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <Arduino.h> | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| namespace arduino | ||
| { | ||
| /************************************************************************************** | ||
| * CLASS DECLARATION | ||
| **************************************************************************************/ | ||
| class CanMsg : public Printable | ||
| { | ||
| public: | ||
| static size_t constexpr MAX_DATA_LENGTH = 8; | ||
| CanMsg(uint32_t const can_id, uint8_t const can_data_len, uint8_t const * can_data_ptr) | ||
| : id{can_id} | ||
| , data_length{can_data_len} | ||
| , data{0} | ||
| { | ||
| memcpy(data, can_data_ptr, min(can_data_len, MAX_DATA_LENGTH)); | ||
| } | ||
| CanMsg() : CanMsg(0, 0, nullptr) { } | ||
| CanMsg(CanMsg const & other) | ||
| { | ||
| this->id = other.id; | ||
| this->data_length = other.data_length; | ||
| memcpy(this->data, other.data, this->data_length); | ||
| } | ||
| virtual ~CanMsg() { } | ||
| void operator = (CanMsg const & other) | ||
| { | ||
| if (this == &other) | ||
| return; | ||
| this->id = other.id; | ||
| this->data_length = other.data_length; | ||
| memcpy(this->data, other.data, this->data_length); | ||
| } | ||
| virtual size_t printTo(Print & p) const override | ||
| { | ||
| char buf[20] = {0}; | ||
| size_t len = 0; | ||
| /* Print the header. */ | ||
| len = snprintf(buf, sizeof(buf), "[%08X] (%d) : ", id, data_length); | ||
| size_t n = p.write(buf, len); | ||
| /* Print the data. */ | ||
| for (size_t d = 0; d < data_length; d++) | ||
| { | ||
| len = snprintf(buf, sizeof(buf), "%02X", data[d]); | ||
| n += p.write(buf, len); | ||
| } | ||
| /* Wrap up. */ | ||
| return n; | ||
| } | ||
| uint32_t id; | ||
| uint8_t data_length; | ||
| uint8_t data[MAX_DATA_LENGTH]; | ||
| }; | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| } /* arduino */ | ||
| #endif /* ARDUINOCORE_API_CAN_MSG_H_ */ |
62 changes: 62 additions & 0 deletionsapi/CanMsgRingbuffer.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * This file is free software; you can redistribute it and/or modify | ||
| * it under the terms of either the GNU General Public License version 2 | ||
| * or the GNU Lesser General Public License version 2.1, both as | ||
| * published by the Free Software Foundation. | ||
| */ | ||
| /************************************************************************************** | ||
| * INCLUDE | ||
| **************************************************************************************/ | ||
| #include "CanMsgRingbuffer.h" | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| namespace arduino | ||
| { | ||
| /************************************************************************************** | ||
| * CTOR/DTOR | ||
| **************************************************************************************/ | ||
| CanMsgRingbuffer::CanMsgRingbuffer() | ||
| : _head{0} | ||
| , _tail{0} | ||
| , _num_elems{0} | ||
| { | ||
| } | ||
| /************************************************************************************** | ||
| * PUBLIC MEMBER FUNCTIONS | ||
| **************************************************************************************/ | ||
| void CanMsgRingbuffer::enqueue(CanMsg const & msg) | ||
| { | ||
| if (isFull()) | ||
| return; | ||
| _buf[_head] = msg; | ||
| _head = next(_head); | ||
| _num_elems++; | ||
| } | ||
| CanMsg CanMsgRingbuffer::dequeue() | ||
| { | ||
| if (isEmpty()) | ||
| return CanMsg(); | ||
| CanMsg const msg = _buf[_tail]; | ||
| _tail = next(_tail); | ||
| _num_elems--; | ||
| return msg; | ||
| } | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| } /* arduino */ |
60 changes: 60 additions & 0 deletionsapi/CanMsgRingbuffer.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * This file is free software; you can redistribute it and/or modify | ||
| * it under the terms of either the GNU General Public License version 2 | ||
| * or the GNU Lesser General Public License version 2.1, both as | ||
| * published by the Free Software Foundation. | ||
| */ | ||
| #ifndef ARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_ | ||
| #defineARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_ | ||
| /************************************************************************************** | ||
| * INCLUDE | ||
| **************************************************************************************/ | ||
| #include<cstdint> | ||
| #include"CanMsg.h" | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| namespacearduino | ||
| { | ||
| /************************************************************************************** | ||
| * CLASS DECLARATION | ||
| **************************************************************************************/ | ||
| classCanMsgRingbuffer | ||
| { | ||
| public: | ||
| staticsize_tconstexpr RING_BUFFER_SIZE =32U; | ||
| CanMsgRingbuffer(); | ||
| inlineboolisFull()const {return (_num_elems == RING_BUFFER_SIZE); } | ||
| voidenqueue(CanMsgconst & msg); | ||
| inlineboolisEmpty()const {return (_num_elems ==0); } | ||
| CanMsgdequeue(); | ||
| inlinesize_tavailable()const {return _num_elems; } | ||
| private: | ||
| CanMsg _buf[RING_BUFFER_SIZE]; | ||
| volatilesize_t _head; | ||
| volatilesize_t _tail; | ||
| volatilesize_t _num_elems; | ||
| inlinesize_tnext(size_tconst idx)const {return ((idx +1) % RING_BUFFER_SIZE); } | ||
| }; | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| }/* arduino*/ | ||
| #endif/* ARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_*/ |
62 changes: 62 additions & 0 deletionsapi/HardwareCAN.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * This file is free software; you can redistribute it and/or modify | ||
| * it under the terms of either the GNU General Public License version 2 | ||
| * or the GNU Lesser General Public License version 2.1, both as | ||
| * published by the Free Software Foundation. | ||
| */ | ||
| #ifndef ARDUINOCORE_API_HARDWARECAN_H | ||
| #define ARDUINOCORE_API_HARDWARECAN_H | ||
| /************************************************************************************** | ||
| * INCLUDE | ||
| **************************************************************************************/ | ||
| #include "CanMsg.h" | ||
| #include "CanMsgRingbuffer.h" | ||
| /************************************************************************************** | ||
| * TYPEDEF | ||
| **************************************************************************************/ | ||
| enum class CanBitRate : int | ||
| { | ||
| BR_125k = 125000, | ||
| BR_250k = 250000, | ||
| BR_500k = 500000, | ||
| BR_1000k = 1000000, | ||
| }; | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| namespace arduino | ||
| { | ||
| /************************************************************************************** | ||
| * CLASS DECLARATION | ||
| **************************************************************************************/ | ||
| class HardwareCAN | ||
| { | ||
| public: | ||
| virtual ~HardwareCAN() {} | ||
| virtual bool begin(CanBitRate const can_bitrate) = 0; | ||
| virtual void end() = 0; | ||
| virtual int write(CanMsg const &msg) = 0; | ||
| virtual size_t available() = 0; | ||
| virtual CanMsg read() = 0; | ||
| }; | ||
| /************************************************************************************** | ||
| * NAMESPACE | ||
| **************************************************************************************/ | ||
| } /* arduino */ | ||
| #endif /* ARDUINOCORE_API_HARDWARECAN_H */ |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.