furi_hal_i2c.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include <furi_hal_i2c.h>
  2. #include <furi_hal_version.h>
  3. #include <furi_hal_power.h>
  4. #include <stm32wbxx_ll_i2c.h>
  5. #include <stm32wbxx_ll_gpio.h>
  6. #include <stm32wbxx_ll_cortex.h>
  7. #include <furi.h>
  8. #define TAG "FuriHalI2C"
  9. void furi_hal_i2c_init_early() {
  10. furi_hal_i2c_bus_power.callback(&furi_hal_i2c_bus_power, FuriHalI2cBusEventInit);
  11. }
  12. void furi_hal_i2c_deinit_early() {
  13. furi_hal_i2c_bus_power.callback(&furi_hal_i2c_bus_power, FuriHalI2cBusEventDeinit);
  14. }
  15. void furi_hal_i2c_init() {
  16. furi_hal_i2c_bus_external.callback(&furi_hal_i2c_bus_external, FuriHalI2cBusEventInit);
  17. FURI_LOG_I(TAG, "Init OK");
  18. }
  19. void furi_hal_i2c_acquire(FuriHalI2cBusHandle* handle) {
  20. furi_hal_power_insomnia_enter();
  21. // Lock bus access
  22. handle->bus->callback(handle->bus, FuriHalI2cBusEventLock);
  23. // Ensuree that no active handle set
  24. furi_check(handle->bus->current_handle == NULL);
  25. // Set current handle
  26. handle->bus->current_handle = handle;
  27. // Activate bus
  28. handle->bus->callback(handle->bus, FuriHalI2cBusEventActivate);
  29. // Activate handle
  30. handle->callback(handle, FuriHalI2cBusHandleEventActivate);
  31. }
  32. void furi_hal_i2c_release(FuriHalI2cBusHandle* handle) {
  33. // Ensure that current handle is our handle
  34. furi_check(handle->bus->current_handle == handle);
  35. // Deactivate handle
  36. handle->callback(handle, FuriHalI2cBusHandleEventDeactivate);
  37. // Deactivate bus
  38. handle->bus->callback(handle->bus, FuriHalI2cBusEventDeactivate);
  39. // Reset current handle
  40. handle->bus->current_handle = NULL;
  41. // Unlock bus
  42. handle->bus->callback(handle->bus, FuriHalI2cBusEventUnlock);
  43. furi_hal_power_insomnia_exit();
  44. }
  45. bool furi_hal_i2c_tx(
  46. FuriHalI2cBusHandle* handle,
  47. uint8_t address,
  48. const uint8_t* data,
  49. uint8_t size,
  50. uint32_t timeout) {
  51. furi_check(handle->bus->current_handle == handle);
  52. furi_assert(timeout > 0);
  53. bool ret = true;
  54. uint32_t timeout_tick = furi_get_tick() + timeout;
  55. do {
  56. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) {
  57. if(furi_get_tick() >= timeout_tick) {
  58. ret = false;
  59. break;
  60. }
  61. }
  62. if(!ret) {
  63. break;
  64. }
  65. LL_I2C_HandleTransfer(
  66. handle->bus->i2c,
  67. address,
  68. LL_I2C_ADDRSLAVE_7BIT,
  69. size,
  70. LL_I2C_MODE_AUTOEND,
  71. LL_I2C_GENERATE_START_WRITE);
  72. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  73. if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) {
  74. LL_I2C_TransmitData8(handle->bus->i2c, (*data));
  75. data++;
  76. size--;
  77. }
  78. if(furi_get_tick() >= timeout_tick) {
  79. ret = false;
  80. break;
  81. }
  82. }
  83. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  84. } while(0);
  85. return ret;
  86. }
  87. bool furi_hal_i2c_rx(
  88. FuriHalI2cBusHandle* handle,
  89. uint8_t address,
  90. uint8_t* data,
  91. uint8_t size,
  92. uint32_t timeout) {
  93. furi_check(handle->bus->current_handle == handle);
  94. furi_assert(timeout > 0);
  95. bool ret = true;
  96. uint32_t timeout_tick = furi_get_tick() + timeout;
  97. do {
  98. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) {
  99. if(furi_get_tick() >= timeout_tick) {
  100. ret = false;
  101. break;
  102. }
  103. }
  104. if(!ret) {
  105. break;
  106. }
  107. LL_I2C_HandleTransfer(
  108. handle->bus->i2c,
  109. address,
  110. LL_I2C_ADDRSLAVE_7BIT,
  111. size,
  112. LL_I2C_MODE_AUTOEND,
  113. LL_I2C_GENERATE_START_READ);
  114. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  115. if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) {
  116. *data = LL_I2C_ReceiveData8(handle->bus->i2c);
  117. data++;
  118. size--;
  119. }
  120. if(furi_get_tick() >= timeout_tick) {
  121. ret = false;
  122. break;
  123. }
  124. }
  125. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  126. } while(0);
  127. return ret;
  128. }
  129. bool furi_hal_i2c_trx(
  130. FuriHalI2cBusHandle* handle,
  131. uint8_t address,
  132. const uint8_t* tx_data,
  133. uint8_t tx_size,
  134. uint8_t* rx_data,
  135. uint8_t rx_size,
  136. uint32_t timeout) {
  137. if(furi_hal_i2c_tx(handle, address, tx_data, tx_size, timeout) &&
  138. furi_hal_i2c_rx(handle, address, rx_data, rx_size, timeout)) {
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. bool furi_hal_i2c_is_device_ready(FuriHalI2cBusHandle* handle, uint8_t i2c_addr, uint32_t timeout) {
  145. furi_check(handle);
  146. furi_check(handle->bus->current_handle == handle);
  147. furi_assert(timeout > 0);
  148. bool ret = true;
  149. uint32_t timeout_tick = furi_get_tick() + timeout;
  150. do {
  151. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) {
  152. if(furi_get_tick() >= timeout_tick) {
  153. return false;
  154. }
  155. }
  156. handle->bus->i2c->CR2 =
  157. ((((uint32_t)(i2c_addr) & (I2C_CR2_SADD)) | (I2C_CR2_START) | (I2C_CR2_AUTOEND)) &
  158. (~I2C_CR2_RD_WRN));
  159. while((!LL_I2C_IsActiveFlag_NACK(handle->bus->i2c)) &&
  160. (!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c))) {
  161. if(furi_get_tick() >= timeout_tick) {
  162. return false;
  163. }
  164. }
  165. if(LL_I2C_IsActiveFlag_NACK(handle->bus->i2c)) {
  166. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c)) {
  167. if(furi_get_tick() >= timeout_tick) {
  168. return false;
  169. }
  170. }
  171. LL_I2C_ClearFlag_NACK(handle->bus->i2c);
  172. // Clear STOP Flag generated by autoend
  173. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  174. // Generate actual STOP
  175. LL_I2C_GenerateStopCondition(handle->bus->i2c);
  176. ret = false;
  177. }
  178. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c)) {
  179. if(furi_get_tick() >= timeout_tick) {
  180. return false;
  181. }
  182. }
  183. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  184. } while(0);
  185. return ret;
  186. }
  187. bool furi_hal_i2c_read_reg_8(
  188. FuriHalI2cBusHandle* handle,
  189. uint8_t i2c_addr,
  190. uint8_t reg_addr,
  191. uint8_t* data,
  192. uint32_t timeout) {
  193. furi_check(handle);
  194. return furi_hal_i2c_trx(handle, i2c_addr, &reg_addr, 1, data, 1, timeout);
  195. }
  196. bool furi_hal_i2c_read_reg_16(
  197. FuriHalI2cBusHandle* handle,
  198. uint8_t i2c_addr,
  199. uint8_t reg_addr,
  200. uint16_t* data,
  201. uint32_t timeout) {
  202. furi_check(handle);
  203. uint8_t reg_data[2];
  204. bool ret = furi_hal_i2c_trx(handle, i2c_addr, &reg_addr, 1, reg_data, 2, timeout);
  205. *data = (reg_data[0] << 8) | (reg_data[1]);
  206. return ret;
  207. }
  208. bool furi_hal_i2c_read_mem(
  209. FuriHalI2cBusHandle* handle,
  210. uint8_t i2c_addr,
  211. uint8_t mem_addr,
  212. uint8_t* data,
  213. uint8_t len,
  214. uint32_t timeout) {
  215. furi_check(handle);
  216. return furi_hal_i2c_trx(handle, i2c_addr, &mem_addr, 1, data, len, timeout);
  217. }
  218. bool furi_hal_i2c_write_reg_8(
  219. FuriHalI2cBusHandle* handle,
  220. uint8_t i2c_addr,
  221. uint8_t reg_addr,
  222. uint8_t data,
  223. uint32_t timeout) {
  224. furi_check(handle);
  225. uint8_t tx_data[2];
  226. tx_data[0] = reg_addr;
  227. tx_data[1] = data;
  228. return furi_hal_i2c_tx(handle, i2c_addr, (const uint8_t*)&tx_data, 2, timeout);
  229. }
  230. bool furi_hal_i2c_write_reg_16(
  231. FuriHalI2cBusHandle* handle,
  232. uint8_t i2c_addr,
  233. uint8_t reg_addr,
  234. uint16_t data,
  235. uint32_t timeout) {
  236. furi_check(handle);
  237. uint8_t tx_data[3];
  238. tx_data[0] = reg_addr;
  239. tx_data[1] = (data >> 8) & 0xFF;
  240. tx_data[2] = data & 0xFF;
  241. return furi_hal_i2c_tx(handle, i2c_addr, (const uint8_t*)&tx_data, 3, timeout);
  242. }
  243. bool furi_hal_i2c_write_mem(
  244. FuriHalI2cBusHandle* handle,
  245. uint8_t i2c_addr,
  246. uint8_t mem_addr,
  247. uint8_t* data,
  248. uint8_t len,
  249. uint32_t timeout) {
  250. furi_check(handle);
  251. furi_check(handle->bus->current_handle == handle);
  252. furi_assert(timeout > 0);
  253. bool ret = true;
  254. uint8_t size = len + 1;
  255. uint32_t timeout_tick = furi_get_tick() + timeout;
  256. do {
  257. while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) {
  258. if(furi_get_tick() >= timeout_tick) {
  259. ret = false;
  260. break;
  261. }
  262. }
  263. if(!ret) {
  264. break;
  265. }
  266. LL_I2C_HandleTransfer(
  267. handle->bus->i2c,
  268. i2c_addr,
  269. LL_I2C_ADDRSLAVE_7BIT,
  270. size,
  271. LL_I2C_MODE_AUTOEND,
  272. LL_I2C_GENERATE_START_WRITE);
  273. while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) {
  274. if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) {
  275. if(size == len + 1) {
  276. LL_I2C_TransmitData8(handle->bus->i2c, mem_addr);
  277. } else {
  278. LL_I2C_TransmitData8(handle->bus->i2c, (*data));
  279. data++;
  280. }
  281. size--;
  282. }
  283. if(furi_get_tick() >= timeout_tick) {
  284. ret = false;
  285. break;
  286. }
  287. }
  288. LL_I2C_ClearFlag_STOP(handle->bus->i2c);
  289. } while(0);
  290. return ret;
  291. }