• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef _STM32MP1_UART_HW_H_
17 #define _STM32MP1_UART_HW_H_
18 
19 #include <stdint.h>
20 
21 #include "osal.h"
22 #include "osal_io.h"
23 
24 #include "KRecvBuf.h"
25 
26 #define TX_BUF_SIZE (64)
27 #define RX_BUF_SIZE (4096)
28 #define UART_IRQ_NAME_SIZE  (16)
29 
30 struct Mp1xxUart;
31 
32 #define UART_HW_PARITY_NONE   (0)
33 #define UART_HW_PARITY_ODD    (1)
34 #define UART_HW_PARITY_EVEN   (2)
35 #define UART_HW_PARITY_MARK   (3)
36 #define UART_HW_PARITY_SPACE  (4)
37 
38 /* data bit */
39 #define UART_HW_DATABIT_8   (0)
40 #define UART_HW_DATABIT_7   (1)
41 #define UART_HW_DATABIT_6   (2)
42 #define UART_HW_DATABIT_5   (3)
43 
44 /* stop bit */
45 #define UART_HW_STOPBIT_1     (0)
46 #define UART_HW_STOPBIT_1P5   (1)
47 #define UART_HW_STOPBIT_2     (2)
48 
49 struct Mp1xxUartRxCtl {
50     char *fifo;
51     KRecvBuf rx_krb;            // 输入缓冲区
52     OSAL_DECLARE_SEMAPHORE(rx_sem);
53     uint32_t (*stm32mp1_uart_recv_hook)(struct Mp1xxUart *uart, char *buf, uint32_t size);
54 };
55 
56 struct Mp1xxUart {
57     uint32_t num;                   // 当前串口编号
58 
59     void volatile *base;            // 虚拟地址
60     uint32_t phy_base;              // 物理地址
61     uint32_t reg_step;              // 映射大小
62 
63     uint32_t irq_num;               // 中断号
64     char irq_name[UART_IRQ_NAME_SIZE];              // 中断名
65 
66     OSAL_DECLARE_SPINLOCK(lock);    // 自旋锁
67 
68     uint32_t clock_rate;            // 时钟源频率
69     char *clock_source;             // 时钟源
70 
71     bool debug_uart;                // 是否作为调试串口
72 
73     bool fifo_en;                   // fifo模式
74 
75     uint32_t baudrate;              // 波特率
76 
77     // state
78 #define UART_STATE_NOT_OPENED       (0)
79 #define UART_STATE_OPENING          (1)
80 #define UART_STATE_USEABLE          (2)
81 #define UART_STATE_SUSPENED         (3)
82     int state;                      // 设备状态
83     int open_count;                 // 打开数量
84 
85     // flags
86 #define UART_FLG_DMA_RX             (1 << 0)
87 #define UART_FLG_DMA_TX             (1 << 1)
88 #define UART_FLG_RD_BLOCK           (1 << 2)
89     uint32_t flags;
90 
91     // tx
92     char tx_buf[TX_BUF_SIZE];
93 
94     // rx
95     uint32_t rx_buf_size;
96     struct Mp1xxUartRxCtl rx_ctl;
97 
98     void *priv;
99 };
100 
101 extern void Mp1xxUartHwPutc(void *base, char c);
102 extern void Mp1xxUartHwPuts(void *base, char *s, uint32_t len);
103 
104 extern uint32_t Mp1xxUartIrqHandler(uint32_t irq, void *data);
105 
106 extern void Mp1xxUartDump(struct Mp1xxUart *uart);
107 
108 extern int32_t Mp1xxUartHwFifoEnable(struct Mp1xxUart *uart, int enable);
109 extern int32_t Mp1xxUartHwRxEnable(struct Mp1xxUart *uart, int enable);
110 extern int32_t Mp1xxUartHwEnable(struct Mp1xxUart *uart, int enable);
111 extern int32_t Mp1xxUartHwDataBits(struct Mp1xxUart *uart, uint32_t bits);
112 extern int32_t Mp1xxUartHwStopBits(struct Mp1xxUart *uart, uint32_t bits);
113 extern int32_t Mp1xxUartHwParity(struct Mp1xxUart *uart, uint32_t parity);
114 extern int32_t Mp1xxUartHwBaudrate(struct Mp1xxUart *uart, uint32_t baudrate);
115 
116 #endif
117