-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpidv3_axi_conf.c
137 lines (118 loc) · 3.52 KB
/
pidv3_axi_conf.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
/*
* Copyright (c) 2019 OscillatorIMP Digital
* Gwenhael Goavec-Merou <[email protected]>
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
/* memory management */
#include <sys/mman.h>
#include <pidv3_axi_core/pidv3_axi_config.h>
#include <pidv3_axi_conf.h>
int pidv3_axi_full_conf(const char *filename,
const int32_t kp_val, const int32_t ki_val, const int32_t kd_val,
const int32_t setpoint_val,
const int8_t int_rst_val, const int8_t sign_val, const enum input_set_t input_val)
{
int retval = EXIT_FAILURE;
int fd = open(filename, O_RDWR);
if (fd < 0) {
printf("erreur d'ouverture de %s\n", filename);
return EXIT_FAILURE;
}
/* configure kxx */
if (ioctl(fd, PIDV3_AXI_SET(REG_PIDV3_AXI_KP), &kp_val) < 0)
goto fd_close;
if (ioctl(fd, PIDV3_AXI_SET(REG_PIDV3_AXI_KI), &ki_val) < 0)
goto fd_close;
if (ioctl(fd, PIDV3_AXI_SET(REG_PIDV3_AXI_KD), &kd_val) < 0)
goto fd_close;
/* configure setpoint */
if (ioctl(fd, PIDV3_AXI_SET(REG_PIDV3_AXI_SETPOINT), &setpoint_val) < 0)
goto fd_close;
/* configure int rst */
if (ioctl(fd, PIDV3_AXI_SET(REG_PIDV3_AXI_INT_RST), &int_rst_val) < 0)
goto fd_close;
/* configure sign */
if (ioctl(fd, PIDV3_AXI_SET(REG_PIDV3_AXI_SIGN), &sign_val) < 0)
goto fd_close;
/* configure if sofware or input */
if (ioctl(fd, PIDV3_AXI_SET(REG_PIDV3_AXI_INPUT), &input_val) < 0)
goto fd_close;
retval = EXIT_SUCCESS;
fd_close:
close(fd);
return retval;
}
int __pidv3_axi_set(const char *filename, const int32_t reg, const int32_t val)
{
int retval = EXIT_FAILURE;
int fd = open(filename, O_RDWR);
if (fd < 0) {
printf("erreur d'ouverture de %s\n", filename);
return EXIT_FAILURE;
}
if (ioctl(fd, PIDV3_AXI_SET(reg), &val) < 0)
goto fd_close;
retval = EXIT_SUCCESS;
fd_close:
close(fd);
return retval;
}
int32_t __pidv3_axi_get(const char *filename, const int32_t reg, int32_t *val)
{
int retval = EXIT_FAILURE;
int fd = open(filename, O_RDWR);
if (fd < 0) {
printf("erreur d'ouverture de %s\n", filename);
return EXIT_FAILURE;
}
if (ioctl(fd, PIDV3_AXI_GET(reg), val) < 0)
goto fd_close;
retval = EXIT_SUCCESS;
fd_close:
close(fd);
return retval;
}
int pidv3_axi_set_kchan(const char *filename, const enum k_chan_t chan, const int32_t val)
{
int reg = (chan == KP) ? REG_PIDV3_AXI_KP : ((chan == KI) ? REG_PIDV3_AXI_KI:REG_PIDV3_AXI_KD);
return __pidv3_axi_set(filename, reg, val);
}
int pidv3_axi_get_kchan(const char *filename, const enum k_chan_t chan, int32_t *val)
{
int reg = (chan == KP) ? REG_PIDV3_AXI_KP : ((chan == KI) ? REG_PIDV3_AXI_KI:REG_PIDV3_AXI_KD);
return __pidv3_axi_get(filename, reg, val);
}
int pidv3_axi_set_setpoint(const char *filename, const int32_t val)
{
return __pidv3_axi_set(filename, REG_PIDV3_AXI_SETPOINT, val);
}
int pidv3_axi_get_setpoint(const char *filename, int32_t *val)
{
return __pidv3_axi_get(filename, REG_PIDV3_AXI_SETPOINT, val);
}
int pidv3_axi_set_int_rst(const char *filename, const int8_t val)
{
return __pidv3_axi_set(filename, REG_PIDV3_AXI_INT_RST, val);
}
int pidv3_axi_set_sign(const char *filename, const int8_t val)
{
return __pidv3_axi_set(filename, REG_PIDV3_AXI_SIGN, val);
}
int pidv3_get_axi_sign(const char *filename, int8_t *val)
{
int32_t v;
int ret = __pidv3_axi_get(filename, REG_PIDV3_AXI_SIGN, &v);
*val = v;
return ret;
}
int pidv3_axi_set_sw_hw(const char *filename, const enum input_set_t input)
{
return __pidv3_axi_set(filename, REG_PIDV3_AXI_INPUT, input);
}