/** *@brief Read the value of a register that has already been select via the address pointer *@param reg register to reag from *@return value of register */ unsigned int I2C_ReadWordPresetPointer(void) { char val[2] = {0}; bcm2835_i2c_read(val,2); unsigned int data = (val[0] << 8)|val[1]; return data; }
uint16_t SlushBoard::getTempRaw(void) { uint8_t buf[2] = { 0, 0 }; bcm2835_i2c_setSlaveAddress(MAX1164_I2C_ADDRESS); bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_626); (void) bcm2835_i2c_read((char *) buf, (uint32_t) 2); return (uint16_t) ((uint16_t) buf[0] << 8 | (uint16_t) buf[1]); }
uint8_t I2C_Bus::read_register(const char reg, 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_read(buf, len); }
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 Read the value of a register that has already been select via the address pointer *@return Data Value of preset register */ unsigned int I2C_ReadWordPresetPointer(unsigned char address) { bcm2835_i2c_setSlaveAddress(address); char val[2] = {0}; bcm2835_i2c_read(val,2); unsigned int data = (val[0] << 8)|val[1]; return data; }
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; }
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; } }
static PyObject * PyBCM2835_i2c_read(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_read(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; } }
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_read(char* buf, uint32_t len); /// Call bcm2835_i2c_read with 1 parameter /// \par Refer /// - buf *(uint32_t **)(buff+1) /// - len *(uint32_t *)(buff+5) /// \par Modify /// - bi_receive_buff void ope_i2c_read(void) { uint8_t ret; char *buf; uint32_t len; get_int_code(); get_int_code(); buf = *(char **)(buff+1); len = *(uint32_t *)(buff+5); ret = bcm2835_i2c_read( bi_rec_buff, len ); set_ope_code( OPE_I2C_READ ); set_byte_code( ret ); set_int_code( (int)buf ); set_int_code( len ); 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); }
/* * 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"); }
uint8_t I2C_Bus::read(char *buf, const uint32_t len) const { return bcm2835_i2c_read(buf, len); }