1 /**
2  * \file
3  *
4  * \brief I2C Master Hardware Proxy Layer(HPL) declaration.
5  *
6  * Copyright (C) 2015 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 #ifndef _HPL_I2C_M_ASYNC_H_INCLUDED
44 #define _HPL_I2C_M_ASYNC_H_INCLUDED
45 
46 #include "hpl_i2c_m_sync.h"
47 #include "hpl_irq.h"
48 #include "utils.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /**
55  * \brief i2c master callback names
56  */
57 enum _i2c_m_async_callback_type {
58 	I2C_M_ASYNC_DEVICE_ERROR,
59 	I2C_M_ASYNC_DEVICE_TX_COMPLETE,
60 	I2C_M_ASYNC_DEVICE_RX_COMPLETE
61 };
62 
63 struct _i2c_m_async_device;
64 
65 typedef void (*_i2c_complete_cb_t)(struct _i2c_m_async_device *i2c_dev);
66 typedef void (*_i2c_error_cb_t)(struct _i2c_m_async_device *i2c_dev, int32_t errcode);
67 
68 /**
69  * \brief i2c callback pointers structure
70  */
71 struct _i2c_m_async_callback {
72 	_i2c_error_cb_t    error;
73 	_i2c_complete_cb_t tx_complete;
74 	_i2c_complete_cb_t rx_complete;
75 };
76 
77 /**
78  * \brief i2c device structure
79  */
80 struct _i2c_m_async_device {
81 	struct _i2c_m_service        service;
82 	void *                       hw;
83 	struct _i2c_m_async_callback cb;
84 	struct _irq_descriptor       irq;
85 };
86 
87 /**
88  * \name HPL functions
89  */
90 
91 /**
92  * \brief Initialize I2C in interrupt mode
93  *
94  * This function does low level I2C configuration.
95  *
96  * \param[in] i2c_dev The pointer to i2c interrupt device structure
97  * \param[in] hw The pointer to hardware instance
98  *
99  * \return Return 0 for success and negative value for error
100  */
101 int32_t _i2c_m_async_init(struct _i2c_m_async_device *const i2c_dev, void *const hw);
102 
103 /**
104  * \brief Deinitialize I2C in interrupt mode
105  *
106  * \param[in] i2c_dev The pointer to i2c device structure
107  *
108  * \return Return 0 for success and negative value for error
109  */
110 int32_t _i2c_m_async_deinit(struct _i2c_m_async_device *const i2c_dev);
111 
112 /**
113  * \brief Enable I2C module
114  *
115  * This function does low level I2C enable.
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_async_enable(struct _i2c_m_async_device *const i2c_dev);
122 
123 /**
124  * \brief Disable I2C module
125  *
126  * This function does low level I2C disable.
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_async_disable(struct _i2c_m_async_device *const i2c_dev);
133 
134 /**
135  * \brief Transfer data by I2C
136  *
137  * This function does low level I2C data transfer.
138  *
139  * \param[in] i2c_dev The pointer to i2c device structure
140  * \param[in] msg The pointer to i2c msg structure
141  *
142  * \return Return 0 for success and negative value for error
143  */
144 int32_t _i2c_m_async_transfer(struct _i2c_m_async_device *const i2c_dev, struct _i2c_m_msg *msg);
145 
146 /**
147  * \brief Set baud rate of I2C
148  *
149  * This function does low level I2C set baud rate.
150  *
151  * \param[in] i2c_dev The pointer to i2c device structure
152  * \param[in] clkrate The clock rate(KHz) input to i2c module
153  * \param[in] baudrate The demand baud rate(KHz) of i2c module
154  *
155  * \return Return 0 for success and negative value for error
156  */
157 int32_t _i2c_m_async_set_baudrate(struct _i2c_m_async_device *const i2c_dev, uint32_t clkrate, uint32_t baudrate);
158 
159 /**
160  * \brief Register callback to I2C
161  *
162  * This function does low level I2C callback register.
163  *
164  * \param[in] i2c_dev The pointer to i2c device structure
165  * \param[in] cb_type The callback type request
166  * \param[in] func The callback function pointer
167  *
168  * \return Return 0 for success and negative value for error
169  */
170 int32_t _i2c_m_async_register_callback(struct _i2c_m_async_device *i2c_dev, enum _i2c_m_async_callback_type cb_type,
171                                        FUNC_PTR func);
172 
173 /**
174  * \brief Generate stop condition on the I2C bus
175  *
176  * This function will generate a stop condition on the I2C bus
177  *
178  * \param[in] i2c_m_async_descriptor An i2c descriptor which is used to communicate through I2C
179  *
180  * \return Operation status
181  * \retval 0 Operation executed successfully
182  * \retval <0 Operation failed
183  */
184 int32_t _i2c_m_async_send_stop(struct _i2c_m_async_device *const i2c_dev);
185 
186 /**
187  * \brief Returns the number of bytes left or not used in the I2C message buffer
188  *
189  * This function will return the number of bytes left (not written to the bus) or still free
190  * (not received from the bus) in the message buffer, depending on direction of transmission.
191  *
192  * \param[in] i2c_m_async_descriptor An i2c descriptor which is used to communicate through I2C
193  *
194  * \return Number of bytes or error code
195  * \retval >0 Positive number indicating bytes left
196  * \retval 0  Buffer is full/empty depending on direction
197  * \retval <0 Error code
198  */
199 int32_t _i2c_m_async_get_bytes_left(struct _i2c_m_async_device *const i2c_dev);
200 
201 /**
202  * \brief Enable/disable I2C master interrupt
203  *
204  * param[in] device The pointer to I2C master device instance
205  * param[in] type The type of interrupt to disable/enable if applicable
206  * param[in] state Enable or disable
207  */
208 void _i2c_m_async_set_irq_state(struct _i2c_m_async_device *const device, const enum _i2c_m_async_callback_type type,
209                                 const bool state);
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif
216