1 /*
2  * Copyright 2020 GreenWaves Technologies
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
18 
19 #ifndef HAL_INCLUDE_HAL_UART_INTERNAL_H_
20 #define HAL_INCLUDE_HAL_UART_INTERNAL_H_
21 
22 #include "hal_uart_pi.h"
23 #include "core-v-mcu-pmsis.h"
24 
25 /*******************************************************************************
26  * Definitions
27  ******************************************************************************/
28 
29 #define UART_TX_BUFFER_SIZE     16
30 #define UART_DEFAULT_PRE_ALLOC_EVT 5
31 
32 #define NB_UART 1
33 
34 #ifndef UART_DRIVER_DATA_IMPLEM_SPECIFC
35     #define UART_DRIVER_DATA_IMPLEM_SPECIFC
36 #endif
37 
38 /*
39  * pi_task:
40  * data[0] = l2_buf
41  * data[1] = size
42  * data[2] = channel
43  * data[3] = repeat_size
44  * data[4] = device_id (used for delegation)
45  */
46 struct uart_itf_data_s
47 {
48     struct pi_task *fifo_head[2]; /*!< 0 = RX | 1 = TX. */
49     struct pi_task *fifo_tail[2]; /*!< 0 = RX | 1 = TX. */
50     uint32_t nb_open;             /*!< Number of times device has been opened. */
51     uint32_t device_id;           /*!< Device ID. */
52     UART_DRIVER_DATA_IMPLEM_SPECIFC
53 };
54 
55 /*******************************************************************************
56  * Driver data
57  *****************************************************************************/
58 
59 /*******************************************************************************
60  * Function declaration
61  ******************************************************************************/
62 
63 /**
64  * \brief Initialize conf struct.
65  *
66  * This function initializes a config struct with default values.
67  *
68  * \param conf           Pointer to struct pi_uart_conf.
69  */
70 void __pi_uart_conf_init(struct pi_uart_conf *conf);
71 
72 /**
73  * \brief Open a device.
74  *
75  * This function opens a device.
76  * A conf struct and a pointer to store UART driver info should be given.
77  *
78  * \para driver_data     Pointer to store driver info.
79  * \param conf           Pointer to struct pi_uart_conf.
80  *
81  * \retval 0            If operation is successfull.
82  * \retval ERRNO        An error code otherwise.
83  */
84 int32_t __pi_uart_open(struct uart_itf_data_s **driver_data,
85                        struct pi_uart_conf *conf);
86 
87 /**
88  * \brief Close a device.
89  *
90  * This function closes an opened device.
91  *
92  * \param driver_data    Pointer to driver info.
93  */
94 void __pi_uart_close(struct uart_itf_data_s *driver_data);
95 
96 /**
97  * \brief Ioctl commands.
98  *
99  * This function allows to configure a device using ioctl commands.
100  *
101  * \param driver_data    Pointer to driver info.
102  * \param cmd            Ioctl command.
103  * \param arg            Ioctl command arg.
104  *
105  * \retval -1            If wrong ioctl command.
106  * \retval Value         Value depending on ioctl command.
107  */
108 int32_t __pi_uart_ioctl(struct uart_itf_data_s *driver_data, uint32_t cmd,
109                         void *arg);
110 
111 /**
112  * \brief Transfer data.
113  *
114  * This function allows to send/receive data using the periph.
115  * The transfer is executed immediately if there is no current transfer or no
116  * pending transfer. Otherwise, the transfer is enqueued in a fifo.
117  *
118  * \param driver_data    Pointer to driver info.
119  * \param l2_buf         Address of data buffer.
120  * \param size           Size of data buffer.
121  * \param channel        Direction of transfer.
122  * \param task           Event task used to notify end of transfer.
123  *
124  * \retval 0             If operation is successfull.
125  * \retval -1            Otherwise.
126  */
127 int32_t __pi_uart_copy(struct uart_itf_data_s *driver_data, uint32_t l2_buf,
128                        uint32_t size, udma_channel_e channel,
129                        struct pi_task *task);
130 
131 /**
132  * \brief Transfer data.
133  *
134  * This function allows to send data using the periph.
135  * The transfer is executed immediately if there is no current transfer or no
136  * pending transfer. Otherwise, the transfer is enqueued in a fifo.
137  *
138  * \param driver_data    Pointer to driver info.
139  * \param buffer         Pointer to data buffer.
140  * \param size           Size of data buffer.
141  * \param task           Event task used to notify end of transfer.
142  *
143  * \retval 0             If operation is successfull.
144  * \retval -1            Otherwise.
145  */
__pi_uart_write(struct uart_itf_data_s * data,void * buffer,uint32_t size,pi_task_t * callback)146 static inline int __pi_uart_write(struct uart_itf_data_s *data, void *buffer,
147                                   uint32_t size, pi_task_t *callback)
148 {
149     return __pi_uart_copy(data, (uint32_t) buffer, size, TX_CHANNEL, callback);
150 }
151 
152 /**
153  * \brief Transfer data.
154  *
155  * This function allows to receive data using the periph.
156  * The transfer is executed immediately if there is no current transfer or no
157  * pending transfer. Otherwise, the transfer is enqueued in a fifo.
158  *
159  * \param driver_data    Pointer to driver info.
160  * \param buffer         Pointer to data buffer.
161  * \param size           Size of data buffer.
162  * \param task           Event task used to notify end of transfer.
163  *
164  * \retval 0             If operation is successfull.
165  * \retval -1            Otherwise.
166  */
__pi_uart_read(struct uart_itf_data_s * data,void * buffer,uint32_t size,pi_task_t * callback)167 static inline int __pi_uart_read(struct uart_itf_data_s *data, void *buffer,
168                                  uint32_t size, pi_task_t *callback)
169 {
170    return  __pi_uart_copy(data, (uint32_t) buffer, size, RX_CHANNEL, callback);
171 }
172 
173 
174 
175 #endif /* HAL_INCLUDE_HAL_UART_INTERNAL_H_ */
176