serial.c 2.59 KB
Newer Older
Rahix's avatar
Rahix committed
1
2
3
#include <stdint.h>
#include <stdio.h>

Rahix's avatar
Rahix committed
4
#include "max32665.h"
Rahix's avatar
Rahix committed
5
6
7
8
9
10
11
#include "cdcacm.h"
#include "uart.h"

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

12
#include "modules.h"
13
#include "modules/log.h"
14
#include "api/interrupt-sender.h"
15

Rahix's avatar
Rahix committed
16
/* Task ID for the serial handler */
Rahix's avatar
Rahix committed
17
TaskHandle_t serial_task_id = NULL;
Rahix's avatar
Rahix committed
18

Rahix's avatar
Rahix committed
19
/* The serial console in use (UART0) */
Rahix's avatar
Rahix committed
20
extern mxc_uart_regs_t *ConsoleUart;
Rahix's avatar
Rahix committed
21
/* Read queue, filled by both UART and CDCACM */
Rahix's avatar
Rahix committed
22
23
static QueueHandle_t read_queue;

Rahix's avatar
Rahix committed
24
25
26
/*
 * API-call to write a string.  Output goes to both CDCACM and UART
 */
27
void epic_uart_write_str(const char *str, intptr_t length)
Rahix's avatar
Rahix committed
28
{
Rahix's avatar
Rahix committed
29
30
	UART_Write(ConsoleUart, (uint8_t *)str, length);
	cdcacm_write((uint8_t *)str, length);
Rahix's avatar
Rahix committed
31
32
}

Rahix's avatar
Rahix committed
33
34
35
/*
 * Blocking API-call to read a character from the queue.
 */
Rahix's avatar
Rahix committed
36
37
38
39
40
41
42
char epic_uart_read_chr(void)
{
	char chr;
	xQueueReceive(read_queue, &chr, portMAX_DELAY);
	return chr;
}

Rahix's avatar
Rahix committed
43
44
45
46
47
48
/* Interrupt handler needed for SDK UART implementation */
void UART0_IRQHandler(void)
{
	UART_Handler(ConsoleUart);
}

Rahix's avatar
Rahix committed
49
static void uart_callback(uart_req_t *req, int error)
Rahix's avatar
Rahix committed
50
51
52
53
54
55
56
57
58
59
{
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	vTaskNotifyGiveFromISR(serial_task_id, &xHigherPriorityTaskWoken);
	portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

static void enqueue_char(char chr)
{
	if (chr == 0x3) {
		/* Control-C */
60
		api_interrupt_trigger(EPIC_INT_CTRL_C);
61
62
63
64
	}

	if (chr == 0x0e) {
		/* Control-N */
65
		api_interrupt_trigger(EPIC_INT_BHI160_TEST);
Rahix's avatar
Rahix committed
66
67
68
69
70
71
72
73
	}

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

Rahix's avatar
Rahix committed
74
void vSerialTask(void *pvParameters)
Rahix's avatar
Rahix committed
75
76
77
78
{
	static uint8_t buffer[sizeof(char) * SERIAL_READ_BUFFER_SIZE];
	static StaticQueue_t read_queue_data;

Rahix's avatar
Rahix committed
79
80
	serial_task_id = xTaskGetCurrentTaskHandle();

Rahix's avatar
Rahix committed
81
82
	/* Setup read queue */
	read_queue = xQueueCreateStatic(
Rahix's avatar
Rahix committed
83
		SERIAL_READ_BUFFER_SIZE, sizeof(char), buffer, &read_queue_data
Rahix's avatar
Rahix committed
84
85
86
87
88
	);

	/* Setup UART interrupt */
	NVIC_ClearPendingIRQ(UART0_IRQn);
	NVIC_DisableIRQ(UART0_IRQn);
Rahix's avatar
Rahix committed
89
	NVIC_SetPriority(UART0_IRQn, 6);
Rahix's avatar
Rahix committed
90
91
	NVIC_EnableIRQ(UART0_IRQn);

Rahix's avatar
Rahix committed
92
93
94
95
96
97
	unsigned char data;
	uart_req_t read_req = {
		.data     = &data,
		.len      = 1,
		.callback = uart_callback,
	};
Rahix's avatar
Rahix committed
98

Rahix's avatar
Rahix committed
99
100
101
	while (1) {
		int ret = UART_ReadAsync(ConsoleUart, &read_req);
		if (ret != E_NO_ERROR && ret != E_BUSY) {
102
			LOG_ERR("serial", "error reading uart: %d", ret);
Rahix's avatar
Rahix committed
103
			vTaskDelay(portMAX_DELAY);
Rahix's avatar
Rahix committed
104
105
		}

Rahix's avatar
Rahix committed
106
107
108
109
		ulTaskNotifyTake(pdTRUE, portTICK_PERIOD_MS * 1000);

		if (read_req.num > 0) {
			enqueue_char(*read_req.data);
110
111
		}

Rahix's avatar
Rahix committed
112
113
		while (UART_NumReadAvail(ConsoleUart) > 0) {
			enqueue_char(UART_ReadByte(ConsoleUart));
Rahix's avatar
Rahix committed
114
		}
Rahix's avatar
Rahix committed
115
116
117
118

		while (cdcacm_num_read_avail() > 0) {
			enqueue_char(cdcacm_read());
		}
Rahix's avatar
Rahix committed
119
120
	}
}