CAN Frames Echoing Thread for STM32F779-Eval board

C

Sometimes, we need an easy way to test and verify that our board works correctly, especially if we are using breadboards and cables.

The following simple FreeRTOS thread is able to echo each incoming CAN frame. To use this thread you must enable FreeRTOS using the ST’s CubeMX software which will generate the appropriate startup source code and afterwards you have to include some extra libraries that can be found in the blog (please check the previous posts about Framework, CAN, threads)

FreeRTOS = A truly free professional grade RTOS for microcontrollers that supports 35 MCU architectures and become a market leader.

#ifndef THREADS_TA_ECHO_CAN_H_
#define THREADS_TA_ECHO_CAN_H_

/******************************************************************************
 * Includes
 ******************************************************************************/

	#include <string.h>
	#include <inttypes.h>

	// CubeMX Libraries
	#include <cmsis_os.h>
	#include "FreeRTOS.h"
	#include "Drivers/BSP/Inc/stm32f779i_eval.h"
	#include "rng.h"

	#include <Libraries/lbr_framework.h>
	#include <Libraries/lbr_threads.h>

	#include <Devices/dev_can.h>

/******************************************************************************
 *  Structures & Global Variables
 ******************************************************************************/

	extern osThreadId 		ta_echo_can1_handle;
	extern thread_info_t  	ta_echo_can1_info;

	extern osThreadId 		ta_echo_can3_handle;
	extern thread_info_t  	ta_echo_can3_info;

	extern uint8_t 	ta_echo_can_port;
	extern uint32_t ta_echo_latency_ms;
/******************************************************************************
 * Functions
 ******************************************************************************/

	void ta_echo_can(void const * arguments);

/******************************************************************************
 * Header File Guard Symbol End
 ******************************************************************************/


#endif /* THREADS_TA_ECHO_CAN_H_ */
/******************************************************************************
 * Source File Start
 ******************************************************************************/

/******************************************************************************
 * Includes
 ******************************************************************************/

#include "ta_echo_can.h"

/******************************************************************************
 * Global Variables
 ******************************************************************************/

	osThreadId 		ta_echo_can1_handle;
	thread_info_t  	ta_echo_can1_info;

	osThreadId 		ta_echo_can3_handle;
	thread_info_t  	ta_echo_can3_info;

/******************************************************************************
 * Functions
 ******************************************************************************/

void dev_can1_rx_callback()
{
	dev_can_send_frame(CAN_1,dev_can_get_rx_frame(CAN_1));
}

void dev_can3_rx_callback()
{
	dev_can_send_frame(CAN_3,dev_can_get_rx_frame(CAN_3));
}

void ta_echo_can(void const * arguments)
{
	uint8_t selected_can = *((uint8_t *)arguments);

	lbr_thread_init(selected_can == 0 ? &ta_echo_can1_info : &ta_echo_can3_info, TT_APPLICATION, TM_MANUAL);

	if( selected_can == 0 )
	{
		if(dev_can_init(CAN_1)==I_INVALID)
		{
			ta_echo_can1_info.state = TS_ERROR_ALL;
			osDelay(345);
			lbr_thread_deinit(&ta_echo_can1_info, &ta_echo_can1_handle);
			return;
		}
		dev_can_set_rx_callback(CAN_1,dev_can1_rx_callback);
	}

	if( selected_can == 3 )
	{
		if(dev_can_init(CAN_3)==I_INVALID)
		{
			ta_echo_can3_info.state = TS_ERROR_ALL;
			osDelay(345);
			lbr_thread_deinit(&ta_echo_can3_info, &ta_echo_can3_handle);
			return;
		}
		dev_can_set_rx_callback(CAN_3,dev_can3_rx_callback);
	}

	osDelay(10);

	// Main Loop

	lbr_thread_execute(selected_can == 0 ? &ta_echo_can1_info : &ta_echo_can3_info);

	while ((selected_can == 0 ? ta_echo_can1_info.state : ta_echo_can3_info.state) == TS_ACTIVE)
	{
		osDelay(128);
	}

	// DeInitialize

	if( selected_can == 0 )
		dev_can_deinit(CAN_1);
	if(  selected_can == 3 )
		dev_can_deinit(CAN_3);

	lbr_thread_deinit(selected_can == 0 ? &ta_echo_can1_info : &ta_echo_can3_info, selected_can == 0 ? &ta_echo_can1_handle : &ta_echo_can3_handle);
}

/******************************************************************************
 * Source File End
 ******************************************************************************/
Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags