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

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2023 Takayoshi Uemitsu (BestTechnology CO.,LTD.)
 *
 * 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 THE
 * AUTHORS OR COPYRIGHT HOLDERS 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.
 */

#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_ */
