this commit won't work :(
[pyfrprog.git] / pkernel / uart.c
1 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
2 /* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
3 /* ELIGIBILITY FOR ANY PURPOSES.                                             */
4 /*                 (C) Fujitsu Microelectronics Europe GmbH                  */
5 /*---------------------------------------------------------------------------*/
6
7 #include "mb91465k.h"
8
9 const char ASCII[] = "0123456789ABCDEF";
10
11 void InitUart4(void)
12 {
13   // Initialize UART asynchronous mode
14   // BGR04 = 1666; //  9600 Baud @ 16MHz
15   // BGR04 = 832;  // 19200 Baud @ 16MHz
16   BGR04 = 416;  // 38400 Baud @ 16MHz
17
18   // BGR04 = 2083; //  9600 Baud @ 20MHz
19   // BGR04 = 1041; // 19200 Baud @ 20MHz
20   // BGR04 = 520;  // 38400 Baud @ 20MHz
21
22   // BGR04 = 2499; //  9600 Baud @ 24MHz
23   // BGR04 = 1249; // 19200 Baud @ 24MHz
24   // BGR04 = 624;  // 38400 Baud @ 24MHz  
25   SCR04 = 0x17;    // 8N1
26   SMR04 = 0x0d;    // enable SOT3, Reset, normal mode
27   SSR04 = 0x00;    // LSB first
28   PFR19 = (PFR19 & 0xFC) | 0x03;        // enable UART: SIN,SOT for async. transfer
29   EPFR19 = 0x00;   // enable UART
30 }
31
32 void Putch4(unsigned char ch)         /* sends a char */
33 {
34   while (SSR04_TDRE == 0);    /* wait for transmit buffer empty         */
35   TDR04 = ch;                 /* put ch into buffer                     */
36 }
37
38 unsigned char Getch4(void)            /* waits for and returns incomming char   */
39 {
40   volatile unsigned ch;
41
42   for(;;)
43   {
44   
45      while(SSR04_RDRF == 0)      /* wait for data received      */
46        HWWD = 0x00; 
47   
48      ch = RDR04;
49   
50      if ((SSR04 & 0xE0) != 0)    /* Check for errors PE, ORE, FRE */
51      {
52          SCR04_CRE = 1;            /* Clear error flags         */
53      }
54      else   
55         return (ch);            /* return char                  */
56   }
57 }
58
59
60 void Puts4(const char *Name2)  /* Puts a String to UART */
61 {
62   volatile int i,len;
63    
64   len = strlen(Name2);
65         
66   for (i=0; i<strlen(Name2); i++)   /* go through string                     */
67   {
68     if (Name2[i] == 10)
69       Putch4(13);
70     Putch4(Name2[i]);              /* send it out                           */
71   }
72 }
73
74
75 char Echo4(void)        /* Echo UART and return ch */
76 {
77   char ch;
78   
79   Puts4("UART 4 receive: ");      // send text to UART
80   ch = RDR04;                      // readout character              
81   Putch4(ch);                          // send character to UART                 
82   if (ch==13)   
83     Putch4(10);
84
85   return (ch);
86 }
87
88 void Puthex4(unsigned long n, unsigned char digits)
89 {
90    unsigned char digit=0,div=0,i;
91
92    div=(4*(digits-1));  /* init shift divisor */
93    for (i=0;i<digits;i++)
94    {
95      digit = ((n >> div)&0xF); /* get hex-digit value */
96          Putch4(digit + ((digit < 0xA) ? '0' : 'A' - 0xA));
97      div-=4;                    /* next digit shift */
98    }
99 }
100
101 void Putdec4(unsigned long x, int digits)
102 {
103         int i;
104         char buf[10],sign=1;
105         
106         if (digits < 0) {     /* should be print of zero? */
107           digits *= (-1);
108           sign =1;
109         }  
110         buf[digits]='\0';                       /* end sign of string */
111         
112         for (i=digits; i>0; i--) {
113                 buf[i-1] = ASCII[x % 10];
114                 x = x/10;
115         }
116
117     if ( sign )
118     {
119           for (i=0; buf[i]=='0'; i++) { /* no print of zero */
120                 if ( i<digits-1)
121                         buf[i] = ' ';
122           }             
123     }
124     
125         Puts4(buf);                                     /* send string */
126 }
127