1 /*
2  * Copyright (c) 2022 OpenLuat & AirM2M
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 
22 #ifndef __CORE_UART_H__
23 #define __CORE_UART_H__
24 #include "bsp_common.h"
25 /**
26  * @brief 对串口做基本的初始化工作,不开启任何中断和DMA
27  *
28  * @param UartID 串口号 0~MAX,对应USART1~UARTX,0默认用于log输出
29  * @param BaudRate 波特率
30  * @param IsRxCacheEnable 是否打开RX缓存功能,打开后,只有接收超时中断才会返回,否则每次数据到来都会返回
31  * @param DataBits 数据位5~8
32  * @param Parity 奇偶校验,可选下列
33  *  UART_PARITY_NONE,
34     UART_PARITY_ODD,
35     UART_PARITY_EVEN,
36  * @param StopBits 停止位
37  *  UART_STOP_BIT1,
38     UART_STOP_BIT1_5,
39     UART_STOP_BIT2,
40  * @param CB 回调函数,用于通知是否有新数据达到,DMA TX或者RX完成,是否有错误
41  */
42 void Uart_BaseInit(uint8_t UartID, uint32_t BaudRate, uint8_t IsRxCacheEnable, uint8_t DataBits, uint8_t Parity, uint8_t StopBits, CBFuncEx_t CB);
43 
44 
45 void Uart_SetCb(uint8_t UartID, CBFuncEx_t CB);
46 
47 void Uart_DeInit(uint8_t UartID);
48 /**
49  * @brief 串口做阻塞输出,一般用于bootloader或者紧急场合
50  *
51  * @param UartID 串口号 0~MAX,对应USART1~UARTX,0默认用于log输出
52  * @param Data
53  * @param Len
54  */
55 void Uart_BlockTx(uint8_t UartID, uint8_t *Data, uint32_t Len);
56 
57 void Uart_NoBlockTx(uint8_t UartID, uint8_t Data);
58 
59 void Uart_EnableRxIrq(uint8_t UartID);
60 
61 void Uart_EnableTxDoneIrq(uint8_t UartID);
62 /**
63  * @brief 串口的DMA配置
64  *
65  * @param UartID 串口号 0~5
66  * @param Stream DMA流序号
67  * @param Channel DMA通道
68  * @return >0 成功返回中断号,其他失败
69  */
70 int Uart_DMATxInit(uint8_t UartID, uint8_t Stream, uint32_t Channel);
71 
72 int Uart_DMARxInit(uint8_t UartID, uint8_t Stream, uint32_t Channel);
73 
74 /**
75  * @brief 串口做DMA非阻塞输出,只能用于APP
76  *
77  * @param UartID 串口号 0~5
78  * @param Stream DMA流序号
79  * @param Data
80  * @param Len
81  */
82 void Uart_DMATx(uint8_t UartID, uint8_t Stream, const uint8_t *Data, uint32_t Len);
83 
84 void Uart_DMARx(uint8_t UartID, uint8_t Stream, uint8_t *Data, uint32_t Len);
85 
86 uint32_t Uart_RxBufferRead(uint8_t UartID, uint8_t *Data, uint32_t Len);
87 
88 void Uart_RxBufferCB(uint8_t UartID, CBFuncEx_t CB);
89 
90 uint32_t Uart_FifoRead(uint8_t UartID, uint8_t *Data);
91 /**
92  * @brief 串口做FIFO非阻塞输出
93  *
94  * @param UartID 串口号 0~5
95  * @param Data
96  * @param Len
97  * @return <0 失败 =0 所有数据传入fifo,不再回调buffer_done,直接回调all_done >0 还有数据没有传入fifo
98  */
99 int32_t Uart_BufferTx(uint8_t UartID, const uint8_t *Data, uint32_t Len);
100 void Uart_BufferTxStop(uint8_t UartID);
101 void Uart_PrintReg(uint8_t UartID);
102 void Uart_ChangeBR(uint8_t UartID, uint32_t BaudRate);
103 uint32_t Uart_GetLastError(uint8_t UartID);
104 void Uart_GlobalInit(void);
105 #endif
106