furi_hal_i2c.c 9.0 KB

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