<commit_before>#include <libftdi1/ftdi.h>

#include "common.h"
#include "uart.h"
#include "params.h"
#include "samples.h"

static struct ftdi_context *ftdi;
static uint8_t uart_rx_buffer[UART_RX_BUFFER_LEN];
static uint8_t uart_tx_buffer[UART_RX_BUFFER_LEN];
static int uart_rx_buffer_write_idx = 0;
static int uart_rx_buffer_read_idx = 0;
static int uart_tx_buffer_idx = 0;

int uart_rx_byte(uint8_t* b) //{{{
{
	int i, attempt = 0;

	while (uart_rx_buffer_read_idx >= uart_rx_buffer_write_idx)
	{
		if (attempt++ >= UART_RX_ATTEMPTS)
		{
			return -1;
		}

		if ((uart_rx_buffer_write_idx = ftdi_read_data(ftdi, uart_rx_buffer, sizeof(uart_rx_buffer))) < 0)
		{
			return -1;
		}
		uart_rx_buffer_read_idx = 0;

		vverb_printf("uart_rx_byte: len:%d", uart_rx_buffer_write_idx);
		for (i = 0; i < uart_rx_buffer_write_idx; i++)
		{
			vverb_printf(" %.2X", uart_rx_buffer[i]);
		}
		vverb_printf("%s\n", "");
	}

	*b = uart_rx_buffer[uart_rx_buffer_read_idx++];
	return uart_rx_buffer_write_idx;
} //}}}

int uart_rx_bytes(uint8_t* type, void* b, uint16_t b_len) //{{{
{
	int i, ret;
	uint8_t* b_b = (uint8_t*)b;
	uint8_t b_tmp;
	uint8_t start_found = 0;
	uint8_t end_found = 0;
	uint16_t bytes_read = 0;

	vverb_printf("uart_rx_bytes: start\n%s", "");

	while (!(start_found && end_found))
	{
		if (uart_rx_byte(&b_tmp) <= 0)
		{
			vverb_printf("uart_rx_bytes: failed to get byte\n%s", "");
			return -1;
		}

		if (b_tmp == UART_ESC_DELIM)
		{
			vverb_printf("uart_rx_bytes: got escape\n%s", "");
			if (uart_rx_byte(&b_tmp) <= 0)
			{
				vverb_printf("uart_rx_bytes: got escape -- failed to get next byte\n%s", "");
				return -1;
			}

			if (bytes_read < b_len)
			{
				vverb_printf("uart_rx_byte: got escape --- stored escaped byte 0x%X\n", b_tmp);
				b_b[bytes_read++] = b_tmp;
			}
		}
		else if (b_tmp == UART_START_DELIM)
		{
			start_found = 1;
			bytes_read = 0;

			if (uart_rx_byte(type) <= 0)
			{
				vverb_printf("uart_rx_byte: got start -- failed to get next byte\n%s", "");
				return -1;
			}
			vverb_printf("uart_rx_byte: got start type 0x%X\n", *type);
		}
		else if (b_tmp == UART_END_DELIM)
		{
			vverb_printf("uart_rx_byte: got end\n%s", "");
			end_found = 1;
		}
		else if (bytes_read < b_len)
		{
			vverb_printf("uart_rx_byte: got byte 0x%X\n", b_tmp);
			b_b[bytes_read++] = b_tmp;
		}
    else
    {
			vverb_printf("uart_rx_byte: nothing worked so done\n%s", "");
      return -1;
    }
	}

	// verbose
	verb_printf("uart_rx_bytes: recv%s", "");
	for (i = 0; i < bytes_read; i++)
	{
		verb_printf(" %.2X", b_b[i]);
	}
	verb_printf("%s\n", "");

	return bytes_read;
} //}}}

void uart_tx_byte(uint8_t b) //{{{
{
	int i,j;
	int write_len;
	int to_write_len;

	uart_tx_buffer[uart_tx_buffer_idx++] = b;

	// reached end of frame so write it
	if (uart_tx_buffer_idx > 3 &&
		uart_tx_buffer[uart_tx_buffer_idx-1] == UART_END_DELIM &&
		!(uart_tx_buffer[uart_tx_buffer_idx-2] == UART_ESC_DELIM &&
		uart_tx_buffer[uart_tx_buffer_idx-3] != UART_ESC_DELIM))
	{
		to_write_len = uart_tx_buffer_idx;
		while (to_write_len > 0)
		{
			if ((write_len = ftdi_write_data(ftdi, uart_tx_buffer + (uart_tx_buffer_idx - to_write_len), to_write_len)) < 0)
			{
				fprintf(stderr, "unable to write to ftdi device: %d (%s)\n", write_len, ftdi_get_error_string(ftdi));
				exit(EXIT_FAILURE);
			}

			// verbose
			verb_printf("uart_tx_byte: sent %s", "");
			for (i = 0; i < write_len; i++)
			{
				verb_printf(" %.2X", uart_tx_buffer[i + (uart_tx_buffer_idx - to_write_len)]);
			}
			verb_printf("%s\n", "");

			to_write_len -= write_len;
		}
		uart_tx_buffer_idx = 0;
	}
} //}}}

void uart_tx_bytes(uint8_t type, void* b, uint16_t len) //{{{
{
	uint16_t i;
	uint8_t* b_b = (uint8_t*)b;

	// verbose
	verb_printf("uart_tx_bytes: send%s", "");
	for (i = 0; i < len; i++)
	{
		verb_printf(" %.2X", b_b[i]);
	}
	verb_printf("%s\n", "");

	uart_tx_byte(UART_START_DELIM);
	uart_tx_byte(type);

	for (i = 0; i < len; i++)
	{
		if (b_b[i] == UART_START_DELIM || b_b[i] == UART_END_DELIM || b_b[i] == UART_ESC_DELIM)
		{
			uart_tx_byte(UART_ESC_DELIM);	
		}

		uart_tx_byte(b_b[i]);
	}

	uart_tx_byte(UART_END_DELIM);
} //}}}

void uart_init() //{{{
{
	int ret, i;
	uint8_t zeros[10];

	memset(zeros, 0, sizeof(zeros));

	if ((ftdi = ftdi_new()) == 0)
	{
		fprintf(stderr, "ftdi_new failed\n");
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_usb_open(ftdi, 0x0403, 0x8077)) < 0)
	{
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_usb_reset(ftdi)) < 0)
	{
		fprintf(stderr, "unable to reset: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_set_baudrate(ftdi, BAUD_RATE)) < 0)
	{
		fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_set_line_property(ftdi, BITS_8, STOP_BIT_1, NONE)) < 0)
	{
		fprintf(stderr, "unable to set line properties: %d (%s)\n", ret, ftdi_get_error_string(ftdi));<commit_msg>demo PID updated<commit_after>#include <libftdi1/ftdi.h>

#include "common.h"
#include "uart.h"
#include "params.h"
#include "samples.h"

static struct ftdi_context *ftdi;
static uint8_t uart_rx_buffer[UART_RX_BUFFER_LEN];
static uint8_t uart_tx_buffer[UART_RX_BUFFER_LEN];
static int uart_rx_buffer_write_idx = 0;
static int uart_rx_buffer_read_idx = 0;
static int uart_tx_buffer_idx = 0;

int uart_rx_byte(uint8_t* b) //{{{
{
	int i, attempt = 0;

	while (uart_rx_buffer_read_idx >= uart_rx_buffer_write_idx)
	{
		if (attempt++ >= UART_RX_ATTEMPTS)
		{
			return -1;
		}

		if ((uart_rx_buffer_write_idx = ftdi_read_data(ftdi, uart_rx_buffer, sizeof(uart_rx_buffer))) < 0)
		{
			return -1;
		}
		uart_rx_buffer_read_idx = 0;

		vverb_printf("uart_rx_byte: len:%d", uart_rx_buffer_write_idx);
		for (i = 0; i < uart_rx_buffer_write_idx; i++)
		{
			vverb_printf(" %.2X", uart_rx_buffer[i]);
		}
		vverb_printf("%s\n", "");
	}

	*b = uart_rx_buffer[uart_rx_buffer_read_idx++];
	return uart_rx_buffer_write_idx;
} //}}}

int uart_rx_bytes(uint8_t* type, void* b, uint16_t b_len) //{{{
{
	int i, ret;
	uint8_t* b_b = (uint8_t*)b;
	uint8_t b_tmp;
	uint8_t start_found = 0;
	uint8_t end_found = 0;
	uint16_t bytes_read = 0;

	vverb_printf("uart_rx_bytes: start\n%s", "");

	while (!(start_found && end_found))
	{
		if (uart_rx_byte(&b_tmp) <= 0)
		{
			vverb_printf("uart_rx_bytes: failed to get byte\n%s", "");
			return -1;
		}

		if (b_tmp == UART_ESC_DELIM)
		{
			vverb_printf("uart_rx_bytes: got escape\n%s", "");
			if (uart_rx_byte(&b_tmp) <= 0)
			{
				vverb_printf("uart_rx_bytes: got escape -- failed to get next byte\n%s", "");
				return -1;
			}

			if (bytes_read < b_len)
			{
				vverb_printf("uart_rx_byte: got escape --- stored escaped byte 0x%X\n", b_tmp);
				b_b[bytes_read++] = b_tmp;
			}
		}
		else if (b_tmp == UART_START_DELIM)
		{
			start_found = 1;
			bytes_read = 0;

			if (uart_rx_byte(type) <= 0)
			{
				vverb_printf("uart_rx_byte: got start -- failed to get next byte\n%s", "");
				return -1;
			}
			vverb_printf("uart_rx_byte: got start type 0x%X\n", *type);
		}
		else if (b_tmp == UART_END_DELIM)
		{
			vverb_printf("uart_rx_byte: got end\n%s", "");
			end_found = 1;
		}
		else if (bytes_read < b_len)
		{
			vverb_printf("uart_rx_byte: got byte 0x%X\n", b_tmp);
			b_b[bytes_read++] = b_tmp;
		}
    else
    {
			vverb_printf("uart_rx_byte: nothing worked so done\n%s", "");
      return -1;
    }
	}

	// verbose
	verb_printf("uart_rx_bytes: recv%s", "");
	for (i = 0; i < bytes_read; i++)
	{
		verb_printf(" %.2X", b_b[i]);
	}
	verb_printf("%s\n", "");

	return bytes_read;
} //}}}

void uart_tx_byte(uint8_t b) //{{{
{
	int i,j;
	int write_len;
	int to_write_len;

	uart_tx_buffer[uart_tx_buffer_idx++] = b;

	// reached end of frame so write it
	if (uart_tx_buffer_idx > 3 &&
		uart_tx_buffer[uart_tx_buffer_idx-1] == UART_END_DELIM &&
		!(uart_tx_buffer[uart_tx_buffer_idx-2] == UART_ESC_DELIM &&
		uart_tx_buffer[uart_tx_buffer_idx-3] != UART_ESC_DELIM))
	{
		to_write_len = uart_tx_buffer_idx;
		while (to_write_len > 0)
		{
			if ((write_len = ftdi_write_data(ftdi, uart_tx_buffer + (uart_tx_buffer_idx - to_write_len), to_write_len)) < 0)
			{
				fprintf(stderr, "unable to write to ftdi device: %d (%s)\n", write_len, ftdi_get_error_string(ftdi));
				exit(EXIT_FAILURE);
			}

			// verbose
			verb_printf("uart_tx_byte: sent %s", "");
			for (i = 0; i < write_len; i++)
			{
				verb_printf(" %.2X", uart_tx_buffer[i + (uart_tx_buffer_idx - to_write_len)]);
			}
			verb_printf("%s\n", "");

			to_write_len -= write_len;
		}
		uart_tx_buffer_idx = 0;
	}
} //}}}

void uart_tx_bytes(uint8_t type, void* b, uint16_t len) //{{{
{
	uint16_t i;
	uint8_t* b_b = (uint8_t*)b;

	// verbose
	verb_printf("uart_tx_bytes: send%s", "");
	for (i = 0; i < len; i++)
	{
		verb_printf(" %.2X", b_b[i]);
	}
	verb_printf("%s\n", "");

	uart_tx_byte(UART_START_DELIM);
	uart_tx_byte(type);

	for (i = 0; i < len; i++)
	{
		if (b_b[i] == UART_START_DELIM || b_b[i] == UART_END_DELIM || b_b[i] == UART_ESC_DELIM)
		{
			uart_tx_byte(UART_ESC_DELIM);	
		}

		uart_tx_byte(b_b[i]);
	}

	uart_tx_byte(UART_END_DELIM);
} //}}}

void uart_init() //{{{
{
	int ret, i;
	uint8_t zeros[10];

	memset(zeros, 0, sizeof(zeros));

	if ((ftdi = ftdi_new()) == 0)
	{
		fprintf(stderr, "ftdi_new failed\n");
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_usb_open(ftdi, 0x0403, 0x8477)) < 0)
	{
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_usb_reset(ftdi)) < 0)
	{
		fprintf(stderr, "unable to reset: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_set_baudrate(ftdi, BAUD_RATE)) < 0)
	{
		fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
		exit(EXIT_FAILURE);
	}

	if ((ret = ftdi_set_line_property(ftdi, BITS_8, STOP_BIT_1, NONE)) < 0)
	{
		fprintf(stderr, "unable to set line properties: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
{'fraction_non_alphanumeric': 0.166351606805293, 'fraction_numerical': 0.01651576957516665, 'mean_word_length': 4.018472291562656, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1612259', 'n_tokens_mistral': 4994, 'n_tokens_neox': 4581, 'n_words': 821}