core_i2c.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __CORE_I2C_H__
  22. #define __CORE_I2C_H__
  23. #include "bsp_common.h"
  24. typedef struct
  25. {
  26. uint8_t Data[2];
  27. }I2C_CommonRegDataStruct;
  28. void I2C_GlobalInit(void);
  29. /**
  30. * @brief i2c主机配置
  31. *
  32. * @param I2CID I2C通道号
  33. * @param Speed 速度,只有100000和400000
  34. */
  35. void I2C_MasterSetup(uint8_t I2CID, uint32_t Speed);
  36. /*
  37. * @brief i2c传输前配置,如果配置和上次一致,则不用设置
  38. *
  39. * @param I2CID I2C通道号
  40. * @param ChipAddress I2C设备地址
  41. * @param ChipAddressLen I2C设备地址长度 ,1或者2
  42. * @param CB 完成后回调函数
  43. * @param pParam 完成后回调函数中的pParam
  44. */
  45. void I2C_Prepare(uint8_t I2CID, uint16_t ChipAddress, uint8_t ChipAddressLen, CBFuncEx_t CB, void *pParam);
  46. /**
  47. * @brief i2c主机传输,兼容直接读写和先写寄存器地址后读数据
  48. *
  49. * @param I2CID I2C通道号
  50. * @param Operate 操作类型
  51. * I2C_OP_READ_REG = 0, //i2c通用读寄存器,一写一读,自动带start信号
  52. I2C_OP_READ, //i2c通用读,只读
  53. I2C_OP_WRITE, //i2c通用写,只写
  54. * @param RegAddress 寄存器地址,在通用读写时忽略
  55. * @param Data 读写数据缓存,直接使用用户的空间,在完成前不可以释放空间
  56. * @param Len 读写数据长度
  57. * @param Toms 传输单个字节超时时间,单位ms
  58. */
  59. void I2C_MasterXfer(uint8_t I2CID, uint8_t Operate, uint8_t RegAddress, uint8_t *Data, uint32_t Len, uint16_t Toms);
  60. /**
  61. * @brief i2c主机传输,多个单一寄存器写入
  62. *
  63. * @param I2CID I2C通道号
  64. * @param RegQueue 寄存器序列
  65. * @param TotalNum 序列长度
  66. * @param Toms 传输单个字节超时时间,单位ms
  67. * @param IsBlock 是否要阻塞
  68. * @return =0 传输成功,其他失败 IsBlock=1才有效,IsBlock=0直接返回0
  69. */
  70. int32_t I2C_MasterWriteRegQueue(uint8_t I2CID, I2C_CommonRegDataStruct *RegQueue, uint32_t TotalNum, uint16_t Toms, uint8_t IsBlock);
  71. /**
  72. * @brief i2c主机传输结果查询
  73. *
  74. * @param I2CID I2C通道号
  75. * @param Result 传输结果 =0成功,其他失败,只有return != 0才有效
  76. * @return =0 传输还未完成 其他已完成
  77. */
  78. int I2C_WaitResult(uint8_t I2CID, int32_t *Result);
  79. int32_t I2C_BlockWrite(uint8_t I2CID, uint8_t ChipAddress, const uint8_t *Data, uint32_t Len, uint16_t Toms, CBFuncEx_t CB, void *pParam);
  80. int32_t I2C_BlockRead(uint8_t I2CID, uint8_t ChipAddress, uint8_t *Reg, uint8_t *Data, uint32_t Len, uint16_t Toms, CBFuncEx_t CB, void *pParam);
  81. #endif