1 /**
2  * \file
3  *
4  * \brief I2C Master Hardware Proxy Layer(HPL) declaration.
5  *
6  * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Subject to your compliance with these terms, you may use Microchip
13  * software and any derivatives exclusively with Microchip products.
14  * It is your responsibility to comply with third party license terms applicable
15  * to your use of third party software (including open source software) that
16  * may accompany Microchip software.
17  *
18  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19  * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20  * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22  * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23  * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24  * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25  * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
26  * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27  * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29  *
30  * \asf_license_stop
31  *
32  */
33 #ifndef _HPL_I2C_M_SYNC_H_INCLUDED
34 #define _HPL_I2C_M_SYNC_H_INCLUDED
35 
36 #include <compiler.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * \brief i2c flags
44  */
45 #define I2C_M_RD 0x0001 /* read data, from slave to master */
46 #define I2C_M_BUSY 0x0100
47 #define I2C_M_TEN 0x0400   /* this is a ten bit chip address */
48 #define I2C_M_SEVEN 0x0800 /* this is a seven bit chip address */
49 #define I2C_M_FAIL 0x1000
50 #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */
51 
52 /**
53  * \brief i2c Return codes
54  */
55 #define I2C_OK 0                     /* Operation successful */
56 #define I2C_ACK -1                   /* Received ACK from device on I2C bus */
57 #define I2C_NACK -2                  /* Received NACK from device on I2C bus */
58 #define I2C_ERR_ARBLOST -3           /* Arbitration lost */
59 #define I2C_ERR_BAD_ADDRESS -4       /* Bad address */
60 #define I2C_ERR_BUS -5               /* Bus error */
61 #define I2C_ERR_BUSY -6              /* Device busy */
62 #define I2c_ERR_PACKAGE_COLLISION -7 /* Package collision */
63 
64 /**
65  * \brief i2c I2C Modes
66  */
67 #define I2C_STANDARD_MODE 0x00
68 #define I2C_FASTMODE 0x01
69 #define I2C_HIGHSPEED_MODE 0x02
70 
71 /**
72  * \brief i2c master message structure
73  */
74 struct _i2c_m_msg {
75 	uint16_t          addr;
76 	volatile uint16_t flags;
77 	int32_t           len;
78 	uint8_t *         buffer;
79 };
80 
81 /**
82  * \brief i2c master service
83  */
84 struct _i2c_m_service {
85 	struct _i2c_m_msg msg;
86 	uint16_t          mode;
87 	uint16_t          trise;
88 };
89 
90 /**
91  * \brief i2c sync master device structure
92  */
93 struct _i2c_m_sync_device {
94 	struct _i2c_m_service service;
95 	void *                hw;
96 };
97 
98 /**
99  * \name HPL functions
100  */
101 
102 /**
103  * \brief Initialize I2C
104  *
105  * This function does low level I2C configuration.
106  *
107  * \param[in] i2c_dev The pointer to i2c device structure
108  * \param[in] hw The pointer to hardware instance
109  *
110  * \return Return 0 for success and negative value for error
111  */
112 int32_t _i2c_m_sync_init(struct _i2c_m_sync_device *const i2c_dev, void *const hw);
113 
114 /**
115  * \brief Deinitialize I2C
116  *
117  * \param[in] i2c_dev The pointer to i2c device structure
118  *
119  * \return Return 0 for success and negative value for error
120  */
121 int32_t _i2c_m_sync_deinit(struct _i2c_m_sync_device *const i2c_dev);
122 
123 /**
124  * \brief Enable I2C module
125  *
126  * This function does low level I2C enable.
127  *
128  * \param[in] i2c_dev The pointer to i2c device structure
129  *
130  * \return Return 0 for success and negative value for error
131  */
132 int32_t _i2c_m_sync_enable(struct _i2c_m_sync_device *const i2c_dev);
133 
134 /**
135  * \brief Disable I2C module
136  *
137  * This function does low level I2C disable.
138  *
139  * \param[in] i2c_dev The pointer to i2c device structure
140  *
141  * \return Return 0 for success and negative value for error
142  */
143 int32_t _i2c_m_sync_disable(struct _i2c_m_sync_device *const i2c_dev);
144 
145 /**
146  * \brief Transfer data by I2C
147  *
148  * This function does low level I2C data transfer.
149  *
150  * \param[in] i2c_dev The pointer to i2c device structure
151  * \param[in] msg The pointer to i2c msg structure
152  *
153  * \return Return 0 for success and negative value for error
154  */
155 int32_t _i2c_m_sync_transfer(struct _i2c_m_sync_device *const i2c_dev, struct _i2c_m_msg *msg);
156 
157 /**
158  * \brief Set baud rate of I2C
159  *
160  * This function does low level I2C set baud rate.
161  *
162  * \param[in] i2c_dev The pointer to i2c device structure
163  * \param[in] clkrate The clock rate(KHz) input to i2c module
164  * \param[in] baudrate The demand baud rate(KHz) of i2c module
165  *
166  * \return Return 0 for success and negative value for error
167  */
168 int32_t _i2c_m_sync_set_baudrate(struct _i2c_m_sync_device *const i2c_dev, uint32_t clkrate, uint32_t baudrate);
169 
170 /**
171  * \brief Send send condition on the I2C bus
172  *
173  * This function will generate a stop condition on the I2C bus
174  *
175  * \param[in] i2c_dev The pointer to i2c device struct
176  *
177  * \return Return 0 for success and negative value for error
178  */
179 int32_t _i2c_m_sync_send_stop(struct _i2c_m_sync_device *const i2c_dev);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif
186