source: rtems/c/src/exec/score/tools/sh/sci.c @ 1fd26a5

4.104.114.84.95
Last change on this file since 1fd26a5 was 1fd26a5, checked in by Joel Sherrill <joel.sherrill@…>, on 09/23/98 at 16:46:52

Patch from Ralf Corsepius <corsepiu@…>.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 * Copyright (c) 1998 Ralf Corsepius (corsepiu@faw.uni-ulm.de)
3 *
4 * See the file COPYING for copyright notice.
5 */
6
7#include <math.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11#include "sci.h"
12
13/*
14   n .. baudrate generator source 0,1,2,3
15   
16   N .. BRR setting (0..255)
17   
18   Phi .. processor baud rate
19   
20   B .. bitrate
21 */
22
23typedef struct sci_tab {
24  unsigned int  B ;
25  unsigned int  n ;
26  int           N ;
27  double        err ;
28  } sci_tab_t ;
29
30static unsigned int bitrate [] = {
31  50,
32  75,
33  110,
34  134,
35  150,
36  200,
37  300,
38  600,
39  1200,
40  1800,
41  2400,
42  4800,
43  9600,
44  19200,
45  38400,
46  57600,
47  115200,
48  230400,
49  460800
50};
51
52static sci_tab_t test_array[4] ;
53
54static void Compute(
55  unsigned int n,
56  unsigned int B,
57  double   Phi,
58  struct sci_tab *entry )
59{
60  int    a = ( 32 << ( 2 * n ) ) * B ;
61 
62  entry->n = n ;
63  entry->B = B ;
64  entry->N = rint( ( Phi / a ) - 1.0 ) ;
65
66  if ( ( entry->N > 0 ) && ( entry->N < 256 ) )
67    entry->err =
68      ( ( Phi / ( (entry->N + 1) * a ) - 1.0 ) * 100.0 );
69  else
70    entry->err = 100.0 ;
71}
72
73static sci_tab_t *SelectN(
74  unsigned int  B,
75  double        Phi )
76{
77  unsigned int i ;
78  struct sci_tab* best = NULL ;
79   
80  for ( i = 0 ; i < 4 ; i++ )
81  {
82    double err ;
83   
84    Compute( i, B, Phi, &test_array[i] );
85    err = fabs( test_array[i].err );
86
87    if ( best )
88    {
89      if ( err < fabs( best->err ) )
90        best = &test_array[i] ;
91    }
92    else
93      best = &test_array[i] ;
94  }
95
96  return best ;
97}
98
99int shgen_gensci(
100  FILE          *file,
101  double        Phi ) /* Processor frequency [Hz] */
102{
103  unsigned int i ;
104
105  fprintf( file,
106    "/*\n * Bitrate table for the serial devices (sci) of the SH at %.3f MHz\n"
107    " */\n\n", Phi / 1000000.0 );
108  fprintf( file,
109    "/*\n"
110    " * n     .. SMR bits 0,1 : baud rate generator clock source\n"
111    " * N     .. BRR bits 0..7: setting for baud rate generator\n"
112    " * error .. percentual error to nominal bitrate\n"
113    " *   Hitachi's HW manual recommends bitrates with an error less than 1%%\n"
114    " *   We experienced values less than 2%% to be stable\n"
115    " */\n\n" );
116  fprintf( file, "#include <termios.h>\n\n" );
117  fprintf( file,
118    "static struct sci_bitrate_t {\n"
119    "  unsigned char n ;\n"
120    "  unsigned char N ;\n"
121    "} _sci_bitrates[] = {\n"
122    "/*  n    N      error */\n" );
123 
124  for ( i = 0 ; i < sizeof(bitrate)/sizeof(int) ; i++ )
125  {
126    struct sci_tab* best = SelectN( bitrate[i], Phi );
127
128    if ( i > 0 )
129      fprintf( file, ",\n" );
130    fprintf( file, "  { %1d, %3d } /* %+7.2f%% ; B%d */",
131      best->n,
132      best->N,
133      best->err,
134      best->B );
135  }
136
137  fprintf( file, "\n};\n\n" );
138
139  fprintf( file,
140    "int _sci_get_brparms( \n"
141    "  tcflag_t      cflag,\n"
142    "  unsigned char *smr,\n"
143    "  unsigned char *brr )\n"
144    "{\n"
145    "  unsigned int offset ;\n\n"
146    "  offset = ( cflag & ( CBAUD & ~CBAUDEX ) )\n"
147    "    + ( ( cflag & CBAUDEX ) ? B38400 : 0 );\n"
148    "  if ( offset == 0 ) return -1 ;\n"
149    "  offset-- ;\n\n"
150    "  *smr &= ~0x03;\n"
151    "  *smr |= _sci_bitrates[offset].n;\n"
152    "  *brr =  _sci_bitrates[offset].N;\n\n"
153    "  return 0;\n"
154    "}\n" );
155   
156  return 0 ;
157}
Note: See TracBrowser for help on using the repository browser.