-
Notifications
You must be signed in to change notification settings - Fork 59
CANModule to fit CAN into app restructure #407
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
b489be4
20010f1
1a488c7
4de2cff
3f052b1
d1c8689
5de8de9
2c23625
541583d
6947dd6
a667ffa
a0d658b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma onceusing | ||
|
|
||
| #include "Module.h" | ||
| #include "mbed.h" | ||
| #include "CANInterface.h" | ||
|
|
||
| class CANModule final : public Module { | ||
| public: | ||
|
|
||
| CANModule(); | ||
|
|
||
| void periodic_1ms(void) override {} | ||
| void periodic_10ms(void) override {} | ||
|
|
||
|
|
||
| private: | ||
| CANInterface | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,8 +59,8 @@ void CANInterface::rxClient(void) { | |
| // Wait for a message to arrive | ||
| do { | ||
| mail = m_rxMailbox.try_get(); // using try_get() because try_get_for() was crashing | ||
| ThisThread::sleep_for(1ms); | ||
| } while (mail == nullptr); | ||
| ThisThread::sleep_for(1ms); // TODO: Check for object in mailbox every ms | ||
| } while (mail == nullptr); | ||
|
|
||
| MBED_ASSERT(mail != nullptr); | ||
|
|
||
|
|
@@ -112,51 +112,47 @@ void CANInterface::rxClient(void) { | |
| } | ||
|
|
||
| void CANInterface::txProcessor(void) { | ||
| while (true) { | ||
| auto startTime = Kernel::Clock::now(); | ||
| auto startTime = Kernel::Clock::now(); | ||
|
||
|
|
||
| CANMsg *mail = nullptr; | ||
| CANMsg *mail = nullptr; | ||
|
|
||
| // Send all one-shot messages that were queued | ||
| while ((mail = m_txMailboxOneShot.try_get()) != nullptr) { | ||
| if (!m_activeCANBus->write(*mail)) { | ||
| MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED), "CAN TX write failed"); | ||
| m_numCANTXFaults++; | ||
| } | ||
| MBED_ASSERT(m_txMailboxOneShot.free(mail) == osOK); | ||
| ThisThread::sleep_for(TX_INTERDELAY); | ||
| // Send all one-shot messages that were queued | ||
| while ((mail = m_txMailboxOneShot.try_get()) != nullptr) { | ||
| if (!m_activeCANBus->write(*mail)) { | ||
| MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED), "CAN TX write failed"); | ||
| m_numCANTXFaults++; | ||
| } | ||
| MBED_ASSERT(m_txMailboxOneShot.free(mail) == osOK); | ||
| ThisThread::sleep_for(TX_INTERDELAY); | ||
|
||
| } | ||
|
|
||
| // Send all streamed messages | ||
| if (m_txMsgMap != nullptr) { | ||
| for (auto it = m_txMsgMap->begin(); it != m_txMsgMap->end(); it++) { | ||
| HWBRIDGE::CANID msgID = it->first; | ||
| HWBRIDGE::CANMsgData_t msgData = {0}; | ||
| size_t len = 0; | ||
|
|
||
| m_txMutex.lock(); | ||
| bool msgPacked = HWBRIDGE::packCANMsg(msgData.raw, msgID, m_txMsgMap, len); | ||
| m_txMutex.unlock(); | ||
|
|
||
| if (msgPacked) { | ||
| // Send message | ||
| CANMsg msg; | ||
| msg.setID(msgID); | ||
| msg.setPayload(msgData, len); | ||
| m_activeCANBus->write(msg); | ||
|
|
||
| m_numStreamedMsgsSent++; | ||
|
|
||
| ThisThread::sleep_for(TX_INTERDELAY); | ||
| } else { | ||
| MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_INVALID_DATA_DETECTED), | ||
| "CAN TX message packing failed"); | ||
| m_numCANTXFaults++; | ||
| } | ||
| // Send all streamed messages | ||
| if (m_txMsgMap != nullptr) { | ||
| for (auto it = m_txMsgMap->begin(); it != m_txMsgMap->end(); it++) { | ||
| HWBRIDGE::CANID msgID = it->first; | ||
| HWBRIDGE::CANMsgData_t msgData = {0}; | ||
| size_t len = 0; | ||
|
|
||
| m_txMutex.lock(); | ||
| bool msgPacked = HWBRIDGE::packCANMsg(msgData.raw, msgID, m_txMsgMap, len); | ||
| m_txMutex.unlock(); | ||
|
|
||
| if (msgPacked) { | ||
| // Send message | ||
| CANMsg msg; | ||
| msg.setID(msgID); | ||
| msg.setPayload(msgData, len); | ||
| m_activeCANBus->write(msg); | ||
|
|
||
| m_numStreamedMsgsSent++; | ||
|
|
||
| ThisThread::sleep_for(TX_INTERDELAY); | ||
|
||
| } else { | ||
| MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_INVALID_DATA_DETECTED), | ||
| "CAN TX message packing failed"); | ||
| m_numCANTXFaults++; | ||
| } | ||
| } | ||
|
|
||
| ThisThread::sleep_until(startTime + TX_PERIOD); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #pragma onceusing | ||
|
|
||
| #include "CANInterface.h" | ||
| #include "Module.h" | ||
| #include "mbed.h" | ||
|
|
||
| class CANModule final : public Module { | ||
| public: | ||
| CANModule(const Config &config); | ||
|
|
||
| void periodic_1ms(void) override {} | ||
| void periodic_10ms(void) override {} | ||
|
|
||
| private: | ||
| CANInterface interface; | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include "CANModule.h" | ||
|
|
||
| #include "Module.h" | ||
| #include "mbed.h" | ||
|
|
||
| // Using initializer lists to construct CANInterface object will attach ISRs and other required tasks | ||
| CANModule::CANModule(const Config &config) : interface(config) { | ||
| // Nothing should be required here | ||
| } | ||
|
|
||
| void CANModule::periodic_1ms(void) { | ||
|
|
||
| // These functions need to be called at 1 kHz, or every 1ms | ||
| interface.rxClient(); | ||
|
||
| } | ||
|
|
||
| void CANModule::periodic_10ms(void) { | ||
|
||
|
|
||
| // These functions need to be called every 100 Hz (10ms) | ||
| interface.txProcessor(); | ||
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the above logic change on lines 58 and 59, but now it results in try_get sometimes returning nullptr if the mailbox is empty. should change this assert to maybe a debug print and early return(or just an if block to block execution of rest of this function. personally, i prefer single return statements from functions in fw code)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the MBED_ASSERT not account for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MBED_ASSERT would actually break the program, I agree that we should remove it and just wrap the logic in an if block