ls1c_i2c.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * File : ls1c_i2c.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-09-06 勤为本 first version
  23. */
  24. // 硬件i2c接口的头文件
  25. #ifndef __OPENLOONGSON_I2C_H
  26. #define __OPENLOONGSON_I2C_H
  27. // I2C的时钟频率
  28. #define LS1C_I2C_CLOCK_DEFAULT (100000) // Hz. 默认频率
  29. #define LS1C_I2C_CLOCK_MAX (400000) // Hz, max 400 Kbits/sec
  30. // I2C模块编号
  31. typedef enum
  32. {
  33. LS1C_I2C_0 = 0,
  34. LS1C_I2C_1,
  35. LS1C_I2C_2
  36. }ls1c_i2c_t;
  37. // I2C数据传输方向
  38. typedef enum
  39. {
  40. LS1C_I2C_DIRECTION_WRITE = 0, // 主机向从机写信息
  41. LS1C_I2C_DIRECTION_READ, // 主机向从机读信息
  42. }ls1c_i2c_direction_t;
  43. // 硬件I2C信息
  44. typedef struct
  45. {
  46. ls1c_i2c_t I2Cx; // i2c模块编号
  47. unsigned long clock; // i2c时钟频率,单位hz
  48. }ls1c_i2c_info_t;
  49. // I2C应答
  50. typedef enum
  51. {
  52. LS1C_I2C_ACK = 0, // 收到应答
  53. LS1C_I2C_NACK = 1, // 没收到应答
  54. }ls1c_i2c_ack_t;
  55. // 函数返回值
  56. typedef enum
  57. {
  58. LS1C_I2C_RET_OK = 0, // OK
  59. LS1C_I2C_RET_TIMEOUT, // 超时
  60. }ls1c_i2c_ret_t;
  61. /*
  62. * 初始化指定i2c模块
  63. * @i2c_info_p 某个i2c模块的信息
  64. */
  65. void i2c_init(ls1c_i2c_info_t *i2c_info_p);
  66. /*
  67. * (再发送一个字节数据之后)接收从机发送的ACK信号
  68. * @i2c_info_p i2c模块信息
  69. * @ret LS1C_I2C_ACK or LS1C_I2C_NACK
  70. */
  71. ls1c_i2c_ack_t i2c_receive_ack(ls1c_i2c_info_t *i2c_info_p);
  72. /*
  73. * 接收数据
  74. * @i2c_info_p i2c模块信息
  75. * @buf 数据缓存
  76. * @len 待接收数据的长度
  77. */
  78. ls1c_i2c_ret_t i2c_receive_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *buf, int len);
  79. /*
  80. * 发送START信号和地址
  81. * @i2c_info_p i2c模块信息
  82. * @slave_addr 从机地址
  83. * @direction 数据传输方向(读、写)
  84. */
  85. ls1c_i2c_ret_t i2c_send_start_and_addr(ls1c_i2c_info_t *i2c_info_p,
  86. unsigned char slave_addr,
  87. ls1c_i2c_direction_t direction);
  88. /*
  89. * 发送数据
  90. * @i2c_info_p i2c模块信息
  91. * @data 待发送的数据
  92. * @len 待发送数据的长度
  93. */
  94. ls1c_i2c_ret_t i2c_send_data(ls1c_i2c_info_t *i2c_info_p, unsigned char *data, int len);
  95. /*
  96. * 发送STOP信号
  97. * @i2c_info_p i2c模块信息
  98. */
  99. void i2c_send_stop(ls1c_i2c_info_t *i2c_info_p);
  100. #endif