summaryrefslogtreecommitdiff
path: root/bsp/maxim/libraries/MAX32660PeriphDriver/Include/uart.h
blob: 92832018ce5bfd79d0a82bbbefa9f3adf0891de2 (plain)
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
/**
 * @file
 * @brief   This files defines the driver API including definitions, data types
 *          and function prototypes.
 */

/* ****************************************************************************
 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated
 * Products, Inc. shall not be used except as stated in the Maxim Integrated
 * Products, Inc. Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *
 * $Date: 2019-10-07 11:05:30 -0500 (Mon, 07 Oct 2019) $
 * $Revision: 47429 $
 *
 *************************************************************************** */


#ifndef _UART_H_
#define _UART_H_

/***** Includes *****/
#include <stdint.h>
#include "uart_regs.h"
#include "mxc_sys.h"

/***** Definitions *****/

/**
 * @brief      Alternate clock rate. (7.3728MHz) */
#define UART_ALTERNATE_CLOCK_HZ 7372800
/**
 * @defgroup uart UART
 * @ingroup periphlibs
 * @{
 */
/**
 * @brief Parity settings type */
typedef enum {
    UART_PARITY_DISABLE = 0,                                /**< Parity disabled */
    UART_PARITY_EVEN_0  = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_EVEN |
                           MXC_F_UART_CTRL_PARMD),          /**< Use for even parity 0 */
    UART_PARITY_EVEN_1  = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_EVEN),    /**< Use for even parity 1 */
    UART_PARITY_EVEN    = UART_PARITY_EVEN_1,               /**< Conventional even parity */
    UART_PARITY_ODD_0   = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_ODD |
                           MXC_F_UART_CTRL_PARMD),          /**< Use for odd parity 0 */
    UART_PARITY_ODD_1   = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_ODD),     /**< Use for odd parity 1 */
    UART_PARITY_ODD     = UART_PARITY_ODD_1,                /**< Conventional odd parity */
    UART_PARITY_MARK_0  = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_MARK |
                           MXC_F_UART_CTRL_PARMD),          /**< Use for mark parity 0 */
    UART_PARITY_MARK_1  = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_MARK),    /**< Use for mark parity 1 */
    UART_PARITY_MARK    = UART_PARITY_MARK_1,               /**< Conventional mark parity */
    UART_PARITY_SPACE_0 = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_SPACE |
                           MXC_F_UART_CTRL_PARMD),          /**< Use for space parity 0 */
    UART_PARITY_SPACE_1 = (MXC_F_UART_CTRL_PARITY_EN |
                           MXC_S_UART_CTRL_PARITY_SPACE),   /**< Use for space parity 1 */
    UART_PARITY_SPACE   = UART_PARITY_SPACE_1,              /**< Conventional space parity */
} uart_parity_t;

/**
 * @brief      Message size settings */
typedef enum {
    UART_DATA_SIZE_5_BITS = MXC_S_UART_CTRL_CHAR_SIZE_5,   /**< Data Size 5 Bits */
    UART_DATA_SIZE_6_BITS = MXC_S_UART_CTRL_CHAR_SIZE_6,   /**< Data Size 6 Bits */
    UART_DATA_SIZE_7_BITS = MXC_S_UART_CTRL_CHAR_SIZE_7,   /**< Data Size 7 Bits */
    UART_DATA_SIZE_8_BITS = MXC_S_UART_CTRL_CHAR_SIZE_8,   /**< Data Size 8 Bits */
} uart_size_t;

/**
 * @brief      Stop bit settings */
typedef enum {
    UART_STOP_1   = 0,                                      /**< UART Stop 1 clock cycle */
    UART_STOP_1P5 = MXC_F_UART_CTRL_STOPBITS,               /**< UART Stop 1.5 clock cycle */
    UART_STOP_2   = MXC_F_UART_CTRL_STOPBITS,               /**< UART Stop 2 clock cycle */
} uart_stop_t;

/**
 * @brief      Flow control */
typedef enum {
    UART_FLOW_CTRL_DIS = 0,                                 /**< RTS/CTS flow is disabled */
    UART_FLOW_CTRL_EN  = MXC_F_UART_CTRL_FLOW_CTRL,         /**< RTS/CTS flow is enabled */
} uart_flow_ctrl_t;

/**
 * @brief      Flow control Polarity */
typedef enum {
    UART_FLOW_POL_DIS = 0,                                  /**< RTS/CTS asserted is low */
    UART_FLOW_POL_EN  = MXC_F_UART_CTRL_FLOW_POL,           /**< RTS/CTS asserted is high */
} uart_flow_pol_t;

#if (TARGET != 32660)
/**
 * @brief      Clock Source Select */
typedef enum {
    UART_CLKSEL_SYSTEM = 0,                                 /**< Peripheral clock will be used as the bit rate clock */
    UART_CLKSEL_ALTERNATE = MXC_F_UART_CTRL_CLKSEL,         /**< Use the device's alternate UART bit rate clock. */
} uart_clksel_t;
#endif

/**
 * @brief      UART configuration type. */
typedef struct {
    uart_parity_t parity;  /**        Configure parity checking */
    uart_size_t size;      /**        Configure character size */
    uart_stop_t stop;      /**        Configure the number of stop bits to use */
    uart_flow_ctrl_t flow; /**        Configure hardware flow control */
    uart_flow_pol_t pol;   /**        Configure hardware flow control */
    uint32_t baud;         /**        Configure baud rate */
#if (TARGET != 32660)
    uart_clksel_t clksel;  /**        Configure hardware clock source */
#endif
} uart_cfg_t;

/**
 * @brief      Non-blocking UART transaction request. */
typedef struct uart_req uart_req_t;
struct uart_req {
    uint8_t *data;    /**       Data buffer for characters */
    int len;          /**       Length of characters in data to send or receive */
    int num;          /**       Number of characters actually sent or received */

    /**
     * @brief   Callback for asynchronous request.
     *
     * @param   uart_req_t*  Pointer to the transaction request.
     * @param   int          Error code.
     *
     */
    void(*callback)(uart_req_t*, int);
};


/***** Functions Prototypes *****/


/**
 * @brief   Initialize and enable UART module.
 * @param   uart        Pointer to the UART registers.
 * @param   cfg         Pointer to UART configuration.
 * @param   sys_cfg     Pointer to system configuration object
 * @returns #E_NO_ERROR UART initialized successfully, @ref MXC_Error_Codes "error" if
 *             unsuccessful.
 */
int UART_Init(mxc_uart_regs_t *uart, const uart_cfg_t *cfg, const sys_cfg_uart_t* sys_cfg);

/**
 * @brief   Shutdown UART module.
 * @param   uart    Pointer to the UART registers.
 * @returns #E_NO_ERROR UART shutdown successfully, @ref MXC_Error_Codes "error" if
 *             unsuccessful.
 */
int UART_Shutdown(mxc_uart_regs_t *uart);

/**
 * @brief      UART interrupt handler.
 * @details    This function should be called by the application from the
 *             interrupt handler if UART interrupts are enabled. Alternately,
 *             this function can be periodically called by the application if
 *             UART interrupts are disabled. It is only necessary to call this
 *             when using asynchronous functions.
 *
 * @param      uart  Pointer to the UART registers.
 */
void UART_Handler(mxc_uart_regs_t *uart);

/**
 * @brief      Read UART data, <em>blocking</em> until transaction is complete.
 *
 * @param      uart  Pointer to the UART registers.
 * @param      data  Pointer to buffer to save the data read.
 * @param      len   Number of bytes to read.
 * @param      num   Pointer to store the number of bytes actually read, pass NULL if not needed.
 *
 * @return     Number of bytes read, @ref MXC_Error_Codes "error" if unsuccessful.
 */
int UART_Read(mxc_uart_regs_t *uart, uint8_t *data, int len, int *num);

/**
 * @brief      Write UART data. This function blocks until the write transaction
 *             is complete.
 * @param      uart  Pointer to the UART registers.
 * @param      data  Pointer to buffer for write data.
 * @param      len   Number of bytes to write.
 * @note       This function will return once data has been put into FIFO, not necessarily
 *             transmitted.
 * @return     Number of bytes written if successful, @ref MXC_Error_Codes "error" if unsuccessful.
 */
int UART_Write(mxc_uart_regs_t *uart, const uint8_t *data, int len);

/**
 * @brief      Asynchronously read UART data.
 *
 * @param      uart  Pointer to the UART registers.
 * @param      req   Pointer to request for a UART transaction, see #uart_req.
 * @note       Request struct must remain allocated until callback function specified in 'req' is called.
 *
 * @return     #E_NO_ERROR Asynchronous read successfully started, @ref MXC_Error_Codes "error" if unsuccessful.
 */
int UART_ReadAsync(mxc_uart_regs_t *uart, uart_req_t *req);

/**
 * @brief      Asynchronously write/transmit UART data.
 *
 * @param      uart  Pointer to the UART registers.
 * @param      req   Request for a UART transaction, see #uart_req.
 * @note       Request struct must remain allocated until callback function specified in 'req' is called.
 *
 * @return     #E_NO_ERROR Asynchronous write successfully started, @ref
 *             MXC_Error_Codes "error" if unsuccessful.
 */
int UART_WriteAsync(mxc_uart_regs_t *uart, uart_req_t *req);

/**
 * @brief      Read a single byte from the UART.
 * @note       This function will block until a character is available.
 *
 * @param      uart  Pointer to the UART registers.
 * @return     The byte read.
 */
uint8_t UART_ReadByte(mxc_uart_regs_t *uart);

/**
 * @brief      Write one byte at a time to the UART.
 * @note       This function will block until the character has been placed in the transmit FIFO.
 *             It may return before the character is actually transmitted.
 *
 * @param      uart  Pointer to the UART registers.
 * @param      data  The byte to write.
 */
void UART_WriteByte(mxc_uart_regs_t *uart, uint8_t data);

/**
 * @brief      Check to see if the UART is busy.
 *
 * @param      uart  Pointer to the UART registers.
 *
 * @return     #E_NO_ERROR if the UART is idle, #E_BUSY if the UART is in use.
 */
int UART_Busy(mxc_uart_regs_t *uart);

/**
 * @brief      Prepare the UART for entry into a Low-Power mode (DEEPSLEEP/BACKUP).
 * @details    Checks for any ongoing transactions. Disables interrupts if the
 *             UART is idle.
 *
 * @param      uart         Pointer to the UART registers.
 * @return     #E_NO_ERROR  UART is ready to enter Low-Power modes (DEEPSLEEP/BACKUP).
 * @return     #E_BUSY      UART is active and busy and not ready to enter a
 *                          Low-Power mode (DEEPSLEEP/BACKUP).
 *
 */
int UART_PrepForSleep(mxc_uart_regs_t *uart);

/**
 * @brief      Abort asynchronous request.
 *
 * @param      req   Pointer to the request to abort. See #uart_req.
 *
 * @return     #E_NO_ERROR if the asynchronous request aborted successfully started, @ref
 *             MXC_Error_Codes "error" if unsuccessful.
 */
int UART_AbortAsync(uart_req_t *req);

/**
 * @brief      Returns the number of bytes still pending transmission in the UART TX FIFO.
 *
 * @param      uart  Pointer to the UART registers.
 *
 * @return     Number of unused bytes in the TX FIFO.
 */
unsigned UART_NumWriteAvail(mxc_uart_regs_t *uart);

/**
 * @brief      Returns the number of bytes available to be read from the RX FIFO.
 *
 * @param      uart  Pointer to the UART registers.
 *
 * @return     The number of bytes available to read in the RX FIFO.
 */
unsigned UART_NumReadAvail(mxc_uart_regs_t *uart);

/**
 * @brief      Clears the specified interrupt flags.
 *
 * @param      uart  Pointer to the UART registers.
 * @param      mask  Mask of the UART interrupts to clear, see
 *                   @ref UART_INT_FL Register.
 */
void UART_ClearFlags(mxc_uart_regs_t *uart, uint32_t mask);

/**
 * @brief      Get the UART interrupt flags.
 *
 * @param      uart  Pointer to the UART registers.
 *
 * @return     Mask of active flags.
 */
unsigned UART_GetFlags(mxc_uart_regs_t *uart);

/**
 * @brief      Enables the UART.
 * @note       This function does not change the existing UART configuration.
 *
 * @param      uart  Pointer to the UART registers.
 */
void UART_Enable(mxc_uart_regs_t *uart);

/**
 * @brief      Disables the UART.
 * @note       This function does not change the existing UART configuration.
 *
 * @param      uart  Pointer to the UART registers.
 */
void UART_Disable(mxc_uart_regs_t *uart);

/**
 * @brief      Drains/empties and data in the RX FIFO, discarding any bytes not yet consumed.
 *
 * @param      uart  Pointer to the UART registers.
 */
void UART_DrainRX(mxc_uart_regs_t *uart);

/**
 * @brief      Drains/empties any data in the TX FIFO, discarding any bytes not yet transmitted.
 *
 * @param      uart  Pointer to the UART registers.
 */
void UART_DrainTX(mxc_uart_regs_t *uart);

/**@} end of group uart */

#endif /* _UART_H_ */