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 
22 #ifndef __CORE_I2C_H__
23 #define __CORE_I2C_H__
24 #include "bsp_common.h"
25 typedef struct
26 {
27     uint8_t Data[2];
28 }I2C_CommonRegDataStruct;
29 
30 void I2C_GlobalInit(void);
31 /**
32  * @brief i2c主机配置
33  *
34  * @param I2CID I2C通道号
35  * @param Speed 速度,只有100000和400000
36  */
37 void I2C_MasterSetup(uint8_t I2CID, uint32_t Speed);
38 /*
39  * @brief i2c传输前配置,如果配置和上次一致,则不用设置
40  *
41  * @param I2CID I2C通道号
42  * @param ChipAddress I2C设备地址
43  * @param ChipAddressLen I2C设备地址长度 ,1或者2
44  * @param CB 完成后回调函数
45  * @param pParam 完成后回调函数中的pParam
46  */
47 void I2C_Prepare(uint8_t I2CID, uint16_t ChipAddress, uint8_t ChipAddressLen, CBFuncEx_t CB, void *pParam);
48 /**
49  * @brief i2c主机传输,兼容直接读写和先写寄存器地址后读数据
50  *
51  * @param I2CID I2C通道号
52  * @param Operate 操作类型
53  *  I2C_OP_READ_REG = 0,    //i2c通用读寄存器,一写一读,自动带start信号
54     I2C_OP_READ,        //i2c通用读,只读
55     I2C_OP_WRITE,       //i2c通用写,只写
56  * @param RegAddress 寄存器地址,在通用读写时忽略
57  * @param Data 读写数据缓存,直接使用用户的空间,在完成前不可以释放空间
58  * @param Len 读写数据长度
59  * @param Toms 传输单个字节超时时间,单位ms
60  */
61 void I2C_MasterXfer(uint8_t I2CID, uint8_t Operate, uint8_t RegAddress, uint8_t *Data, uint32_t Len, uint16_t Toms);
62 
63 /**
64  * @brief i2c主机传输,多个单一寄存器写入
65  *
66  * @param I2CID I2C通道号
67  * @param RegQueue 寄存器序列
68  * @param TotalNum 序列长度
69  * @param Toms 传输单个字节超时时间,单位ms
70  * @param IsBlock 是否要阻塞
71  * @return =0 传输成功,其他失败 IsBlock=1才有效,IsBlock=0直接返回0
72  */
73 int32_t I2C_MasterWriteRegQueue(uint8_t I2CID, I2C_CommonRegDataStruct *RegQueue, uint32_t TotalNum, uint16_t Toms, uint8_t IsBlock);
74 
75 /**
76  * @brief i2c主机传输结果查询
77  *
78  * @param I2CID I2C通道号
79  * @param Result 传输结果 =0成功,其他失败,只有return != 0才有效
80  * @return =0 传输还未完成 其他已完成
81  */
82 int I2C_WaitResult(uint8_t I2CID, int32_t *Result);
83 
84 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);
85 
86 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);
87 #endif
88