blob: 8a9491debb2f6f602725e150d56b553c59d6a348 [file] [log] [blame]
Kévin Redon4cd3f7d2019-01-24 17:57:13 +01001/**
2 * \file
3 *
4 * \brief I2C Master Hardware Proxy Layer(HPL) declaration.
5 *
6 * Copyright (c) 2015-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_ASYNC_H_INCLUDED
34#define _HPL_I2C_M_ASYNC_H_INCLUDED
35
36#include "hpl_i2c_m_sync.h"
37#include "hpl_irq.h"
38#include "utils.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * \brief i2c master callback names
46 */
47enum _i2c_m_async_callback_type {
48 I2C_M_ASYNC_DEVICE_ERROR,
49 I2C_M_ASYNC_DEVICE_TX_COMPLETE,
50 I2C_M_ASYNC_DEVICE_RX_COMPLETE
51};
52
53struct _i2c_m_async_device;
54
55typedef void (*_i2c_complete_cb_t)(struct _i2c_m_async_device *i2c_dev);
56typedef void (*_i2c_error_cb_t)(struct _i2c_m_async_device *i2c_dev, int32_t errcode);
57
58/**
59 * \brief i2c callback pointers structure
60 */
61struct _i2c_m_async_callback {
62 _i2c_error_cb_t error;
63 _i2c_complete_cb_t tx_complete;
64 _i2c_complete_cb_t rx_complete;
65};
66
67/**
68 * \brief i2c device structure
69 */
70struct _i2c_m_async_device {
71 struct _i2c_m_service service;
72 void * hw;
73 struct _i2c_m_async_callback cb;
74 struct _irq_descriptor irq;
75};
76
77/**
78 * \name HPL functions
79 */
80
81/**
82 * \brief Initialize I2C in interrupt mode
83 *
84 * This function does low level I2C configuration.
85 *
86 * \param[in] i2c_dev The pointer to i2c interrupt device structure
87 * \param[in] hw The pointer to hardware instance
88 *
89 * \return Return 0 for success and negative value for error
90 */
91int32_t _i2c_m_async_init(struct _i2c_m_async_device *const i2c_dev, void *const hw);
92
93/**
94 * \brief Deinitialize I2C in interrupt mode
95 *
96 * \param[in] i2c_dev The pointer to i2c device structure
97 *
98 * \return Return 0 for success and negative value for error
99 */
100int32_t _i2c_m_async_deinit(struct _i2c_m_async_device *const i2c_dev);
101
102/**
103 * \brief Enable I2C module
104 *
105 * This function does low level I2C enable.
106 *
107 * \param[in] i2c_dev The pointer to i2c device structure
108 *
109 * \return Return 0 for success and negative value for error
110 */
111int32_t _i2c_m_async_enable(struct _i2c_m_async_device *const i2c_dev);
112
113/**
114 * \brief Disable I2C module
115 *
116 * This function does low level I2C disable.
117 *
118 * \param[in] i2c_dev The pointer to i2c device structure
119 *
120 * \return Return 0 for success and negative value for error
121 */
122int32_t _i2c_m_async_disable(struct _i2c_m_async_device *const i2c_dev);
123
124/**
125 * \brief Transfer data by I2C
126 *
127 * This function does low level I2C data transfer.
128 *
129 * \param[in] i2c_dev The pointer to i2c device structure
130 * \param[in] msg The pointer to i2c msg structure
131 *
132 * \return Return 0 for success and negative value for error
133 */
134int32_t _i2c_m_async_transfer(struct _i2c_m_async_device *const i2c_dev, struct _i2c_m_msg *msg);
135
136/**
137 * \brief Set baud rate of I2C
138 *
139 * This function does low level I2C set baud rate.
140 *
141 * \param[in] i2c_dev The pointer to i2c device structure
142 * \param[in] clkrate The clock rate(KHz) input to i2c module
143 * \param[in] baudrate The demand baud rate(KHz) of i2c module
144 *
145 * \return Return 0 for success and negative value for error
146 */
147int32_t _i2c_m_async_set_baudrate(struct _i2c_m_async_device *const i2c_dev, uint32_t clkrate, uint32_t baudrate);
148
149/**
150 * \brief Register callback to I2C
151 *
152 * This function does low level I2C callback register.
153 *
154 * \param[in] i2c_dev The pointer to i2c device structure
155 * \param[in] cb_type The callback type request
156 * \param[in] func The callback function pointer
157 *
158 * \return Return 0 for success and negative value for error
159 */
160int32_t _i2c_m_async_register_callback(struct _i2c_m_async_device *i2c_dev, enum _i2c_m_async_callback_type cb_type,
161 FUNC_PTR func);
162
163/**
164 * \brief Generate stop condition on the I2C bus
165 *
166 * This function will generate a stop condition on the I2C bus
167 *
168 * \param[in] i2c_m_async_descriptor An i2c descriptor which is used to communicate through I2C
169 *
170 * \return Operation status
171 * \retval 0 Operation executed successfully
172 * \retval <0 Operation failed
173 */
174int32_t _i2c_m_async_send_stop(struct _i2c_m_async_device *const i2c_dev);
175
176/**
177 * \brief Returns the number of bytes left or not used in the I2C message buffer
178 *
179 * This function will return the number of bytes left (not written to the bus) or still free
180 * (not received from the bus) in the message buffer, depending on direction of transmission.
181 *
182 * \param[in] i2c_m_async_descriptor An i2c descriptor which is used to communicate through I2C
183 *
184 * \return Number of bytes or error code
185 * \retval >0 Positive number indicating bytes left
186 * \retval 0 Buffer is full/empty depending on direction
187 * \retval <0 Error code
188 */
189int32_t _i2c_m_async_get_bytes_left(struct _i2c_m_async_device *const i2c_dev);
190
191/**
192 * \brief Enable/disable I2C master interrupt
193 *
194 * param[in] device The pointer to I2C master device instance
195 * param[in] type The type of interrupt to disable/enable if applicable
196 * param[in] state Enable or disable
197 */
198void _i2c_m_async_set_irq_state(struct _i2c_m_async_device *const device, const enum _i2c_m_async_callback_type type,
199 const bool state);
200
201#ifdef __cplusplus
202}
203#endif
204
205#endif