furi_hal_i2c.c 8.8 KB

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