From 47323abef3896f85d87aadf3d6ef849176cd44d8 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 30 Jun 2018 10:54:16 +0200 Subject: [PATCH] Whoa! Interrupt-driven I2C is working! --- Inc/app.h | 4 ++ Src/bmp085.c | 94 +++++++++++++--------------------------------- Src/stm32f1xx_it.c | 46 +++++++++++++++++++++++ 3 files changed, 76 insertions(+), 68 deletions(-) diff --git a/Inc/app.h b/Inc/app.h index 9eb2b0d..acae0ae 100644 --- a/Inc/app.h +++ b/Inc/app.h @@ -19,5 +19,9 @@ void tx_packet_send(void); extern s16 adjusted_temp; extern s16 adjusted_press; +extern volatile byte *bmp_i2c_ptr; +extern volatile byte bmp_i2c_len; +extern volatile byte bmp_i2c_addr; + void bmp_init(void); void bmp_step(void); diff --git a/Src/bmp085.c b/Src/bmp085.c index 76c6481..d150b90 100644 --- a/Src/bmp085.c +++ b/Src/bmp085.c @@ -4,82 +4,37 @@ #include "util.h" #include "app.h" +static byte bmp_i2c_buf[4]; +volatile byte *bmp_i2c_ptr; +volatile byte bmp_i2c_len; +volatile byte bmp_i2c_addr; + static uint bmp_read(uint reg, uint bytes) { + bmp_i2c_buf[0] = reg; + bmp_i2c_ptr = bmp_i2c_buf; + bmp_i2c_len = 1; + bmp_i2c_addr = 0xee; + LL_I2C_GenerateStartCondition(I2C1); - while (!LL_I2C_IsActiveFlag_SB(I2C1)) - ; - LL_I2C_TransmitData8(I2C1, 0xee); - while (!LL_I2C_IsActiveFlag_ADDR(I2C1)) - ; - LL_I2C_ClearFlag_ADDR(I2C1); - while (!LL_I2C_IsActiveFlag_TXE(I2C1)) - ; - LL_I2C_TransmitData8(I2C1, reg); - while (!LL_I2C_IsActiveFlag_TXE(I2C1)) + LL_I2C_EnableIT_TX(I2C1); + + while (bmp_i2c_len) ; - LL_I2C_GenerateStopCondition(I2C1); + + bmp_i2c_ptr = bmp_i2c_buf; + bmp_i2c_len = bytes; + bmp_i2c_addr = 0xef; LL_I2C_GenerateStartCondition(I2C1); - while (!LL_I2C_IsActiveFlag_SB(I2C1)) - ; - LL_I2C_DisableBitPOS(I2C1); - LL_I2C_TransmitData8(I2C1, 0xef); - while (!LL_I2C_IsActiveFlag_ADDR(I2C1)) + LL_I2C_EnableIT_RX(I2C1); + + while (bmp_i2c_len) ; - // This is quite tricky (see manual) uint d = 0; - if (bytes == 1) - { - LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK); - LL_I2C_ClearFlag_ADDR(I2C1); - LL_I2C_GenerateStopCondition(I2C1); - while (!LL_I2C_IsActiveFlag_RXNE(I2C1)) - ; - d = (d << 8) | LL_I2C_ReceiveData8(I2C1); - } - else if (bytes == 2) - { - LL_I2C_EnableBitPOS(I2C1); - LL_I2C_ClearFlag_ADDR(I2C1); - LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK); - while (!LL_I2C_IsActiveFlag_BTF(I2C1)) - ; - LL_I2C_GenerateStopCondition(I2C1); - for (uint i=0; i<2; i++) - d = (d << 8) | LL_I2C_ReceiveData8(I2C1); - } - else - { - LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK); - LL_I2C_ClearFlag_ADDR(I2C2); - - uint d = 0; - for (uint i=0; iSR1; + if (sr1 & I2C_SR1_SB) + LL_I2C_TransmitData8(I2C1, bmp_i2c_addr); + else if (bmp_i2c_addr & 1) + { + // Receive + if (sr1 & I2C_SR1_ADDR) + { + LL_I2C_DisableBitPOS(I2C1); + if (bmp_i2c_len == 1) + LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK); + LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK); + LL_I2C_ClearFlag_ADDR(I2C1); + } + else if (sr1 & I2C_SR1_RXNE) + { + if (bmp_i2c_len > 0) + { + if (bmp_i2c_len == 1) + { + LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK); + LL_I2C_GenerateStopCondition(I2C1); + } + *bmp_i2c_ptr++ = LL_I2C_ReceiveData8(I2C1); + bmp_i2c_len--; + } + else + LL_I2C_DisableIT_RX(I2C1); + } + } + else + { + // Transmit + if (sr1 & I2C_SR1_ADDR) + LL_I2C_ClearFlag_ADDR(I2C1); + else if (sr1 & I2C_SR1_TXE) + { + if (bmp_i2c_len) + { + LL_I2C_TransmitData8(I2C1, *bmp_i2c_ptr++); + bmp_i2c_len--; + } + else + LL_I2C_DisableIT_TX(I2C1); + } + } /* USER CODE END I2C1_EV_IRQn 0 */ -- 2.39.2