yincheng.zhong
2024-08-20 7744fffacb03dc81cc9dbaf9f5d86a0f21e79c03
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#ifndef MK_UART_H_
#define MK_UART_H_
#include "mk_common.h"
#include "mk_dma.h"
 
#ifndef UART_INT_MODE_EN
#define UART_INT_MODE_EN (1)
#endif
 
#ifndef UART_DMA_MODE_EN
#define UART_DMA_MODE_EN (1)
#endif
 
#ifndef UART_POLL_MODE_EN
#define UART_POLL_MODE_EN (1)
#endif
 
/**
 * @addtogroup MK8000_UART
 * @{
 */
 
// error code
#define UART_ERR_MODEM 0x80000000
#define UART_ERR_LINE 0x40000000
#define UART_ERR_TIMEOUT 0x20000000
#define UART_ERR_BUSY 0x10000000
//ÒÆÖ²
#define SerialPutString(x)   Serial_PutString((uint8_t*)(x))
#define Serial0PutString(x)   Serial0_PutString((uint8_t*)(x))
uint32_t SerialKeyPressed(uint8_t *key);
void SerialPutChar(uint8_t c);
void SerialPutChar(uint8_t c);
void Serial_PutString(uint8_t *s);
void Serial0PutChar(uint8_t c);
void Serial0_PutString(uint8_t *s);
/**
 * @brief UART device IDs enumeration
 */
enum UART_DEV_T
{
    UART_ID0 = 0,
    UART_ID1,
    UART_MAX_NUM
};
 
/**
 * @brief UART parity enumeration
 */
enum UART_PARITY_T
{
    UART_PARITY_NONE,
    UART_PARITY_ODD,
    UART_PARITY_EVEN,
    UART_PARITY_FORCE1,
    UART_PARITY_FORCE0,
};
 
/**
 * @brief UART number of stop bits enumeration
 */
enum UART_STOP_BITS_T
{
    UART_STOP_BITS_1 = 0,
    UART_STOP_BITS_1_5 = 1, // 1.5 stop bits when data bits = 5
    UART_STOP_BITS_2 = 1,
};
 
/**
 * @brief UART number of data bits enumeration
 */
enum UART_DATA_BITS_T
{
    UART_DATA_BITS_5,
    UART_DATA_BITS_6,
    UART_DATA_BITS_7,
    UART_DATA_BITS_8,
};
 
/**
 * @brief UART flow control enumeration
 */
enum UART_FLOW_CONTROL_T
{
    UART_FLOW_CONTROL_NONE = 0,
    UART_FLOW_CONTROL_AUTO,
};
 
/**
 * @brief UART baudrate enumeration
 */
enum UART_BAUD_T
{
    BAUD_1200 = 0,
    BAUD_2400,
    BAUD_4800,
    BAUD_9600,
    BAUD_19200,
    BAUD_38400,
    BAUD_57600,
    BAUD_115200,
    BAUD_230400,
    BAUD_460800,
    BAUD_921600,
    BAUD_1843200,
    BAUD_1000000,
    BAUD_2000000,
    BAUD_MAX,
};
 
/**
 * @brief UART TX FIFO level enumeration
 */
enum UART_TXFIFO_LEVEL_T
{
    UART_TXFIFO_EMPTY = 0,
    UART_TXFIFO_CHAR_2,
    UART_TXFIFO_QUARTER_FULL,
    UART_TXFIFO_HALF_FULL,
};
 
/**
 * @brief UART RX FIFO level enumeration
 */
enum UART_RXFIFO_LEVEL_T
{
    UART_RXFIFO_CHAR_1 = 0,
    UART_RXFIFO_QUARTER_FULL,
    UART_RXFIFO_HALF_FULL,
    UART_RXFIFO_FULL_2,
};
 
/**
 * @brief UART state enumeration
 */
enum UART_STATE_T
{
    UART_STATE_RESET = 0x00U,
    UART_STATE_READY = 0x01U,
    UART_STATE_BUSY_TX = 0x10U,
    UART_STATE_BUSY_RX = 0x20U,
    UART_STATE_BUSY_TX_RX = 0x30U,
    UART_STATE_TIMEOUT = 0x40U,
    UART_STATE_ERROR = 0x80U,
};
 
/**
 * @brief UART Divisor structure (used by baudrate)
 */
struct UART_DIVISOR_T
{
    uint8_t dll;
    uint8_t dlh;
    uint8_t fraction;
};
 
/**
 * @brief UART config structure
 */
struct UART_CFG_T
{
    enum UART_BAUD_T baud;             /*!< Specifies the baudrate*/
    enum UART_PARITY_T parity;         /*!< Specifies the parity mode.*/
    enum UART_STOP_BITS_T stop;        /*!< Specifies the number of stop bits.*/
    enum UART_DATA_BITS_T data;        /*!< Specifies the number of data bits.*/
    enum UART_FLOW_CONTROL_T flow;     /*!< Specifies the flow control*/
    enum UART_RXFIFO_LEVEL_T rx_level; /*!< Specifies Rx FIFO level */
    enum UART_TXFIFO_LEVEL_T tx_level; /*!< Specifies Tx FIFO level */
    uint8_t dma_en;                    /*!< Specifies whether the dma is enabled or disabled. \n
                                               This parameter shoule be one of the following values:
                                                   @arg true is enable
                                                   @arg false is disable
                                       */
    uint8_t int_rx;                    /*!< Specifies whether Rx interupt in the NVIC interrupt controller is enabled or disabled. \n
                                               This parameter shoule be one of the following values:
                                                   @arg true is enable
                                                   @arg false is disable
                                       */
    uint8_t int_tx;                    /*!< Specifies whether Tx interupt in the NVIC interrupt controller is enabled or disabled. \n
                                               This parameter shoule be one of the following values:
                                                   @arg true is enable
                                                   @arg false is disable
                                       */
    uint8_t reserved;                  /*!< reserved */
};
 
/**
 * @brief  UART handle Structure
 */
struct UART_HANDLE_T
{
    UART_TypeDef *const base;         /*!< UART registers base address */
    const IRQn_Type irq;              /*!< UART interupt number */
    enum DMA_CH_T dma_rx_ch;          /*!< UART dma Rx channel */
    enum DMA_CH_T dma_tx_ch;          /*!< UART dma Tx channel */
    uint8_t dma_en;                   /*!< UART dma switch */
    uint8_t int_rx;                   /*!< UART Rx interupt switch */
    uint8_t int_tx;                   /*!< UART Tx interupt switch */
    uint8_t reserved;                 /*!< reserved */
    uint32_t ctrl0;                   /*!< Register CTRL0 storage */
    __IOM enum UART_STATE_T state;    /*!< UART state */
    __IOM uint32_t dma_rx_err_state;  /*!< UART dma rx err state */
    uint8_t *tx_buff;                 /*!< Pointer to UART Tx transfer Buffer */
    uint32_t tx_size;                 /*!< UART Tx Transfer size */
    __IOM uint32_t tx_count;          /*!< UART Tx Transfer Counter */
    uint8_t *rx_buff;                 /*!< Pointer to UART Rx transfer Buffer */
    uint32_t rx_size;                 /*!< UART Rx Transfer size */
    __IOM uint32_t rx_count;          /*!< UART Rx Transfer Counter */
    drv_callback_t tx_callback;       /*!< Pointer to UART Tx Callback function */
    drv_callback_t rx_callback;       /*!< Pointer to UART Rx Callback function */
    drv_callback_t tx_abort_callback; /*!< Pointer to UART Tx Callback function */
    drv_callback_t rx_abort_callback; /*!< Pointer to UART Rx Callback function */
};
 
#ifdef __cplusplus
extern "C" {
#endif
 
/**
 * @brief  open UART
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @param[in] config            Pointer to a UART_CFG_T structure that contains the configuration information for UART. \n
 * @return
 *         @arg DRV_ERROR   error id
 *         @arg DEV_OK      open succeed
 */
int uart_open(enum UART_DEV_T id, struct UART_CFG_T *config);
 
/**
 * @brief  close UART
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @return
 *         @arg DRV_ERROR   error id
 *         @arg DEV_OK      open succeed
 */
int uart_close(enum UART_DEV_T id);
 
/**
 * @brief  set UART baudrate
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @param[in] baud              baudrate to be set. \n
 *                              This parameter should be one of the UART_BAUD_T enum values without BAUD_MAX
 * @return None
 */
void uart_baud_set(enum UART_DEV_T id, enum UART_BAUD_T baud);
 
/**
 * @brief  check whether the UART TX in progress
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @return
 *         @arg true   UART TX in progress
 *         @arg false  UART TX done
 */
bool uart_tx_in_progress(enum UART_DEV_T id);
 
/**
 * @brief  UART busy
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @return
 *         @arg true   UART TX or RX in progress on the interface, or TX data or RX data present in FIFO
           @arg false  UART idle or inactive
 */
bool uart_fifo_busy(enum UART_DEV_T id);
 
/**
 * @brief  Clear UART RX FIFO
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 */
void uart_rx_fifo_clear(enum UART_DEV_T id);
 
/**
 * @brief  Get UART state
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @return
 *         @arg UART_STATE_T    UART state
 */
enum UART_STATE_T uart_state_get(enum UART_DEV_T id);
 
/**
 * @brief  Sends an amount of data.
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @param[in] tx_buf            Pointer to data buffer
 * @param[in] len               Bytes of data to be sent
 * @param[in] callback          Pointer to UART Tx Callback function. \n
 *                              This parameter will only be called in interupt or DMA mode
 * @return
 *         @arg DRV_ERROR   error id
 *         @arg DRV_BUSY    send failed because of UART Tx is busy
 *         @arg DEV_OK      send succeed
 */
int uart_send(enum UART_DEV_T id, uint8_t *tx_buf, uint32_t len, drv_callback_t callback);
 
/**
 * @brief  Receive an amount of data.
 * @param[in] id                UART device ID. \n
 *                              This parameter should be one of the UART_DEV_T enum values as below
 *                              @arg UART_ID0 uart0
 *                              @arg UART_ID1 uart1
 * @param[out] rx_buf           Pointer to data buffer
 * @param[in] len               Bytes of data to be received
 * @param[in] callback          Pointer to UART Rx Callback function. \n
 *                              This parameter will only be called in interupt or DMA mode
 * @return
 *         @arg DRV_ERROR   error id
 *         @arg DRV_BUSY    send failed because of UART Tx is busy
 *         @arg DEV_OK      send succeed
 */
int uart_receive(enum UART_DEV_T id, uint8_t *rx_buf, uint32_t len, drv_callback_t callback);
 
/**
 * @brief  Abort ongoing Transmit transfer.
 * @note This function is used to abort the ongoing uart TX transfer, if the
 *       TX transfer is completed before uart dma abort, the abort_tx_callback
 *       function will not be called.
 * @param[in] id                    UART device ID. \n
 *                                  This parameter should be one of the UART_DEV_T enum values as below
 *                                  @arg UART_ID0 uart0
 *                                  @arg UART_ID1 uart1
 * @param[in] abort_tx_callback     DMA UART TX transfer abort callback
 * @return @arg DRV_ERROR   Abort error
 *         @arg DEV_OK      Disable UART DMA TX request succeed
 */
int uart_tx_abort_dma(enum UART_DEV_T id, drv_callback_t abort_tx_callback);
 
/**
 * @brief Abort ongoing Receive transfer.
 * @note This function is used to abort the ongoing uart RX transfer, if the
 *       RX transfer is completed before uart dma abort, the abort_rx_callback
 *       function will not be called.
 * @param[in] id                    UART device ID. \n
 *                                  This parameter should be one of the UART_DEV_T enum values as below
 *                                  @arg UART_ID0 uart0
 *                                  @arg UART_ID1 uart1
 * @param[in] abort_rx_callback     DMA UART RX transfer abort callback
 * @return @arg DRV_ERROR   Abort error
 *         @arg DEV_OK      Disable UART DMA RX request succeed
 */
int uart_rx_abort_dma(enum UART_DEV_T id, drv_callback_t abort_rx_callback);
 
/**
 * @brief Handles interrupt request.
 * @param[in] id        UART device ID. \n
 *                      This parameter should be one of the UART_DEV_T enum values as below
 *                      @arg UART_ID0 uart0
 *                      @arg UART_ID1 uart1
 * @return None
 */
void uart_irq_handler(enum UART_DEV_T id);
 
/**
 * @brief This function handles UART0 interrupt.
 */
void UART0_IRQHandler(void);
 
/**
 * @brief This function handles UART1 interrupt.
 */
void UART1_IRQHandler(void);
 
/**
 * @brief Initialization function to sending messages over a UART by call uart_printf.
 * @param[in] id        UART device ID. \n
 *                      This parameter should be one of the UART_DEV_T enum values as below
 *                      @arg UART_ID0 uart0
 *                      @arg UART_ID1 uart1
 * @return
 *         @arg true   UART is busy
 *         @arg false  UART is idle
 */
int uart_printf_init(enum UART_DEV_T id);
 
/**
 * @brief This function sends messages over a UART similar to printf
 * @param[in] id        UART device ID. \n
 *                      This parameter should be one of the UART_DEV_T enum values as below
 *                      @arg UART_ID0 uart0
 *                      @arg UART_ID1 uart1
 * @param[in] fmt
 * @return None
 */
void uart_printf(enum UART_DEV_T id, const char *fmt, ...);
/**
 * @brief This function get USART1 rxcount to DMA handler
 * @param[in] id        void default usart1
 * @param[in] fmt
 * @return rx count from usart1
 */
uint32_t get_rx_count_from_uart1(void);
#ifdef __cplusplus
}
#endif
 
/**
 * @}
 */
 
#endif /* MK_UART_H_ */