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; }
int tda18219_read_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_BTF | I2C_SR1_TxE))); /* Send re-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. */ i2c_send_7bit_address(I2C1, TDA18219_I2C_ADDR, I2C_READ); I2C_CR1(I2C1) &= ~I2C_CR1_ACK; /* Waiting for address is transferred. */ while (!(I2C_SR1(I2C1) & I2C_SR1_ADDR)); /* Cleaning ADDR condition sequence. */ reg32 = I2C_SR2(I2C1); i2c_send_stop(I2C1); while (!(I2C_SR1(I2C1) & I2C_SR1_RxNE)); *value = I2C_DR(I2C1); return 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); }
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); }
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); }
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; }
float tmp_read_temperature(void) { // Store signed temperature info (12 bit from device, with 4LSBs = 0) s16 temperature; // Dummy variable to store read results from registers that must be read u32 reg; // Send START i2c_send_start(I2C1); while(!(I2C1_SR1 & I2C_SR1_SB)); // Send the TMP102's address and READ i2c_send_7bit_address(I2C1, 0x48, I2C_READ); while(!(I2C1_SR1 & I2C_SR1_ADDR)); // Clear ADDR bit, send ACKs, read byte one (MSB) reg = I2C1_SR2; I2C1_CR1 |= I2C_CR1_ACK; while(!(I2C1_SR1 & I2C_SR1_BTF)); temperature = (u16)(I2C1_DR << 8); // Stop sending ACKs, send STOP, read byte two (LSB) I2C1_CR1 &= ~I2C_CR1_ACK; I2C1_CR1 |= I2C_CR1_STOP; while(!(I2C1_SR1 & I2C_SR1_BTF)); temperature |= I2C1_DR; temperature >>= 4; return (float)temperature * 0.0625; }
/* 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 {
static void com_send_start() { /* send start */ i2c_send_start(I2C1); while (!((I2C_SR1(I2C1) & I2C_SR1_SB) & (I2C_SR2(I2C1) & (I2C_SR2_MSL | I2C_SR2_BUSY)))) ; /* send address */ i2c_send_7bit_address(I2C1, current_address, I2C_WRITE); while (!(I2C_SR1(I2C1) & I2C_SR1_ADDR)) ; (void)I2C_SR2(I2C1); selected = true; }
static uint8_t i2c_start(uint32_t i2c, uint8_t address, uint8_t mode) { i2c_send_start(i2c); /* Wait for master mode selected */ while (!((I2C_SR1(i2c) & I2C_SR1_SB) & (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY)))); i2c_send_7bit_address(i2c, address, mode); /* Waiting for address is transferred. */ while (!(I2C_SR1(i2c) & I2C_SR1_ADDR)); /* Cleaning ADDR condition sequence. */ uint32_t reg32 = I2C_SR2(i2c); (void) reg32; /* unused */ return 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; }