From 46e5ed202a16f29775f06430c0ce8b1878121ed9 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Fri, 5 Mar 2010 22:35:29 +0100 Subject: Add i2c_stts75_sensor example. Thanks Thomas Otto for the patch! --- examples/other/i2c_stts75_sensor/stts75.c | 186 ++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 examples/other/i2c_stts75_sensor/stts75.c (limited to 'examples/other/i2c_stts75_sensor/stts75.c') diff --git a/examples/other/i2c_stts75_sensor/stts75.c b/examples/other/i2c_stts75_sensor/stts75.c new file mode 100644 index 0000000..69aa4be --- /dev/null +++ b/examples/other/i2c_stts75_sensor/stts75.c @@ -0,0 +1,186 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 Thomas Otto + * + * 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 . + */ + +#include +#include "stts75.h" + +void stts75_write_config(u32 i2c, u8 sensor) +{ + u32 tempreg; + + /* 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. */ + tempreg = 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 tempreg; + + /* 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. */ + tempreg = 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 tempreg; + + /* 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. */ + tempreg = 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 tempreg; + 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. */ + tempreg = 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. */ + tempreg = 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; +} -- cgit v1.2.3