void i2c_disp_init (void) { __u16 val; omap_i2c_outw(0x2, I2C_SYSC); udelay(1000); omap_i2c_outw(0x0, I2C_SYSC); val = omap_i2c_inw(I2C_CON); if (val & I2C_CON_EN) { omap_i2c_outw(0, I2C_CON); udelay (50000); } omap_i2c_outw(0, I2C_PSC); val = 0xfff9; omap_i2c_outw(val, I2C_SCLL); omap_i2c_outw(val, I2C_SCLH); omap_i2c_outw(1, I2C_OA); omap_i2c_outw(I2C_CON_EN, I2C_CON); omap_i2c_outw(0x1F, I2C_IE); i2c_flush(); omap_i2c_outw(0xFFFF, I2C_STAT); omap_i2c_outw(0, I2C_CNT); // init disp i2c_write_byte (0x49, 0x9B, 0x80); i2c_write_byte (0x49, 0x9E, 0x80); }
void MPU_9050_init (void) { // Configure Pins for I2C P1SEL0 |= BIT6 | BIT7; // I2C pins __enable_interrupt(); NVIC_ISER0 = 1 << ((INT_EUSCIB0 - 16) & 31); // Enable eUSCIB0 interrupt in NVIC module // Configure USCI_B0 for I2C mode UCB0CTLW0 |= UCSWRST; // put eUSCI_B in reset state UCB0CTLW0 |= UCMODE_3 | UCMST; // I2C master mode, SMCLK UCB0BRW = 0x0018; // baudrate = SMCLK /24 UCB0CTLW0 &=~ UCSWRST; // clear reset register UCB0IE |= UCTXIE0 | UCNACKIE; // transmit and NACK interrupt enable UCB0I2CSA = MPU_dir; // Adress while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent (channel empty) i2c_write_byte(MPU_START,MPU_PWR_MGMT_1); i2c_write_byte(MPU_START,MPU_PWR_MGMT_1); i2c_write_byte(MPU_LPFIL,MPU_CONFIG_GLOBAL); i2c_write_byte(0x00,MPU_CONFIG_G); }
//------------------------------------------------------------------------ static void send_data ( unsigned int data ) { //PUT32(GPSET0,1<<25); //D/C = 1 for data //spi_one_byte(data); i2c_start(); if(i2c_write_byte(PADDR)) { i2c_stop(); hexstring(0xBADBAD00); return; } //not continuing //want D/C a one if(i2c_write_byte(0x40)) { //i2c_stop(); //hexstring(0xBADBAD00); //return; } if(i2c_write_byte(data)) { //i2c_stop(); //hexstring(0xBADBAD00); //return; } i2c_stop(); }
uint8_t I2C_ReadOneByte(uint8_t SlaveAddr, uint8_t RegAddr) { uint8_t result; i2c_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_WRITE); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, RegAddr); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_repeated_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_READ); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_set_rx_mode(I2C0_B); i2c_give_nack(I2C0_B); result = i2c_read_byte(I2C0_B); i2c_wait(I2C0_B); i2c_stop(I2C0_B); result = i2c_read_byte(I2C0_B); pause(40); return result; }
void imu_g_read_data_raw(vector *v) { unsigned char xl,xh,yl,yh,zl,zh; i2c_start(); i2c_write_byte(0xD0); i2c_write_byte(0x1d); i2c_start(); i2c_write_byte(0xD1); xh = i2c_read_byte(); xl = i2c_read_byte(); yh = i2c_read_byte(); yl = i2c_read_byte(); zh = i2c_read_byte(); zl = i2c_read_last_byte(); i2c_stop(); v->x = xh << 8 | xl; v->y = yh << 8 | yl; v->z = zh << 8 | zl; }
/*! * @brief 读取I2C设备指定地址寄存器的数据 * @param I2Cn_e I2C模块(I2C0、I2C1) * @param SlaveID 从机地址(7位地址) * @param reg 从机寄存器地址 * @return 读取的寄存器值 * @since v5.0 * Sample usage: uint8 value = i2c_read_reg(I2C0, 0x1D, 1); */ uint8 i2c_read_reg(I2Cn_e i2cn, uint8 SlaveID, uint8 reg) { //先写入寄存器地址,再读取数据,因此此过程是 I2C 的复合格式,改变数据方向时需要重新启动 uint8 result; ASSERT((SlaveID & 0x80) == 0); //断言,我们要求的7位地址的值仅仅是7bit,不是通信时要求的高7位 //有些手册,给出的7位地址指的是8bit里的高7位 //有些手册,给出的7位地址指的是7bit //请自行确认,可以尝试是否通信正常来确认 i2c_Start(i2cn); //发送启动信号 i2c_write_byte(i2cn, ( SlaveID << 1 ) | MWSR); //发送从机地址和写位 i2c_write_byte(i2cn, reg); //发送从机里的寄存器地址 i2c_RepeatedStart(i2cn); //复合格式,发送重新启动信号 i2c_write_byte(i2cn, ( SlaveID << 1) | MRSW ); //发送从机地址和读位 i2c_PutinRxMode(i2cn); //进入接收模式(不应答,只接收一个字节) result = I2C_D_REG(I2CN[i2cn]); //虚假读取一次,启动接收数据 i2c_Wait(i2cn); //等待接收完成 i2c_Stop(i2cn); //发送停止信号 result = I2C_D_REG(I2CN[i2cn]); //读取数据 Pause(); //必须延时一下,否则出错 return result; }
uint8 hal_dev_mag3110_read_reg(uint8 addr) { uint8 result; i2c_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_WRITE); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_write_byte(I2C_MAG, addr); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_repeated_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_READ); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_set_rx_mode(I2C_MAG); i2c_give_nack(I2C_MAG); result = i2c_read_byte(I2C_MAG); i2c_wait(I2C_MAG); i2c_stop(I2C_MAG); result = i2c_read_byte(I2C_MAG); pause(); return result; }
uint16_t bh1750fvi_sample(const bh1750fvi_t *dev) { uint32_t tmp; uint8_t raw[2]; /* power on the device and send single H-mode measurement command */ DEBUG("[bh1750fvi] sample: triggering a conversion\n"); i2c_acquire(dev->i2c); i2c_write_byte(dev->i2c, dev->addr, OP_POWER_ON, 0); i2c_write_byte(dev->i2c, dev->addr, OP_SINGLE_HRES1, 0); i2c_release(dev->i2c); /* wait for measurement to complete */ xtimer_usleep(DELAY_HMODE); /* read the results */ DEBUG("[bh1750fvi] sample: reading the results\n"); i2c_acquire(dev->i2c); i2c_read_bytes(dev->i2c, dev->addr, raw, 2, 0); i2c_release(dev->i2c); /* and finally we calculate the actual LUX value */ tmp = ((uint32_t)raw[0] << 24) | ((uint32_t)raw[1] << 16); tmp /= RES_DIV; return (uint16_t)(tmp); }
int i2c_read (uint8_t chip_id, uint8_t reg_addr, uint8_t* buffer, uint16_t len) { int status = 0; /* send chip internal register address (pointer register) */ if (status == 0) i2c_send_start (); if (status == 0) status = i2c_write_byte (chip_id << 1 | I2C_WRITE_OPER); if (status == 0) status = i2c_write_byte (reg_addr); if (status == 0) i2c_send_repeated_start (); /* send again chip id before to switch to read mode */ if (status == 0) status = i2c_write_byte ((chip_id << 1) | I2C_READ_OPER); if (status == 0) i2c_switch_to_read_operation(len > 1); /* read specified number of bytes */ if (status == 0) { while (len > 1) { *buffer++ = i2c_read_byte (--len > 1); } } i2c_send_stop (); if (status == 0) *buffer++ = i2c_read_byte (false); return status; }
unsigned char I2C_GPIO_Write_iM205 (unsigned char addr, unsigned char reg, unsigned char val ) { unsigned char state, data; #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0u; #endif APP_TRACE_INFO(("\r\nI2C_GPIO_Write_iM205(0x%0X,0x%0X, 0x%0X)", addr, reg, val)); OS_ENTER_CRITICAL(); i2c_start(); //起始条件,开始数据通信 //发送地址和数据读写方向 data = (addr << 1) | 0; //低位为0,表示写数据 state = i2c_write_byte(data); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); //APP_TRACE_INFO(("\r\n write byte err1!")); return 1; } //写入数据 state = i2c_write_byte( reg ); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); //APP_TRACE_INFO(("\r\n write byte err2!")); return 1; } i2c_restart(); //发送地址和数据读写方向 data = (addr << 1) | 0; //低位为0,表示写数据 state = i2c_write_byte(data); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); //APP_TRACE_INFO(("\r\n write byte err3!")); return 1; } //写入数据 state = i2c_write_byte( val ); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); //APP_TRACE_INFO(("\r\n write byte err4!")); return 1; } i2c_stop(); //终止条件,结束数据通信 OS_EXIT_CRITICAL(); return 0; }
/************************************************************************* ** 函数名称: pcf8536_read(uint8 addr,uint8 *p,uint8 num) ** 功能描述: 读pcf8563 ** 输 入: uint8 addr :高八位为器件地址,低八位为内部寄存器地址 uint8 *p :读出的数据存放地址的起始地址 uint8 num :读出数据的个数 **************************************************************************/ void pcf8536_read(uint8 addr,uint8 *p,uint8 num) { uint8 *pbuf = p; i2c_start(); if(i2c_write_byte(WR_ADDR_CMD)==SLA_W) { i2c_write_byte(addr); } else { syserr=ERR_SLA_W; } i2c_start(); if(i2c_write_byte(RD_ADDR_CMD)==SLA_W) { i2c_write_byte(addr); } else { syserr=ERR_SLA_W; } for(; num > 0; num--) { *pbuf = i2c_read_byte(); pbuf++; } }
//------------------------------------------------------------------------ //static unsigned int i2c_read_byte ( unsigned int *b ) //{ //unsigned int ra; //unsigned int rb; //i2c_delay(); //sda_input(); //rb=0; //for(ra=0;ra<9;ra++) //{ //i2c_delay(); //scl_high(); //i2c_delay(); //rb<<=1; //if(sda_read()) rb|=1; //i2c_delay(); //scl_low(); //i2c_delay(); //} //sda_output(); //i2c_delay(); //ra=rb&1; //*b=rb>>1; //return(ra); //} //------------------------------------------------------------------------ static void send_command ( unsigned int cmd ) { // PUT32(GPCLR0,1<<25); //D/C = 0 for command //spi_one_byte(cmd); i2c_start(); if(i2c_write_byte(PADDR)) { i2c_stop(); hexstring(0xBADBAD00); return; } //not continuing //want D/C a zero if(i2c_write_byte(0x00)) { //i2c_stop(); //hexstring(0xBADBAD00); //return; } if(i2c_write_byte(cmd)) { //i2c_stop(); //hexstring(0xBADBAD00); //return; } i2c_stop(); }
static int i2c_write_bytes(wm_i2c *i2c, int reg, uint8_t *data, int datasize) { int retry = 60; int i; do { if (reg >= 0) { if (!i2c_write_byte(i2c, reg)) return false; } for (i = 0; i < datasize; i++) if (!i2c_write_byte(i2c, data[i])) return false; if (!(i2c->reg->RAW_INTR_STAT.WORDVAL & 0x40 /* TX ABRT */)) break; os_thread_sleep(os_msec_to_ticks(10)); #if I2C_VERBOSE > 1 mc_log_debug("I2C: xmit aborted [%d]: cause = 0x%x.\n", retry, i2c_get_abort_cause(i2c)); #endif i2c_clear_abort(i2c); } while (--retry >= 0); #if I2C_VERBOSE if (retry < 0) mc_log_debug("I2C: xmit aborted!\n"); #endif return (retry >= 0); }
static int ewtsa_system_restart(struct i2c_client *client) { int err; char reg; char smpl , dlpf; err = i2c_write_byte(client, ( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_DISABLE); if (err < 0) { return err; } ///Set SMPL register if (EWTSA_delay <= ( unsigned char)DELAY_THRES_2) { smpl = ( unsigned char)DELAY_INTMIN_THRES; }else{ smpl = ( unsigned char)(EWTSA_delay - ( unsigned char)1); } err = i2c_write_byte(client, ( unsigned char)REG_SMPL, ( unsigned char)smpl); if (err < 0) { return err; } ///Set DLPF register if (EWTSA_delay >= ( unsigned char)DELAY_THRES_6){ dlpf = ( unsigned char)DELAY_DLPF_6; }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_5) { dlpf = ( unsigned char)DELAY_DLPF_5; }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_4){ dlpf = ( unsigned char)DELAY_DLPF_4; }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_3) { dlpf = ( unsigned char)DELAY_DLPF_3; }else{ dlpf = ( unsigned char)DELAY_DLPF_2; } reg = ( unsigned char)(( unsigned char)(EWTSA_range << 3) | dlpf | ( unsigned char)0x80 ) ; err = i2c_write_byte(client, REG_FS_DLPF, reg); if (err < 0) { return err; } if (EWTSA_calib== EWTSA_ON) { printk("EWTSA_set_calibration() start \n"); err = i2c_write_byte(client,( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_ENABLE); if (err < 0) { return err; } mdelay(500); printk("EWTSA_set_calibration() end \n"); } return 0; }
int16_t adxl346_read_x(void) { uint8_t acceleration[2]; int16_t x; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAX0_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[0]); i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAX1_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[1]); x = (acceleration[1] << 8) | acceleration[0]; return x; }
int16_t adxl346_read_y(void) { uint8_t acceleration[2]; int16_t y; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAY0_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[0]); i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAY1_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[1]); y = (acceleration[1] << 8) | acceleration[0]; return y; }
int16_t adxl346_read_z(void) { uint8_t acceleration[2]; int16_t z; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAZ0_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[0]); i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAZ1_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[1]); z = (acceleration[1] << 8) | acceleration[0]; return z; }
int command_read_pages(struct ftdi_context *ftdi, uint32_t address, uint32_t size, uint8_t *buffer) { int res = -EIO; uint32_t remaining = size; int cnt; uint16_t page; if (spi_flash_follow_mode(ftdi, "fast read") < 0) goto failed_read; while (remaining) { uint8_t cmd = 0x9; cnt = (remaining > PAGE_SIZE) ? PAGE_SIZE : remaining; page = address / PAGE_SIZE; draw_spinner(remaining, size); /* Fast Read command */ if (spi_flash_command_short(ftdi, SPI_CMD_FAST_READ, "fast read") < 0) goto failed_read; res = i2c_write_byte(ftdi, 0x08, page >> 8); res += i2c_write_byte(ftdi, 0x08, page & 0xff); res += i2c_write_byte(ftdi, 0x08, 0x00); res += i2c_write_byte(ftdi, 0x08, 0x00); if (res < 0) { fprintf(stderr, "page address set failed\n"); goto failed_read; } /* read page data */ res = i2c_byte_transfer(ftdi, I2C_CMD_ADDR, &cmd, 1, 1); res = i2c_byte_transfer(ftdi, I2C_BLOCK_ADDR, buffer, 0, cnt); if (res < 0) { fprintf(stderr, "page data read failed\n"); goto failed_read; } address += cnt; remaining -= cnt; buffer += cnt; } /* No error so far */ res = size; failed_read: if (spi_flash_follow_mode_exit(ftdi, "fast read") < 0) res = -EIO; return res; }
int nmi_i2c_write(unsigned char adr, unsigned char *b, unsigned long sz) { int i; i2c_begin(); i2c_write_byte((adr << 1)); for(i = 0; i < sz; i++) { i2c_write_byte(b[i]); } i2c_end(); return 1; }
/* Exit follow mode */ static int spi_flash_follow_mode_exit(struct ftdi_context *ftdi, char *desc) { int ret = 0; ret |= i2c_write_byte(ftdi, 0x07, 0x00); ret |= i2c_write_byte(ftdi, 0x06, 0x00); ret = (ret ? -EIO : 0); if (ret < 0) fprintf(stderr, "Flash %s exit follow mode FAILED (%d)\n", desc, ret); return ret; }
void i2c_write_reg(I2Cn_e i2cn, uint8 SlaveID, uint8 reg, uint8 Data) { i2c_Start(i2cn); //发送启动信号 i2c_write_byte(i2cn, ( SlaveID << 1 ) | MWSR); //发送从机地址和写位 i2c_write_byte(i2cn, reg); //发送从机里的寄存器地址 i2c_write_byte(i2cn, Data); //发送需要写入的数据 i2c_Stop(i2cn); Pause(); //延时太短的话,可能写出错 }
/************************************************************************* * 野火嵌入式开发工作室 * * 函数名称:I2C_WriteAddr * 功能说明:写入一个字节数据到I2C设备指定寄存器地址 * 参数说明:I2Cn 模块号(I2C0、I2C1) * SlaveID 7位从机地址 * Addr 从机的寄存器地址 * Data 数据 * 函数返回:无 * 修改时间:2012-1-20 * 备 注: *************************************************************************/ void I2C_WriteAddr(I2Cn i2cn, u8 SlaveID, u8 Addr, u8 Data) { /* send data to slave */ I2C_StartTransmission(i2cn, SlaveID, MWSR); //启动传输 i2c_Wait(i2cn); i2c_write_byte(i2cn, Addr); //写地址 i2c_Wait(i2cn); i2c_write_byte(i2cn, Data); //写数据 i2c_Wait(i2cn); i2c_Stop(i2cn); Pause(); //延时太短的话,可能写出错 }
/* SPI Flash set erase page */ static int spi_flash_set_erase_page(struct ftdi_context *ftdi, int page, char *desc) { int ret = 0; ret |= i2c_write_byte(ftdi, 0x08, page >> 8); ret |= i2c_write_byte(ftdi, 0x08, page & 0xff); ret |= i2c_write_byte(ftdi, 0x08, 0); ret = (ret ? -EIO : 0); if (ret < 0) fprintf(stderr, "Flash %s set page FAILED (%d)\n", desc, ret); return ret; }
/*! * Start I2C Transmision * @param SlaveID is the 7 bit Slave Address * @param Mode sets Read or Write Mode */ void IIC_StartTransmission (unsigned char SlaveID, unsigned char Mode) { if(Mode == MWSR) { /* set transmission mode */ MasterTransmission = MWSR; } else { /* set transmission mode */ MasterTransmission = MRSW; } /* shift ID in right possition */ SlaveID = (unsigned char) ACCEL_I2C_ADDRESS << 1; /* Set R/W bit at end of Slave Address */ SlaveID |= (unsigned char)MasterTransmission; /* send start signal */ i2c_Start(); /* send ID with W/R bit */ i2c_write_byte(SlaveID); }
/************************************************************************* ** 函数名称: pcf8536_write(uint8 addr,uint8 data) ** 功能描述: 向pcf8563指定地址写入一条数据 ** 输 入: uint8 addr :高八位为器件地址,低八位为内部寄存器地址 uint8 data :需要写入的数据 **************************************************************************/ void pcf8536_write(uint8 addr,uint8 data) { i2c_start(); if(i2c_write_byte(WR_ADDR_CMD)==SLA_W) { i2c_write_byte(addr); i2c_write_byte(data); } else { syserr=ERR_SLA_W; } i2c_stop(); }
unsigned char I2C_GPIO_Read(unsigned char addr, unsigned char* buf, unsigned int len) { unsigned int i; unsigned char state, data; #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0u; #endif OS_ENTER_CRITICAL(); i2c_start(); //起始条件,开始数据通信 //发送地址和数据读写方向 data = (addr << 1) | 1; //低位为1,表示读数据 state = i2c_write_byte(data); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); return 1; } //读入数据 for (i=0; i<len; i++) { buf[i] = i2c_read_byte(); } i2c_stop(); //终止条件,结束数据通信 OS_EXIT_CRITICAL(); return 0; }
void ak8975a_read_raw_data(int16_t *val) { start: // Launch the first acquisition i2c_write_byte(AK8975A_ADDRESS, AK8975A_CNTL, 0x01); //nrf_delay_ms(1); // Wait for a data to become available while ((i2c_read_byte(AK8975A_ADDRESS, AK8975A_ST1) & 0x01) == 0); // If there is no overflow if((i2c_read_byte(AK8975A_ADDRESS, AK8975A_ST2) & 0x0C)==0) { int16_t v[3]; // Read the six raw data registers sequentially into data array // WARNING : code valid for little endian only ! i2c_read_bytes(AK8975A_ADDRESS, AK8975A_XOUT_L, 6, (uint8_t *)v); // WARNING, magnetometer axis are not the same as the accel / gyro ones // Thus : x <--> y, and z <--> -z val[0] = v[1]; val[1] = v[0]; val[2] = -v[2]; return; } goto start; }
/* SPI Flash generic command, short version */ static int spi_flash_command_short(struct ftdi_context *ftdi, uint8_t cmd, char *desc) { int ret = 0; ret |= i2c_write_byte(ftdi, 0x05, 0xfe); ret |= i2c_write_byte(ftdi, 0x08, 0x00); ret |= i2c_write_byte(ftdi, 0x05, 0xfd); ret |= i2c_write_byte(ftdi, 0x08, cmd); ret = (ret ? -EIO : 0); if (ret < 0) fprintf(stderr, "Flash CMD %s FAILED (%d)\n", desc, ret); return ret; }
bool i2c_probe (uint8_t chip_id) { i2c_send_start (); int status = i2c_write_byte ((chip_id << 1) | I2C_WRITE_OPER); i2c_send_stop (); return status == 0; }
uint8_t adxl346_is_present(void) { uint8_t is_present; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DEVID_ADDR); i2c_read_byte(ADXL346_ADDRESS, &is_present); return (is_present == ADXL346_DEVID_VALUE); }