From 2843696682ad716511cb972d29766b694e18030e Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 30 Jun 2018 14:17:42 +0200 Subject: [PATCH] BMP over USB --- Inc/app.h | 4 +-- Inc/util.h | 14 +++++++++++ Src/bmp085.c | 67 ++++++++++++++++++++++++++++++++++++++++++--------- Src/main.c | 6 ++--- host/Makefile | 7 ++++-- host/test.c | 20 +++++++++------ 6 files changed, 90 insertions(+), 28 deletions(-) diff --git a/Inc/app.h b/Inc/app.h index acae0ae..18e58d1 100644 --- 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; diff --git a/Inc/util.h b/Inc/util.h index 827cc9c..0beed7c 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -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 diff --git a/Src/bmp085.c b/Src/bmp085.c index d150b90..9b241a6 100644 --- a/Src/bmp085.c +++ b/Src/bmp085.c @@ -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; } diff --git a/Src/main.c b/Src/main.c index e064ccf..0ff373e 100644 --- a/Src/main.c +++ b/Src/main.c @@ -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); diff --git a/host/Makefile b/host/Makefile index d4da827..a31dce1 100644 --- a/host/Makefile +++ b/host/Makefile @@ -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 diff --git a/host/test.c b/host/test.c index ce91851..6533c10 100644 --- a/host/test.c +++ b/host/test.c @@ -1,5 +1,9 @@ +#include +#include + #include #include +#include #include #include #include @@ -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); } -- 2.39.2