Add mcp2210 helper functions for getting and setting one GPIO pin val and direction.
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
diff --git a/mcp2210.c b/mcp2210.c
index bacfbd5..511d7c6 100644
--- a/mcp2210.c
+++ b/mcp2210.c
@@ -43,6 +43,7 @@ bool mcp2210_send_recv(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd)
return true;
}
+/* Get all the pinvals */
bool mcp2210_get_gpio_pinvals(struct cgpu_info *cgpu, struct gpio_pin *gp)
{
char buf[MCP2210_BUFFER_LENGTH];
@@ -60,6 +61,7 @@ bool mcp2210_get_gpio_pinvals(struct cgpu_info *cgpu, struct gpio_pin *gp)
return true;
}
+/* Get all the pindirs */
bool mcp2210_get_gpio_pindirs(struct cgpu_info *cgpu, struct gpio_pin *gp)
{
char buf[MCP2210_BUFFER_LENGTH];
@@ -76,3 +78,97 @@ bool mcp2210_get_gpio_pindirs(struct cgpu_info *cgpu, struct gpio_pin *gp)
return true;
}
+
+/* Get one pinval */
+bool mcp2210_get_gpio_pinval(struct cgpu_info *cgpu, int pin, int *val)
+{
+ char buf[MCP2210_BUFFER_LENGTH];
+
+ memset(buf, 0, MCP2210_BUFFER_LENGTH);
+ buf[0] = MCP2210_GET_GPIO_PIN_VAL;
+ if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINVAL))
+ return false;
+
+ buf[0] = MCP2210_GET_GPIO_PIN_VAL;
+
+ if (pin < 8)
+ *val = !!(buf[4] & (0x01u << pin));
+ else
+ *val = !!(buf[5] & 0x01u);
+
+ return true;
+}
+
+/* Set one pinval */
+bool mcp2210_set_gpio_pinval(struct cgpu_info *cgpu, int pin, int val)
+{
+ char buf[MCP2210_BUFFER_LENGTH];
+
+ /* Get the current pin vals first since we're only changing one. */
+ memset(buf, 0, MCP2210_BUFFER_LENGTH);
+ buf[0] = MCP2210_GET_GPIO_PIN_VAL;
+ if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINVAL))
+ return false;
+
+ buf[0] = MCP2210_SET_GPIO_PIN_VAL;
+
+ if (pin < 8) {
+ if (val)
+ buf[4] |= (0x01u << pin);
+ else
+ buf[4] &= ~(0x01u << pin);
+ } else {
+ if (val)
+ buf[5] |= 0x01u;
+ else
+ buf[5] &= ~0x01u;
+ }
+ return mcp2210_send_recv(cgpu, buf, C_MCP_SETGPIOPINVAL);
+}
+
+/* Get one pindir */
+bool mcp2210_get_gpio_pindir(struct cgpu_info *cgpu, int pin, int *dir)
+{
+ char buf[MCP2210_BUFFER_LENGTH];
+
+ memset(buf, 0, MCP2210_BUFFER_LENGTH);
+ buf[0] = MCP2210_GET_GPIO_PIN_DIR;
+ if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINDIR))
+ return false;
+
+ buf[0] = MCP2210_GET_GPIO_PIN_DIR;
+
+ if (pin < 8)
+ *dir = !!(buf[4] & (0x01u << pin));
+ else
+ *dir = !!(buf[5] & 0x01u);
+
+ return true;
+}
+
+/* Set one pindir */
+bool mcp2210_set_gpio_pindir(struct cgpu_info *cgpu, int pin, int dir)
+{
+ char buf[MCP2210_BUFFER_LENGTH];
+
+ /* Get the current pin dirs first since we're only changing one. */
+ memset(buf, 0, MCP2210_BUFFER_LENGTH);
+ buf[0] = MCP2210_GET_GPIO_PIN_DIR;
+ if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINDIR))
+ return false;
+
+ buf[0] = MCP2210_SET_GPIO_PIN_DIR;
+
+ if (pin < 8) {
+ if (dir)
+ buf[4] |= (0x01u << pin);
+ else
+ buf[4] &= ~(0x01u << pin);
+ } else {
+ if (dir)
+ buf[5] |= 0x01u;
+ else
+ buf[5] &= ~0x01u;
+ }
+ return mcp2210_send_recv(cgpu, buf, C_MCP_SETGPIOPINDIR);
+}
diff --git a/mcp2210.h b/mcp2210.h
index eabdaf5..df04c91 100644
--- a/mcp2210.h
+++ b/mcp2210.h
@@ -39,5 +39,9 @@ struct gpio_pin {
bool mcp2210_send_recv(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd);
bool mcp2210_get_gpio_pinvals(struct cgpu_info *cgpu, struct gpio_pin *gp);
bool mcp2210_get_gpio_pindirs(struct cgpu_info *cgpu, struct gpio_pin *gp);
+bool mcp2210_get_gpio_pinval(struct cgpu_info *cgpu, int pin, int *val);
+bool mcp2210_set_gpio_pinval(struct cgpu_info *cgpu, int pin, int val);
+bool mcp2210_get_gpio_pindir(struct cgpu_info *cgpu, int pin, int *dir);
+bool mcp2210_set_gpio_pindir(struct cgpu_info *cgpu, int pin, int dir);
#endif /* MCP2210_H */