serial.c 3.3 KB
Newer Older
1
2
3
4
#include "epicardium.h"
#include "api/interrupt-sender.h"
#include "modules/log.h"
#include "modules/modules.h"
Rahix's avatar
Rahix committed
5

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

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

14
15
#include <stdint.h>
#include <stdio.h>
16

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

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

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

Rahix's avatar
Rahix committed
35
/*
36
 * API-call to read a character from the queue.
Rahix's avatar
Rahix committed
37
 */
38
int epic_uart_read_char(void)
Rahix's avatar
Rahix committed
39
40
{
	char chr;
41
42
43
44
	if (xQueueReceive(read_queue, &chr, 0) == pdTRUE) {
		return (int)chr;
	}
	return (-1);
Rahix's avatar
Rahix committed
45
46
}

47
48
49
50
51
/*
 * API-call to read data from the queue.
 */
int epic_uart_read_str(char *buf, size_t cnt)
{
52
	size_t i = 0;
53
54
55
56
57
58
59
60
61
62

	for (i = 0; i < cnt; i++) {
		if (xQueueReceive(read_queue, &buf[i], 0) != pdTRUE) {
			break;
		}
	}

	return i;
}

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
long _write_epicardium(int fd, const char *buf, size_t cnt)
{
	/*
	 * Only print one line at a time.  Insert `\r` between lines so they are
	 * properly displayed on the serial console.
	 */
	size_t i, last = 0;
	for (i = 0; i < cnt; i++) {
		if (buf[i] == '\n') {
			epic_uart_write_str(&buf[last], i - last);
			epic_uart_write_str("\r", 1);
			last = i;
		}
	}
	epic_uart_write_str(&buf[last], cnt - last);
	return cnt;
}

Rahix's avatar
Rahix committed
81
82
83
84
85
86
/* Interrupt handler needed for SDK UART implementation */
void UART0_IRQHandler(void)
{
	UART_Handler(ConsoleUart);
}

Rahix's avatar
Rahix committed
87
static void uart_callback(uart_req_t *req, int error)
Rahix's avatar
Rahix committed
88
89
90
91
92
93
{
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	vTaskNotifyGiveFromISR(serial_task_id, &xHigherPriorityTaskWoken);
	portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

94
void serial_enqueue_char(char chr)
Rahix's avatar
Rahix committed
95
96
97
{
	if (chr == 0x3) {
		/* Control-C */
98
		api_interrupt_trigger(EPIC_INT_CTRL_C);
99
100
	}

Rahix's avatar
Rahix committed
101
102
103
104
	if (xQueueSend(read_queue, &chr, 100) == errQUEUE_FULL) {
		/* Queue overran, wait a bit */
		vTaskDelay(portTICK_PERIOD_MS * 50);
	}
105
106

	api_interrupt_trigger(EPIC_INT_UART_RX);
Rahix's avatar
Rahix committed
107
108
}

Rahix's avatar
Rahix committed
109
void vSerialTask(void *pvParameters)
Rahix's avatar
Rahix committed
110
111
112
113
{
	static uint8_t buffer[sizeof(char) * SERIAL_READ_BUFFER_SIZE];
	static StaticQueue_t read_queue_data;

Rahix's avatar
Rahix committed
114
115
	serial_task_id = xTaskGetCurrentTaskHandle();

Rahix's avatar
Rahix committed
116
117
	/* Setup read queue */
	read_queue = xQueueCreateStatic(
Rahix's avatar
Rahix committed
118
		SERIAL_READ_BUFFER_SIZE, sizeof(char), buffer, &read_queue_data
Rahix's avatar
Rahix committed
119
120
121
122
123
	);

	/* Setup UART interrupt */
	NVIC_ClearPendingIRQ(UART0_IRQn);
	NVIC_DisableIRQ(UART0_IRQn);
Rahix's avatar
Rahix committed
124
	NVIC_SetPriority(UART0_IRQn, 6);
Rahix's avatar
Rahix committed
125
126
	NVIC_EnableIRQ(UART0_IRQn);

Rahix's avatar
Rahix committed
127
128
129
130
131
132
	unsigned char data;
	uart_req_t read_req = {
		.data     = &data,
		.len      = 1,
		.callback = uart_callback,
	};
Rahix's avatar
Rahix committed
133

Rahix's avatar
Rahix committed
134
135
136
	while (1) {
		int ret = UART_ReadAsync(ConsoleUart, &read_req);
		if (ret != E_NO_ERROR && ret != E_BUSY) {
137
			LOG_ERR("serial", "error reading uart: %d", ret);
Rahix's avatar
Rahix committed
138
			vTaskDelay(portMAX_DELAY);
Rahix's avatar
Rahix committed
139
140
		}

Rahix's avatar
Rahix committed
141
142
143
		ulTaskNotifyTake(pdTRUE, portTICK_PERIOD_MS * 1000);

		if (read_req.num > 0) {
144
			serial_enqueue_char(*read_req.data);
145
146
		}

Rahix's avatar
Rahix committed
147
		while (UART_NumReadAvail(ConsoleUart) > 0) {
148
			serial_enqueue_char(UART_ReadByte(ConsoleUart));
Rahix's avatar
Rahix committed
149
		}
Rahix's avatar
Rahix committed
150
151

		while (cdcacm_num_read_avail() > 0) {
152
			serial_enqueue_char(cdcacm_read());
Rahix's avatar
Rahix committed
153
		}
Rahix's avatar
Rahix committed
154
155
	}
}