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