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