Exemplo n.º 1
0
int main(int argc, char **argv) {

    printf("Running ... \n");
    
    // parse the command line
    if (comparse(argc, argv) == EXIT_FAILURE) return showusage (EXIT_FAILURE);

    if (!bcm2835_init())
    {
      printf("bcm2835_init failed. Are you running as root??\n");
      return 1;
    }
      
    // I2C begin if specified    
    if (init == I2C_BEGIN)
    {
      if (!bcm2835_i2c_begin())
      {
        printf("bcm2835_i2c_begin failed. Are you running as root??\n");
	return 1;
      }
    }
	  

    // If len is 0, no need to continue, but do I2C end if specified
    if (len == 0) {
         if (init == I2C_END) bcm2835_i2c_end();
	 printf("... done!\n");
         return EXIT_SUCCESS;
    }

    bcm2835_i2c_setSlaveAddress(slave_address);
    bcm2835_i2c_setClockDivider(clk_div);
    fprintf(stderr, "Clock divider set to: %d\n", clk_div);
    fprintf(stderr, "len set to: %d\n", len);
    fprintf(stderr, "Slave address set to: %d\n", slave_address);   
    
    if (mode == MODE_READ) {
    	for (i=0; i<MAX_LEN; i++) buf[i] = 'n';
    	data = bcm2835_i2c_read(buf, len);
    	printf("Read Result = %d\n", data);   
    	for (i=0; i<MAX_LEN; i++) {
    		if(buf[i] != 'n') printf("Read Buf[%d] = %x\n", i, buf[i]);
	}    
    }
    if (mode == MODE_WRITE) {
    	data = bcm2835_i2c_write(wbuf, len);
    	printf("Write Result = %d\n", data);
    }   

    // This I2C end is done after a transfer if specified
    if (init == I2C_END) bcm2835_i2c_end();   
    bcm2835_close();
    printf("... done!\n");
    return 0;
}
Exemplo n.º 2
0
/** Enable or disable I2C, 
 * @param isEnabled true = enable, false = disable
 */
void I2Cdev::enable(bool isEnabled) {
  if ( set_I2C_pins ){
    if (isEnabled)
      bcm2835_i2c_end();
    else
      bcm2835_i2c_begin() ;
  }
}
Exemplo n.º 3
0
/*
* Enable or disable I2C,
* @param isEnabled true = enable, false = disable
*/
void i2c_enable(int isEnabled) {
    if (I2C_SET_PINS) {
        if (isEnabled) {
            bcm2835_i2c_end();
        } else {
            bcm2835_i2c_begin() ;
        }
    }
}
int readPage(int aI2CDevice, char aPage, unsigned char* aTagBuffer, int* aTagBufferLen)
{
  int ret = BCM2835_I2C_REASON_ERROR_NACK; // Just needs to be not BCM2835_I2C_REASON_OK
  int i;
  struct s_cmdResponse* resp;

  /* Set up command to select a tag */
  cmdBuffer[0] = 2;
  cmdBuffer[1] = SL030_READ_PAGE;
  cmdBuffer[2] = aPage;
  int tries = 0;
  while ((tries++ < 12) && (ret != BCM2835_I2C_REASON_OK))
  {
    bcm2835_i2c_begin();
  
    bcm2835_i2c_setSlaveAddress(SL030_ID);
    ret = bcm2835_i2c_write(cmdBuffer, 3);
#if DEBUG
    printf("write returned %d\n", ret);
#endif
    usleep(12000);
    memset(cmdBuffer, 0, CMD_BUF_LEN);
    ret = bcm2835_i2c_read(cmdBuffer, cmdBufferLen);
    bcm2835_i2c_end();
  }
#if DEBUG
  printf("read returned %d\n", ret);
  for (i = 0; i < cmdBufferLen; i++)
  {
    printf("%02x ", cmdBuffer[i]);
  }
  printf("\n");
#endif
  resp = (struct s_cmdResponse*)cmdBuffer;
#if DEBUG
  printf("Length: %d\n", resp->iLength);
  printf("Status: %d\n", resp->iStatus);
  printf("Status: %u\n", (char)resp->iStatus);
#endif
  if ((resp->iStatus == 0) && (resp->iLength > 2))
  {
    /* We found a tag! */
    /* Copy the ID across */
    /* drop 2 bytes from iLength: one each for command and status */
    *aTagBufferLen = (resp->iLength - 2 > MAX_TAG_ID_LENGTH ? MAX_TAG_ID_LENGTH : resp->iLength-2);
    memcpy(aTagBuffer, resp->iTag, *aTagBufferLen);
    return 1;
  }
  else
  {
    return 0;
  }
}
Exemplo n.º 5
0
int main(int argc, char **argv) {
	int first = 0x03, last = 0x77;
    int flags = 0;
    int version = 0;

	while (1 + flags < argc && argv[1 + flags][0] == '-') {
		switch (argv[1 + flags][1]) {
		case 'V': version = 1; break;
		case 'a':
			first = 0x00;
			last = 0x7F;
			break;
		default:
			fprintf(stderr, "Warning: Unsupported flag \"-%c\"!\n", argv[1 + flags][1]);
			//help(); // TODO
			exit(1);
		}
		flags++;
	}

    if (version) {
		fprintf(stderr, "i2cdetect version %s\n", VERSION);
		exit(0);
	}

	if (bcm2835_init() != 1) {
		fprintf(stderr, "bcm2835_init() failed\n");
		exit(1);
	}

	bcm2835_i2c_begin();

	bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500); // 100kHz

	int address;

	for (address = 0x00; address <= 0x7F; address++) {
		if (address < first || address > last) {
			continue;
		}
		bcm2835_i2c_setSlaveAddress(address);
		if (bcm2835_i2c_write(NULL, 0) == 0) {
			printf("0x%.2X : 0x%.2X : %s\n", address, address << 1, lookup_device(address));
		}
	}

	bcm2835_i2c_end();

	bcm2835_close();

	return 0;
}
Exemplo n.º 6
0
/**
 *@brief Initializes the I2C peripheral
 *@param address Address the I2C peripheral is communicating with.
 *@return none
 */
void I2C_Initialize(unsigned char address)
{
    if (!bcm2835_init())						//Configure I2C pins
    {
        printf("BCM libray error.\n");
    }
    bcm2835_i2c_end();		//Close I2C peripheral to reconfigure it

    bcm2835_i2c_begin();						//Set pins as I2C
    bcm2835_i2c_set_baudrate(baudrate);			//Set I2C baudrate
    bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500);		//100 Khz
    bcm2835_i2c_setSlaveAddress(address);	//Set device address
}
int checkForTag(int aI2CDevice, unsigned char* aTagBuffer, int* aTagBufferLen)
{
  int ret = 0;
  int i;
  struct s_cmdResponse* resp;

  /* Set up command to select a tag */
  cmdBuffer[0] = 1;
  cmdBuffer[1] = SL030_CMD_SELECT;
  bcm2835_i2c_begin();

  bcm2835_i2c_setSlaveAddress(SL030_ID);
  ret = bcm2835_i2c_write(cmdBuffer, 2);
#if DEBUG
  printf("write returned %d\n", ret);
#endif
  usleep(30000);
  memset(cmdBuffer, 0, CMD_BUF_LEN);
  ret = bcm2835_i2c_read(cmdBuffer, cmdBufferLen);
  bcm2835_i2c_end();
#if DEBUG
  printf("read returned %d\n", ret);
  for (i = 0; i < cmdBufferLen; i++)
  {
    printf("%02x ", cmdBuffer[i]);
  }
  printf("\n");
#endif
  resp = (struct s_cmdResponse*)cmdBuffer;
#if DEBUG
  printf("Length: %d\n", resp->iLength);
  printf("Status: %d\n", resp->iStatus);
  printf("Status: %u\n", (char)resp->iStatus);
#endif
  /* We'll get a status of 128 for success on a Pi 1, and 0 for any later Pi models */
  if ( ((resp->iStatus == 128) || (resp->iStatus == 0)) && (resp->iLength > 2) )
  {
    /* We found a tag! */
    /* Copy the ID across */
    /* drop 3 bytes from iLength: one each for command, status and tag type */
    *aTagBufferLen = (resp->iLength - 3 > MAX_TAG_ID_LENGTH ? MAX_TAG_ID_LENGTH : resp->iLength-3);
    memcpy(aTagBuffer, resp->iTag, *aTagBufferLen);
    return 1;
  }
  else
  {
    return 0;
  }
}
Exemplo n.º 8
0
IMU::IMU(void)
{	 

	char buf[1];
	bcm2835_init();
	bcm2835_i2c_begin();
	bcm2835_i2c_setSlaveAddress(MPU6050_ADDRESS);
	WriteRegister(SMPRT_DIV,0x07);	// Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
	WriteRegister(CONFIG,0x00); // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
	WriteRegister(GYRO_CONFIG,0x00); //250dpi
	WriteRegister(ACCEL_CONFIG,0x00); //2g resolution
	WriteRegister(PWR_MGMT_1,0x00); //sleep mode disabled

	
	regaddr[0]=WHO_AM_I;
	bcm2835_i2c_write(regaddr, 1);
	bcm2835_i2c_read(buf, 1);
	if(buf[0]==0x88)
	{
		printf("sensor config was successful WHO_AM_I: %x\n",buf[0]);
	}
	else
	{
		printf("sensor config was unsuccessful, %x\n",buf[0]);
	}
	
	bcm2835_i2c_end();
	///////////SETUP VARIABLES

	ReadGyr();
	ReadAccel();
	#ifdef RESTRICT_PITCH // Eq. 25 and 26
	  KFData.roll  = atan2(AData.y, AData.z) * RAD_TO_DEG;
	  KFData.pitch = atan(-AData.x / sqrt(AData.y * AData.y + AData.z * AData.z)) * RAD_TO_DEG;
	#else // Eq. 28 and 29
	  KFData.roll  = atan(AData.y / sqrt(AData.x * AData.x + AData.z * AData.z)) * RAD_TO_DEG;
	  KFData.pitch = atan2(-AData.x, AData.z) * RAD_TO_DEG;
	#endif

	kalmanX.setAngle(KFData.roll); // Set starting angle
  	kalmanY.setAngle(KFData.pitch);


}
Exemplo n.º 9
0
void ArduiPi_OLED::close(void) 
{
  // De-Allocate memory for OLED buffer if any
  if (poledbuff)
    free(poledbuff);
    
  poledbuff = NULL;

  // Release Raspberry SPI
  if ( isSPI() )
    bcm2835_spi_end();

    // Release Raspberry I2C
  if ( isI2C() )
    bcm2835_i2c_end();

  // Release Raspberry I/O control
  bcm2835_close();
}
Exemplo n.º 10
0
/*
*	Function: i2c_test
* Description: be used to test I2C related functions by using the PCF8574 module
*/
void i2c_test(void)
{
	uint8_t i2cWBuf[10] = {0x00};
	uint8_t i2cRBuf[10] = {0X00};
	printf("--------------->Test I2C With Pcf8574<--------------\n");
       i2cWBuf[0] = 0x40;
	bcm2835_i2c_begin();
	bcm2835_i2c_setSlaveAddress(PCF8574_ADDR); //set the slave address
       bcm2835_i2c_set_baudrate(400000);  //set the speed of the SDA 400kb/s
	bcm2835_i2c_write(i2cWBuf,1); 
	
	bcm2835_i2c_read(i2cRBuf, 1);
	if(i2cWBuf[0] == i2cRBuf[0])
		printf("I2C interface work well !...\n");
	else
		printf("I2C interface work bad!...\n");
	
	bcm2835_i2c_end();
	printf("==============Test Over Of I2C=================\n");		
}
Exemplo n.º 11
0
int ReadRegisterPair(int REG_H)
{
	char buf[1];
	int ret;
	int value = 0;
	

	bcm2835_i2c_begin();
	bcm2835_i2c_setSlaveAddress(MPU6050_ADDRESS);

	regaddr[0]=REG_H;
	ret = BCM2835_I2C_REASON_ERROR_DATA;
	while(ret != BCM2835_I2C_REASON_OK)
	{
		//This is the basic operation to read an register
		//regaddr[0] is the register address
		//buf[0] is the value
		bcm2835_i2c_write(regaddr, 1);
		ret = bcm2835_i2c_read(buf, 1);
		//printf("%d\n",ret);
	}
	value = buf[0]<<8;
	regaddr[0]=(REG_H+1);
	
    	ret = BCM2835_I2C_REASON_ERROR_DATA;
    	while(ret != BCM2835_I2C_REASON_OK)
	{
		bcm2835_i2c_write(regaddr, 1);
		ret = bcm2835_i2c_read(buf, 1);
	}
	value += buf[0];
	if (value & 1<<15)
   	{
        	value -= 1<<16;
   	}
	bcm2835_i2c_end();
	//printf("%d ",value);
	return value;


}
Exemplo n.º 12
0
int main(int argc, char **argv)
{


      if (!bcm2835_init())
	return 1;
	char temp[1];				//temporary values
	int ret;
	int ad[2];

	bcm2835_i2c_begin();
	bcm2835_i2c_setSlaveAddress(0x29);      // addr pin attached to ground
	bcm2835_i2c_set_baudrate(1000);         // Default

	temp[0] = 0xa0;				//select the control register
	bcm2835_i2c_write(temp,1);
	temp[0] = 0x03;				//Power up the device
   	bcm2835_i2c_write(temp,1);
	bcm2835_delay(500);

	bcm2835_i2c_read(temp,1);
	printf("%x - if 33 the device is turned on\n",temp[0]);

	temp[0] = 0xac;				//Channel 0 lower byte
    	bcm2835_i2c_write(temp,1);		
	bcm2835_i2c_read(temp,1);

	ad[1]= (int)temp[0];

        temp[0] = 0xad;				//channel 0 upper byte
        bcm2835_i2c_write(temp,1);
        bcm2835_i2c_read(temp,1); 

	ad[0] = (int)temp[0];
	printf("ad value:%d\n",ad[0]*256+ad[1]);

	bcm2835_i2c_end();
	bcm2835_close();
	
	return 0;
}
Exemplo n.º 13
0
void mcp7941x_end (void) {
	bcm2835_i2c_end();
	bcm2835_close();
}
Exemplo n.º 14
0
static PyObject *
PyBCM2835_i2c_end(PyObject *self)
{
	bcm2835_i2c_end();
	Py_RETURN_NONE;
}
Exemplo n.º 15
0
int end_all(I2CVariables *i2c_var) {
    bcm2835_i2c_end();
    return I2CVariables_end(i2c_var);
}
//void bcm2835_i2c_end(void);
/// Call bcm2835_i2c_end
/// \par            Refer
/// \par            Modify
void ope_i2c_end(void)
{
    bcm2835_i2c_end();
}
Exemplo n.º 17
0
/**
*@brief Closes the I2C peripheral
*@param none
*@return none
*/
void I2C_Close(void)
{
    bcm2835_i2c_end();
}