-
Notifications
You must be signed in to change notification settings - Fork 0
/
ara_tester_axis.h
144 lines (128 loc) · 5.34 KB
/
ara_tester_axis.h
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
#ifndef _ARA_TESTER_AXIS_H
#define _ARA_TESTER_AXIS_H
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/slab.h>
#include "output_pin.h"
MODULE_LICENSE("GPL v2");
struct ara_tester_axis {
struct output_pin pulse_pin;
struct output_pin direction_pin;
struct hrtimer timer;
unsigned long t_max;
unsigned long t_min;
unsigned long t_delta;
unsigned long pulse_width;
unsigned long t_current;
unsigned long count;
unsigned long progressive;
unsigned long linear;
unsigned long counter;
int movment_state;
int active;
int even;
int direction;
int pulse;
int in_use;
};
typedef enum hrtimer_restart (*hrtimer_function)(struct hrtimer*);
static inline struct hrtimer* __ara_tester_axis_timer_pointer(struct ara_tester_axis* ara_tester_axis) {
return &(ara_tester_axis->timer);
}
static inline struct output_pin* __ara_tester_axis_pulse_pin_pointer(struct ara_tester_axis* ara_tester_axis) {
return &(ara_tester_axis->pulse_pin);
}
static inline struct output_pin* __ara_tester_axis_direction_pin_pointer(struct ara_tester_axis* ara_tester_axis) {
return &(ara_tester_axis->direction_pin);
}
static inline struct ara_tester_axis* ara_tester_axis_alloc(int pulse_pin, int direction_pin, hrtimer_function function) {
struct ara_tester_axis* ara_tester_axis;
ara_tester_axis = (struct ara_tester_axis*)kmalloc(sizeof(struct ara_tester_axis), GFP_KERNEL);
if(ara_tester_axis) {
if(output_pin_init(__ara_tester_axis_pulse_pin_pointer(ara_tester_axis), pulse_pin) || output_pin_init(__ara_tester_axis_direction_pin_pointer(ara_tester_axis), direction_pin)) {
printk("Error: can't request one or both of that gpios: %d, %d\n", pulse_pin, direction_pin);
kfree(ara_tester_axis);
return NULL;
}
hrtimer_init(__ara_tester_axis_timer_pointer(ara_tester_axis), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
ara_tester_axis->timer.function = function;
ara_tester_axis->active = 0;
ara_tester_axis->in_use = 0;
ara_tester_axis->pulse_width = 0;
ara_tester_axis->t_max = 0;
ara_tester_axis->t_min = 0;
ara_tester_axis->t_delta = 0;
}
return ara_tester_axis;
}
static inline void ara_tester_axis_stop(struct ara_tester_axis* ara_tester_axis) {
ara_tester_axis->active = 0;
while(hrtimer_try_to_cancel(__ara_tester_axis_timer_pointer(ara_tester_axis)));
}
static inline void ara_tester_axis_clean(struct ara_tester_axis* ara_tester_axis) {
if(ara_tester_axis) {
ara_tester_axis_stop(ara_tester_axis);
output_pin_clean(__ara_tester_axis_pulse_pin_pointer(ara_tester_axis));
output_pin_clean(__ara_tester_axis_direction_pin_pointer(ara_tester_axis));
kfree(ara_tester_axis);
}
}
static inline void ara_tester_axis_exec(struct ara_tester_axis* ara_tester_axis) {
ara_tester_axis->active = 1;
ara_tester_axis->pulse = 0;
ara_tester_axis->counter = 0;
ara_tester_axis->movment_state = 0;
ara_tester_axis->t_current = ara_tester_axis->t_max;
ara_tester_axis->count = ara_tester_axis->progressive;
output_pin_set_state(__ara_tester_axis_direction_pin_pointer(ara_tester_axis), ara_tester_axis->direction);
ara_tester_axis->timer.function(__ara_tester_axis_timer_pointer(ara_tester_axis));
}
static inline enum hrtimer_restart ara_tester_axis_change_state(struct ara_tester_axis* ara_tester_axis) {
if(ara_tester_axis->active) {
unsigned long timeout = ara_tester_axis->pulse ? ara_tester_axis->t_current : ara_tester_axis->pulse_width;
ara_tester_axis->pulse = !ara_tester_axis->pulse;
output_pin_set_state(__ara_tester_axis_pulse_pin_pointer(ara_tester_axis), ara_tester_axis->pulse);
hrtimer_start(__ara_tester_axis_timer_pointer(ara_tester_axis), ktime_set(0, timeout), HRTIMER_MODE_REL);
if(!ara_tester_axis->pulse) {
ara_tester_axis->counter++;
if(ara_tester_axis->count > 1) {
ara_tester_axis->count--;
} else {
switch(ara_tester_axis->movment_state) {
case 0: {
if(ara_tester_axis->linear) {
ara_tester_axis->movment_state = 1;
ara_tester_axis->count = ara_tester_axis->linear;
break;
}
}
case 1: {
ara_tester_axis->movment_state = 2;
ara_tester_axis->count = ara_tester_axis->progressive + ara_tester_axis->even;
break;
}
case 2: {
ara_tester_axis->active = 0;
printk("Counter %lu\n", ara_tester_axis->counter);
break;
}
}
return HRTIMER_NORESTART;
}
switch(ara_tester_axis->movment_state) {
case 0: {
ara_tester_axis->t_current -= ara_tester_axis->t_delta;
break;
}
case 2: {
ara_tester_axis->t_current += ara_tester_axis->t_delta;
break;
}
}
}
}
return HRTIMER_NORESTART;
}
#endif