-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpin_config.c
191 lines (169 loc) · 5.91 KB
/
pin_config.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
#include "pin_config.h"
khash_t(pin_config) *handle_pin_config = NULL;
static int init = 0;
static int model_beaglebone = 0;
int config_pins(int pru_pin_number, int mode, char* pru_id)
{
if (init == 0)
{
handle_pin_config = kh_init(pin_config);
init = 1;
}
cJSON *json_head = NULL;
switch (read_device_model())
{
case MODEL_POCKETBEAGLE:
json_head = cJSON_Parse(POCKETBEAGLE_PINOUT);
break;
case MODEL_BEAGLEBONE_BLACK:
case MODEL_BEAGLEBONE_BLACK_WIRELESS:
json_head = cJSON_Parse(BEAGLEBONEBLACK_PINOUT);
break;
case MODEL_BEAGLEBONE_BLUE:
json_head = cJSON_Parse(BEAGLEBONEBLUE_PINOUT);
break;
case MODEL_BEAGLEBONE_AI:
json_head = cJSON_Parse(BEAGLEBONEAI_PINOUT);
break;
default:
json_head = cJSON_Parse(POCKETBEAGLE_PINOUT);
break;
}
if (json_head == NULL)
{
return -1;
}
cJSON *pru_head = cJSON_GetObjectItemCaseSensitive(json_head, pru_id);
if (pru_head == NULL)
{
return -1;
}
char buffer[3];
sprintf(buffer, "%d", pru_pin_number);
cJSON *pin_head = cJSON_GetObjectItemCaseSensitive(pru_head, buffer);
if (pin_head == NULL)
{
return -1;
}
int array_size = cJSON_GetArraySize(pin_head);
for (int i = 0; i < array_size; i++)
{
cJSON* array = cJSON_GetArrayItem(pin_head, i);
int pin_mode = cJSON_GetArrayItem(array, 1)->valueint;
if (pin_mode == mode || pin_mode == 3)
{
char command_string[50];
khint_t temp = kh_get(pin_config, handle_pin_config, pru_pin_number);
switch(mode)
{
case IN:
if (temp == kh_end(handle_pin_config))
{
sprintf(command_string, "%s %s %s", "config-pin", cJSON_GetArrayItem(array, 0)->valuestring, "pruin");
printf("[%d] : setting %s as input\n", pru_pin_number, cJSON_GetArrayItem(array, 0)->valuestring);
int err = 0;
int index = kh_put(pin_config, handle_pin_config, pru_pin_number, &err);
if (err == -1)
{
return err;
}
kh_value(handle_pin_config, index) = IN;
}
else if (kh_value(handle_pin_config, temp) != IN)
{
printf("[%d] : pin already set\n", pru_pin_number);
return -1;
}
else if (kh_value(handle_pin_config, temp) == IN)
{
return 1;
}
break;
case OUT:
if (temp == kh_end(handle_pin_config))
{
sprintf(command_string, "%s %s %s", "config-pin", cJSON_GetArrayItem(array, 0)->valuestring, "pruout");
printf("[%d] : setting %s as output\n", pru_pin_number, cJSON_GetArrayItem(array, 0)->valuestring);
int err = 0;
int index = kh_put(pin_config, handle_pin_config, pru_pin_number, &err);
if (err == -1)
{
return err;
}
kh_value(handle_pin_config, index) = OUT;
}
else if (kh_value(handle_pin_config, temp) != OUT)
{
printf("[%d] : pin already set\n", pru_pin_number);
return -1;
}
else if (kh_value(handle_pin_config, temp) == OUT)
{
return 1;
}
break;
}
if (system(command_string) == -1)
{
fprintf(stderr, "\e[31mfatal error:\e[0m unable to set pin mode using config pin\n");
return -1;
}
return 1;
}
}
return -1;
}
int set_device_model(int model_bb)
{
if (model_bb == MODEL_AUTODETECT)
{
FILE *model = fopen("/proc/device-tree/model", "r");
if (model == NULL)
{
fprintf(stderr, "Error opening /proc/device-tree/model\n");
model_beaglebone = MODEL_POCKETBEAGLE;
return -1;
}
char model_name[50];
for (int i = 0; fread(&model_name[i], sizeof(char), 1, model) == 1 ; i++);
if (!strcmp(model_name, "TI AM335x BeagleBone Black Wireless"))
{
model_beaglebone = MODEL_BEAGLEBONE_BLACK_WIRELESS;
printf("Detected %s\n", model_name);
}
else if (!strcmp(model_name, "TI AM335x BeagleBone Black"))
{
model_beaglebone = MODEL_BEAGLEBONE_BLACK;
printf("Detected %s\n", model_name);
}
else if (!strcmp(model_name, "TI AM335x BeagleBone Blue"))
{
model_beaglebone = MODEL_BEAGLEBONE_BLUE;
printf("Detected %s\n", model_name);
}
else if (!strcmp(model_name, "TI AM335x PocketBeagle"))
{
model_beaglebone = MODEL_POCKETBEAGLE;
printf("Detected %s\n", model_name);
}
else if (!strcmp(model_name, "BeagleBoard.org BeagleBone AI"))
{
model_beaglebone = MODEL_BEAGLEBONE_AI;
printf("Detected %s\n", model_name);
}
else
{
model_beaglebone = MODEL_POCKETBEAGLE;
printf("Detected %s\n", model_name);
}
}
else
{
model_beaglebone = model_bb;
}
return 1;
}
int read_device_model()
{
return model_beaglebone;
}