1 /**
2  * \file
3  *
4  * \brief I2C Master Hardware Proxy Layer(HPL) declaration.
5  *
6  * Copyright (C) 2014 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_SYNC_H_INCLUDED
44 #define _HPL_I2C_M_SYNC_H_INCLUDED
45 
46 #include <compiler.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /**
53  * \brief i2c flags
54  */
55 #define I2C_M_RD 0x0001 /* read data, from slave to master */
56 #define I2C_M_BUSY 0x0100
57 #define I2C_M_TEN 0x0400   /* this is a ten bit chip address */
58 #define I2C_M_SEVEN 0x0800 /* this is a seven bit chip address */
59 #define I2C_M_FAIL 0x1000
60 #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */
61 
62 /**
63  * \brief i2c Return codes
64  */
65 #define I2C_OK 0                     /* Operation successful */
66 #define I2C_ACK -1                   /* Received ACK from device on I2C bus */
67 #define I2C_NACK -2                  /* Received NACK from device on I2C bus */
68 #define I2C_ERR_ARBLOST -3           /* Arbitration lost */
69 #define I2C_ERR_BAD_ADDRESS -4       /* Bad address */
70 #define I2C_ERR_BUS -5               /* Bus error */
71 #define I2C_ERR_BUSY -6              /* Device busy */
72 #define I2c_ERR_PACKAGE_COLLISION -7 /* Package collision */
73 
74 /**
75  * \brief i2c I2C Modes
76  */
77 #define I2C_STANDARD_MODE 0x00
78 #define I2C_FASTMODE 0x01
79 #define I2C_HIGHSPEED_MODE 0x02
80 
81 /**
82  * \brief i2c master message structure
83  */
84 struct _i2c_m_msg {
85 	uint16_t          addr;
86 	volatile uint16_t flags;
87 	int32_t           len;
88 	uint8_t *         buffer;
89 };
90 
91 /**
92  * \brief i2c master service
93  */
94 struct _i2c_m_service {
95 	struct _i2c_m_msg msg;
96 	uint16_t          mode;
97 	uint16_t          trise;
98 };
99 
100 /**
101  * \brief i2c sync master device structure
102  */
103 struct _i2c_m_sync_device {
104 	struct _i2c_m_service service;
105 	void *                hw;
106 };
107 
108 /**
109  * \name HPL functions
110  */
111 
112 /**
113  * \brief Initialize I2C
114  *
115  * This function does low level I2C configuration.
116  *
117  * \param[in] i2c_dev The pointer to i2c device structure
118  * \param[in] hw The pointer to hardware instance
119  *
120  * \return Return 0 for success and negative value for error
121  */
122 int32_t _i2c_m_sync_init(struct _i2c_m_sync_device *const i2c_dev, void *const hw);
123 
124 /**
125  * \brief Deinitialize I2C
126  *
127  * \param[in] i2c_dev The pointer to i2c device structure
128  *
129  * \return Return 0 for success and negative value for error
130  */
131 int32_t _i2c_m_sync_deinit(struct _i2c_m_sync_device *const i2c_dev);
132 
133 /**
134  * \brief Enable I2C module
135  *
136  * This function does low level I2C enable.
137  *
138  * \param[in] i2c_dev The pointer to i2c device structure
139  *
140  * \return Return 0 for success and negative value for error
141  */
142 int32_t _i2c_m_sync_enable(struct _i2c_m_sync_device *const i2c_dev);
143 
144 /**
145  * \brief Disable I2C module
146  *
147  * This function does low level I2C disable.
148  *
149  * \param[in] i2c_dev The pointer to i2c device structure
150  *
151  * \return Return 0 for success and negative value for error
152  */
153 int32_t _i2c_m_sync_disable(struct _i2c_m_sync_device *const i2c_dev);
154 
155 /**
156  * \brief Transfer data by I2C
157  *
158  * This function does low level I2C data transfer.
159  *
160  * \param[in] i2c_dev The pointer to i2c device structure
161  * \param[in] msg The pointer to i2c msg structure
162  *
163  * \return Return 0 for success and negative value for error
164  */
165 int32_t _i2c_m_sync_transfer(struct _i2c_m_sync_device *const i2c_dev, struct _i2c_m_msg *msg);
166 
167 /**
168  * \brief Set baud rate of I2C
169  *
170  * This function does low level I2C set baud rate.
171  *
172  * \param[in] i2c_dev The pointer to i2c device structure
173  * \param[in] clkrate The clock rate(KHz) input to i2c module
174  * \param[in] baudrate The demand baud rate(KHz) of i2c module
175  *
176  * \return Return 0 for success and negative value for error
177  */
178 int32_t _i2c_m_sync_set_baudrate(struct _i2c_m_sync_device *const i2c_dev, uint32_t clkrate, uint32_t baudrate);
179 
180 /**
181  * \brief Send send condition on the I2C bus
182  *
183  * This function will generate a stop condition on the I2C bus
184  *
185  * \param[in] i2c_dev The pointer to i2c device struct
186  *
187  * \return Return 0 for success and negative value for error
188  */
189 int32_t _i2c_m_sync_send_stop(struct _i2c_m_sync_device *const i2c_dev);
190 
191 #ifdef __cplusplus
192 }
193 #endif
194 
195 #endif
196