]> mj.ucw.cz Git - home-hw.git/commitdiff
BMP over USB
authorMartin Mares <mj@ucw.cz>
Sat, 30 Jun 2018 12:17:42 +0000 (14:17 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 30 Jun 2018 12:17:42 +0000 (14:17 +0200)
Inc/app.h
Inc/util.h
Src/bmp085.c
Src/main.c
host/Makefile
host/test.c

index acae0ae4800437aa238b8edbd9875574581a94f3..18e58d192615a8e3a54a9c6aef2de4d05701386b 100644 (file)
--- a/Inc/app.h
+++ b/Inc/app.h
@@ -16,8 +16,8 @@ void tx_packet_send(void);
 
 // bmp085.c
 
-extern s16 adjusted_temp;
-extern s16 adjusted_press;
+extern int adjusted_temp;
+extern int adjusted_press;
 
 extern volatile byte *bmp_i2c_ptr;
 extern volatile byte bmp_i2c_len;
index 827cc9c4a09082cf23336f38c7de54c5b3de9ace..0beed7c87232f5748061bc26b52585eb2524b8ea 100644 (file)
@@ -22,6 +22,20 @@ static inline void put_u16_le(byte *p, u16 x)
   p[1] = x >> 8;
 }
 
+static inline void put_u16_be(byte *p, u16 x)
+{
+  p[0] = x >> 8;
+  p[1] = x;
+}
+
+static inline void put_u32_be(byte *p, u32 x)
+{
+  p[0] = x >> 24;
+  p[1] = (x >> 16) & 0xff;
+  p[2] = (x >> 8) & 0xff;
+  p[3] = x & 0xff;
+}
+
 // debug.c
 
 #define DEBUG_SEMIHOSTING
index d150b90fb5cfdd25faee56be2935321464184848..9b241a6f068bb1961c01e8c9a0c986cd27c20a80 100644 (file)
@@ -4,6 +4,15 @@
 #include "util.h"
 #include "app.h"
 
+#undef TEST_VECTOR
+
+#if 0
+#define bmp_debug debug_printf
+#else
+static inline void bmp_debug(char *msg, ...)
+{ }
+#endif
+
 static byte bmp_i2c_buf[4];
 volatile byte *bmp_i2c_ptr;
 volatile byte bmp_i2c_len;
@@ -64,7 +73,7 @@ static void bmp_start_measure(uint type)
 }
 
 // Formulae from BMP085 specs
-static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], s16 *tt, s16 *pp)
+static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
 {
   s16 AC1 = cc[0];
   s16 AC2 = cc[1];
@@ -74,7 +83,6 @@ static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], s16 *tt, s16 *pp)
   u16 AC6 = cc[5];
   s16 B1 = cc[6];
   s16 B2 = cc[7];
-  // FIXME: Why is this unused?
   // s16 MB = cc[8];
   s16 MC = cc[9];
   s16 MD = cc[10];
@@ -108,17 +116,45 @@ static void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], s16 *tt, s16 *pp)
   *pp = p;
 }
 
+#ifdef TEST_VECTOR
+
+#define BMP_OSS 0
+static u16 bmp_constants[11] = {
+  408,
+  -72,
+  -14383,
+  32741,
+  32757,
+  23153,
+  6190,
+  4,
+  32768,
+  -8711,
+  2868,
+};
+
+void bmp_init(void)
+{
+  bmp_debug("BMP: Test constants\n");
+}
+
+#else
+
+#define BMP_OSS 3
 static u16 bmp_constants[11];
 
 void bmp_init(void)
 {
+  bmp_debug("BMP: Reading constants\n");
   for (uint i=0; i<11; i++)
     {
       bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
-      debug_printf("BMP: const[%d] = %u\n", i, bmp_constants[i]);
+      bmp_debug("BMP: const[%d] = %04x\n", i, bmp_constants[i]);
     }
 }
 
+#endif
+
 enum bmp_state {
   BMP_IDLE,
   BMP_TEMP,
@@ -128,36 +164,43 @@ enum bmp_state {
 static byte bmp_state = BMP_IDLE;
 static u16 raw_temp;
 static u32 raw_press;
-#define BMP_OSS 3
-s16 adjusted_temp;
-s16 adjusted_press;
+int adjusted_temp;
+int adjusted_press;
 
 void bmp_step(void)
 {
   switch (bmp_state)
     {
     case BMP_IDLE:
-      debug_puts("BMP: Start measure\n");
+      bmp_debug("BMP: Start measure\n");
       bmp_start_measure(0x2e);
       bmp_state++;
       break;
     case BMP_TEMP:
       if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
        return;
-      debug_puts("BMP: Temperature measured\n");
+      bmp_debug("BMP: Temperature measured\n");
+#ifdef TEST_VECTOR
+      raw_temp = 27898;
+#else
       raw_temp = bmp_read(0xf6, 2);
-      debug_printf("BMP: Temperature read: %u\n", raw_temp);
+#endif
+      bmp_debug("BMP: Raw temperature: %04x\n", raw_temp);
       bmp_start_measure(0xf4 | (BMP_OSS<<6));
       bmp_state++;
       break;
     case BMP_PRESSURE:
       if (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
        return;
-      debug_puts("BMP: Pressure measured\n");
+      bmp_debug("BMP: Pressure measured\n");
+#ifdef TEST_VECTOR
+      raw_press = 23843 << 8;
+#else
       raw_press = bmp_read(0xf6, 3);
-      debug_printf("BMP: Pressure read: %u\n", raw_press);
+#endif
+      bmp_debug("BMP: Raw pressure: %06x\n", raw_press);
       bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
-      debug_printf("BMP: Adjusted temp %u, press %u\n", adjusted_temp, adjusted_press);
+      bmp_debug("BMP: Adjusted temp %d, press %d\n", adjusted_temp, adjusted_press);
       bmp_state = BMP_IDLE;
       break;
     }
index e064ccff55cb1b65bde196ce6d6f489ef914b35c..0ff373e6635e5ab6c23bbb31f3fc9f8ac678c197 100644 (file)
@@ -155,10 +155,8 @@ int main(void)
 
        // display_buffer(rx_packet);
        tx_packet_state = 1;
-       tx_packet[0] = adjusted_temp >> 8;
-       tx_packet[1] = adjusted_temp & 0xff;
-       tx_packet[2] = adjusted_press >> 8;
-       tx_packet[3] = adjusted_press & 0xff;
+       put_u32_be(tx_packet, adjusted_temp);
+       put_u32_be(tx_packet + 4, adjusted_press);
        usb_ep_send(&usb, 0x82, tx_packet, 8);
        rx_packet_state = 0;
        usb_ep_receive(&usb, 0x01, rx_packet, 64);
index d4da827561aa33511301db6c6018aa4b9bf19306..a31dce131d6efbb2b7857072f8d0563f6fb6b2dd 100644 (file)
@@ -1,5 +1,8 @@
-CFLAGS=-std=gnu99 -O2 -Wall -Wextra -Wno-parentheses
-LDLIBS=-lusb-1.0
+UCWCF:=$(shell PKG_CONFIG_PATH=$(LIBUCW)/lib/pkgconfig pkg-config --cflags libucw)
+UCWLF:=$(shell PKG_CONFIG_PATH=$(LIBUCW)/lib/pkgconfig pkg-config --libs libucw)
+
+CFLAGS=-std=gnu99 -O2 -Wall -Wextra -Wno-parentheses $(UCWCF)
+LDLIBS=-lusb-1.0 $(UCWLF)
 
 all: test
 
index ce9185196e03dc042970d626df880d96416ed88e..6533c1004e1f488802cd4b9edcb108de2ff5e83c 100644 (file)
@@ -1,5 +1,9 @@
+#include <ucw/lib.h>
+#include <ucw/unaligned.h>
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <unistd.h>
 #include <time.h>
 #include <libusb-1.0/libusb.h>
@@ -75,20 +79,20 @@ int main(void)
       };
       int transferred;
       if (err = libusb_bulk_transfer(devh, 0x01, req, 8, &transferred, 2000))
-       {
-         fprintf(stderr, "Transfer failed: error %d\n", err);
-         exit(1);
-       }
-      printf("Transferred %d bytes\n", transferred);
+       die("Transfer failed: error %d\n", err);
+      // printf("Transferred %d bytes\n", transferred);
 
       unsigned char resp[64];
       int received;
       if (err = libusb_bulk_transfer(devh, 0x82, resp, 64, &received, 2000))
+       die("Receive failed: error %d\n", err);
+      // printf("Received %d bytes\n", received);
+      if (received >= 8)
        {
-         fprintf(stderr, "Receive failed: error %d\n", err);
-         exit(1);
+         int t = get_u32_be(resp);
+         int p = get_u32_be(resp + 4);
+         msg(L_INFO, "Temperature %d ddegC, pressure %d Pa", t, p);
        }
-      printf("Received %d bytes [%02x]\n", received, resp[0]);
 
       sleep(1);
     }