-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathik485.c
229 lines (180 loc) · 5.25 KB
/
ik485.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Copyright (c) 2024, sulfurandcu.github.io
*
* SPDX-License-Identifier: MIT
*
* Change Logs:
* Date Author Notes
* 2024-12-08 liujitong first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <ik485.h>
#define DBG_TAG "ik485"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
static void ik485_set_tx_on(ik485_device_t *ik485)
{
RT_ASSERT(ik485 != RT_NULL);
if (ik485->tx_pin != PIN_NONE)
{
if (ik485->tx_level == 1)
rt_pin_write(ik485->tx_pin, PIN_HIGH);
else
rt_pin_write(ik485->tx_pin, PIN_LOW);
if (ik485->tx_delay)
rt_hw_us_delay(ik485->tx_delay);
}
}
static void ik485_set_tx_off(ik485_device_t *ik485)
{
RT_ASSERT(ik485 != RT_NULL);
if (ik485->tx_pin != PIN_NONE)
{
if (ik485->tx_level == 1)
rt_pin_write(ik485->tx_pin, PIN_LOW);
else
rt_pin_write(ik485->tx_pin, PIN_HIGH);
}
}
static rt_err_t ik485_init(struct rt_device *dev)
{
RT_ASSERT(dev != RT_NULL);
ik485_device_t *ik485 = (ik485_device_t *)dev;
if (ik485->tx_pin != PIN_NONE)
{
rt_pin_mode(ik485->tx_pin, PIN_MODE_OUTPUT);
}
return RT_EOK;
}
static rt_err_t ik485_open(struct rt_device *dev, rt_uint16_t oflag)
{
RT_ASSERT(dev != RT_NULL);
ik485_device_t *ik485 = (ik485_device_t *)dev;
if (rt_device_open(ik485->serial, oflag) != RT_EOK)
{
LOG_E("ik485: serial open fail.");
return -RT_ERROR;
}
if (ik485->tx_pin != PIN_NONE)
{
rt_pin_mode(ik485->tx_pin, PIN_MODE_OUTPUT);
}
return RT_EOK;
}
static rt_err_t ik485_close(struct rt_device *dev)
{
RT_ASSERT(dev != RT_NULL);
ik485_device_t *ik485 = (ik485_device_t *)dev;
ik485->rx_notify.dev = RT_NULL;
ik485->rx_notify.notify = RT_NULL;
rt_device_close(ik485->serial);
if (ik485->tx_pin != PIN_NONE)
{
rt_pin_mode(ik485->tx_pin, PIN_MODE_INPUT);
}
return RT_EOK;
}
static rt_ssize_t ik485_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{
RT_ASSERT(dev != RT_NULL && (buffer != RT_NULL));
if (size == 0) return 0;
ik485_device_t *ik485 = (ik485_device_t *)dev;
return rt_device_read(ik485->serial, pos, buffer, size);
}
static rt_ssize_t ik485_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
RT_ASSERT(dev != RT_NULL && (buffer != RT_NULL));
if (size == 0) return 0;
ik485_device_t *ik485 = (ik485_device_t *)dev;
ik485_set_tx_on(ik485);
rt_ssize_t len = rt_device_write(ik485->serial, pos, buffer, size);
ik485_set_tx_off(ik485);
return len;
}
static rt_err_t ik485_control(struct rt_device *dev, int cmd, void *args)
{
RT_ASSERT(dev != RT_NULL);
ik485_device_t *ik485 = (ik485_device_t *)dev;
return rt_device_control(ik485->serial, cmd, args);
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops ik485_ops =
{
ik485_init,
ik485_open,
ik485_close,
ik485_read,
ik485_write,
ik485_control
};
#endif
static rt_err_t ik485_register(ik485_device_t *ik485, const char *name, rt_uint32_t flag, void *data)
{
RT_ASSERT(ik485 != RT_NULL);
rt_device_t device = &(ik485->parent);
device->type = RT_Device_Class_Char;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &ik485_ops;
#else
device->init = ik485_init;
device->open = ik485_open;
device->close = ik485_close;
device->read = ik485_read;
device->write = ik485_write;
device->control = ik485_control;
#endif
device->user_data = data;
return rt_device_register(device, name, flag);
}
static void ik485_notify(rt_device_t dev)
{
RT_ASSERT(dev != RT_NULL);
ik485_device_t *ik485 = (ik485_device_t *)dev;
if (ik485->parent.rx_indicate)
{
ik485->parent.rx_indicate(&ik485->parent, 0);
}
}
ik485_device_t *ik485_create(const char *ik485_name, const char *serial_name,
rt_base_t tx_pin, rt_int32_t tx_level, rt_int32_t tx_delay)
{
RT_ASSERT(ik485_name != RT_NULL);
RT_ASSERT(serial_name != RT_NULL);
rt_device_t serial = rt_device_find(serial_name);
if (serial == RT_NULL)
{
LOG_E("ik485: create error, serial device(%s) not found.", serial_name);
return RT_NULL;
}
ik485_device_t *ik485 = rt_malloc(sizeof(ik485_device_t));
if (ik485 == RT_NULL)
{
LOG_E("ik485: create error, malloc fail.");
return RT_NULL;
}
ik485->serial = serial;
ik485->tx_pin = tx_pin;
ik485->tx_level = tx_level;
ik485->tx_delay = tx_delay;
ik485_register(ik485, ik485_name, RT_DEVICE_FLAG_RDWR, RT_NULL);
struct rt_device_notify notify;
notify.notify = ik485_notify;
notify.dev = &ik485->parent;
rt_device_control(ik485->serial, RT_DEVICE_CTRL_NOTIFY_SET, ¬ify);
LOG_D("ik485: create success: 0x%08x", ik485);
return RT_EOK;
}
rt_err_t ik485_delete(ik485_device_t *ik485)
{
if (ik485 == RT_NULL)
{
LOG_E("ik485: delete error, ik485 not exist.");
return -RT_ERROR;
}
rt_free(ik485);
LOG_D("ik485: delete success.");
return RT_EOK;
}