summaryrefslogtreecommitdiff
path: root/HisiPkg/Library/BspUartLib/BspUartLib.c
blob: 7c313d3001d4e55e2c8a64c4dd17c6748cec1703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************
#
#  
#  Copyright (c) Huawei Technologies Co., Ltd. 2013. All rights reserved.
#  This program and the accompanying materials
#  are licensed and made available under the terms and conditions of the BSD License
#  which accompanies this distribution.  The full text of the license may be found at
#  http://opensource.org/licenses/bsd-license.php
#  
#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#  
#**/


#include "BspUartLib.h"
#include "BrdCommon.h"
#include "config.h"
#include <Library/DebugLib.h>
#include <Library/ResetWdtLib.h>
#include <Library/DebugLib.h>


#define BSP_UartDelay(loop) delayuart(2 * loop)
void delayuart(unsigned long ulCount)
{
    unsigned int ulRet, ulNumber;
    for(ulRet = 0; ulRet < 2; ulRet++)
    {
        ulNumber = ulCount;
        while ( ulNumber-- )
        {
            ;
        }
    }
}


UINT8 ROM_UartChkSndEnd(void)
{
    if(!(*(volatile UINT8 *)UART_LSR_REG & UART_LSR_THRE))
    {
        WDT_ResetWatchdog();
        return 0;
    }
    else
    {
        return 1;
    }
}

void ROM_UartSendChar(UINT8 scShowChar)
{
    UINT32 i = 0;

    while(i < UART_SEND_DELAY)
    {
        WDT_ResetWatchdog();
        if ((*(volatile UINT8 *)(UART_USR_REG) & UART_USR_TFNF) == UART_USR_TFNF)
        {
            break;
        }
        i++;
    }

    *(volatile UINT8 *)(UART_THR_REG) = scShowChar;


    i = 0;
    while (i < UART_SEND_DELAY)
    {
        if (ROM_UartChkSndEnd())
        {
            break;
        }
        i++;
    }

    return;

}

void BspSendUintHex(register UINT32 ulData)
{
    INT8 Buff[8];
    INT32 i;
    UINT32 uTemp = 0x0f;

    for(i = 0; i < 8; i++)
    {
        Buff[i] = ((INT8)(ulData & uTemp));
        ulData = ulData >> 4;
    }

    ROM_UartSendChar('0');
    ROM_UartSendChar('x');

    for(i = 0; i < 8; i++)
    {
        if(Buff[7 - i] < (char)10)
        {
            Buff[7 - i] += '0';
            ROM_UartSendChar(Buff[7 - i]);
        }
        else
        {
            Buff[7 - i] = Buff[7 - i] - 10 + 'A';
            ROM_UartSendChar(Buff[7-i]);
        }
    }
    return;

}

void BspSendString(char *pShow)
{
    if( NULL == pShow)
    {
        return;
    }
    while( *((char *)pShow) )
    {
        WDT_ResetWatchdog();
        ROM_UartSendChar( * ( (char *) pShow ) );
        pShow++;
    }
    return;
}

char BspGetChar(UINT32 ulTimeOut)
{
    UINT32 i = 0;
    register UINT8 recvchar = 0;
    for(;;)
    {
        if ((*(UINT8 *)(UART_LSR_REG) & UART_LSR_DR) == UART_LSR_DR)
        {
            break;
        }
        WDT_ResetWatchdog();

        if (i > ulTimeOut)
        {
            WDT_ResetWatchdog();
            return recvchar;
        }
        
        BSP_UartDelay(10000);
        
        i++;
	}
    recvchar = (*(volatile UINT8 *)(UART_RBR_REG));

    *(volatile UINT8 *)(UART_THR_REG) = recvchar;

    return recvchar;
}