/*
 * usart.h
 *
 *  Created on: 2023/06/09
 *      Author: BestTechnology
 */

#ifndef USART_H_
#define USART_H_

#include "hal_data.h"

#include <stddef.h>

#define UART_CFG_DATALEN_7      0x10
#define UART_CFG_DATALEN_8      0x20
#define UART_CFG_DATALEN_9      0x30
#define UART_CFG_PARITY_NONE    0x04
#define UART_CFG_PARITY_EVEN    0x08
#define UART_CFG_PARITY_ODD     0x0c
#define UART_CFG_STOPLEN_1      0x01
#define UART_CFG_STOPLEN_2      0x02

#define USIP_7N1 (UART_CFG_DATALEN_7 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1) //NOE
#define USIP_8N1 (UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1)
#define USIP_7N2 (UART_CFG_DATALEN_7 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_2)
#define USIP_8N2 (UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_2)
#define USIP_7E1 (UART_CFG_DATALEN_7 | UART_CFG_PARITY_EVEN | UART_CFG_STOPLEN_1) //EVEN
#define USIP_8E1 (UART_CFG_DATALEN_8 | UART_CFG_PARITY_EVEN | UART_CFG_STOPLEN_1)
#define USIP_7E2 (UART_CFG_DATALEN_7 | UART_CFG_PARITY_EVEN | UART_CFG_STOPLEN_2)
#define USIP_8E2 (UART_CFG_DATALEN_8 | UART_CFG_PARITY_EVEN | UART_CFG_STOPLEN_2)
#define USIP_7O1 (UART_CFG_DATALEN_7 | UART_CFG_PARITY_ODD  | UART_CFG_STOPLEN_1) //ODD
#define USIP_8O1 (UART_CFG_DATALEN_8 | UART_CFG_PARITY_ODD  | UART_CFG_STOPLEN_1)
#define USIP_7O2 (UART_CFG_DATALEN_7 | UART_CFG_PARITY_ODD  | UART_CFG_STOPLEN_2)
#define USIP_8O2 (UART_CFG_DATALEN_8 | UART_CFG_PARITY_ODD  | UART_CFG_STOPLEN_2)

extern bool us_tx_buff (void);
extern bool us_rx_buff (void);
extern void us_rx_purge (void);
extern void us_putc (char ch);
extern void us_puts (const char *s);
extern size_t us_putsb (const uint8_t *s, size_t len);
extern size_t us_putsbd (const uint8_t *s, size_t len);
extern char us_getc (void);
extern uint32_t us_setbaudrate (uint32_t baud);
extern uint32_t us_init (uint32_t baudrate, uint8_t us_param, uint8_t *txbuf, uint16_t txl, uint8_t *rxbuf, uint16_t rxl);
extern bool us_deinit (void);

extern int us_printf (const char *fmt, ...);

#endif /* USART_H_ */
