Browse Source

mcp2210: Set GPIO output mode atomically with their output values

Luke Dashjr 12 years ago
parent
commit
6bf2c3d63e
1 changed files with 20 additions and 17 deletions
  1. 20 17
      mcp2210.c

+ 20 - 17
mcp2210.c

@@ -391,37 +391,40 @@ bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
 	return true;
 	return true;
 }
 }
 
 
-static
-bool mcp2210_set_gpio_(struct mcp2210_device * const h, const int pin, const int byteoffset, const bool value)
+bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
 {
 {
 	const int bit = 1 << (pin % 8);
 	const int bit = 1 << (pin % 8);
-	const int byte = (pin / 8) + byteoffset;
+	const int byte = (pin / 8);
+	
+	// Set pin to GPIO mode
+	h->cfg_gpio[pin] = 0;
 	
 	
-	if (value)
-		h->cfg_gpio[byte] |= bit;
+	// Set GPIO to output mode
+	h->cfg_gpio[byte + 0xb] &= ~bit;
+	
+	// Set value for GPIO output
+	if (d == MGV_HIGH)
+		h->cfg_gpio[byte + 9] |= bit;
 	else
 	else
-		h->cfg_gpio[byte] &= ~bit;
+		h->cfg_gpio[byte + 9] &= ~bit;
 	
 	
 	return mcp2210_set_cfg_gpio(h);
 	return mcp2210_set_cfg_gpio(h);
 }
 }
 
 
-bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
-{
-	h->cfg_gpio[pin] = 0;
-	if (!mcp2210_set_gpio_(h, pin, 0xb, false))
-		return false;
-	return mcp2210_set_gpio_(h, pin, 0x9, d == MGV_HIGH);
-}
-
 enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
 enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
 {
 {
 	hid_device * const hid = h->hid;
 	hid_device * const hid = h->hid;
 	uint8_t cmd[0x40] = {0x31}, buf[0x40];
 	uint8_t cmd[0x40] = {0x31}, buf[0x40];
 	const int bit = 1 << (pin % 8);
 	const int bit = 1 << (pin % 8);
-	const int byte = (pin / 8) + 4;
+	const int byte = (pin / 8);
 	
 	
+	// Set pin to GPIO mode
 	h->cfg_gpio[pin] = 0;
 	h->cfg_gpio[pin] = 0;
-	if (!mcp2210_set_gpio_(h, pin, 0xb, true))
+	
+	// Set GPIO to input mode
+	h->cfg_gpio[byte + 0xb] |= bit;
+	
+	if (!mcp2210_set_cfg_gpio(h))
 		return MGV_ERROR;
 		return MGV_ERROR;
 	
 	
 	if (!mcp2210_io(hid, cmd, buf))
 	if (!mcp2210_io(hid, cmd, buf))
@@ -430,7 +433,7 @@ enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h,
 		return MGV_ERROR;
 		return MGV_ERROR;
 	}
 	}
 	
 	
-	if (buf[byte] & bit)
+	if (buf[byte + 4] & bit)
 		return MGV_HIGH;
 		return MGV_HIGH;
 	else
 	else
 		return MGV_LOW;
 		return MGV_LOW;