1 /*
2   ******************************************************************************
3   * @file    HAL_LPUART.h
4   * @version V1.0.0
5   * @date    2020
6   * @brief   Header file of CAN HAL module.
7   ******************************************************************************
8 */
9 #ifndef __HAL_LPUART_H__
10 #define __HAL_LPUART_H__
11 
12 #include "ACM32Fxx_HAL.h"
13 
14 
15 #define IS_LPUART_INSTANCE(INSTANCE)          ((INSTANCE) == LPUART)
16 
17 #define LPUART_PEN_INDEX  0
18 #define LPUART_SPS_INDEX  1
19 #define LPUART_EPS_INDEX  2
20 
21 #define LPUART_IE_RX_INDEX         0  //has wakeup function
22 #define LPUART_IE_TX_FINISH_INDEX  1
23 #define LPUART_IE_TX_EMPTY_INDEX   2
24 
25 #define LPUART_SR_RX_INDEX         0
26 #define LPUART_SR_TX_FINISH_INDEX  1
27 #define LPUART_SR_TX_EMPTY_INDEX   2
28 #define LPUART_SR_RX_FULL_INDEX    6
29 #define LPUART_SR_MATCH_INDEX      8  //has wakeup function
30 #define LPUART_SR_START_INDEX      9  //has wakeup function
31 #define LPUART_SR_BITS_ALL         0x1FFU
32 
33 #define LPUART_WAKEUP_RX_BIT       BIT0
34 #define LPUART_WAKEUP_MATCH_BIT    BIT8
35 #define LPUART_WAKEUP_START_BIT    BIT9
36 
37 #define LPUART_CR_RXE_INDEX  0
38 #define LPUART_CR_TXE_INDEX  1
39 #define LPUART_CR_DMA_EN_INDEX  2
40 
41 /** @defgroup  GPIO Private Macros
42   * @{
43   */
44 typedef enum
45 {
46     LPUART_RXWKS_START = 0x00,
47     LPUART_RXWKS_OneByte = 0x01,
48     LPUART_RXWKS_DataCompare = 0x02,
49     LPUART_RXWKS_NoWake = 0x03,
50 } LPUART_RXWKS;
51 
52 typedef enum
53 {
54     LPUART_WKCK_Check = 0x00,
55     LPUART_WKCK_NoCheck = 0x01,
56 } LPUART_WKCK ;
57 
58 /**
59   * @}
60   */
61 
62 
63 
64 /*
65  * @brief  LPUART DATABITS  definition
66  */
67 typedef enum
68 {
69     LPUART_DATABITS_8 = 0x00,
70     LPUART_DATABITS_7 = 0x01,
71 } LPUART_DATABITS;
72 
73 /*
74  * @brief  LPUART STOPBITS  definition
75  */
76 typedef enum
77 {
78     LPUART_STOPBITS_ONE = 0x00U,
79     LPUART_STOPBITS_TWO = 0x01U,
80 }LPUART_STOPBITS;
81 
82 /*
83  * @brief  LPUART CLOCK_SOURCE  definition
84  */
85 typedef enum
86 {
87     LPUART_CLOCK_SOURCE_RC32K = 0x00U,
88     LPUART_CLOCK_SOURCE_XTAL = 0x01U,
89     LPUART_CLOCK_SOURCE_PLL_DIV = 0x02U,
90 }LPUART_CLOCK_SOURCE;
91 
92 /*
93  * @brief  LPUART PARITY  definition
94  */
95 typedef enum
96 {
97     LPUART_PARITY_NONE = 0x00U,
98     LPUART_PARITY_SELECT_ODD = 0x01U,
99     LPUART_PARITY_SELECT_EVEN = 0x02U,
100     LPUART_PARITY_SELECT_ONE = 0x03U,
101     LPUART_PARITY_SELECT_ZERO = 0x04U,
102 }LPUART_PARITY_SELECT;
103 
104 /**
105   * @brief  LPUART  Config structure definition
106   */
107 typedef struct
108 {
109     uint8_t  WordLength;  // LPUART_DATABITS
110     uint8_t  StopBits;    // LPUART_STOPBITS
111     uint8_t  Parity;     // LPUART_PARITY_SELECT
112     uint8_t  ClockSrc;   // LPUART_CLOCK_SOURCE
113     uint32_t BaudRate;
114 }LPUART_ConfigParam;
115 
116 /**
117   * @brief  LPUART  Config structure definition
118   */
119 typedef struct
120 {
121     uint8_t  Wakeup_Source;  // Wakeup_Source
122     uint8_t  Wakeup_Check;  // Wakeup_Check
123     uint8_t  Wakeup_Addr;  // Wakeup_Addr
124 
125 }LPUART_StopWakeupDef;
126 
127 
128 
129 /**
130   * @brief  LPUART  init structure definition
131   */
132 typedef struct
133 {
134     LPUART_TypeDef *Instance;
135     LPUART_ConfigParam ConfigParam;
136     LPUART_StopWakeupDef StopWakeup;
137     volatile uint8_t *rx_buffer;
138     volatile uint16_t rx_buffer_size;
139     volatile uint16_t rx_write_index;
140     volatile uint16_t rx_read_index;
141     volatile uint16_t tx_busy;
142     DMA_HandleTypeDef *dma_rx_handler;
143     DMA_HandleTypeDef *dma_tx_handler;
144 }LPUART_HandleTypeDef;
145 
146 /* HAL_LPUART_Init */
147 HAL_StatusTypeDef HAL_LPUART_Init(LPUART_HandleTypeDef *hlpuart);
148 
149 HAL_StatusTypeDef HAL_LPUART_DeInit(LPUART_HandleTypeDef *hlpuart);
150 
151 void HAL_LPUART_MSPInit(LPUART_HandleTypeDef *hlpuart);
152 
153 void HAL_LPUART_MspDeInit(LPUART_HandleTypeDef *hlpuart);
154 
155 /* HAL_LPUART_Send_Bytes */
156 void HAL_LPUART_Send_Bytes(LPUART_HandleTypeDef *hlpuart, uint8_t *buff, uint32_t length);
157 
158 /* HAL_LPUART_IRQHander */
159 void HAL_LPUART_IRQHander(LPUART_HandleTypeDef *hlpuart);
160 
161 /* HAL_LPUART_Receive_Bytes_Timeout */
162 uint32_t HAL_LPUART_Receive_Bytes_Timeout(LPUART_HandleTypeDef *hlpuart, uint8_t * rxbuff, uint32_t length, uint32_t ms);
163 
164 /* HAL_LPUART_Receive_Bytes */
165 uint32_t HAL_LPUART_Receive_Bytes(LPUART_HandleTypeDef *hlpuart, uint8_t * rxbuff, uint32_t length);
166 
167 /* HAL_LPUART_DMA_Send_Bytes */
168 void HAL_LPUART_DMA_Send_Bytes(LPUART_HandleTypeDef *hlpuart, uint8_t *buff, uint32_t length);
169 
170 /* HAL_LPUART_Clear_Wakeup_Flags */
171 void HAL_LPUART_Clear_Wakeup_Flags(LPUART_HandleTypeDef *hlpuart, uint32_t Wakeup_Bits);
172 
173 #endif
174