#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; i<bytes; i++)
- {
- if (i+3 < bytes || i+1 == bytes)
- {
-#if 0
- while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
- ;
-#endif
- }
- else if (i+3 == bytes)
- {
- while (!LL_I2C_IsActiveFlag_BTF(I2C1))
- ;
- LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_NACK);
- }
- else if (i+2 == bytes)
- {
- while (!LL_I2C_IsActiveFlag_BTF(I2C1))
- ;
- LL_I2C_GenerateStopCondition(I2C1);
- }
- d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
- }
- }
+ for (uint i=0; i<bytes; i++)
+ d = (d << 8) | bmp_i2c_buf[i];
return d;
}
void bmp_init(void)
{
for (uint i=0; i<11; i++)
- bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
+ {
+ bmp_constants[i] = bmp_read(0xaa + 2*i, 2);
+ debug_printf("BMP: const[%d] = %u\n", i, bmp_constants[i]);
+ }
}
enum bmp_state {
void I2C1_EV_IRQHandler(void)
{
/* USER CODE BEGIN I2C1_EV_IRQn 0 */
+ u32 sr1 = I2C1->SR1;
+ 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 */