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