furi-hal-i2c.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @file furi-hal-i2c.h
  3. * I2C HAL API
  4. */
  5. #pragma once
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <furi-hal-resources.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /** Init I2C
  13. */
  14. void furi_hal_i2c_init();
  15. /** Perform I2C tx transfer
  16. *
  17. * @param instance I2C_TypeDef instance
  18. * @param address I2C slave address
  19. * @param data pointer to data buffer
  20. * @param size size of data buffer
  21. * @param timeout timeout in CPU ticks
  22. *
  23. * @return true on successful transfer, false otherwise
  24. */
  25. bool furi_hal_i2c_tx(
  26. I2C_TypeDef* instance,
  27. const uint8_t address,
  28. const uint8_t* data,
  29. const uint8_t size,
  30. uint32_t timeout);
  31. /** Perform I2C rx transfer
  32. *
  33. * @param instance I2C_TypeDef instance
  34. * @param address I2C slave address
  35. * @param data pointer to data buffer
  36. * @param size size of data buffer
  37. * @param timeout timeout in CPU ticks
  38. *
  39. * @return true on successful transfer, false otherwise
  40. */
  41. bool furi_hal_i2c_rx(
  42. I2C_TypeDef* instance,
  43. const uint8_t address,
  44. uint8_t* data,
  45. const uint8_t size,
  46. uint32_t timeout);
  47. /** Perform I2C tx and rx transfers
  48. *
  49. * @param instance I2C_TypeDef instance
  50. * @param address I2C slave address
  51. * @param tx_data pointer to tx data buffer
  52. * @param tx_size size of tx data buffer
  53. * @param rx_data pointer to rx data buffer
  54. * @param rx_size size of rx data buffer
  55. * @param timeout timeout in CPU ticks
  56. *
  57. * @return true on successful transfer, false otherwise
  58. */
  59. bool furi_hal_i2c_trx(
  60. I2C_TypeDef* instance,
  61. const uint8_t address,
  62. const uint8_t* tx_data,
  63. const uint8_t tx_size,
  64. uint8_t* rx_data,
  65. const uint8_t rx_size,
  66. uint32_t timeout);
  67. /** Acquire I2C mutex
  68. */
  69. void furi_hal_i2c_lock();
  70. /** Release I2C mutex
  71. */
  72. void furi_hal_i2c_unlock();
  73. /** With clause for I2C peripheral
  74. *
  75. * @param type type of function_body
  76. * @param pointer pointer to return of function_body
  77. * @param function_body a (){} lambda declaration, executed with I2C mutex
  78. * acquired
  79. *
  80. * @return Nothing
  81. */
  82. #define with_furi_hal_i2c(type, pointer, function_body) \
  83. { \
  84. furi_hal_i2c_lock(); \
  85. *pointer = ({ type __fn__ function_body __fn__; })(); \
  86. furi_hal_i2c_unlock(); \
  87. }
  88. #ifdef __cplusplus
  89. }
  90. #endif