summaryrefslogtreecommitdiff
path: root/bsp/synopsys/emsk_em9d/drivers/dw_uart_obj.c
blob: 9e3ebd3db90179892278d812bedf3fb04944446e (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright (c) 2018, Synopsys, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#include "inc/arc/arc.h"
#include "inc/arc/arc_builtin.h"
#include "inc/embARC_toolchain.h"
#include "inc/embARC_error.h"

#include "inc/arc/arc_exception.h"
#include "device/designware/uart/dw_uart.h"
#include "dw_uart_obj.h"

#include "emsk_hardware.h"

#define DW_UART_FIFO_LEN    32

/**
 * \name    EMSK DesignWare UART 0 Object Instantiation
 * @{
 */
#if (USE_DW_UART_0)
static void dw_uart_0_isr(void *ptr);
#define DW_UART_0_RELBASE   (REL_REGBASE_UART0) /*!< designware uart 0 relative baseaddr */
#define DW_UART_0_INTNO     (INTNO_UART0)       /*!< designware uart 0 interrupt number  */

DEV_UART        dw_uart_0;          /*!< designware uart object */
DW_UART_CTRL    dw_uart_0_ctrl = {          /*!< designware uart 0 ctrl */
    0, CLK_BUS_APB, DW_UART_0_INTNO, (INT_HANDLER)dw_uart_0_isr,
    DW_UART_FIFO_LEN, DW_UART_FIFO_LEN, 0
};

/** designware uart 0 open */
static int32_t dw_uart_0_open (uint32_t baud)
{
    return dw_uart_open(&dw_uart_0, baud);
}
/** designware uart 0 close */
static int32_t dw_uart_0_close (void)
{
    return dw_uart_close(&dw_uart_0);
}
/** designware uart 0 control */
static int32_t dw_uart_0_control (uint32_t ctrl_cmd, void *param)
{
    return dw_uart_control(&dw_uart_0, ctrl_cmd, param);
}
/** designware uart 0 write */
static int32_t dw_uart_0_write (const void *data, uint32_t len)
{
    return dw_uart_write(&dw_uart_0, data, len);
}
/** designware uart 0 close */
static int32_t dw_uart_0_read (void *data, uint32_t len)
{
    return dw_uart_read(&dw_uart_0, data, len);
}
/** designware uart 0 interrupt rountine */
static void dw_uart_0_isr(void *ptr)
{
    dw_uart_isr(&dw_uart_0, ptr);
}
/** install designware uart 0 to system */
static void dw_uart_0_install(void)
{
    uint32_t uart_abs_base = 0;
    DEV_UART *dw_uart_ptr = &dw_uart_0;
    DEV_UART_INFO *dw_uart_info_ptr = &(dw_uart_0.uart_info);
    DW_UART_CTRL *dw_uart_ctrl_ptr = &dw_uart_0_ctrl;

    /**
     * get absolute designware base address
     */
    uart_abs_base = (uint32_t)PERIPHERAL_BASE + DW_UART_0_RELBASE;
    dw_uart_ctrl_ptr->dw_uart_regbase = uart_abs_base;

    /** uart info init */
    dw_uart_info_ptr->uart_ctrl = (void *)dw_uart_ctrl_ptr;
    dw_uart_info_ptr->opn_cnt = 0;
    dw_uart_info_ptr->status = 0;
    dw_uart_info_ptr->baudrate = UART_BAUDRATE_115200;  /* default 115200bps */

    /** uart dev init */
    dw_uart_ptr->uart_open = dw_uart_0_open;
    dw_uart_ptr->uart_close = dw_uart_0_close;
    dw_uart_ptr->uart_control = dw_uart_0_control;
    dw_uart_ptr->uart_write = dw_uart_0_write;
    dw_uart_ptr->uart_read = dw_uart_0_read;

}
#endif /* USE_DW_UART_0 */
/** @} end of name */

/**
 * \name    EMSK DesignWare UART 1 Object Instantiation
 * @{
 */
#if (USE_DW_UART_1)
static void dw_uart_1_isr(void *ptr);
#define DW_UART_1_RELBASE   (REL_REGBASE_UART1) /*!< designware uart 1 relative baseaddr */
#define DW_UART_1_INTNO     (INTNO_UART1)       /*!< designware uart 1 interrupt number  */

DEV_UART        dw_uart_1;          /*!< designware uart 1 object */
DW_UART_CTRL    dw_uart_1_ctrl = {          /*!< designware uart 1 ctrl */
    0, CLK_BUS_APB, DW_UART_1_INTNO, (INT_HANDLER)dw_uart_1_isr,
    DW_UART_FIFO_LEN, DW_UART_FIFO_LEN, 0
};

/** designware uart 1 open */
static int32_t dw_uart_1_open (uint32_t baud)
{
    return dw_uart_open(&dw_uart_1, baud);
}
/** designware uart 1 close */
static int32_t dw_uart_1_close (void)
{
    return dw_uart_close(&dw_uart_1);
}
/** designware uart 1 control */
static int32_t dw_uart_1_control (uint32_t ctrl_cmd, void *param)
{
    return dw_uart_control(&dw_uart_1, ctrl_cmd, param);
}
/** designware uart 1 write */
static int32_t dw_uart_1_write (const void *data, uint32_t len)
{
    return dw_uart_write(&dw_uart_1, data, len);
}
/** designware uart 1 close */
static int32_t dw_uart_1_read (void *data, uint32_t len)
{
    return dw_uart_read(&dw_uart_1, data, len);
}
/** designware uart 1 interrupt routine */
static void dw_uart_1_isr(void *ptr)
{
    dw_uart_isr(&dw_uart_1, ptr);
}
/** install designware uart 1 to system */
static void dw_uart_1_install(void)
{
    uint32_t uart_abs_base = 0;
    DEV_UART *dw_uart_ptr = &dw_uart_1;
    DEV_UART_INFO *dw_uart_info_ptr = &(dw_uart_1.uart_info);
    DW_UART_CTRL *dw_uart_ctrl_ptr = &dw_uart_1_ctrl;

    /**
     * get absolute designware base address
     */
    uart_abs_base = (uint32_t)PERIPHERAL_BASE + DW_UART_1_RELBASE;
    dw_uart_ctrl_ptr->dw_uart_regbase = uart_abs_base;

    /** uart info init */
    dw_uart_info_ptr->uart_ctrl = (void *)dw_uart_ctrl_ptr;
    dw_uart_info_ptr->opn_cnt = 0;
    dw_uart_info_ptr->status = 0;
    dw_uart_info_ptr->baudrate = UART_BAUDRATE_115200;  /* default 115200bps */

    /** uart dev init */
    dw_uart_ptr->uart_open = dw_uart_1_open;
    dw_uart_ptr->uart_close = dw_uart_1_close;
    dw_uart_ptr->uart_control = dw_uart_1_control;
    dw_uart_ptr->uart_write = dw_uart_1_write;
    dw_uart_ptr->uart_read = dw_uart_1_read;
}
#endif /* USE_DW_UART_1 */
/** @} end of name */

/** get one designware device structure */
DEV_UART_PTR uart_get_dev(int32_t uart_id)
{
    static uint32_t install_flag = 0;

    /* intall device objects */
    if (install_flag == 0) {
        install_flag = 1;
        dw_uart_all_install();
    }

    switch (uart_id) {
#if (USE_DW_UART_0)
        case DW_UART_0_ID:
            return &dw_uart_0;
            break;
#endif
#if (USE_DW_UART_1)
        case DW_UART_1_ID:
            return &dw_uart_1;
            break;
#endif
        default:
            break;
    }
    return NULL;
}

/**
 * \brief   install all uart objects
 * \note    \b MUST be called during system init
 */
void dw_uart_all_install(void)
{
#if (USE_DW_UART_0)
    dw_uart_0_install();
#endif
#if (USE_DW_UART_1)
    dw_uart_1_install();
#endif
}