This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart_terminal.cpp
85 lines (69 loc) · 2.32 KB
/
uart_terminal.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "uart_terminal.h"
#ifdef HAL_UART_MODULE_ENABLED
namespace mc {
uart_terminal::uart_terminal (const uart_cfg *const cfg, uint32_t cfg_num, uint32_t thread_prio) :
uart_base(cfg, cfg_num) {
USER_OS_STATIC_TASK_CREATE(uart_terminal::thread,
"uart_terminal",
TB_THREAD_SIZE,
this,
thread_prio,
this->tb_thread,
&this->ts_thread);
this->q_answer = USER_OS_STATIC_QUEUE_CREATE(Q_LEN, Q_ITEM_SIZE, this->qb, &this->qs);
}
void uart_terminal::char_parser (char c, uint32_t &i) {
if (c != '\r') {
if ((c == this->BACK_SPACE_8) || (c == this->BACK_SPACE_127)) {
if (this->input_pos > 0) {
this->input_pos--;
this->buf_repack_answer[i++] = c;
this->cfg->byte_handler(c);
return;
}
} else {
this->buf_repack_answer[i++] = c;
this->input_pos++;
this->cfg->byte_handler(c);
}
} else {
this->buf_repack_answer[i++] = '\n';
this->buf_repack_answer[i++] = '\r';
this->tx(this->buf_repack_answer, i, 100);
i = 0;
this->input_pos = 0;
this->cfg->byte_handler(c);
}
}
void uart_terminal::thread (void *obj) {
auto *o = reinterpret_cast<uart_terminal*>(obj);
while(true) {
uint32_t i = 0;
uint8_t b_char = 0;
USER_OS_QUEUE_RECEIVE(o->q_answer, &b_char, portMAX_DELAY);
o->char_parser(b_char, i);
for (;i < sizeof(buf_repack_answer); i++) {
if (USER_OS_QUEUE_RECEIVE(o->q_answer, &b_char, 10) == pdFALSE) {
break;
}
o->char_parser(b_char, i);
}
o->tx(o->buf_repack_answer, i, 100);
}
}
void uart_terminal::uart_irq_handler (void) {
static BaseType_t hp = pdFALSE;
volatile uint8_t sr = this->u.Instance->SR;
uint8_t data = this->u.Instance->DR;
if (this->cfg->byte_handler) {
if ((sr &UART_FLAG_RXNE) == UART_FLAG_RXNE) {
xQueueSendFromISR(this->q_answer, &data, &hp);
if (hp == pdTRUE) {
taskYIELD();
}
}
}
HAL_UART_IRQHandler(&this->u);
}
}
#endif