serial.c 1.32 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdint.h>
#include <stdio.h>

#include "serial.h"

#include "cdcacm.h"
#include "uart.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

extern mxc_uart_regs_t * ConsoleUart;

static QueueHandle_t read_queue;

void epic_uart_write_str(char*str, intptr_t length)
{
	UART_Write(ConsoleUart, (uint8_t*)str, length);
	cdcacm_write((uint8_t*)str, length);
}

char epic_uart_read_chr(void)
{
	char chr;
	xQueueReceive(read_queue, &chr, portMAX_DELAY);
	return chr;
}

void vSerialTask(void*pvParameters)
{
	static uint8_t buffer[sizeof(char) * SERIAL_READ_BUFFER_SIZE];
	static StaticQueue_t read_queue_data;

	/* Setup read queue */
	read_queue = xQueueCreateStatic(
		SERIAL_READ_BUFFER_SIZE,
		sizeof(char),
		buffer,
		&read_queue_data
	);

	/* Setup UART interrupt */
	NVIC_ClearPendingIRQ(UART0_IRQn);
	NVIC_DisableIRQ(UART0_IRQn);
	NVIC_SetPriority(UART0_IRQn, 1);
	NVIC_EnableIRQ(UART0_IRQn);

	while (1) {
		char chr;

		/* TODO: Wait for interrupt on either device */
		vTaskDelay(portTICK_PERIOD_MS * 10);

		if(UART_NumReadAvail(ConsoleUart) > 0) {
			chr = UART_ReadByte(ConsoleUart);
		} else if(cdcacm_num_read_avail() > 0) {
			chr = cdcacm_read();
		} else {
			continue;
		}

		if (xQueueSend(read_queue, &chr, 100) == errQUEUE_FULL) {
			/* Queue overran, wait a bit */
			vTaskDelay(portTICK_PERIOD_MS * 50);
		}
	}
}