OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [tools/] [cpu/] [sh/] [sci.c] - Blame information for rev 851

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
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
 
23
typedef struct sci_tab {
24
  unsigned int  B ;
25
  unsigned int  n ;
26
  int           N ;
27
  double        err ;
28
  } sci_tab_t ;
29
 
30
static 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
 
52
static sci_tab_t test_array[4] ;
53
 
54
static 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
  {
71
    entry->err = 100.0 ;
72
    entry->n = 255 ;
73
    entry->N = 0 ;
74
  }
75
}
76
 
77
static sci_tab_t *SelectN(
78
  unsigned int  B,
79
  double        Phi )
80
{
81
  unsigned int i ;
82
  struct sci_tab* best = NULL ;
83
 
84
  for ( i = 0 ; i < 4 ; i++ )
85
  {
86
    double err ;
87
 
88
    Compute( i, B, Phi, &test_array[i] );
89
    err = fabs( test_array[i].err );
90
 
91
    if ( best )
92
    {
93
      if ( err < fabs( best->err ) )
94
        best = &test_array[i] ;
95
    }
96
    else
97
      best = &test_array[i] ;
98
  }
99
 
100
  return best ;
101
}
102
 
103
int shgen_gensci(
104
  FILE          *file,
105
  double        Phi ) /* Processor frequency [Hz] */
106
{
107
  unsigned int i ;
108
 
109
  fprintf( file,
110
    "/*\n * Bitrate table for the serial devices (sci) of the SH at %.3f MHz\n"
111
    " */\n\n", Phi / 1000000.0 );
112
  fprintf( file,
113
    "/*\n"
114
    " * n     .. SMR bits 0,1 : baud rate generator clock source\n"
115
    " * N     .. BRR bits 0..7: setting for baud rate generator\n"
116
    " * error .. percentual error to nominal bitrate\n"
117
    " *   Hitachi's HW manual recommends bitrates with an error less than 1%%\n"
118
    " *   We experienced values less than 2%% to be stable\n"
119
    " */\n\n" );
120
  fprintf( file, "#include <termios.h>\n\n" );
121
  fprintf( file,
122
    "static struct sci_bitrate_t {\n"
123
    "  unsigned char n ;\n"
124
    "  unsigned char N ;\n"
125
    "} _sci_bitrates[] = {\n"
126
    "/*  n    N      error */\n" );
127
 
128
  for ( i = 0 ; i < sizeof(bitrate)/sizeof(int) ; i++ )
129
  {
130
    struct sci_tab* best = SelectN( bitrate[i], Phi );
131
 
132
    if ( i > 0 )
133
      fprintf( file, ",\n" );
134
    fprintf( file, "  { %1d, %3d } /* %+7.2f%% ; B%d ",
135
      best->n,
136
      best->N,
137
      best->err,
138
      best->B );
139
    if ( best->n > 3 )
140
      fprintf( file, "(unusable) " );
141
    fprintf( file, "*/" );
142
  }
143
 
144
  fprintf( file, "\n};\n\n" );
145
 
146
  fprintf( file,
147
    "int _sci_get_brparms( \n"
148
    "  tcflag_t      cflag,\n"
149
    "  unsigned char *smr,\n"
150
    "  unsigned char *brr )\n"
151
    "{\n"
152
    "  unsigned int offset ;\n\n"
153
    "  offset = ( cflag & ( CBAUD & ~CBAUDEX ) )\n"
154
    "    + ( ( cflag & CBAUDEX ) ? B38400 : 0 );\n"
155
    "  if ( offset == 0 ) return -1 ;\n"
156
    "  offset-- ;\n\n"
157
    "  if ( _sci_bitrates[offset].n > 3 )  return -1;\n\n"
158
    "  *smr &= ~0x03;\n"
159
    "  *smr |= _sci_bitrates[offset].n;\n"
160
    "  *brr =  _sci_bitrates[offset].N;\n\n"
161
    "  return 0;\n"
162
    "}\n" );
163
 
164
  return 0 ;
165
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.