-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlkm_mf4005_motor.h
205 lines (173 loc) · 4.89 KB
/
lkm_mf4005_motor.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
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
/*
* Copyright (c) 2024 Felipe Neves
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef INCLUDE_LKM_MF4005_DRIVER_H_
#define INCLUDE_LKM_MF4005_DRIVER_H_
#include <errno.h>
#include <stdlib.h>
#include <zephyr/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Motor channels.
*/
enum mf4005_motor_channel {
MF4005_CHAN_POSITION_MILI_DEGREES = 0,
MF4005_CHAN_SPEED_DEG_SECONDS,
MF4005_CHAN_RAW_TORQUE_MILIAMPERES,
MF4005_CHAN_COUNT,
MF4005_CHAN_MAX = INT16_MAX,
};
/**
* @brief Motor attribute types.
*/
enum mf4005_motor_attribute {
MF4005_ATTR_POSITION_KP = 0,
MF4005_ATTR_POSITION_KI,
MF4005_ATTR_SPEED_KP,
MF4005_ATTR_SPEED_KI,
MF4005_ATTR_TORQUE_KP,
MF4005_ATTR_TORQUE_KI,
MF4005_ATTR_COUNT,
MF4005_ATTR_MAX = INT16_MAX,
};
/**
* @brief MF4005 motor driver generic API definition
*/
typedef int (*mf4005_motor_start_t)(const struct device *dev);
typedef int (*mf4005_motor_stop_t)(const struct device *dev);
typedef int (*mf4005_motor_attr_set_t)(const struct device *dev,
enum mf4005_motor_attribute attr,
int attr_val);
typedef int (*mf4005_motor_attr_get_t)(const struct device *dev,
enum mf4005_motor_attribute attr,
int *attr_val);
typedef int (*mf4005_motor_channel_set_t)(const struct device *dev,
enum mf4005_motor_channel chan,
int val);
typedef int (*mf4005_motor_channel_get_t)(const struct device *dev,
enum mf4005_motor_channel chan,
int64_t *val);
struct mf4005_motor_driver_api {
mf4005_motor_start_t motor_start;
mf4005_motor_stop_t motor_stop;
mf4005_motor_attr_set_t motor_attr_set;
mf4005_motor_attr_get_t motor_attr_get;
mf4005_motor_channel_set_t motor_channel_set;
mf4005_motor_channel_get_t motor_channel_get;
};
/**
* @brief Enable power of the motor
*
* @param dev Pointer to the MF40005 motor device
*
* @return 0 if successful, negative errno code if failure.
*/
static inline int mf4005_motor_start(const struct device *dev)
{
const struct mf4005_motor_driver_api *api =
(const struct mf4005_motor_driver_api *)dev->api;
if (api->motor_start == NULL) {
return -ENOSYS;
}
return api->motor_start(dev);
}
/**
* @brief Disable the motor power
*
* @param dev Pointer to the MF40005 motor device
*
* @return 0 if successful, negative errno code if failure.
*/
static inline int mf4005_motor_stop(const struct device *dev)
{
const struct mf4005_motor_driver_api *api =
(const struct mf4005_motor_driver_api *)dev->api;
if (api->motor_stop == NULL) {
return -ENOSYS;
}
return api->motor_stop(dev);
}
/**
* @brief Set an attribute for a motor
*
* @param dev Pointer to the MF40005 motor device
* @param attr The attribute to set
* @param val The value to set the attribute to
*
* @return 0 if successful, negative errno code if failure.
*/
static inline int mf4005_motor_attr_set(const struct device *dev, enum mf4005_motor_attribute attr,
int attr_val)
{
const struct mf4005_motor_driver_api *api =
(const struct mf4005_motor_driver_api *)dev->api;
if (api->motor_attr_set == NULL) {
return -ENOSYS;
}
return api->motor_attr_set(dev, attr, attr_val);
}
/**
* @brief Get an attribute for a motor
*
* @param dev Pointer to the MF40005 motor device
* @param attr The attribute to get
* @param val pointer to the value to store the attribute get
*
* @return 0 if successful, negative errno code if failure.
*/
static inline int mf4005_motor_attr_get(const struct device *dev, enum mf4005_motor_attribute attr,
int *attr_val)
{
const struct mf4005_motor_driver_api *api =
(const struct mf4005_motor_driver_api *)dev->api;
if (api->motor_attr_get == NULL) {
return -ENOSYS;
}
return api->motor_attr_get(dev, attr, attr_val);
}
/**
* @brief Set an channel for a motor
*
* @param dev Pointer to the MF40005 motor device
* @param chan The channel to set
* @param val The value to set the channel to
*
* @return 0 if successful, negative errno code if failure.
*/
static inline int mf4005_motor_channel_set(const struct device *dev,
enum mf4005_motor_channel chan, int val)
{
const struct mf4005_motor_driver_api *api =
(const struct mf4005_motor_driver_api *)dev->api;
if (api->motor_channel_set == NULL) {
return -ENOSYS;
}
return api->motor_channel_set(dev, chan, val);
}
/**
* @brief Get a channel for a motor
*
* @param dev Pointer to the MF40005 motor device
* @param chan The channel to get
* @param val The pointer where to store value from the channel
*
* @return 0 if successful, negative errno code if failure.
*/
static inline int mf4005_motor_channel_get(const struct device *dev,
enum mf4005_motor_channel chan, int64_t *val)
{
const struct mf4005_motor_driver_api *api =
(const struct mf4005_motor_driver_api *)dev->api;
if (api->motor_channel_get == NULL) {
return -ENOSYS;
}
return api->motor_channel_get(dev, chan, val);
}
#ifdef __cplusplus
}
#endif
#endif /* INCLUDE_LKM_MF4005_DRIVER_H_ */