aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32/f1/other/i2c_stts75_sensor/stts75.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/stm32/f1/other/i2c_stts75_sensor/stts75.c')
-rw-r--r--examples/stm32/f1/other/i2c_stts75_sensor/stts75.c186
1 files changed, 186 insertions, 0 deletions
diff --git a/examples/stm32/f1/other/i2c_stts75_sensor/stts75.c b/examples/stm32/f1/other/i2c_stts75_sensor/stts75.c
new file mode 100644
index 0000000..f28019c
--- /dev/null
+++ b/examples/stm32/f1/other/i2c_stts75_sensor/stts75.c
@@ -0,0 +1,186 @@
+/*
+ * This file is part of the libopencm3 project.
+ *
+ * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <libopencm3/stm32/i2c.h>
+#include "stts75.h"
+
+void stts75_write_config(u32 i2c, u8 sensor)
+{
+ u32 reg32;
+
+ /* 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. */
+ i2c_send_data(i2c, 0x4); /* pol reverse - LED glows if temp is below Tos/Thyst */
+ 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;
+
+ /* 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 */
+ while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE))); /* After the last byte we have to wait for TxE too. */
+
+ /* Send STOP condition. */
+ i2c_send_stop(i2c);
+}
+
+void stts75_write_temp_hyst(u32 i2c, u8 sensor, u16 temp_hyst)
+{
+ u32 reg32;
+
+ /* 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 */
+ while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE))); /* After the last byte we have to wait for TxE too. */
+
+ /* Send STOP condition. */
+ i2c_send_stop(i2c);
+}
+
+u16 stts75_read_temperature(u32 i2c, u8 sensor)
+{
+ u32 reg32;
+ 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;
+}