-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_interface.h
132 lines (119 loc) · 4.63 KB
/
i2c_interface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
******************************************************************************
* @file i2c_interface.h
* @author Ali Batuhan KINDAN
* @date 28.12.2020
* @brief This file constains I2C Interface class definition.
*
* MIT License
*
* Copyright (c) 2022 Ali Batuhan KINDAN
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdint.h>
#ifndef I2C_INTERFACE_H
#define I2C_INTERFACE_H
/* I2C status types.
* TODO: will be extended for different I2C error types! Keep it as enum for now. */
enum i2c_status_t
{
I2C_STATUS_SUCCESS = 0x00,
I2C_STATUS_ERROR = 0x01,
I2C_STATUS_NONE = 0x02
};
/* I2C clock speed types */
enum class i2c_clockspeed_t
{
CLK_NONE = 0,
CLK_100KHz = 100000,
CLK_200KHz = 200000,
CLK_400KHz = 400000
};
/* I2C Interface class to make sensor driver work with
* other MCU architectures by simply overriding this virtual
* methods according to current architecture I2C driver methods. */
class I2C_Interface
{
public:
/**
* @brief Class constructor.
* @param none
* @retval none
*/
I2C_Interface() {/* empty constructor */};
/**
* @brief Class destructor
* @param none
* @retval none
*/
virtual ~I2C_Interface() {/* empty virtual destructor */};
/* TODO: Init_I2C methods should be differenciated by compiler without the need of casting! */
/**
* @brief I2C peripheral initialization method.
* @param clock I2C clock frequency (default 100 kHz)
* @retval i2c_status_t
*/
virtual i2c_status_t Init_I2C(i2c_clockspeed_t clock = i2c_clockspeed_t::CLK_100KHz) {return I2C_STATUS_NONE;};
/**
* @brief I2C peripheral initialization method.
* @param slaveAddress adress of the device that will be communicated
* @retval i2c_status_t
*/
virtual i2c_status_t Init_I2C(uint8_t slaveAddress) {return I2C_STATUS_NONE;};
/**
* @brief This method will be used for reading the data of the given register from
* the slave with given address.
* @param slaveAddress Slave chip I2C bus address
* @param regAddress Register address to be read
* @param status Pointer for operation status
* @retval uint8_t Read register value
*/
virtual uint8_t ReadRegister(uint8_t slaveAddress, uint8_t regAddress, i2c_status_t *status) = 0;
/**
* @brief This method will be used for writing gven data to the given register of the slave device
* with the given address.
* @param slaveAddress Slave chip I2C bus address
* @param regAddress Register address that the data to be written
* @param data Data to be written
* @retval i2c_status_t
*/
virtual i2c_status_t WriteRegister(uint8_t slaveAddress, uint8_t regAddress, uint8_t data) = 0;
/**
* @brief This method will be used for reading a bit value of the given register
* from the slace device with the given address.
* @param slaveAddress Slave chip I2C bus address
* @param regAddress Register address that the data to be read
* @param bitMask Bit mask to be read
* @param status Pointer for operation status
* @retval bool Bit value
*/
bool ReadRegisterBit(uint8_t slaveAddress, uint8_t regAddress, uint8_t bitMask, i2c_status_t *status);
/**
* @brief This method will be used for writing a bit value of the given register
* from the slace device with the given address.
* @param slaveAddress Slave chip I2C bus address
* @param regAddress Register address that the data to be written
* @param bitMask Bit mask to be set/reset
* @param bitVal Bit value to be written
* @retval i2c_status_t
*/
i2c_status_t WriteRegisterBit(uint8_t slaveAddress, uint8_t regAddress, uint8_t bitMask, bool bitVal);
};
#endif /* include guard */