示例#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);
}
int 
SPIDevice_Read(LPSKYETEK_DEVICE device,
		unsigned char* buffer,
		unsigned int length,
    unsigned int timeout
    )
{
  LPSPI_INFO info;
	int bytes = 0;
  unsigned char req = 0x00;
  unsigned int i;
  unsigned char bit;
  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 SKYETEK_INVALID_PARAMETER;

  SKYETEK_Sleep(100);

	MUTEX_LOCK(&info->lock);
  if( info->type == AA_FEATURE_I2C )
  {
    bytes = aa_i2c_read(info->spiHandle, 0x007F, 0, len, buffer);
  }
  else
  {
    for( i = 0; i < length; i++ )
    {
      bit = aa_gpio_get(info->spiHandle);
      if( bit & 0x01 )
        bytes += aa_spi_write(info->spiHandle, 1, &req, &buffer[i]);
      else
        SKYETEK_Sleep(10);
    }
  }
  MUTEX_UNLOCK(&info->lock);
  return bytes;
}