示例#1
0
static void _readMemory (Aardvark handle, u08 device, u08 addr, u16 length)
{
    int count, i;
    u08 *data_in = (u08 *)malloc(length);

    // Write the address
    aa_i2c_write(handle, device, AA_I2C_NO_STOP, 1, &addr);

    count = aa_i2c_read(handle, device, AA_I2C_NO_FLAGS, length, data_in);
    if (count < 0) {
        printf("error: %s\n", aa_status_string(count));
        return;
    }
    if (count == 0) {
        printf("error: no bytes read\n");
        printf("  are you sure you have the right slave address?\n");
        return;
    }
    else if (count != length) {
        printf("error: read %d bytes (expected %d)\n", count, length);
    }

    // Dump the data to the screen
    printf("\nData read from device:");
    for (i = 0; i < count; ++i) {
        if ((i&0x0f) == 0)      printf("\n%04x:  ", addr+i);
        printf("%02x ", data_in[i] & 0xff);
        if (((i+1)&0x07) == 0)  printf(" ");
    }
    printf("\n");

    // Free the data_in pointer
    free(data_in);
}
示例#2
0
//=========================================================================
// MAIN PROGRAM
//=========================================================================
int main (int argc, char *argv[]) {
    Aardvark handle;
    int   port    = 0;
    int   bitrate = 100;
    int   res     = 0;
    FILE *logfile = 0;
    
    if (argc < 2) {
        printf("usage: aalights PORT\n");
        return 1;
    }

    port = atoi(argv[1]);

    // Open the device
    handle = aa_open(port);
    if (handle <= 0) {
        printf("Unable to open Aardvark device on port %d\n", port);
        printf("Error code = %d\n", handle);
        return 1;
    }

    // Enable logging
    logfile = fopen("log.txt", "at");
    if (logfile != 0) {
        aa_log(handle, 3, fileno(logfile));
    }

    // Ensure that the I2C subsystem is enabled
    aa_configure(handle,  AA_CONFIG_SPI_I2C);
    
    // Enable the I2C bus pullup resistors (2.2k resistors).
    // This command is only effective on v2.0 hardware or greater.
    // The pullup resistors on the v1.02 hardware are enabled by default.
    aa_i2c_pullup(handle, AA_I2C_PULLUP_BOTH);
    
    // Power the board using the Aardvark adapter's power supply.
    // This command is only effective on v2.0 hardware or greater.
    // The power pins on the v1.02 hardware are not enabled by default.
    aa_target_power(handle, AA_TARGET_POWER_BOTH);

    // Set the bitrate
    bitrate = aa_i2c_bitrate(handle, I2C_BITRATE);
    printf("Bitrate set to %d kHz\n", bitrate);

    res = flash_lights(handle);
    if (res < 0)
        printf("error: %s\n", aa_status_string(res));

    // Close the device and exit
    aa_close(handle);

    // Close the logging file
    fclose(logfile);
   
    return 0;
}
示例#3
0
static void blast_bytes (Aardvark handle, u08 slave_addr, char *filename)
{
    FILE *file;
    int trans_num = 0;

    // Open the file
    file = fopen(filename, "rb");
    if (!file) {
        printf("Unable to open file '%s'\n", filename);
        return;
    }
    
    while (!feof(file)) {
        int num_write, count;
        int i;

        // Read from the file
        num_write = fread((void *)data_out, 1, BUFFER_SIZE, file);
        if (!num_write)  break;

        // Write the data to the bus
        count = aa_i2c_write(handle, slave_addr, AA_I2C_NO_FLAGS,
                             (u16)num_write, data_out);
        if (count < 0) {
            printf("error: %s\n", aa_status_string(count));
            goto cleanup;
        } else if (count == 0) {
            printf("error: no bytes written\n");
            printf("  are you sure you have the right slave address?\n");
            goto cleanup;
        } else if (count != num_write) {
            printf("error: only a partial number of bytes written\n");
            printf("  (%d) instead of full (%d)\n", count, num_write);
            goto cleanup;
        }

        printf("*** Transaction #%02d\n", trans_num);
        
        // Dump the data to the screen
        printf("Data written to device:");
        for (i = 0; i < count; ++i) {
            if ((i&0x0f) == 0)      printf("\n%04x:  ", i);
            printf("%02x ", data_out[i] & 0xff);
            if (((i+1)&0x07) == 0)  printf(" ");
        }
        printf("\n\n");
        
        ++trans_num;
        
        // Sleep a tad to make sure slave has time to process this request
        aa_sleep_ms(10);
    }

cleanup:    
    fclose(file);
}
static void dump (Aardvark handle, int timeout_ms)
{
    int trans_num = 0;
    int result;
    u08 addr;
    
    //supress this message because no need. printf("Watching slave I2C data...\n");

    // Wait for data on bus
    result = aa_async_poll(handle, timeout_ms);
    if (result == AA_ASYNC_NO_DATA) {
        //supress this message because no need. printf("No data available.\n");
        return;
    }
    
    printf("\n");

    // Loop until aa_async_poll times out
    for (;;) {
        // Read the I2C message.
        // This function has an internal timeout (see datasheet), though
        // since we have already checked for data using aa_async_poll,
        // the timeout should never be exercised.
        if (result == AA_ASYNC_I2C_READ) {
            // Get data written by master
            int num_bytes = aa_i2c_slave_read(handle, &addr,
                                              BUFFER_SIZE, data_in);
            int i;

            if (num_bytes < 0) {
                printf("error: %s\n", aa_status_string(num_bytes));
                return;
            }

            // Dump the data to the screen
#if 0
            printf("*** Transaction #%02d\n", trans_num);
            printf("Data read from master:");
            for (i = 0; i < num_bytes; ++i) {
                if ((i&0x0f) == 0)      printf("\n%04x:  ", i);
                printf("%02x ", data_in[i] & 0xff);
                if (((i+1)&0x07) == 0)  printf(" ");
            }
			printf("\n\n");
#endif

			//printf("Sending to null modem terminal\n");
			//for(i=2;i<num_bytes;i++) {	//Skip the first byte
			//	WriteByteToSerialPort( data_in[i]&0xFF );
			//}
			if( num_bytes > 2 )
				WriteStringToSerialPort(&data_in[2], num_bytes-2);
			//printf("\n\n");
        }
        
        else if (result == AA_ASYNC_I2C_WRITE) {
            // Get number of bytes written to master
            int num_bytes = aa_i2c_slave_write_stats(handle);

            if (num_bytes < 0) {
                printf("error: %s\n", aa_status_string(num_bytes));
                return;
            }
            
            // Print status information to the screen
            printf("*** Transaction #%02d\n", trans_num);
            printf("Number of bytes written to master: %04d\n", num_bytes);

            printf("\n");
        }
        
        else {
            printf("error: non-I2C asynchronous message is pending\n");
            return;
        }
                
        ++trans_num;

        // Use aa_async_poll to wait for the next transaction
        result = aa_async_poll(handle, INTERVAL_TIMEOUT);
        if (result == AA_ASYNC_NO_DATA) {
            printf("No more data available from I2C master.\n");
            break;
        }
    }
}