⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can_shell.c

📁 can initialization for coldfire microcontorllers
💻 C
字号:
/************************************************************************
*
*  Freescale Semicondactor
*  ALL RIGHTS RESERVED
*  (c) Copyright 2004Freescale Semiconductor
*
*************************************************************************
*  FILE NAME: can_shell.c
*
*  PURPOSE: Implementation of ETHERNET/TCPIP related settings
*           in shell UI functions
*
*  AUTHOR(s): Andrej A. Oulianov, Igor Byeda
*
*************************************************************************/

#include "sys_global.h"
#include "can_shell.h"


extern can_SpeedParam_t can_SpeedParam[];
static can_SpeedParam_t can_TempSet;

/************************************************************************
* NAME: can_shl_init
*
* DESCRIPTION: Main Shell initial function.
************************************************************************/
void can_shl_init (void)
{
	// coping current settings to temporary variable
	can_flexcan_getBitRate(&can_TempSet);	
	
	printf(" >> CAN_unit submenu\n");
	printf(" Note: You could freely modify any low level CAN bus bit timing\n");
	printf("       parameters - modification stores in virtual buffer.\n");
	printf("       To check result baudrate use 'speed calc'\n");
	printf("       If you satisfy with it use 'speed apply' to copy settings\n");
	printf("       to CAN HW controller and restart unit\n\n");
	
	shell_CurrPrompt = CAN_SHL_PROMPT_STR;
}

/************************************************************************
* NAME: can_shl_exit
*
* DESCRIPTION: Main Shell release function.
************************************************************************/
void can_shl_exit (void)
{
	printf(" << main menu\n");
	shell_CurrPrompt = MAIN_SHL_PROMPT_STR;
}

/************************************************************************
* NAME: can_shl_help
*
* DESCRIPTION: Shows command shell help.
************************************************************************/
void can_shl_help (struct shell_t * shl, int argc, char **argv)
{
	int32 index;
	
	(void)argc;
	(void)argv;
	
	printf("\n");
	for (index = 0; index < shl->command_table_size; index++)
	{
		printf(CAN_SHL_HELP_FORMAT,
			shl->command_table[index].cmd,
			shl->command_table[index].description,
			shl->command_table[index].cmd,
			shl->command_table[index].syntax);
	}
	printf(CAN_SHL_HELP_FORMAT, SHL_EXIT_STR, "Back to main menu", SHL_EXIT_STR, "");

	printf("\n");
}

/************************************************************************
* NAME: can_shl_SHOW
*
* DESCRIPTION: Show all parameters
************************************************************************/
void can_shl_show(struct shell_t * shl, int argc, char **argv)
{
	int32 i;
	can_SpeedParam_t* can_set;
	
	can_set = &(sys_flash_CurrSettings.CAN_SpeedLastValue);
	
	printf("\n");
	printf(CAN_OUT_BR_FRMT, can_set->CanSpeed/1000);
	printf(CAN_OUT_AD_FRMT, ((sys_flash_CurrSettings.CAN_SpeedForceAutodetect)?("ON"):("OFF")));
	printf(CAN_OUT_SC_FRMT, (SYSTEM_CLOCK)/(2*(can_set->PREScalerDIVision + 1)), can_set->PREScalerDIVision);
	printf(CAN_OUT_SP_FRMT,  ((can_set->NumberOfTQ - can_set->PhaseSEG2)*100/can_set->NumberOfTQ), can_set->NumberOfTQ);
	printf(CAN_OUT_TBL_NAMES);
	printf(CAN_OUT_TBL_LINE);
	printf(CAN_OUT_TBL_FRMT, can_set->PROPagationSEG, 
							 can_set->PhaseSEG1,
							 can_set->PhaseSEG2,
							 can_set->ResyncJumpWidth,
							 can_set->SAMPlingMode*2+1 );

	printf("\n   Use 'set std' command without parameters to see\n");
	printf("   the list of standart CAN baudrates available.\n");
	printf("\n");														   	  
}

/************************************************************************
* NAME: GROUP of SET functions
*
* DESCRIPTION: Set bittiming parameters functions
************************************************************************/
void can_shl_set(struct shell_t * shl, int argc, char **argv)
{
	int32 index;

	switch (argc)
	{
		case 1:
			printf("\n");
			printf("Valid 'set' options:\n");
			for (index = 0; index < CAN_SHELL_SET_CMD_NUM; ++index)
			{
				printf(CAN_SHL_SET_OPT_FORMAT, can_shl_set_cmd[index].option);
				printf("%s\n", can_shl_set_cmd[index].syntax);
			}
			printf("\n");
			return;
		case 2:
		case 3:
		case 6:
			for (index = 0; index < CAN_SHELL_SET_CMD_NUM; index++)
			{
				if (strcasecmp(can_shl_set_cmd[index].option, argv[1]) == 0)
				{
					can_shl_set_cmd[index].func(shl, argc, argv);
					return;
				}
			}
			printf(CAN_SHL_SET_OPT_ERR, argv[1]);
			return;
		default:
			printf("Error: Invalid argument list\n");
	}
}

void can_shl_set_std(struct shell_t * shl, int argc, char **argv)
{
	int32 i;
	int32 isStd = 0;
	
	if (argv[2] == NULL)
	{
		printf("\n   List of standart CAN baudrate (kBit/s) available:\n");
		printf("   SPEED nTQ   SYNC PROPSEG PSEG1 PSEG2   RJW SAMP\n");
		printf("   -----------------------------------------------\n");
		for (i=0; i<5; i++)
			printf("   %4d  %2d     1    %d       %d     %d       %d   %d\n",
			 	can_SpeedParam[i].CanSpeed/1000,
				can_SpeedParam[i].NumberOfTQ,
				can_SpeedParam[i].PROPagationSEG,
				can_SpeedParam[i].PhaseSEG1,
				can_SpeedParam[i].PhaseSEG2,
				can_SpeedParam[i].ResyncJumpWidth,
				can_SpeedParam[i].SAMPlingMode*2+1 );
		printf("\n");														   	  
	}
	else
	{
	 	for (i=0; i <5; i++)
			if (can_SpeedParam[i].CanSpeed == atoi(argv[2])*1000)
				isStd = 1;
				
		if (isStd)
		{
			can_unit_Stop();
   			can_set_GlobalParam(PARAM_CAN_SPEED, atoi(argv[2])*1000);
   			can_unit_Run();
   			
			sys_update_CurrSettingsFromHW( &sys_flash_CurrSettings );
		   	sys_update_Flash( &sys_flash_CurrSettings );		
		}
		else
			printf(CAN_SHL_SET_STD_ERR, atoi(argv[2]));
	}
}

void can_shl_set_presdiv(struct shell_t * shl, int argc, char **argv)
{
	if (argv[2] == NULL)
	{
		printf("\n");														   	  
		printf("   MCF5282 F_sys: %d MHz\n", SYSTEM_CLOCK);
		printf("   CAN S-clock is determined by the following calculations:\n");
		printf("   S_clk = F_sys/(2*PRESDIV + 1), PRESDIV range is [0..255]\n");
		printf("\n   use 'speed calc' to see possible result\n\n");
	}
	else
	{
		if (atoi(argv[2]) <= 255)
		{
			can_TempSet.PREScalerDIVision = atoi(argv[2]);
		}
		else
			printf(CAN_SHL_SET_PRESDIV_ERR, atoi(argv[2]));
	}
}

void can_shl_set_bitparam(struct shell_t * shl, int argc, char **argv)
{
	uint32	nTQ = 0, 
			PropSEG = 0, 
			PSEG1 = 0, 
			PSEG2 = 0;
	
	if (argc != 6)
	{
		printf("\n");														   	  
		printf("   Enter BitTiming parameters in follows:\n");
		printf("   nTQ PropSEG PhaseSEG1 PhaseSEG2\n");
		printf("\n   use 'speed calc' to see possible result\n\n");
	}
	else
	{
		PropSEG = atoi(argv[3]);
		if (PropSEG<1 || PropSEG>8)
		{
			printf(CAN_SHL_SET_BT_2_ERR, PropSEG);
			return;
		}
		PSEG1 = atoi(argv[4]);
		if (PSEG1<1 || PSEG1>8)
		{
			printf(CAN_SHL_SET_BT_3_ERR, PSEG1);
			return;
	    	}
		PSEG2 = atoi(argv[5]);
		if (PSEG2<2 || PSEG2>8)
		{
			printf(CAN_SHL_SET_BT_4_ERR, PSEG2);
			return;
		}
		nTQ = atoi(argv[2]);
		if (nTQ != (1 + PropSEG + PSEG1 + PSEG2))
		{
			printf(CAN_SHL_SET_BT_1_ERR);
			return;
		}
		
		// if finally everything is OK
		can_TempSet.NumberOfTQ = nTQ;
		can_TempSet.PROPagationSEG = PropSEG;
		can_TempSet.PhaseSEG1 = PSEG1;
		can_TempSet.PhaseSEG2 = PSEG2;		
	}
}

void can_shl_set_rjw(struct shell_t * shl, int argc, char **argv)
{
	if (argv[2] == NULL)
	{
		printf("\n");														   	  
		printf("   Resynchronization Jump Width\n");
		printf("   Possible range is [1..4]\n\n");
	}
	else
	{
		if (atoi(argv[2])>=1 && atoi(argv[2])<=4)
		{
			can_TempSet.ResyncJumpWidth = atoi(argv[2]);
		}
		else
			printf(CAN_SHL_SET_RJW_ERR, atoi(argv[2]));
	}
}

void can_shl_set_samp(struct shell_t * shl, int argc, char **argv)
{
	if (argv[2] == NULL)
	{
		printf("\n");														   	  
		printf("   Sampling mode\n");
		printf("   Possible values are 1 or 3 times.\n\n");
	}
	else
	{
		if (atoi(argv[2])==1 || atoi(argv[2])==3)
		{
			can_TempSet.SAMPlingMode = atoi(argv[2])/2;
		}
		else
    			printf(CAN_SHL_SET_SAMP_ERR, atoi(argv[2]));
	}
}

/************************************************************************
* NAME: GROUP of SPEED functions
*
* DESCRIPTION: Check/Apply changed bittiming parameters
************************************************************************/
void can_shl_speed(struct shell_t * shl, int argc, char **argv)
{
	int32 index;

	switch (argc)
	{
		case 1:
			printf("\n");
			printf("Valid 'speed' options:\n");
			for (index = 0; index < CAN_SHELL_SPEED_CMD_NUM; ++index)
			{
				printf(CAN_SHL_SET_OPT_FORMAT, can_shl_speed_cmd[index].option);
				printf("%s\n", can_shl_speed_cmd[index].syntax);
			}
			printf("\n");
			return;
		case 2:
			for (index = 0; index < CAN_SHELL_SPEED_CMD_NUM; index++)
			{
				if (strcasecmp(can_shl_speed_cmd[index].option, argv[1]) == 0)
				{
					can_shl_speed_cmd[index].func(shl, argc, argv);
					return;
				}
			}
			printf(CAN_SHL_SET_OPT_ERR, argv[1]);
			return;
		default:
			printf("Error: Invalid argument list\n");
	}
}

void can_shl_speed_auto(struct shell_t * shl, int argc, char **argv)
{
	can_SpeedParam_t can_saveCurr;
	uint8	mode;
	
	// saving current settings
	mode = can_unit_getMode();
	can_flexcan_getBitRate(&can_saveCurr);
	can_unit_Stop();

	printf("\nAutomatic CAN baudrate detection...\n");
	if (can_unit_Auto_Integration())
	{
		can_flexcan_getBitRate(&can_TempSet);
		printf("\n   To apply founded settings type 'speed apply'.\n");
	}
	else
	{
		printf("Autodetection FAILED.\n\nPossible reasons:\n");
		printf("Less than 1 TX and 1 RX node in network communicating each other,\n");
		printf("Non standart CAN bus speed or Physical connection issue.\n\n");
	}
	// return settings back
	can_flexcan_setBitRate(&can_saveCurr);
	if (mode == can_ModeRunFull) can_unit_Run();
}

void can_shl_speed_calc(struct shell_t * shl, int argc, char **argv)
{
	can_TempSet.CanSpeed = can_calcSpeedFromParams(&can_TempSet);
    
	printf("\n");
	printf("If the settings will be applied:\n\n");
	printf("   Baudrate %d kBit/s\n", can_TempSet.CanSpeed/1000);
	printf("   S-clock: %d MHz, PRESDIV: %d\n", (SYSTEM_CLOCK)/(2*(can_TempSet.PREScalerDIVision + 1)), can_TempSet.PREScalerDIVision);
	printf("   Sampling point: %d%%, BTL cycles: %d\n",  ((can_TempSet.NumberOfTQ - can_TempSet.PhaseSEG2)*100/can_TempSet.NumberOfTQ), can_TempSet.NumberOfTQ);
	printf(CAN_OUT_TBL_NAMES);
	printf(CAN_OUT_TBL_LINE);
	printf(CAN_OUT_TBL_FRMT, can_TempSet.PROPagationSEG, 
							 can_TempSet.PhaseSEG1,
							 can_TempSet.PhaseSEG2,
							 can_TempSet.ResyncJumpWidth,
							 can_TempSet.SAMPlingMode*2+1 );
	printf("\nIf you satisfy, type 'speed apply'\n");
}

void can_shl_speed_apply(struct shell_t * shl, int argc, char **argv)
{
	if (can_flexcan_setBitRate(&can_TempSet) == 0)
	{
		// OK
		can_unit_Run();
		printf("   Settings were sucessfully applied, unit started.\n");

		sys_update_CurrSettingsFromHW( &sys_flash_CurrSettings );
	   	sys_update_Flash( &sys_flash_CurrSettings );		
	}
	else
		printf("Error: Invalid settings to apply.\n");
}

/************************************************************************
* NAME: GROUP of MODE functions
*
* DESCRIPTION: Shows/Changes CAN unit mode (Run/Stop)
************************************************************************/
void can_shl_mode(struct shell_t * shl, int argc, char **argv)
{
	int index;

	switch (argc)
	{
		case 1:
			printf("\nCurrent CAN unit mode: ");
			switch ( can_unit_getMode() ) 
			{
				case can_ModeStop:     
					printf("Stoped\n"); 	  break;
				case can_ModeRunFull:  
					printf("Running Full\n"); break;
				default:
					printf("undefined\n");
			}
			printf("\nValid 'mode' options:\n");
			for (index = 0; index < CAN_SHELL_MODE_CMD_NUM; ++index)
			{
				printf(CAN_SHL_SET_OPT_FORMAT, can_shl_mode_cmd[index].option);
				printf("%s\n", can_shl_mode_cmd[index].syntax);
			}
			printf("\n");
			return;
		case 2:
			for (index = 0; index < CAN_SHELL_MODE_CMD_NUM; index++)
			{
				if (strcasecmp(can_shl_mode_cmd[index].option, argv[1]) == 0)
				{
					can_shl_mode_cmd[index].func(shl, argc, argv);
					return;
				}
			}
			printf(CAN_SHL_SET_OPT_ERR, argv[1]);
			return;
		default:
			printf("Error: Invalid argument list\n");
	}
}

void can_shl_mode_start(struct shell_t * shl, int argc, char **argv)
{
	if (can_unit_getMode() == can_ModeRunFull)
	{
		printf("   Unit already running.\n");
	}
	else
	{
		can_unit_Run();
		printf("   Unit started.\n");
	}
}

void can_shl_mode_stop(struct shell_t * shl, int argc, char **argv)
{
	if (can_unit_getMode() == can_ModeStop)
	{
		printf("   Unit already stopped.\n");
	}
	else
	{
		can_unit_Stop();
		printf("   Unit stopped.\n");
	}
}

/************************************************************************
* NAME: GROUP of AUTODT functions
*
* DESCRIPTION: Forse Startup time CAN baudrate detection ON/OFF
************************************************************************/
void can_shl_autodt(struct shell_t * shl, int argc, char **argv)
{
	int32 index;

	switch (argc)
	{
		case 1:
			printf("\nForce baudrate Autodetect at startup: %s\n", 
				((sys_flash_CurrSettings.CAN_SpeedForceAutodetect)?("ON"):("OFF")));
			printf("\nValid 'autodt' options:\n");
			for (index = 0; index < CAN_SHELL_AUTODT_CMD_NUM; ++index)
			{
				printf(CAN_SHL_SET_OPT_FORMAT, can_shl_autodt_cmd[index].option);
				printf("%s\n", can_shl_autodt_cmd[index].syntax);
			}
			printf("\n");
			return;
		case 2:
			for (index = 0; index < CAN_SHELL_AUTODT_CMD_NUM; index++)
			{
				if (strcasecmp(can_shl_autodt_cmd[index].option, argv[1]) == 0)
				{
					can_shl_autodt_cmd[index].func(shl, argc, argv);
					return;
				}
			}
			printf(CAN_SHL_SET_OPT_ERR, argv[1]);
			return;
		default:
			printf("Error: Invalid argument list\n");
	}
}

void can_shl_autodt_on(struct shell_t * shl, int argc, char **argv)
{
	sys_flash_CurrSettings.CAN_SpeedForceAutodetect = 1;
	
	sys_update_CurrSettingsFromHW( &sys_flash_CurrSettings );
   	sys_update_Flash( &sys_flash_CurrSettings );		
}

void can_shl_autodt_off(struct shell_t * shl, int argc, char **argv)
{
	sys_flash_CurrSettings.CAN_SpeedForceAutodetect = 0;
	
	sys_update_CurrSettingsFromHW( &sys_flash_CurrSettings );
   	sys_update_Flash( &sys_flash_CurrSettings );		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -