core_i2c.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 RegLen 寄存器地址长度,在通用读写时忽略
  56. * @param Data 读写数据缓存,直接使用用户的空间,在完成前不可以释放空间
  57. * @param Len 读写数据长度
  58. * @param Toms 传输单个字节超时时间,单位ms
  59. */
  60. void I2C_MasterXfer(uint8_t I2CID, uint8_t Operate, uint8_t *RegAddress, uint32_t RegLen, uint8_t *Data, uint32_t Len, uint16_t Toms);
  61. /**
  62. * @brief i2c主机传输结果查询
  63. *
  64. * @param I2CID I2C通道号
  65. * @param Result 传输结果 =0成功,其他失败,只有return != 0才有效
  66. * @return =0 传输还未完成 其他已完成
  67. */
  68. int I2C_WaitResult(uint8_t I2CID, int32_t *Result);
  69. 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);
  70. int32_t I2C_BlockRead(uint8_t I2CID, uint8_t ChipAddress, uint8_t *Reg, uint32_t RegLen, uint8_t *Data, uint32_t Len, uint16_t Toms, CBFuncEx_t CB, void *pParam);
  71. #endif