- 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);
- }
- }