2022-05-09 21:19:06 +02:00

130 lines
2.5 KiB
C++

/**
* @file
* @brief Implementation of CanController using STM32 HAL.
*/
#ifndef STA_STM32_CAN_HPP
#define STA_STM32_CAN_HPP
/**
* @defgroup stm32CAN CAN
* @ingroup stm32
* @brief STM32 CAN module.
*
* Check @ref stm32BuildConfig for configuration options.
*/
#ifdef DOXYGEN
/**
* @def STA_STM32_CAN_ENABLE
* @brief Enable module.
*
* @ingroup stm32BuildConfig
*/
# define STA_STM32_CAN_ENABLE
/**
* @def STA_STM32_CAN_GLOBAL
* @brief Create global CanBus object using this CAN instance.
*
* @ingroup stm32BuildConfig
*/
# define STA_STM32_CAN_GLOBAL
#endif // DOXYGEN
#include <sta/config.hpp>
#ifdef STA_STM32_CAN_ENABLE
#include <sta/can_bus/controller.hpp>
#include <sta/stm32/hal.hpp>
namespace sta
{
/**
* @brief Implementation of CanController interface using HAL.
*
* @ingroup stm32CAN
*/
class STM32CanController : public CanController
{
public:
static constexpr uint8_t MAX_FILTER_COUNT = 14; /**< Max number of filters */
static constexpr uint8_t MAX_FIFO_COUNT = 2; /**< Max number of FIFOs */
static constexpr uint8_t MAX_PAYLOAD_SIZE = 8; /**< Maximum payload size */
public:
/**
* @param handle CAN handle
*/
STM32CanController(CAN_HandleTypeDef * handle);
/**
* @brief Enable RX pending interrupts.
*/
void enableRxInterrupts();
/**
* @brief Start CAN controller.
*/
void start();
/**
* @brief Stop CAN controller.
*/
void stop();
// RX/TX
//
bool sendFrame(const CanTxHeader & header, const uint8_t * payload) override;
bool receiveFrame(uint8_t fifo, CanRxHeader * header, uint8_t * payload) override;
uint32_t getRxFifoFlags() override;
// RX Filter
//
void configureFilter(uint8_t idx, const CanFilter & filter, bool active = false) override;
void enableFilter(uint8_t idx) override;
void disableFilter(uint8_t idx) override;
void clearFilters() override;
private:
/**
* @brief Initialize filter settings.
*/
void initFilters();
private:
CAN_HandleTypeDef * handle_; /**< CAN handle */
CAN_FilterTypeDef filters_[MAX_FILTER_COUNT]; /**< Filter settings */
};
#ifdef STA_STM32_CAN_GLOBAL
/**
* @brief Global CAN instance.
*
* @ingroup stm32CAN
*/
extern STM32CanController CanBus;
/**
* @brief Interrupt handler for pending RX frames.
*
* May be implemented by application.
*
* @ingroup stm32CAN
*/
void CanBus_RxPendingCallback();
#endif // STA_STM32_CAN_GLOBAL
} // namespace sta
#endif // STA_STM32_CAN_ENABLE
#endif // STA_STM32_CAN_HPP