aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32/other/i2c_stts75_sensor
diff options
context:
space:
mode:
authorUwe Hermann2010-12-30 01:52:44 +0100
committerUwe Hermann2010-12-30 01:52:44 +0100
commitbcce98da54b047a15ef0abf41e0c7794f29f0439 (patch)
tree98462dcb9874341bd7342a1794ec01e8ad420d47 /examples/stm32/other/i2c_stts75_sensor
parentb65f3f06d061823fac1af91594e0d702b4c80693 (diff)
Move STM32 examples to examples/stm32 subdir.
Diffstat (limited to 'examples/stm32/other/i2c_stts75_sensor')
-rw-r--r--examples/stm32/other/i2c_stts75_sensor/Makefile25
-rw-r--r--examples/stm32/other/i2c_stts75_sensor/README13
-rw-r--r--examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.c146
-rw-r--r--examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.ld31
-rw-r--r--examples/stm32/other/i2c_stts75_sensor/stts75.c186
-rw-r--r--examples/stm32/other/i2c_stts75_sensor/stts75.h39
6 files changed, 440 insertions, 0 deletions
diff --git a/examples/stm32/other/i2c_stts75_sensor/Makefile b/examples/stm32/other/i2c_stts75_sensor/Makefile
new file mode 100644
index 0000000..32ee066
--- /dev/null
+++ b/examples/stm32/other/i2c_stts75_sensor/Makefile
@@ -0,0 +1,25 @@
+##
+## This file is part of the libopenstm32 project.
+##
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
+##
+## 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/>.
+##
+
+BINARY = i2c_stts75_sensor
+
+OBJS = stts75.o
+
+include ../../Makefile.include
+
diff --git a/examples/stm32/other/i2c_stts75_sensor/README b/examples/stm32/other/i2c_stts75_sensor/README
new file mode 100644
index 0000000..91e3ff6
--- /dev/null
+++ b/examples/stm32/other/i2c_stts75_sensor/README
@@ -0,0 +1,13 @@
+------------------------------------------------------------------------------
+README
+------------------------------------------------------------------------------
+
+This example program sends some characters on USART1.
+Afterwards it connects to an STTS75 sensor (ST LM75 compatible)
+at adress A0/1/2=0 and sets reverse polarity, 26 degree Tos and Thyst.
+
+It reads out the temperature and submits the temperature over USART1 in
+binary format (ASCII 0/1).
+
+The terminal settings for the receiving device/PC are 115200 8n1.
+
diff --git a/examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.c b/examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.c
new file mode 100644
index 0000000..1227fd1
--- /dev/null
+++ b/examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.c
@@ -0,0 +1,146 @@
+/*
+ * This file is part of the libopenstm32 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 <libopenstm32/rcc.h>
+#include <libopenstm32/flash.h>
+#include <libopenstm32/gpio.h>
+#include <libopenstm32/usart.h>
+#include <libopenstm32/i2c.h>
+#include "stts75.h"
+
+void usart_setup(void)
+{
+ /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN);
+
+ /* Setup GPIO pin GPIO_USART1_TX/GPIO9 on GPIO port A for transmit. */
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
+
+ /* Setup UART parameters. */
+ usart_set_baudrate(USART1, 115200);
+ usart_set_databits(USART1, 8);
+ usart_set_stopbits(USART1, USART_STOPBITS_1);
+ usart_set_mode(USART1, USART_MODE_TX_RX);
+ usart_set_parity(USART1, USART_PARITY_NONE);
+ usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
+
+ /* Finally enable the USART. */
+ usart_enable(USART1);
+}
+
+void gpio_setup(void)
+{
+ /* Enable GPIOB clock. */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
+
+ /* Set GPIO6/7 (in GPIO port B) to 'output push-pull' for the LEDs. */
+ gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO6);
+ gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, GPIO7);
+}
+
+void i2c_setup(void)
+{
+ /* Enable clocks for I2C2 and AFIO. */
+ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C2EN);
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
+
+ /* Set alternate functions for the SCL and SDA pins of I2C2. */
+ gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN,
+ GPIO_I2C2_SCL | GPIO_I2C2_SDA);
+
+ /* Disable the I2C before changing any configuration. */
+ i2c_peripheral_disable(I2C2);
+
+ /* APB1 is running at 36MHz. */
+ i2c_set_clock_frequency(I2C2, I2C_CR2_FREQ_36MHZ);
+
+ /* 400KHz - I2C Fast Mode */
+ i2c_set_fast_mode(I2C2);
+
+ /*
+ * fclock for I2C is 36MHz APB2 -> cycle time 28ns, low time at 400KHz
+ * incl trise -> Thigh= 1600ns; CCR= tlow/tcycle= 0x1C,9;
+ * datasheet suggests 0x1e.
+ */
+ i2c_set_ccr(I2C2, 0x1e);
+
+ /*
+ * fclock for I2C is 36MHz -> cycle time 28ns, rise time for
+ * 400KHz => 300ns and 100KHz => 1000ns; 300ns/28ns = 10;
+ * incremented by 1 -> 11.
+ */
+ i2c_set_trise(I2C2, 0x0b);
+
+ /*
+ * This is our slave address - needed only if we want to receive from
+ * other masters.
+ */
+ i2c_set_own_7bit_slave_address(I2C2, 0x32);
+
+ /* If everything is configured -> enable the peripheral. */
+ i2c_peripheral_enable(I2C2);
+}
+
+int main(void)
+{
+ int i = 0;
+ u16 temperature;
+
+ rcc_clock_setup_in_hse_16mhz_out_72mhz();
+ gpio_setup();
+ usart_setup();
+ i2c_setup();
+
+ gpio_clear(GPIOB, GPIO7); /* LED1 on */
+ gpio_set(GPIOB, GPIO6); /* LED2 off */
+
+ /* Send a message on USART1. */
+ usart_send(USART1, 's');
+ usart_send(USART1, 't');
+ usart_send(USART1, 'm');
+ usart_send(USART1, '\r');
+ usart_send(USART1, '\n');
+
+ stts75_write_config(I2C2, STTS75_SENSOR0);
+ stts75_write_temp_os(I2C2, STTS75_SENSOR0, 0x1a00); /* 26 degrees */
+ stts75_write_temp_hyst(I2C2, STTS75_SENSOR0, 0x1a00);
+ temperature = stts75_read_temperature(I2C2, STTS75_SENSOR0);
+
+ /* Send the temperature as binary over USART1. */
+ for (i = 15; i >= 0 ; i--) {
+ if (temperature & (1 << i))
+ usart_send(USART1, '1');
+ else
+ usart_send(USART1, '0');
+ }
+
+ usart_send(USART1, '\r');
+ usart_send(USART1, '\n');
+
+ gpio_clear(GPIOB, GPIO6); /* LED2 on */
+
+ while(1); /* Halt. */
+
+ return 0;
+}
diff --git a/examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.ld b/examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.ld
new file mode 100644
index 0000000..a1e9de5
--- /dev/null
+++ b/examples/stm32/other/i2c_stts75_sensor/i2c_stts75_sensor.ld
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the libopenstm32 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/>.
+ */
+
+/* Linker script for an STM32F103RBT6 board (128K flash, 20K RAM). */
+
+/* Define memory regions. */
+MEMORY
+{
+ rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
+ ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
+}
+
+/* Include the common ld script from libopenstm32. */
+INCLUDE libopenstm32.ld
+
diff --git a/examples/stm32/other/i2c_stts75_sensor/stts75.c b/examples/stm32/other/i2c_stts75_sensor/stts75.c
new file mode 100644
index 0000000..319394f
--- /dev/null
+++ b/examples/stm32/other/i2c_stts75_sensor/stts75.c
@@ -0,0 +1,186 @@
+/*
+ * This file is part of the libopenstm32 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 <libopenstm32/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;
+}
diff --git a/examples/stm32/other/i2c_stts75_sensor/stts75.h b/examples/stm32/other/i2c_stts75_sensor/stts75.h
new file mode 100644
index 0000000..e07fe23
--- /dev/null
+++ b/examples/stm32/other/i2c_stts75_sensor/stts75.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of the libopenstm32 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/>.
+ */
+
+#ifndef STTS75_H
+#define STTS75_H
+
+#include <stdint.h>
+
+#define STTS75_SENSOR0 0x48
+#define STTS75_SENSOR1 0x49
+#define STTS75_SENSOR2 0x4a
+#define STTS75_SENSOR3 0x4b
+#define STTS75_SENSOR4 0x4c
+#define STTS75_SENSOR5 0x4d
+#define STTS75_SENSOR6 0x4e
+#define STTS75_SENSOR7 0x4f
+
+void stts75_write_config(u32 i2c, u8 sensor);
+void stts75_write_temp_os(u32 i2c, u8 sensor, u16 temp_os);
+void stts75_write_temp_hyst(u32 i2c, u8 sensor, u16 temp_hyst);
+u16 stts75_read_temperature(u32 i2c, u8 sensor);
+
+#endif