예제 #1
0
/**
 * @brief Writes an 8-bit values to an 8-bit registers on an I2C slave starting from registerAddress
 * @param[in] slaveId The device's I2C slave id
 * @param[in] registerAddress Address of the device register to write to
 * @param[in] buffer The buffer with values to write
 * @return 0 is successful, 1 if not
 */
int i2c_write_registers(register uint8_t slaveId, register uint8_t registerAddress, register uint8_t registerCount, register uint8_t *buffer){
  uint8_t i = 0;
  
  /* loop while the bus is still busy */
	i2c_wait_while_busy();	
	
	/* send I2C start signal and set write direction*/
	i2c_send_start();
		
	/* send the slave address and wait for the I2C bus operation to complete */
	i2c_send_byte(I2C_WRITE_ADDRESS(slaveId));
	
	/* send the register address */
	i2c_send_byte(registerAddress);
		
  for(i = 0; i < registerCount; i++) {
    /* send the register value */
    i2c_send_byte(buffer[i]);
  }
	
	/* issue stop signal by clearing master mode. */
	i2c_send_stop();
  
  return 0;
}
예제 #2
0
void i2c_send_packet(unsigned char value, unsigned char address)
{
	i2c_start_condition();
	i2c_send_byte(address);
	i2c_send_byte(value);
	i2c_stop_condition();
}
예제 #3
0
/**
 * @brief Reads an 8-bit register from an I2C slave 
 */
uint8_t i2c_read_register(register uint8_t slaveId, register uint8_t registerAddress){
  /* loop while the bus is still busy */
	i2c_wait_while_busy();
	
	/* send I2C start signal and set write direction, also enables ACK */
	i2c_send_start();
	
	/* send the slave address and wait for the I2C bus operation to complete */
	i2c_send_byte(I2C_WRITE_ADDRESS(slaveId));
	
	/* send the register address */
	i2c_send_byte(registerAddress);
	
	/* signal a repeated start condition */
	i2c_send_repeated_start();

	/* send the read address */
	i2c_send_byte(I2C_READ_ADDRESS(slaveId));
	
	/* switch to receive mode but disable ACK because only one data byte will be read */
	i2c_enter_receive_mode_without_ack();
	
	/* read a dummy byte to drive the clock */
	i2c_read_dummy_byte();
	
	/* stop signal */
	i2c_send_stop();
	
	/* fetch the last received byte */
	register uint8_t result = I2C0->D;
	return result;
}
예제 #4
0
float ds1721_read()
{
    unsigned char hi, lo;
    unsigned char i;
    short tmp = 1;
    float rev;
    float frac[] = { 0.0625, 0.125, 0.25, 0.5 };
    
    i2c_send_start();
    i2c_send_byte(DS1721_ADDR | DS1721_WRITE);
    i2c_send_byte(DS1721_CMD_READ);

    i2c_send_start();           /* restart */
    i2c_send_byte(DS1721_ADDR | DS1721_READ);
    hi = i2c_read_byte(1);
    lo = i2c_read_byte(1);
    // _delay_us(70);              /* WHAT ?! */
    // i2c_send_stop();
    // task_sm.piezo0_count = 5;

    ////////////////
    ////////////////
    
    if (hi & 0x80)
    {                           /* negative */
        tmp = (hi << 8) + lo;
        tmp = ~tmp;
        tmp++;

        hi = tmp >> 8;
        lo = tmp & 0x00ff;

        tmp = -1;
    }
예제 #5
0
int i2c_write_block(const uint8_t devaddr, const uint8_t regaddr, const uint8_t *data, int length)
{
	i2c_start();
	int res;
	res = i2c_send_byte(devaddr);
	if(res) {
		printf("Error sending write address (%d)\n", res);
		i2c_stop();
		return -1;
	}
	res = i2c_send_byte(regaddr);
	if(res) {
		printf("Error sending register address (%d)\n", res);
		i2c_stop();
		return -1;
	}
	res = i2c_send_byte((uint8_t)length);
	if(res) {
		printf("Error sending block length (%d)\n", res);
		i2c_stop();
		return -1;
	}
	for(int i=0;i<length;i++, data++) {
		res = i2c_send_byte(*data);
		if(res) {
			printf("Error sending register data %02x (%d)\n", *data, res);
			i2c_stop();
			return -2;
		}
	}
	i2c_stop();
	return 0;
}
예제 #6
0
int i2c_read_block(const uint8_t devaddr, const uint8_t regaddr, uint8_t *data, const int length)
{
	if(!length)
		return 0;

	i2c_start();
	int res = i2c_send_byte(devaddr);
	if(res) {
		printf("NACK on address %02x (%d)\n", devaddr, res);
		i2c_stop();
		return -1;
	}
	res = i2c_send_byte(regaddr);
	if(res) {
		printf("NACK on register address %02x (%d)\n", regaddr, res);
		i2c_stop();
		return -1;
	}
	i2c_restart();
	res = i2c_send_byte(devaddr | 1);
	if(res) {
		printf("NACK on address %02x (%d)\n", devaddr | 1, res);
		i2c_stop();
		return -2;
	}
	for(int i=0;i<length;i++) {
		*(data++) = i2c_receive_byte((i == length-1)? 0 : 1);
	}
	i2c_stop();
	return 0;
}
예제 #7
0
int i2c_write_word(const uint8_t devaddr, const uint16_t regaddr, const uint16_t data)
{
	i2c_start();
	int res;
	res = i2c_send_byte(devaddr);
	if(res) {
		printf("Error sending write address (%d)\n", res);
		i2c_stop();
		return -1;
	}
	res = i2c_send_byte(regaddr >> 8);
	res += i2c_send_byte(regaddr & 0xFF);
	if(res) {
		printf("Error sending register address (%d)\n", res);
		i2c_stop();
		return -1;
	}
	res = i2c_send_byte(data >> 8);
	res += i2c_send_byte(data & 0xFF);
	if(res) {
		printf("Error sending register data (%d)\n", res);
	}
	i2c_stop();
	return res;
}
예제 #8
0
/* This performs a fast write of one or more consecutive bytes to an
 * I2C device.  Not all devices support consecutive writes of more
 * than one byte; for these devices use efx_i2c_write() instead.
 */
int efx_i2c_fast_write(struct efx_i2c_interface *i2c,
		       u8 device_id, u8 offset,
		       const u8 *data, unsigned int len)
{
	int i;
	int rc;

	EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
	EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
	EFX_WARN_ON_PARANOID(len < 1);

	/* Select device and starting offset */
	i2c_start(i2c);
	rc = i2c_send_byte(i2c, i2c_write_cmd(device_id));
	if (rc)
		goto out;
	rc = i2c_send_byte(i2c, offset);
	if (rc)
		goto out;

	/* Write data to device */
	for (i = 0; i < len; i++) {
		rc = i2c_send_byte(i2c, data[i]);
		if (rc)
			goto out;
	}

 out:
	i2c_stop(i2c);
	i2c_release(i2c);

	return rc;
}
예제 #9
0
int gpio_i2c_readX(int Grp, unsigned char devaddress, unsigned char address)
{
    int rxdata;

    I2C_LOCK(Grp);

    i2c_start_bit(Grp);

    i2c_send_byte(Grp, (unsigned char)(devaddress));
    i2c_receive_ack(Grp);

    i2c_send_byte(Grp, address);
    i2c_receive_ack(Grp);
     
    i2c_start_bit(Grp);

    i2c_send_byte(Grp, (unsigned char)(devaddress) | 1);
    i2c_receive_ack(Grp);

    rxdata = i2c_receive_byte(Grp);

    i2c_stop_bit(Grp);
    
    I2C_UNLOCK(Grp);

    return (unsigned char)rxdata;
}
bool lidar_write_register(const uint8_t reg, const uint8_t length, const uint8_t *data) {
	__disable_irq();
	i2c_start();
	if(!i2c_send_byte((I2C_ADDRESS << 1) | I2C_WRITE)) {
		i2c_stop();
		__enable_irq();
		return false;
	}
	if(!i2c_send_byte(length > 1 ? (reg | CONTINOUS_RW) : reg)) {
		i2c_stop();
		__enable_irq();
		return false;
	}
	for(uint8_t i = 0; i < length; i++) {
		if(!i2c_send_byte(data[i])) {
			i2c_stop();
			__enable_irq();
			return false;
		}
	}
	i2c_stop();
	__enable_irq();

	return true;
}
예제 #11
0
int gpio_i2c_wr_arr(int Grp, unsigned char dev,
    unsigned char rarr[], int rsize,
    unsigned char darr[], int dsize)
{
    int i;

    I2C_LOCK(Grp);

    i2c_start_bit(Grp);

    i2c_send_byte(Grp, dev);
    i2c_receive_ack(Grp);

    for(i = 0; i < rsize; i ++) {
        i2c_send_byte(Grp, rarr[i]);
        i2c_receive_ack(Grp);
    }

    if(dsize > 0) {
	    for(i = 0; i < dsize-1; i ++) {
	        i2c_send_byte(Grp, darr[i]);
	        i2c_receive_ack(Grp);
	    }

	    i2c_send_byte(Grp, darr[i]);
    }

    i2c_stop_bit(Grp);

    I2C_UNLOCK(Grp);
    
    return 0;
}
예제 #12
0
/**
 * @brief Reads multiple 8-bit registers from an I2C slave
 * @param[in] slaveId The slave device ID
 * @param[in] startRegisterAddress The first register address
 * @param[in] registerCount The number of registers to read; Must be greater than or equal to two.
 * @param[out] buffer The buffer to write into
 * @return 0 is successful, 1 if not
 */
static int i2c_read_registers_internal(register uint8_t slaveId, register uint8_t startRegisterAddress, register uint8_t registerCount, uint8_t *const buffer){
  if(registerCount < 2)
    return -1;
	
	/* loop while the bus is still busy */
	i2c_wait_while_busy();
	
	/* send I2C start signal and set write direction, also enables ACK */
	i2c_send_start();
	
	/* send the slave address and wait for the I2C bus operation to complete */
	i2c_send_byte(I2C_WRITE_ADDRESS(slaveId));
	
	/* send the register address */
	i2c_send_byte(startRegisterAddress);
	
	/* signal a repeated start condition */
	i2c_send_repeated_start();

	/* send the read address */
	i2c_send_byte(I2C_READ_ADDRESS(slaveId));
	
	/* switch to receive mode and assume more than one register */
	i2c_enter_receive_mode_with_ack();
	
	/* read a dummy byte to drive the clock */
	i2c_read_dummy_byte();
	
	/* for all remaining bytes, read */
	--registerCount;
	uint8_t index = 0;
	while (--registerCount > 0)
	{
		/* fetch and store value */
		register uint8_t value = I2C0->D;
		buffer[index++] = value;
		
		/* wait for completion */
		i2c_wait();
	}
	
	/* disable ACK and read second-to-last byte */
	i2c_disable_ack();
	
	/* fetch and store value */
	buffer[index++] = I2C0->D;
	
	/* wait for completion */
	i2c_wait();
	
	/* stop signal */
	i2c_send_stop();
	
	/* fetch the last received byte */
	buffer[index++] = I2C0->D; 
  
  return 0;
}
예제 #13
0
void i2c_reg_write(uint8_t regAddr, uint8_t dat0, uint8_t dat1)
{
    i2c_start();
    i2c_send_byte(I2C_INA_ADDR << 1 | I2C_WRITE_CMD);
    i2c_send_byte(regAddr);
    i2c_send_byte(dat0);
    i2c_send_byte(dat1);
    i2c_stop();
}
예제 #14
0
void mpu_write_byte(unsigned char addr,unsigned char dat){
	i2c_start();
	if(i2c_send_byte(SlaveAddress)){ //Address for mpu6050
		send_str("i2c_send_byte(SlaveAddress) success!");
	}  
	i2c_send_byte(addr);  //Address for register
	i2c_send_byte(dat);   //write data to rigister
	i2c_stop();
}
예제 #15
0
void ds1721_init()
{
    i2c_init();

    i2c_send_start();
    i2c_send_byte(DS1721_ADDR | DS1721_WRITE);
    i2c_send_byte(DS1721_CMD_INIT);
    i2c_send_stop();

    _delay_us(10);
}
예제 #16
0
파일: iic.c 프로젝트: Fyiyu/X4_Flyer
void ByteWrite24c512(unsigned char faddr,
										 unsigned char saddr,
										 unsigned char dat)
{
	i2c_start() ;
  i2c_send_byte(0xA0) ;
  i2c_send_byte(faddr) ;
	i2c_send_byte(saddr) ;
  i2c_send_byte(dat) ;
  i2c_stop() ;
}
예제 #17
0
파일: gpio_i2c.c 프로젝트: MindShow/hi3535
void gpio_i2c_write(unsigned char devaddress, unsigned char address, unsigned char data)
{
    i2c_start_bit();
    i2c_send_byte((unsigned char)(devaddress));
    i2c_receive_ack();
    i2c_send_byte(address);
    i2c_receive_ack();
    i2c_send_byte(data); 
   // i2c_receive_ack();//add by hyping for tw2815
    i2c_stop_bit();
}
예제 #18
0
unsigned char mpu_read_byte(unsigned char addr){
	unsigned char dat;
	i2c_start();
	i2c_send_byte(SlaveAddress);  //Address for mpu6050
	i2c_send_byte(addr);  //Address for register
	i2c_start();
	i2c_send_byte(SlaveAddress+1);  //Address for mpu6050
	dat=i2c_recv_byte();
	i2c_ack();
	i2c_stop();
	return dat;
}
예제 #19
0
uint16_t i2c_reg_read(uint8_t regAddr)
{
    uint16_t dat;
    i2c_start();
    i2c_send_byte(I2C_INA_ADDR << 1 | I2C_WRITE_CMD);
    i2c_send_byte(regAddr);
    i2c_repeat_start();                        
    i2c_send_byte(I2C_INA_ADDR << 1 | I2C_READ_CMD);
    dat = i2c_read_byte(1) << 8; 
    dat |= i2c_read_byte(1);
    i2c_stop();
    return dat;
}
예제 #20
0
파일: iic.c 프로젝트: Fyiyu/X4_Flyer
unsigned char i2c_write(unsigned char dev,
                        unsigned char reg,
                        unsigned char len,
                        unsigned char * dat)
{
    unsigned char i ;
    i2c_start() ;
    i2c_send_byte(dev) ;
    i2c_send_byte(reg) ;
    for(i = 0; i < len; i ++)
    {
        i2c_send_byte(dat[i]) ;
    }
    i2c_stop() ;
    return 0 ;
}
예제 #21
0
enum i2c_ack_type i2c_send_bytes(i2c_connection conn, uint8_t *buf,
                                 size_t buf_size) {
  for (size_t i = 0; i < buf_size; i++) {
    if (i2c_send_byte(conn, buf[i]) == I2C_NAK) return I2C_NAK;
  }
  return I2C_ACK;
}
예제 #22
0
/* I2C receiving of bytes - does not send an offset byte */
int efx_i2c_recv_bytes(struct efx_i2c_interface *i2c, u8 device_id,
		       u8 *bytes, unsigned int len)
{
	int i;
	int rc;

	EFX_WARN_ON_PARANOID(getsda(i2c) != 1);
	EFX_WARN_ON_PARANOID(getscl(i2c) != 1);
	EFX_WARN_ON_PARANOID(len < 1);

	/* Select device */
	i2c_start(i2c);

	/* Read data from device */
	rc = i2c_send_byte(i2c, i2c_read_cmd(device_id));
	if (rc)
		goto out;

	for (i = 0; i < (len - 1); i++)
		/* Read and acknowledge all but the last byte */
		bytes[i] = i2c_recv_byte(i2c, 1);
	/* Read last byte with no acknowledgement */
	bytes[i] = i2c_recv_byte(i2c, 0);

 out:
	i2c_stop(i2c);
	i2c_release(i2c);

	return rc;
}
예제 #23
0
int gpio_i2c_readX_KeyPad_Fix(
    int Grp,
    unsigned char Dev,
    void (*Func[2])(void))
{
    int rxdata;

    I2C_LOCK(Grp);

    if(Func[0]) {
        (*Func[0])();
    }

    i2c_start_bit(Grp);

    i2c_send_byte(Grp, (unsigned char)(Dev) | 1);
    i2c_receive_ack(Grp);

    rxdata = i2c_receive_byte(Grp);
    i2c_receive_ack(Grp);

    i2c_stop_bit(Grp);

    if(Func[1]) {
        (*Func[1])();
    }

    I2C_UNLOCK(Grp);

    return (unsigned char)rxdata;
}
예제 #24
0
void firmware() {
    display_init();
    gfx_init_ctxt(&gfx_ctxt, display_init_framebuffer(), 720, 1280, 768);
    gfx_clear_color(&gfx_ctxt, 0xFF000000);
    gfx_con_init(&gfx_con, &gfx_ctxt);
    gfx_con_setcol(&gfx_con, DEFAULT_TEXT_COL, 0, 0);

    while (!sdMount()) {
        error("Failed to init SD card!\n");
        print("Press POWER to power off, any other key to retry\n");
        if (btn_wait() & BTN_POWER)
            i2c_send_byte(I2C_5, 0x3C, MAX77620_REG_ONOFFCNFG1, MAX77620_ONOFFCNFG1_PWR_OFF);
        btn_wait();
    }
    
    if(PMC(APBDEV_PMC_SCRATCH49) != 69 && fopen("/ReiNX.bin", "rb")) {
        fread((void*)PAYLOAD_ADDR, fsize(), 1);
        fclose();
        sdUnmount();
        display_end();
        CLOCK(CLK_RST_CONTROLLER_CLK_OUT_ENB_V) |= 0x400; // Enable AHUB clock.
        CLOCK(CLK_RST_CONTROLLER_CLK_OUT_ENB_Y) |= 0x40;  // Enable APE clock.
        PMC(APBDEV_PMC_SCRATCH49) = 69;
        ((void (*)())PAYLOAD_ADDR)();
    }
    SYSREG(AHB_AHB_SPARE_REG) = (volatile vu32)0xFFFFFF9F;
	PMC(APBDEV_PMC_SCRATCH49) = 0;
    
    print("Welcome to ReiNX %s!\n", VERSION);
    loadFirm();
    drawSplash();
    launch();
}
예제 #25
0
int gpio_i2c_write(unsigned char devaddress, unsigned char address, unsigned char data)
{
    int Grp = 0;
    I2C_LOCK(Grp);
    
    i2c_start_bit(Grp);
    i2c_send_byte(Grp, (unsigned char)(devaddress));
    i2c_receive_ack(Grp);
    i2c_send_byte(Grp, address);
    i2c_receive_ack(Grp);
    i2c_send_byte(Grp, data); 
    i2c_stop_bit(Grp);

    I2C_UNLOCK(Grp);

    return 0 ;
}
예제 #26
0
파일: iic.c 프로젝트: Fyiyu/X4_Flyer
void PageWrite24c512(unsigned char faddr,
										 unsigned char saddr, 
										 unsigned char* dat, 
										 unsigned char num)
{
	unsigned char i;
	
	i2c_start();
	i2c_send_byte(0xA0) ;
  i2c_send_byte(faddr) ;
	i2c_send_byte(saddr) ;
	for(i = 0; i < num; i ++)
  {
    i2c_send_byte(dat[i]) ;
  }
  i2c_stop() ;
}
예제 #27
0
파일: iic.c 프로젝트: Fyiyu/X4_Flyer
unsigned char RandomRead24c512(unsigned char faddr,
											unsigned char saddr)
{
	unsigned char dat;
	
	i2c_start() ;
  i2c_send_byte(0xA0);
  i2c_send_byte(faddr);
	i2c_send_byte(saddr);
  
	i2c_start() ;
	i2c_send_byte(0xA1);
	dat = i2c_recv_byte();
  i2c_stop() ;
	
	return dat;
}
예제 #28
0
파일: i2c_api.c 프로젝트: donatieng/mbed
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
{
    int ack = 0;
    int sadr;
    char *ptr;
    char addr;
    ptr = (char *)data;
    switch ((int)obj->i2c) {
        case I2C_0:
            sadr = TSC_I2C_ADDR;
            addr = address;
            break;
        case I2C_1:
            sadr = AAIC_I2C_ADDR;
            addr = address;
            break;
        case I2C_2:
        case I2C_3:
            sadr = address; //LM75_I2C_ADDR or MMA7660_I2C_ADDR;
            addr = *ptr++;
            break;
    }

//    printf("adr = %x, reg = %x\n",sadr, address);
    i2c_start(obj);

    // Set serial and register address
    i2c_send_byte(obj, sadr);
    ack += i2c_receive_ack(obj);
    i2c_send_byte(obj, addr);
    ack += i2c_receive_ack(obj);

    for (int i = 1; i < length; i++) {
        i2c_send_byte(obj, *ptr++);
        ack += i2c_receive_ack(obj);
    }

    i2c_stop(obj);
    if (ack == 3) {
        return 1;
    } else {
        return 0;
    }

}
예제 #29
0
파일: gpio_i2c.c 프로젝트: MindShow/hi3535
unsigned char gpio_i2c_read(unsigned char devaddress, unsigned char address)
{
    int rxdata;
    
    i2c_start_bit();
    i2c_send_byte((unsigned char)(devaddress));
    i2c_receive_ack();
    i2c_send_byte(address);
    i2c_receive_ack();   
    i2c_start_bit();
    i2c_send_byte((unsigned char)(devaddress) | 1);
    i2c_receive_ack();
    rxdata = i2c_receive_byte();
    //i2c_send_ack();
    i2c_stop_bit();

    return rxdata;
}
예제 #30
0
파일: i2c.c 프로젝트: KevinOConnor/klipper
void
i2c_write(struct i2c_config config, uint8_t write_len, uint8_t *write)
{
    SercomI2cm *si = (SercomI2cm *)config.si;
    i2c_start(si, config.addr);
    while (write_len--)
        i2c_send_byte(si, *write++);
    i2c_stop(si);
}