-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkobuki_serial.c
75 lines (63 loc) · 1.56 KB
/
kobuki_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
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-05-28 hw630 the first version
*/
#include "kobuki_serial.h"
#define DBG_SECTION_NAME "kobuki_serial"
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
static struct rt_semaphore rx_sem;
static rt_device_t kobuki_serial;
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem);
return RT_EOK;
}
int kobuki_serial_init()
{
kobuki_serial = rt_device_find(KOBUKI_SERIAL_NAME);
if (!kobuki_serial)
{
LOG_E("Failed to open device %s", KOBUKI_SERIAL_NAME);
return -1;
}
rt_sem_init(&rx_sem, "kobuki_rx_sem", 0, RT_IPC_FLAG_FIFO);
rt_device_open(kobuki_serial, RT_DEVICE_FLAG_INT_RX);
rt_device_set_rx_indicate(kobuki_serial, uart_input);
return 0;
}
char kobuki_serial_read(char* ch)
{
int tick = kobuki_get_tick();
while (rt_device_read(kobuki_serial, -1, ch, 1) != 1)
{
rt_sem_take(&rx_sem, KOBUKI_SERIAL_TIMEOUT / 5);
if( (kobuki_get_tick() - tick) > KOBUKI_SERIAL_TIMEOUT)
{
return 0;
}
}
return 1;
}
int kobuki_serial_write_char(uint8_t ch)
{
return rt_device_write(kobuki_serial, 0, &ch, 1);
}
int kobuki_serial_write(uint8_t* dat, int len)
{
return rt_device_write(kobuki_serial, 0, dat, len);
}
void kobuki_serial_close()
{
rt_device_close(kobuki_serial);
rt_sem_detach(&rx_sem);
}
uint32_t kobuki_get_tick()
{
return rt_tick_get();
}