furi-hal-i2c.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <furi-hal-i2c.h>
  2. #include <furi-hal-version.h>
  3. #include <stm32wbxx_ll_i2c.h>
  4. #include <stm32wbxx_ll_gpio.h>
  5. #include <stm32wbxx_ll_cortex.h>
  6. #include <furi.h>
  7. #define TAG "FuriHalI2C"
  8. void furi_hal_i2c_init() {
  9. furi_hal_i2c_bus_power.callback(&furi_hal_i2c_bus_power, FuriHalI2cBusEventInit);
  10. furi_hal_i2c_bus_external.callback(&furi_hal_i2c_bus_external, FuriHalI2cBusEventInit);
  11. FURI_LOG_I(TAG, "Init OK");
  12. }
  13. void furi_hal_i2c_acquire(FuriHalI2cBusHandle* handle) {
  14. // Lock bus access
  15. handle->bus->callback(handle->bus, FuriHalI2cBusEventLock);
  16. // Ensuree that no active handle set
  17. furi_check(handle->bus->current_handle == NULL);
  18. // Set current handle
  19. handle->bus->current_handle = handle;
  20. // Activate bus
  21. handle->bus->callback(handle->bus, FuriHalI2cBusEventActivate);
  22. // Activate handle
  23. handle->callback(handle, FuriHalI2cBusHandleEventActivate);
  24. }
  25. void furi_hal_i2c_release(FuriHalI2cBusHandle* handle) {
  26. // Ensure that current handle is our handle
  27. furi_check(handle->bus->current_handle == handle);
  28. // Deactivate handle
  29. handle->callback(handle, FuriHalI2cBusHandleEventDeactivate);
  30. // Deactivate bus
  31. handle->bus->callback(handle->bus, FuriHalI2cBusEventDeactivate);
  32. // Reset current handle
  33. handle->bus->current_handle = NULL;
  34. // Unlock bus
  35. handle->bus->callback(handle->bus, FuriHalI2cBusEventUnlock);
  36. }
  37. bool furi_hal_i2c_tx(
  38. FuriHalI2cBusHandle* handle,
  39. uint8_t address,
  40. const uint8_t* data,
  41. uint8_t size,
  42. uint32_t timeout) {
  43. furi_check(handle->bus->current_handle == handle);
  44. furi_assert(timeout > 0);
  45. bool ret = true;
  46. uint32_t timeout_tick = HAL_GetTick() + timeout;
  47. do {
  48. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) {
  49. if(HAL_GetTick() >= timeout_tick) {
  50. ret = false;
  51. break;
  52. }
  53. }
  54. if(!ret) {
  55. break;
  56. }
  57. LL_I2C_HandleTransfer(
  58. handle->bus->i2c,
  59. address,
  60. LL_I2C_ADDRSLAVE_7BIT,
  61. size,
  62. LL_I2C_MODE_AUTOEND,
  63. LL_I2C_GENERATE_START_WRITE);
  64. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  65. if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) {
  66. LL_I2C_TransmitData8(handle->bus->i2c, (*data));
  67. data++;
  68. size--;
  69. }
  70. if(HAL_GetTick() >= timeout_tick) {
  71. ret = false;
  72. break;
  73. }
  74. }
  75. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  76. } while(0);
  77. return ret;
  78. }
  79. bool furi_hal_i2c_rx(
  80. FuriHalI2cBusHandle* handle,
  81. uint8_t address,
  82. uint8_t* data,
  83. uint8_t size,
  84. uint32_t timeout) {
  85. furi_check(handle->bus->current_handle == handle);
  86. furi_assert(timeout > 0);
  87. bool ret = true;
  88. uint32_t timeout_tick = HAL_GetTick() + timeout;
  89. do {
  90. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) {
  91. if(HAL_GetTick() >= timeout_tick) {
  92. ret = false;
  93. break;
  94. }
  95. }
  96. if(!ret) {
  97. break;
  98. }
  99. LL_I2C_HandleTransfer(
  100. handle->bus->i2c,
  101. address,
  102. LL_I2C_ADDRSLAVE_7BIT,
  103. size,
  104. LL_I2C_MODE_AUTOEND,
  105. LL_I2C_GENERATE_START_READ);
  106. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  107. if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) {
  108. *data = LL_I2C_ReceiveData8(handle->bus->i2c);
  109. data++;
  110. size--;
  111. }
  112. if(HAL_GetTick() >= timeout_tick) {
  113. ret = false;
  114. break;
  115. }
  116. }
  117. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  118. } while(0);
  119. return ret;
  120. }
  121. bool furi_hal_i2c_trx(
  122. FuriHalI2cBusHandle* handle,
  123. uint8_t address,
  124. const uint8_t* tx_data,
  125. uint8_t tx_size,
  126. uint8_t* rx_data,
  127. uint8_t rx_size,
  128. uint32_t timeout) {
  129. if(furi_hal_i2c_tx(handle, address, tx_data, tx_size, timeout) &&
  130. furi_hal_i2c_rx(handle, address, rx_data, rx_size, timeout)) {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }