strict-flower

gpio.c

Nov 23rd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. /*
  2.  * See https://pastebin.com/i3z2Ts50
  3.  *
  4.  * Copyright 2018 strict-flower
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22.  * IN THE SOFTWARE.
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include "hidapi.h"
  30. #include "libmcp2221.h"
  31. #include "avr_writer.h"
  32.  
  33. const pin_config_t g_config[4] = {
  34.   {AVR_SCK, MCP2221_GPIO0, MCP2221_GPIO_DIR_OUTPUT},
  35.   {AVR_MISO, MCP2221_GPIO1, MCP2221_GPIO_DIR_INPUT},
  36.   {AVR_MOSI, MCP2221_GPIO2, MCP2221_GPIO_DIR_OUTPUT},
  37.   {AVR_RESET, MCP2221_GPIO3, MCP2221_GPIO_DIR_OUTPUT},
  38. };
  39.  
  40. void gpio_init(void) {
  41.   if (g_is_gpio_init) {
  42.     mcp2221_exit();
  43.     fputs("[-] GPIO was already initialized\n", stderr);
  44.     exit(1);
  45.   }
  46.  
  47.   g_gpio_conf = mcp2221_GPIOConfInit();
  48.   for (int i = 0; i < (int)(sizeof(g_config) / sizeof(pin_config_t)); i++) {
  49.     g_gpio_conf.conf[i].gpios = g_config[i].pin;
  50.     g_gpio_conf.conf[i].mode = MCP2221_GPIO_MODE_GPIO;
  51.     g_gpio_conf.conf[i].direction = g_config[i].direction;
  52.     if (g_config[i].direction == MCP2221_GPIO_DIR_OUTPUT) {
  53.       g_gpio_conf.conf[i].value = MCP2221_GPIO_VALUE_LOW;
  54.     }
  55.   }
  56.   mcp2221_setGPIOConf(g_dev, &g_gpio_conf);
  57.   g_is_gpio_init = 1;
  58. }
  59.  
  60. void gpio_set_value(int idx, int value) {
  61.   g_gpio_conf.conf[idx].value = (value != 0) ? MCP2221_GPIO_VALUE_HIGH : MCP2221_GPIO_VALUE_LOW;
  62.   mcp2221_setGPIOConf(g_dev, &g_gpio_conf);
  63. }
  64.  
  65. int gpio_get_value(int idx) {
  66.   mcp2221_gpio_value_t values[MCP2221_GPIO_COUNT];
  67.   if (!(0 <= idx < 4)) {
  68.     mcp2221_exit();
  69.     fputs("[-] specified invalid index range (valid range is 0 - 3)\n", stderr);
  70.     exit(1);
  71.   }
  72.   if (mcp2221_readGPIO(g_dev, values) != MCP2221_SUCCESS) {
  73.     mcp2221_exit();
  74.     fputs("[-] Failed read from GPIO\n", stderr);
  75.     exit(1);
  76.   }
  77.   return values[idx];
  78. }
Advertisement
Add Comment
Please, Sign In to add comment