summaryrefslogtreecommitdiff
path: root/bsp/bluetrum/libraries/hal_libraries/ab32vg1_hal/source/ab32vg1_hal_uart.c
blob: 7f1f9cd6ffd28b5877c5eb8916069495d8643938 (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
211
212
213
214
215
216
217
/*
 * Copyright (c) 2020-2020, BLUETRUM Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "ab32vg1_hal.h"

#ifdef HAL_UART_MODULE_ENABLED

enum
{
    UARTxCON = 0x00,
    UARTxCPND,
    UARTxBAUD,
    UARTxDATA,
};

/**
 * @brief Set the UART baud rate.
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 * @param baud Baud rate.
 */
void hal_uart_setbaud(hal_sfr_t uartx, uint32_t baud)
{
    uint32_t baud_cfg;

    uartx[UARTxCON] |= UART_CLK_SRC1;
    baud_cfg = (26000000/2)/baud;
    uartx[UARTxBAUD] = (baud_cfg << 16) | baud_cfg;
}

/**
 * @brief Set the UART misc paramter.
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 * @param param uart config paramter pointer.
 */
void hal_uart_setparam(hal_sfr_t uartx, struct uart_init *param)
{
    switch (param->word_len)
    {
    case UART_WORDLENGTH_8B:
        uartx[UARTxCON] &= ~UART_BIT9_ENABLE;
        break;
    case UART_WORDLENGTH_9B:
        uartx[UARTxCON] |= UART_BIT9_ENABLE;
        break;
    default:
        break;
    }

    switch (param->stop_bits)
    {
    case UART_STOPBITS_1:
        uartx[UARTxCON] &= ~UART_SB2_ENABLE;
        break;
    case UART_STOPBITS_2:
        uartx[UARTxCON] |= UART_SB2_ENABLE;
        break;
    default:
        break;
    }

    if (param->mode & UART_MODE_1LINE)
    {
        uartx[UARTxCON] |= UART_1LINE_ENABLE;
    }
    else
    {
        uartx[UARTxCON] &= ~UART_1LINE_ENABLE;
    }
}
/**
 * @brief Initialize the UART mode.
 *
 * @param huart UART handle.
 * @return hal_error_t
 */
hal_error_t hal_uart_init(struct uart_handle *huart)
{
    if (huart == HAL_NULL) {
        return -HAL_ERROR;
    }

    hal_uart_mspinit(huart);
    uart_config_all(huart);

    return HAL_EOK;
}

/**
 * @brief DeInitialize the UART peripheral.
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 */
void hal_uart_deinit(hal_sfr_t uartx)
{
    uartx[UARTxCON] = 0;
}

/**
 * @brief Initialize the UART MSP.
 *
 * @param huart UART handle.
 */
WEAK void HAL_UART_MspInit(struct uart_handle *huart)
{}

/**
 * @brief Control the UART peripheral.
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 * @param cntl
 *      @arg UART_MODULE_ENABLE
 *      @arg UART_BIT9_ENABLE
 *      @arg UART_RXIT_ENABLE
 *      @arg UART_TXIT_ENABLE
 *      @arg UART_SB2_ENABLE
 *      @arg UART_CLK_SRC1
 *      @arg UART_1LINE_ENABLE
 *      @arg UART_RX_ENABLE
 * @param param
 *      @arg HAL_DISABLE
 *      @arg HAL_ENABLE
 */
void hal_uart_control(hal_sfr_t uartx, uint32_t cntl, uint32_t param)
{
    if (param == HAL_ENABLE) {
        uartx[UARTxCON] |= (cntl);
    } else {
        uartx[UARTxCON] &= ~(cntl);
    }
}

/**
 * @brief Send a character
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 * @param data The characters that need to be sent
 */
void hal_uart_write(hal_sfr_t uartx, uint8_t data)
{
    uartx[UARTxDATA] = data;
}

/**
 * @brief Receive a character.
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 * @return uint8_t Received character.
 */
uint8_t hal_uart_read(hal_sfr_t uartx)
{
    return (uartx[UARTxDATA] & 0xff);
}

/**
 * @brief Get the UART flag.
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 * @param flag
 *      @arg UART_FLAG_RXPND
 *      @arg UART_FLAG_TXPND
 * @return uint32_t
 */
uint32_t hal_uart_getflag(hal_sfr_t uartx, uint32_t flag)
{
    uint32_t ret = uartx[UARTxCON] & flag;
    return ret;
}

/**
 * @brief Clear the UART flag.
 *
 * @param uartx This parameter can be UARTxN where x can be (0.2).
 * @param flag
 *      @arg UART_FLAG_RXPND
 *      @arg UART_FLAG_TXPND
 */
void hal_uart_clrflag(hal_sfr_t uartx, uint32_t flag)
{
    uartx[UARTxCPND] |= flag;
}

/**
 * @brief Configure the UART peripheral.
 *
 * @param huart UART handle.
 */
void uart_config_all(struct uart_handle *huart)
{
    hal_uart_control(huart->instance, UART_MODULE_ENABLE, HAL_DISABLE);

    CLKCON1 |= BIT(14);
    if (huart->instance == UART0_BASE) {
        hal_rcu_periph_clk_enable(RCU_UART0);
    } else if (huart->instance == UART1_BASE) {
        hal_rcu_periph_clk_enable(RCU_UART1);
    } else if (huart->instance == UART2_BASE) {
        hal_rcu_periph_clk_enable(RCU_UART2);
    } else {
        return; /* Not support! */
    }

    hal_uart_deinit(huart->instance);
    hal_uart_setbaud(huart->instance, huart->init.baud);
    hal_uart_setparam(huart->instance, &huart->init);

    if (huart->init.mode != UART_MODE_TX) {
        hal_uart_control(huart->instance, UART_RX_ENABLE, HAL_ENABLE);
    }
    hal_uart_control(huart->instance, UART_MODULE_ENABLE, HAL_ENABLE);
}

#endif