Esempio n. 1
0
int main(int argc, char *argv[])
{
    int counter = 250;
    srand(time(NULL));

    gpio_open(15);
    gpio_open(16);
    gpio_open(17);

    gpio_set_direction(15, "out");
    gpio_set_direction(16, "out");
    gpio_set_direction(17, "out");

    /* blink leds randomly with random delay */
    while (counter > 0) {
        gpio_set(random(15, 17), random(0, 1));
        delay(random(0, random(MIN_DELAY_VALUE, MAX_DELAY_VALUE)));
        counter--;
    }

    /* Turn off all leds */
    gpio_set(17, 1);
    gpio_set(16, 1);
    gpio_set(15, 1);

    gpio_close_all();

    return 0;
}
Esempio n. 2
0
int16_t btn_open(BTN_DEV_ST * btn) {
	btn->duration = 0;
	btn->lastPress = 0;
	btn->pressedCtr = 0;
	btn->state = BTN_STATE_IDLE;
	return gpio_open(btn->port, btn->pin, GPIO_INPUT);
}
Esempio n. 3
0
pwm_driver2_t* pwm_driver2_open(pwm_driver2_descriptor_ptr driver) {
  if (!pwm_driver2_prepare_device(driver)) {
    printf("Cannot prepare driver %d\n", driver->id);
    return NULL;
  }
  pwm_driver2_t* pwm_driver2 = malloc(sizeof(pwm_driver2_t));
  pwm_driver2->pwm = pwm_open(driver->pwm, 50000);
  if (!pwm_driver2->pwm) {
    free(pwm_driver2);
    return NULL;
  }
  pwm_driver2->direction = gpio_open(driver->direction, GPIO_OUT);
  if (!pwm_driver2->direction) {
    pwm_close(pwm_driver2->pwm);
    free(pwm_driver2);
    return NULL;
  }
  pwm_driver2->enable = driver->enable;
  if (!gpio_write_direction_file(pwm_driver2->enable, GPIO_OUT)) {
    pwm_close(pwm_driver2->pwm);
    gpio_close(pwm_driver2->direction);
    free(pwm_driver2);
    return NULL;
  }
  pwm_driver2->pwm->zero = 0;
  pwm_set_duty(pwm_driver2->pwm, pwm_driver2->pwm->zero);
  gpio_write_value_file(pwm_driver2->enable, 0);
  return pwm_driver2;
}
Esempio n. 4
0
static int lua_gpio_open(lua_State *L) {
    gpio_t *gpio;
    unsigned int pin;
    gpio_direction_t direction;
    const char *str_direction;
    int ret;

    gpio = luaL_checkudata(L, 1, "periphery.GPIO");

    /* Initialize file descriptor to an invalid value, in case an error occurs
     * below and gc later close()'s this object. */
    gpio->fd = -1;

    /* Arguments passed in table form */
    if (lua_istable(L, 2)) {
        lua_getfield(L, 2, "pin");
        if (!lua_isnumber(L, -1))
            return lua_gpio_error(L, GPIO_ERROR_ARG, 0, "Error: invalid type of table argument 'pin', should be number");
        pin = lua_tounsigned(L, -1);

        lua_getfield(L, 2, "direction");
        if (lua_isnil(L, -1))
            str_direction = "preserve";
        else if (lua_isstring(L, -1))
            str_direction = lua_tostring(L, -1);
        else
            return lua_gpio_error(L, GPIO_ERROR_ARG, 0, "Error: invalid type of table argument 'direction', should be string");

    /* Arguments passed on stack */
    } else {
        lua_gpio_checktype(L, 2, LUA_TNUMBER);
        pin = lua_tounsigned(L, 2);

        if (lua_gettop(L) > 2) {
            lua_gpio_checktype(L, 3, LUA_TSTRING);
            str_direction = lua_tostring(L, 3);
        } else {
            str_direction = "preserve";
        }
    }

    if (strcmp(str_direction, "in") == 0)
        direction = GPIO_DIR_IN;
    else if (strcmp(str_direction, "out") == 0)
        direction = GPIO_DIR_OUT;
    else if (strcmp(str_direction, "low") == 0)
        direction = GPIO_DIR_OUT_LOW;
    else if (strcmp(str_direction, "high") == 0)
        direction = GPIO_DIR_OUT_HIGH;
    else if (strcmp(str_direction, "preserve") == 0)
        direction = GPIO_DIR_PRESERVE;
    else
        return lua_gpio_error(L, GPIO_ERROR_ARG, 0, "Error: invalid direction, should be 'in', 'out', 'low', 'high', 'preserve'");

    if ((ret = gpio_open(gpio, pin, direction)) < 0)
        return lua_gpio_error(L, ret, gpio_errno(gpio), gpio_errmsg(gpio));

    return 0;
}
Esempio n. 5
0
uint32_t gpio_read(void)
{
	int f;
	uint32_t r;

	if ((f = gpio_open(0)) < 0) return ~0;
	r = _gpio_read(f);
	close(f);
	return r;
}
Esempio n. 6
0
int main(int argc, char *argv[])
{
    CCoreThread *core;

    appStartMs = Utils::getCurrentMs();
    
    initializeFlags();                                          // initialize flags 
    Debug::setDefaultLogFile();
    loadDefaultArgumentsFromFile();                             // first parse default arguments from file
    loadLastHwConfig();                                         // load last found HW IF, HW version, SCSI machine
    
    printf("CosmosEx preinit app starting...\n");
    //------------------------------------
    if(signal(SIGINT, sigint_handler) == SIG_ERR) {		    // register SIGINT handler
        printf("Cannot register SIGINT handler!\n");
    }

    if(signal(SIGHUP, sigint_handler) == SIG_ERR) {		    // register SIGHUP handler
        printf("Cannot register SIGHUP handler!\n");
    }

    //------------------------------------
    // normal app run follows
    Debug::printfLogLevelString();

    char appVersion[16];
    Version::getAppVersion(appVersion);
    
    Debug::out(LOG_INFO, "\n\n---------------------------------------------------");
    Debug::out(LOG_INFO, "CosmosEx preinit starting, version: %s", appVersion);

//	system("sudo echo none > /sys/class/leds/led0/trigger");	    // disable usage of GPIO 23 (pin 16) by LED

	if(!gpio_open()) {									            // try to open GPIO and SPI on RPi
		return 0;
	}

    //-------------
    // Copy the configdrive to /tmp so we can change the content as needed.
    // This must be done before new CCoreThread because it reads the data from /tmp/configdrive 
    system("rm -rf /tmp/configdrive");                      // remove any old content
    system("mkdir /tmp/configdrive");                       // create dir
    system("cp -r /ce/app/configdrive/* /tmp/configdrive"); // copy new content
    //-------------
    core = new CCoreThread();
    
	core->run();										// run the main thread

	delete core;
	gpio_close();										// close gpio and spi

    Debug::out(LOG_INFO, "CosmosEx preinit terminated.");
    printf("Preinit terminated\n");
    return 0;
}
Esempio n. 7
0
void gpio_write(uint32_t bit, int en)
{
	int f;

	if ((f = gpio_open(0)) < 0) return;

	_gpio_ioctl(f, GPIO_IOC_RESERVE, bit, bit);
	_gpio_ioctl(f, GPIO_IOC_OUTEN, bit, bit);
	_gpio_ioctl(f, GPIO_IOC_OUT, bit, en ? bit : 0);
	close(f);
}
Esempio n. 8
0
main(){
  int i;
  int fd;

  gpio_export(PIN);
  fd = gpio_open(PIN, "direction", O_WRONLY);
  write(fd, "out", 3);
  close(fd);

  fd = gpio_open(PIN, "value", O_WRONLY);
  for(i = 0; i < 5; i++){
    write(fd, "1", 1);
    usleep(50000);
    write(fd, "0", 1);
    usleep(50000);
  }

  close(fd);
  gpio_unexport(PIN);
}
Esempio n. 9
0
static gboolean
chassis_reboot(gpointer connection)
{
    int rc = 0;
    uint8_t gpio = 0;
    GDBusProxy *proxy;
    GError *error;
    GVariant *parm = NULL;
    GVariant *result = NULL;

    // The gpio line may flicker during power on/off, so check that the value
    // is still 0 (checkstopped) and that host is booted in order to reboot
    rc = gpio_open(&checkstop);
    if (rc != GPIO_OK) {
        return FALSE;
    }
    rc = gpio_read(&checkstop, &gpio);
    if (rc != GPIO_OK) {
        gpio_close(&checkstop);
        return FALSE;
    }
    gpio_close(&checkstop);
    if ((!gpio) && (is_host_booted(connection)))
    {
        printf("Host Checkstop, rebooting host\n");
        error = NULL;
        proxy = g_dbus_proxy_new_sync((GDBusConnection*)connection,
            G_DBUS_PROXY_FLAGS_NONE,
            NULL, /* GDBusInterfaceInfo* */
            "org.openbmc.control.Chassis", /* name */
            "/org/openbmc/control/chassis0", /* object path */
            "org.openbmc.control.Chassis", /* interface name */
            NULL, /* GCancellable */
            &error);
        g_assert_no_error(error);

        error = NULL;
        result = g_dbus_proxy_call_sync(proxy,
            "reboot",
            parm,
            G_DBUS_CALL_FLAGS_NONE,
            -1,
            NULL,
            &error);
        g_assert_no_error(error);
    }

    return FALSE;
}
Esempio n. 10
0
/**
  Do GPIO initialization

  Initialize input/output pins, and pin status

  @param void
  @return void
*/
void IO_InitGPIO(void)
{
    UINT32 iSValue;

    DBG_IND("GPIO START\r\n");
    //--------------------------------------------------------------------
    // Open GPIO driver
    //--------------------------------------------------------------------
    #if 1 //_MIPS_TODO
    gpio_open();
    for(iSValue=0 ; iSValue<totalGpioInitCount ; iSValue++)
    {
        if (uiGPIOMapInitTab[iSValue].GpioDir == GPIO_DIR_INPUT)
        {
            gpio_setDir(uiGPIOMapInitTab[iSValue].GpioPin, GPIO_DIR_INPUT);
            pad_setPullUpDown(uiGPIOMapInitTab[iSValue].PadPin, uiGPIOMapInitTab[iSValue].PadDir);
        }
        else
        {
            gpio_setDir(uiGPIOMapInitTab[iSValue].GpioPin, GPIO_DIR_OUTPUT);
            if (uiGPIOMapInitTab[iSValue].PadDir == GPIO_SET_OUTPUT_HI)
            {
                gpio_setPin(uiGPIOMapInitTab[iSValue].GpioPin);
            }
            else
            {
                gpio_clearPin(uiGPIOMapInitTab[iSValue].GpioPin);
            }
        }
    }
    #endif

    //--------------------------------------------------------------------
    // Use Non-Used PWM to be Delay Timer
    //--------------------------------------------------------------------
    #if defined(PWMID_TIMER)
    Delay_setPwmChannels(PWMID_TIMER);
    #endif

    #if 1 // defined(__ECOS) && (_WIFI_MODULE_ == _WIFI_MODULE_RTL8192_)
    pad_setDrivingSink(PAD_DS_CGPIO23, PAD_DRIVINGSINK_10MA);
    //pad_setDrivingSink(PAD_DS_CGPIO23, PAD_DRIVINGSINK_10MA);
    debug_msg("^Gpad_setDrivingSink\r\n");
    #endif

    DBG_IND("GPIO END\r\n");

}
Esempio n. 11
0
int gpio_main(int argc, char *argv[])
{
    const char hex[] = "0123456789ABCDEF0123456789ABCDEF";
    unsigned long v;
    int bit;
    int i;
    char s[33];
    int f;

    if ((argc == 3) && ((strncmp(argv[1], "en", 2) == 0) || (strncmp(argv[1], "di", 2) == 0))) {
        bit = atoi(argv[2]);
        if ((bit >= 0) && (bit <= 20)) {
            bit = 1 << bit;
            {
                gpio_write(bit, argv[1][0] == 'e');
                return 0;
            }
        }
    }
    else if (argc >= 2) {
        if (strncmp(argv[1], "po", 2) == 0) {
            if (argc >= 3)
                bit = atoi(argv[2]);
            else
                bit = 0;
            printf("Enable gpio mask: 0x%04X\n", bit);

            if ((f = gpio_open(bit)) < 0) {
                printf("Failed to open gpio\n");
                return 0;
            }
            while ((v = _gpio_read(f)) != ~0) {
                for (i = 31; i >= 0; --i) {
                    s[i] = (v & (1 << i)) ? hex[i] : '.';
                }
                s[32] = 0;
                printf("%08lX %s\n", v, s);
                sleep(1);
            }
            close(f);
            return 0;
        }
    }

    usage_exit(argv[0], "<enable|disable|poll> <pin|[poll_mask]>\n");
}
Esempio n. 12
0
static gboolean
on_set_power_state(ControlPower *pwr,
		GDBusMethodInvocation *invocation,
		guint state,
		gpointer user_data)
{
	Control* control = object_get_control((Object*)user_data);
	if(state > 1)
	{
		g_dbus_method_invocation_return_dbus_error(invocation,
				"org.openbmc.ControlPower.Error.Failed",
				"Invalid power state");
		return TRUE;
	}
	// return from method call
	control_power_complete_set_power_state(pwr,invocation);
	if(state == control_power_get_state(pwr))
	{
		g_print("Power already at requested state: %d\n",state);
	}
	else
	{
		int error = 0;
		do {
			if(state == 1) {
				control_emit_goto_system_state(control,"HOST_POWERING_ON");
			} else {
				control_emit_goto_system_state(control,"HOST_POWERING_OFF");
			}
			error = gpio_open(&power_pin);
			if(error != GPIO_OK) { break;	}
			error = gpio_write(&power_pin,!state);
			if(error != GPIO_OK) { break;	}
			gpio_close(&power_pin);
			control_power_set_state(pwr,state);
		} while(0);
		if(error != GPIO_OK)
		{
			printf("ERROR PowerControl: GPIO set power state (rc=%d)\n",error);
		}
	}
	return TRUE;
}
Esempio n. 13
0
    /**
      Do GPIO initialization

      Initialize input/output pins, and pin status

      @param void
      @return void
    */
    void IO_InitGPIO(void)
    {
        UINT32 iSValue;

        DBG_IND("GPIO START\r\n");
        //--------------------------------------------------------------------
        // Open GPIO driver
        //--------------------------------------------------------------------
#if 1 //_MIPS_TODO
gpio_open();
for(iSValue=0 ; iSValue<totalGpioInitCount ; iSValue++)
{
if (uiGPIOMapInitTab[iSValue].GpioDir == GPIO_DIR_INPUT)
{
gpio_setDir(uiGPIOMapInitTab[iSValue].GpioPin, GPIO_DIR_INPUT);
pad_setPullUpDown(uiGPIOMapInitTab[iSValue].PadPin, uiGPIOMapInitTab[iSValue].PadDir);
} 
else
{
gpio_setDir(uiGPIOMapInitTab[iSValue].GpioPin, GPIO_DIR_OUTPUT);
if (uiGPIOMapInitTab[iSValue].PadDir == GPIO_SET_OUTPUT_HI)
{
gpio_setPin(uiGPIOMapInitTab[iSValue].GpioPin);
} 
else
{
gpio_clearPin(uiGPIOMapInitTab[iSValue].GpioPin);
}
}
}
#endif

        //--------------------------------------------------------------------
        // Use Non-Used PWM to be Delay Timer
        //--------------------------------------------------------------------
#if defined(PWMID_TIMER)
Delay_setPwmChannels(PWMID_TIMER);
#endif

        DBG_IND("GPIO END\r\n");

    }
Esempio n. 14
0
int get_presence(GDBusConnection* connection, GPIO* gpio, uint8_t* present)
{
	int rc = GPIO_OK;
	do {
		rc = gpio_init(connection,gpio);
		if (rc != GPIO_OK) { break; }
		uint8_t gpio_val;
		rc = gpio_open(gpio);
		if (rc != GPIO_OK) { break; }
		rc = gpio_read(gpio,&gpio_val);
		if (rc != GPIO_OK) { gpio_close(gpio); break; }
		gpio_close(gpio);
		*present = gpio_val;	
	} while(0);
	if (rc != GPIO_OK)
	{
		printf("ERROR pcie_slot_present: GPIO error %s (rc=%d)\n",gpio->name,rc);
	}
	return rc; 
}
Esempio n. 15
0
BOOL GPIO_IO_Init(void)
{
	fdIO = gpio_open();
    	if (fdIO < 0)
		return FALSE;
    	else 	
	{

		__gpio_as_output(AC97_CTL);      /* gpio pin as output */
		__gpio_as_output(RS485_SEND);    /* gpio pin as output */
		__gpio_as_output(IO_RED_LED); 		
		__gpio_as_output(IO_GREEN_LED); 	
		__gpio_as_output(TFT_BACK_LIGHT);
		//__gpio_as_input(TFT_BACK_LIGHT);
		
		__gpio_set_pin(AC97_CTL);        /* output high level */
	//	__gpio_clear_pin(AC97_CTL);        /* output low level */
		__gpio_set_pin(RS485_SEND);      /* output high level */
		
		CalibrationTimeBaseValue();
		return TRUE;
   	}
}
Esempio n. 16
0
// python function setup(channel, direction, pull_up_down=PUD_OFF)
static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs)
{
   int channel, direction;
   static char *kwlist[] = {"channel", "direction", NULL};
   int func;
   
   if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", kwlist, &channel, &direction))
      return NULL;

   if (direction != INPUT && direction != OUTPUT)
   {
      PyErr_SetString(InvalidDirectionException, "An invalid direction was passed to setup()");
      return NULL;
   }

   func = gpio_open(channel, direction);
   if (func != 0)  // already an output not set from this program)
   {
      PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.", 1);
   }

   Py_INCREF(Py_None);
   return Py_None;
}
Esempio n. 17
0
static gboolean
on_boot(ControlHost *host,
		GDBusMethodInvocation *invocation,
		gpointer user_data)
{
	int rc = GPIO_OK;
	GDBusProxy *proxy;
	GError *error = NULL;
	GVariant *result = NULL;
	GDBusConnection *connection =
		g_dbus_object_manager_server_get_connection(manager);

	if(control_host_get_debug_mode(host)==1)
	{
		g_print("Enabling debug mode; not booting host\n");
		rc |= gpio_open(&fsi_enable);
		rc |= gpio_open(&cronus_sel);
		rc |= gpio_write(&fsi_enable,1);
		rc |= gpio_write(&cronus_sel,0);
		if(rc!=GPIO_OK) {
			g_print("ERROR enabling debug mode: %d\n",rc);
		}
		return TRUE;
	}
	g_print("Booting host\n");
	Control* control = object_get_control((Object*)user_data);
	control_host_complete_boot(host,invocation);
	do {
		rc = gpio_open(&fsi_clk);
		rc |= gpio_open(&fsi_data);
		rc |= gpio_open(&fsi_enable);
		rc |= gpio_open(&cronus_sel);
		rc |= gpio_open(&Throttle);
		rc |= gpio_open(&idbtn);
		if(rc!=GPIO_OK) { break; }

		//setup dc pins
		rc = gpio_write(&cronus_sel,1);
		rc |= gpio_write(&fsi_enable,1);
		rc |= gpio_write(&fsi_clk,1);
		rc |= gpio_write(&Throttle,1);
		rc |= gpio_write(&idbtn,0);
		if(rc!=GPIO_OK) { break; }

		//data standy state
		rc = fsi_standby();

		//clear out pipes
		rc |= gpio_write(&fsi_data,0);
		rc |= gpio_clock_cycle(&fsi_clk,256);
		rc |= gpio_write(&fsi_data,1);
		rc |= gpio_clock_cycle(&fsi_clk,50);
		if(rc!=GPIO_OK) { break; }

		rc = fsi_bitbang(attnA);
		rc |= fsi_standby();

		rc |= fsi_bitbang(attnB);
		rc |= fsi_standby();

		rc |= fsi_bitbang(attnC);
		rc |= fsi_standby();
		if(rc!=GPIO_OK) { break; }

		const gchar* flash_side = control_host_get_flash_side(host);
		g_print("Using %s side of the bios flash\n",flash_side);
		if(strcmp(flash_side,"primary")==0) {
			rc |= fsi_bitbang(primary);
		} else if(strcmp(flash_side,"golden") == 0) {
			rc |= fsi_bitbang(golden);
		} else {
			g_print("ERROR: Invalid flash side: %s\n",flash_side);
			rc = 0xff;

		}
		rc |= fsi_standby();
		if(rc!=GPIO_OK) { break; }

		rc = fsi_bitbang(go);

		rc |= gpio_write(&fsi_data,1); /* Data standby state */
		rc |= gpio_clock_cycle(&fsi_clk,2);

		rc |= gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
		rc |= gpio_write(&fsi_enable,0);
		rc |= gpio_clock_cycle(&fsi_clk,16);
		rc |= gpio_write(&fsi_clk,0); /* Data standby state */

	} while(0);
	if(rc != GPIO_OK)
	{
		g_print("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
	} else {
		control_emit_goto_system_state(control,"HOST_BOOTING");
	}
	gpio_close(&fsi_clk);
	gpio_close(&fsi_data);
	gpio_close(&fsi_enable);
	gpio_close(&cronus_sel);
	gpio_close(&Throttle);
	gpio_close(&idbtn);

	// Start watchdog with 30s timeout per the OpenPower Host IPMI Spec.
	// Once the host starts booting, it'll reset and refresh the timer.
	error = NULL;
	proxy = g_dbus_proxy_new_sync(connection,
		G_DBUS_PROXY_FLAGS_NONE,
		NULL, /* GDBusInterfaceInfo* */
		"org.openbmc.watchdog.Host", /* name */
		"/org/openbmc/watchdog/host0", /* object path */
		"org.openbmc.Watchdog", /* interface name */
		NULL, /* GCancellable */
		&error);
	g_assert_no_error(error);
	if(error)
		goto exit;

	// Set watchdog timer to 30s
	error = NULL;
	result = g_dbus_proxy_call_sync(proxy,
		"set",
		g_variant_new("(i)", 30000),
		G_DBUS_CALL_FLAGS_NONE,
		-1,
		NULL,
		&error);
	g_assert_no_error(error);
	if (error)
		goto exit;
	if (result)
		g_variant_unref(result);

	// Start watchdog timer
	error = NULL;
	result = g_dbus_proxy_call_sync(proxy,
		"start",
		NULL,
		G_DBUS_CALL_FLAGS_NONE,
		-1,
		NULL,
		&error);
	g_assert_no_error(error);
	if (error)
		goto exit;

	control_host_emit_booted(host);

exit:
	if (result)
		g_variant_unref(result);

	return TRUE;
}
Esempio n. 18
0
BOOL GPIO_IO_Init(void)
{
        int i;
        fdJzIO = open ("/dev/mem", O_RDWR);
	int fd;
	fd=gpio_open();
	if (fd < 0)
	{
		printf("open nand fail!\n");
	}

        if (fdJzIO < 0)
                return FALSE;
        else
        {
                //mmap jz register
                jz_reg = (unsigned char *) mmap(0, 0x100000, PROT_READ|PROT_WRITE,
                                          MAP_SHARED, fdJzIO, JZ_REG_BASE);
                if (jz_reg == NULL)
                {
                        close(fdJzIO);
                        return FALSE;
                }

                //jz gpio base
                jz_gpio_base=(unsigned char *)(jz_reg + JZ_GPIO_REG);
                gpio_reg = (jz_gpio_t *)(jz_reg + JZ_GPIO_REG);

#ifdef ZEM500
                __gpio_as_output(GPIO33);     //multi used by sp2
                __gpio_as_output(GPIO100);    
                __gpio_as_output(GPIO101);    
                __gpio_as_output(GPIO104);    
                __gpio_as_output(GPIO105);
                __gpio_as_output(GPIO107);

                __gpio_as_input(GPIO_BATTERY);    
		
                __gpio_set_pin(GPIO107);        /* enable i2s voice */
                __gpio_set_pin(GPIO33);      /* output high level */
#else
                __gpio_as_output(USB_POWER_PIN); /* gpio pin as output */
                __gpio_as_output(RS485_SEND);    /* gpio pin as output */
                __gpio_as_output(SHUT_KEY);     //SHUT_KEY = 103 is Eth/232 switch

                __gpio_clear_pin(USB_POWER_PIN); /* output low level  */
                __gpio_set_pin(AC97_CTL);        /* output high level */
                __gpio_set_pin(RS485_SEND);      /* output high level */
        //      __gpio_set_pin(SHUT_KEY);        /* output high level */ none for ZEM500
#endif
                //flash 4M Bytes
                FlashBaseAddr = (REG16 *)mmap(0, 0x400000, PROT_READ|PROT_WRITE,
                                              MAP_SHARED, fdJzIO, FLASH_BASE);
                if (FlashBaseAddr == NULL)
                {
                        close(fdJzIO);
                        return FALSE;
                }
                gFlash16 = FlashBaseAddr + 0x100000; //2M BYTES OFFSET
                //if (GetFlashID() == FLASH_TOP_BOOT)  //跟nor flash的相关资料需要删除掉
                {
                        gFlash16 -= STD_SECTOR_SIZE; //64KBytes 
                        DBPRINTF("FLASH IS TOP BOOT TYPE!\n");
                }

                for(i = 0; i < FLASH_SECTOR_COUNT; i++)
                        FlashSectorSizes[i] = STD_SECTOR_SIZE;
                return TRUE;
        }
}
Esempio n. 19
0
int
main(int argc, char **argv)
{
	int i;
	gpio_config_t pin;
	gpio_handle_t handle;
	char *ctlfile = NULL;
	int pinn, pinv, ch;
	int flags, flag, ok;
	int config, toggle, verbose, list;

	config = toggle = verbose = list = pinn = 0;

	while ((ch = getopt(argc, argv, "c:f:lt:v")) != -1) {
		switch (ch) {
		case 'c':
			config = 1;
			pinn = str2int(optarg, &ok);
			if (!ok)
				fail("Invalid pin number: %s\n", optarg);
			break;
		case 'f':
			ctlfile = optarg;
			break;
		case 'l':
			list = 1;
			break;
		case 't':
			toggle = 1;
			pinn = str2int(optarg, &ok);
			if (!ok)
				fail("Invalid pin number: %s\n", optarg);
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			usage();
			break;
		}
	}
	argv += optind;
	argc -= optind;
	if (ctlfile == NULL)
		handle = gpio_open(0);
	else
		handle = gpio_open_device(ctlfile);
	if (handle == GPIO_INVALID_HANDLE) {
		perror("gpio_open");
		exit(1);
	}

	if (list) {
		dump_pins(handle, verbose);
		gpio_close(handle);
		exit(0);
	}

	if (toggle) {
		/*
		 * -t pin assumes no additional arguments
		 */
		if (argc > 0) {
			usage();
			exit(1);
		}
		if (gpio_pin_toggle(handle, pinn) < 0) {
			perror("gpio_pin_toggle");
			exit(1);
		}
		gpio_close(handle);
		exit(0);
	}

	if (config) {
		flags = 0;
		for (i = 0; i < argc; i++) {
			flag = 	str2cap(argv[i]);
			if (flag < 0)
				fail("Invalid flag: %s\n", argv[i]);
			flags |= flag;
		}
		pin.g_pin = pinn;
		pin.g_flags = flags;
		if (gpio_pin_set_flags(handle, &pin) < 0) {
			perror("gpio_pin_set_flags");
			exit(1);
		}
		exit(0);
	}

	/*
	 * Last two cases - set value or print value
	 */
	if ((argc == 0) || (argc > 2)) {
		usage();
		exit(1);
	}

	pinn = str2int(argv[0], &ok);
	if (!ok)
		fail("Invalid pin number: %s\n", argv[0]);

	/*
	 * Read pin value
	 */
	if (argc == 1) {
		pinv = gpio_pin_get(handle, pinn);
		if (pinv < 0) {
			perror("gpio_pin_get");
			exit(1);
		}
		printf("%d\n", pinv);
		exit(0);
	}

	/* Is it valid number (0 or 1) ? */
	pinv = str2int(argv[1], &ok);
	if (!ok || ((pinv != 0) && (pinv != 1)))
		fail("Invalid pin value: %s\n", argv[1]);

	/*
	 * Set pin value
	 */
	if (gpio_pin_set(handle, pinn, pinv) < 0) {
		perror("gpio_pin_set");
		exit(1);
	}

	gpio_close(handle);
	exit(0);
}
Esempio n. 20
0
/**
  Do GPIO initialization

  Initialize input/output pins, and pin status

  @param void
  @return void
*/
void GPIOMap_Init(void)
{

    // Open GPIO driver
    gpio_open();

    // Storage Card detect
    gpio_setIntTypePol(GPIOINT_CARD_DETECT, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_CARD_DETECT, TRUE);

    // Storage Card write protect
    gpio_setIntTypePol(GPIOINT_CARD_WP, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_CARD_WP, TRUE);

    // LCD
    gpio_setDir(GPIO_LCD_BL_CTRL, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_LCD_BL_CTRL);

    //lcd rotate
    gpio_setIntTypePol(GPIOINT_LCD_ROTATE, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_LCD_ROTATE, TRUE);

#if 0
    // LCD reset
    gpio_setDir(GPIO_LCD_RESET, GPIO_DIR_OUTPUT);
    gpio_setPin(GPIO_LCD_RESET);
    TimerDelayMs(1);
    gpio_clearPin(GPIO_LCD_RESET);
    TimerDelayMs(10);
    gpio_setPin(GPIO_LCD_RESET);
    TimerDelayMs(120);
#endif

    // TV
    gpio_setIntTypePol(GPIOINT_TV_DETECT, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_TV_DETECT, TRUE);

    // Sensor
    gpio_setDir(GPIO_SENSOR_RESET, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_SENSOR_RESET);

    // Key
    gpio_setIntTypePol(GPIOINT_KEY_REC, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_KEY_REC, TRUE);

    gpio_setIntTypePol(GPIO_KEY_PLAYBACK, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_KEY_PLAYBACK, TRUE);

    gpio_setIntTypePol(GPIOINT_KEY_MODE, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_KEY_MODE, TRUE);

    gpio_setIntTypePol(GPIOINT_KEY_MENU, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_KEY_MENU, TRUE);

    //LCD rotate
    gpio_setIntTypePol(GPIOINT_LCD_ROTATE, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_LCD_ROTATE, TRUE);

    gpio_setIntTypePol(GPIOINT_LCD_ROTATE2, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_LCD_ROTATE2, TRUE);

    // lcd close pin
    gpio_setIntTypePol(GPIOINT_LCD_CLOSE, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_LCD_CLOSE, TRUE);


    //LDOx_OUT is used to GPIO
    pll_setLDO(PLL_LDO_ID_1, FALSE, FALSE, PLL_LDO_VOLTAGE_2_8V);
    pll_setLDOEn(PLL_LDO_ID_1, FALSE);

    //LDOx_OUT is used to GPIO
    pll_setLDO(PLL_LDO_ID_2, FALSE, FALSE, PLL_LDO_VOLTAGE_2_8V);
    pll_setLDOEn(PLL_LDO_ID_2, FALSE);

}
Esempio n. 21
0
void InitSoccer(const char *port_name,speed_t speed){
  port = SerialOpen(port_name,speed);
  gpio_open(17,1);
  gpio_open(18,1);
}
Esempio n. 22
0
int
main(int argc, char **argv)
{
	int i;
	gpio_config_t pin;
	gpio_handle_t handle;
	char *ctlfile = NULL;
	int pinn, pinv, pin_type, ch;
	int flags, flag, ok;
	int config, list, name, toggle, verbose;

	config = toggle = verbose = list = name = pin_type = 0;

	while ((ch = getopt(argc, argv, "cf:lntvNp")) != -1) {
		switch (ch) {
		case 'c':
			config = 1;
			break;
		case 'f':
			ctlfile = optarg;
			break;
		case 'l':
			list = 1;
			break;
		case 'n':
			name = 1;
			break;
		case 'N':
			pin_type = PIN_TYPE_NAME;
			break;
		case'p':
			pin_type = PIN_TYPE_NUMBER;
			break;
		case 't':
			toggle = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			usage();
			break;
		}
	}
	argv += optind;
	argc -= optind;
	if (ctlfile == NULL)
		handle = gpio_open(0);
	else
		handle = gpio_open_device(ctlfile);
	if (handle == GPIO_INVALID_HANDLE) {
		perror("gpio_open");
		exit(1);
	}

	if (list) {
		dump_pins(handle, verbose);
		gpio_close(handle);
		exit(0);
	}

	if (argc == 0)
		usage();

	/* Find the pin number by the name */
	switch (pin_type) {
	default:
		/* First test if it is a pin number */
		pinn = str2int(argv[0], &ok);
		if (ok) {
			/* Test if we have any pin named by this number and tell the user */
			if (get_pinnum_by_name(handle, argv[0]) != -1)
				fail("%s is also a pin name, use -p or -N\n", argv[0]);
		} else {
			/* Test if it is a name */
			if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
				fail("Can't find pin named \"%s\"\n", argv[0]);
		}
		break;
	case PIN_TYPE_NUMBER:
		pinn = str2int(argv[0], &ok);
		if (!ok)
			fail("Invalid pin number: %s\n", argv[0]);
		break;
	case PIN_TYPE_NAME:
		if ((pinn = get_pinnum_by_name(handle, argv[0])) == -1)
			fail("Can't find pin named \"%s\"\n", argv[0]);
		break;
	}

	/* Set the pin name. */
	if (name) {
		if (argc != 2)
			usage();
		if (gpio_pin_set_name(handle, pinn, argv[1]) < 0) {
			perror("gpio_pin_set_name");
			exit(1);
		}
		exit(0);
	}

	if (toggle) {
		/*
                * -t pin assumes no additional arguments
                */
		if (argc > 1)
			usage();
		if (gpio_pin_toggle(handle, pinn) < 0) {
			perror("gpio_pin_toggle");
			exit(1);
		}
		gpio_close(handle);
		exit(0);
	}

	if (config) {
		flags = 0;
		for (i = 1; i < argc; i++) {
			flag = 	str2cap(argv[i]);
			if (flag < 0)
				fail("Invalid flag: %s\n", argv[i]);
			flags |= flag;
		}
		pin.g_pin = pinn;
		pin.g_flags = flags;
		if (gpio_pin_set_flags(handle, &pin) < 0) {
			perror("gpio_pin_set_flags");
			exit(1);
		}
		exit(0);
	}

	/*
	 * Last two cases - set value or print value
	 */
	if ((argc == 0) || (argc > 2))
		usage();

	/*
	 * Read pin value
	 */
	if (argc == 1) {
		pinv = gpio_pin_get(handle, pinn);
		if (pinv < 0) {
			perror("gpio_pin_get");
			exit(1);
		}
		printf("%d\n", pinv);
		exit(0);
	}

	/* Is it valid number (0 or 1) ? */
	pinv = str2int(argv[1], &ok);
	if (ok == 0 || ((pinv != 0) && (pinv != 1)))
		fail("Invalid pin value: %s\n", argv[1]);

	/*
	 * Set pin value
	 */
	if (gpio_pin_set(handle, pinn, pinv) < 0) {
		perror("gpio_pin_set");
		exit(1);
	}

	gpio_close(handle);
	exit(0);
}
Esempio n. 23
0
static gboolean
on_boot         (ControlHost        *host,
                GDBusMethodInvocation  *invocation,
                gpointer                user_data)
{
	// TODO: Add error checking
	g_print("Do Boot\n");
	int rc = GPIO_OK;

	Control* control = object_get_control((Object*)user_data);
	control_host_complete_boot(host,invocation);
	do {	
		rc |= gpio_open(&fsi_clk);
		rc |= gpio_open(&fsi_data);
		rc |= gpio_open(&fsi_enable);
		rc |= gpio_open(&cronus_sel);
		if (rc!=GPIO_OK) { break; }
		
		rc = gpio_write(&cronus_sel,1);
		//putcfam pu 281c 30000000 -p0
		char a[] = "000011111111110101111000111001100111111111111111111111111111101111111111";
		//putcfam pu 281c B0000000 -p0
		char b[] = "000011111111110101111000111000100111111111111111111111111111101101111111";

		gpio_write(&fsi_enable,1);
		gpio_write(&fsi_clk,1);
		gpio_write(&fsi_data,1);
		gpio_clock_cycle(&fsi_clk,5000);
		gpio_write(&fsi_data,0);
		gpio_clock_cycle(&fsi_clk,256);
		gpio_write(&fsi_data,1);
		gpio_clock_cycle(&fsi_clk,50);
		uint16_t i=0;
		for(i=0;i<strlen(a);i++) {
			gpio_writec(&fsi_data,a[i]);
			gpio_clock_cycle(&fsi_clk,1);
		}
		gpio_write(&fsi_data,1); /* Data standby state */
		gpio_clock_cycle(&fsi_clk,5000);

		for(i=0;i<strlen(b);i++) {
			gpio_writec(&fsi_data,b[i]);
			gpio_clock_cycle(&fsi_clk,1);
		}
		gpio_write(&fsi_data,1); /* Data standby state */
		gpio_clock_cycle(&fsi_clk,2);

	        gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
	        gpio_write(&fsi_enable,0);
	        gpio_clock_cycle(&fsi_clk,16);
	        gpio_write(&fsi_clk,0); /* Data standby state */
	
	} while(0);
	if (rc != GPIO_OK)
	{
		printf("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
	}	
	gpio_close(&fsi_clk);
	gpio_close(&fsi_data);
	gpio_close(&fsi_enable);
	gpio_close(&cronus_sel);

	control_emit_goto_system_state(control,"HOST_BOOTING");
	
	control_host_emit_booted(host);
	return TRUE;
}
Esempio n. 24
0
/**
  Do GPIO initialization

  Initialize input/output pins, and pin status

  @param void
  @return void
*/
void GPIOMap_Init(void)
{
    // Open GPIO driver
    gpio_open();

    // Shutter key
    gpio_setIntTypePol(GPIOINT_KEY_SHUTTER1, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_SHUTTER1, TRUE);

    gpio_setIntTypePol(GPIOINT_KEY_SHUTTER2, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_SHUTTER2, TRUE);

    // Storage Card detect
    gpio_setIntTypePol(GPIOINT_CARD_DETECT, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_CARD_DETECT, TRUE);

    // Storage Card write protect
    dma_setKeyScanEN(DRAM_KEY_CARD_WP, TRUE);

    // LCD
    gpio_setDir(GPIO_LCD_BL_CTRL, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_LCD_BL_CTRL);

    //gpio_setDir(GPIO_LCD_BL_PWR, GPIO_DIR_OUTPUT);
    //gpio_setPin(GPIO_LCD_BL_PWR);

     //Video Light  //michael
    gpio_setDir(VIDEO_FLOOD_LIGHT, GPIO_DIR_OUTPUT);
    gpio_clearPin(VIDEO_FLOOD_LIGHT);

    // TV detect
    gpio_setIntTypePol(GPIOINT_TV_DETECT, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_TV_DETECT, TRUE);

    // LCD ROTATE detect
    gpio_setIntTypePol(GPIOINT_LCD_ROTATE, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_LCD_ROTATE, TRUE);

    //Power Det
    gpio_setIntTypePol(GPIOINT_POWER_DET, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_POWER_DET, TRUE);

    // LED
//    gpio_setDir(GPIO_LED_R, GPIO_DIR_OUTPUT);
//    gpio_setPin(GPIO_LED_R);


    // Sensor
    //pinmux_switch_dgpio(PINMUX_DGPIO1, PINMUX_DGPIO1_GPIO);

    gpio_setDir(GPIO_SENSOR_RESET, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_SENSOR_RESET);

    #if (_SUDPLAY_USE_ == _SUDPLAY_USE_PWM_IOSIM_)
    gpio_setDir(GPIO_BEEP_PWMIOSIM, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_BEEP_PWMIOSIM);
    #endif

    // PWM Init
    //pwm_open(PWM_FLASH_CHARGE);

    #if (_SUDPLAY_USE_ == _SUDPLAY_USE_PWM_)
    pwm_open(PWM_BEEP_SOUND);
    #endif
}
Esempio n. 25
0
int main(int argc, char **argv) {
    pthread_t pth;
    int opt;
    struct ifreq ifr;
    struct trigger_t trigger_data;
    struct sockaddr_can caddr;
    fd_set readfds, exceptfds;
    struct can_frame frame;
    uint16_t member;
    uint8_t buffer[MAXLEN];

    memset(&trigger_data, 0, sizeof(trigger_data));
    memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
    strcpy(ifr.ifr_name, "can0");

    trigger_data.led_pin = -1;
    trigger_data.pb_pin = -1;

    trigger_data.interval = DEF_INTERVAL;

    while ((opt = getopt(argc, argv, "i:l:p:t:fvh?")) != -1) {
	switch (opt) {
	case 'i':
	    strncpy(ifr.ifr_name, optarg, sizeof(ifr.ifr_name));
	    break;
	case 't':
	    trigger_data.interval = atoi(optarg);
	    break;
	case 'l':
	    trigger_data.led_pin = atoi(optarg);
	    break;
	case 'p':
	    trigger_data.pb_pin = atoi(optarg);
	    break;
	case 'v':
	    trigger_data.verbose = 1;
	    break;
	case 'f':
	    trigger_data.background = 0;
	    break;
	case 'h':
	case '?':
	    usage(basename(argv[0]));
	    exit(EXIT_SUCCESS);
	default:
	    usage(basename(argv[0]));
	    exit(EXIT_FAILURE);
	}
    }

    /* prepare CAN socket */
    if ((trigger_data.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
	fprintf(stderr, "error creating CAN socket: %s\n", strerror(errno));
	exit(EXIT_FAILURE);
    }

    memset(&caddr, 0, sizeof(caddr));
    caddr.can_family = AF_CAN;
    if (ioctl(trigger_data.socket, SIOCGIFINDEX, &ifr) < 0) {
	fprintf(stderr, "setup CAN socket error: %s %s\n", strerror(errno), ifr.ifr_name);
	exit(EXIT_FAILURE);
    }
    caddr.can_ifindex = ifr.ifr_ifindex;

    if (bind(trigger_data.socket, (struct sockaddr *)&caddr, sizeof(caddr)) < 0) {
	fprintf(stderr, "error binding CAN socket: %s\n", strerror(errno));
	exit(EXIT_FAILURE);
    }

    trigger_data.caddr = caddr;

    /* Create thread if LED pin defined */
    if ((trigger_data.led_pin) > 0) {
	trigger_data.led_pattern = LED_ST_HB_SLOW;
	gpio_export(trigger_data.led_pin);
	gpio_direction(trigger_data.led_pin, 0);

	if (pthread_mutex_init(&lock, NULL)) {
	    fprintf(stderr, "can't init mutex %s\n", strerror(errno));
	    exit(EXIT_FAILURE);
	}

	if (pthread_create(&pth, NULL, LEDMod, &trigger_data)) {
	    fprintf(stderr, "can't create pthread %s\n", strerror(errno));
	    exit(EXIT_FAILURE);
	}
	if (!trigger_data.background && trigger_data.verbose)
	    printf("created LED thread\n");
    }

    if (trigger_data.background) {
	pid_t pid;

	/* fork off the parent process */
	pid = fork();
	if (pid < 0) {
	    exit(EXIT_FAILURE);
	}
	/* if we got a good PID, then we can exit the parent process. */
	if (pid > 0) {
	    if (trigger_data.verbose)
		printf("Going into background ...\n");
	    exit(EXIT_SUCCESS);
	}
    }

    /* initialize push button */
    if ((trigger_data.pb_pin) > 0) {
	/* first free pin */
	gpio_unexport(trigger_data.pb_pin);
	gpio_export(trigger_data.pb_pin);
	gpio_direction(trigger_data.pb_pin, 1);
	gpio_edge(trigger_data.pb_pin, EDGE_FALLING);
	trigger_data.pb_fd = gpio_open(trigger_data.pb_pin);
    }

    FD_ZERO(&readfds);
    FD_ZERO(&exceptfds);
    /* delete pending push button event */
    if ((trigger_data.pb_pin) > 0) {
	read(trigger_data.pb_fd, NULL, 100);
	lseek(trigger_data.pb_fd, 0, SEEK_SET);
    }
    /* loop forever TODO: if interval is set */
    while (1) {
	FD_SET(trigger_data.socket, &readfds);
	/* extend FD_SET only if push button pin is set */
	if (trigger_data.pb_pin > 0)
	    FD_SET(trigger_data.pb_fd, &exceptfds);
	if (select(MAX(trigger_data.socket, trigger_data.pb_fd) + 1, &readfds, NULL, &exceptfds, NULL) < 0) {
	    fprintf(stderr, "select error: %s\n", strerror(errno));
	    exit(EXIT_FAILURE);
	}
	/* CAN frame event */
	if (FD_ISSET(trigger_data.socket, &readfds)) {
	    if (read(trigger_data.socket, &frame, sizeof(struct can_frame)) < 0)
		fprintf(stderr, "error reading CAN frame: %s\n", strerror(errno));

	    if (frame.can_id & CAN_EFF_FLAG) {
		switch ((frame.can_id & 0x00FF0000UL) >> 16) {
		case 0x31:
		    if (trigger_data.verbose)
			print_can_frame(F_CAN_FORMAT_STRG, &frame);
		    memcpy(&member, frame.data, sizeof(member));
		    member = ntohs(member);
		    /* look for MS2 */
		    if ((member & 0xfff0) == 0x4d50)
			get_ms2_dbsize(&trigger_data);
		    break;
		case 0x41:
		    if (trigger_data.verbose)
			print_can_frame(F_CAN_FORMAT_STRG, &frame);
		    break;
		case 0x42:
		    get_data(&trigger_data, &frame);
		    /* if (trigger_data.verbose)
		       print_can_frame(F_CAN_FORMAT_STRG, &frame); */
		    break;
		default:
		    if (trigger_data.verbose)
			print_can_frame(F_CAN_FORMAT_STRG, &frame);
		    break;
		}
	    }
	}
	/* push button event */
	if (FD_ISSET(trigger_data.pb_fd, &exceptfds)) {
	    set_led_pattern(&trigger_data, LED_ST_HB_FAST);

	    /* wait some time for LED pattern change */
	    usec_sleep(1000 * 1000);
	    /* send CAN Member Ping */
	    frame.can_id = 0x00300300;
	    frame.can_dlc = 0;
	    memset(frame.data, 0, 8);
	    if (send_can_frame(trigger_data.socket, &frame, trigger_data.verbose) < 0)
		fprintf(stderr, "can't send CAN Member Ping: %s\n", strerror(errno));

	    lseek(trigger_data.pb_fd, 0, SEEK_SET);
	    if (read(trigger_data.pb_fd, buffer, sizeof(buffer)) < 0)
		fprintf(stderr, "error reading GPIO status: %s\n", strerror(errno));
	    printf("push button event\n");
	}
    }

    /* TODO : wait until copy is done */

    if ((trigger_data.pb_pin) > 0)
	gpio_unexport(trigger_data.pb_pin);
    if ((trigger_data.led_pin) > 0) {
	gpio_unexport(trigger_data.led_pin);
	pthread_join(pth, (void *)&trigger_data);
	pthread_mutex_unlock(&lock);
    }
    return 0;
}
Esempio n. 26
0
// TODO:  Change to interrupt driven instead of polling
static gboolean
poll_pgood(gpointer user_data)
{
	ControlPower *control_power = object_get_control_power((Object*)user_data);
	Control* control = object_get_control((Object*)user_data);

	//send the heartbeat
	guint poll_int = control_get_poll_interval(control);
	if(poll_int == 0)
	{
		printf("ERROR PowerControl: Poll interval cannot be 0\n");
		return FALSE;
	}
	//handle timeout
	time_t current_time = time(NULL);
	if(difftime(current_time,pgood_timeout_start) > control_power_get_pgood_timeout(control_power)
			&& pgood_timeout_start != 0)
	{
		printf("ERROR PowerControl: Pgood poll timeout\n");
		// set timeout to 0 so timeout doesn't happen again
		control_power_set_pgood_timeout(control_power,0);
		pgood_timeout_start = 0;
		return TRUE;
	}
	uint8_t gpio;

	int rc = gpio_open(&pgood);
	rc = gpio_read(&pgood,&gpio);
	gpio_close(&pgood);
	if(rc == GPIO_OK)
	{
		//if changed, set property and emit signal
		if(gpio != control_power_get_pgood(control_power))
		{
			control_power_set_pgood(control_power,gpio);
			if(gpio==0)
			{
				control_power_emit_power_lost(control_power);
				control_emit_goto_system_state(control,"HOST_POWERED_OFF");
				rc = gpio_open(&pcie_reset);
				rc = gpio_write(&pcie_reset,0);
				gpio_close(&pcie_reset);

				rc = gpio_open(&usb_reset);
				rc = gpio_write(&usb_reset,0);
				gpio_close(&usb_reset);

			}
			else
			{
				control_power_emit_power_good(control_power);
				control_emit_goto_system_state(control,"HOST_POWERED_ON");
				rc = gpio_open(&pcie_reset);
				rc = gpio_write(&pcie_reset,1);
				gpio_close(&pcie_reset);

				rc = gpio_open(&usb_reset);
				rc = gpio_write(&usb_reset,1);
				gpio_close(&usb_reset);
			}
		}
	} else {
		printf("ERROR PowerControl: GPIO read error (gpio=%s,rc=%d)\n",pgood.name,rc);
		//return false so poll won't get called anymore
		return FALSE;
	}
	//pgood is not at desired state yet
	if(gpio != control_power_get_state(control_power) &&
			control_power_get_pgood_timeout(control_power) > 0)
	{
		if(pgood_timeout_start == 0 ) {
			pgood_timeout_start = current_time;
		}
	}
	else
	{
		pgood_timeout_start = 0;
	}
	return TRUE;
}
Esempio n. 27
0
static void
on_bus_acquired(GDBusConnection *connection,
		const gchar *name,
		gpointer user_data)
{
	ObjectSkeleton *object;
	cmdline *cmd = user_data;
	if(cmd->argc < 3)
	{
		g_print("Usage: power_control.exe [poll interval] [timeout]\n");
		return;
	}
	manager = g_dbus_object_manager_server_new(dbus_object_path);
	gchar *s;
	s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
	object = object_skeleton_new(s);
	g_free(s);

	ControlPower* control_power = control_power_skeleton_new();
	object_skeleton_set_control_power(object, control_power);
	g_object_unref(control_power);

	Control* control = control_skeleton_new();
	object_skeleton_set_control(object, control);
	g_object_unref(control);

	//define method callbacks here
	g_signal_connect(control_power,
			"handle-set-power-state",
			G_CALLBACK(on_set_power_state),
			object); /* user_data */

	g_signal_connect(control_power,
			"handle-get-power-state",
			G_CALLBACK(on_get_power_state),
			NULL); /* user_data */

	g_signal_connect(control,
			"handle-init",
			G_CALLBACK(on_init),
			object); /* user_data */


	/* Export the object (@manager takes its own reference to @object) */
	g_dbus_object_manager_server_set_connection(manager, connection);
	g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
	g_object_unref(object);

	// get gpio device paths
	int rc = GPIO_OK;
	do {
		rc = gpio_init(connection,&power_pin);
		if(rc != GPIO_OK) { break; }
		rc = gpio_init(connection,&pgood);
		if(rc != GPIO_OK) { break; }
		rc = gpio_init(connection,&pcie_reset);
		if(rc != GPIO_OK) { break; }
		rc = gpio_init(connection,&usb_reset);
		if(rc != GPIO_OK) { break; }

		uint8_t gpio;
		rc = gpio_open(&pgood);
		if(rc != GPIO_OK) { break; }
		rc = gpio_read(&pgood,&gpio);
		if(rc != GPIO_OK) { break; }
		gpio_close(&pgood);
		control_power_set_pgood(control_power,gpio);
		control_power_set_state(control_power,gpio);
		printf("Pgood state: %d\n",gpio);

	} while(0);
	if(rc != GPIO_OK)
	{
		printf("ERROR PowerControl: GPIO setup (rc=%d)\n",rc);
	}
	//start poll
	pgood_timeout_start = 0;
	int poll_interval = atoi(cmd->argv[1]);
	int pgood_timeout = atoi(cmd->argv[2]);
	if(poll_interval < 1000 || pgood_timeout <5) {
		printf("ERROR PowerControl: poll_interval < 1000 or pgood_timeout < 5\n");
	} else {
		control_set_poll_interval(control,poll_interval);
		control_power_set_pgood_timeout(control_power,pgood_timeout);
		g_timeout_add(poll_interval, poll_pgood, object);
	}
}
Esempio n. 28
0
int main(int argc, char * const argv[])
{
  bitbang_init_st init;
  bitbang_handle_st *hdl = NULL;
  int cs = -1, clk = -1, in = -1, out = -1;
  gpio_st cs_gpio;
  int opt;
  int is_write = 0;
  int is_read = 0;
  int read_bits = 0;
  int write_bits = 0;
  int read_bytes = 0;
  int write_bytes = 0;
  int i;
  uint8_t *read_buf = NULL;;
  uint8_t *write_buf = NULL;;
  bitbang_clk_edge_en dout_edge = BITBANG_CLK_EDGE_FALLING;
  bitbang_clk_edge_en din_edge = BITBANG_CLK_EDGE_RISING;
  bitbang_pin_value_en clk_start = BITBANG_PIN_HIGH;
  bitbang_pin_value_en cs_value = BITBANG_PIN_HIGH;
  spi_context_st ctx;
  bitbang_io_st io;
  int rc = 0;
  int binary = 0;

  memset(&ctx, sizeof(ctx), 0);
  gpio_init_default(&ctx.sc_clk);
  gpio_init_default(&ctx.sc_mosi);
  gpio_init_default(&ctx.sc_miso);
  gpio_init_default(&cs_gpio);

  while ((opt = getopt(argc, argv, "bs:S:c:C:o:O:i:I:w:r:")) != -1) {
    switch (opt) {
    case 'b':
      binary = 1;
      break;
    case 's':
      cs = atoi(optarg);
      break;
    case 'S':
      if (!strcmp(optarg, "high")) {
        cs_value = BITBANG_PIN_HIGH;
      } else if (!strcmp(optarg, "low")) {
        cs_value = BITBANG_PIN_LOW;
      } else {
        usage();
        exit(-1);
      }
      break;
    case 'c':
      clk = atoi(optarg);
      break;
    case 'C':
      if (!strcasecmp(optarg, "high")) {
        clk_start = BITBANG_PIN_HIGH;
      } else if (!strcasecmp(optarg, "low")) {
        clk_start = BITBANG_PIN_LOW;
      } else {
        usage();
        exit(-1);
      }
      break;
    case 'o':
      out = atoi(optarg);
      break;
    case 'O':
      if (!strcasecmp(optarg, "rising")) {
        dout_edge = BITBANG_CLK_EDGE_RISING;
      } else if (!strcasecmp(optarg, "falling")) {
        dout_edge = BITBANG_CLK_EDGE_FALLING;
      } else {
        usage();
        exit(-1);
      }
      break;
    case 'i':
      in = atoi(optarg);
      break;
    case 'I':
      if (!strcasecmp(optarg, "rising")) {
        din_edge = BITBANG_CLK_EDGE_RISING;
      } else if (!strcasecmp(optarg, "falling")) {
        din_edge = BITBANG_CLK_EDGE_FALLING;
      } else {
        usage();
        exit(-1);
      }
      break;
    case 'w':
      is_write = 1;
      write_bits = atoi(optarg);
      if (write_bits <= 0) {
        usage();
        exit(-1);
      }
      break;
    case 'r':
      is_read = 1;
      read_bits = atoi(optarg);
      if (read_bits <= 0) {
        usage();
        exit(-1);
      }
      break;
    default:
      usage();
      exit(-1);
    }
  }

  if (clk < 0 || in < 0 || out < 0) {
    usage();
    exit(-1);
  }

  if ((!is_read && !is_write)) {
    usage();
    exit(-1);
  }

  write_bytes = ((write_bits + 7) / 8);
  if (write_bytes) {
    write_buf = calloc(write_bytes, sizeof(uint8_t));
    if (!write_buf) {
      goto out;
    }
    if (binary) {
      size_t written_bytes;
      written_bytes = fread(write_buf, sizeof(*write_buf), write_bytes, stdin);
      if( written_bytes != write_bytes ) {
        goto out;
      }
    } else {
      for (i = 0; i < write_bytes && i + optind < argc; i++) {
        write_buf[i] = strtoul(argv[i + optind], NULL, 0);
      }
    }
  }

  read_bytes = ((read_bits + 7) / 8);
  if (read_bytes) {
    read_buf = calloc(read_bytes, sizeof(uint8_t));
    if (!read_buf) {
      goto out;
    }
  }

  if (gpio_open(&ctx.sc_clk, clk) || gpio_open(&ctx.sc_miso, in)
      || gpio_open(&ctx.sc_mosi, out)) {
    goto out;
  }

  /* change GPIO directions, only MISO is input, all others are output */
  if (gpio_change_direction(&ctx.sc_clk, GPIO_DIRECTION_OUT)
      || gpio_change_direction(&ctx.sc_miso, GPIO_DIRECTION_IN)
      || gpio_change_direction(&ctx.sc_mosi, GPIO_DIRECTION_OUT)) {
    goto out;
  }

  if (cs != -1) {
    if (gpio_open(&cs_gpio, cs)) {
        goto out;
    }
    if (gpio_change_direction(&cs_gpio, GPIO_DIRECTION_OUT)) {
      goto out;
    }
  }

  bitbang_init_default(&init);
  init.bbi_clk_start = clk_start;
  init.bbi_data_out = dout_edge;
  init.bbi_data_in = din_edge;
  init.bbi_freq = 1000 * 1000;   /* 1M Hz */
  init.bbi_pin_f = spi_pin_f;
  init.bbi_context = &ctx;

  hdl = bitbang_open(&init);
  if (!hdl) {
    goto out;
  }

  if (cs != -1) {
    /* have chip select */
    gpio_write(&cs_gpio, ((cs_value == BITBANG_PIN_HIGH)
                          ? GPIO_VALUE_HIGH : GPIO_VALUE_LOW));
  }

  memset(&io, sizeof(io), 0);
  io.bbio_in_bits = read_bits;
  io.bbio_din = read_buf;
  io.bbio_out_bits = write_bits;
  io.bbio_dout = write_buf;

  rc = bitbang_io(hdl, &io);
  if (rc != 0) {
    goto out;
  }

  if (binary) {
    fwrite(read_buf, sizeof(*read_buf), read_bytes, stdout);
  } else {
    if (write_bits) {
      printf("Wrote %u bits:", write_bits);
      for (i = 0; i < write_bytes; i++) {
        printf(" %02x", write_buf[i]);
      }
      printf("\n");
    }

    if (read_bits) {
      printf("Read %u bits:", read_bits);
      for (i = 0; i < read_bytes; i++) {
        printf(" %02x", read_buf[i]);
      }
      printf("\n");
    }
  }

 out:
  if (hdl) {
    bitbang_close(hdl);
  }
  gpio_close(&ctx.sc_clk);
  gpio_close(&ctx.sc_miso);
  gpio_close(&ctx.sc_mosi);
  if (cs != -1) {
    /* reset have chip select */
    gpio_write(&cs_gpio, ((cs_value == BITBANG_PIN_HIGH)
                          ? GPIO_VALUE_LOW : GPIO_VALUE_HIGH));
    gpio_close(&cs_gpio);
  }

  if (read_buf) {
    free(read_buf);
  }
  if (write_buf) {
    free(write_buf);
  }
  return rc;
}
Esempio n. 29
0
void i2c_init(void){//ok
	gpio_open(SCL,1);
	gpio_open(SDA,1);
	gpio_write(SDA,1);
	gpio_write(SCL,1);
}
Esempio n. 30
0
/**
  Do GPIO initialization

  Initialize input/output pins, and pin status

  @param void
  @return void
*/
void GPIOMap_Init(void)
{
    // Open GPIO driver
    gpio_open();

    #if 0
    gpio_setDir(GPIO_KEY_LEFT, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_LEFT, PAD_PULLUP);

    gpio_setDir(GPIO_KEY_RIGHT, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_RIGHT, PAD_PULLUP);

    gpio_setDir(GPIO_KEY_UP, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_UP, PAD_PULLUP);

    gpio_setDir(GPIO_KEY_DOWN, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_DOWN, PAD_PULLUP);

    gpio_setDir(GPIO_KEY_ZOOMOUT, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_ZOOMOUT, PAD_PULLUP);

    gpio_setDir(GPIO_KEY_ZOOMIN, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_ZOOMIN, PAD_PULLUP);

    gpio_setDir(GPIO_KEY_MENU, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_MENU, PAD_PULLUP);

    gpio_setDir(GPIO_KEY_SHUTTER, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_SHUTTER, PAD_PULLUP);

    gpio_setDir(DGPIO_KEY_SHUTTER2, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_KEY_SHUTTER2, PAD_PULLUP);
    #endif

    // Storage Card detect
    //gpio_setDir(GPIO_CARD_DETECT, GPIO_DIR_INPUT);
    //pad_setPullUpDown(PAD_CARD_DETECT, PAD_PULLUP);
    gpio_setIntTypePol(GPIOINT_CARD_DETECT, GPIO_INTTYPE_LEVEL, GPIO_INTPOL_POSHIGH);
    dma_setKeyScanEN(DRAM_KEY_CARD_DETECT, TRUE);

    // Storage Card write protect
    gpio_setDir(GPIO_CARD_WP, GPIO_DIR_INPUT);
    pad_setPullUpDown(PAD_CARD_WP, PAD_PULLUP);

    //LCD
    gpio_setDir(GPIO_LCD_BL_CTRL, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_LCD_BL_CTRL);

    gpio_setDir(GPIO_LCD_BL_PWR, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_LCD_BL_PWR);

    // Sensor
    gpio_setDir(GPIO_SENSOR_RESET, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_SENSOR_RESET);

    #if (_SUDPLAY_USE_ == _SUDPLAY_USE_PWM_IOSIM_)
    gpio_setDir(GPIO_BEEP_PWMIOSIM, GPIO_DIR_OUTPUT);
    gpio_clearPin(GPIO_BEEP_PWMIOSIM);
    #endif

    // PWM Init
    //pwm_open(PWM_FLASH_CHARGE);

    #if (_SUDPLAY_USE_ == _SUDPLAY_USE_PWM_)
    pwm_open(PWM_BEEP_SOUND);
    #endif
}