-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
135 lines (112 loc) · 2.96 KB
/
serial.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* File: serial.c
* Project: Klik
* -----
* This source code is released under BSD-3 license.
* Check LICENSE file for full list of conditions and disclaimer.
* -----
* Copyright 2022 - 2023 M.Kusiak (timax)
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "serial.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#define SERIAL_UART_ID uart0
#define SERIAL_BAUD_RATE 115200
#define SERIAL_DATA_BITS 8
#define SERIAL_STOP_BITS 1
#define SERIAL_PARITY 0
/**
* @brief Initiates and setups serial communication for uart.
*/
void serialUartInit()
{
uart_init(SERIAL_UART_ID, SERIAL_BAUD_RATE);
uart_set_format(SERIAL_UART_ID, SERIAL_DATA_BITS, SERIAL_STOP_BITS, SERIAL_PARITY);
uart_set_fifo_enabled(SERIAL_UART_ID, true);
uart_set_hw_flow(SERIAL_UART_ID, false, false);
}
/**
* @brief Gets last full line from uart.
*
* @return char* Last full line from uart, or 0 when empty.
*/
char *serialUartGetLastLine()
{
static char income[SERIAL_MAX_INCOME_LEN];
static uint16_t incomeIndex;
char symbol;
if (!income[0])
incomeIndex = 0;
if (!incomeIndex)
memset(income, 0, sizeof income);
while (uart_is_readable(SERIAL_UART_ID))
{
symbol = uart_getc(SERIAL_UART_ID);
if (symbol == '\n' || symbol == '\r' || (incomeIndex > SERIAL_MAX_INCOME_LEN - 1))
{
income[incomeIndex] = 0;
incomeIndex = 0;
return income;
}
income[incomeIndex] = symbol;
incomeIndex++;
}
return 0;
}
char *serialUsbGetLastLine()
{
static char income[SERIAL_MAX_INCOME_LEN];
static uint16_t incomeIndex;
char symbol;
if (!income[0])
incomeIndex = 0;
if (!incomeIndex)
memset(income, 0, sizeof income);
while (true)
{
symbol = getchar_timeout_us(0);
if (symbol == 255)
break;
if (symbol == '\n' || symbol == '\r' || (incomeIndex > SERIAL_MAX_INCOME_LEN - 1))
{
income[incomeIndex] = 0;
incomeIndex = 0;
return income;
}
income[incomeIndex] = symbol;
incomeIndex++;
}
return 0;
}
/**
* @brief Prints text to serial.
* This differs from printf, that it checks if uart is writable.
*
* @param line text to print.
* @return true printing succeeded.
* @return false printing failed, uart unavailable.
*/
bool serialUartSendLine(char *line)
{
if (uart_is_writable(SERIAL_UART_ID))
{
printf("%s\n", line);
return true;
}
return false;
}
/**
* @brief Sets interrupt on uart activity with handler function.
*
* @param handlerFunction handler function.
*/
void serialUartSetInterruptHandler(void *handlerFunction)
{
static int UART_IRQ = SERIAL_UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
irq_set_exclusive_handler(UART_IRQ, handlerFunction);
irq_set_enabled(UART_IRQ, true);
uart_set_irq_enables(SERIAL_UART_ID, true, false);
}