Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit b39c087

Browse files
borneoatormodvolden
authored andcommitted
gpio: encapsulate in port
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
1 parent e528df1 commit b39c087

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

init.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "init.h"
3030
#include "serial.h"
3131
#include "stm32.h"
32+
#include "port.h"
3233

3334
struct gpio_list {
3435
struct gpio_list *next;
@@ -105,7 +106,7 @@ static int release_gpio(int n)
105106
return write_to("/sys/class/gpio/unexport", num);
106107
}
107108

108-
static int gpio_sequence(serial_t *serial, const char *s, size_t l)
109+
static int gpio_sequence(struct port_interface *port, const char *s, size_t l)
109110
{
110111
struct gpio_list *gpio_to_release = NULL, *to_free;
111112
int ret, level, gpio;
@@ -154,7 +155,7 @@ static int gpio_sequence(serial_t *serial, const char *s, size_t l)
154155
}
155156
}
156157
if (gpio < 0)
157-
ret = (serial_gpio(serial, -gpio, level) == SERIAL_ERR_OK);
158+
ret = (port->gpio(port, -gpio, level) == PORT_ERR_OK);
158159
else
159160
ret = drive_gpio(gpio, level, &gpio_to_release);
160161
usleep(100000);
@@ -170,7 +171,7 @@ static int gpio_sequence(serial_t *serial, const char *s, size_t l)
170171
return ret;
171172
}
172173

173-
static int gpio_bl_entry(serial_t *serial, const char *seq)
174+
static int gpio_bl_entry(struct port_interface *port, const char *seq)
174175
{
175176
char *s;
176177

@@ -179,12 +180,12 @@ static int gpio_bl_entry(serial_t *serial, const char *seq)
179180

180181
s = strchr(seq, ':');
181182
if (s == NULL)
182-
return gpio_sequence(serial, seq, strlen(seq));
183+
return gpio_sequence(port, seq, strlen(seq));
183184

184-
return gpio_sequence(serial, seq, s - seq);
185+
return gpio_sequence(port, seq, s - seq);
185186
}
186187

187-
static int gpio_bl_exit(serial_t *serial, const char *seq)
188+
static int gpio_bl_exit(struct port_interface *port, const char *seq)
188189
{
189190
char *s;
190191

@@ -195,21 +196,21 @@ static int gpio_bl_exit(serial_t *serial, const char *seq)
195196
if (s == NULL || s[1] == '\0')
196197
return 1;
197198

198-
return gpio_sequence(serial, s + 1, strlen(s + 1));
199+
return gpio_sequence(port, s + 1, strlen(s + 1));
199200
}
200201

201-
int init_bl_entry(serial_t *serial, const char *seq)
202+
int init_bl_entry(struct port_interface *port, const char *seq)
202203
{
203204
if (seq)
204-
return gpio_bl_entry(serial, seq);
205+
return gpio_bl_entry(port, seq);
205206

206207
return 1;
207208
}
208209

209-
int init_bl_exit(stm32_t *stm, serial_t *serial, const char *seq)
210+
int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq)
210211
{
211212
if (seq)
212-
return gpio_bl_exit(serial, seq);
213+
return gpio_bl_exit(port, seq);
213214

214215
return stm32_reset_device(stm);
215216
}

init.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#ifndef _INIT_H
2323
#define _INIT_H
2424

25-
#include "serial.h"
2625
#include "stm32.h"
26+
#include "port.h"
2727

28-
int init_bl_entry(serial_t *serial, const char *seq);
29-
int init_bl_exit(stm32_t *stm, serial_t *serial, const char *seq);
28+
int init_bl_entry(struct port_interface *port, const char *seq);
29+
int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq);
3030

3131
#endif

main.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ int main(int argc, char* argv[]) {
148148
serial = (serial_t *)port->private;
149149

150150
fprintf(diag, "Serial Config: %s\n", serial_get_setup_str(serial));
151-
if (init_flag && init_bl_entry(serial, gpio_seq) == 0) goto close;
151+
if (init_flag && init_bl_entry(port, gpio_seq) == 0)
152+
goto close;
152153
stm = stm32_init(port, init_flag);
153154
if (!stm)
154155
goto close;
@@ -405,7 +406,7 @@ int main(int argc, char* argv[]) {
405406
if (stm && reset_flag) {
406407
fprintf(diag, "\nResetting device... ");
407408
fflush(diag);
408-
if (init_bl_exit(stm, serial, gpio_seq))
409+
if (init_bl_exit(stm, port, gpio_seq))
409410
fprintf(diag, "done.\n");
410411
else fprintf(diag, "failed.\n");
411412
}

port.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct port_interface {
4444
port_err_t (*close)(struct port_interface *port);
4545
port_err_t (*read)(struct port_interface *port, void *buf, size_t nbyte);
4646
port_err_t (*write)(struct port_interface *port, void *buf, size_t nbyte);
47+
port_err_t (*gpio)(struct port_interface *port, serial_gpio_t n, int level);
4748
void *private;
4849
};
4950

serial_posix.c

+14
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,25 @@ static port_err_t serial_posix_write(struct port_interface *port, void *buf,
372372
return PORT_ERR_UNKNOWN;
373373
}
374374

375+
static port_err_t serial_posix_gpio(struct port_interface *port,
376+
serial_gpio_t n, int level)
377+
{
378+
serial_t *h;
379+
380+
h = (serial_t *)port->private;
381+
if (h == NULL)
382+
return PORT_ERR_UNKNOWN;
383+
if (serial_gpio(h, n, level) == SERIAL_ERR_OK)
384+
return PORT_ERR_OK;
385+
return PORT_ERR_UNKNOWN;
386+
}
387+
375388
struct port_interface port_serial = {
376389
.name = "serial_posix",
377390
.flags = PORT_BYTE,
378391
.open = serial_posix_open,
379392
.close = serial_posix_close,
380393
.read = serial_posix_read,
381394
.write = serial_posix_write,
395+
.gpio = serial_posix_gpio,
382396
};

serial_w32.c

+14
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,25 @@ static port_err_t serial_w32_write(struct port_interface *port, void *buf, size_
362362
return PORT_ERR_UNKNOWN;
363363
}
364364

365+
static port_err_t serial_w32_gpio(struct port_interface *port,
366+
serial_gpio_t n, int level)
367+
{
368+
serial_t *h;
369+
370+
h = (serial_t *)port->private;
371+
if (h == NULL)
372+
return PORT_ERR_UNKNOWN;
373+
if (serial_gpio(h, n, level) == SERIAL_ERR_OK)
374+
return PORT_ERR_OK;
375+
return PORT_ERR_UNKNOWN;
376+
}
377+
365378
struct port_interface port_serial = {
366379
.name = "serial_w32",
367380
.flags = PORT_BYTE,
368381
.open = serial_w32_open,
369382
.close = serial_w32_close,
370383
.read = serial_w32_read,
371384
.write = serial_w32_write,
385+
.gpio = serial_w32_gpio,
372386
};

0 commit comments

Comments
 (0)