furi_hal_i2c.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <assert.h>
  7. void furi_hal_i2c_init() {
  8. furi_hal_i2c_bus_power.callback(&furi_hal_i2c_bus_power, FuriHalI2cBusEventInit);
  9. }
  10. void furi_hal_i2c_acquire(FuriHalI2cBusHandle* handle) {
  11. handle->bus->callback(handle->bus, FuriHalI2cBusEventLock);
  12. assert(handle->bus->current_handle == NULL);
  13. handle->bus->current_handle = handle;
  14. handle->bus->callback(handle->bus, FuriHalI2cBusEventActivate);
  15. handle->callback(handle, FuriHalI2cBusHandleEventActivate);
  16. }
  17. void furi_hal_i2c_release(FuriHalI2cBusHandle* handle) {
  18. assert(handle->bus->current_handle == handle);
  19. handle->callback(handle, FuriHalI2cBusHandleEventDeactivate);
  20. handle->bus->callback(handle->bus, FuriHalI2cBusEventDeactivate);
  21. handle->bus->current_handle = NULL;
  22. handle->bus->callback(handle->bus, FuriHalI2cBusEventUnlock);
  23. }
  24. bool furi_hal_i2c_tx(
  25. FuriHalI2cBusHandle* handle,
  26. uint8_t address,
  27. const uint8_t* data,
  28. uint8_t size,
  29. uint32_t timeout) {
  30. assert(handle->bus->current_handle == handle);
  31. uint32_t time_left = timeout;
  32. bool ret = true;
  33. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c))
  34. ;
  35. LL_I2C_HandleTransfer(
  36. handle->bus->i2c,
  37. address,
  38. LL_I2C_ADDRSLAVE_7BIT,
  39. size,
  40. LL_I2C_MODE_AUTOEND,
  41. LL_I2C_GENERATE_START_WRITE);
  42. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  43. if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) {
  44. LL_I2C_TransmitData8(handle->bus->i2c, (*data));
  45. data++;
  46. size--;
  47. time_left = timeout;
  48. }
  49. if(LL_SYSTICK_IsActiveCounterFlag()) {
  50. if(--time_left == 0) {
  51. ret = false;
  52. break;
  53. }
  54. }
  55. }
  56. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  57. return ret;
  58. }
  59. bool furi_hal_i2c_rx(
  60. FuriHalI2cBusHandle* handle,
  61. uint8_t address,
  62. uint8_t* data,
  63. uint8_t size,
  64. uint32_t timeout) {
  65. assert(handle->bus->current_handle == handle);
  66. uint32_t time_left = timeout;
  67. bool ret = true;
  68. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c))
  69. ;
  70. LL_I2C_HandleTransfer(
  71. handle->bus->i2c,
  72. address,
  73. LL_I2C_ADDRSLAVE_7BIT,
  74. size,
  75. LL_I2C_MODE_AUTOEND,
  76. LL_I2C_GENERATE_START_READ);
  77. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  78. if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) {
  79. *data = LL_I2C_ReceiveData8(handle->bus->i2c);
  80. data++;
  81. size--;
  82. time_left = timeout;
  83. }
  84. if(LL_SYSTICK_IsActiveCounterFlag()) {
  85. if(--time_left == 0) {
  86. ret = false;
  87. break;
  88. }
  89. }
  90. }
  91. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  92. return ret;
  93. }
  94. bool furi_hal_i2c_trx(
  95. FuriHalI2cBusHandle* handle,
  96. uint8_t address,
  97. const uint8_t* tx_data,
  98. uint8_t tx_size,
  99. uint8_t* rx_data,
  100. uint8_t rx_size,
  101. uint32_t timeout) {
  102. if(furi_hal_i2c_tx(handle, address, tx_data, tx_size, timeout) &&
  103. furi_hal_i2c_rx(handle, address, rx_data, rx_size, timeout)) {
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }