Skip to content

Commit

Permalink
CAN: rebase master
Browse files Browse the repository at this point in the history
  • Loading branch information
cindyli-13 committed May 26, 2021
1 parent 550d512 commit ad71c4d
Show file tree
Hide file tree
Showing 20 changed files with 403 additions and 193 deletions.
27 changes: 0 additions & 27 deletions apps/arm/CMakeLists.txt

This file was deleted.

25 changes: 0 additions & 25 deletions apps/gimbal/CMakeLists.txt

This file was deleted.

11 changes: 0 additions & 11 deletions apps/test-can/CMakeLists.txt

This file was deleted.

71 changes: 0 additions & 71 deletions apps/test-can/src/main_old.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions apps/test-stress-can/CMakeLists.txt

This file was deleted.

7 changes: 3 additions & 4 deletions libs/can/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ add_library(CANBus STATIC)
target_sources(CANBus PRIVATE src/CANBus.cpp)
target_include_directories(CANBus PUBLIC include)
target_link_libraries(CANBus
PUBLIC
mbed-os
PRIVATE
uwrt-mars-rover-hw-bridge
mbed-os
)
target_set_mbed_dependency(CANBus)

add_library(CANInterface STATIC)
target_sources(CANInterface PRIVATE src/CANInterface.cpp)
Expand All @@ -36,5 +34,6 @@ target_link_libraries(CANInterface
uwrt-mars-rover-hw-bridge
CANBus
CANMsg
mbed-os
mbed-events
)
target_set_mbed_dependency(CANInterface)
94 changes: 94 additions & 0 deletions libs/can/include/CANInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#pragma once

#include "CANBus.h"
#include "CANMsg.h"
#include "hw_bridge.h"
#include "mbed.h"

class CANInterface {
public:
typedef struct {
// CAN bus pins
PinName can1_RX;
PinName can1_TX;
PinName can2_RX;
PinName can2_TX;

// Message maps and handlers
HWBRIDGE::CANMsgMap *rxMsgMap;
HWBRIDGE::CANMsgMap *txMsgMap;
const CANMsg::CANMsgHandlerMap *rxOneShotMsgHandler;

// Bus frequency
uint32_t frequency_hz = HWBRIDGE::ROVER_CANBUS_FREQUENCY_HZ;
} Config;

// Initialize CAN interface
CANInterface(const Config &config);

// Queue up a one shot message to be sent
bool sendOneShotMessage(CANMsg &msg, Kernel::Clock::duration_u32 timeout);

// Update a TX CAN signal
bool setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue);

// Read a RX CAN signal
bool getRXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t &signalValue);

// Switch CAN bus
bool switchCANBus(HWBRIDGE::CANBUSID canBusID);

// Set CAN bus hw filter
bool setFilter(HWBRIDGE::CANFILTER filter, CANFormat format = CANStandard,
uint16_t mask = HWBRIDGE::ROVER_CANID_FILTER_MASK, int handle = 0);

// For diagnostic purposes
uint32_t getNumStreamedMsgsReceived(void);
uint32_t getNumOneShotMsgsReceived(void);
uint32_t getNumStreamedMsgsSent(void);
uint32_t getNumOneShotMsgsSent(void);

uint16_t getNumCANRXFaults(void);
uint16_t getNumCANTXFaults(void);

private:
static constexpr osPriority RX_POSTMAN_THREAD_PRIORITY = osPriorityRealtime;
static constexpr osPriority RX_CLIENT_THREAD_PRIORITY = osPriorityAboveNormal;
static constexpr osPriority TX_PROCESSOR_THREAD_PRIORITY = osPriorityBelowNormal;
static constexpr std::chrono::milliseconds TX_INTERDELAY = 1ms;
static constexpr std::chrono::milliseconds TX_PERIOD = 10ms;

void rxISR(void);
void rxPostman(void);
void rxClient(void);
void txProcessor(void);

CANBus m_CANBus1;
CANBus m_CANBus2;
CANBus *m_activeCANBus;

Thread m_rxPostmanThread;
Thread m_rxClientThread;
Thread m_txProcessorThread;

Mutex m_rxMutex;
Mutex m_txMutex;

Mail<CANMsg, 100> m_rxMailbox;
Mail<CANMsg, 32> m_txMailboxOneShot;
EventQueue m_rxEventQueue;

HWBRIDGE::CANMsgMap *m_rxMsgMap;
HWBRIDGE::CANMsgMap *m_txMsgMap;

const CANMsg::CANMsgHandlerMap *m_rxOneShotMsgHandler;

// For diagnostic purposes
uint32_t m_numStreamedMsgsReceived;
uint32_t m_numOneShotMsgsReceived;
uint32_t m_numStreamedMsgsSent;
uint32_t m_numOneShotMsgsSent;

uint16_t m_numCANRXFaults;
uint16_t m_numCANTXFaults;
};
Loading

0 comments on commit ad71c4d

Please sign in to comment.