File: init.c

package info (click to toggle)
stm32flash 0.5-1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 296 kB
  • ctags: 380
  • sloc: ansic: 3,192; makefile: 41
file content (272 lines) | stat: -rw-r--r-- 6,050 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
  stm32flash - Open Source ST STM32 flash program for *nix
  Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
  Copyright (C) 2013 Antonio Borneo <borneo.antonio@gmail.com>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "init.h"
#include "serial.h"
#include "stm32.h"
#include "port.h"

struct gpio_list {
	struct gpio_list *next;
	int gpio;
	int input; /* 1 if direction of gpio should be changed back to input. */
	int exported; /* 0 if gpio should be unexported. */
};

static int write_to(const char *filename, const char *value)
{
	int fd, ret;

	fd = open(filename, O_WRONLY);
	if (fd < 0) {
		fprintf(stderr, "Cannot open file \"%s\"\n", filename);
		return 0;
	}
	ret = write(fd, value, strlen(value));
	if (ret < 0) {
		fprintf(stderr, "Error writing in file \"%s\"\n", filename);
		close(fd);
		return 0;
	}
	close(fd);
	return 1;
}

#if !defined(__linux__)
static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release)
{
	fprintf(stderr, "GPIO control only available in Linux\n");
	return 0;
}
#else
static int read_from(const char *filename, char *buf, size_t len)
{
	int fd, ret;
	ssize_t n = 0;

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "Cannot open file \"%s\"\n", filename);
		return 0;
	}

	do {
		ret = read(fd, buf + n, len - n);
		if (ret < 0) {
			if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
				continue; /* try again */
			fprintf(stderr, "Error reading in file \"%s\"\n", filename);
			close(fd);
			return 0;
		}
		n += ret;
	} while (n < len && ret);

	close(fd);
	return n;
}

static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release)
{
	char num[16]; /* sized to carry MAX_INT */
	char file[48]; /* sized to carry longest filename */
	char dir;
	struct stat buf;
	struct gpio_list *new;
	int ret;
	int exported = 1;
	int input = 0;

	sprintf(file, "/sys/class/gpio/gpio%d/value", n);
	ret = stat(file, &buf);
	if (ret) {
		/* file miss, GPIO not exported yet */
		sprintf(num, "%d", n);
		ret = write_to("/sys/class/gpio/export", num);
		if (!ret)
			return 0;
		ret = stat(file, &buf);
		if (ret) {
			fprintf(stderr, "GPIO %d not available\n", n);
			return 0;
		}
		exported = 0;
	}

	sprintf(file, "/sys/class/gpio/gpio%d/direction", n);
	ret = stat(file, &buf);
	if (!ret)
		if (read_from(file, &dir, sizeof(dir)))
			if (dir == 'i')
				input = 1;

	if (exported == 0 || input == 1) {
		new = (struct gpio_list *)malloc(sizeof(struct gpio_list));
		if (new == NULL) {
			fprintf(stderr, "Out of memory\n");
			return 0;
		}
		new->gpio = n;
		new->exported = exported;
		new->input = input;
		new->next = *gpio_to_release;
		*gpio_to_release = new;
	}

	return write_to(file, level ? "high" : "low");
}
#endif

static int release_gpio(int n, int input, int exported)
{
	char num[16]; /* sized to carry MAX_INT */
	char file[48]; /* sized to carry longest filename */

	sprintf(num, "%d", n);
	if (input) {
		sprintf(file, "/sys/class/gpio/gpio%d/direction", n);
		write_to(file, "in");
	}
	if (!exported)
		write_to("/sys/class/gpio/unexport", num);

	return 1;
}

static int gpio_sequence(struct port_interface *port, const char *s, size_t l)
{
	struct gpio_list *gpio_to_release = NULL, *to_free;
	int ret, level, gpio;

	ret = 1;
	while (ret == 1 && *s && l > 0) {
		if (*s == '-') {
			level = 0;
			s++;
			l--;
		} else
			level = 1;

		if (isdigit(*s)) {
			gpio = atoi(s);
			while (isdigit(*s)) {
				s++;
				l--;
			}
		} else if (!strncmp(s, "rts", 3)) {
			gpio = -GPIO_RTS;
			s += 3;
			l -= 3;
		} else if (!strncmp(s, "dtr", 3)) {
			gpio = -GPIO_DTR;
			s += 3;
			l -= 3;
		} else if (!strncmp(s, "brk", 3)) {
			gpio = -GPIO_BRK;
			s += 3;
			l -= 3;
		} else {
			fprintf(stderr, "Character \'%c\' is not a digit\n", *s);
			ret = 0;
			break;
		}

		if (*s && (l > 0)) {
			if (*s == ',') {
				s++;
				l--;
			} else {
				fprintf(stderr, "Character \'%c\' is not a separator\n", *s);
				ret = 0;
				break;
			}
		}
		if (gpio < 0)
			ret = (port->gpio(port, -gpio, level) == PORT_ERR_OK);
		else
			ret = drive_gpio(gpio, level, &gpio_to_release);
		usleep(100000);
	}

	while (gpio_to_release) {
		release_gpio(gpio_to_release->gpio, gpio_to_release->input, gpio_to_release->exported);
		to_free = gpio_to_release;
		gpio_to_release = gpio_to_release->next;
		free(to_free);
	}
	usleep(500000);
	return ret;
}

static int gpio_bl_entry(struct port_interface *port, const char *seq)
{
	char *s;

	if (seq == NULL || seq[0] == ':')
		return 1;

	s = strchr(seq, ':');
	if (s == NULL)
		return gpio_sequence(port, seq, strlen(seq));

	return gpio_sequence(port, seq, s - seq);
}

static int gpio_bl_exit(struct port_interface *port, const char *seq)
{
	char *s;

	if (seq == NULL)
		return 1;

	s = strchr(seq, ':');
	if (s == NULL || s[1] == '\0')
		return 1;

	return gpio_sequence(port, s + 1, strlen(s + 1));
}

int init_bl_entry(struct port_interface *port, const char *seq)
{
	if (seq)
		return gpio_bl_entry(port, seq);

	return 1;
}

int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq)
{
	if (seq && strchr(seq, ':'))
		return gpio_bl_exit(port, seq);

	if (stm32_reset_device(stm) != STM32_ERR_OK)
		return 0;
	return 1;
}