Example #1
0
int main(int argc, char **argv)
{
  unsigned char buf[6];
  unsigned char i,reg;
  double temp=0,calc=0, skytemp,atemp;
  FILE *flog;
  flog=fopen("mlxlog.csv", "a");
    bcm2835_init();
    bcm2835_i2c_begin();
    bcm2835_i2c_set_baudrate(25000);
    // set address...........................................................................................
    bcm2835_i2c_setSlaveAddress(0x5a);
    printf("\nOk, your device is working!!\n");
    while(1) {
      time_t t = time(NULL);
      struct tm tm = *localtime(&t);
      calc=0;
      reg=7;
      for(i=0;i<AVG;i++){
	bcm2835_i2c_begin();
	bcm2835_i2c_write (&reg, 1);
	bcm2835_i2c_read_register_rs(&reg,&buf[0],3);
	temp = (double) (((buf[1]) << 8) + buf[0]);
	temp = (temp * 0.02)-0.01;
	temp = temp - 273.15;
	calc+=temp;
	sleep(1);
      }
      skytemp=calc/AVG;
      calc=0;
      reg=6;
      for(i=0;i<AVG;i++){
	bcm2835_i2c_begin();
	bcm2835_i2c_write (&reg, 1);
	bcm2835_i2c_read_register_rs(&reg,&buf[0],3);
	temp = (double) (((buf[1]) << 8) + buf[0]);
	temp = (temp * 0.02)-0.01;
	temp = temp - 273.15;
	calc+=temp;
	sleep(1);
      }
      atemp=calc/AVG;
      printf("%02d-%02d %02d:%02d:%02d\n    Tambi=%04.2f C, Tobj=%04.2f C\n", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
      fprintf(flog,"%04d-%02d-%02d %02d:%02d:%02d,%04.2f,%04.02f\n",tm.tm_year+1900, tm.tm_mon +1, tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
      fflush(flog);
      sleep(LOGTIME-(2*AVG));
    }
    printf("[done]\n");
}
Example #2
0
	void I2C_Bus::init() 
	{
		BCM2835::instance();
		
		bcm2835_i2c_begin();
		bus_slaveAddr = 0;
	}
Example #3
0
// initializer for I2C - we only indicate the reset pin and OLED type !
boolean ArduiPi_OLED::init(int8_t RST, uint8_t OLED_TYPE) 
{
  dc = cs = -1; // DC and chip Select do not exist in I2C
  rst = RST==OLED_PIN_DEFAULT ? DEF_I2C_RESET : RST;  // Reset Pin

  // Select OLED parameters
  if (!select_oled(OLED_TYPE))
    return false;

  // Init & Configure Raspberry PI I2C
  if (bcm2835_i2c_begin()==0)
    return false;
    
  bcm2835_i2c_setSlaveAddress(_i2c_addr) ;
    
  // Set clock to 400 KHz
  // does not seem to work, will check this later
  // bcm2835_i2c_set_baudrate(400000);
  bcm2835_i2c_set_baudrate(400000);

  // Setup reset pin direction as output
  if (rst >= 0)
    bcm2835_gpio_fsel(rst, BCM2835_GPIO_FSEL_OUTP);
  
  return ( true);
}
Example #4
0
boolean ssd1306_init()
{
	ssd1306_lcdwidth  = 128;
	ssd1306_lcdheight = 64;
	_i2c_addr = 0x3C;
	cursor_y = cursor_x = 0;
	//textsize = 1;

	// De-Allocate memory for OLED buffer if any
	if (poledbuff)
		free(poledbuff);

	// Allocate memory for OLED buffer
	poledbuff = (uint8_t *) malloc ( (ssd1306_lcdwidth * ssd1306_lcdheight / 8 ));
	if (!poledbuff)
    		return false;

	// Init Raspberry PI GPIO
  	if (!bcm2835_init())
    		return false;

	// default OLED are using internal boost VCC converter
	vcc_type = SSD_Internal_Vcc;
	// Init & Configure Raspberry PI I2C
	if (bcm2835_i2c_begin()==0)
		return false;

	bcm2835_i2c_setSlaveAddress(_i2c_addr) ;

  	// Setup reset pin direction as output
  	bcm2835_gpio_fsel(OLED_I2C_RESET, BCM2835_GPIO_FSEL_OUTP);

	return ( true);
}
Example #5
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() ;
  }
}
Example #6
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() ;
        }
    }
}
Example #7
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;
}
int MAG3110_Init() {
	char v = 0;
	bcm2835_i2c_begin();
	bcm2835_i2c_setClockDivider(2500);
	MAG3110_WRITE_REGISTER(16, 0);
	v = MAG3110_READ_REGISTER(7);
	if (v == 0xc4) {
		MAG3110_WRITE_REGISTER(17, 128);
		MAG3110_Initialized = 1;
	}
	return MAG3110_Initialized;
}
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;
  }
}
Example #10
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;
}
Example #11
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
}
Example #12
0
int mcp7941x_start (char slave_address) {

	if (bcm2835_init() != 1)
		return MCP7941X_ERROR;

	bcm2835_i2c_begin();

	if (slave_address <= 0)
		i2c_mcp7941x_slave_address = MCP7941X_DEFAULT_SLAVE_ADDRESS;
	else
		i2c_mcp7941x_slave_address = slave_address;

	return MCP7941X_OK;
}
Example #13
0
int init_all(I2CVariables *i2c_var) {
    if (!bcm2835_init()) return ERROR_BCM2835_INIT;
    bcm2835_i2c_begin();
    bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_626);         // 400 kHz

    ADXL345_init();
    L3G4200D_init();
    HMC5883L_init();
    BMP085_init();
    PCA9685PW_init(1);

    I2CVariables_init(i2c_var);
    return 0;
}
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;
  }
}
Example #15
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);


}
Example #16
0
/**************************************************************************//*!
* @brief    The IIC Initialization for MMA7660
*
* @param    mod - current used module for conection
******************************************************************************/
uint8_t MAG3110_Init(void)
{
  byte who_i_am = 0;

  bcm2835_i2c_begin();
  bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500); // The default	 
  
  MAG3110_WRITE_REGISTER(REG3110_CTRL_REG1, 0x00);

  who_i_am = MAG3110_READ_REGISTER(REG3110_WHO_AM_I);
	
  if(who_i_am == ID_MAG3110) 
  {
    MAG3110_WRITE_REGISTER(REG3110_CTRL_REG2, 0x80);
    MAG3110_enabled = TRUE;
  }
  
  return MAG3110_enabled;
}  
Example #17
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");		
}
Example #18
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;


}
Example #19
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;
}
Example #20
0
int main(int argc, char **argv)
{
  printf("MMA8452Q Basic Example\n");

  // Set up the interrupt pins, they're set as active high, push-pull
  //pinMode(int1Pin, INPUT);
  //digitalWrite(int1Pin, LOW);
  //pinMode(int2Pin, INPUT);
  //digitalWrite(int2Pin, LOW);

  bcm2835_init();
  bcm2835_i2c_begin();
  bcm2835_i2c_set_baudrate(100000);
  bcm2835_i2c_setSlaveAddress(MMA8452_ADDRESS);

  initMMA8452(); //Test and intialize the MMA8452

while(1){
    loop();
  }
  return 0;
}
Example #21
0
static PyObject *
PyBCM2835_i2c_begin(PyObject *self)
{
	bcm2835_i2c_begin();
	Py_RETURN_NONE;
}
//void bcm2835_i2c_begin(void);
/// Call bcm2835_i2c_begin
/// \par            Refer
/// \par            Modify
void ope_i2c_begin(void)
{
    bcm2835_i2c_begin();
}