forked from arduino/ArduinoCore-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzephyrSerial.h
104 lines (87 loc) · 2.55 KB
/
zephyrSerial.h
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
/*
* Copyright (c) 2022 Dhruva Gole
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <zephyr/sys/ring_buffer.h>
#include <api/HardwareSerial.h>
namespace arduino {
class ZephyrSerialStub : public HardwareSerial
{
public:
void begin(__attribute__((unused)) unsigned long baudRate) { }
void begin(__attribute__((unused)) unsigned long baudrate, __attribute__((unused)) uint16_t config) { }
void end() { }
int available() { return 0; }
int peek() { return 0; }
int read() { return 0; }
void flush() { }
size_t write(const uint8_t data)
{
printk("%c", static_cast<char>(data));
return 1;
}
operator bool() { return true; }
};
class ZephyrSerial : public HardwareSerial
{
public:
template <int SZ>
class ZephyrSerialBuffer
{
friend arduino::ZephyrSerial;
struct ring_buf ringbuf;
uint8_t buffer[SZ];
struct k_sem sem;
ZephyrSerialBuffer()
{
k_sem_init(&sem, 1, 1);
ring_buf_init(&ringbuf, sizeof(buffer), buffer);
}
};
ZephyrSerial(const struct device *dev) : uart(dev) { }
void begin(unsigned long baudrate, uint16_t config);
void begin(unsigned long baudrate) { begin(baudrate, SERIAL_8N1); }
void flush();
void end() { }
size_t write(const uint8_t *buffer, size_t size);
size_t write(const uint8_t data) { return write(&data, 1); }
using Print::write; // pull in write(str) and write(buf, size) from Print
int available();
int availableForWrite();
int peek();
int read();
operator bool()
{
return true;
}
friend class SerialUSB_;
protected:
void IrqHandler();
static void IrqDispatch(const struct device *dev, void *data);
const struct device *uart;
ZephyrSerialBuffer<CONFIG_ARDUINO_API_SERIAL_BUFFER_SIZE> tx;
ZephyrSerialBuffer<CONFIG_ARDUINO_API_SERIAL_BUFFER_SIZE> rx;
};
} // namespace arduino
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
#if !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)
// If CDC USB, use that object as Serial (and SerialUSB)
extern arduino::ZephyrSerial Serial;
#endif
#if (DT_PROP_LEN(DT_PATH(zephyr_user), serials) > 1)
#define SERIAL_DEFINED_0 1
#define EXTERN_SERIAL_N(i) extern arduino::ZephyrSerial Serial##i;
#define DECLARE_EXTERN_SERIAL_N(n, p, i) COND_CODE_1(SERIAL_DEFINED_##i, (), (EXTERN_SERIAL_N(i)))
/* Declare Serial1, Serial2, ... */
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), serials, DECLARE_EXTERN_SERIAL_N)
#undef DECLARE_EXTERN_SERIAL_N
#undef EXTERN_SERIAL_N
#undef SERIAL_DEFINED_0
#endif
#elif DT_NODE_HAS_STATUS(DT_NODELABEL(arduino_serial), okay)
extern arduino::ZephyrSerial Serial;
#else
extern arduino::ZephyrSerialStub Serial;
#endif