//========================================================================= // STATIC FUNCTIONS //========================================================================= static int flash_lights (Aardvark handle) { int res, i; unsigned char data_out[16]; // Configure I/O expander lines as outputs data_out[0] = 0x03; data_out[1] = 0x00; res = aa_i2c_write(handle, 0x38, AA_I2C_NO_FLAGS, 2, data_out); if (res < 0) return res; if (res == 0) { printf("error: slave device 0x38 not found\n"); return 0; } // Turn lights on in sequence i = 0xff; do { i = ((i<<1) & 0xff); data_out[0] = 0x01; data_out[1] = i; res = aa_i2c_write(handle, 0x38, AA_I2C_NO_FLAGS, 2, data_out); if (res < 0) return res; aa_sleep_ms(70); } while (i != 0); // Leave lights on for 100 ms aa_sleep_ms(100); // Turn lights off in sequence i = 0x00; do { i = ((i<<1) | 0x01); data_out[0] = 0x01; data_out[1] = i; res = aa_i2c_write(handle, 0x38, AA_I2C_NO_FLAGS, 2, data_out); if (res < 0) return res; aa_sleep_ms(70); } while (i != 0xff); aa_sleep_ms(100); // Configure I/O expander lines as inputs data_out[0] = 0x03; data_out[1] = 0xff; res = aa_i2c_write(handle, 0x38, AA_I2C_NO_FLAGS, 2, data_out); if (res < 0) return res; return 0; }
int SPIDevice_Write(LPSKYETEK_DEVICE device, unsigned char* buffer, unsigned int length, unsigned int timeout) { LPSPI_INFO info; int bytes = 0; unsigned char resp; unsigned int i; aa_u16 len = (aa_u16)length; if((device == NULL) || (buffer == NULL) || (device->user == NULL) ) return 0; if( length == 0 ) return 0; info = (LPSPI_INFO)device->user; if( info->spiHandle < 1 ) return 0; MUTEX_LOCK(&info->lock); if( info->type == AA_FEATURE_I2C ) { bytes = aa_i2c_write(info->spiHandle, 0x007F, 0, len, buffer); } else { for( i = 0; i < length; i++ ) bytes += aa_spi_write(info->spiHandle, 1, &buffer[i], &resp); } MUTEX_UNLOCK(&info->lock); return bytes; }
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); }
//========================================================================= // FUNCTION //========================================================================= static void _writeMemory (Aardvark handle, u08 device, u08 addr, u16 length, int zero) { u16 i, n; u08 data_out[1+PAGE_SIZE]; // Write to the I2C EEPROM // // The AT24C02 EEPROM has 8 byte pages. Data can written // in pages, to reduce the number of overall I2C transactions // executed through the Aardvark adapter. n = 0; while (n < length) { // Fill the packet with data data_out[0] = addr; // Assemble a page of data i = 1; do { data_out[i++] = zero ? 0 : (u08)n; ++addr; ++n; } while ( n < length && (addr & (PAGE_SIZE-1)) ); // Write the address and data aa_i2c_write(handle, device, AA_I2C_NO_FLAGS, i, data_out); aa_sleep_ms(10); } }
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); }