lib_i2c.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. ******************************************************************************
  3. * @file lib_i2c.h
  4. * @author Application Team
  5. * @version V4.5.0
  6. * @date 2019-05-14
  7. * @brief IIC library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #ifndef __LIB_I2C_H
  14. #define __LIB_I2C_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "target.h"
  19. typedef struct
  20. {
  21. uint32_t SlaveAddr;
  22. uint32_t GeneralCallAck;
  23. uint32_t AssertAcknowledge;
  24. uint32_t ClockSource;
  25. } I2C_InitType;
  26. //GeneralCallAck
  27. #define I2C_GENERALCALLACK_ENABLE I2C_ADDR_GC
  28. #define I2C_GENERALCALLACK_DISABLE 0
  29. //AssertAcknowledge
  30. #define I2C_ASSERTACKNOWLEDGE_ENABLE I2C_CTRL_AA
  31. #define I2C_ASSERTACKNOWLEDGE_DISABLE 0
  32. //ClockSource
  33. #define I2C_CLOCKSOURCE_APBD256 I2C_CTRL_CR_0
  34. #define I2C_CLOCKSOURCE_APBD224 I2C_CTRL_CR_1
  35. #define I2C_CLOCKSOURCE_APBD192 I2C_CTRL_CR_2
  36. #define I2C_CLOCKSOURCE_APBD160 I2C_CTRL_CR_3
  37. #define I2C_CLOCKSOURCE_APBD960 I2C_CTRL_CR_4
  38. #define I2C_CLOCKSOURCE_APBD120 I2C_CTRL_CR_5
  39. #define I2C_CLOCKSOURCE_APBD60 I2C_CTRL_CR_6
  40. #define I2C_CLOCKSOURCE_TIM3OFD8 I2C_CTRL_CR_7
  41. typedef struct
  42. {
  43. uint16_t SlaveAddr;
  44. uint8_t SubAddrType;
  45. uint32_t PageRange;
  46. uint32_t SubAddress;
  47. uint8_t *pBuffer;
  48. uint32_t Length;
  49. } I2C_WRType;
  50. //SubAddrType
  51. #define I2C_SUBADDR_1BYTE 1
  52. #define I2C_SUBADDR_2BYTE 2
  53. #define I2C_SUBADDR_OTHER 3
  54. //remap
  55. #define I2C_REMAP_ENABLE 1
  56. #define I2C_REMAP_DISABLE 0
  57. /* Private macros ------------------------------------------------------------*/
  58. #define IS_I2C_GC(__GC__) (((__GC__) == I2C_GENERALCALLACK_ENABLE) ||\
  59. ((__GC__) == I2C_GENERALCALLACK_DISABLE))
  60. #define IS_I2C_AA(__AA__) (((__AA__) == I2C_ASSERTACKNOWLEDGE_ENABLE) ||\
  61. ((__AA__) == I2C_ASSERTACKNOWLEDGE_DISABLE))
  62. #define IS_I2C_CLKSRC(__CLKSRC__) (((__CLKSRC__) == I2C_CLOCKSOURCE_APBD256) ||\
  63. ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD224) ||\
  64. ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD192) ||\
  65. ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD160) ||\
  66. ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD960) ||\
  67. ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD120) ||\
  68. ((__CLKSRC__) == I2C_CLOCKSOURCE_APBD60) ||\
  69. ((__CLKSRC__) == I2C_CLOCKSOURCE_TIM3OFD8))
  70. #define I2C_SUBADDR_TYPE(__TYPE__) (((__TYPE__) == I2C_SUBADDR_1BYTE) ||\
  71. ((__TYPE__) == I2C_SUBADDR_2BYTE) ||\
  72. ((__TYPE__) == I2C_SUBADDR_OTHER))
  73. /* Exported Functions ------------------------------------------------------- */
  74. /* I2C Exported Functions Group1:
  75. (De)Initialization ------------------------*/
  76. void I2C_DeInit(uint32_t remap);
  77. void I2C_StructInit(I2C_InitType *InitStruct);
  78. void I2C_Init(I2C_InitType *InitStruct);
  79. /* I2C Exported Functions Group2:
  80. Interrupt ---------------------------------*/
  81. void I2C_INTConfig(uint32_t NewState);
  82. uint8_t I2C_GetINTStatus(void);
  83. void I2C_ClearINTStatus(void);
  84. /* I2C Exported Functions Group3:
  85. Transfer datas ----------------------------*/
  86. uint16_t I2C_MasterReadBytes(I2C_WRType *InitStruct);
  87. uint16_t I2C_MasterWriteBytes(I2C_WRType *InitStruct);
  88. /* I2C Exported Functions Group4:
  89. MISC Configuration ------------------------*/
  90. void I2C_Cmd(uint32_t NewState);
  91. /* I2C Exported Functions Group5:
  92. Others ------------------------------------*/
  93. void I2C_AssertAcknowledgeConfig(uint32_t NewState);
  94. uint8_t I2C_ReceiveData(void);
  95. void I2C_SendData(uint8_t Dat);
  96. void I2C_GenerateSTART(uint32_t NewState);
  97. void I2C_GenerateSTOP(uint32_t NewState);
  98. uint8_t I2C_GetStatusCode(void);
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif /* __LIB_I2C_H */
  103. /*********************************** END OF FILE ******************************/