2024-01-30 14:00:43 +01:00

127 lines
3.1 KiB
C++

/**
* @file
* @brief Implementation of CanController using STM32 HAL.
*
* Configuration:
* * STA_STM32_CAN_GLOBAL: Create global CanBus object using this CAN instance
*/
#ifndef STA_CORE_STM32_CAN_HPP
#define STA_CORE_STM32_CAN_HPP
/**
* @defgroup sta_core_stm32_can CAN
* @ingroup sta_core_stm32
* @brief STM32 CAN module.
*/
// Only enable module on STM32 platform w/ HAL CAN module enabled
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/devices/stm32/hal.hpp>
# ifdef HAL_CAN_MODULE_ENABLED
# define STA_STM32_CAN_ENABLED
# endif // HAL_CAN_MODULE_ENABLED
#endif // STA_PLATFORM_STM32
#if defined(STA_STM32_CAN_ENABLED) || defined(DOXYGEN)
#include <sta/bus/can/controller.hpp>
namespace sta
{
/**
* @brief Implementation of CanController interface using HAL.
*
* @ingroup sta_core_stm32_can
*/
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;
// Return pending RX FIFOs as iterable container
CanPendingRxFifos getPendingRxFifos() override;
// Const getters
uint8_t maxFilterCount() const override;
uint8_t maxFifoCount() const override;
uint8_t maxPayloadSize() const override;
private:
/**
* @brief Initialize filter settings.
*/
void initFilters();
private:
CAN_HandleTypeDef * handle_; /**< CAN handle */
CAN_FilterTypeDef filters_[MAX_FILTER_COUNT]; /**< Filter settings */
};
#if defined(STA_STM32_CAN_GLOBAL) || DOXYGEN
/**
* @brief Global CAN instance.
*
* @ingroup sta_core_stm32_can
*/
extern STM32CanController CanBus;
/**
* @brief Interrupt handler for pending RX frames.
*
* May be implemented by application.
*
* @ingroup sta_core_stm32_can
*/
void CanBus_RxPendingCallback();
#endif // STA_STM32_CAN_GLOBAL
} // namespace sta
#endif // STA_STM32_CAN_ENABLED
#endif // STA_CORE_STM32_CAN_HPP