furi-hal-i2c.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. uint32_t time_left = timeout;
  45. bool ret = true;
  46. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c))
  47. ;
  48. LL_I2C_HandleTransfer(
  49. handle->bus->i2c,
  50. address,
  51. LL_I2C_ADDRSLAVE_7BIT,
  52. size,
  53. LL_I2C_MODE_AUTOEND,
  54. LL_I2C_GENERATE_START_WRITE);
  55. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  56. if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) {
  57. LL_I2C_TransmitData8(handle->bus->i2c, (*data));
  58. data++;
  59. size--;
  60. time_left = timeout;
  61. }
  62. if(LL_SYSTICK_IsActiveCounterFlag()) {
  63. if(--time_left == 0) {
  64. ret = false;
  65. break;
  66. }
  67. }
  68. }
  69. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  70. return ret;
  71. }
  72. bool furi_hal_i2c_rx(
  73. FuriHalI2cBusHandle* handle,
  74. uint8_t address,
  75. uint8_t* data,
  76. uint8_t size,
  77. uint32_t timeout) {
  78. furi_check(handle->bus->current_handle == handle);
  79. uint32_t time_left = timeout;
  80. bool ret = true;
  81. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c))
  82. ;
  83. LL_I2C_HandleTransfer(
  84. handle->bus->i2c,
  85. address,
  86. LL_I2C_ADDRSLAVE_7BIT,
  87. size,
  88. LL_I2C_MODE_AUTOEND,
  89. LL_I2C_GENERATE_START_READ);
  90. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  91. if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) {
  92. *data = LL_I2C_ReceiveData8(handle->bus->i2c);
  93. data++;
  94. size--;
  95. time_left = timeout;
  96. }
  97. if(LL_SYSTICK_IsActiveCounterFlag()) {
  98. if(--time_left == 0) {
  99. ret = false;
  100. break;
  101. }
  102. }
  103. }
  104. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  105. return ret;
  106. }
  107. bool furi_hal_i2c_trx(
  108. FuriHalI2cBusHandle* handle,
  109. uint8_t address,
  110. const uint8_t* tx_data,
  111. uint8_t tx_size,
  112. uint8_t* rx_data,
  113. uint8_t rx_size,
  114. uint32_t timeout) {
  115. if(furi_hal_i2c_tx(handle, address, tx_data, tx_size, timeout) &&
  116. furi_hal_i2c_rx(handle, address, rx_data, rx_size, timeout)) {
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }