i2c_bsp.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /************************************************************************************************/
  2. /**
  3. * @file i2c_bsp.h
  4. * @author MCU Ecosystem Development Team
  5. * @brief I2C BSP头文件。
  6. *
  7. *
  8. **************************************************************************************************
  9. * @attention
  10. * Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
  11. *
  12. **************************************************************************************************
  13. */
  14. /* 避免头文件重复引用 */
  15. #ifndef I2C_BSP_H
  16. #define I2C_BSP_H
  17. #include "ciu32f003_std.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /*------------------------------------------includes--------------------------------------------*/
  22. /*-------------------------------------------define---------------------------------------------*/
  23. /* I2C延时操作 */
  24. #define CLK_DELAY (16U)
  25. #define ACK_DELAY (6U)
  26. #define USER_DELAY (5U) //
  27. /* I2C返回ACK的类型 */
  28. #define RET_ACK (0x00000000U)
  29. #define RET_NACK (0x00000001U)
  30. /* I2C主机读写请求 */
  31. #define REQ_WRITE (0U)
  32. #define REQ_READ (1U)
  33. /*------------------------------------------- io_set ------------------------------------------*/
  34. #define IIC_SCL_PA0 1U
  35. #define IIC_SCL_PA1 2U
  36. #define IIC_SCL_PA2 3U
  37. #define IIC_SCL_PA3 4U
  38. #define IIC_SCL_PA4 5U
  39. #define IIC_SCL_PA5 6U
  40. #define IIC_SCL_PA6 7U
  41. #define IIC_SCL_PA7 8U
  42. #define IIC_SCL_PB0 9U
  43. #define IIC_SCL_PB1 10U
  44. #define IIC_SCL_PB2 11U
  45. #define IIC_SCL_PB3 12U
  46. #define IIC_SCL_PB4 13U
  47. #define IIC_SCL_PB5 14U
  48. #define IIC_SCL_PB6 15U
  49. #define IIC_SCL_PB7 16U
  50. #define IIC_SCL_PC0 17U
  51. #define IIC_SCL_PC1 18U
  52. #define IIC_SDA_PA0 1U
  53. #define IIC_SDA_PA1 2U
  54. #define IIC_SDA_PA2 3U
  55. #define IIC_SDA_PA3 4U
  56. #define IIC_SDA_PA4 5U
  57. #define IIC_SDA_PA5 6U
  58. #define IIC_SDA_PA6 7U
  59. #define IIC_SDA_PA7 8U
  60. #define IIC_SDA_PB0 9U
  61. #define IIC_SDA_PB1 10U
  62. #define IIC_SDA_PB2 11U
  63. #define IIC_SDA_PB3 12U
  64. #define IIC_SDA_PB4 13U
  65. #define IIC_SDA_PB5 14U
  66. #define IIC_SDA_PB6 15U
  67. #define IIC_SDA_PB7 16U
  68. #define IIC_SDA_PC0 17U
  69. #define IIC_SDA_PC1 18U
  70. #define IIC_PULLUP_NULL 0U
  71. #define IIC_PULLUP 1U
  72. /*-------------------------------------------functions-内部iic接口-----------------------------------------*/
  73. static void bsp_i2c_generate_start(void);
  74. static void bsp_i2c_generate_stop(void);
  75. static void bsp_i2c_generate_ack(uint32_t ack_type);
  76. static uint32_t bsp_i2c_master_send_byte(uint8_t data);
  77. static uint8_t bsp_i2c_master_receive_byte(void);
  78. /*-------------------------------------------functions-----用户iic接口-------------------------------------*/
  79. void SN_IIC_IO_set(uint8_t SCL_PIN_x ,uint8_t SDA_PIN_x ,uint8_t PULLUP ); //设置iic要使用的通信引脚
  80. void SN_IIC_slave(uint8_t iic_slave_id ); //设置从机id
  81. void SN_IIC_ERROR_call(void (*error_call) (void)); //设置通信错误处理
  82. void bsp_i2c_master_send(uint8_t * g_tx_buffer , uint16_t BUFF_SIZE); //主机发送数据
  83. void bsp_i2c_master_receive(uint8_t * g_rx_buffer,uint16_t BUFF_SIZE); //主机接收数据
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif /* I2C_BSP_H */