Beispiel #1
0
mraa_gpio_context setupISRO( int pinNumber, void (*isrHandler)(void *) ) {

	mraa_gpio_context isro = mraa_gpio_init( pinNumber );
    if ( ! isro ) {
        printf( "  Failed initing isro\n" );
        mraa_result_print( MRAA_ERROR_UNSPECIFIED );
    	return 0;
//    } else {
//        printf( "  Inited isro on pin: %d\n", pinNumber );
    }

    response = mraa_gpio_dir( isro, MRAA_GPIO_IN );
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting isro pin direction\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }
//    printf( "  Setup isro pin direction to IN\n" );
    mraa_result_t result = mraa_gpio_isr( isro, MRAA_GPIO_EDGE_BOTH, isrHandler, isro);

    if ( result != MRAA_SUCCESS ) {
//        printf( "  Setup isro interrupt service routine failed with result: %d\n", result );
        return 0;
    }
//    printf( "  Setup isro interrupt service routine\n" );

    // Init to test pin level
//    lastTime = getTimeCheck();
//    isr1( NULL );				// Check level

 	return isro;
}
Beispiel #2
0
mraa_pwm_context setupPWMO( int pinNumber ) {

    on = 0;

    mraa_pwm_context pwmo = mraa_pwm_init( pinNumber );
    if ( ! pwmo ) {
        printf( "  Failed initing pwmo\n" );
        mraa_result_print( MRAA_ERROR_UNSPECIFIED );
    	return 0;
    }

    response = mraa_pwm_enable( pwmo, 0 );					// Initially off
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting pwmo enable to off during setup\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }
    response = mraa_pwm_config_percent( pwmo, 100, 0.25 );	// Startup settings, 0.1 second, 50% duty cycle
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting pwmo period and duty cycle\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }
    response = mraa_pwm_enable( pwmo, 1 );					// Now enable it
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting pwmo enable to on during setup\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }

//    printf( "  Inited pwmo for pin %d\n", pinNumber );
   	return pwmo;
}
Beispiel #3
0
/** Read multiple words from a 16-bit device register.
 * @param devAddr I2C slave device address or Slave Select pin if SPI
 * @param regAddr First register regAddr to read from
 * @param length Number of words to read
 * @param data Buffer to store read data in
 * @return Number of words read (0 indicates failure)
 */
mraa_result_t I2Cdev::readWords (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data) {
#ifdef I2CDEV_SERIAL_DEBUG
	printf ("readWords: addr 0x%02X regAddr 0x%02X...", devAddr, regAddr);
#endif

	mraa_result_t result;
	const uint8_t dlength = length + length;
	uint8_t *newData = new uint8_t[dlength];

	mutex->lock();
	if ((result = _i2c->address (devAddr)) != MRAA_SUCCESS) {
		printf ("ERROR: ");
		mraa_result_print (result);
		printf (" Exit\n");
		return result;
	}
	if ((result = _i2c->writeByte (regAddr)) != MRAA_SUCCESS) {
		printf ("ERROR: ");
		mraa_result_print (result);
		printf (" Exit\n");
		return result;
	}

	if ((result = _i2c->address (devAddr)) != MRAA_SUCCESS) {
		printf ("ERROR: ");
		mraa_result_print (result);
		printf (" Exit\n");
		return result;
	}
	if (_i2c->read (newData, dlength) != dlength) {
		printf ("ERROR: received different amount of bytes\n");
		return MRAA_ERROR_UNSPECIFIED;
	}

	for (uint8_t i = 0, j = 0; i < length; i++) {
		data[i] = (uint16_t) (newData[j++] << 8);
		data[i] |= (uint16_t) newData[j];
	}
	mutex->unlock();

	delete[] newData;

#ifdef I2CDEV_SERIAL_DEBUG
	printf ("Succeeded\n\r");
#endif

	return MRAA_SUCCESS;
}
Beispiel #4
0
void mkInterface::outputPin( int offOn ) {

    response = gpio->write( offOn );
    if (response != MRAA_SUCCESS) {
        mraa_result_print((mraa_result_t) response);
    }
}
Beispiel #5
0
void outputPin(  mraa_gpio_context gpio, int offOn ) {

	response = mraa_gpio_write( gpio, offOn );
    if (response != MRAA_SUCCESS) {
        mraa_result_print((mraa_result_t) response);
    }
}
Beispiel #6
0
void mkInterface::togglePin() {

    response = gpio->write( on );
    if (response != MRAA_SUCCESS) {
        mraa_result_print((mraa_result_t) response);
    }
    on = ( 0 == on ) ? 1 : 0;
}
Beispiel #7
0
void togglePin( mraa_gpio_context gpio ) {

	response = mraa_gpio_write( gpio, on );
    if (response != MRAA_SUCCESS) {
//        printf( "\n  Failed writing to gpio pin: %p\n", gpio );
        mraa_result_print((mraa_result_t) response);
    }
    on = ( 0 == on ) ? 1 : 0;
}
Beispiel #8
0
void closePWMO( mraa_pwm_context pwmo ) {

    response = mraa_pwm_enable( pwmo, 0 );					// Set to off
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting pwmo enable to off when closing output\n" );
        mraa_result_print((mraa_result_t) response);
    } else {
    	mraa_pwm_close( pwmo );
    }
}
Beispiel #9
0
/**
 *Set the i2c bus and address of where the data will be written to.
 *
 *@param i2c context, pass in the i2c context that represents
 *a master on the i2c bus.
 *@param addr The address of the I2C device
 *
*/
static int set_address(mraa_i2c_context i2c, int addr) {
	mraa_result_t mraa_result = mraa_i2c_address(i2c, addr);

    if (mraa_result != MRAA_SUCCESS)
    {
        DEBUG("set_address(): mraa_i2c_address() failure, error code: %d\n", mraa_result);
        mraa_result_print(mraa_result);
    }

    return mraa_result;
}
Beispiel #10
0
mraa_aio_context setupAIO( int pinNumber ) {

	mraa_aio_context aio = mraa_aio_init( pinNumber );
    if ( ! aio ) {
        printf( "  Failed initing aio\n" );
        mraa_result_print( MRAA_ERROR_UNSPECIFIED );
    	return 0;
//    } else {
//        printf( "  Inited aio: %p\n", aio );
    }
	return aio;
}
Beispiel #11
0
/** Read multiple bytes from an 8-bit device register.
 * @param devAddr I2C slave device address
 * @param regAddr First register regAddr to read from
 * @param length Number of bytes to read
 * @param data Buffer to store read data in
 * @return Number of bytes read (0 indicates failure)
 */
mraa_result_t I2Cdev::readBytes (uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) {
#ifdef I2CDEV_SERIAL_DEBUG
	printf ("readBytes: addr 0x%02X regAddr 0x%02X...", devAddr, regAddr);
#endif

	mraa_result_t result;

	mutex->lock();
	if ((result = _i2c->address (devAddr)) != MRAA_SUCCESS) {
		printf ("ERROR: ");
		mraa_result_print (result);
		printf (" Exit\n");
		return result;
	}
	if ((result = _i2c->writeByte (regAddr)) != MRAA_SUCCESS) {
		printf ("ERROR: ");
		mraa_result_print (result);
		printf (" Exit\n");
		return result;
	}

	if ((result = _i2c->address (devAddr)) != MRAA_SUCCESS) {
		printf ("ERROR: ");
		mraa_result_print (result);
		printf (" Exit\n");
		return result;
	}
	if (_i2c->read (data, length) != length) {
		printf ("ERROR: received different amount of bytes\n");
		return MRAA_ERROR_UNSPECIFIED;
	}
	mutex->unlock();

#ifdef I2CDEV_SERIAL_DEBUG
	printf ("Succeeded\n\r");
#endif

	return MRAA_SUCCESS;
}
Beispiel #12
0
mraa_gpio_context setupGPIO( int pinNumber, int direction ) {

    on = 0;

    mraa_gpio_context gpio = mraa_gpio_init( pinNumber );
    if ( ! gpio ) {
        printf( "  Failed initing gpio\n" );
        mraa_result_print( MRAA_ERROR_UNSPECIFIED );
    	return 0;
//    } else {
//        printf( "  Inited gpio: %p\n", gpio );
    }

    response = mraa_gpio_dir( gpio, direction );
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting gpio pin direction\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }

   	return gpio;
}
int main(int ac, char **av)
{
    int iopin;
    mraa_result_t result;
    mraa_gpio_context gpio;
    int value;

    mraa_init();
    
    if (ac != 3) {
        fprintf(stderr, "Usage: %s pin_number\n"        \
               "\nExample: %s 7 -> Read pin 7\n"
               , av[0], av[0]);
        return -1;
    }

    iopin = atoi(av[1]);

    /* Initialisation of pin */
    gpio = mraa_gpio_init(iopin);
    if (gpio == NULL) {
        fprintf(stderr, "[-] Initialisation of pin %d failed. Is this pin exist on your platform?\n", iopin);
        return -1;
    }

    /* Set GPIO direction */
    result = mraa_gpio_dir(gpio, MRAA_GPIO_IN);
    if (result != MRAA_SUCCESS) {
        mraa_result_print(result);
        return -1;
    }

    value = mraa_gpio_read(gpio);
    if (result != MRAA_SUCCESS)
        mraa_result_print(result);
    printf("%d", value);
    return 0;
}
Beispiel #14
0
bool mkInterface::setupGPIO( int pinNumber ) {

    on = 0;

    iopin = pinNumber;
    gpio = new mraa::Gpio( iopin, true, false );

    fprintf(stdout, "\nHello mraa\n  Version: %s", mraa_get_version());

    mraa_platform_t platform = mraa_get_platform_type();
    fprintf(stdout, "\n  Platform type: %d\n", platform );

    response = gpio->dir(mraa::DIR_OUT);
    if (response != MRAA_SUCCESS) {
        mraa_result_print((mraa_result_t) response);
        return false;
    }
   	return true;
}
Beispiel #15
0
/**
 * Print a textual representation of the mraa_result_t
 *
 * @param result the result to print
 */
void printError(mraa_result_t result)
{
    mraa_result_print(result);
}
/*
bool I2Cdev4Edison::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) {
    //int8_t count = 0;
    uint8_t buf[128];
    //int fd;

    if (length > 127) {
        fprintf(stderr, "Byte write count (%d) > 127\n", length);
        return(FALSE);
    }
    //fd = open("/dev/i2c-0", O_RDWR);
    //if (fd < 0) {
    //    fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
    //    return(FALSE);
    //}
    i2c->address(devAddr);
    //if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
    //    fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
   //     close(fd);
   //     return(FALSE);
   // }
    buf[0] = regAddr;
    memcpy(buf+1,data,length);
     //count = write(fd, buf, length+1);
    mraa::Result result = i2c->write(buf, length + 1);
    if (result != mraa::SUCCESS){
       	mraa::printError(result);
       	return false;
    }
    return true;

    //if (count < 0) {
    //    fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
    //    close(fd);
    //    return(FALSE);
    //} else if (count != length+1) {
    //    fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
    //    close(fd);
    //    return(FALSE);
    //}
    //close(fd);

}
*/
int edison_i2c_write(unsigned char slave_addr,
                     unsigned char reg_addr,
                     unsigned char length,
                     unsigned char const *data)
{
	uint8_t buf[128];
	if (length > 127) {
		fprintf(stderr, "MRAA Byte write count (%d) > 127\n", length);
	    return(-1);
	}
	buf[0] = reg_addr;
	memcpy(buf+1,data,length);
	mraa_result_t mr = mraa_i2c_write(i2c, buf, length + 1);
	if (mr != MRAA_SUCCESS){
		fprintf(stderr, "MRAA Failed to write data in reg address %x on the i2c bus.\n", reg_addr);
		mraa_result_print(mr);
		return -1;
	}
	return 0;

	    //if (count < 0) {
	    //    fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
	    //    close(fd);
	    //    return(FALSE);
	    //} else if (count != length+1) {
	    //    fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
	    //    close(fd);
	    //    return(FALSE);
	    //}
	    //close(fd);

    /*
	unsigned long start, cur;
    if (!i2c.enabled)
        return -1;
    if (!length)
        return 0;

    // Populate struct.
    i2c.state = STATE_WRITING;
    i2c.slave_reg = reg_addr;
    i2c.slave_reg_written = 0;
    i2c.data = (unsigned char*)data;
    i2c.length = length;

    I2CSA = slave_addr;
    CTL1 |= UCTR | UCTXSTT;

    msp430_get_clock_ms(&start);
    while (i2c.state != STATE_WAITING) {
        __bis_SR_register(LPM0_bits + GIE);
        msp430_get_clock_ms(&cur);
        if (cur >= (start + I2C_TIMEOUT_MS)) {
            CTL1 |= UCTXSTP;
            i2c.state = STATE_WAITING;
            msp430_i2c_disable();
            msp430_delay_ms(1);
            CLEAR_SCL();
            CLEAR_SDA();
            msp430_delay_ms(1);
            SET_SCL();
            SET_SDA();
            msp430_i2c_enable();
            return -1;
        }
    }
    return 0;
    */
}
Beispiel #17
0
int main(int argc, char** argv)
{
    mraa_result_t r = MRAA_SUCCESS;
    /* These are Joule's onboard LED's. */
    int default_joule_leds[] = {100, 101, 102, 103};
    int *gpio_pins;
    int num_pins;
    int *input_values, *output_values;

    printf("Provide int arg(s) if you want to toggle gpio pins other than Joule's onboard LED's\n");

    if (argc < 2) {
        gpio_pins = default_joule_leds;
        num_pins = 4;
    } else {
        num_pins = argc - 1;
        gpio_pins = malloc(num_pins * sizeof(int));

        for (int i = 0; i < num_pins; ++i) {
            gpio_pins[i] = strtol(argv[i+1], NULL, 10);
        }
    }

    /* Allocate input and output values arrays. */
    input_values = malloc(num_pins * sizeof(int));
    output_values = malloc(num_pins * sizeof(int));

    mraa_init();
    fprintf(stdout, "MRAA Version: %s\nStarting program...\n", mraa_get_version());

    mraa_gpio_context gpio = mraa_gpio_init_multi(gpio_pins, num_pins);
    if (gpio == NULL) {
        fprintf(stderr, "Error during gpio initialization\n");
        exit(1);
    }

    r = mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
    if (r != MRAA_SUCCESS) {
        mraa_result_print(r);
    }

    signal(SIGINT, sig_handler);

    /* Set input values first. */
    memset(input_values, 0, num_pins * sizeof(int));

    while (running == 0) {
        r = mraa_gpio_write_multi(gpio, input_values);

        sleep(1);

        if (r != MRAA_SUCCESS) {
            mraa_result_print(r);
        } else {
            r = mraa_gpio_read_multi(gpio, output_values);
            if (r != MRAA_SUCCESS) {
                mraa_result_print(r);
            }
        }

        for (int i = 0; i < num_pins; ++i) {
            input_values[i] = (input_values[i] + 1) % 2;
        }
    }

    memset(input_values, 0, num_pins * sizeof(int));
    mraa_gpio_write_multi(gpio, input_values);

    r = mraa_gpio_close(gpio);
    if (r != MRAA_SUCCESS) {
        mraa_result_print(r);
    }

    /* Cleanup. */
    if (argc >= 2) {
       free(gpio_pins);
    }
    free(input_values);
    free(output_values);

    return r;
}
Beispiel #18
0
int
main(int argc, char** argv)
{
    mraa_platform_t platform = mraa_get_platform_type();
    mraa_gpio_context gpio, gpio_in = NULL;
    const char* board_name = mraa_get_platform_name();
    int ledstate = 0;
    mraa_result_t r = MRAA_SUCCESS;

    switch (platform) {
        case MRAA_INTEL_GALILEO_GEN1:
            gpio = mraa_gpio_init_raw(3);
            break;
        case MRAA_INTEL_MINNOWBOARD_MAX:
            // there is no onboard LED that we can flash on the minnowboard max
            // but on the calamari lure pin 21 is an LED. If you don't have the
            // lure put an LED on pin 21
            gpio = mraa_gpio_init(21);
            break;
        case MRAA_INTEL_JOULE_EXPANSION:
            gpio = mraa_gpio_init(101);
            break;
        default:
            gpio = mraa_gpio_init(13);
    }

    fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s (Ctrl+C to exit)\n", mraa_get_version(), board_name);


    if (gpio == NULL) {
        fprintf(stdout, "Could not initilaize gpio\n");
        return 1;
    }

    // on platforms with physical button use gpio_in
    if (platform == MRAA_INTEL_MINNOWBOARD_MAX) {
        gpio_in = mraa_gpio_init(14);
        if (gpio_in != NULL) {
            mraa_gpio_dir(gpio_in, MRAA_GPIO_IN);
            // S1 on minnowboardmax's calamari lure maps to pin 14, SW1 != S1
            fprintf(stdout, "Press and hold S1 to stop, Press SW1 to shutdown!\n");
        }
    }

    mraa_gpio_dir(gpio, MRAA_GPIO_OUT);

    signal(SIGINT, sig_handler);

    while (running == 0) {
        if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) {
            mraa_gpio_close(gpio_in);
            return 0;
        }
        ledstate = !ledstate;
        mraa_gpio_write(gpio, !ledstate);
        sleep(1);
    }

    r = mraa_gpio_close(gpio);
    if (r != MRAA_SUCCESS) {
        mraa_result_print(r);
    }

    return r;
}
Beispiel #19
0
Datei: spi.c Projekt: KurtE/mraa
int
main(int argc, char** argv)
{
    mraa_result_t status = MRAA_SUCCESS;
    mraa_spi_context spi;
    int i, j;

    /* initialize mraa for the platform (not needed most of the times) */
    mraa_init();

    //! [Interesting]
    /* initialize SPI bus */
    spi = mraa_spi_init(SPI_BUS);
    if (spi == NULL) {
        fprintf(stderr, "Failed to initialize SPI\n");
        mraa_deinit();
        return EXIT_FAILURE;
    }

    /* set SPI frequency */
    status = mraa_spi_frequency(spi, SPI_FREQ);
    if (status != MRAA_SUCCESS)
        goto err_exit;

    /* set big endian mode */
    status = mraa_spi_lsbmode(spi, 0);
    if (status != MRAA_SUCCESS) {
        goto err_exit;
    }

    /* MAX7219/21 chip needs the data in word size */
    status = mraa_spi_bit_per_word(spi, 16);
    if (status != MRAA_SUCCESS) {
        fprintf(stdout, "Failed to set SPI Device to 16Bit mode\n");
        goto err_exit;
    }

    /* do not decode bits */
    mraa_spi_write_word(spi, 0x0900);

    /* brightness of LEDs */
    mraa_spi_write_word(spi, 0x0a05);

    /* show all scan lines */
    mraa_spi_write_word(spi, 0x0b07);

    /* set display on */
    mraa_spi_write_word(spi, 0x0c01);

    /* testmode off */
    mraa_spi_write_word(spi, 0x0f00);

    while (flag) {
        /* set display pattern */
        mraa_spi_write_buf_word(spi, pat, 16);

        sleep(2);

        /* set inverted display pattern */
        mraa_spi_write_buf_word(spi, pat_inv, 16);

        sleep(2);

        /* clear the LED's */
        mraa_spi_write_buf_word(spi, pat_clear, 16);

        /* cycle through all LED's */
        for (i = 1; i <= 8; i++) {
            for (j = 0; j < 8; j++) {
                mraa_spi_write_word(spi, (i << 8) + (1 << j));
                sleep(1);
            }
            mraa_spi_write_word(spi, i << 8);
        }
    }

    /* stop spi */
    mraa_spi_stop(spi);

    //! [Interesting]
    /* deinitialize mraa for the platform (not needed most of the times) */
    mraa_deinit();

    return EXIT_SUCCESS;

err_exit:
    mraa_result_print(status);

    /* stop spi */
    mraa_spi_stop(spi);

    /* deinitialize mraa for the platform (not needed most of the times) */
    mraa_deinit();

    return EXIT_FAILURE;
}