1 /**
2 * \file
3 *
4 * \brief I/O I2C related functionality implementation.
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 #include <hal_i2c_m_sync.h>
34 #include <utils.h>
35 #include <utils_assert.h>
36
37 /**
38 * \brief Driver version
39 */
40 #define DRIVER_VERSION 0x00000001u
41
42 /**
43 * \brief Sync version of I2C I/O read
44 */
i2c_m_sync_read(struct io_descriptor * io,uint8_t * buf,const uint16_t n)45 static int32_t i2c_m_sync_read(struct io_descriptor *io, uint8_t *buf, const uint16_t n)
46 {
47 struct i2c_m_sync_desc *i2c = CONTAINER_OF(io, struct i2c_m_sync_desc, io);
48 struct _i2c_m_msg msg;
49 int32_t ret;
50
51 msg.addr = i2c->slave_addr;
52 msg.len = n;
53 msg.flags = I2C_M_STOP | I2C_M_RD;
54 msg.buffer = buf;
55
56 ret = _i2c_m_sync_transfer(&i2c->device, &msg);
57
58 if (ret) {
59 return ret;
60 }
61
62 return n;
63 }
64
65 /**
66 * \brief Sync version of I2C I/O write
67 */
i2c_m_sync_write(struct io_descriptor * io,const uint8_t * buf,const uint16_t n)68 static int32_t i2c_m_sync_write(struct io_descriptor *io, const uint8_t *buf, const uint16_t n)
69 {
70 struct i2c_m_sync_desc *i2c = CONTAINER_OF(io, struct i2c_m_sync_desc, io);
71 struct _i2c_m_msg msg;
72 int32_t ret;
73
74 msg.addr = i2c->slave_addr;
75 msg.len = n;
76 msg.flags = I2C_M_STOP;
77 msg.buffer = (uint8_t *)buf;
78
79 ret = _i2c_m_sync_transfer(&i2c->device, &msg);
80
81 if (ret) {
82 return ret;
83 }
84
85 return n;
86 }
87
88 /**
89 * \brief Sync version of i2c initialize
90 */
i2c_m_sync_init(struct i2c_m_sync_desc * i2c,void * hw)91 int32_t i2c_m_sync_init(struct i2c_m_sync_desc *i2c, void *hw)
92 {
93 int32_t init_status;
94 ASSERT(i2c);
95
96 init_status = _i2c_m_sync_init(&i2c->device, hw);
97 if (init_status) {
98 return init_status;
99 }
100
101 /* Init I/O */
102 i2c->io.read = i2c_m_sync_read;
103 i2c->io.write = i2c_m_sync_write;
104
105 return ERR_NONE;
106 }
107
108 /**
109 * \brief deinitialize
110 */
i2c_m_sync_deinit(struct i2c_m_sync_desc * i2c)111 int32_t i2c_m_sync_deinit(struct i2c_m_sync_desc *i2c)
112 {
113 int32_t status;
114 ASSERT(i2c);
115
116 status = _i2c_m_sync_deinit(&i2c->device);
117 if (status) {
118 return status;
119 }
120
121 i2c->io.read = NULL;
122 i2c->io.write = NULL;
123
124 return ERR_NONE;
125 }
126
127 /**
128 * \brief Sync version of i2c enable
129 */
i2c_m_sync_enable(struct i2c_m_sync_desc * i2c)130 int32_t i2c_m_sync_enable(struct i2c_m_sync_desc *i2c)
131 {
132 return _i2c_m_sync_enable(&i2c->device);
133 }
134
135 /**
136 * \brief Sync version of i2c disable
137 */
i2c_m_sync_disable(struct i2c_m_sync_desc * i2c)138 int32_t i2c_m_sync_disable(struct i2c_m_sync_desc *i2c)
139 {
140 return _i2c_m_sync_disable(&i2c->device);
141 }
142
143 /**
144 * \brief Sync version of i2c set slave address
145 */
i2c_m_sync_set_slaveaddr(struct i2c_m_sync_desc * i2c,int16_t addr,int32_t addr_len)146 int32_t i2c_m_sync_set_slaveaddr(struct i2c_m_sync_desc *i2c, int16_t addr, int32_t addr_len)
147 {
148 return i2c->slave_addr = (addr & 0x3ff) | (addr_len & I2C_M_TEN);
149 }
150
151 /**
152 * \brief Sync version of i2c set baudrate
153 */
i2c_m_sync_set_baudrate(struct i2c_m_sync_desc * i2c,uint32_t clkrate,uint32_t baudrate)154 int32_t i2c_m_sync_set_baudrate(struct i2c_m_sync_desc *i2c, uint32_t clkrate, uint32_t baudrate)
155 {
156 return _i2c_m_sync_set_baudrate(&i2c->device, clkrate, baudrate);
157 }
158
159 /**
160 * \brief Sync version of i2c write command
161 */
i2c_m_sync_cmd_write(struct i2c_m_sync_desc * i2c,uint8_t reg,uint8_t * buffer,uint8_t length)162 int32_t i2c_m_sync_cmd_write(struct i2c_m_sync_desc *i2c, uint8_t reg, uint8_t *buffer, uint8_t length)
163 {
164 struct _i2c_m_msg msg;
165 int32_t ret;
166
167 msg.addr = i2c->slave_addr;
168 msg.len = 1;
169 msg.flags = 0;
170 msg.buffer = ®
171
172 ret = _i2c_m_sync_transfer(&i2c->device, &msg);
173
174 if (ret != 0) {
175 /* error occurred */
176 return ret;
177 }
178
179 msg.flags = I2C_M_STOP;
180 msg.buffer = buffer;
181 msg.len = length;
182
183 ret = _i2c_m_sync_transfer(&i2c->device, &msg);
184
185 if (ret != 0) {
186 /* error occurred */
187 return ret;
188 }
189
190 return ERR_NONE;
191 }
192
193 /**
194 * \brief Sync version of i2c read command
195 */
i2c_m_sync_cmd_read(struct i2c_m_sync_desc * i2c,uint8_t reg,uint8_t * buffer,uint8_t length)196 int32_t i2c_m_sync_cmd_read(struct i2c_m_sync_desc *i2c, uint8_t reg, uint8_t *buffer, uint8_t length)
197 {
198 struct _i2c_m_msg msg;
199 int32_t ret;
200
201 msg.addr = i2c->slave_addr;
202 msg.len = 1;
203 msg.flags = 0;
204 msg.buffer = ®
205
206 ret = _i2c_m_sync_transfer(&i2c->device, &msg);
207
208 if (ret != 0) {
209 /* error occurred */
210 return ret;
211 }
212
213 msg.flags = I2C_M_STOP | I2C_M_RD;
214 msg.buffer = buffer;
215 msg.len = length;
216
217 ret = _i2c_m_sync_transfer(&i2c->device, &msg);
218
219 if (ret != 0) {
220 /* error occurred */
221 return ret;
222 }
223
224 return ERR_NONE;
225 }
226
227 /**
228 * \brief Sync version of i2c transfer command
229 */
i2c_m_sync_transfer(struct i2c_m_sync_desc * const i2c,struct _i2c_m_msg * msg)230 int32_t i2c_m_sync_transfer(struct i2c_m_sync_desc *const i2c, struct _i2c_m_msg *msg)
231 {
232 return _i2c_m_sync_transfer(&i2c->device, msg);
233 }
234
235 /**
236 * \brief Sync version of i2c send stop condition command
237 */
i2c_m_sync_send_stop(struct i2c_m_sync_desc * const i2c)238 int32_t i2c_m_sync_send_stop(struct i2c_m_sync_desc *const i2c)
239 {
240 return _i2c_m_sync_send_stop(&i2c->device);
241 }
242
243 /**
244 * \brief Retrieve I/O descriptor
245 */
i2c_m_sync_get_io_descriptor(struct i2c_m_sync_desc * const i2c,struct io_descriptor ** io)246 int32_t i2c_m_sync_get_io_descriptor(struct i2c_m_sync_desc *const i2c, struct io_descriptor **io)
247 {
248 *io = &i2c->io;
249 return ERR_NONE;
250 }
251
252 /**
253 * \brief Retrieve the current driver version
254 */
i2c_m_sync_get_version(void)255 uint32_t i2c_m_sync_get_version(void)
256 {
257 return DRIVER_VERSION;
258 }
259