ls1c_i2c.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-09-06 勤为本 first version
  9. */
  10. // 硬件i2c接口的头文件
  11. #ifndef __OPENLOONGSON_I2C_H
  12. #define __OPENLOONGSON_I2C_H
  13. // I2C的时钟频率
  14. #define LS1C_I2C_CLOCK_DEFAULT (100000) // Hz. 默认频率
  15. #define LS1C_I2C_CLOCK_MAX (400000) // Hz, max 400 Kbits/sec
  16. // I2C模块编号
  17. typedef enum
  18. {
  19. LS1C_I2C_0 = 0,
  20. LS1C_I2C_1,
  21. LS1C_I2C_2
  22. }ls1c_i2c_t;
  23. // I2C数据传输方向
  24. typedef enum
  25. {
  26. LS1C_I2C_DIRECTION_WRITE = 0, // 主机向从机写信息
  27. LS1C_I2C_DIRECTION_READ, // 主机向从机读信息
  28. }ls1c_i2c_direction_t;
  29. // 硬件I2C信息
  30. typedef struct
  31. {
  32. ls1c_i2c_t I2Cx; // i2c模块编号
  33. unsigned long clock; // i2c时钟频率,单位hz
  34. }ls1c_i2c_info_t;
  35. // I2C应答
  36. typedef enum
  37. {
  38. LS1C_I2C_ACK = 0, // 收到应答
  39. LS1C_I2C_NACK = 1, // 没收到应答
  40. }ls1c_i2c_ack_t;
  41. // 函数返回值
  42. typedef enum
  43. {
  44. LS1C_I2C_RET_OK = 0, // OK
  45. LS1C_I2C_RET_TIMEOUT, // 超时
  46. }ls1c_i2c_ret_t;
  47. /*
  48. * 初始化指定i2c模块
  49. * @i2c_info_p 某个i2c模块的信息
  50. */
  51. void i2c_init(ls1c_i2c_info_t *i2c_info_p);
  52. /*
  53. * (再发送一个字节数据之后)接收从机发送的ACK信号
  54. * @i2c_info_p i2c模块信息
  55. * @ret LS1C_I2C_ACK or LS1C_I2C_NACK
  56. */
  57. ls1c_i2c_ack_t i2c_receive_ack(ls1c_i2c_info_t *i2c_info_p);
  58. /*
  59. * 接收数据
  60. * @i2c_info_p i2c模块信息
  61. * @buf 数据缓存
  62. * @len 待接收数据的长度
  63. */
  64. ls1c_i2c_ret_t i2c_receive_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *buf, int len);
  65. /*
  66. * 发送START信号和地址
  67. * @i2c_info_p i2c模块信息
  68. * @slave_addr 从机地址
  69. * @direction 数据传输方向(读、写)
  70. */
  71. ls1c_i2c_ret_t i2c_send_start_and_addr(ls1c_i2c_info_t *i2c_info_p,
  72. unsigned char slave_addr,
  73. ls1c_i2c_direction_t direction);
  74. /*
  75. * 发送数据
  76. * @i2c_info_p i2c模块信息
  77. * @data 待发送的数据
  78. * @len 待发送数据的长度
  79. */
  80. ls1c_i2c_ret_t i2c_send_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *data, int len);
  81. /*
  82. * 发送STOP信号
  83. * @i2c_info_p i2c模块信息
  84. */
  85. void i2c_send_stop(ls1c_i2c_info_t *i2c_info_p);
  86. #endif