semf
semf::Stm32Can Class Reference

Class for using CAN with Stm32. More...

#include <stm32can.h>

Inheritance diagram for semf::Stm32Can:
Inheritance graph
Collaboration diagram for semf::Stm32Can:
Collaboration graph

Public Types

enum class  ErrorCode : uint8_t {
  Init_HalError = 0 , Init_HalBusy , Init_HalTimeout , Deinit_HalError ,
  Deinit_HalBusy , Deinit_HalTimeout , SetFilter_IndexOutOfBounds , SetFilter_MessageIdInvalid ,
  SetFilter_MessageIdMaskInvalid , EnterDeepPowerDownMode_SleepStateReadyListeningHalError , EnterDeepPowerDownMode_SleepStateReadyListeningHalBusy , EnterDeepPowerDownMode_SleepStateReadyListeningHalTimeout ,
  EnterDeepPowerDownMode_InitStateResetSleepPendingHalError , EnterDeepPowerDownMode_InitStateResetSleepPendingHalBusy , EnterDeepPowerDownMode_InitStateResetSleepPendingHalTimeout , EnterDeepPowerDownMode_StartStateResetSleepPendingHalError ,
  EnterDeepPowerDownMode_StartStateResetSleepPendingHalBusy , EnterDeepPowerDownMode_StartStateResetSleepPendingHalTimeout , EnterDeepPowerDownMode_SleepStateResetSleepPendingHalError , EnterDeepPowerDownMode_SleepStateResetSleepPendingHalBusy ,
  EnterDeepPowerDownMode_SleepStateResetSleepPendingHalTimeout , ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalError , ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalBusy , ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalTimeout ,
  ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalError , ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalBusy , ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalTimeout , ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalError ,
  ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalBusy , ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalTimeout , ReleaseDeepPowerDownMode_StartStateError , ReleaseDeepPowerDownMode_StartStateInvalid ,
  IsrRead_NullpointerReadBuffer , IsrRead_FifoIdInvalid , IsrRead_HalError , IsrRead_HalBusy ,
  IsrRead_HalTimeout , IsrError_Error , WriteHardware_InvalidMessageId , WriteHardware_HalError ,
  WriteHardware_HalBusy , WriteHardware_HalTimeout , WriteHardware_NotStarted , RequestHardware_InvalidMessageId ,
  RequestHardware_HalError , RequestHardware_HalBusy , RequestHardware_HalTimeout , RequestHardware_NotStarted ,
  Start_HalTimeout , Start_HalNotInitialized , Stop_HalTimeout
}
 
- Public Types inherited from semf::CanHardware
enum class  ErrorCode : uint8_t {
  Write_IsBusy = 0 , Write_DataIsNullptr , Write_DataSizeIsZero , Read_DataIsNullptr ,
  Read_DataSizeIsZero , Request_IsBusy
}
 Error codes for this class. Error ID identify a unique error() / onError call (excluding transferring). More...
 
- Public Types inherited from semf::CommunicationHardware
enum class  Type { SynchronousMaster = 1 , SynchronousSlave , Asynchronous }
 
enum class  Frame : uint8_t { First = 0x01 , Next = 0x08 , Last = 0x10 , FirstAndLast = 0x11 }
 

Public Member Functions

 Stm32Can (CAN_HandleTypeDef &hwHandle)
 Constructor. More...
 
 Stm32Can (const Stm32Can &other)=delete
 
virtual ~Stm32Can ()=default
 
void init () override
 
void deinit () override
 
void setReadBuffer (uint8_t buffer[], size_t bufferSize) override
 Sets the read buffer for having the possibility to handle the received data. More...
 
void stopWrite () override
 
void stopRead () override
 
void start ()
 Hardware driver will be started. More...
 
void stop ()
 Hardware driver will be stopped. More...
 
bool isStarted () const
 Get the state of the start of the CAN driver. More...
 
uint32_t messageId () const override
 Returns the message ID for reading data. Use this function after reading the data via read-function. More...
 
void setMessageId (uint32_t id) override
 Sets the message ID for writing data. Use this function before writing the data via write-function. More...
 
void setFrequency (uint32_t hz) override
 Sets the speed. More...
 
void setFilter (uint32_t filterBank, uint32_t messageId, uint32_t messageIdMask) override
 Sets a message hardware receive filter. More...
 
void enterDeepPowerDownMode ()
 Go to deep power down mode. More...
 
void releaseDeepPowerDownMode ()
 Go to active mode. More...
 
void isrRead (CAN_HandleTypeDef &can, uint8_t fifoId)
 Saves a received message and sends a readyRead or readyRequested signal. More...
 
void isrWritten (CAN_HandleTypeDef &can, uint8_t mailboxId)
 Sending a readyWritten signal. More...
 
void isrError (CAN_HandleTypeDef &can)
 Sending a error signal. More...
 
- Public Member Functions inherited from semf::CanHardware
virtual ~CanHardware ()=default
 
void write (const uint8_t data[], size_t dataSize) override
 For writing data, dataWritten signal will be emitted after successful write. More...
 
void read (uint8_t buffer[], size_t bufferSize) override
 For setting the read buffer. This has be done for getting a dataAvailable signal for handling the data. More...
 
bool isBusyReading () const override
 Communication hardware is busy reading at the moment. More...
 
bool isBusyWriting () const override
 Communication hardware is busy writing at the moment. More...
 
void request () override
 
- Public Member Functions inherited from semf::Can
virtual ~Can ()=default
 
virtual void request ()=0
 
virtual uint32_t messageId () const =0
 Returns the message ID for reading data. Use this function after reading the data via read-function. More...
 
virtual void setMessageId (uint32_t id)=0
 Sets the message ID for writing data. Use this function before writing the data via write-function. More...
 
virtual void setFrequency (uint32_t hz)=0
 Sets the speed. More...
 
virtual void setFilter (uint32_t filterBank, uint32_t messageId, uint32_t messageIdMask)=0
 Sets a message hardware receive filter. More...
 
- Public Member Functions inherited from semf::CommunicationHardwareAsynchronous
virtual ~CommunicationHardwareAsynchronous ()=default
 
Type type () const override
 Returns the hardware type. More...
 
void setFrame (Frame frame) final
 Sets the selected usage of start and stop condition. More...
 
- Public Member Functions inherited from semf::CommunicationHardware
virtual ~CommunicationHardware ()=default
 
virtual void init ()=0
 
virtual void deinit ()=0
 
virtual Type type () const =0
 Returns the hardware type. More...
 
virtual void setFrame (Frame frame)=0
 Sets the selected usage of start and stop condition. More...
 
- Public Member Functions inherited from semf::app::Communication
virtual ~Communication ()=default
 
virtual void write (const uint8_t data[], size_t dataSize)=0
 For writing data, dataWritten signal will be emitted after successful write. More...
 
virtual void read (uint8_t buffer[], size_t bufferSize)=0
 For reading data, dataAvailable signal will be emitted after successful read. More...
 
virtual void stopRead ()=0
 
virtual void stopWrite ()=0
 
virtual bool isBusyReading () const =0
 Communication hardware is busy reading at the moment. More...
 
virtual bool isBusyWriting () const =0
 Communication hardware is busy writing at the moment. More...
 
- Public Member Functions inherited from semf::LinkedQueue< T >::Node
virtual ~Node ()=default
 
T * next () const
 Returns a pointer to the next element in a queue. More...
 
void setNext (T *next)
 Sets a pointer to the next element in a list. More...
 
bool isInQueue (LinkedQueue &queue)
 Returns if a node is part of this LinkedQueue. More...
 

Static Public Member Functions

static LinkedQueue< Stm32Can > * queue ()
 Get the list with all cans. More...
 
static void systemIsrRead (CAN_HandleTypeDef &can, uint8_t fifoId)
 System-wide interrupt service routine for can receive. More...
 
static void systemIsrWritten (CAN_HandleTypeDef &can, uint8_t mailboxId)
 System-wide interrupt service routine for can transmit. More...
 
static void systemIsrError (CAN_HandleTypeDef &can)
 System-wide interrupt service routine for can error. More...
 

Protected Member Functions

void writeHardware (const uint8_t data[], size_t dataSize) override
 Hardware will write data. More...
 
void requestHardware () override
 
- Protected Member Functions inherited from semf::CanHardware
void setBusyWriting (bool isBusy)
 Sets the busy flag for writing. More...
 
virtual void setReadBuffer (uint8_t buffer[], size_t bufferSize)=0
 Sets the read buffer for having the possibility to handle the received data. More...
 
virtual void writeHardware (const uint8_t data[], size_t dataSize)=0
 Hardware will write data. More...
 
virtual void requestHardware ()=0
 
void onDataWritten ()
 
void onDataAvailable ()
 
void onError (Error thrown)
 Is called if an error occurred by hardware read or write access. Will emit error signal. More...
 
void onDataRequested ()
 

Additional Inherited Members

- Public Attributes inherited from semf::Can
Signal dataRequested
 
- Public Attributes inherited from semf::app::Communication
Signal dataWritten
 
Signal dataAvailable
 
Signal writeStopped
 
Signal readStopped
 
Signal< Errorerror
 

Detailed Description

Class for using CAN with Stm32.

Attention
For using the driver you need to call start().
The implementation is for single CAN periphery usage.

Definition at line 25 of file stm32can.h.

Member Enumeration Documentation

◆ ErrorCode

enum class semf::Stm32Can::ErrorCode : uint8_t
strong

Error codes for this class. Error ID identify a unique error() / onError call (excluding transferring).

Enumerator
Init_HalError 
Init_HalBusy 
Init_HalTimeout 
Deinit_HalError 
Deinit_HalBusy 
Deinit_HalTimeout 
SetFilter_IndexOutOfBounds 
SetFilter_MessageIdInvalid 
SetFilter_MessageIdMaskInvalid 
EnterDeepPowerDownMode_SleepStateReadyListeningHalError 
EnterDeepPowerDownMode_SleepStateReadyListeningHalBusy 
EnterDeepPowerDownMode_SleepStateReadyListeningHalTimeout 
EnterDeepPowerDownMode_InitStateResetSleepPendingHalError 
EnterDeepPowerDownMode_InitStateResetSleepPendingHalBusy 
EnterDeepPowerDownMode_InitStateResetSleepPendingHalTimeout 
EnterDeepPowerDownMode_StartStateResetSleepPendingHalError 
EnterDeepPowerDownMode_StartStateResetSleepPendingHalBusy 
EnterDeepPowerDownMode_StartStateResetSleepPendingHalTimeout 
EnterDeepPowerDownMode_SleepStateResetSleepPendingHalError 
EnterDeepPowerDownMode_SleepStateResetSleepPendingHalBusy 
EnterDeepPowerDownMode_SleepStateResetSleepPendingHalTimeout 
ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalError 
ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalBusy 
ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalTimeout 
ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalError 
ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalBusy 
ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalTimeout 
ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalError 
ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalBusy 
ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalTimeout 
ReleaseDeepPowerDownMode_StartStateError 
ReleaseDeepPowerDownMode_StartStateInvalid 
IsrRead_NullpointerReadBuffer 
IsrRead_FifoIdInvalid 
IsrRead_HalError 
IsrRead_HalBusy 
IsrRead_HalTimeout 
IsrError_Error 
WriteHardware_InvalidMessageId 
WriteHardware_HalError 
WriteHardware_HalBusy 
WriteHardware_HalTimeout 
WriteHardware_NotStarted 
RequestHardware_InvalidMessageId 
RequestHardware_HalError 
RequestHardware_HalBusy 
RequestHardware_HalTimeout 
RequestHardware_NotStarted 
Start_HalTimeout 
Start_HalNotInitialized 
Stop_HalTimeout 

Definition at line 29 of file stm32can.h.

Constructor & Destructor Documentation

◆ Stm32Can() [1/2]

semf::Stm32Can::Stm32Can ( CAN_HandleTypeDef &  hwHandle)
explicit

Constructor.

Parameters
hwHandlecan hardware interface

Definition at line 44 of file stm32can.cpp.

Here is the call graph for this function:

◆ Stm32Can() [2/2]

semf::Stm32Can::Stm32Can ( const Stm32Can other)
explicitdelete

◆ ~Stm32Can()

virtual semf::Stm32Can::~Stm32Can ( )
virtualdefault

Member Function Documentation

◆ deinit()

void semf::Stm32Can::deinit ( )
overridevirtual

Deinitializes the hardware

Exceptions
Deinit_HalErrorIf the ST-HAL stumbles upon an error.
Deinit_HalBusyIf the ST-HAL is busy.
Deinit_HalTimeoutIf the ST-HAL times out.

Implements semf::CommunicationHardware.

Definition at line 75 of file stm32can.cpp.

Here is the call graph for this function:

◆ enterDeepPowerDownMode()

void semf::Stm32Can::enterDeepPowerDownMode ( )

Go to deep power down mode.

Exceptions
EnterDeepPowerDownMode_SleepStateReadyListeningHalErrorIf sleep returns a hal error.
EnterDeepPowerDownMode_SleepStateReadyListeningHalBusyIf sleep returns a hal busy.
EnterDeepPowerDownMode_SleepStateReadyListeningHalTimeoutIf sleep returns a hal timeout.
EnterDeepPowerDownMode_InitStateResetSleepPendingHalErrorIf init returns a hal error.
Enterdeeppowerdownmode_InitstateresetsleeppendinghalbusyIf init returns a hal busy.
EnterDeepPowerDownMode_InitStateResetSleepPendingHalTimeoutIf init returns a hal timeout.
EnterDeepPowerDownMode_StartStateResetSleepPendingHalErrorIf start returns a hal error.
EnterDeepPowerDownMode_StartStateResetSleepPendingHalBusyIf start returns a hal busy.
EnterDeepPowerDownMode_StartStateResetSleepPendingHalTimeoutIf start returns a hal timeout.
EnterDeepPowerDownMode_SleepStateResetSleepPendingHalErrorIf sleep returns a hal error.
EnterDeepPowerDownMode_SleepStateResetSleepPendingHalBusyIf sleep returns a hal busy.
EnterDeepPowerDownMode_SleepStateResetSleepPendingHalTimeoutIf sleep returns a hal timeout.

Definition at line 179 of file stm32can.cpp.

Here is the call graph for this function:

◆ init()

void semf::Stm32Can::init ( )
overridevirtual

Initializes the hardware.

Exceptions
Init_HalErrorIf the ST-HAL stumbles upon an error.
Init_HalBusyIf the ST-HAL is busy.
Init_HalTimeoutIf the ST-HAL times out.

Implements semf::CommunicationHardware.

Definition at line 50 of file stm32can.cpp.

Here is the call graph for this function:

◆ isrError()

void semf::Stm32Can::isrError ( CAN_HandleTypeDef &  can)

Sending a error signal.

Parameters
canPointer to CAN hardware handler.

Definition at line 468 of file stm32can.cpp.

Here is the call graph for this function:

◆ isrRead()

void semf::Stm32Can::isrRead ( CAN_HandleTypeDef &  can,
uint8_t  fifoId 
)

Saves a received message and sends a readyRead or readyRequested signal.

Parameters
canPointer to CAN hardware handler.
fifoIdFIFO ID of the received message.
Exceptions
IsrRead_NullpointerReadBufferRead data is not set.
IsrRead_FifoIdInvalidIf FIFO-id is invalid.
IsrRead_HalErrorIf ST-HAL returns hal error.
IsrRead_HalBusyIf ST-HAL returns hal busy.
IsrRead_HalTimeoutIf ST-HAL returns hal timeout.

Definition at line 403 of file stm32can.cpp.

Here is the call graph for this function:

◆ isrWritten()

void semf::Stm32Can::isrWritten ( CAN_HandleTypeDef &  can,
uint8_t  mailboxId 
)

Sending a readyWritten signal.

Parameters
canPointer to CAN hardware handler.
mailboxIdId of mailbox where message was written from.

Definition at line 455 of file stm32can.cpp.

Here is the call graph for this function:

◆ isStarted()

bool semf::Stm32Can::isStarted ( ) const

Get the state of the start of the CAN driver.

Returns
True if the driver already started.

Definition at line 121 of file stm32can.cpp.

◆ messageId()

uint32_t semf::Stm32Can::messageId ( ) const
overridevirtual

Returns the message ID for reading data. Use this function after reading the data via read-function.

Returns
message ID of read data

Implements semf::Can.

Definition at line 126 of file stm32can.cpp.

Here is the caller graph for this function:

◆ queue()

LinkedQueue< Stm32Can > * semf::Stm32Can::queue ( )
static

Get the list with all cans.

Returns
The list with all cnas.

Definition at line 379 of file stm32can.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ releaseDeepPowerDownMode()

void semf::Stm32Can::releaseDeepPowerDownMode ( )

Go to active mode.

Exceptions
ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalErrorIf wakeup returns a hal error.
ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalBusyIf wakeup returns a hal busy.
ReleaseDeepPowerDownMode_WakeupStateSleepActiveHalTimeoutIf wakeup returns a hal timeout.
ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalErrorIf init returns a hal error.
ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalBusyIf init returns a hal busy.
ReleaseDeepPowerDownMode_InitStateResetSleepPendingHalTimeoutIf init returns a hal timeout.
ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalErrorIf start returns a hal error.
ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalBusyIf start returns a hal busy.
ReleaseDeepPowerDownMode_StartStateResetSleepPendingHalTimeoutIf start returns a hal timeout.
ReleaseDeepPowerDownMode_StartStateErrorIf illegal state got reached.
ReleaseDeepPowerDownMode_StartStateInvalidIf illegal state got reached.

Definition at line 288 of file stm32can.cpp.

Here is the call graph for this function:

◆ requestHardware()

void semf::Stm32Can::requestHardware ( )
overrideprotectedvirtual

Hardware will request data on specific id. Id can be set by calling setMessageId().

Exceptions
RequestHardware_InvalidMessageIdIf message id is invalid.
RequestHardware_HalErrorIf ST-HAL returns HAL error.
RequestHardware_HalBusyIf ST-HAL returns HAL busy.
RequestHardware_HalTimeoutIf ST-HAL returns HAL timeout.
RequestHardware_NotStartedIf ST-HAL not started.

Implements semf::CanHardware.

Definition at line 540 of file stm32can.cpp.

Here is the call graph for this function:

◆ setFilter()

void semf::Stm32Can::setFilter ( uint32_t  filterBank,
uint32_t  messageId,
uint32_t  messageIdMask 
)
overridevirtual

Sets a message hardware receive filter.

Parameters
filterBankIndex of filter bank.
messageIdMessage id for allowing to receive.
messageIdMaskMessage id range for allowing to receive.
Attention
Filter is tested for the normal can address. Filter implementation is on the link.
See also
https://schulz-m.github.io/2017/03/23/stm32-can-id-filter
Exceptions
SetFilter_IndexOutOfBoundsIf index is too high.
SetFilter_MessageIdInvalidIf message id is invalid.
SetFilter_MessageIdMaskInvalidIf the mask of the messageID is invalid.

Implements semf::Can.

Definition at line 145 of file stm32can.cpp.

Here is the call graph for this function:

◆ setFrequency()

void semf::Stm32Can::setFrequency ( uint32_t  hz)
overridevirtual

Sets the speed.

Parameters
hzfrequency in hz

Implements semf::Can.

Definition at line 137 of file stm32can.cpp.

◆ setMessageId()

void semf::Stm32Can::setMessageId ( uint32_t  id)
overridevirtual

Sets the message ID for writing data. Use this function before writing the data via write-function.

Parameters
idmessage ID of read data

Implements semf::Can.

Definition at line 131 of file stm32can.cpp.

◆ setReadBuffer()

void semf::Stm32Can::setReadBuffer ( uint8_t  buffer[],
size_t  bufferSize 
)
overridevirtual

Sets the read buffer for having the possibility to handle the received data.

Parameters
bufferBuffer to store received bytes in.
bufferSizeSize of receive buffer.

Implements semf::CanHardware.

Definition at line 101 of file stm32can.cpp.

◆ start()

void semf::Stm32Can::start ( )

Hardware driver will be started.

Exceptions
Start_HalTimeoutIf the ST-HAL times out.
Start_HalNotInitializedIf the ST-HAL is not initialized.

Definition at line 595 of file stm32can.cpp.

Here is the call graph for this function:

◆ stop()

void semf::Stm32Can::stop ( )

Hardware driver will be stopped.

Exceptions
Stop_HalTimeoutIf the ST-HAL times out.

Definition at line 623 of file stm32can.cpp.

Here is the call graph for this function:

◆ stopRead()

void semf::Stm32Can::stopRead ( )
overridevirtual

Stops the read process as fast as possible without destroying anything. The readStopped Signal gets emitted if the call was successful.

Implements semf::app::Communication.

Definition at line 114 of file stm32can.cpp.

◆ stopWrite()

void semf::Stm32Can::stopWrite ( )
overridevirtual

Stops the write process as fast as possible without destroying anything. The writeStopped Signal gets emitted if the call was successful.

Implements semf::app::Communication.

Definition at line 107 of file stm32can.cpp.

◆ systemIsrError()

void semf::Stm32Can::systemIsrError ( CAN_HandleTypeDef &  can)
static

System-wide interrupt service routine for can error.

Parameters
canHardware handler.

Definition at line 397 of file stm32can.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemIsrRead()

void semf::Stm32Can::systemIsrRead ( CAN_HandleTypeDef &  can,
uint8_t  fifoId 
)
static

System-wide interrupt service routine for can receive.

Parameters
canHardware handler.
fifoIdId message has read into.

Definition at line 385 of file stm32can.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemIsrWritten()

void semf::Stm32Can::systemIsrWritten ( CAN_HandleTypeDef &  can,
uint8_t  mailboxId 
)
static

System-wide interrupt service routine for can transmit.

Parameters
canHardware handler.
mailboxIdId of mailbox where message was written from.

Definition at line 391 of file stm32can.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeHardware()

void semf::Stm32Can::writeHardware ( const uint8_t  data[],
size_t  dataSize 
)
overrideprotectedvirtual

Hardware will write data.

Parameters
datadata array
dataSizedata array size
Exceptions
WriteHardware_InvalidMessageIdIf message id is invalid.
WriteHardware_HalErrorIf ST-HAL returns HAL error.
WriteHardware_HalBusyIf ST-HAL returns HAL busy.
WriteHardware_HalTimeoutIf ST-HAL returns HAL timeout.
WriteHardware_NotStartedIf ST-HAL not started.

Implements semf::CanHardware.

Definition at line 477 of file stm32can.cpp.

Here is the call graph for this function:
Here is the caller graph for this function: