Programmable Clock with RTOS 0.0.1
Implementaiton of a programmable clock using RTOS with STM32GO microcontroller.
Loading...
Searching...
No Matches
can_tp.c File Reference

Board Support Package. More...

#include "bsp.h"
#include "can_tp.h"
#include <string.h>

Macros

#define SINGLE_FRAME_TYPE   0u
 
#define FIRST_FRAME_TYPE   1u
 
#define CONSECUTIVE_FRAME_TYPE   2u
 
#define FLOW_CONTROL_FRAME_TYPE   3u
 
#define CONTINUE_TO_SEND   0u
 
#define WAIT   1u
 
#define OVERFLOW_ABORT   2u
 
#define FLAG_CAN_TP_ON   1u
 
#define FLAG_CAN_TP_OFF   0u
 
#define CTS_ON   1u
 
#define CTS_OFF   0u
 
#define WAIT_ON   1u
 
#define WAIT_OFF   0u
 
#define OVERFLOW_ABORT_ON   1u
 
#define OVERFLOW_ABORT_OFF   0u
 
#define OFFSET_FIRST_FRAME   6u
 

Functions

void CAN_TP_Tick (void)
 Timing function for CAN_TP.
 
void CAN_TP_RxMessageBufferSet (CAN_TP_Header *header, uint8_t *buffer, uint32_t bufferSize)
 Sets the received message buffer and its size.
 
void CAN_TP_RxSeparationTimeSet (CAN_TP_Header *header, uint8_t separationTime)
 Sets the separation time between received messages.
 
void CAN_TP_RxBlockSizeSet (CAN_TP_Header *header, uint8_t blockSize)
 Sets the block size for received messages.
 
void CAN_TP_NewMessage (CAN_TP_Header *header, void *data)
 Handles a new message received by CAN_TP.
 
void CAN_TP_TransmitMessage (CAN_TP_Header *header, const uint8_t *data, uint32_t length)
 Initiates the transmission of a CAN_TP_Transmited message.
 
static void CAN_TP_TxTransmit_Period_Task (CAN_TP_Header *header)
 Transmits CAN_TP messages based on the current state.
 
static void CAN_TP_RxReceive_Period_Task (CAN_TP_Header *header)
 Handles the received CAN_TP messages.
 
uint8_t CAN_TP_IsMessageReady (const CAN_TP_Header *header)
 Checks if the message is ready to be processed.
 
void CAN_TP_MessageGet (CAN_TP_Header *header, uint8_t *data, uint8_t data_length)
 Gets the processed message.
 
void CAN_TP_Init (CAN_TP_Header *header)
 Initializes the CAN_TP configuration for the CAN_TP_Periodic_Task.
 
void CAN_TP_Periodic_Task (CAN_TP_Header *header)
 Task process for CAN_TP.
 
void CAN_TP_TransmitInterruptMessage (CAN_TP_Header *header)
 Message transmission with interruption.
 

Variables

static uint8_t flag_can_tp_flowcontrol_status [3] = { 0u , 0u , 0u }
 Array to store CAN_TP flow control status flags.
 

Detailed Description

Board Support Package.

File provides the neccesary drivers, libraries, and configurations for the CAN BUS.

==============================================================================
                   ##### How to use this driver #####
==============================================================================

Interruption necessary:

- HAL_FDCAN_RxFifo0Callback( FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs )
- HAL_FDCAN_TxFifoEmptyCallback( FDCAN_HandleTypeDef *hfdcan )

In the interrupcion HAL_FDCAN_RxFifo0Callback, you need to put the function
CAN_TP_NewMessage( CAN_TP_Header *header, void *data );

For example:

   void HAL_FDCAN_RxFifo0Callback( FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs )
   {

       FDCAN_RxHeaderTypeDef CANRxHeader;

       static uint8_t RxData[ 8 ] = { 0 };

       HAL_FDCAN_GetRxMessage( hfdcan, FDCAN_RX_FIFO0, &CANRxHeader, RxData );

       CAN_TP_NewMessage( &features_packet, (void *)RxData );

   }


In the interrupcion HAL_FDCAN_TxFifoEmptyCallback, you need to put the function
void CAN_TP_TransmitInterruptMessage( CAN_TP_Header *header );

For example:

   void HAL_FDCAN_TxFifoEmptyCallback( FDCAN_HandleTypeDef *hfdcan )
   {
       CAN_TP_TransmitInterruptMessage(&features_packet);
   }

Important!!! You must select the size of message that your bufferRx will receive:

#define MAX_ARRAY_SIZE 25

For the receiving this is a example:

   static CAN_TP_Header features_packet;

   void vTask1( void *pvParameters )
   {
       uint8_t rxBuffer[ 22 ]  = { 0 };
       uint8_t messageRx[ 22 ] = { 0 };
       uint8_t value           = 1u;

       CAN_TP_Init( &features_packet );

       CAN_TP_RxBlockSizeSet( &features_packet, 10 );

       CAN_TP_RxSeparationTimeSet( &features_packet, 10 );

       CAN_TP_RxMessageBufferSet( &features_packet, rxBuffer, sizeof( rxBuffer ) );

       for( ;; )
       {
           CAN_TP_Periodic_Task( &features_packet );

           if( value == CAN_TP_IsMessageReady( &features_packet ) )
           {

               CAN_TP_MessageGet( &features_packet, messageRx, sizeof( messageRx ) );

               SEGGER_RTT_printf( 0, "Message:" );

               for( uint8_t i = 0; i < sizeof( messageRx ); i++ )
               {

                   SEGGER_RTT_printf( 0, " %x", messageRx[ i ] );
               }

               SEGGER_RTT_printf( 0, "\n" );
           }

           vTaskDelay( 100 );
       }

       (void)pvParameters;
   }

For the transmision this is a example:

Macro Definition Documentation

◆ CONSECUTIVE_FRAME_TYPE

#define CONSECUTIVE_FRAME_TYPE   2u

Frame type to mark consecutive frame

◆ CONTINUE_TO_SEND

#define CONTINUE_TO_SEND   0u

Indicate the flow control flag 0

◆ CTS_OFF

#define CTS_OFF   0u

Indicate the number when the flow control flag 0 is not ready

◆ CTS_ON

#define CTS_ON   1u

Indicate the number when the flow control flag 0 is ready

◆ FIRST_FRAME_TYPE

#define FIRST_FRAME_TYPE   1u

Frame type to mark the first frame

◆ FLAG_CAN_TP_OFF

#define FLAG_CAN_TP_OFF   0u

The flag indicates when CAN TP is not ready to transmit a message

◆ FLAG_CAN_TP_ON

#define FLAG_CAN_TP_ON   1u

The flag indicates when CAN TP is ready to transmit a message

◆ FLOW_CONTROL_FRAME_TYPE

#define FLOW_CONTROL_FRAME_TYPE   3u

Frame type to mark the flow control frame

◆ OFFSET_FIRST_FRAME

#define OFFSET_FIRST_FRAME   6u

Offset data of the first frame because it transmitted 6 bytes of data

◆ OVERFLOW_ABORT

#define OVERFLOW_ABORT   2u

Indicate the flow control flag 2

◆ OVERFLOW_ABORT_OFF

#define OVERFLOW_ABORT_OFF   0u

Indicate the number when the flow control flag 2 is not ready

◆ OVERFLOW_ABORT_ON

#define OVERFLOW_ABORT_ON   1u

Indicate the number when the flow control flag 2 is ready

◆ SINGLE_FRAME_TYPE

#define SINGLE_FRAME_TYPE   0u

Frame type to mark a single frame

◆ WAIT

#define WAIT   1u

Indicate the flow control flag 1

◆ WAIT_OFF

#define WAIT_OFF   0u

Indicate the number when the flow control flag 1 is not ready

◆ WAIT_ON

#define WAIT_ON   1u

Indicate the number when the flow control flag 1 is ready

Function Documentation

◆ CAN_TP_Init()

void CAN_TP_Init ( CAN_TP_Header * header)

Initializes the CAN_TP configuration for the CAN_TP_Periodic_Task.

Function necessary for work with normality

Parameters
headerCAN_TP header data structure.

< The flag indicates when CAN TP is not ready to transmit a message

< The flag indicates when CAN TP is not ready to transmit a message

◆ CAN_TP_IsMessageReady()

uint8_t CAN_TP_IsMessageReady ( const CAN_TP_Header * header)

Checks if the message is ready to be processed.

Function that tells us if we can read the complete message received through CAN_TP

Parameters
headerCAN_TP header data structure.
Returns
1 if the message is ready, 0 otherwise.

◆ CAN_TP_MessageGet()

void CAN_TP_MessageGet ( CAN_TP_Header * header,
uint8_t * data,
uint8_t data_length )

Gets the processed message.

Function to extract all the message received by CAN TP into a variable and then delete all the message received in the Rx buffer.

Parameters
headerCAN_TP header data structure.
dataPointer to the message data.
data_lengthLength of the data to retrieve.

◆ CAN_TP_NewMessage()

void CAN_TP_NewMessage ( CAN_TP_Header * header,
void * data )

Handles a new message received by CAN_TP.

Function to implement in HAL_FDCAN_RxFifo0Callback. This function tells us when to enter CAN_TP_RxReceive_Period_Task

Parameters
headerCAN_TP header data structure.
dataPointer to the received message data.

◆ CAN_TP_Periodic_Task()

void CAN_TP_Periodic_Task ( CAN_TP_Header * header)

Task process for CAN_TP.

Function switches the CAN TP periodically if it transmits or receives.

Parameters
headerCAN_TP header data structure.

< The flag indicates when CAN TP is ready to transmit a message

< The flag indicates when CAN TP is not ready to transmit a message

◆ CAN_TP_RxBlockSizeSet()

void CAN_TP_RxBlockSizeSet ( CAN_TP_Header * header,
uint8_t blockSize )

Sets the block size for received messages.

Function to indicate the blocks that will be received in the transmission of messages. If kept at 0, message limits will not be taken into account for transmission.

Parameters
headerCAN_TP header data structure.
blockSizeBlock size.

◆ CAN_TP_RxMessageBufferSet()

void CAN_TP_RxMessageBufferSet ( CAN_TP_Header * header,
uint8_t * buffer,
uint32_t bufferSize )

Sets the received message buffer and its size.

Function to indicate the size and memory space of the Rx buffer where the messages received from the data transmission will be saved.

Parameters
headerCAN_TP header data structure.
bufferReceived message buffer.
bufferSizeSize of the message buffer.

◆ CAN_TP_RxReceive_Period_Task()

static void CAN_TP_RxReceive_Period_Task ( CAN_TP_Header * header)
static

Handles the received CAN_TP messages.

This function processes the received CAN_TP messages, including flow control frames, first frames, and consecutive frames. It updates the transmission state accordingly. Also save the decoded message from the reception.

Parameters
headerCAN_TP header data structure.

< Frame type to mark the flow control frame

< Indicate the flow control flag 0

< Indicate the flow control flag 0

< Indicate the number when the flow control flag 0 is ready

< Indicate the flow control flag 1

< Indicate the flow control flag 1

< Indicate the number when the flow control flag 1 is ready

< Indicate the flow control flag 2

< Indicate the flow control flag 2

< Indicate the number when the flow control flag 2 is ready

< Frame type to mark a single frame

< Frame type to mark the first frame

< Frame type to mark consecutive frame

◆ CAN_TP_RxSeparationTimeSet()

void CAN_TP_RxSeparationTimeSet ( CAN_TP_Header * header,
uint8_t separationTime )

Sets the separation time between received messages.

Function to indicate the separation time of message transmission for the Rx buffer. If kept at 0, no waiting limits will apply between received messages.

Parameters
headerCAN_TP header data structure.
separationTimeSeparation time between messages.

◆ CAN_TP_Tick()

void CAN_TP_Tick ( void )

Timing function for CAN_TP.

Function that counts CAN TP ticks to ensure separation time and sending of a wait, overflow/abort.

◆ CAN_TP_TransmitInterruptMessage()

void CAN_TP_TransmitInterruptMessage ( CAN_TP_Header * header)

Message transmission with interruption.

Function that calls CAN_TP_TxTransmit_Period_Task when the flag_interruption is activated, this happens when a flowcontrol typeframe CTS is received.

Parameters
headerCAN_TP header data structure.

< The flag indicates when CAN TP is ready to transmit a message

◆ CAN_TP_TransmitMessage()

void CAN_TP_TransmitMessage ( CAN_TP_Header * header,
const uint8_t * data,
uint32_t length )

Initiates the transmission of a CAN_TP_Transmited message.

The function analyzes whether to send a single frame or multiple consecutive frames depending on the length of the data. In the case of multiple consecutive frames, the number of consecutive frames that will be sent is calculated.

Parameters
headerCAN_TP header data structure.
dataPointer to the message data.
lengthLength of the message.

< The flag indicates when CAN TP is ready to transmit a message

< Offset data of the first frame because it transmitted 6 bytes of data

< Offset data of the first frame because it transmitted 6 bytes of data

< Offset data of the first frame because it transmitted 6 bytes of data

< Offset data of the first frame because it transmitted 6 bytes of data

< Offset data of the first frame because it transmitted 6 bytes of data

< The flag indicates when CAN TP is ready to transmit a message

◆ CAN_TP_TxTransmit_Period_Task()

static void CAN_TP_TxTransmit_Period_Task ( CAN_TP_Header * header)
static

Transmits CAN_TP messages based on the current state.

This function manages the transmission of CAN_TP messages, including first frames and consecutive frames. It handles the flow control logic and updates the state for transmission. To make the transmissions, right shifts are made depending on the type of frame for the general data bytes of the frame and in the data bytes a while is used to extract the message sent by the user and encode it in the corresponding variable.

Parameters
headerCAN_TP header data structure.

< Frame type to mark the first frame

< Frame type to mark a single frame

< Frame type to mark a single frame

< Frame type to mark the first frame

< The flag indicates when CAN TP is not ready to transmit a message

< Frame type to mark the first frame

< Frame type to mark the flow control frame

< Frame type to mark consecutive frame

< Offset data of the first frame because it transmitted 6 bytes of data

< Frame type to mark consecutive frame

< Offset data of the first frame because it transmitted 6 bytes of data

< Frame type to mark consecutive frame

< Offset data of the first frame because it transmitted 6 bytes of data

< Frame type to mark the first frame

< The flag indicates when CAN TP is not ready to transmit a message

< The flag indicates when CAN TP is not ready to transmit a message

< Frame type to mark the flow control frame

< Indicate the flow control flag 0

< Indicate the number when the flow control flag 0 is ready

< Frame type to mark consecutive frame

< The flag indicates when CAN TP is ready to transmit a message

< The flag indicates when CAN TP is not ready to transmit a message

< Indicate the flow control flag 1

< Indicate the number when the flow control flag 1 is ready

< Indicate the flow control flag 2

< Indicate the number when the flow control flag 2 is ready

Variable Documentation

◆ flag_can_tp_flowcontrol_status

uint8_t flag_can_tp_flowcontrol_status[3] = { 0u , 0u , 0u }
static

Array to store CAN_TP flow control status flags.

This array holds the status flags indicating the flow control conditions for CAN_TP transmission. Index 0 represents 'Continue to Send' (CTS), index 1 represents 'Wait', and index 2 represents 'Overflow Abort'. Initial values are set to indicate OFF states for all conditions.