From: Martin Mares Date: Tue, 26 Jun 2018 22:56:05 +0000 (+0200) Subject: BMP partial X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=a660e78ffe99e98c4708f1dda166a47553bf9240;p=home-hw.git BMP partial --- diff --git a/Inc/app.h b/Inc/app.h index b125b58..9eb2b0d 100644 --- a/Inc/app.h +++ b/Inc/app.h @@ -16,4 +16,8 @@ void tx_packet_send(void); // bmp085.c +extern s16 adjusted_temp; +extern s16 adjusted_press; + void bmp_init(void); +void bmp_step(void); diff --git a/Src/bmp085.c b/Src/bmp085.c index 4df07c2..76f548f 100644 --- a/Src/bmp085.c +++ b/Src/bmp085.c @@ -12,7 +12,7 @@ static uint bmp_read(uint reg, uint bytes) LL_I2C_TransmitData8(I2C1, 0xee); while (!LL_I2C_IsActiveFlag_ADDR(I2C1)) ; - LL_I2C_ClearFlag_ADDR(I2C2); + LL_I2C_ClearFlag_ADDR(I2C1); while (!LL_I2C_IsActiveFlag_TXE(I2C1)) ; LL_I2C_TransmitData8(I2C1, reg); @@ -27,23 +27,56 @@ static uint bmp_read(uint reg, uint bytes) LL_I2C_TransmitData8(I2C1, 0xef); while (!LL_I2C_IsActiveFlag_ADDR(I2C1)) ; - LL_I2C_ClearFlag_ADDR(I2C2); + // This is quite tricky (see manual) uint d = 0; - for (uint i=0; i>= (8-oss); @@ -124,30 +153,43 @@ void bmp_init(void) bmp_constants[i] = bmp_read(0xaa + 2*i, 2); } -#if 0 -void run_test(void) -{ - for (;;) - { - debug_puts("Constants:"); - u16 cc[11]; - for (uint i=0; i<11; i++) - { - cc[i] = bmp_read(0xaa + 2*i, 2); - debug_printf(" %04x", cc[i]); - } - debug_puts("\r\n"); - - uint raw_temp = bmp_measure(0x2e, 2); - debug_printf("Raw temperature: %04x\r\n", raw_temp); +enum bmp_state { + BMP_IDLE, + BMP_TEMP, + BMP_PRESSURE, +}; - uint oss = 3; // Over-sampling setting - uint raw_press = bmp_measure(0xf4 | (oss<<6), 3); - debug_printf("Raw pressure: %06x\r\n", raw_press); +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 temp, press; - bmp_recalc(raw_temp, raw_press, oss, cc, &temp, &press); - debug_printf("Temperature: %d ddegC\r\n", temp); - debug_printf("Pressure: %d Pa\r\n", press); +void bmp_step(void) +{ + switch (bmp_state) + { + case BMP_IDLE: + debug_puts("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 done\n"); + raw_press = bmp_read(0xf6, 2); + 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 done\n"); + raw_press = bmp_read(0xf6, 3); + bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press); + bmp_state = BMP_IDLE; + break; } -#endif +} diff --git a/Src/main.c b/Src/main.c index 043fe33..e2769f2 100644 --- a/Src/main.c +++ b/Src/main.c @@ -125,7 +125,7 @@ int main(void) /* USER CODE BEGIN 2 */ display_init(); usb_start(&usb); - // bmp_init(); + bmp_init(); LL_TIM_EnableCounter(TIM4); LL_TIM_EnableIT_UPDATE(TIM4); @@ -146,11 +146,17 @@ 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; usb_ep_send(&usb, 0x82, tx_packet, 8); rx_packet_state = 0; usb_ep_receive(&usb, 0x01, rx_packet, 64); } + bmp_step(); + // debug_printf("Counter = %d\n", cnt); // display_counter(cnt);