forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_io_ameba.cc
87 lines (73 loc) · 2.09 KB
/
sys_io_ameba.cc
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
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pw_sys_io/sys_io.h"
#include <cassert>
#include <cinttypes>
#include <stdio.h>
#include <string.h>
#include "serial_api.h"
#define UART_TX PA_18
#define UART_RX PA_19
serial_t sobj;
int console_getchar(uint8_t * chr)
{
*chr = (uint8_t) serial_getc(&sobj);
return 1;
}
int console_putchar(const char * chr)
{
serial_putc(&sobj, *chr);
return 1;
}
void console_init(void)
{
serial_init(&sobj, UART_TX, UART_RX);
serial_baud(&sobj, 115200);
serial_format(&sobj, 8, ParityNone, 1);
}
extern "C" void pw_sys_io_Init()
{
console_init();
}
namespace pw::sys_io {
Status ReadByte(std::byte * dest)
{
if (!dest)
return Status::InvalidArgument();
int ret = console_getchar(reinterpret_cast<uint8_t *>(dest));
return ret < 0 ? Status::FailedPrecondition() : OkStatus();
}
Status WriteByte(std::byte b)
{
int ret = console_putchar(reinterpret_cast<const char *>(&b));
return ret < 0 ? Status::FailedPrecondition() : OkStatus();
}
// Writes a string using pw::sys_io, and add newline characters at the end.
StatusWithSize WriteLine(std::string_view s)
{
size_t chars_written = 0;
StatusWithSize result = WriteBytes(pw::as_bytes(pw::span(s)));
if (!result.ok())
{
return result;
}
chars_written += result.size();
result = WriteBytes(pw::as_bytes(pw::span("\r\n", 2)));
chars_written += result.size();
return StatusWithSize(result.status(), chars_written);
}
} // namespace pw::sys_io