furi_hal_i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. }
  109. bool furi_hal_i2c_read_reg_8(
  110. FuriHalI2cBusHandle* handle,
  111. uint8_t i2c_addr,
  112. uint8_t reg_addr,
  113. uint8_t* data,
  114. uint32_t timeout) {
  115. assert(handle);
  116. return furi_hal_i2c_trx(handle, i2c_addr, &reg_addr, 1, data, 1, timeout);
  117. }
  118. bool furi_hal_i2c_read_reg_16(
  119. FuriHalI2cBusHandle* handle,
  120. uint8_t i2c_addr,
  121. uint8_t reg_addr,
  122. uint16_t* data,
  123. uint32_t timeout) {
  124. assert(handle);
  125. uint8_t reg_data[2];
  126. bool ret = furi_hal_i2c_trx(handle, i2c_addr, &reg_addr, 1, reg_data, 2, timeout);
  127. *data = (reg_data[0] << 8) | (reg_data[1]);
  128. return ret;
  129. }
  130. bool furi_hal_i2c_read_mem(
  131. FuriHalI2cBusHandle* handle,
  132. uint8_t i2c_addr,
  133. uint8_t mem_addr,
  134. uint8_t* data,
  135. uint8_t len,
  136. uint32_t timeout) {
  137. assert(handle);
  138. return furi_hal_i2c_trx(handle, i2c_addr, &mem_addr, 1, data, len, timeout);
  139. }
  140. bool furi_hal_i2c_write_reg_8(
  141. FuriHalI2cBusHandle* handle,
  142. uint8_t i2c_addr,
  143. uint8_t reg_addr,
  144. uint8_t data,
  145. uint32_t timeout) {
  146. assert(handle);
  147. uint8_t tx_data[2];
  148. tx_data[0] = reg_addr;
  149. tx_data[1] = data;
  150. return furi_hal_i2c_tx(handle, i2c_addr, (const uint8_t*)&tx_data, 2, timeout);
  151. }
  152. bool furi_hal_i2c_write_reg_16(
  153. FuriHalI2cBusHandle* handle,
  154. uint8_t i2c_addr,
  155. uint8_t reg_addr,
  156. uint16_t data,
  157. uint32_t timeout) {
  158. assert(handle);
  159. uint8_t tx_data[3];
  160. tx_data[0] = reg_addr;
  161. tx_data[1] = (data >> 8) & 0xFF;
  162. tx_data[2] = data & 0xFF;
  163. return furi_hal_i2c_tx(handle, i2c_addr, (const uint8_t*)&tx_data, 3, timeout);
  164. }