/* * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and * its subsidiaries and affiliates (collectly called MKSEMI). * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into an MKSEMI * integrated circuit in a product or a software update for such product, * must reproduce the above copyright notice, this list of conditions and * the following disclaimer in the documentation and/or other materials * provided with the distribution. * * 3. Neither the name of MKSEMI nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * 4. This software, with or without modification, must only be used with a * MKSEMI integrated circuit. * * 5. Any software provided in binary form under this license must not be * reverse engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef MK_UART_H_ #define MK_UART_H_ #include "mk_common.h" #include "mk_dma.h" #ifndef UART_INT_MODE_EN #define UART_INT_MODE_EN (1) #endif #ifndef UART_DMA_MODE_EN #define UART_DMA_MODE_EN (1) #endif #ifndef UART_POLL_MODE_EN #define UART_POLL_MODE_EN (1) #endif /** * @addtogroup MK8000_UART * @{ */ // error code #define UART_ERR_MODEM 0x80000000 #define UART_ERR_LINE 0x40000000 #define UART_ERR_TIMEOUT 0x20000000 #define UART_ERR_BUSY 0x10000000 //ÒÆÖ² #define SerialPutString(x) Serial_PutString((uint8_t*)(x)) #define Serial0PutString(x) Serial0_PutString((uint8_t*)(x)) uint32_t SerialKeyPressed(uint8_t *key); uint32_t Serial0KeyPressed(uint8_t *key); void SerialPutChar(uint8_t c); void SerialPutChar(uint8_t c); void Serial_PutString(uint8_t *s); void Serial0PutChar(uint8_t c); void Serial0_PutString(uint8_t *s); /** * @brief UART device IDs enumeration */ enum UART_DEV_T { UART_ID0 = 0, UART_ID1, UART_MAX_NUM }; /** * @brief UART parity enumeration */ enum UART_PARITY_T { UART_PARITY_NONE, UART_PARITY_ODD, UART_PARITY_EVEN, UART_PARITY_FORCE1, UART_PARITY_FORCE0, }; /** * @brief UART number of stop bits enumeration */ enum UART_STOP_BITS_T { UART_STOP_BITS_1 = 0, UART_STOP_BITS_1_5 = 1, // 1.5 stop bits when data bits = 5 UART_STOP_BITS_2 = 1, }; /** * @brief UART number of data bits enumeration */ enum UART_DATA_BITS_T { UART_DATA_BITS_5, UART_DATA_BITS_6, UART_DATA_BITS_7, UART_DATA_BITS_8, }; /** * @brief UART flow control enumeration */ enum UART_FLOW_CONTROL_T { UART_FLOW_CONTROL_NONE = 0, UART_FLOW_CONTROL_AUTO, }; /** * @brief UART baudrate enumeration */ enum UART_BAUD_T { BAUD_1200 = 0, BAUD_2400, BAUD_4800, BAUD_9600, BAUD_19200, BAUD_38400, BAUD_57600, BAUD_115200, BAUD_230400, BAUD_460800, BAUD_921600, BAUD_1843200, BAUD_1000000, BAUD_2000000, BAUD_MAX, }; /** * @brief UART TX FIFO level enumeration */ enum UART_TXFIFO_LEVEL_T { UART_TXFIFO_EMPTY = 0, UART_TXFIFO_CHAR_2, UART_TXFIFO_QUARTER_FULL, UART_TXFIFO_HALF_FULL, }; /** * @brief UART RX FIFO level enumeration */ enum UART_RXFIFO_LEVEL_T { UART_RXFIFO_CHAR_1 = 0, UART_RXFIFO_QUARTER_FULL, UART_RXFIFO_HALF_FULL, UART_RXFIFO_FULL_2, }; /** * @brief UART state enumeration */ enum UART_STATE_T { UART_STATE_RESET = 0x00U, UART_STATE_READY = 0x01U, UART_STATE_BUSY_TX = 0x10U, UART_STATE_BUSY_RX = 0x20U, UART_STATE_BUSY_TX_RX = 0x30U, UART_STATE_TIMEOUT = 0x40U, UART_STATE_ERROR = 0x80U, }; /** * @brief UART Divisor structure (used by baudrate) */ struct UART_DIVISOR_T { uint8_t dll; uint8_t dlh; uint8_t fraction; }; /** * @brief UART config structure */ struct UART_CFG_T { enum UART_BAUD_T baud; /*!< Specifies the baudrate*/ enum UART_PARITY_T parity; /*!< Specifies the parity mode.*/ enum UART_STOP_BITS_T stop; /*!< Specifies the number of stop bits.*/ enum UART_DATA_BITS_T data; /*!< Specifies the number of data bits.*/ enum UART_FLOW_CONTROL_T flow; /*!< Specifies the flow control*/ enum UART_RXFIFO_LEVEL_T rx_level; /*!< Specifies Rx FIFO level */ enum UART_TXFIFO_LEVEL_T tx_level; /*!< Specifies Tx FIFO level */ uint8_t dma_en; /*!< Specifies whether the dma is enabled or disabled. \n This parameter shoule be one of the following values: @arg true is enable @arg false is disable */ uint8_t int_rx; /*!< Specifies whether Rx interupt in the NVIC interrupt controller is enabled or disabled. \n This parameter shoule be one of the following values: @arg true is enable @arg false is disable */ uint8_t int_tx; /*!< Specifies whether Tx interupt in the NVIC interrupt controller is enabled or disabled. \n This parameter shoule be one of the following values: @arg true is enable @arg false is disable */ uint8_t reserved; /*!< reserved */ }; /** * @brief UART handle Structure */ struct UART_HANDLE_T { UART_TypeDef *const base; /*!< UART registers base address */ const IRQn_Type irq; /*!< UART interupt number */ enum DMA_CH_T dma_rx_ch; /*!< UART dma Rx channel */ enum DMA_CH_T dma_tx_ch; /*!< UART dma Tx channel */ uint8_t dma_en; /*!< UART dma switch */ uint8_t int_rx; /*!< UART Rx interupt switch */ uint8_t int_tx; /*!< UART Tx interupt switch */ uint8_t reserved; /*!< reserved */ uint32_t ctrl0; /*!< Register CTRL0 storage */ __IOM enum UART_STATE_T state; /*!< UART state */ __IOM uint32_t dma_rx_err_state; /*!< UART dma rx err state */ uint8_t *tx_buff; /*!< Pointer to UART Tx transfer Buffer */ uint32_t tx_size; /*!< UART Tx Transfer size */ __IOM uint32_t tx_count; /*!< UART Tx Transfer Counter */ uint8_t *rx_buff; /*!< Pointer to UART Rx transfer Buffer */ uint32_t rx_size; /*!< UART Rx Transfer size */ __IOM uint32_t rx_count; /*!< UART Rx Transfer Counter */ drv_callback_t tx_callback; /*!< Pointer to UART Tx Callback function */ drv_callback_t rx_callback; /*!< Pointer to UART Rx Callback function */ drv_callback_t tx_abort_callback; /*!< Pointer to UART Tx Callback function */ drv_callback_t rx_abort_callback; /*!< Pointer to UART Rx Callback function */ }; #ifdef __cplusplus extern "C" { #endif /** * @brief open UART * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @param[in] config Pointer to a UART_CFG_T structure that contains the configuration information for UART. \n * @return * @arg DRV_ERROR error id * @arg DEV_OK open succeed */ int uart_open(enum UART_DEV_T id, struct UART_CFG_T *config); /** * @brief close UART * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @return * @arg DRV_ERROR error id * @arg DEV_OK open succeed */ int uart_close(enum UART_DEV_T id); /** * @brief set UART baudrate * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @param[in] baud baudrate to be set. \n * This parameter should be one of the UART_BAUD_T enum values without BAUD_MAX * @return None */ void uart_baud_set(enum UART_DEV_T id, enum UART_BAUD_T baud); /** * @brief check whether the UART TX in progress * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @return * @arg true UART TX in progress * @arg false UART TX done */ bool uart_tx_in_progress(enum UART_DEV_T id); /** * @brief UART busy * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @return * @arg true UART TX or RX in progress on the interface, or TX data or RX data present in FIFO @arg false UART idle or inactive */ bool uart_fifo_busy(enum UART_DEV_T id); /** * @brief Clear UART RX FIFO * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 */ void uart_rx_fifo_clear(enum UART_DEV_T id); /** * @brief Get UART state * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @return * @arg UART_STATE_T UART state */ enum UART_STATE_T uart_state_get(enum UART_DEV_T id); /** * @brief Sends an amount of data. * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @param[in] tx_buf Pointer to data buffer * @param[in] len Bytes of data to be sent * @param[in] callback Pointer to UART Tx Callback function. \n * This parameter will only be called in interupt or DMA mode * @return * @arg DRV_ERROR error id * @arg DRV_BUSY send failed because of UART Tx is busy * @arg DEV_OK send succeed */ int uart_send(enum UART_DEV_T id, uint8_t *tx_buf, uint32_t len, drv_callback_t callback); /** * @brief Receive an amount of data. * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @param[out] rx_buf Pointer to data buffer * @param[in] len Bytes of data to be received * @param[in] callback Pointer to UART Rx Callback function. \n * This parameter will only be called in interupt or DMA mode * @return * @arg DRV_ERROR error id * @arg DRV_BUSY send failed because of UART Tx is busy * @arg DEV_OK send succeed */ int uart_receive(enum UART_DEV_T id, uint8_t *rx_buf, uint32_t len, drv_callback_t callback); /** * @brief Abort ongoing Transmit transfer. * @note This function is used to abort the ongoing uart TX transfer, if the * TX transfer is completed before uart dma abort, the abort_tx_callback * function will not be called. * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @param[in] abort_tx_callback DMA UART TX transfer abort callback * @return @arg DRV_ERROR Abort error * @arg DEV_OK Disable UART DMA TX request succeed */ int uart_tx_abort_dma(enum UART_DEV_T id, drv_callback_t abort_tx_callback); /** * @brief Abort ongoing Receive transfer. * @note This function is used to abort the ongoing uart RX transfer, if the * RX transfer is completed before uart dma abort, the abort_rx_callback * function will not be called. * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @param[in] abort_rx_callback DMA UART RX transfer abort callback * @return @arg DRV_ERROR Abort error * @arg DEV_OK Disable UART DMA RX request succeed */ int uart_rx_abort_dma(enum UART_DEV_T id, drv_callback_t abort_rx_callback); /** * @brief Handles interrupt request. * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @return None */ void uart_irq_handler(enum UART_DEV_T id); /** * @brief This function handles UART0 interrupt. */ void UART0_IRQHandler(void); /** * @brief This function handles UART1 interrupt. */ void UART1_IRQHandler(void); /** * @brief Initialization function to sending messages over a UART by call uart_printf. * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @return * @arg true UART is busy * @arg false UART is idle */ int uart_printf_init(enum UART_DEV_T id); /** * @brief This function sends messages over a UART similar to printf * @param[in] id UART device ID. \n * This parameter should be one of the UART_DEV_T enum values as below * @arg UART_ID0 uart0 * @arg UART_ID1 uart1 * @param[in] fmt * @return None */ void uart_printf(enum UART_DEV_T id, const char *fmt, ...); /** * @brief This function get USART1 rxcount to DMA handler * @param[in] id void default usart1 * @param[in] fmt * @return rx count from usart1 */ uint32_t get_rx_count_from_uart1(void); #ifdef __cplusplus } #endif /** * @} */ #endif /* MK_UART_H_ */