forked from lukicdarkoo/pico-micro-ros
-
Notifications
You must be signed in to change notification settings - Fork 56
/
pico_uart_transport.c
executable file
·72 lines (63 loc) · 1.62 KB
/
pico_uart_transport.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
#include <stdio.h>
#include <time.h>
#include "pico/stdlib.h"
#include <uxr/client/profile/transport/custom/custom_transport.h>
void usleep(uint64_t us)
{
sleep_us(us);
}
int clock_gettime(clockid_t unused, struct timespec *tp)
{
uint64_t m = time_us_64();
tp->tv_sec = m / 1000000;
tp->tv_nsec = (m % 1000000) * 1000;
return 0;
}
bool pico_serial_transport_open(struct uxrCustomTransport * transport)
{
// Ensure that stdio_init_all is only called once on the runtime
static bool require_init = true;
if(require_init)
{
stdio_init_all();
require_init = false;
}
return true;
}
bool pico_serial_transport_close(struct uxrCustomTransport * transport)
{
return true;
}
size_t pico_serial_transport_write(struct uxrCustomTransport * transport, uint8_t *buf, size_t len, uint8_t *errcode)
{
for (size_t i = 0; i < len; i++)
{
if (buf[i] != putchar(buf[i]))
{
*errcode = 1;
return i;
}
}
return len;
}
size_t pico_serial_transport_read(struct uxrCustomTransport * transport, uint8_t *buf, size_t len, int timeout, uint8_t *errcode)
{
uint64_t start_time_us = time_us_64();
for (size_t i = 0; i < len; i++)
{
int64_t elapsed_time_us = timeout * 1000 - (time_us_64() - start_time_us);
if (elapsed_time_us < 0)
{
*errcode = 1;
return i;
}
int character = getchar_timeout_us(elapsed_time_us);
if (character == PICO_ERROR_TIMEOUT)
{
*errcode = 1;
return i;
}
buf[i] = character;
}
return len;
}