Skip to content

Commit 034d734

Browse files
authored
Merge pull request #3225 from BsAtHome/fix_deduplicate-hm2-code
Deduplicate hm2 driver code and constants
2 parents a46989c + cf2b85d commit 034d734

File tree

4 files changed

+44
-399
lines changed

4 files changed

+44
-399
lines changed

src/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,12 +1058,16 @@ hm2_pci-objs := \
10581058
$(MATHSTUB)
10591059
hm2_eth-objs := \
10601060
hal/drivers/mesa-hostmot2/hm2_eth.o \
1061+
hal/drivers/mesa-hostmot2/eshellf.o \
10611062
$(MATHSTUB)
10621063
hm2_spi-objs := \
10631064
hal/drivers/mesa-hostmot2/hm2_spi.o \
1065+
hal/drivers/mesa-hostmot2/llio_info.o \
10641066
$(MATHSTUB)
10651067
hm2_rpspi-objs := \
1066-
hal/drivers/mesa-hostmot2/hm2_rpspi.o \
1068+
hal/drivers/mesa-hostmot2/hm2_rpspi.o \
1069+
hal/drivers/mesa-hostmot2/llio_info.o \
1070+
hal/drivers/mesa-hostmot2/eshellf.o \
10671071
$(MATHSTUB)
10681072
hm2_spix-objs := \
10691073
hal/drivers/mesa-hostmot2/hm2_spix.o \

src/hal/drivers/mesa-hostmot2/hm2_eth.c

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "hostmot2-lowlevel.h"
4646
#include "hostmot2.h"
4747
#include "hm2_eth.h"
48+
#include "eshellf.h"
4849

4950
struct kvlist {
5051
struct rtapi_list_head list;
@@ -463,32 +464,6 @@ static int eth_socket_recv(int sockfd, void *buffer, int len, int flags);
463464
#define IPTABLES "env \"PATH=/usr/sbin:/sbin:${PATH}\" iptables"
464465
#define CHAIN "hm2-eth-rules-output"
465466

466-
static int shell(char *command) {
467-
char *const argv[] = {"sh", "-c", command, NULL};
468-
pid_t pid;
469-
int res = rtapi_spawn_as_root(&pid, "/bin/sh", NULL, NULL, argv, environ);
470-
if(res < 0) perror("rtapi_spawn_as_root");
471-
int status;
472-
waitpid(pid, &status, 0);
473-
if(WIFEXITED(status)) return WEXITSTATUS(status);
474-
else if(WIFSTOPPED(status)) return WTERMSIG(status)+128;
475-
else return status;
476-
}
477-
478-
static int eshellf(char *fmt, ...) {
479-
char commandbuf[1024];
480-
va_list ap;
481-
va_start(ap, fmt);
482-
vsnprintf(commandbuf, sizeof(commandbuf), fmt, ap);
483-
va_end(ap);
484-
485-
int res = shell(commandbuf);
486-
if(res == EXIT_SUCCESS) return 0;
487-
488-
LL_PRINT("ERROR: Failed to execute '%s'\n", commandbuf);
489-
return -EINVAL;
490-
}
491-
492467
static bool chain_exists() {
493468
int result =
494469
shell(IPTABLES" -n -L "CHAIN" > /dev/null 2>&1");
@@ -627,7 +602,7 @@ static int install_iptables_perinterface(const char *ifbuf) {
627602
ifbuf);
628603
if(res < 0) return res;
629604

630-
res = eshellf("/sbin/sysctl -q net.ipv6.conf.%s.disable_ipv6=1", ifbuf);
605+
res = eshellf(HM2_LLIO_NAME, "/sbin/sysctl -q net.ipv6.conf.%s.disable_ipv6=1", ifbuf);
631606
if(res < 0) return res;
632607

633608
return 0;

src/hal/drivers/mesa-hostmot2/hm2_rpspi.c

Lines changed: 13 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include "hostmot2-lowlevel.h"
4242
#include "hostmot2.h"
4343
#include "spi_common_rpspi.h"
44+
#include "eshellf.h"
45+
#include "llio_info.h"
4446

4547
#define HM2_LLIO_NAME "hm2_rpspi"
4648

@@ -112,127 +114,6 @@ static uint32_t aux_enables; // Previous state of SPI1 enable
112114
static hm2_rpspi_t boards[RPSPI_MAX_BOARDS]; // Connected boards
113115
static int comp_id; // Upstream assigned component ID
114116

115-
static const char *hm2_7c80_pin_names[] = {
116-
"TB07-02/TB07-03", /* Step/Dir/Misc 5V out */
117-
"TB07-04/TB07-05",
118-
"TB08-02/TB08-03",
119-
"TB08-04/TB08-05",
120-
"TB09-02/TB09-03",
121-
"TB09-04/TB09-05",
122-
"TB10-02/TB10-03",
123-
"TB10-04/TB10-05",
124-
"TB11-02/TB11-03",
125-
"TB11-04/TB11-05",
126-
"TB12-02/TB12-03",
127-
"TB12-04/TB12-05",
128-
"TB03-03/TB04-04", /* RS-422/RS-485 interface */
129-
"TB03-05/TB04-06",
130-
"TB03-05/TB03-06",
131-
"TB04-01/TB04-02", /* Encoder */
132-
"TB04-04/TB04-05",
133-
"TB04-07/TB04-08",
134-
"TB05-02", /* Spindle */
135-
"TB05-02",
136-
"TB05-05/TB05-06",
137-
"TB05-07/TB05-08",
138-
"Internal InMux0", /* InMux */
139-
"Internal InMux1",
140-
"Internal InMux2",
141-
"Internal InMux3",
142-
"Internal InMux4",
143-
144-
"Internal InMuxData",
145-
"TB13-01/TB13-02", /* SSR */
146-
"TB13-03/TB13-04",
147-
"TB13-05/TB13-06",
148-
"TB13-07/TB13-08",
149-
"TB14-01/TB14-02",
150-
"TB14-03/TB14-04",
151-
"TB14-05/TB14-06",
152-
"TB14-07/TB14-08",
153-
"Internal SSR",
154-
"P1-01/DB25-01",
155-
"P1-02/DB25-14",
156-
"P1-03/DB25-02",
157-
"P1-04/DB25-15",
158-
"P1-05/DB25-03",
159-
"P1-06/DB25-16",
160-
"P1-07/DB25-04",
161-
"P1-08/DB25-17",
162-
"P1-09/DB25-05",
163-
"P1-11/DB25-06",
164-
"P1-13/DB25-07",
165-
"P1-15/DB25-08",
166-
"P1-17/DB25-09",
167-
"P1-19/DB25-10",
168-
"P1-21/DB25-11",
169-
"P1-23/DB25-12",
170-
"P1-25/DB25-13",
171-
};
172-
173-
static const char *hm2_7c81_pin_names[] = {
174-
"P1-01/DB25-01",
175-
"P1-02/DB25-14",
176-
"P1-03/DB25-02",
177-
"P1-04/DB25-15",
178-
"P1-05/DB25-03",
179-
"P1-06/DB25-16",
180-
"P1-07/DB25-04",
181-
"P1-08/DB25-17",
182-
"P1-09/DB25-05",
183-
"P1-11/DB25-06",
184-
"P1-13/DB25-07",
185-
"P1-15/DB25-08",
186-
"P1-17/DB25-09",
187-
"P1-19/DB25-10",
188-
"P1-21/DB25-11",
189-
"P1-23/DB25-12",
190-
"P1-25/DB25-13",
191-
"J5-TX0",
192-
"J6-TX1",
193-
194-
"P2-01/DB25-01",
195-
"P2-02/DB25-14",
196-
"P2-03/DB25-02",
197-
"P2-04/DB25-15",
198-
"P2-05/DB25-03",
199-
"P2-06/DB25-16",
200-
"P2-07/DB25-04",
201-
"P2-08/DB25-17",
202-
"P2-09/DB25-05",
203-
"P2-11/DB25-06",
204-
"P2-13/DB25-07",
205-
"P2-15/DB25-08",
206-
"P2-17/DB25-09",
207-
"P2-19/DB25-10",
208-
"P2-21/DB25-11",
209-
"P2-23/DB25-12",
210-
"P2-25/DB25-13",
211-
"J5-TXEN0",
212-
"J6-TXEN1",
213-
214-
"P7-01/DB25-01",
215-
"P7-02/DB25-14",
216-
"P7-03/DB25-02",
217-
"P7-04/DB25-15",
218-
"P7-05/DB25-03",
219-
"P7-06/DB25-16",
220-
"P7-07/DB25-04",
221-
"P7-08/DB25-17",
222-
"P7-09/DB25-05",
223-
"P7-11/DB25-06",
224-
"P7-13/DB25-07",
225-
"P7-15/DB25-08",
226-
"P7-17/DB25-09",
227-
"P7-19/DB25-10",
228-
"P7-21/DB25-11",
229-
"P7-23/DB25-12",
230-
"P7-25/DB25-13",
231-
"P5-RX0",
232-
"P6-RX1"
233-
};
234-
235-
236117
/*
237118
* Configuration parameters
238119
*/
@@ -929,8 +810,8 @@ static uint32_t read_spiclkbase(void)
929810
/*************************************************/
930811
static int probe_board(hm2_rpspi_t *board) {
931812
int32_t ret;
932-
char ident[8+1];
933-
char *base;
813+
hm2_idrom_t idrom;
814+
const char *base;
934815

935816
#if defined(RPSPI_DEBUG_WRITE)
936817
uint32_t buf[8] = {0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f, 0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f};
@@ -942,53 +823,16 @@ static int probe_board(hm2_rpspi_t *board) {
942823

943824
rtapi_print_msg(RPSPI_INFO, "hm2_rpspi: SPI%d/CE%d Valid cookie matched\n", board->spidevid, board->spiceid);
944825

945-
// Read the board identification.
946-
// The IDROM address offset is returned in the cookie check and the
947-
// board_name offset is added (see hm2_idrom_t in hostmot2.h)
948-
// FIXME: should we not simply read the entire IDROM here?
949-
if(!board->llio.read(&board->llio, (uint32_t)ret + 0x000c, ident, 8)) {
826+
// Read the IDROM from the board. The IDROM address offset was returned in
827+
// the cookie check.
828+
if(board->llio.read(&board->llio, (uint32_t)ret, &idrom, sizeof(hm2_idrom_t)) <= 0) {
950829
rtapi_print_msg(RPSPI_ERR, "hm2_rpspi: SPI%d/CE%d Board ident read failed\n", board->spidevid, board->spiceid);
951830
return -EIO; // Cookie could be read, so this is a comms error
952831
}
953-
ident[sizeof(ident)-1] = 0; // Because it may be used in printf, regardless format limits
954-
955-
if(!memcmp(ident, "MESA7I90", 8)) {
956-
base = "hm2_7i90";
957-
board->llio.num_ioport_connectors = 3;
958-
board->llio.pins_per_connector = 24;
959-
board->llio.ioport_connector_name[0] = "P1";
960-
board->llio.ioport_connector_name[1] = "P2";
961-
board->llio.ioport_connector_name[2] = "P3";
962-
board->llio.num_leds = 2;
963-
board->llio.fpga_part_number = "xc6slx9tq144";
964-
} else if(!memcmp(ident, "MESA7C80", 8)){
965-
base = "hm2_7c80";
966-
board->llio.num_ioport_connectors = 2;
967-
board->llio.pins_per_connector = 27;
968-
board->llio.ioport_connector_name[0] = "Embedded I/O";
969-
board->llio.ioport_connector_name[1] = "Embedded I/O + P1 expansion";
970-
board->llio.io_connector_pin_names = hm2_7c80_pin_names;
971-
board->llio.num_leds = 4;
972-
board->llio.fpga_part_number = "xc6slx9tq144";
973-
} else if(!memcmp(ident, "MESA7C81", 8)){
974-
base = "hm2_7c81";
975-
board->llio.num_ioport_connectors = 3;
976-
board->llio.pins_per_connector = 19;
977-
board->llio.ioport_connector_name[0] = "P1";
978-
board->llio.ioport_connector_name[1] = "P2";
979-
board->llio.ioport_connector_name[2] = "P7";
980-
board->llio.io_connector_pin_names = hm2_7c81_pin_names;
981-
board->llio.num_leds = 4;
982-
board->llio.fpga_part_number = "xc6slx9tq144";
983-
} else {
984-
int i;
985-
for(i = 0; i < sizeof(ident) - 1; i++) {
986-
if(!isprint(ident[i]))
987-
ident[i] = '?';
988-
}
989-
rtapi_print_msg(RPSPI_ERR, "hm2_rpspi: Unknown board at SPI%d/CE%d: %.8s\n", board->spidevid, board->spiceid, ident);
990-
return -1;
991-
}
832+
833+
// Detect board name and fill in informational values
834+
if(!(base = set_llio_info_spi(&board->llio, &idrom)))
835+
return -ENOENT;
992836

993837
rtapi_print_msg(RPSPI_INFO, "hm2_rpspi: SPI%d/CE%d Base: %s.%d\n", board->spidevid, board->spiceid, base, board->nr);
994838
rtapi_snprintf(board->llio.name, sizeof(board->llio.name), "%s.%d", base, board->nr);
@@ -1421,49 +1265,22 @@ static int hm2_rpspi_setup(void)
14211265
return j > 0 ? 0 : -ENODEV;
14221266
}
14231267

1424-
static int shell(char *command) {
1425-
char *const argv[] = {"sh", "-c", command, NULL};
1426-
pid_t pid;
1427-
int res = rtapi_spawn_as_root(&pid, "/bin/sh", NULL, NULL, argv, environ);
1428-
if(res < 0) perror("rtapi_spawn_as_root");
1429-
int status;
1430-
waitpid(pid, &status, 0);
1431-
if(WIFEXITED(status)) return WEXITSTATUS(status);
1432-
else if(WIFSTOPPED(status)) return WTERMSIG(status)+128;
1433-
else return status;
1434-
}
1435-
1436-
static int eshellf(char *fmt, ...) {
1437-
char commandbuf[1024];
1438-
va_list ap;
1439-
va_start(ap, fmt);
1440-
vsnprintf(commandbuf, sizeof(commandbuf), fmt, ap);
1441-
va_end(ap);
1442-
1443-
int res = shell(commandbuf);
1444-
if(res == EXIT_SUCCESS) return 0;
1445-
1446-
LL_PRINT("ERROR: Failed to execute '%s'\n", commandbuf);
1447-
return -EINVAL;
1448-
}
1449-
1450-
14511268
/*************************************************/
14521269
static void hm2_rpspi_cleanup(void)
14531270
{
14541271
if((void *)peripheralmem != MAP_FAILED) {
14551272
peripheral_restore();
14561273
munmap(peripheralmem, peripheralsize);
14571274
}
1458-
eshellf("/sbin/modprobe spi-bcm2835");
1275+
eshellf(HM2_LLIO_NAME, "/sbin/modprobe spi-bcm2835");
14591276
}
14601277

14611278
/*************************************************/
14621279
int rtapi_app_main()
14631280
{
14641281
int ret;
14651282

1466-
eshellf("/sbin/rmmod spi_bcm2835");
1283+
eshellf(HM2_LLIO_NAME, "/sbin/rmmod spi_bcm2835");
14671284

14681285
if((comp_id = ret = hal_init("hm2_rpspi")) < 0)
14691286
goto fail;

0 commit comments

Comments
 (0)