#include "hal/uart2.h"
#include "libs/circle_buffer.h"
#include "serial.h"

static c_buffer_t c_buff;
static u8 _tx_buf[256];

void serial_init(void){
	UART2_Init();
	circle_buffer_init(&c_buff , _tx_buf, sizeof(_tx_buf));
}

void serial_write(u8 *data, int len){
	circle_put_data(&c_buff, data, len);
	UART2_EnableTx();
}

bool uart_tx_finished(void) {
	u8 data;
	if (circle_get_one_data(&c_buff, &data) <= 0){
		return true;
	}
	UART2_TxData(data);
	return false;
}


int fputc(int c, FILE *fp){
	u8 data = c;
	serial_write(&data, 1);
	return 1;
}


