/* Reflow Oven Controller
 *
 * Copyright (C) 2020  Mario Hüttel <mario.huettel@gmx.net>
 *
 * This file is part of the Reflow Oven Controller Project.
 *
 * The reflow oven controller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * GDSII-Converter 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 the reflow oven controller project.
 * If not, see <http://www.gnu.org/licenses/>.
 */

#include <stm32/stm32f4xx.h>
#include <stm-periph/dma-ring-buffer.h>
#include <stdint.h>
#include <stddef.h>

#ifndef UART_UART_H_
#define UART_UART_H_

struct stm_uart {
	USART_TypeDef *uart_dev;
	uint32_t brr_val;
	uint8_t base_dma_num;
	DMA_Stream_TypeDef *dma_tx_stream;
	uint8_t dma_tx_trigger_channel;
	DMA_Stream_TypeDef *dma_rx_stream;
	uint8_t dma_rx_trigger_channel;
	char *dma_rx_buff;
	char *dma_tx_buff;
	size_t rx_buff_count;
	size_t tx_buff_count;
	volatile uint32_t *rcc_reg;
	uint8_t rcc_bit_no;
	struct dma_ring_buffer_to_mem rx_ring_buff;
	struct dma_ring_buffer_to_periph tx_ring_buff;
	uint8_t tx : 1;
	uint8_t rx : 1;
};

int uart_init(struct stm_uart *uart);

void uart_change_brr(struct stm_uart *uart, uint32_t brr);

void uart_disable(struct stm_uart *uart);

void uart_send_char(struct stm_uart *uart, char c);

void uart_send_array(struct stm_uart *uart, const char *data, uint32_t len);

void uart_send_string(struct stm_uart *uart, const char *string);

void uart_send_array_with_dma(struct stm_uart *uart, const char *data, uint32_t len);

void uart_send_string_with_dma(struct stm_uart *uart, const char *string);

int uart_receive_data_with_dma(struct stm_uart *uart, const char **data, size_t *len);

char uart_get_char(struct stm_uart *uart);

int uart_check_rx_avail(struct stm_uart *uart);

void uart_tx_dma_complete_int_callback(struct stm_uart *uart);

size_t uart_dma_tx_queue_avail(struct stm_uart *uart);

size_t uart_dma_rx_queue_avail(struct stm_uart *uart);

#endif /* UART_UART_H_ */
