From 8887dbbde2e35d3ed21a8b5cee9be2a68bfe900c Mon Sep 17 00:00:00 2001
From: Rocco Marco Guglielmi <roccomarco.guglielmi@gmail.com>
Date: Fri, 9 Mar 2018 23:45:17 +0000
Subject: Updated HTS221 Driver and related demo

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11678 110e8d01-0319-4d1e-a829-52ad28d1bb01
---
 testex/STM32/STM32F4xx/I2C-HTS221/Makefile         |  34 ++--
 ...F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch |   2 +-
 testex/STM32/STM32F4xx/I2C-HTS221/main.c           | 172 +++++----------------
 3 files changed, 51 insertions(+), 157 deletions(-)

(limited to 'testex/STM32')

diff --git a/testex/STM32/STM32F4xx/I2C-HTS221/Makefile b/testex/STM32/STM32F4xx/I2C-HTS221/Makefile
index 7beed88eb..09b7c1d4e 100644
--- a/testex/STM32/STM32F4xx/I2C-HTS221/Makefile
+++ b/testex/STM32/STM32F4xx/I2C-HTS221/Makefile
@@ -87,6 +87,9 @@ PROJECT = ch
 
 # Imported source files and paths
 CHIBIOS = ../../../..
+
+# Licensing files.
+include $(CHIBIOS)/os/license/license.mk
 # Startup files.
 include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f4xx.mk
 # HAL-OSAL files (optional).
@@ -97,31 +100,23 @@ include $(CHIBIOS)/os/hal/osal/rt/osal.mk
 # RTOS files (optional).
 include $(CHIBIOS)/os/rt/rt.mk
 include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk
-# Other files (optional).
+# EX files (optional).
 include $(CHIBIOS)/os/ex/ST/hts221.mk
+# Other files (optional).
 include $(CHIBIOS)/os/hal/lib/streams/streams.mk
-include $(CHIBIOS)/os/various/shell/shell.mk
 
 # Define linker script file here
 LDSCRIPT= $(STARTUPLD)/STM32F401xE.ld
 
 # C sources that can be compiled in ARM or THUMB mode depending on the global
 # setting.
-CSRC = $(STARTUPSRC) \
-       $(KERNSRC) \
-       $(PORTSRC) \
-       $(OSALSRC) \
-       $(HALSRC) \
-       $(PLATFORMSRC) \
-       $(BOARDSRC) \
-       $(HTS221SRC) \
-       $(STREAMSSRC) \
-       $(SHELLSRC) \
+CSRC = $(ALLCSRC) \
+       $(TESTSRC) \
        main.c
 
 # C++ sources that can be compiled in ARM or THUMB mode depending on the global
 # setting.
-CPPSRC =
+CPPSRC = $(ALLCPPSRC)
 
 # C sources to be compiled in ARM mode regardless of the global setting.
 # NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
@@ -147,10 +142,11 @@ TCPPSRC =
 ASMSRC =
 ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM)
 
-INCDIR = $(CHIBIOS)/os/license \
-         $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
-         $(HALINC) $(PLATFORMINC) $(BOARDINC) $(HTS221INC) \
-         $(STREAMSINC) $(SHELLINC)
+# List ASM source files here
+ASMSRC = $(ALLASMSRC)
+ASMXSRC = $(ALLXASMSRC)
+
+INCDIR = $(ALLINC) $(TESTINC)
 
 #
 # Project, sources and paths
@@ -200,8 +196,8 @@ CPPWARN = -Wall -Wextra -Wundef
 #
 
 # List all user C define here, like -D_DEBUG=1
-UDEFS = -DCHPRINTF_USE_FLOAT=1 -DSHELL_CMD_TEST_ENABLED=0 \
-        -DHTS221_USE_ADVANCED=0 -DLPS25H_SHARED_I2C=0
+UDEFS = -DCHPRINTF_USE_FLOAT=1 \
+        -DHTS221_USE_ADVANCED=0 -DHTS221_SHARED_I2C=0
 
 # Define ASM defines here
 UADEFS =
diff --git a/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch b/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch
index 83de3fdfd..e1ea50989 100644
--- a/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch	
+++ b/testex/STM32/STM32F4xx/I2C-HTS221/debug/STM32F4xx-I2C-HTS221 (OpenOCD, Flash and Run).launch	
@@ -33,7 +33,7 @@
 <intAttribute key="org.eclipse.cdt.launch.ATTR_BUILD_BEFORE_LAUNCH_ATTR" value="2"/>
 <stringAttribute key="org.eclipse.cdt.launch.COREFILE_PATH" value=""/>
 <stringAttribute key="org.eclipse.cdt.launch.DEBUGGER_REGISTER_GROUPS" value=""/>
-<stringAttribute key="org.eclipse.cdt.launch.FORMAT" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;contentList&gt;&lt;content id=&quot;rxbuf-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;reg-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;cr-hts221Start-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;tmp-read_raw.lto_priv.21-(format)&quot; val=&quot;0&quot;/&gt;&lt;content id=&quot;null-lps25hStart-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;null-read_raw-(format)&quot; val=&quot;1&quot;/&gt;&lt;content id=&quot;null-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;rxbuf-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;tmp-hygro_read_raw.lto_priv.26-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;H0_rH_x2-hts221Calibrate-(format)&quot; val=&quot;1&quot;/&gt;&lt;/contentList&gt;"/>
+<stringAttribute key="org.eclipse.cdt.launch.FORMAT" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;contentList&gt;&lt;content id=&quot;H0_rH_x2-hts221Calibrate-(format)&quot; val=&quot;1&quot;/&gt;&lt;content id=&quot;tmp-hygro_read_raw.lto_priv.26-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;rxbuf-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;null-lps25hI2CReadRegister-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;null-read_raw-(format)&quot; val=&quot;1&quot;/&gt;&lt;content id=&quot;null-lps25hStart-(format)&quot; val=&quot;4&quot;/&gt;&lt;content id=&quot;tmp-read_raw.lto_priv.21-(format)&quot; val=&quot;0&quot;/&gt;&lt;content id=&quot;cr-hts221Start-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;reg-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;content id=&quot;rxbuf-hts221I2CReadRegister-(format)&quot; val=&quot;2&quot;/&gt;&lt;/contentList&gt;"/>
 <stringAttribute key="org.eclipse.cdt.launch.GLOBAL_VARIABLES" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;globalVariableList/&gt;&#13;&#10;"/>
 <stringAttribute key="org.eclipse.cdt.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;memoryBlockExpressionList/&gt;&#13;&#10;"/>
 <stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="./build/ch.elf"/>
diff --git a/testex/STM32/STM32F4xx/I2C-HTS221/main.c b/testex/STM32/STM32F4xx/I2C-HTS221/main.c
index 891073dd8..dc1cb5cd8 100644
--- a/testex/STM32/STM32F4xx/I2C-HTS221/main.c
+++ b/testex/STM32/STM32F4xx/I2C-HTS221/main.c
@@ -17,12 +17,11 @@
 #include "ch.h"
 #include "hal.h"
 
-#include "string.h"
-#include "shell.h"
 #include "chprintf.h"
-
 #include "hts221.h"
 
+#define cls(chp)  chprintf(chp, "\033[2J\033[1;1H")
+
 /*===========================================================================*/
 /* HTS221 related.                                                           */
 /*===========================================================================*/
@@ -30,10 +29,11 @@
 /* HTS221 Driver: This object represent an HTS221 instance */
 static  HTS221Driver HTS221D1;
 
-static int32_t rawdata[HTS221_HYGRO_NUMBER_OF_AXES +
-                       HTS221_THERMO_NUMBER_OF_AXES];
-static float cookeddata[HTS221_HYGRO_NUMBER_OF_AXES +
-                        HTS221_THERMO_NUMBER_OF_AXES];
+static int32_t hygroraw;
+static int32_t thermoraw;
+
+static float hygrocooked;
+static float thermocooked;
 
 static const I2CConfig i2ccfg = {
   OPMODE_I2C,
@@ -46,6 +46,8 @@ static const HTS221Config hts221cfg = {
   &i2ccfg,
   NULL,
   NULL,
+  NULL,
+  NULL,
   HTS221_ODR_7HZ,
 #if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
   HTS221_BDU_CONTINUOUS,
@@ -55,113 +57,10 @@ static const HTS221Config hts221cfg = {
 };
 
 /*===========================================================================*/
-/* Command line related.                                                     */
-/*===========================================================================*/
-
-/* Enable use of special ANSI escape sequences */
-#define CHPRINTF_USE_ANSI_CODE      TRUE
-#define SHELL_WA_SIZE               THD_WORKING_AREA_SIZE(2048)
-
-static void cmd_read(BaseSequentialStream *chp, int argc, char *argv[]) {
-  (void)argv;
-  if (argc != 2) {
-    chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
-    return;
-  }
-
-  while (chnGetTimeout((BaseChannel *)chp, 150) == Q_TIMEOUT) {
-    if (!strcmp (argv[0], "hygro")) {
-      if (!strcmp (argv[1], "raw")) {
-#if CHPRINTF_USE_ANSI_CODE
-        chprintf(chp, "\033[2J\033[1;1H");
-#endif
-        hygrometerReadRaw(&HTS221D1, rawdata);
-        chprintf(chp, "HTS221 Hygrometer raw data...\r\n");
-        chprintf(chp, "Raw humidity: %d\r\n", *rawdata);
-      }
-      else if (!strcmp (argv[1], "cooked")) {
-#if CHPRINTF_USE_ANSI_CODE
-        chprintf(chp, "\033[2J\033[1;1H");
-#endif
-        hygrometerReadCooked(&HTS221D1, cookeddata);
-        chprintf(chp, "HTS221 Hygrometer cooked data...\r\n");
-        chprintf(chp, "Cooked humidity: %.2f %%\r\n", *cookeddata);
-      }
-      else {
-        chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
-        return;
-      }
-    }
-    else if (!strcmp (argv[0], "thermo")) {
-      if (!strcmp (argv[1], "raw")) {
-#if CHPRINTF_USE_ANSI_CODE
-        chprintf(chp, "\033[2J\033[1;1H");
-#endif
-        thermometerReadRaw(&HTS221D1, rawdata);
-        chprintf(chp, "HTS221 Thermometer raw data...\r\n");
-        chprintf(chp, "Raw temperature: %d\r\n", *rawdata);
-      }
-      else if (!strcmp (argv[1], "cooked")) {
-#if CHPRINTF_USE_ANSI_CODE
-        chprintf(chp, "\033[2J\033[1;1H");
-#endif
-        thermometerReadCooked(&HTS221D1, cookeddata);
-        chprintf(chp, "HTS221 Thermometer cooked data...\r\n");
-        chprintf(chp, "Cooked temperature: %.2f �C\r\n", *cookeddata);
-      }
-      else {
-        chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
-        return;
-      }
-    }
-    else if (!strcmp (argv[0], "both")) {
-      if (!strcmp (argv[1], "raw")) {
-#if CHPRINTF_USE_ANSI_CODE
-        chprintf(chp, "\033[2J\033[1;1H");
-#endif
-        sensorReadRaw(&HTS221D1, rawdata);
-        chprintf(chp, "HTS221 Hygrometer raw data...\r\n");
-        chprintf(chp, "Raw humidity: %d\r\n", rawdata[0]);
-        chprintf(chp, "HTS221 Thermometer raw data...\r\n");
-        chprintf(chp, "Raw temperature: %d\r\n", rawdata[1]);
-      }
-      else if (!strcmp (argv[1], "cooked")) {
-#if CHPRINTF_USE_ANSI_CODE
-        chprintf(chp, "\033[2J\033[1;1H");
-#endif
-        sensorReadCooked(&HTS221D1, cookeddata);
-        chprintf(chp, "HTS221 Hygrometer cooked data...\r\n");
-        chprintf(chp, "Cooked humidity: %.2f %%\r\n", cookeddata[0]);
-        chprintf(chp, "HTS221 Thermometer cooked data...\r\n");
-        chprintf(chp, "Cooked temperature: %.2f �C\r\n", cookeddata[1]);
-      }
-      else {
-        chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
-        return;
-      }
-    }
-    else {
-      chprintf(chp, "Usage: read [hygro|thermo|both] [raw|cooked]\r\n");
-      return;
-    }
-  }
-  chprintf(chp, "Stopped\r\n");
-}
-
-static const ShellCommand commands[] = {
-  {"read", cmd_read},
-  {NULL, NULL}
-};
-
-static const ShellConfig shell_cfg1 = {
-  (BaseSequentialStream *)&SD2,
-  commands
-};
-
-/*===========================================================================*/
-/* Main code.                                                                */
+/* Generic code.                                                             */
 /*===========================================================================*/
 
+static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
 /*
  * LED blinker thread, times are in milliseconds.
  */
@@ -171,9 +70,7 @@ static THD_FUNCTION(Thread1, arg) {
   (void)arg;
   chRegSetThreadName("blinker");
   while (true) {
-    palClearPad(GPIOA, GPIOA_LED_GREEN);
-    chThdSleepMilliseconds(500);
-    palSetPad(GPIOA, GPIOA_LED_GREEN);
+    palToggleLine(LINE_LED_GREEN);
     chThdSleepMilliseconds(500);
   }
 }
@@ -193,43 +90,44 @@ int main(void) {
   halInit();
   chSysInit();
 
+  /* Configuring I2C SCK and I2C SDA related GPIOs .*/
   palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
                  PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
   palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
                  PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
 
-  /*
-   * Activates the serial driver 2 using the driver default configuration.
-   */
+  /* Activates the serial driver 1 using the driver default configuration.*/
   sdStart(&SD2, NULL);
 
-  /*
-   * Creates the blinker thread.
-   */
+  /* Creates the blinker thread.*/
   chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
 
-  /*
-   * HTS221 Object Initialization
-   */
+  /* HTS221 Object Initialization.*/
   hts221ObjectInit(&HTS221D1);
 
-  /*
-   * Activates the HTS221 driver.
-   */
+  /* Activates the HTS221 driver.*/
   hts221Start(&HTS221D1, &hts221cfg);
 
-  /*
-   * Shell manager initialization.
-   */
-  shellInit();
+  /* Normal main() thread activity, printing MEMS data on the SD1. */
+  while (true) {
+    hts221HygrometerReadRaw(&HTS221D1, &hygroraw);
+    chprintf(chp, "HTS221D1 Hygrometer raw data...\r\n");
+    chprintf(chp, "Hum: %d\r\n", hygroraw);
+
+    hts221ThermometerReadRaw(&HTS221D1, &thermoraw);
+    chprintf(chp, "HTS221D1 Thermometer raw data...\r\n");
+    chprintf(chp, "Temp: %d\r\n", &thermoraw);
+
+    hts221HygrometerReadCooked(&HTS221D1, &hygrocooked);
+    chprintf(chp, "HTS221D1 Hygrometer cooked data...\r\n");
+    chprintf(chp, "Hum: %.2f\r\n", hygrocooked);
 
-  while(TRUE) {
+    hts221ThermometerReadCooked(&HTS221D1, &thermocooked);
+    chprintf(chp, "HTS221D1 Thermometer cooked data...\r\n");
+    chprintf(chp, "Temp: %.2f\r\n", thermocooked);
 
-    thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
-                                            "shell", NORMALPRIO + 1,
-                                            shellThread, (void *)&shell_cfg1);
-    chThdWait(shelltp);                  /* Waiting termination.             */
-    chThdSleepMilliseconds(1000);
+    chThdSleepMilliseconds(100);
+    cls(chp);
   }
   hts221Stop(&HTS221D1);
 }
-- 
cgit v1.2.3