// 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;
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
#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;
}
// 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];
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];
*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,
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;
}
// 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);
-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
+#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>
};
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);
}