/* * This file is part of the libopencm3 project. * * Copyright (C) 2010 Thomas Otto * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library. If not, see . */ #include #include "stts75.h" void stts75_write_config(u32 i2c, u8 sensor) { u32 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); } void stts75_write_temp_os(u32 i2c, u8 sensor, u16 temp_os) { u32 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, (u8)(temp_os >> 8)); /* MSB */ while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); i2c_send_data(i2c, (u8)(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_temp_hyst(u32 i2c, u8 sensor, u16 temp_hyst) { u32 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, (u8)(temp_hyst >> 8)); /* MSB */ while (!(I2C_SR1(i2c) & I2C_SR1_BTF)); i2c_send_data(i2c, (u8)(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); } u16 stts75_read_temperature(u32 i2c, u8 sensor) { u32 reg32 __attribute__((unused)); u16 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 = (u16)(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; }