/*
* Copyright (c) 2023­2028, SpaceTouch Inc.
*
* SPDX­License­Identifier: Apache­2.0
*
* Change Logs:
* Date Author Notes
* 2023­04­18 dongx the first version
*/

#ifndef _HAL_UART_H
#define _HAL_UART_H

#ifdef __cplusplus
extern "C"
{
#endif


#include "platform.h"
#include "hal_dma.h"
/*
 * This enum defines return status of the UART HAL public API.
 * User should check return value after calling these APIs.
 */
typedef enum
{
    HAL_UART_STATUS_ERROR_PARAMETER = -4,      /**< Invalid user input parameter. */
    HAL_UART_STATUS_ERROR_BUSY = -3,           /**< UART port is currently in use. */
    HAL_UART_STATUS_ERROR_UNINITIALIZED = -2,  /**< UART port has not been initialized. */
    HAL_UART_STATUS_ERROR = -1,                /**< UART driver detected a common error. */
    HAL_UART_STATUS_OK = 0                     /**< UART function executed successfully. */
} hal_uart_status_t;

typedef enum
{
    UART_0 = 0,
    UART_1,
    UART_2,
    UART_3,
    UART_NUM,
} uart_port_t;

/* This enum defines baud rate of the UART frame. */
typedef enum
{
    UART_BAUDRATE_300 = 0,
    UART_BAUDRATE_600,
    UART_BAUDRATE_1200,
    UART_BAUDRATE_2400,
    UART_BAUDRATE_4800,
    UART_BAUDRATE_9600,
    UART_BAUDRATE_19200,
    UART_BAUDRATE_38400,
    UART_BAUDRATE_57600,
    UART_BAUDRATE_115200,
    UART_BAUDRATE_230400,
    UART_BAUDRATE_576000,
    UART_BAUDRATE_921600,
    UART_BAUDRATE_1000000,
    UART_BAUDRATE_1500000,
    UART_BAUDRATE_3000000,
    UART_BAUDRATE_4000000,
    UART_BAUDRATE_MAX,
} uart_baudrate_t;

/* This enum defines word length of the UART frame. */
typedef enum
{
    UART_WORD_LENGTH_5 = 0,
    UART_WORD_LENGTH_6,
    UART_WORD_LENGTH_7,
    UART_WORD_LENGTH_8,
} uart_data_length_t;

/* This enum defines stop bit of the UART frame. */
typedef enum
{
    UART_STOP_BIT_1 = 0,
    UART_STOP_BIT_2,
} uart_stop_bit_t;

/* This enum defines parity of the UART frame. */
typedef enum
{
    UART_PARITY_NONE = 0,
    UART_PARITY_ODD,
    UART_PARITY_EVEN
} uart_parity_t;

/* This struct defines UART configure parameters. */
typedef struct
{
    uart_baudrate_t baudrate;
    uart_data_length_t data_width;
    uart_stop_bit_t stop_bit;
    uart_parity_t parity;
} uart_config_t;


typedef void (*uart_callback)(void *param, uint32_t flag);

typedef struct
{
    DMA_HandleTypeDef dma_handle_tx;
    DMA_HandleTypeDef dma_handle_rx;
    uint32_t dma_rx_num;
} uart_dma_handle;

typedef struct
{
    uart_callback callback;
    void  *param;
    uint32_t flag;
    uart_dma_handle *dma_handle;
} uart_callback_t;




//#define HAL_UART0_TX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION3
//#define HAL_UART0_RX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION3
#define HAL_UART0_TX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION2
#define HAL_UART0_RX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION2

#define HAL_UART1_TX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION3
#define HAL_UART1_RX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION3


#define HAL_UART2_TX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION3
#define HAL_UART2_RX_MUXSEL_FUNCTION    GPIO_MUXSEL_FUNCTION3


void hal_uart_hw_set_change(int32_t uart_port, uart_config_t *uart_config);
int32_t hal_uart_set_rx_callback(int32_t uart_port, void *callback, void *param, uint32_t flag, uart_dma_handle *dma_handle);
int32_t hal_uart_set_rx_interrupt(int32_t uart_port, uint8_t enale);
void hal_uart_set_baudrate(int32_t uart_port, uint32_t baudrate);
void hal_uart_set_data_width(int32_t uart_port, uint32_t data_width);
void hal_uart_set_stop_bit(int32_t uart_port, uint32_t stop_bit);
void hal_uart_set_parity(int32_t uart_port, uint32_t parity);
int32_t hal_uart_get_char(int32_t uart_port);
int32_t hal_uart_put_char(int32_t uart_port, char c);
int32_t hal_uart_init(int32_t uart_port);

int32_t hal_uart_tx_dma_init(int32_t uart_port, DMA_HandleTypeDef *dma_handle);
int32_t hal_uart_rx_dma_init(int32_t uart_port, DMA_HandleTypeDef *dma_handle);
void hal_uart_tx_dma_start(DMA_HandleTypeDef *hdma, uint32_t srca_ddress, uint32_t data_length, int32_t uart_port, void *param);
void hal_uart_rx_dma_start(DMA_HandleTypeDef *hdma, uint32_t dst_address, uint32_t data_length, int32_t uart_port, void *param);
int32_t hal_uart_set_tx_DMA_enable(int32_t uart_port, uint8_t enale);
int32_t hal_uart_set_rx_DMA_enable(int32_t uart_port, uint8_t enale);
int32_t hal_uart_set_rx_timeout_interrupt(int32_t uart_port, uint8_t enale);

void hal_uart_auto_baud_rate_detect(int32_t uart_port);
uint16_t hal_uart_get_rx_div(int32_t uart_port);
void hal_uart0_send_bytes(uint8_t *addr, uint32_t length);
void show_uart_wave(uint16_t ch0, uint16_t ch1, uint16_t ch2, uint16_t ch3, uint16_t ch4);

extern void uart0_rxd_listen_handle(void);
#ifdef __cplusplus
}
#endif

#endif

