Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * See https://pastebin.com/i3z2Ts50
- *
- * Copyright 2018 strict-flower
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "hidapi.h"
- #include "libmcp2221.h"
- #include "avr_writer.h"
- const pin_config_t g_config[4] = {
- {AVR_SCK, MCP2221_GPIO0, MCP2221_GPIO_DIR_OUTPUT},
- {AVR_MISO, MCP2221_GPIO1, MCP2221_GPIO_DIR_INPUT},
- {AVR_MOSI, MCP2221_GPIO2, MCP2221_GPIO_DIR_OUTPUT},
- {AVR_RESET, MCP2221_GPIO3, MCP2221_GPIO_DIR_OUTPUT},
- };
- void gpio_init(void) {
- if (g_is_gpio_init) {
- mcp2221_exit();
- fputs("[-] GPIO was already initialized\n", stderr);
- exit(1);
- }
- g_gpio_conf = mcp2221_GPIOConfInit();
- for (int i = 0; i < (int)(sizeof(g_config) / sizeof(pin_config_t)); i++) {
- g_gpio_conf.conf[i].gpios = g_config[i].pin;
- g_gpio_conf.conf[i].mode = MCP2221_GPIO_MODE_GPIO;
- g_gpio_conf.conf[i].direction = g_config[i].direction;
- if (g_config[i].direction == MCP2221_GPIO_DIR_OUTPUT) {
- g_gpio_conf.conf[i].value = MCP2221_GPIO_VALUE_LOW;
- }
- }
- mcp2221_setGPIOConf(g_dev, &g_gpio_conf);
- g_is_gpio_init = 1;
- }
- void gpio_set_value(int idx, int value) {
- g_gpio_conf.conf[idx].value = (value != 0) ? MCP2221_GPIO_VALUE_HIGH : MCP2221_GPIO_VALUE_LOW;
- mcp2221_setGPIOConf(g_dev, &g_gpio_conf);
- }
- int gpio_get_value(int idx) {
- mcp2221_gpio_value_t values[MCP2221_GPIO_COUNT];
- if (!(0 <= idx < 4)) {
- mcp2221_exit();
- fputs("[-] specified invalid index range (valid range is 0 - 3)\n", stderr);
- exit(1);
- }
- if (mcp2221_readGPIO(g_dev, values) != MCP2221_SUCCESS) {
- mcp2221_exit();
- fputs("[-] Failed read from GPIO\n", stderr);
- exit(1);
- }
- return values[idx];
- }
Advertisement
Add Comment
Please, Sign In to add comment