drivers/ecos_lpc2138_sja1000/build_baudrate.c
changeset 93 16c8ceea8f18
parent 92 0d84d95790d9
child 94 bdf4c86be6b2
equal deleted inserted replaced
92:0d84d95790d9 93:16c8ceea8f18
     1 #include <stdio.h>
       
     2 #include <stdlib.h>
       
     3 
       
     4 
       
     5 /*
       
     6 	CiA DS-301, p.20
       
     7 */
       
     8 #define MAX_BIT_TIMING 9
       
     9 int table_bit_timing_settings[MAX_BIT_TIMING][3] =
       
    10 {
       
    11 	{1000, 8, 6}, /* baudrate, number of time quanta per bit, tsync+tseg1 */
       
    12 	{800, 10, 8},
       
    13 	{500, 16, 14},
       
    14 	{250, 16, 14},
       
    15 	{125, 16, 14},
       
    16 	{100, 16, 14},
       
    17 	{ 50, 16, 14},
       
    18 	{ 25, 16, 14},
       
    19 	{ 10, 16, 14}
       
    20 };
       
    21 
       
    22 
       
    23 void can_timing_registers(double f, int *v)
       
    24 /* fill the vector v with the proper setting for TIMER 0 and TIMER 1
       
    25    regarding the clock and the baudrate */
       
    26 {
       
    27 	int i;
       
    28 
       
    29 	int BRP, TSEG1, TSEG2;
       
    30 
       
    31 	double nominal, tq, tscl;
       
    32 
       
    33 	double tclk = 1 / (f*1e6); /* sec */
       
    34 
       
    35 	for(i=0; i<MAX_BIT_TIMING; i++)
       
    36 	{
       
    37 		nominal = 1. / (table_bit_timing_settings[i][0]*1e3); /* nominal bit time */
       
    38 
       
    39 		tq = nominal / table_bit_timing_settings[i][1]; /* time quanta */
       
    40 
       
    41 		tscl = tq; /* Tsyncseg = Tscl (ref. SJA 1000 datasheet, p.51) */
       
    42 
       
    43 		/* tcsl = 2 tclk (BRP + 1) */
       
    44 		BRP = (int)(tscl / (2. * tclk) - 1.);
       
    45 
       
    46 		/* 
       
    47 			BRT0 = SJW * 64 + BRP 
       
    48 			SJW : synchonisation jump Width, user defined (0..3)
       
    49 		*/
       
    50 
       
    51 		/* tseg1 = tscl * (TSEG1 + 1) */
       
    52 		TSEG1 = (int)((table_bit_timing_settings[i][2] - 1)*tq/tscl - 1.);
       
    53 
       
    54 		/* tseg2 = tscl * (TSEG2 + 1) */
       
    55 		TSEG2 = (int)(2.*tq/tscl - 1.);
       
    56 
       
    57 		/*
       
    58 			BRT1 = SAM*128 + TSEG2*16 + TSEG1
       
    59 			SAM = Sampling (0 = single sampling, 1 = triple sampling)
       
    60 		*/
       
    61 
       
    62 		if (BRP < 64)
       
    63 		{
       
    64 			v[2*i] = BRP;
       
    65 			v[2*i+1] = TSEG1+16*TSEG2;
       
    66 		}
       
    67 		else
       
    68 		{
       
    69 			v[2*i] = 0;
       
    70 			v[2*i+1] = 0;
       
    71 		}
       
    72 	}
       
    73 }
       
    74 
       
    75 
       
    76 void print_header(double frequency, /* clock rate in MHz*/
       
    77                   int    sjw)       /* 0..3 */
       
    78 {
       
    79 	int i, array[18];
       
    80 	FILE *f = fopen("baudrate_table.h", "w");
       
    81 
       
    82 	can_timing_registers(frequency, array);
       
    83 
       
    84 	fprintf(f, "#if !defined(_BAUDRATE_TABLE_H_)\n");
       
    85 	fprintf(f, "#define _BAUDRATE_TABLE_H_\n");
       
    86 	fprintf(f, "\n");
       
    87 	fprintf(f, "/*\n");
       
    88 	fprintf(f, "this file must be include only once in the project\n");
       
    89 	fprintf(f, "*/\n");
       
    90 	fprintf(f, "\n");
       
    91 	fprintf(f, "/* clock speed in MHz */\n");
       
    92 	fprintf(f, "#define CAN_CONTROLER_CLOCK_SPEED %.3lf\n", frequency);
       
    93 	fprintf(f, "#define CAN_CONTROLER_PHASE_SHIFT_TOLERANCE %d\n", sjw);
       
    94 
       
    95 	sjw = sjw << 6;
       
    96 
       
    97 	fprintf(f, "\n");
       
    98 	fprintf(f, "static int can_baudrate_registers[9][3] =\n");
       
    99 	fprintf(f, "{  /* BTR0    BTR1 */\n");
       
   100 
       
   101 	for(i=0; i<MAX_BIT_TIMING; i++)
       
   102 	{
       
   103 		if (array[2*i] == 0  && array[2*i+1] == 0)
       
   104 			fprintf(f, "    {0, 0x00, 0x00}, /* %4d kbits/s -- out of range*/\n", 
       
   105 				table_bit_timing_settings[i][0]);
       
   106 		else
       
   107 			fprintf(f, "    {1, 0x%02x, 0x%02x}, /* %4d kbits/s */\n", 
       
   108 				sjw|array[2*i], array[2*i+1],
       
   109 				table_bit_timing_settings[i][0]);
       
   110 	}
       
   111 
       
   112 	fprintf(f, "};\n");
       
   113 	fprintf(f, "\n");
       
   114 	fprintf(f, "#endif\n\n");
       
   115 
       
   116 	fclose(f);
       
   117 }
       
   118 
       
   119 
       
   120 int main(int argc, char *argv[])
       
   121 {	
       
   122 	if (argc == 3)
       
   123 		print_header(atof(argv[1]), atoi(argv[2]));
       
   124 	else
       
   125 		printf("usage: %s clock_in_Mhz error_rate_in_percent\n", argv[0]);
       
   126 }