/*! \param freq desired frequency. 40Hz to 1000Hz using internal 25MHz oscillator. */ void PCA9685::setPWMFreq(int freq) { uint8_t prescale_val = (CLOCK_FREQ / 4096 / freq) - 1; bcm2835_i2c_set_baudrate(400000); bcm2835_i2c_setSlaveAddress(addr); pca_sendBuf[0] = MODE1; pca_sendBuf[1] = 0x10; bcm2835_i2c_write (pca_sendBuf, 2); pca_sendBuf[0] = PRE_SCALE; pca_sendBuf[1] = prescale_val; bcm2835_i2c_write (pca_sendBuf, 2); pca_sendBuf[0] = MODE1; pca_sendBuf[1] = 0x80; bcm2835_i2c_write (pca_sendBuf, 2); pca_sendBuf[0] = MODE2; pca_sendBuf[1] = 0x04; bcm2835_i2c_write (pca_sendBuf, 2); //i2c->write_byte(MODE1, 0x10); //sleep //i2c->write_byte(PRE_SCALE, prescale_val); // multiplyer for PWM frequency //i2c->write_byte(MODE1, 0x80); //restart //i2c->write_byte(MODE2, 0x04); //totem pole (default) }
/*! \param led channel to set PWM value for \param on_value 0-4095 value to turn on the pulse \param off_value 0-4095 value to turn off the pulse */ void PCA9685::setPWM(uint8_t led, int on_value, int off_value) { bcm2835_i2c_set_baudrate(400000); bcm2835_i2c_setSlaveAddress(addr); pca_sendBuf[0] = LED0_ON_L + LED_MULTIPLYER * (led - 1); pca_sendBuf[1] = on_value & 0xFF; bcm2835_i2c_write (pca_sendBuf, 2); pca_sendBuf[0] = LED0_ON_H + LED_MULTIPLYER * (led - 1); pca_sendBuf[1] = on_value >> 8; bcm2835_i2c_write (pca_sendBuf, 2); pca_sendBuf[0] = LED0_OFF_L + LED_MULTIPLYER * (led - 1); pca_sendBuf[1] = off_value & 0xFF; //std::cout << (int)pca_sendBuf[1] << std::endl; bcm2835_i2c_write (pca_sendBuf, 2); pca_sendBuf[0] = LED0_OFF_H + LED_MULTIPLYER * (led - 1); pca_sendBuf[1] = off_value >> 8; //std::cout << (int)pca_sendBuf[1] << std::endl; bcm2835_i2c_write (pca_sendBuf, 2); //i2c->write_byte(LED0_ON_L + LED_MULTIPLYER * (led - 1), on_value & 0xFF); //i2c->write_byte(LED0_ON_H + LED_MULTIPLYER * (led - 1), on_value >> 8); //i2c->write_byte(LED0_OFF_L + LED_MULTIPLYER * (led - 1), off_value & 0xFF); //i2c->write_byte(LED0_OFF_H + LED_MULTIPLYER * (led - 1), off_value >> 8); }
uint8_t I2C_Bus::write_register(char reg, const char *buf, const uint32_t len) const { uint8_t status = bcm2835_i2c_write(®, 1); if (status != BCM2835_I2C_REASON_OK) { return status; } return bcm2835_i2c_write(buf, len); }
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 (®, 1); bcm2835_i2c_read_register_rs(®,&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 (®, 1); bcm2835_i2c_read_register_rs(®,&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"); }
void mcp7941x_set_date_time(struct rtc_time *t) { static char cmd[] = {MCP7941X_RTCC_TCR_SECONDS}; char reg[] = {0,0,0,0,0,0,0}; reg[MCP7941X_RTCC_TCR_SECONDS] = DEC2BCD(t->tm_sec & 0x7f); reg[MCP7941X_RTCC_TCR_MINUTES] = DEC2BCD(t->tm_min & 0x7f); reg[MCP7941X_RTCC_TCR_HOURS] = DEC2BCD(t->tm_hour & 0x1f); reg[MCP7941X_RTCC_TCR_DAY] = DEC2BCD(t->tm_wday & 0x07); reg[MCP7941X_RTCC_TCR_DATE] = DEC2BCD(t->tm_mday & 0x3f); reg[MCP7941X_RTCC_TCR_MONTH] = DEC2BCD(t->tm_mon & 0x1f); reg[MCP7941X_RTCC_TCR_YEAR] = DEC2BCD(t->tm_year); reg[MCP7941X_RTCC_TCR_SECONDS] |= MCP7941X_RTCC_BIT_ST; reg[MCP7941X_RTCC_TCR_DAY] |= MCP7941X_RTCC_BIT_VBATEN; char data[8]; data[0] = cmd[1]; data[1] = reg[0]; data[2] = reg[1]; data[3] = reg[2]; data[4] = reg[3]; data[5] = reg[4]; data[6] = reg[5]; data[7] = reg[6]; mcp7941x_setup(); bcm2835_i2c_write(data, sizeof(data)/sizeof(char)); }
int WriteRegister(int REG,int value) { regaddr[0]=REG; regaddr[1]=value; return(bcm2835_i2c_write(regaddr, 2)); }
//! Sets PCA9685 mode to 00 void PCA9685::reset() { bcm2835_init(); bcm2835_i2c_set_baudrate(400000); bcm2835_i2c_setSlaveAddress(addr); pca_sendBuf[0] = MODE1; pca_sendBuf[1] = 0x00; bcm2835_i2c_write (pca_sendBuf, 2); pca_sendBuf[0] = MODE2; pca_sendBuf[1] = 0x04; bcm2835_i2c_write (pca_sendBuf, 2); //i2c->write_byte(MODE1, 0x00); //Normal mode //i2c->write_byte(MODE2, 0x04); //totem pole }
// Read bytesToRead sequentially, starting at addressToRead into the dest byte array void readRegisters(char addressToRead, int bytesToRead, char* dest) { //Set Address to read bcm2835_i2c_write(&addressToRead, 1); //Get reading bcm2835_i2c_read_register_rs(&addressToRead,dest,bytesToRead); }
/** *@brief Writes a byte value to a register address *@param reg Address of sensor register. *@param data Data byte to be written on register. *@return none */ void I2C_WriteByteRegister(unsigned char reg,unsigned char data) { unsigned char wr_buf[2]; wr_buf[0] = reg; wr_buf[1] = data; bcm2835_i2c_write((const char *)wr_buf, 2); }
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; }
/** *@brief Writes a word value (16 bit) to a register address. *@param reg Address of sensor register. *@param data Data word to be written on word size register. *@return none */ void I2C_WriteWordRegister(unsigned char reg, unsigned char* data) { unsigned char wr_buf[3]; wr_buf[0] = reg; wr_buf[1] = data[0]; wr_buf[2] = data[1]; bcm2835_i2c_write((const char *)wr_buf, 3); }
uint8_t MAG3110_WRITE_REGISTER(char reg, char value) { /* care the end of string '\n' here */ char data[2]; data[0] = reg; data[1] = value; bcm2835_i2c_setSlaveAddress(MAG3110_I2C_ADDRESS); bcm2835_i2c_write(data, 2); }
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; }
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; }
/** *@brief Writes a byte value to a register address *@param reg Address of sensor register. *@param data Data byte to be written on register. *@return none */ void I2C_WriteByteRegister(unsigned char address,unsigned char reg,unsigned char data) { bcm2835_i2c_setSlaveAddress(address); unsigned char wr_buf[2]; wr_buf[0] = reg; wr_buf[1] = data; bcm2835_i2c_write((const char *)wr_buf, 2); }
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; } }
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; }
/** *@brief Writes a word value (16 bit) to a register address. *@param reg Address of sensor register. *@param data Data word to be written on word size register. *@return none */ void I2C_WriteWordRegister(unsigned char address,unsigned char reg, unsigned char* data) { bcm2835_i2c_setSlaveAddress(address); unsigned char wr_buf[3]; wr_buf[0] = reg; wr_buf[1] = data[0]; wr_buf[2] = data[1]; bcm2835_i2c_write((const char *)wr_buf, 3); }
static PyObject * PyBCM2835_i2c_write(PyObject *self, PyObject *args) { char *buf; int buf_len; uint32_t len; if (!PyArg_ParseTuple(args,"s#i",&buf, &buf_len,&len)) { return NULL; } uint8_t rtn = bcm2835_i2c_write(buf, len); return Py_BuildValue("i",rtn); }
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; } }
/** *@brief Writes a buffer array to the registers *@param reg Address of sensor register, address autoincrements *@param data Pointer to byte data buffer array *@param length length of buffer array *@return none */ void I2C_WriteByteArray(char reg, char* data, unsigned int length) { char* wr_buf = (char*) malloc(sizeof(char) * length); if (wr_buf==NULL) { printf("Error allocating memory!\n"); //print an error message } wr_buf[0] = reg; for(unsigned int i = 1;i<length;i++) { wr_buf[i] = data[i]; } bcm2835_i2c_write((const char *)wr_buf, length); }
void mcp7941x_get_date_time(struct rtc_time *t) { static char cmd[] = {MCP7941X_RTCC_TCR_SECONDS}; char reg[] = {0,0,0,0,0,0,0}; mcp7941x_setup(); bcm2835_i2c_write(cmd, sizeof(cmd)/sizeof(char)); bcm2835_i2c_read(reg, sizeof(reg)/sizeof(char)); t->tm_sec = BCD2DEC(reg[MCP7941X_RTCC_TCR_SECONDS] & 0x7f); t->tm_min = BCD2DEC(reg[MCP7941X_RTCC_TCR_MINUTES] & 0x7f); t->tm_hour = BCD2DEC(reg[MCP7941X_RTCC_TCR_HOURS] & 0x3f); t->tm_wday = BCD2DEC(reg[MCP7941X_RTCC_TCR_DAY] & 0x07); t->tm_mday = BCD2DEC(reg[MCP7941X_RTCC_TCR_DATE] & 0x3f); t->tm_mon = BCD2DEC(reg[MCP7941X_RTCC_TCR_MONTH] & 0x1f); t->tm_year = BCD2DEC(reg[MCP7941X_RTCC_TCR_YEAR]); }
//uint8_t bcm2835_i2c_write(const char * buf, uint32_t len); /// Call bcm2835_i2c_write with 2 parameters /// \par Refer /// - bi_send_buff /// - buf *(uint32_t **)(buff+1) /// - len *(uint32_t *)(buff+5) /// \par Modify void ope_i2c_write(void) { uint8_t ret; uint8_t *buf; uint32_t len; get_int_code(); buf = *(uint32_t **)(buff+1); get_int_code(); len = *(uint32_t *)(buff+5); ret = bcm2835_i2c_write( bi_send_buff, *((uint32_t *)(buff+5)) ); set_ope_code( OPE_I2C_WRITE ); set_byte_code( ret ); put_reply(); mark_sync(); }
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); }
/** * @ingroup I2C-RTC * * @param slave_address * @return */ uint8_t mcp7941x_start (const uint8_t slave_address) { #if !defined(BARE_METAL) && !defined(__AVR_ARCH__) if (bcm2835_init() != 1) return MCP7941X_ERROR; #endif FUNC_PREFIX(i2c_begin()); if (slave_address <= 0) i2c_mcp7941x_slave_address = MCP7941X_DEFAULT_SLAVE_ADDRESS; else i2c_mcp7941x_slave_address = slave_address; mcp7941x_setup(); if (bcm2835_i2c_write(NULL, 0) == 0) return MCP7941X_OK; return MCP7941X_ERROR; }
/* * 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"); }
/** *@brief Writes a byte value to the I2C bus. This assumes the register pointer is preset. *@param bdata Send single byte to I2C bus *@return none */ void I2C_WriteByte(unsigned char address,char bdata) { bcm2835_i2c_setSlaveAddress(address); char data = bdata; bcm2835_i2c_write(&data, 1); }
inline void ArduiPi_OLED::fastI2Cwrite(char* tbuf, uint32_t len) { bcm2835_i2c_write(tbuf, len); }
/** *@brief Writes a byte value to the I2C bus. This assumes the register pointer is preset. *@param bdata Send single byte to I2C bus *@return none */ void I2C_WriteByte(char bdata) { char data = bdata; bcm2835_i2c_write(&data, 1); }
int MAG3110_WRITE_REGISTER(char reg, char val) { char x = reg; char c = val; bcm2835_i2c_setSlaveAddress(14); bcm2835_i2c_write(&x, 2); }