/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/** BusVoodoo generic UART mode (API)
 *  @note this only contains the common UART methods and should be supplied with mode specific methods and information
 *  @file busvoodoo_uart_generic.h
 *  @author King Kévin <kingkevin@cuvoodoo.info>
 *  @date 2018
 */
#include <libopencm3/stm32/timer.h> // timer library

/** UART specific methods that will be called by the generic methods */
struct busvoodoo_uart_generic_specific_t {
	uint32_t usart; /**< USART peripheral base address */
	uint32_t usart_rcc; /**< USART RCC address */
	uint32_t usart_rst; /**< USART RST address */
	bool multidrive; /**< if multiple drive modes are supported (push-pull, open-drain with internal resistors, open-drain with external resistors), or just push-pull */
	uint32_t tx_port; /**< GPIO port address of transmit pin */
	uint32_t tx_pin; /**< GPIO pin address of transmit pin */
	uint32_t tx_rcc; /**< GPIO RCC address of transmit pin */
	void (*tx_pre)(void); /**< method to be called before transmitting data */
	void (*tx_post)(void); /**< method to be called after transmitting data */
	uint32_t rx_port; /**< GPIO port address of receive pin */
	uint32_t rx_pin; /**< GPIO pin address of receive pin */
	uint32_t rx_rcc; /**< GPIO RCC address of receive pin */
	void (*rx_pre)(void); /**< method to be called before receiving data */
	void (*rx_post)(void); /**< method to be called after receiving data */
	/* hardware flow control configuration */
	bool hwflowctl; /**< if RTC/CTS hardware flow control is supported */
	uint32_t rts_port; /**< GPIO port address of request to send pin */
	uint32_t rts_pin; /**< GPIO pin address of request to send pin */
	uint32_t rts_rcc; /**< GPIO RCC address of request to send pin */
	uint32_t cts_port; /**< GPIO port address of clear to send pin */
	uint32_t cts_pin; /**< GPIO pin address of clear to send pin */
	uint32_t cts_rcc; /**< GPIO RCC address of clear to send pin */
	/* timer input channel capture to measure Rx edge timing */
	uint32_t timer; /**< timer peripheral */
	uint32_t timer_rcc; /**< timer RCC address */
	uint32_t timer_port; /**< port of timer capture channel */
	uint32_t timer_port_rcc; /**< port RCC of timer capture channel */
	uint32_t timer_pin; /**< pin of timer capture channel */
	enum tim_ic_id timer_ic; /**< timer input capture channel */
	enum tim_ic_input timer_ic_in_ti; /**< timer input capture channel TIn */
	uint32_t timer_sr_ccif; /**< timer channel capture interrupt flag */
	uint32_t timer_sr_ccof; /**< timer channel capture overrun flag */
	volatile uint32_t* timer_ccr; /**< timer channel capture register */
	uint32_t timer_dier_ccie; /**< timer channel capture interrupt enable */
	uint32_t timer_nvic_irq; /**< timer IRQ */
};

/** provide the generic USART with mode specific information
 *  @param[in] conf USART mode specific information
 *  @return if configuration is valid
 */
bool busvoodoo_uart_generic_configure(const struct busvoodoo_uart_generic_specific_t* conf);
/** setup generic UART mode
 *  @param[out] prefix terminal prompt prefix
 *  @param[in] line terminal prompt line to configure mode
 *  @return if setup is complete
 *  @note run busvoodoo_uart_generic_configure before
 */
bool busvoodoo_uart_generic_setup(char** prefix, const char* line);
/** exit genetic UART mode */
void busvoodoo_uart_generic_exit(void);
/** number of commands supported by the generic UART mode
 *  @warning this variable must be constant, thus be adjusted by hand corresponding to the actual content
 */
#define busvoodoo_uart_generic_commands_nb 6
/** commands supported by the generic UART mode */
extern const struct menu_command_t busvoodoo_uart_generic_commands[busvoodoo_uart_generic_commands_nb];
