コード例 #1
0
ファイル: i2c.c プロジェクト: 0utsider89/libopencm3
void write_i2c(uint32_t i2c, uint8_t i2c_addr, uint8_t reg, uint8_t size,
	       uint8_t *data)
{
	int wait;
	int i;
	while (i2c_busy(i2c) == 1);
	while (i2c_is_start(i2c) == 1);
	/*Setting transfer properties*/
	i2c_set_bytes_to_transfer(i2c, size + 1);
	i2c_set_7bit_address(i2c, (i2c_addr & 0x7F));
	i2c_set_write_transfer_dir(i2c);
	i2c_enable_autoend(i2c);
	/*start transfer*/
	i2c_send_start(i2c);

	wait = true;
	while (wait) {
		if (i2c_transmit_int_status(i2c)) {
			wait = false;
		}
		while (i2c_nack(i2c));
	}

	i2c_send_data(i2c, reg);
	for (i = 0; i < size; i++) {
		wait = true;
		while (wait) {
			if (i2c_transmit_int_status(i2c)) {
				wait = false;
			}
			while (i2c_nack(i2c));
		}
		i2c_send_data(i2c, data[i]);
	}
}
コード例 #2
0
void stts75_write_config(uint32_t i2c, uint8_t sensor)
{
	uint32_t reg32 __attribute__((unused));

	/* Send START condition. */
	i2c_send_start(i2c);

	/* Waiting for START is send and switched to master mode. */
	while (!((I2C_SR1(i2c) & I2C_SR1_SB)
		& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

	/* Send destination address. */
	i2c_send_7bit_address(i2c, sensor, I2C_WRITE);

	/* Waiting for address is transferred. */
	while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));

	/* Cleaning ADDR condition sequence. */
	reg32 = I2C_SR2(i2c);

	/* Sending the data. */
	i2c_send_data(i2c, 0x1); /* stts75 config register */
	while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); /* Await ByteTransferedFlag. */
	/* Polarity reverse - LED glows if temp is below Tos/Thyst. */
	i2c_send_data(i2c, 0x4);
	while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE)));

	/* Send STOP condition. */
	i2c_send_stop(i2c);
}
コード例 #3
0
ファイル: i2c.c プロジェクト: DavidJohan/Assemble-and-Animate
void i2c_write_byte(uint8_t dev, uint8_t addr, uint8_t data){
	if(i2c_send_start()!=0) return;
	if(i2c_send_addr(dev,TW_WRITE)!=0) return;
	if(i2c_send_data(addr)!=0) return ;
	if(i2c_send_data(data)!=0) return;
	i2c_send_stop();
}
コード例 #4
0
ファイル: i2c.c プロジェクト: franzbischoff/stm32f4inicial
static int i2c_write(uint8_t reg, uint8_t val)
{
    while ((I2C_SR2(I2C_PORT) & I2C_SR2_BUSY)) {
    }
    gpio_set(GPIOG, GPIO13);
    i2c_send_start(I2C_PORT);

    /* Wait for master mode selected */
    while (!((I2C_SR1(I2C_PORT) & I2C_SR1_SB)
           & (I2C_SR2(I2C_PORT) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
    gpio_set(GPIOG, GPIO14);
    i2c_send_7bit_address(I2C_PORT, SLAVE_ADDRESS, I2C_WRITE);

    /* Waiting for address is transferred. */
    while (!(I2C_SR1(I2C_PORT) & I2C_SR1_ADDR));

    /* Cleaning ADDR condition sequence. */
    uint32_t reg32 = I2C_SR2(I2C_PORT);
    (void) reg32; /* unused */

    /* Common above here */

    /* Sending the data. */
    i2c_send_data(I2C_PORT, reg);
    while (!(I2C_SR1(I2C_PORT) & (I2C_SR1_BTF)));
    i2c_send_data(I2C_PORT, val);
    while (!(I2C_SR1(I2C_PORT) & (I2C_SR1_BTF | I2C_SR1_TxE)));

    /* Send STOP condition. */
    i2c_send_stop(I2C_PORT);
    return 0;
}
コード例 #5
0
void stts75_write_temp_os(uint32_t i2c, uint8_t sensor, uint16_t temp_os)
{
	uint32_t reg32 __attribute__((unused));

	/* Send START condition. */
	i2c_send_start(i2c);

	/* Waiting for START is send and switched to master mode. */
	while (!((I2C_SR1(i2c) & I2C_SR1_SB)
		& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

	/* Send destination address. */
	i2c_send_7bit_address(i2c, sensor, I2C_WRITE);

	/* Waiting for address is transferred. */
	while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));

	/* Cleaning ADDR condition sequence. */
	reg32 = I2C_SR2(i2c);

	/* Sending the data. */
	i2c_send_data(i2c, 0x3); /* OvertemperatureShutdown register */
	while (!(I2C_SR1(i2c) & I2C_SR1_BTF));
	i2c_send_data(i2c, (uint8_t)(temp_os >> 8)); /* MSB */
	while (!(I2C_SR1(i2c) & I2C_SR1_BTF));
	i2c_send_data(i2c, (uint8_t)(temp_os & 0xff00)); /* LSB */
	/* After the last byte we have to wait for TxE too. */
	while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE)));

	/* Send STOP condition. */
	i2c_send_stop(i2c);
}
コード例 #6
0
void stts75_write_temp_hyst(uint32_t i2c, uint8_t sensor, uint16_t temp_hyst)
{
	uint32_t reg32 __attribute__((unused));

	/* Send START condition. */
	i2c_send_start(i2c);

	/* Waiting for START is send and therefore switched to master mode. */
	while (!((I2C_SR1(i2c) & I2C_SR1_SB)
		& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

	/* Say to what address we want to talk to. */
	i2c_send_7bit_address(i2c, sensor, I2C_WRITE);

	/* Waiting for address is transferred. */
	while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));

	/* Cleaning ADDR condition sequence. */
	reg32 = I2C_SR2(i2c);

	/* Sending the data. */
	i2c_send_data(i2c, 0x2); /* TemperatureHysteresis register */
	while (!(I2C_SR1(i2c) & I2C_SR1_BTF));
	i2c_send_data(i2c, (uint8_t)(temp_hyst >> 8)); /* MSB */
	while (!(I2C_SR1(i2c) & I2C_SR1_BTF));
	i2c_send_data(i2c, (uint8_t)(temp_hyst & 0xff00)); /* LSB */
	/* After the last byte we have to wait for TxE too. */
	while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE)));

	/* Send STOP condition. */
	i2c_send_stop(i2c);
}
コード例 #7
0
ファイル: eeprom.c プロジェクト: HackLinux/eeprom_driver
/* Function to write a byte at a specific address */
bool eeprom_write_byte(uint16_t address, uint8_t data)
{
	bool success = true;

	/* send START and wait for completion */
	i2c_send_start(I2C1);
	while ((I2C_SR1(I2C1) & I2C_SR1_SB) == 0);

	/* send device address, r/w request and wait for completion */
	i2c_send_7bit_address(I2C1, ADDRESS_BYTE, I2C_WRITE);
	while ((I2C_SR1(I2C1) & I2C_SR1_ADDR) == 0);

	/* check SR2 and go on if OK */
	if ((I2C_SR2(I2C1) & I2C_SR2_MSL)		/* master mode */
	&&	(I2C_SR2(I2C1) & I2C_SR2_BUSY)) {	/* communication ongoing  */

		/* send memory address MSB */
		i2c_send_data(I2C1, ((uint8_t)(address >> 8)));
		while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

		/* send memory address LSB */
		i2c_send_data(I2C1, ((uint8_t)address));
		while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

		/* send data byte */
		i2c_send_data(I2C1, data);
		while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

		/* send stop */
		i2c_send_stop(I2C1);

		/* ATTENTION: consider to wait for a while */
	} else {
コード例 #8
0
ファイル: i2c_arch.c プロジェクト: 1bitsquared/paparazzi
static inline void stmi2c_clear_pending_interrupts(uint32_t i2c)
{
  uint16_t SR1 = I2C_SR1(i2c);

  // Certainly do not wait for buffer interrupts:
  // -------------------------------------------
  i2c_disable_interrupt(i2c, I2C_CR2_ITBUFEN);			// Disable TXE, RXNE

  // Error interrupts are handled separately:
  // ---------------------------------------

  // Clear Event interrupt conditions:
  // --------------------------------

  // Start Condition Was Generated
  if (BIT_X_IS_SET_IN_REG( I2C_SR1_SB, SR1 ) )
  {
    // SB: cleared by software when reading SR1 and writing to DR
    i2c_send_data(i2c, 0x00);
  }
  // Address Was Sent
  if (BIT_X_IS_SET_IN_REG(I2C_SR1_ADDR, SR1) )
  {
    // ADDR: Cleared by software when reading SR1 and then SR2
    uint16_t SR2 __attribute__ ((unused)) = I2C_SR2(i2c);
  }
  // Byte Transfer Finished
  if (BIT_X_IS_SET_IN_REG(I2C_SR1_BTF, SR1) )
  {
    // SB: cleared by software when reading SR1 and reading/writing to DR
    uint8_t dummy __attribute__ ((unused)) = i2c_get_data(i2c);
    i2c_send_data(i2c, 0x00);
  }

}
コード例 #9
0
int tda18219_write_reg(uint8_t reg, uint8_t value)
{
	uint32_t __attribute__((unused)) reg32;

	/* Send START condition. */
	i2c_send_start(I2C1);

	/* Waiting for START is send and switched to master mode. */
	while (!((I2C_SR1(I2C1) & I2C_SR1_SB)
		& (I2C_SR2(I2C1) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

	/* Say to what address we want to talk to. */
	/* Yes, WRITE is correct - for selecting register in STTS75. */
	i2c_send_7bit_address(I2C1, TDA18219_I2C_ADDR, I2C_WRITE);

	/* Waiting for address is transferred. */
	while (!(I2C_SR1(I2C1) & I2C_SR1_ADDR));

	/* Cleaning ADDR condition sequence. */
	reg32 = I2C_SR2(I2C1);

	i2c_send_data(I2C1, reg);
	while (!(I2C_SR1(I2C1) & I2C_SR1_TxE));

	i2c_send_data(I2C1, value);
	while (!(I2C_SR1(I2C1) & (I2C_SR1_BTF | I2C_SR1_TxE)));

	i2c_send_stop(I2C1);

	return 0;
}
コード例 #10
0
ファイル: i2c.c プロジェクト: DavidJohan/Assemble-and-Animate
void i2c_write_word(uint8_t dev, uint8_t addr,uint16_t data){
	if(i2c_send_start()!=0) return;
	if(i2c_send_addr(dev,TW_WRITE)!=0) return;
	if(i2c_send_data(addr)!=0) return;
	if(i2c_send_data(data & 0xff)!=0) return;
	if(i2c_send_data(((data & 0xff00)>>8))!=0) return;
	i2c_send_stop();
}
コード例 #11
0
ファイル: issi.c プロジェクト: Kpat1/tmk_keyboard
void issi_send_data(const uint8_t* data, uint8_t len, uint8_t page) {
    cli();
    uint8_t pageSelect[] = { DEVICE_ADDRESS_WRITE, ADDRESS_COMMAND_REGISTER, page };

    i2c_send_data(pageSelect, 3);
    i2c_send_data(data, len);
    sei();
}
コード例 #12
0
ファイル: issi.c プロジェクト: Kpat1/tmk_keyboard
void issi_zero_pages(uint8_t startPage, uint8_t numPages, uint8_t startRegister, uint8_t endRegister) {
    uint8_t pageSelect[] = { DEVICE_ADDRESS_WRITE, ADDRESS_COMMAND_REGISTER, 0 };

    uint8_t fullPage[ 0xB4 + 2 ] = { 0 };
    fullPage[0] = DEVICE_ADDRESS_WRITE;
    fullPage[1] = startRegister;

    for(uint8_t page = startPage; page < startPage + numPages; page++) {
        pageSelect[2] = page;
        i2c_send_data(pageSelect, sizeof(pageSelect));
        i2c_send_data(fullPage, endRegister - startRegister + 2);
    }
}
コード例 #13
0
ファイル: i2c.c プロジェクト: lxmlx/pcduino-test
int i2c_do_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
{
	u32 status;
	u32 ctl;

	if (i2c_start(TWI_STAT_TX_STA) != 0)
		return -1;

	/* Send chip address */
	if (i2c_send_data(chip << 1 | 0, TWI_STAT_TX_AW_ACK) != 0)
		return -1;

	/* Send data address */
	if (i2c_send_data(addr, TWI_STAT_TXD_ACK) != 0)
		return -1;

	/* Send restart for read */
	if (i2c_start(TWI_STAT_TX_RESTA) != 0)
		return -1;

	/* Send chip address */
	if (i2c_send_data(chip << 1 | 1, TWI_STAT_TX_AR_ACK) != 0)
		return -1;

	/* Set ACK mode */
	ctl = readl(&i2c_base->ctl);
	ctl |= TWI_CTL_ACK;
	writel(ctl, &i2c_base->ctl);
	status = TWI_STAT_RXD_ACK;

	/* Read data */
	while (len > 0) {
		if (len == 1) {
			/* Set NACK mode (last byte) */
			ctl = readl(&i2c_base->ctl);
			ctl &= ~TWI_CTL_ACK;
			writel(ctl, &i2c_base->ctl);
			status = TWI_STAT_RXD_NAK;
		}

		i2c_clear_irq();
		if (i2c_wait_irq_status(status) != 0)
			return -1;

		*buffer++ = readl(&i2c_base->data);
		len--;
	}

	return 0;
}
コード例 #14
0
static uint8_t i2c_write(uint32_t i2c, uint8_t address, uint8_t reg,
	uint8_t data)
{
	i2c_start(i2c, address, I2C_WRITE);

	i2c_send_data(i2c, reg);

	while (!(I2C_SR1(i2c) & (I2C_SR1_BTF)));
	i2c_send_data(i2c, data);

	while (!(I2C_SR1(i2c) & (I2C_SR1_BTF)));

	i2c_send_stop(i2c);

	return 0;
}
コード例 #15
0
ファイル: main.c プロジェクト: Linux-enCaja/ecbot
int main(void)
{
unsigned char IR[12]; // 0 -> avcc=ref

  init_hw();
  while (1) {
    if (i2c_get_received_data(rec_buf)){
      if (rec_buf[0] == SET_COLOR){
            RGB[0] = rec_buf[1];
            RGB[1] = rec_buf[2];
            RGB[2] = rec_buf[3];
      }
      if(rec_buf[0] == READ_IR ){
        out_buf[0]= IR[0]; out_buf[1]= IR[1];
        out_buf[2]= IR[2]; out_buf[3]= IR[3];
        i2c_send_data(out_buf);
      }
    }
    // Update PWMs
    set_color (RED,   RGB[0]);
    set_color (GREEN, RGB[1]);
    set_color (BLUE,  RGB[2]);


    Read_IR((unsigned short*)IR, 0x80);

    _delay_ms(IR_DELAY); 


  }
 return(0); // avoid gcc warning
}
コード例 #16
0
ファイル: issi.c プロジェクト: Kpat1/tmk_keyboard
uint8_t issi_read_byte(uint8_t page, uint8_t address) {
    cli();
    uint8_t pageSelect[] = { DEVICE_ADDRESS_WRITE, ADDRESS_COMMAND_REGISTER, page };
    uint8_t registerSelect[] = { DEVICE_ADDRESS_WRITE, address };
    uint8_t readAddress[] = { DEVICE_ADDRESS_READ };

    i2c_send_data(pageSelect, 3);
    i2c_send_data(registerSelect, 2);

    i2c_start();
    i2c_send_data(readAddress, 1);
    uint8_t ret = i2c_read(1);
    i2c_stop();
    sei();
    return ret;
}
コード例 #17
0
ファイル: i2c_arch.c プロジェクト: 1bitsquared/paparazzi
// Doc ID 13902 Rev 11 p 714/1072
// Transfer Sequence Diagram for Master Receiver for N=1
static inline enum STMI2CSubTransactionStatus stmi2c_read1(uint32_t i2c, struct i2c_periph *periph, struct i2c_transaction *trans)
{
  uint16_t SR1 = I2C_SR1(i2c);

  // Start Condition Was Just Generated
  if (BIT_X_IS_SET_IN_REG( I2C_SR1_SB, SR1 ) )
  {
    i2c_disable_interrupt(i2c, I2C_CR2_ITBUFEN);
    i2c_send_data(i2c, trans->slave_addr | 0x01);

    // Document the current Status
    periph->status = I2CAddrRdSent;
  }
  // Address Was Sent
  else if (BIT_X_IS_SET_IN_REG(I2C_SR1_ADDR, SR1) )
  {
    // First Clear the ACK bit: after the next byte we do not want new bytes
    i2c_nack_current(i2c);
    i2c_disable_ack(i2c);

    // --- next to steps MUST be executed together to avoid missing the stop
    __I2C_REG_CRITICAL_ZONE_START;

    // Only after setting ACK, read SR2 to clear the ADDR (next byte will start arriving)
    uint16_t SR2 __attribute__ ((unused)) = I2C_SR2(i2c);

    // Schedule a Stop
    PPRZ_I2C_SEND_STOP(i2c);

    __I2C_REG_CRITICAL_ZONE_STOP;
    // --- end of critical zone -----------

    // Enable the RXNE: it will trigger as soon as the 1 byte is received to get the result
    i2c_enable_interrupt(i2c, I2C_CR2_ITBUFEN);

    // Document the current Status
    periph->status = I2CReadingLastByte;
  }
  // As soon as there is 1 byte ready to read, we have our byte
  else if (BIT_X_IS_SET_IN_REG(I2C_SR1_RxNE, SR1) )
  {
    i2c_disable_interrupt(i2c, I2C_CR2_ITBUFEN);
    trans->buf[0] = I2C_DR(i2c);

    // We got all the results (stop condition might still be in progress but this is the last interrupt)
    trans->status = I2CTransSuccess;

    // Document the current Status:
    // -the stop was actually already requested in the previous step
    periph->status = I2CStopRequested;

    return STMI2C_SubTra_Ready_StopRequested;
  }
  else // Event Logic Error
  {
    return STMI2C_SubTra_Error;
  }

  return STMI2C_SubTra_Busy;
}
コード例 #18
0
ファイル: lpc11xx_i2c.c プロジェクト: 88kacper8/u8glib
uint8_t i2c_send_2byte(i2c_struct *i2c, uint8_t adr, uint8_t b1, uint8_t b2, uint8_t send_stop)
{
  uint8_t buf[2];
  buf[0] = b1;
  buf[1] = b2;
  
  return i2c_send_data(i2c, adr, 2, buf, send_stop);
}
コード例 #19
0
ファイル: c8051f040_i2c.c プロジェクト: dlts200466/density
void eep_set_addr(unsigned char slave,unsigned int addr)
{
	start();
	i2c_send_addr(slave,0);//д
	i2c_send_data(addr>>8);
	i2c_send_data((unsigned char)addr);
//	stop();
}
コード例 #20
0
ファイル: i2c.c プロジェクト: DavidJohan/Assemble-and-Animate
uint8_t i2c_read_byte(uint8_t dev, uint8_t addr){
	if(i2c_send_start()!=0) return 0;
	if(i2c_send_addr(dev,TW_WRITE)!=0) return 0;
	if(i2c_send_data(addr)!=0) return 0;
	if(i2c_send_start()!=0) return 0;
	if(i2c_send_addr(dev,TW_READ)!=0) return 0;
	uint8_t data= i2c_receive_data(0);
	i2c_send_stop();
	return data;
}	
コード例 #21
0
ファイル: i2c.c プロジェクト: DavidJohan/Assemble-and-Animate
int16_t i2c_read_word(uint8_t dev, uint8_t addr){
	if(i2c_send_start()!=0) return 0;
	if(i2c_send_addr(dev,TW_WRITE)!=0) return 0;
	if(i2c_send_data(addr)!=0) return 0;
	if(i2c_send_start()!=0) return 0;
	if(i2c_send_addr(dev,TW_READ)!=0) return 0;
	uint8_t datal= i2c_receive_data(1);
	uint8_t datah= i2c_receive_data(0);
	i2c_send_stop();
	return ((datah<<8) |  datal);
}
コード例 #22
0
ファイル: i2c.c プロジェクト: DavidJohan/Assemble-and-Animate
void i2c_seq_read(uint8_t dev, uint8_t start_addr, uint8_t*buff, uint8_t len){
	int i=0;
	if(i2c_send_start()!=0) return;
	if(i2c_send_addr(dev,TW_WRITE)!=0) return;
	if(i2c_send_data(start_addr)!=0) return;
	if(i2c_send_start()!=0) return;
	if(i2c_send_addr(dev,TW_READ)!=0) return;
	for(i=0;i<len;i++)
		*buff++=i2c_receive_data((i<(len-1))?1:0); //send ack until last byte to read
	i2c_send_stop();
}
コード例 #23
0
ファイル: board_fy20ap.cpp プロジェクト: px4dev/mavmon
static void
com_send_data(uint8_t c)
{
//	if (!selected)
		com_send_start();

	i2c_send_data(I2C1, c);
	while (!(I2C_SR1(I2C1) & I2C_SR1_BTF))
		;

	com_send_stop();
}
コード例 #24
0
ファイル: i2c.c プロジェクト: franzbischoff/stm32f4inicial
static uint32_t i2c_read(uint8_t reg)
{
    //    while ((I2C_SR2(i2c) & I2C_SR2_BUSY)) {
    //    }

    i2c_send_start(I2C_PORT);

    /* Wait for master mode selected */
    while (!((I2C_SR1(I2C_PORT) & I2C_SR1_SB)
           & (I2C_SR2(I2C_PORT) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

    i2c_send_7bit_address(I2C_PORT, SLAVE_ADDRESS, I2C_WRITE);

    /* Waiting for address is transferred. */
    while (!(I2C_SR1(I2C_PORT) & I2C_SR1_ADDR));

    /* Cleaning ADDR condition sequence. */
    uint32_t reg32 = I2C_SR2(I2C_PORT);
    (void) reg32; /* unused */

    /*  Common stuff ABOVE HERE     */

    i2c_send_data(I2C_PORT, reg);
    while (!(I2C_SR1(I2C_PORT) & (I2C_SR1_BTF)));

    i2c_send_start(I2C_PORT);

    /* Wait for master mode selected */
    while (!((I2C_SR1(I2C_PORT) & I2C_SR1_SB)
           & (I2C_SR2(I2C_PORT) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

    i2c_send_7bit_address(I2C_PORT, SLAVE_ADDRESS, I2C_READ);

    /* Waiting for address is transferred. */
    while (!(I2C_SR1(I2C_PORT) & I2C_SR1_ADDR));

    i2c_disable_ack(I2C_PORT);

    /* Cleaning ADDR condition sequence. */
    reg32 = I2C_SR2(I2C_PORT);
    (void) reg32; /* unused */

    i2c_send_stop(I2C_PORT);

    while (!(I2C_SR1(I2C_PORT) & I2C_SR1_RxNE));
    uint32_t result = i2c_get_data(I2C_PORT);

    i2c_enable_ack(I2C_PORT);
    I2C_SR1(I2C_PORT) &= ~I2C_SR1_AF;
    return result;
}
コード例 #25
0
ファイル: i2c.c プロジェクト: lxmlx/pcduino-test
static int i2c_do_write(uchar chip, uint addr, int alen, uchar *buffer,
			int len)
{
	if (i2c_start(TWI_STAT_TX_STA) != 0)
		return -1;

	/* Send chip address */
	if (i2c_send_data(chip << 1 | 0, TWI_STAT_TX_AW_ACK) != 0)
		return -1;

	/* Send data address */
	if (i2c_send_data(addr, TWI_STAT_TXD_ACK) != 0)
		return -1;

	/* Send data */
	while (len > 0) {
		if (i2c_send_data(*buffer++, TWI_STAT_TXD_ACK) != 0)
			return -1;
		len--;
	}

	return 0;
}
コード例 #26
0
static uint32_t i2c_read(uint32_t i2c, uint8_t address, uint8_t reg)
{
	while ((I2C_SR2(i2c) & I2C_SR2_BUSY));

	i2c_start(i2c, address, I2C_WRITE);
	i2c_send_data(i2c, reg);

	while (!(I2C_SR1(i2c) & (I2C_SR1_BTF)));

	i2c_start(i2c, address, I2C_READ);

	i2c_send_stop(i2c);

	while (!(I2C_SR1(i2c) & I2C_SR1_RxNE));

	uint32_t result = i2c_get_data(i2c);

	I2C_SR1(i2c) &= ~I2C_SR1_AF;

	return result;
}
コード例 #27
0
ファイル: i2c.c プロジェクト: 0utsider89/libopencm3
void read_i2c(uint32_t i2c, uint8_t i2c_addr, uint8_t reg, uint8_t size,
	      uint8_t *data)
{
	int wait;
	int i;
	while (i2c_busy(i2c) == 1);
	while (i2c_is_start(i2c) == 1);
	/*Setting transfer properties*/
	i2c_set_bytes_to_transfer(i2c, 1);
	i2c_set_7bit_address(i2c, i2c_addr);
	i2c_set_write_transfer_dir(i2c);
	i2c_disable_autoend(i2c);
	/*start transfer*/
	i2c_send_start(i2c);

	wait = true;
	while (wait) {
		if (i2c_transmit_int_status(i2c)) {
			wait = false;
		}
		while (i2c_nack(i2c)); /* Some error */
	}
	i2c_send_data(i2c, reg);

	while (i2c_is_start(i2c) == 1);
	/*Setting transfer properties*/
	i2c_set_bytes_to_transfer(i2c, size);
	i2c_set_7bit_address(i2c, i2c_addr);
	i2c_set_read_transfer_dir(i2c);
	i2c_enable_autoend(i2c);
	/*start transfer*/
	i2c_send_start(i2c);

	for (i = 0; i < size; i++) {
		while (i2c_received_data(i2c) == 0);
		data[i] = i2c_get_data(i2c);
	}
}
コード例 #28
0
void check_i2c_interface(void){
	if (i2c_get_received_data(i2c_buf)){
		if (i2c_buf[0]=='i'){
			if (i2c_buf[1]=='=' && i2c_buf[2]!='\0'){
				set_val[0]=atoi(&i2c_buf[2]);
				if(set_val[0]>I_MAX){
					set_val[0]=I_MAX;
				}
				if(set_val[0]<0){
					set_val[0]=0;
				}
				i2c_send_data("ok");
			}else{
				int_to_ascii(measured_val[0],i2c_buf,2,0);
				strcat(i2c_buf,"A");
				i2c_send_data(i2c_buf);
			}
		}else if (i2c_buf[0]=='s'){
			store_permanent();
			i2c_send_data("ok");
		}else if (i2c_buf[0]=='u'){
			if (i2c_buf[1]=='=' && i2c_buf[2]!='\0'){
				set_val[1]=atoi(&i2c_buf[2]);
				if(set_val[1]>U_MAX){
					set_val[1]=U_MAX;
				}
				if(set_val[1]<0){
					set_val[1]=0;
				}
				i2c_send_data("ok");
			}else{
				int_to_ascii(measured_val[1],i2c_buf,1,0);
				strcat(i2c_buf,"V");
				i2c_send_data(i2c_buf);
			}
		}else{
			i2c_send_data("err");
		}
	}
}
コード例 #29
0
uint16_t stts75_read_temperature(uint32_t i2c, uint8_t sensor)
{
	uint32_t reg32 __attribute__((unused));
	uint16_t temperature;

	/* Send START condition. */
	i2c_send_start(i2c);

	/* Waiting for START is send and switched to master mode. */
	while (!((I2C_SR1(i2c) & I2C_SR1_SB)
		& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

	/* Say to what address we want to talk to. */
	/* Yes, WRITE is correct - for selecting register in STTS75. */
	i2c_send_7bit_address(i2c, sensor, I2C_WRITE);

	/* Waiting for address is transferred. */
	while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));

	/* Cleaning ADDR condition sequence. */
	reg32 = I2C_SR2(i2c);

	i2c_send_data(i2c, 0x0); /* temperature register */
	while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE)));

	/*
	 * Now we transferred that we want to ACCESS the temperature register.
	 * Now we send another START condition (repeated START) and then
	 * transfer the destination but with flag READ.
	 */

	/* Send START condition. */
	i2c_send_start(i2c);

	/* Waiting for START is send and switched to master mode. */
	while (!((I2C_SR1(i2c) & I2C_SR1_SB)
		& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));

	/* Say to what address we want to talk to. */
	i2c_send_7bit_address(i2c, sensor, I2C_READ);

	/* 2-byte receive is a special case. See datasheet POS bit. */
	I2C_CR1(i2c) |= (I2C_CR1_POS | I2C_CR1_ACK);

	/* Waiting for address is transferred. */
	while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));

	/* Cleaning ADDR condition sequence. */
	reg32 = I2C_SR2(i2c);

	/* Cleaning I2C_SR1_ACK. */
	I2C_CR1(i2c) &= ~I2C_CR1_ACK;

	/* Now the slave should begin to send us the first byte. Await BTF. */
	while (!(I2C_SR1(i2c) & I2C_SR1_BTF));
	temperature = (uint16_t)(I2C_DR(i2c) << 8); /* MSB */

	/*
	 * Yes they mean it: we have to generate the STOP condition before
	 * saving the 1st byte.
	 */
	I2C_CR1(i2c) |= I2C_CR1_STOP;

	temperature |= I2C_DR(i2c); /* LSB */

	/* Original state. */
	I2C_CR1(i2c) &= ~I2C_CR1_POS;

	return temperature;
}
コード例 #30
0
int main(void)
{
	char out_buf[20+1];
	measured_val[0]=0;
	measured_val[1]=0;
	init_dac();
	lcd_init(LCD_DISP_ON);
	init_kbd();
	set_val[0]=15;set_val[1]=50; // 150mA and 5V
	if (eeprom_read_byte((uint8_t *)0x0) == 19){
		// ok magic number matches accept values
		set_val[1]=eeprom_read_word((uint16_t *)0x04);
		set_val[0]=eeprom_read_word((uint16_t *)0x02);
	}
	// I2C also called TWI
	i2c_init(3,1,0);
	sei();
	i2c_send_data("on");
	init_analog();
	while (1) {

		// current
		measured_val[0]=adc_i_to_disp(getanalogresult(0));
		set_val_adcUnits[0]=disp_i_to_adc(set_val[0]);
		set_target_adc_val(0,set_val_adcUnits[0]);
		// voltage
		measured_val[1]=adc_u_to_disp(getanalogresult(1),measured_val[0]);
		set_val_adcUnits[1]=disp_u_to_adc(set_val[1])+disp_i_to_u_adc_offset(measured_val[0]);
		set_target_adc_val(1,set_val_adcUnits[1]);

		// voltage
		lcd_clrscr();
		int_to_ascii(measured_val[1],out_buf,1,1);
		lcd_puts(out_buf);
		lcd_puts("V ");
		int_to_ascii(set_val[1],out_buf,1,1);
		lcd_putc('[');
		lcd_puts(out_buf);
		lcd_putc(']');
		if (!is_current_limit()){
			// put a marker to show which value is currenlty limiting
			lcd_puts("<-");
		}

		// current
		lcd_gotoxy(0,1);
		int_to_ascii(measured_val[0],out_buf,2,0);
		lcd_puts(out_buf);
		lcd_puts("A ");
		int_to_ascii(set_val[0],out_buf,2,0);
		lcd_putc('[');
		lcd_puts(out_buf);
		lcd_putc(']');
		if (is_current_limit()){
			// put a marker to show which value is currenlty limiting
			lcd_puts("<-");
		}
		//dbg
		//int_to_ascii(is_dacval(),out_buf,0,0);
		//lcd_puts(out_buf);
		check_i2c_interface();

		// the buttons must be responsive but they must not 
		// scroll too fast if pressed permanently
		if (check_buttons()==0){
			// no buttons pressed
			delay_ms(100);
			bpress=0;
			check_i2c_interface();
			check_buttons();
			delay_ms(150);
		}else{
			// button press
			if (bpress > 11){
				// somebody pressed permanetly the button=>scroll fast
				delay_ms(10);
				check_i2c_interface();
				delay_ms(40);
			}else{
				bpress++;
				delay_ms(100);
				check_i2c_interface();
				delay_ms(150);
			}
		}
	}
	return(0);
}