-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo.c
235 lines (186 loc) · 6.15 KB
/
servo.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
230
231
232
233
234
235
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/gpio/consumer.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#define SERVO_DT_COMPATIBLE "servo"
#define SERVO_CLASS_NAME "servo-class"
struct gpio_descs *leds;
struct gpio_desc *detector;
static ssize_t servo_write_handler(struct file *filp, const char *data, size_t data_len, loff_t *offset);
static ssize_t servo_read_handler(struct file *filp, char *buffer, size_t length, loff_t *offset);
static int servo_open_handler(struct inode *inode, struct file *file);
static void led_on(struct gpio_descs *leds, const int led_idx);
struct servo_state {
unsigned curr_pos;
unsigned dest_pos;
enum state {IDLE, CALLIB, IN_PROGRESS} state;
};
struct servo {
/* char driver */
dev_t devt;
struct cdev cdev;
struct device *device;
struct class *class;
/* gpio */
struct gpio_descs *leds;
struct gpio_desc *detector;
int led_active_idx;
struct timer_list timer;
struct servo_state servo_state;
};
struct file_operations fops = {
.open = servo_open_handler,
.write = servo_write_handler,
.read = servo_read_handler
};
static ssize_t servo_callib_store (struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct servo *servo = (struct servo*)dev_get_drvdata(dev);
if (servo->servo_state.state == IDLE) {
servo->servo_state.state = CALLIB;
}
return count;
}
static DEVICE_ATTR(callib, S_IWUSR, NULL, servo_callib_store);
static ssize_t servo_write_handler(struct file *filp, const char *data, size_t data_len, loff_t *offset)
{
char private_buffer[10];
long received_value;
struct servo *servo;
if (data_len > 10) {
return -EMSGSIZE;
}
copy_from_user(private_buffer, data, data_len);
private_buffer[data_len] = 0;
printk("Got message %s of size %ld\n", private_buffer, data_len);
if (kstrtol(private_buffer, 10, &received_value) != 0) {
return -EINVAL;
}
servo = filp->private_data;
if (servo->servo_state.state != CALLIB && servo->servo_state.state != IN_PROGRESS) {
servo->servo_state.dest_pos = received_value;
servo->servo_state.state = IN_PROGRESS;
}
return data_len;
}
static ssize_t servo_read_handler(struct file *filp, char *buffer, size_t length, loff_t *offset)
{
char private_buffer[10];
int data_len;
struct servo *servo;
servo = filp->private_data;
data_len = snprintf(private_buffer, 10, "%d\n", servo->servo_state.curr_pos);
copy_to_user(buffer, private_buffer, data_len);
return data_len;
}
static int servo_open_handler(struct inode *inode, struct file *file)
{
struct servo *servo;
servo = container_of(inode->i_cdev, struct servo, cdev);
file->private_data = servo;
return 0;
}
static void led_on(struct gpio_descs *leds, const int led_idx)
{
int leds_ctr;
if ((led_idx < 0) || (led_idx >= leds->ndescs)) {
return;
}
for (leds_ctr = 0; leds_ctr<leds->ndescs; leds_ctr++) {
if (leds_ctr == led_idx) {
gpiod_set_value(leds->desc[leds_ctr], 1);
} else {
gpiod_set_value(leds->desc[leds_ctr], 0);
}
}
}
static void servo_update(struct timer_list *timer)
{
struct servo *servo = container_of(timer, struct servo, timer);
switch(servo->servo_state.state) {
case CALLIB:
if (gpiod_get_value(servo->detector)) {
servo->led_active_idx = (servo->led_active_idx + 1) % servo->leds->ndescs;
led_on(servo->leds, servo->led_active_idx);
} else {
servo->servo_state.state = IDLE;
servo->servo_state.curr_pos = 0;
servo->servo_state.dest_pos = 0;
}
break;
case IN_PROGRESS:
if (servo->servo_state.curr_pos > servo->servo_state.dest_pos) {
servo->led_active_idx = (servo->led_active_idx - 1) % servo->leds->ndescs;
led_on(servo->leds, servo->led_active_idx);
servo->servo_state.curr_pos--;
} else if (servo->servo_state.curr_pos < servo->servo_state.dest_pos) {
servo->led_active_idx = (servo->led_active_idx + 1) % servo->leds->ndescs;
led_on(servo->leds, servo->led_active_idx);
servo->servo_state.curr_pos++;
} else {
servo->servo_state.state = IDLE;
}
case IDLE:
default:
break;
}
mod_timer(timer, jiffies + msecs_to_jiffies(100));
}
static int servo_probe(struct platform_device *device)
{
struct servo *servo = devm_kzalloc(&device->dev, sizeof(struct servo), GFP_KERNEL);
dev_set_drvdata(&device->dev, servo);
servo->leds = devm_gpiod_get_array(&device->dev, "coil", GPIOD_OUT_LOW);
servo->detector = devm_gpiod_get(&device->dev, "detector", GPIOD_IN);
alloc_chrdev_region(&servo->devt, 0, 1, THIS_MODULE->name);
cdev_init(&servo->cdev, &fops);
servo->cdev.owner = THIS_MODULE;
cdev_add(&servo->cdev, servo->devt, 1);
servo->class = class_create(THIS_MODULE, SERVO_CLASS_NAME);
servo->device = device_create(servo->class, NULL, servo->devt, NULL, THIS_MODULE->name);
device_create_file(&device->dev, &dev_attr_callib);
servo->timer.function = servo_update;
servo->led_active_idx = 0;
led_on(servo->leds, 0);
servo->servo_state.state = CALLIB;
servo->timer.expires = jiffies + msecs_to_jiffies(200);
add_timer(&servo->timer);
printk("Servo probed!! %d\n", gpiod_get_value(servo->detector));
return 0;
}
static int servo_remove(struct platform_device *device)
{
struct servo *servo = (struct servo*)dev_get_drvdata(&device->dev);
device_remove_file(&device->dev, &dev_attr_callib);
del_timer(&servo->timer);
device_destroy(servo->class, servo->devt);
class_destroy(servo->class);
cdev_del(&servo->cdev);
unregister_chrdev_region(servo->devt, 1);
printk("Servo removed!!\n");
return 0;
}
static const struct of_device_id gpio_pins_match[] = {
{ .compatible = "servo" },
{ /* Intentionally left blank */ },
};
static struct platform_driver servo_driver = {
.probe = servo_probe,
.remove = servo_remove,
.driver = {
.name = THIS_MODULE->name,
.owner = THIS_MODULE,
.of_match_table = gpio_pins_match,
},
};
module_platform_driver(servo_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Filip Zajdel <zajdel.filip97@gmail.com>");