/**
 * \file    sys_uart1.h
 * \brief   UART1 handler
 * 
 * \par     Copyright (c) 2018 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2018-08-01
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2018-08-01  Original Creation
 * 
 */
 
#ifndef __SYS_UART_H_
#define __SYS_UART_H_

#include "types.h"

#define SYS_UART_REG_DEV_ID_OFFSET      (0)
#define SYS_UART_REG_NUM_OFFSET         (1)
#define SYS_UART_REG_OFFSET             (1)
#define SYS_UART_REG_OFFSET_L           (2)

#define SYS_UART_REG_MAX_NUM            (37)
#define SYS_UART_REG_CTRL_HEAD_SIZE     (2)
#define SYS_UART_REG_CTRL_HEAD_SIZE_L   (3)

#define SYS_UART_BUFFER_SIZE            (40)
#define SYS_UART_TIMEOUT                (20000U)
#define SYS_UART_TIMEOUT_L              (60000U)
#define SYS_UART_REG_READ_MISS_MAX_CNT  (20)

#pragma pack(1)

typedef struct sys_uart_info_s
{
    u32_t rx_data_index:8,              /** Index of receive data */
          read_reg_miss_cnt:8,          /** Read register miss count */
          reserved:16;

    /** UART buffer for data received */
    u8_t rx_data[SYS_UART_BUFFER_SIZE];

    /* bus lock for multi-task */
    void * bus_lock;

    sys_tick_t rx_start_time;

} sys_uart_info_t;

#pragma pack()


extern volatile sys_uart_info_t g_sys_uart;

bool_t sys_uart_reg_read(unsigned char reg, unsigned char *p_val);
bool_t sys_uart_reg_mread(unsigned char reg, int num, unsigned char *p_val);
bool_t sys_uart_reg_write(unsigned char reg, unsigned char *p_val);
bool_t sys_uart_reg_mwrite(unsigned char reg, int num, unsigned char *p_val);
void sys_uart_handler(int input);
void sys_uart_polling(u32_t uart_id);


#endif /* #ifndef __SYS_UART_H_ */

