api-hal-i2c.h 2.2 KB

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