source: rtems/tools/cpu/sh/sci.c @ 94a4865

5
Last change on this file since 94a4865 was 1c6926c1, checked in by Kevin Kirspel <kevin-kirspel@…>, on 03/21/17 at 19:39:48

termios: Synchronize with latest FreeBSD headers

Adding modified FreeBSD headers to synchronize RTEMS termios with
FreeBSD. Modify termios to support dedicated input and output baud for
termios structure. Updated BSPs to use dedicated input and output baud
in termios structure. Updated tools to use dedicated input and output
baud in termios structure. Updated termios testsuites to use dedicated
input and output baud in termios structure.

Close #2897.

  • Property mode set to 100644
File size: 3.5 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  7200,
47  14400,
48  28800,
49  57600,
50  76800,
51  115200,
52  230400,
53  460800,
54  921600
55};
56
57static sci_tab_t test_array[4] ;
58
59static void Compute(
60  unsigned int n,
61  unsigned int B,
62  double   Phi,
63  struct sci_tab *entry )
64{
65  int    a = ( 32 << ( 2 * n ) ) * B ;
66
67  entry->n = n ;
68  entry->B = B ;
69  entry->N = rint( ( Phi / a ) - 1.0 ) ;
70
71  if ( ( entry->N > 0 ) && ( entry->N < 256 ) )
72    entry->err =
73      ( ( Phi / ( (entry->N + 1) * a ) - 1.0 ) * 100.0 );
74  else
75  {
76    entry->err = 100.0 ;
77    entry->n = 255 ;
78    entry->N = 0 ;
79  }
80}
81
82static sci_tab_t *SelectN(
83  unsigned int  B,
84  double        Phi )
85{
86  unsigned int i ;
87  struct sci_tab* best = NULL ;
88
89  for ( i = 0 ; i < 4 ; i++ )
90  {
91    double err ;
92
93    Compute( i, B, Phi, &test_array[i] );
94    err = fabs( test_array[i].err );
95
96    if ( best )
97    {
98      if ( err < fabs( best->err ) )
99        best = &test_array[i] ;
100    }
101    else
102      best = &test_array[i] ;
103  }
104
105  return best ;
106}
107
108int shgen_gensci(
109  FILE          *file,
110  double        Phi ) /* Processor frequency [Hz] */
111{
112  unsigned int i ;
113
114  fprintf( file,
115    "/*\n * Bitrate table for the serial devices (sci) of the SH at %.3f MHz\n"
116    " */\n\n", Phi / 1000000.0 );
117  fprintf( file,
118    "/*\n"
119    " * n     .. SMR bits 0,1 : baud rate generator clock source\n"
120    " * N     .. BRR bits 0..7: setting for baud rate generator\n"
121    " * error .. percentual error to nominal bitrate\n"
122    " *   Hitachi's HW manual recommends bitrates with an error less than 1%%\n"
123    " *   We experienced values less than 2%% to be stable\n"
124    " */\n\n" );
125  fprintf( file, "#include <bsp.h>\n" );
126  fprintf( file, "#include <termios.h>\n\n" );
127  fprintf( file,
128    "static struct sci_bitrate_t {\n"
129    "  unsigned char n ;\n"
130    "  unsigned char N ;\n"
131    "  speed_t       B ;\n"
132    "} _sci_bitrates[] = {\n"
133    "/*  n    N      B      error */\n" );
134
135  for ( i = 0 ; i < sizeof(bitrate)/sizeof(int) ; i++ )
136  {
137    struct sci_tab* best = SelectN( bitrate[i], Phi );
138
139    if ( i > 0 )
140      fprintf( file, ",\n" );
141      fprintf( file, "  { %1d, %3d, %d } /* %+7.2f%% ; B%d ",
142      best->n,
143      best->N,
144      best->B,
145      best->err,
146      best->B );
147    if ( best->n > 3 )
148      fprintf( file, "(unusable) " );
149    fprintf( file, "*/" );
150  }
151
152  fprintf( file, "\n};\n\n" );
153
154  fprintf( file,
155    "int _sci_get_brparms( \n"
156    "  speed_t        spd,\n"
157    "  unsigned char *smr,\n"
158    "  unsigned char *brr )\n"
159    "{\n"
160    "  int offset = -1;\n"
161    "  int i;\n\n"
162    "  for(i = 0; i < sizeof(_sci_bitrates)/sizeof(_sci_bitrates[0]); i++) {\n"
163    "    if( _sci_bitrates[i].B == spd ) {\n"
164    "      offset = i;\n"
165    "      break;\n"
166    "    }\n"
167    "  }\n"
168    "  if ( offset == -1 ) return -1 ;\n"
169    "  if ( _sci_bitrates[offset].n > 3 )  return -1;\n\n"
170    "  *smr &= ~0x03;\n"
171    "  *smr |= _sci_bitrates[offset].n;\n"
172    "  *brr =  _sci_bitrates[offset].N;\n\n"
173    "  return 0;\n"
174    "}\n" );
175
176  return 0 ;
177}
Note: See TracBrowser for help on using the repository browser.