Browse Source

Use a common enum bfg_gpio_value for both lowl-vcom (cts, dtr, rts) and mcp2210

Luke Dashjr 12 years ago
parent
commit
083e2f9c0d
10 changed files with 57 additions and 61 deletions
  1. 1 1
      driver-avalon.h
  2. 2 8
      driver-dualminer.c
  3. 10 10
      driver-nanofury.c
  4. 2 2
      gc3355.c
  5. 1 2
      gc3355.h
  6. 20 20
      lowl-vcom.c
  7. 4 3
      lowl-vcom.h
  8. 7 7
      mcp2210.c
  9. 2 8
      mcp2210.h
  10. 8 0
      util.h

+ 1 - 1
driver-avalon.h

@@ -128,7 +128,7 @@ struct avalon_info {
 #define avalon_open(devpath, baud)  avalon_open2(devpath, baud, true)
 #define avalon_close(fd) close(fd)
 
-#define avalon_buffer_full(fd)	get_serial_cts(fd)
+#define avalon_buffer_full(fd)	(get_serial_cts(fd) != BGV_LOW)
 
 #define AVALON_READ_TIME(baud) ((double)AVALON_READ_SIZE * (double)8.0 / (double)(baud))
 #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]

+ 2 - 8
driver-dualminer.c

@@ -68,12 +68,6 @@ const char scrypt_golden_ob[] =
 static
 const char scrypt_golden_nonce[] = "dd0c0500";
 
-enum
-{
-	RTS_LOW = 0,
-	RTS_HIGH = 1
-};
-
 BFG_REGISTER_DRIVER(dualminer_drv)
 static
 const struct bfg_set_device_definition dualminer_set_device_funcs[];
@@ -101,7 +95,7 @@ void dualminer_teardown_device(int fd)
 	else
 		gc3355_open_sha2_unit(fd, "0");
 
-	gc3355_set_rts_status(fd, RTS_LOW);
+	set_serial_rts(fd, BGV_LOW);
 }
 
 static
@@ -124,7 +118,7 @@ void dualminer_init_firstrun(struct cgpu_info *icarus)
 	dualminer_bootstrap_device(fd);
 
 	if (opt_scrypt)
-		gc3355_set_rts_status(fd, RTS_HIGH);
+		set_serial_rts(fd, BGV_HIGH);
 
 	gc3355_init(fd, opt_dualminer_sha2_gating, !opt_dual_mode);
 	applog(LOG_DEBUG, "%"PRIpreprv": scrypt: %d, scrypt only: %d; have fan: %d\n", icarus->proc_repr, opt_scrypt, opt_scrypt, opt_hubfans);

+ 10 - 10
driver-nanofury.c

@@ -52,14 +52,14 @@ bool nanofury_spi_reset(struct mcp2210_device * const mcp)
 	char buf[1];
 	
 	// SCK_OVRRIDE
-	if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_SCK_OVR, MGV_HIGH))
+	if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_SCK_OVR, BGV_HIGH))
 		return false;
 	
 	for (r = 0; r < 16; ++r)
 		if (!mcp2210_spi_transfer(mcp, tx, buf, 1))
 			return false;
 	
-	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) == MGV_ERROR)
+	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) == BGV_ERROR)
 		return false;
 	
 	return true;
@@ -125,7 +125,7 @@ static
 void nanofury_send_led_gpio(struct nanofury_state * const state)
 {
 	struct mcp2210_device * const mcp = state->mcp;
-	mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? MGV_HIGH : MGV_LOW);
+	mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? BGV_HIGH : BGV_LOW);
 }
 
 static
@@ -152,17 +152,17 @@ bool nanofury_checkport(struct mcp2210_device * const mcp, const unsigned long b
 	
 	// default: set everything to input
 	for (i = 0; i < 9; ++i)
-		if (MGV_ERROR == mcp2210_get_gpio_input(mcp, i))
+		if (BGV_ERROR == mcp2210_get_gpio_input(mcp, i))
 			goto fail;
 	
 	// configure the pins that we need:
 	
 	// LED
-	if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_HIGH))
+	if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, BGV_HIGH))
 		goto fail;
 	
 	// PWR_EN
-	if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, MGV_HIGH))
+	if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, BGV_HIGH))
 		goto fail;
 	
 	// cancel any outstanding SPI transfers
@@ -180,7 +180,7 @@ bool nanofury_checkport(struct mcp2210_device * const mcp, const unsigned long b
 	
 	// after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 0 should be 0)
 	
-	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
+	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != BGV_LOW)
 		goto fail;
 	
 	// switch SCK to polarity (default SCK=1 in mode 2)
@@ -191,7 +191,7 @@ bool nanofury_checkport(struct mcp2210_device * const mcp, const unsigned long b
 	
 	// after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 2 should be 1)
 	
-	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_HIGH)
+	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != BGV_HIGH)
 		goto fail;
 	
 	// switch SCK to polarity (default SCK=0 in mode 0)
@@ -200,7 +200,7 @@ bool nanofury_checkport(struct mcp2210_device * const mcp, const unsigned long b
 	if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
 		goto fail;
 	
-	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
+	if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != BGV_LOW)
 		goto fail;
 	
 	return true;
@@ -423,7 +423,7 @@ void nanofury_poll(struct thr_info * const thr)
 	if (state->identify_requested)
 	{
 		if (!timer_isset(&state->identify_started))
-			mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? MGV_LOW : MGV_HIGH);
+			mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? BGV_LOW : BGV_HIGH);
 		timer_set_delay_from_now(&state->identify_started, 5000000);
 		state->identify_requested = false;
 	}

+ 2 - 2
gc3355.c

@@ -230,9 +230,9 @@ bool opt_dual_mode = false;
 
 void gc3355_dual_reset(int fd)
 {
-	set_serial_dtr(fd, 1);
+	set_serial_dtr(fd, BGV_HIGH);
 	cgsleep_ms(1000);
-	set_serial_dtr(fd, 0);
+	set_serial_dtr(fd, BGV_LOW);
 }
 
 static

+ 1 - 2
gc3355.h

@@ -57,7 +57,6 @@ void gc3355_open_sha2_unit(int fd, char *opt_sha2_gating);
 extern
 void gc3355_open_scrypt_unit(int fd, int status);
 
-#define gc3355_get_cts_status(fd)  (get_serial_cts(fd) ? 0 : 1)
-#define gc3355_set_rts_status(fd, val)  set_serial_rts(fd, val)
+#define gc3355_get_cts_status(fd)  ((get_serial_cts(fd) == BGV_LOW) ? 1 : 0)
 
 #endif

+ 20 - 20
lowl-vcom.c

@@ -1097,24 +1097,24 @@ ssize_t _serial_read(int fd, char *buf, size_t bufsiz, char *eol)
 
 #ifndef WIN32
 
-int get_serial_cts(int fd)
+enum bfg_gpio_value get_serial_cts(int fd)
 {
 	int flags;
 
 	if (fd == -1)
-		return -1;
+		return BGV_ERROR;
 
 	ioctl(fd, TIOCMGET, &flags);
-	return (flags & TIOCM_CTS) ? 1 : 0;
+	return (flags & TIOCM_CTS) ? BGV_HIGH : BGV_LOW;
 }
 
 static
-int _set_serial_cmflag(int fd, int flag, bool val)
+enum bfg_gpio_value _set_serial_cmflag(int fd, int flag, bool val)
 {
 	int flags;
 
 	if (fd == -1)
-		return -1;
+		return BGV_ERROR;
 
 	ioctl(fd, TIOCMGET, &flags);
 	
@@ -1124,57 +1124,57 @@ int _set_serial_cmflag(int fd, int flag, bool val)
 		flags &= ~flag;
 
 	ioctl(fd, TIOCMSET, &flags);
-	return val ? 1 : 0;
+	return val ? BGV_HIGH : BGV_LOW;
 }
 
-int set_serial_dtr(int fd, int dtr)
+enum bfg_gpio_value set_serial_dtr(int fd, enum bfg_gpio_value dtr)
 {
 	return _set_serial_cmflag(fd, TIOCM_DTR, dtr);
 }
 
-int set_serial_rts(int fd, int rts)
+enum bfg_gpio_value set_serial_rts(int fd, enum bfg_gpio_value rts)
 {
 	return _set_serial_cmflag(fd, TIOCM_RTS, rts);
 }
 #else
-int get_serial_cts(const int fd)
+enum bfg_gpio_value get_serial_cts(const int fd)
 {
 	if (fd == -1)
-		return -1;
+		return BGV_ERROR;
 	const HANDLE fh = (HANDLE)_get_osfhandle(fd);
 	if (fh == INVALID_HANDLE_VALUE)
-		return -1;
+		return BGV_ERROR;
 
 	DWORD flags;
 	if (!GetCommModemStatus(fh, &flags))
-		return -1;
+		return BGV_ERROR;
 
-	return (flags & MS_CTS_ON) ? 1 : 0;
+	return (flags & MS_CTS_ON) ? BGV_HIGH : BGV_LOW;
 }
 
 static
-int _set_serial_cmflag(int fd, void (*setfunc)(DCB*, bool), bool val, const char * const fname)
+enum bfg_gpio_value _set_serial_cmflag(int fd, void (*setfunc)(DCB*, bool), bool val, const char * const fname)
 {
 	if (fd == -1)
-		return -1;
+		return BGV_ERROR;
 	const HANDLE fh = (HANDLE)_get_osfhandle(fd);
 	if (fh == INVALID_HANDLE_VALUE)
-		return -1;
+		return BGV_ERROR;
 	
 	DCB dcb;
 	if (!GetCommState(fh, &dcb))
-		applogr(-1, LOG_DEBUG, "Failed to %s"IN_FMT_FFL": %s",
+		applogr(BGV_ERROR, LOG_DEBUG, "Failed to %s"IN_FMT_FFL": %s",
 		        "GetCommState", __FILE__, fname, __LINE__,
 		        bfg_strerror(GetLastError(), BST_SYSTEM));
 	
 	setfunc(&dcb, val);
 	
 	if (!SetCommState(fh, &dcb))
-		applogr(-1, LOG_DEBUG, "Failed to %s"IN_FMT_FFL": %s",
+		applogr(BGV_ERROR, LOG_DEBUG, "Failed to %s"IN_FMT_FFL": %s",
 		        "GetCommState", __FILE__, fname, __LINE__,
 		        bfg_strerror(GetLastError(), BST_SYSTEM));
 	
-	return val ? 1 : 0;
+	return val ? BGV_HIGH : BGV_LOW;
 }
 #define _set_serial_cmflag2(name, field, trueval, falseval)  \
 static  \
@@ -1183,7 +1183,7 @@ void _set_serial_cmflag_ ## name(DCB *dcb, bool val)  \
 	dcb->field = val ? (trueval) : (falseval);  \
 }  \
   \
-int set_serial_ ## name(int fd, int val)  \
+enum bfg_gpio_value set_serial_ ## name(int fd, enum bfg_gpio_value val)  \
 {  \
 	return _set_serial_cmflag(fd, _set_serial_cmflag_ ## name, val, "set_serial_" #name);  \
 }  \

+ 4 - 3
lowl-vcom.h

@@ -7,6 +7,7 @@
 #include <unistd.h>
 
 #include "deviceapi.h"
+#include "util.h"
 
 struct device_drv;
 struct cgpu_info;
@@ -38,9 +39,9 @@ extern ssize_t _serial_read(int fd, char *buf, size_t buflen, char *eol);
 	_serial_read(fd, buf, bufsiz, &eol)
 extern int serial_close(int fd);
 
-extern int get_serial_cts(int fd);
-extern int set_serial_dtr(int fd, int dtr);
-extern int set_serial_rts(int fd, int rts);
+extern enum bfg_gpio_value get_serial_cts(int fd);
+extern enum bfg_gpio_value set_serial_dtr(int fd, enum bfg_gpio_value dtr);
+extern enum bfg_gpio_value set_serial_rts(int fd, enum bfg_gpio_value rts);
 extern bool valid_baud(int baud);
 
 #endif

+ 7 - 7
mcp2210.c

@@ -281,7 +281,7 @@ bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
 	return true;
 }
 
-bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
+bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum bfg_gpio_value d)
 {
 	const int bit = 1 << (pin % 8);
 	const int byte = (pin / 8);
@@ -293,7 +293,7 @@ bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, con
 	h->cfg_gpio[byte + 0xb] &= ~bit;
 	
 	// Set value for GPIO output
-	if (d == MGV_HIGH)
+	if (d == BGV_HIGH)
 		h->cfg_gpio[byte + 9] |= bit;
 	else
 		h->cfg_gpio[byte + 9] &= ~bit;
@@ -301,7 +301,7 @@ bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, con
 	return mcp2210_set_cfg_gpio(h);
 }
 
-enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
+enum bfg_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
 {
 	hid_device * const hid = h->hid;
 	uint8_t cmd[0x41] = {0,0x31}, buf[0x40];
@@ -315,18 +315,18 @@ enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h,
 	h->cfg_gpio[byte + 0xb] |= bit;
 	
 	if (!mcp2210_set_cfg_gpio(h))
-		return MGV_ERROR;
+		return BGV_ERROR;
 	
 	if (!mcp2210_io(hid, cmd, buf))
 	{
 		applog(LOG_ERR, "%s: Failed to get current GPIO input values", __func__);
-		return MGV_ERROR;
+		return BGV_ERROR;
 	}
 	
 	if (buf[byte + 4] & bit)
-		return MGV_HIGH;
+		return BGV_HIGH;
 	else
-		return MGV_LOW;
+		return BGV_LOW;
 }
 
 struct lowlevel_driver lowl_mcp2210 = {

+ 2 - 8
mcp2210.h

@@ -9,12 +9,6 @@ enum mcp2210_gpio_direction {
 	MGD_INPUT,
 };
 
-enum mcp2210_gpio_value {
-	MGV_LOW,
-	MGV_HIGH,
-	MGV_ERROR,
-};
-
 struct mcp2210_device;
 
 extern struct mcp2210_device *mcp2210_open(const struct lowlevel_device_info *);
@@ -25,7 +19,7 @@ extern bool mcp2210_configure_spi(struct mcp2210_device *, uint32_t bitrate, uin
 extern bool mcp2210_set_spimode(struct mcp2210_device *, uint8_t spimode);
 extern bool mcp2210_spi_transfer(struct mcp2210_device *, const void *tx, void *rx, uint8_t sz);
 
-extern bool mcp2210_set_gpio_output(struct mcp2210_device *, int pin, enum mcp2210_gpio_value);
-extern enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device *, int pin);
+extern bool mcp2210_set_gpio_output(struct mcp2210_device *, int pin, enum bfg_gpio_value);
+extern enum bfg_gpio_value mcp2210_get_gpio_input(struct mcp2210_device *, int pin);
 
 #endif

+ 8 - 0
util.h

@@ -111,6 +111,14 @@ bool isCspace(int c)
 	}
 }
 
+
+enum bfg_gpio_value {
+	BGV_LOW   =  0,
+	BGV_HIGH  =  1,
+	BGV_ERROR = -1,
+};
+
+
 typedef struct timeval cgtimer_t;
 
 struct thr_info;