Exemplo n.º 1
0
IPAddress WiFiClass::getLocalIP()
{
	FILE *fp = NULL;
	char cmd[128];
	uint8_t ipb[4];
	IPAddress ip;
	trace_debug("getLocalIP");
	sprintf(cmd, "ifconfig %s | egrep \"inet addr\" | cut -d: -f2- > %s",
			ARDUINO_WLAN, TMP_PATH);
	system(cmd);
	if (NULL == (fp = fopen(TMP_PATH, "r"))) {
		trace_error("can't open handle to %s", TMP_PATH);
		return ip;
	}
	fscanf(fp, "%s", cmd); /* inet addr */
	fclose(fp);
	trace_debug("my IP=%s", cmd);
	if(isdigit(cmd[0])) {
		sscanf(cmd, "%hhd.%hhd.%hhd.%hhd", &ipb[0], &ipb[1],
				&ipb[2], &ipb[3]);
		ip._sin.sin_addr.s_addr = ( ((uint32_t)ipb[3])<<24 | \
			((uint32_t)ipb[2])<<16 | ((uint32_t)ipb[1])<<8 | ((uint32_t)ipb[0]) );
		trace_debug("returning ip %3d.%3d.%3d.%3d",
			(ip._sin.sin_addr.s_addr&0x000000FF),
			(ip._sin.sin_addr.s_addr&0x0000FF00)>>8,
			(ip._sin.sin_addr.s_addr&0x00FF0000)>>16,
			(ip._sin.sin_addr.s_addr&0xFF000000)>>24);
	} else {
Exemplo n.º 2
0
/*
 * Initialise ADCs.
 *  - save persistent handles to read ADC input values
 */
int sysfsAdcExport(unsigned adc, int *handle)
{
	char fs_path[SYSFS_BUF];
	static char iio_dev_name[32] = { 0 };
	int fd;

	trace_debug("%s: adc=%u", __func__, adc);

	if (adc > (NUM_ANALOG_INPUTS - 1)) {
		trace_error("%s err adc > %d", __func__, NUM_ANALOG_INPUTS);
		return -1;
	}

	/* We only check for the iio_dev_name once */
	if (iio_dev_name[0] == 0
			&& find_iio_dev_name(iio_dev_name, sizeof(iio_dev_name)) < 0)
		return -1;

	/* Open persistent handle to adc channel */
	snprintf(fs_path, sizeof(fs_path), LINUX_ADC_FMT, iio_dev_name, adc);
	if ((fd = open(fs_path, O_RDONLY)) < 0) {
		trace_error("%s Can't open handle to analog input channel %u: %s",
			    __func__, adc, strerror(errno));
		return -1;
	}
	*handle = fd;

	trace_debug("%s adc=%u, handle=%d, path=%s", __func__, adc, *handle,
			fs_path);

	return 0;
}
Exemplo n.º 3
0
uint8_t Servo::attach(int pin, int min, int max)
{
	// need to validate the pin
	uint8_t list_index = 0;
	bool is_valid_pin = false;

	// let's check the boundaries
	if (min < MIN_PULSE_WIDTH)
		min = MIN_PULSE_WIDTH;
	if (max > MAX_PULSE_WIDTH)
		max = MAX_PULSE_WIDTH;

	trace_debug("%s pin:%d min:%d max:%d\n", __func__, pin, min, max);

	for (list_index = 0; list_index < MAX_NUMBER_OF_SERVOS; list_index++) {
		if (pinData[list_index].pin == pin) {
			is_valid_pin = true;
			break;
		}
	}

	if (!is_valid_pin) {
		trace_error("invalid pin");
		return INVALID_SERVO;
	}

	if (this->index < MAX_NUMBER_OF_SERVOS) {

		// set as active
		pinData[list_index].isActive = true;

		this->pin = pin;
		this->min = min;
		this->max = max;
		this->isAttached = true;

#ifdef SERVO_PWM_WITH_I2C
		this->is188hz = true;
		pinMode(pin, OUTPUT);
		analogWrite(pin, 1);
		writeMicroseconds(DEFAULT_PULSE_WIDTH);
#else
		// sysfs
		this->is188hz = false;
		this->setPeriod(PWM_50Hz);
		this->setDutyCycle(DEFAULT_PULSE_WIDTH * 1000);
		this->enablePin();
#endif  
	}

	trace_debug("%s Attached ok on pin:%d min:%d max:%d\n", __func__,
			pin, this->min, this->max);

	return this->index;
}
Exemplo n.º 4
0
int EthernetUDP::endPacket()
{
	if ( _sock == -1 )
		return -1;
	trace_debug("%s called", __func__);
	return sendUDP();
}
Exemplo n.º 5
0
void digitalWrite(register uint8_t pin, register uint8_t val)
{
	uint32_t idx;

	if (unlikely(pin >= GPIO_TOTAL))
		return;

	if (unlikely(g_APinState[pin].uCurrentInput)) {
		if (val) {
			trace_debug("%s: input pin%u driven high: enabling "
				    "pullup", __func__, pin);
			pinMode(pin, INPUT_PULLUP);
		} else {
			trace_error("%s: input pin%u driven low!", __func__,
				    pin);
		}
		return;
	}

	if (unlikely(g_APinState[pin].uCurrentPwm)) {
		turnOffPWM(pin);
	}

	idx = pinGetIndex(pin);
	digitalWriteSetVal(idx, pin, val);

	// alias - enable w/o on Fab D for waggle of pin 20 to compensate for pin 13 error
	if (unlikely(g_APinDescription[idx].ulGPIOAlias != NONE)){
		idx = pinGetIndex(g_APinDescription[idx].ulGPIOAlias);
		digitalWriteSetVal(idx, g_APinDescription[idx].ulGPIOAlias, val);
	}
	//trace_debug("%s: pin=%d, handle=%d, val=%d", __func__, pin, handle, val);
}
Exemplo n.º 6
0
byte Servo::transform_cypress_duty_cycle_byte(int microsecs)
{

	/* the max division of 23ms (100% duty cycle) is
	 255 units (bytes 0 to 255 according regs 0x2B

	 So, the maximum units for 2ms is 22.17

	 The principle it is used for 188Hz thar correspond
	 a period of 5.319ms.

	 So, the max byte available for different frequency
	 is in maximum time of 2ms or 2.4ms in 8 bits resolution
	 is:

	 = max_duty/1000 * (1/freq)/255
	 = max_duty*255/(1000/freq)

	 */

	int freq = (this->is188hz) ? 188:43.4;
	int max_byte = MAX_PULSE_WIDTH*255*freq/1000000L;

	if(this->min==1000) trace_debug("maxbyte:%d\n", max_byte);

	byte b_duty = map(microsecs, 0, MAX_PULSE_WIDTH, 0, max_byte);

	return b_duty;
}
Exemplo n.º 7
0
/**
 * fastGpioSCInit
 *
 * Initialise the fast SC GPIO interface
 */
int fastGpioSCInit(void)
{
    extern int errno;

    fgpio.regs = NULL;
    fgpio.uio_handle = -1;

    /* Get handle to UIO regs */
    fgpio.uio_handle = open(UIO_DEVICE, O_RDWR);
    if (fgpio.uio_handle < 0) {
        fprintf(stderr, "unable to open %s O_RDONLY\n", UIO_DEVICE);
        return errno;
    }

    /* mmap */
    fgpio.regs = (char*)mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fgpio.uio_handle, 0);
    if (fgpio.regs == MAP_FAILED) {
        fprintf(stderr, "unable to mmap UIO %s @ handle %d", UIO_DEVICE, fgpio.uio_handle);
        close(fgpio.uio_handle);
        fgpio.uio_handle = -1;
    }

    trace_debug("successfully mapped %s size %d handle %d", UIO_DEVICE, MAP_SIZE, fgpio.uio_handle);
    return 0;
}
Exemplo n.º 8
0
int variantPinMode(uint8_t pin, uint8_t mode)
{
    /*
     * Standard (sysfs) or fast-mode UIO options are available for some pins
     *
     * The pin at this time is set to Fast-mode by default, if available
     */

    int ret = 0;
    PinDescription *p = NULL;

    /* Search for entry */
    if ((p = pinDescriptionById(pin)) == NULL) {
        trace_error("%s: invalid pin %u\n", __func__, pin);
        return PIN_EINVAL;
    }

    /* Alternate entries for Fast-Mode GPIO: enable by default if available */
    if (p->pAlternate) {
        p->iAlternate = 1;
        trace_debug("%s: enable Fast-Mode SoC GPIO for pin%u",
                    __func__, pin);
    }

    return 0;
}
Exemplo n.º 9
0
void eepromInit(void)
{
    int fd;
    char buf = 0xff;

    /* Do nothing if file exists already */
    if (access(LINUX_EEPROM, F_OK) == 0)
        return;

    if ((fd = open(LINUX_EEPROM, O_RDWR | O_CREAT, 0660)) < 0) {
        trace_error("%s Can't create EEPROM file: %s", __func__,
                    strerror(errno));
        return;
    }

    if (lseek(fd, 0, SEEK_SET)) {
        trace_error("%s Can't lseek in EEPROM file: %s", __func__,
                    strerror(errno));
        goto err;
    }

    if (write(fd, &buf, LINUX_EEPROM_SIZE) != LINUX_EEPROM_SIZE)
        trace_error("%s Can't write to EEPROM file: %s", __func__,
                    strerror(errno));

    trace_debug("%s Created EEPROM file '%s' of size %u bytes", __func__,
                LINUX_EEPROM, LINUX_EEPROM_SIZE);
err:
    close(fd);
}
Exemplo n.º 10
0
uint8_t gmacd_setup_queue(struct _gmacd* gmacd, uint8_t queue,
		uint16_t rx_size, uint8_t* rx_buffer, struct _gmac_desc* rx_desc,
		uint16_t tx_size, uint8_t* tx_buffer, struct _gmac_desc* tx_desc,
		gmacd_callback_t *tx_callbacks)
{
	Gmac *gmac = gmacd->gmac;
	struct _gmacd_queue* q = &gmacd->queues[queue];

	if (rx_size <= 1 || tx_size <= 1)
		return GMACD_PARAM;

	/* Assign RX buffers */
	if (((uint32_t)rx_buffer & 0x7)
	    || ((uint32_t)rx_desc & 0x7)) {
		rx_size--;
		trace_debug("RX list address adjusted\n\r");
	}
	q->rx_buffer = (uint8_t*)((uint32_t)rx_buffer & 0xFFFFFFF8);
	q->rx_desc = (struct _gmac_desc *)((uint32_t)rx_desc & 0xFFFFFFF8);
	q->rx_size = rx_size;
	q->rx_callback = NULL;

	/* Assign TX buffers */
	if (((uint32_t)tx_buffer & 0x7)
	    || ((uint32_t)tx_desc & 0x7)) {
		tx_size--;
		trace_debug("TX list address adjusted\n\r");
	}
	q->tx_buffer = (uint8_t*)((uint32_t)tx_buffer & 0xFFFFFFF8);
	q->tx_desc = (struct _gmac_desc*)((uint32_t)tx_desc & 0xFFFFFFF8);
	q->tx_size = tx_size;
	q->tx_callbacks = tx_callbacks;
	q->tx_wakeup_callback = NULL;

	/* Reset TX & RX */
	_gmacd_reset_rx(gmacd, queue);
	_gmacd_reset_tx(gmacd, queue);

	/* Setup the interrupts for RX/TX completion (and errors) */
	gmac_enable_it(gmac, queue, GMAC_INT_RX_BITS | GMAC_INT_TX_BITS | GMAC_IER_HRESP);

	return GMACD_OK;
}
Exemplo n.º 11
0
/**
 * fastGpioNCInit
 *
 * Initialise the fast NC GPIO interface
 */
int fastGpioNCInit(void)
{
	int i, ret;
	extern int errno;

	ret = fastGpioFindUioByName(UIO_NAME);
	if (ret < 0) {
		trace_error("Failed to find UIO name '%s': %s\n",
			    UIO_NAME, strerror(ret));
		return ret;
	}
	fgpio.uio_num = ret;

	ret = fastGpioGetInfo(fgpio.uio_num, UIO_PORT, UIO_START_PATH_FMT);
	if (ret < 0) {
		trace_error("Failed to read UIO base port: %s\n",
			    strerror(ret));
		return ret;
	}
	fgpio.baseport = ret;


	ret = fastGpioGetInfo(fgpio.uio_num, UIO_PORT, UIO_SIZE_PATH_FMT);
	if (ret < 0) {
		trace_error("Failed to read UIO size: %s\n", strerror(ret));
		return ret;
	}
	fgpio.size = ret;

	trace_debug("Requesting access to %u I/O ports starting at 0x%04X\n",
		    fgpio.size, fgpio.baseport); fflush(stdout);

	/* Get access to the ports */
	if (ioperm(fgpio.baseport, fgpio.size, 1)) {
		perror("ioperm");
		return errno;
	}

	trace_debug("Initialised PIO on UIO %d size %d baseport 0x%04X\n",
		    fgpio.uio_num, fgpio.size, fgpio.baseport); fflush(stdout);
	return 0;
}
Exemplo n.º 12
0
uint8_t WiFiClass::getEncrBySsid(int count, char * ssid)
{
	for (int i = 0; i < count; i++ ) {
		//trace_debug("checking ssid %s against %s", _networkSsid[i], ssid);
		if (0 == strcmp(ssid, _networkSsid[i])){
			trace_debug("found ssid %s, encr=%d", ssid, getEncrType(_networkEncr[i]));
			return getEncrType(_networkEncr[i]);
		}
	}
	return 0;
}
Exemplo n.º 13
0
void ghoard::heap::deallocate_all_superblocks(){
    for(int i= 0; i<FGROUP_COUNT; ++i){
        superblock * sb = fgroup_heads[i];
        while(sb != NULL){
            remove_superblock(sb);
            raw_deallocate(sb, SUPERBLOCK_SIZE);
            sb = fgroup_heads[i];
        }
    }
    trace_debug();
}
Exemplo n.º 14
0
int parse_arguments(int argc, char** argv, config* conf)
{
    int i;
    size_t* current_size;
    char* name;
    trace_debug("parse_argument: parsing %d arguments", argc);
    for(i = 0; i < argc; i++)
    {
        trace_debug("parse_argument: parsing arg #%d", i);
        if(strlen(argv[i]) > 2 && argv[i][0] == '-' && argv[i][1] == '-')
        {
            name = argv[i] + 2;
            CONFIG_CURRENT(directories)
            else CONFIG_CURRENT(persist)
            else CONFIG_CURRENT(excluded_files)
            else CONFIG_CURRENT(substitutions)
            else CONFIG_CURRENT(zookeepers)
            else CONFIG_CURRENT(brokers)
            else CONFIG_CURRENT(topic)
            else CONFIG_CURRENT(fields)
            else CONFIG_CURRENT(tags)
            else CONFIG_CURRENT(quota)
            else CONFIG_CURRENT(input)
            else CONFIG_CURRENT(output)
            else CONFIG_CURRENT(encoder)
            else CONFIG_CURRENT(debug)
            else CONFIG_CURRENT(log)
            else if(!strcmp(name, "version"))
            {
                printf("fuse_kafka " VERSION ", commit " COMMIT "\n") ;
                return 0;
            }
            else
            {
                printf("unknown option %s\n", argv[i]);
                return 0;
            }
            *current_size = 0;
        }
        else
        {
Exemplo n.º 15
0
IPAddress EthernetClass::subnetMask()
{
	IPAddress ret;
	struct ifaddrs *ifaddr = NULL, *ifa = NULL;
	int family, s;
	char ip_addr[NI_MAXHOST];

	// code from the manpage on getifaddrs
	if (getifaddrs(&ifaddr) == -1) {
		return ret;
	}

	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
			if (ifa->ifa_addr == NULL)
				continue;

			family = ifa->ifa_addr->sa_family;

			/* Display interface name and family (including symbolic
				form of the latter for the common families) */

			trace_debug("%s  address family: %d%s",
					ifa->ifa_name, family,
					(family == AF_PACKET) ? " (AF_PACKET)" :
					(family == AF_INET) ?   " (AF_INET)" :
					(family == AF_INET6) ?  " (AF_INET6)" : "");
			if (family == AF_INET && !strcmp(ifa->ifa_name, ARDUINO_ETH)){
				s = getnameinfo(ifa->ifa_netmask,
								sizeof(struct sockaddr_in),
								ip_addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
				if (s != 0) {
						trace_error("getnameinfo() failed: %s\n", gai_strerror(s));
						//exit(EXIT_FAILURE);
				}
				trace_debug("\taddress: <%s>", ip_addr);
				ret = (const uint8_t*)ip_addr;
			}
	}
	freeifaddrs(ifaddr);
	return ret;
}
Exemplo n.º 16
0
int variantPinModeIRQ(uint8_t pin, uint8_t mode)
{
	/*
	 * Pin2 and pin3 can be individually assigned to a SoC or a Cypress
	 * GPIO.
	 *
	 * Assignment depends on the triggering mode:
	 * - CHANGE: Cypress
	 * - remaining: SoC
	 */

	PinDescription *p = NULL;
	int ret = 0;

	if (pin >= GPIO_TOTAL){
		trace_error("%s: invalid pin%u", __func__, pin);
		return PIN_EINVAL;
	}

	/* Nothing to do if it's not pin2 nor pin3 */
	if (2 != pin && 3 != pin) {
		return 0;
	}

	/* Search for entry */
	p = &g_APinDescription[ardPin2DescIdx[pin]];

	if (CHANGE != mode) {
		/* SoC is second entry: enable alternate */
		p->iAlternate = 1;
		trace_debug("%s: enable SoC GPIO for pin%u",
				__func__, pin);
	} else {
		/* Cypress is first entry: disable alternate */
		p->iAlternate = 0;
		trace_debug("%s: enable Cypress GPIO for pin%u", __func__,
				pin);
	}

	return 0;
}
Exemplo n.º 17
0
static bool _gmac_phy_wait_idle(Gmac* gmac, uint32_t retries)
{
	uint32_t count = 0;
	while ((gmac->GMAC_NSR & GMAC_NSR_IDLE) == 0) {
		if (retries > 0 && count > retries) {
			trace_debug("Timeout reached while waiting for PHY management logic to become idle");
			return false;
		}
		count++;
	}
	return true;
}
Exemplo n.º 18
0
static int find_iio_dev_name(char *iio_dev_name, size_t iio_dev_name_len)
{
	DIR *dir;
	struct dirent *iio_dev_dir;
	char adc_dev_name[32];
	char fs_path[SYSFS_BUF];
	int fd;

	if ((dir = opendir(SYS_BUS_IIO_DEVICES)) == NULL) {
		trace_error("%s Can't open directory %s: %s", __func__,
				SYS_BUS_IIO_DEVICES, strerror(errno));
		return -1;
	}

	/* Go through all devices and read their names.
	 * Stop when ads7955 is found. */
	while ((iio_dev_dir = readdir(dir)) != NULL) {
		snprintf(fs_path, sizeof(fs_path), LINUX_ADC_DEVICE_NAME_FMT,
				iio_dev_dir->d_name);

		if ((fd = open(fs_path, O_RDONLY)) < 0)
			continue;

		if (read(fd, adc_dev_name, strlen(LINUX_ADC_DEVICE_NAME)) > 0) {
			/* Make sure strncmp is happy */
			adc_dev_name[sizeof(LINUX_ADC_DEVICE_NAME)] = '\0';

			if (!strncmp(adc_dev_name, LINUX_ADC_DEVICE_NAME,
					(size_t)strlen(LINUX_ADC_DEVICE_NAME)))
				break;
		}
		else {
			trace_error("%s Can't read %s: %s", __func__, fs_path,
					strerror(errno));
		}
		close(fd);
	}
	closedir(dir);

	if (iio_dev_dir == NULL) {
		trace_error("%s Can't find ADC driver '%s' entry in %s", __func__,
				LINUX_ADC_DEVICE_NAME, SYS_BUS_IIO_DEVICES);
		return -1;
	}

	strncpy(iio_dev_name, iio_dev_dir->d_name, iio_dev_name_len);

	trace_debug("%s Found IIO device called %s", __func__, iio_dev_name);

	return 0;
}
Exemplo n.º 19
0
void gmac_set_tx_desc(Gmac* gmac, uint8_t queue, struct _eth_desc* desc)
{
	if (queue == 0) {
		gmac->GMAC_TBQB = ((uint32_t)desc) & GMAC_TBQB_ADDR_Msk;
	}
#ifdef CONFIG_HAVE_GMAC_QUEUES
	else if (queue <= GMAC_NUM_QUEUES) {
		gmac->GMAC_TBQBAPQ[queue - 1] = ((uint32_t)desc) & GMAC_TBQBAPQ_TXBQBA_Msk;
	}
#endif
	else {
		trace_debug("Invalid queue number %d\r\n", queue);
	}
}
Exemplo n.º 20
0
void gmac_disable_it(Gmac * gmac, uint8_t queue, uint32_t mask)
{
	if (queue == 0) {
		gmac->GMAC_IDR = mask;
	}
#ifdef CONFIG_HAVE_GMAC_QUEUES
	else if (queue <= GMAC_NUM_QUEUES) {
		gmac->GMAC_IDRPQ[queue - 1] = mask;
	}
#endif
	else {
		trace_debug("Invalid queue number %d\r\n", queue);
	}
}
Exemplo n.º 21
0
/**
 * pinModeIRQ
 *
 * pin - arduino pin
 * mode - one of the modes defined for arudino IRQs LOW , CHANGE, RISING, FALLING, HIGH
 */
int pinModeIRQ(uint8_t pin, int8_t mode)
{
	int ret = 0;
	uint32_t gpio = 0;

	ret = variantPinModeIRQ(pin, mode);
	if (ret) {
		trace_error("%s: variantPinMode failed\n", __func__);
		return ret;
	}

	gpio = pin2gpio(pin);
	if (gpio == PIN_EINVAL){
		trace_error("%s: pin %d out of range (gpio%d)", __func__, pin, gpio);
		return gpio;
	}

	ret = muxSelectDigitalPin(pin);
	if (ret) {
		trace_error("%s: can't set mux for pin%d\n", __func__, pin);
		return ret;
	}

	trace_debug("%s: pin=%d, gpio=%d, mode=%d", __func__, pin, gpio, mode);

	// Now set the mode for IRQ granularity
	switch (mode) {
		case NONE:
		case CHANGE:
		case RISING:
		case FALLING:
			ret = sysfsGpioEdgeConfig(gpio, mode);
			break;
		case LOW:
		case HIGH:
			// Implies edge config none
			ret = sysfsGpioEdgeConfig(gpio, NONE);
			if ( ret < 0 )
				break;

			// Set to active high or active low
			ret = sysfsGpioLevelConfig(gpio, mode);
			break;
		default:
			ret = PIN_EINVAL;
			break;
	}

	return ret;
}
Exemplo n.º 22
0
/*
 * Initialize PWMs.
 *  - export PWMs to sysfs
 *  - save persistent handles to enable/duty_cycle/period
 *
 * This is way more coarse-grained than the GPIO version: Arduino code doesn't
 * require too much flexibility.
 */
int sysfsPwmExport(unsigned pwm, int *handle_enable, int *handle_duty,
		int *handle_period)
{
	int fd;
	char export_value[16] = "";

	trace_debug("%s: pwm=%u", __func__, pwm);

	if ((fd = open(LINUX_PWM_EXPORT, O_WRONLY)) < 0) {
		trace_error("%s Can't open handle to %s: %s", __func__,
				LINUX_PWM_EXPORT, strerror(errno));
		return -1;
	}
	lseek(fd, 0, SEEK_SET);

	snprintf(export_value, sizeof(export_value), "%u", pwm);
	write(fd, &export_value, sizeof(export_value));
	/* If PWM channel has been already exported write will fail.
	 * Ignore write return value */
	close(fd);

	/* Open persistent handle to pwm/period */
	if (sysfsPwmOpenHandler(handle_period, LINUX_PWM_PERIOD_FMT, pwm) < 0)
		return -1;

	/* Open persistent handle to pwm/enable */
	if (sysfsPwmOpenHandler(handle_enable, LINUX_PWM_ENABLE_FMT, pwm) < 0)
		return -1;

	/* Open persistent handle to pwm/duty_cycle */
	if (sysfsPwmOpenHandler(handle_duty, LINUX_PWM_DUTY_FMT, pwm) < 0)
		return -1;

	trace_debug("%s: pwm=%u, handle_enable=%d, handle_duty=%d, handle_period=%d",
		    __func__, pwm, *handle_enable, *handle_duty, *handle_period);
	return 0;
}
Exemplo n.º 23
0
int sysfsGpioSetCurrentPinmux(unsigned int gpio, unsigned int mode)
{
	FILE *fp = NULL;
	int ret = 0;
	char value[8] = "";
	char fs_path[SYSFS_BUF] = "";

	trace_debug("%s: gpio%u, mode=%u", __func__, gpio, mode);
	trace_error("%s: gpio%u, mode=%u", __func__, gpio, mode);

	/* Set pinmux mode  */
	snprintf(fs_path, sizeof(fs_path), LINUX_GPIO_CURRENT_PINMUX_FMT, gpio);
	if (NULL == (fp = fopen(fs_path, "rb+"))) {
		trace_error("err set pinmux fs_path=%s\n", fs_path);
		return -1;
	}
	rewind(fp);

	switch(mode) {
	case PIN_MODE_0:
		trace_error("set pinmux mode0 fs_path=%s\n", fs_path);
		strcpy(value, "mode0");
		break;
	case PIN_MODE_1:
		strcpy(value, "mode1");
		break;
	case PIN_MODE_2:
		strcpy(value, "mode2");
		break;
	case PIN_MODE_3:
		strcpy(value, "mode3");
		break;
	case PIN_MODE_4:
		strcpy(value, "mode4");
		break;
	case PIN_MODE_5:
		strcpy(value, "mode5");
		break;
	default:
		trace_error("%s: unknown mode %u", __func__, mode);
		return -1;
		break;
	}

	fwrite(&value, sizeof(char), sizeof(value), fp);
	fclose(fp);

	return ret;
}
Exemplo n.º 24
0
void Servo::write(int val)
{
	// according to Arduino reference lib, if this angle will
	// be bigger than 200, it should be considered as microsenconds

	if (val < MIN_PULSE_WIDTH) {
		// yeah.. user is passing angles

		if (val < 0)
			val = 0;
		else if (val > 180)
			val = 180;

		trace_debug("it is an angle:%d  this->min:%d  this->max:%d\n",
				val, this->min, this->max);
		writeMicroseconds(map(val, MIN_ANGLE, MAX_ANGLE, this->min,
					this->max));
	}
	else {
		trace_debug("it is microseconds:%d\n", val);
		// actually angle on this case it is microsencods
		writeMicroseconds(val);
	}
}
Exemplo n.º 25
0
uint32_t gmac_get_it_status(Gmac* gmac, uint8_t queue)
{
	if (queue == 0) {
		return gmac->GMAC_ISR;
	}
#ifdef CONFIG_HAVE_GMAC_QUEUES
	else if (queue <= GMAC_NUM_QUEUES) {
		return gmac->GMAC_ISRPQ[queue - 1];
	}
#endif
	else {
		trace_debug("Invalid queue number %d\r\n", queue);
		return 0;
	}
}
Exemplo n.º 26
0
struct _eth_desc* gmac_get_tx_desc(Gmac* gmac, uint8_t queue)
{
	if (queue == 0) {
		return (struct _eth_desc*)(gmac->GMAC_TBQB & GMAC_TBQB_ADDR_Msk);
	}
#ifdef CONFIG_HAVE_GMAC_QUEUES
	else if (queue <= GMAC_NUM_QUEUES) {
		return (struct _eth_desc*)(gmac->GMAC_TBQBAPQ[queue - 1] & GMAC_TBQBAPQ_TXBQBA_Msk);
	}
#endif
	else {
		trace_debug("Invalid queue number %d\r\n", queue);
		return NULL;
	}
}
Exemplo n.º 27
0
// mode - one of the modes defined for arudino IRQs CHANGE "both", RISING "rising", FALLING "falling", NONE "none"
int sysfsGpioEdgeConfig(unsigned int gpio, int mode)
{
	FILE *fp = NULL;
	int idx;
	char * edge_state [] = {
		"both",
		"rising",
		"falling",
		"none",
	};
	char fs_path[SYSFS_BUF] = "";

	switch(mode){
		case CHANGE:
			idx = 0;
			break;
		case RISING:
			idx = 1;
			break;
		case FALLING:
			idx = 2;
			break;
		case NONE:
			idx = 3;
			break;
		default:
			return -1;
	}

	trace_debug("%s: gpio%u, edge=%s", __func__, gpio, edge_state[idx]);

	/* Set GPIO direction  */
	snprintf(fs_path, sizeof(fs_path), LINUX_GPIO_EDGE_FMT, gpio);
	if (NULL == (fp = fopen(fs_path, "rb+"))) {
		trace_error("%s err edge fs_path=%s gpio not capable of edge config\n",
				__func__, fs_path);
		return -1;
	}
	rewind(fp);

	fwrite(edge_state[idx], sizeof(char), strlen(edge_state[idx]), fp);
	fclose(fp);

	return 0;
}
Exemplo n.º 28
0
/* echo 'output' > /sys/class/gpio/gpio'gpio'/direction */
int sysfsGpioDirection(unsigned int gpio, int output, int outval)
{
	FILE *fp = NULL;
	int ret = 0;
	int handle = PIN_EINVAL;
	char dir_value[5] = "";
	char fs_path[SYSFS_BUF] = "";

	trace_debug("%s: gpio%u, output=%d, outval=%d", __func__, gpio, output, outval);

	/* Set GPIO direction  */
	snprintf(fs_path, sizeof(fs_path), LINUX_GPIO_DIRECTION_FMT, gpio);
	if (0 == access(fs_path, F_OK)) { /* Some GPIOs are output-only so check for direction attribute */
		if (NULL == (fp = fopen(fs_path, "rb+"))) {
			trace_error("err direction fs_path=%s\n", fs_path);
			return -1;
		}
		rewind(fp);
		if (output) {
			strcpy(dir_value, outval ? "high" : "low");
		} else {
			strcpy(dir_value, "in");
		}
		fwrite(&dir_value, sizeof(char), sizeof(dir_value), fp);
		fclose(fp);
	}

	/*
	 * Workaround for RTC #55197: some GPIO IPs/drivers won't apply a value
	 * before switching direction to output.
	 * Also handles the fall-through case for output-only GPIOs with no direction attribute
	 */
	if (output) {
		handle = gpio2gpiohandle(gpio);
		if (PIN_EINVAL == handle) {
			ret = handle;
			goto end;
		}
		ret = sysfsGpioSet(handle, outval);
	}

end:
	return ret;
}
Exemplo n.º 29
0
int sysfsGpioSetDrive(unsigned int gpio, unsigned int mode)
{
	FILE *fp = NULL;
	int ret = 0;
	char value[8] = "";
	char fs_path[SYSFS_BUF] = "";

	trace_debug("%s: gpio%u, mode=%u", __func__, gpio, mode);

	/* Set GPIO direction  */
	snprintf(fs_path, sizeof(fs_path), LINUX_GPIO_DRIVE_FMT, gpio);
	if (NULL == (fp = fopen(fs_path, "rb+"))) {
		trace_error("err set drive fs_path=%s\n", fs_path);
		return -1;
	}
	rewind(fp);

	switch(mode) {
	case GPIO_DRIVE_PULLUP:
		strcpy(value, "pullup");
		break;
	case GPIO_DRIVE_PULLDOWN:
		strcpy(value, "pulldown");
		break;
	case GPIO_DRIVE_STRONG:
		strcpy(value, "strong");
		break;
	case GPIO_DRIVE_HIZ:
		strcpy(value, "hiz");
		break;
	default:
		trace_error("%s: unknown mode %u", __func__, mode);
		return -1;
		break;
	}

	fwrite(&value, sizeof(char), sizeof(value), fp);
	fclose(fp);

	return ret;
}
Exemplo n.º 30
0
// mode - one of the modes defined for arudino IRQs HIGH "high", LOW "low"
int sysfsGpioLevelConfig(unsigned int gpio, int mode)
{
	FILE *fp = NULL;
	int ret = 0, idx;
	char * level_state [] = {
		"high",
		"low",
	};
	char fs_path[SYSFS_BUF] = "";

	switch(mode){
		case HIGH:
			idx = 0;
			break;
		case LOW:
			idx = 1;
			break;
		default:
			return -1;
	}

	trace_debug("%s: gpio%u, level_state=%s", __func__, gpio, level_state[idx]);

	/* Set GPIO direction  */
	snprintf(fs_path, sizeof(fs_path), LINUX_GPIO_LEVEL_FMT, gpio);
	if (NULL == (fp = fopen(fs_path, "rb+"))) {
		trace_error("err level fs_path=%s gpio not capable of level_state config\n", fs_path);
		return -1;
	}
	rewind(fp);

	fwrite(level_state[idx], sizeof(char), strlen(level_state[idx]), fp);
	fclose(fp);

	return ret;
}