1 /**
2  * \file
3  *
4  * \brief I2C Master Driver for SAMB
5  *
6  * Copyright (C) 2015-2016 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 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 
47 #include "i2c_master.h"
48 #if I2C_MASTER_CALLBACK_MODE == true
49 #  include "i2c_master_interrupt.h"
50 #endif
51 
52 /**
53  * \brief Gets the I<SUP>2</SUP>C master default configurations
54  *
55  * Use to initialize the configuration structure to known default values.
56  *
57  * The default configuration is as follows:
58  * - Baudrate 100KHz
59  * - Clock sourc I2C_CLK_INPUT_3
60  * - Clock divider = 0x10
61  * - Pinmux pad0 PINMUX_LP_GPIO_8_MUX2_I2C0_SDA
62  * - Pinmux pad1 PINMUX_LP_GPIO_9_MUX2_I2C0_SCK
63  *
64  * \param[out] config  Pointer to configuration structure to be initiated
65  */
i2c_master_get_config_defaults(struct i2c_master_config * const config)66 void i2c_master_get_config_defaults(
67 		struct i2c_master_config *const config)
68 {
69 	/* Sanity check */
70 	Assert(config);
71 
72 	config->clock_source    = I2C_CLK_INPUT_3;
73 	config->clock_divider   = 0x10;
74 	config->pin_number_pad0 = PIN_LP_GPIO_8;
75 	config->pin_number_pad1 = PIN_LP_GPIO_9;
76 	config->pinmux_sel_pad0 = MUX_LP_GPIO_8_I2C0_SDA;
77 	config->pinmux_sel_pad1 = MUX_LP_GPIO_9_I2C0_SCL;
78 }
79 
80 #if !defined(__DOXYGEN__)
81 /**
82  * \internal Sets configurations to module
83  *
84  * \param[out] module  Pointer to software module structure
85  * \param[in]  config  Configuration structure with configurations to set
86  *
87  */
_i2c_master_set_config(struct i2c_master_module * const module,const struct i2c_master_config * const config)88 static void _i2c_master_set_config(
89 		struct i2c_master_module *const module,
90 		const struct i2c_master_config *const config)
91 {
92 	/* Sanity check */
93 	Assert(module);
94 	Assert(module->hw);
95 	Assert(config);
96 
97 	I2c *const i2c_module = (module->hw);
98 
99 	/* Set the pinmux for this i2c module. */
100 	gpio_pinmux_cofiguration(config->pin_number_pad0, (uint16_t)(config->pinmux_sel_pad0));
101 	gpio_pinmux_cofiguration(config->pin_number_pad1, (uint16_t)(config->pinmux_sel_pad1));
102 	/* Set clock. */
103 	i2c_module->CLOCK_SOURCE_SELECT.reg = config->clock_source;
104 	i2c_module->I2C_CLK_DIVIDER.reg = I2C_CLK_DIVIDER_I2C_DIVIDE_RATIO(config->clock_divider);
105 	/* Enable master mode. */
106 	i2c_module->I2C_MASTER_MODE.reg = I2C_MASTER_MODE_MASTER_ENABLE_1;
107 }
108 #endif /* __DOXYGEN__ */
109 
110 /**
111  * \brief Initializes the requested I<SUP>2</SUP>C hardware module
112  *
113  * Initializes the I<SUP>2</SUP>C master device requested and sets the provided
114  * software module struct. Run this function before any further use of
115  * the driver.
116  *
117  * \param[out] module  Pointer to software module struct
118  * \param[in]  config  Pointer to the configuration struct
119  *
120  * \return Status of initialization.
121  * \retval STATUS_OK                        Module initiated correctly
122  * \retval STATUS_ERR_INVALID_ARG           Invalid argument in module or config structure.
123  * \retval STATUS_ERR_ALREADY_INITIALIZED   If the Pinmux is not a valid one for I2C signals.
124  *
125  */
i2c_master_init(struct i2c_master_module * const module,I2c * const hw,const struct i2c_master_config * const config)126 enum status_code i2c_master_init(
127 		struct i2c_master_module *const module,
128 		I2c *const hw,
129 		const struct i2c_master_config *const config)
130 {
131 	/* Sanity check */
132 	Assert(module);
133 	Assert(module->hw);
134 	Assert(config);
135 
136 	module->hw = hw;
137 
138 	/* Sanity check arguments. */
139 	if ((module == NULL) || (config == NULL))
140 		return STATUS_ERR_INVALID_ARG;
141 
142 	i2c_disable(module->hw);
143 	if (module->hw == I2C0) {
144 		system_peripheral_reset(PERIPHERAL_I2C0_CORE);
145 	} else if (module->hw == I2C1) {
146 		system_peripheral_reset(PERIPHERAL_I2C1_CORE);
147 	} else {
148 		return STATUS_ERR_INVALID_ARG;
149 	}
150 
151 #if I2C_MASTER_CALLBACK_MODE == true
152 	/* Initialize values in module. */
153 	module->registered_callback = 0;
154 	module->enabled_callback    = 0;
155 	module->buffer_length       = 0;
156 	module->buffer_remaining    = 0;
157 	module->status              = STATUS_OK;
158 	module->buffer              = NULL;
159 
160 	_i2c_instances = (void*)module;
161 	if (module->hw == I2C0) {
162 		system_register_isr(RAM_ISR_TABLE_I2CRX0_INDEX, (uint32_t)_i2c_master_isr_handler);
163 		system_register_isr(RAM_ISR_TABLE_I2CTX0_INDEX, (uint32_t)_i2c_master_isr_handler);
164 		NVIC_EnableIRQ(I2C0_RX_IRQn);
165 		NVIC_EnableIRQ(I2C0_TX_IRQn);
166 	} else if (module->hw == I2C1) {
167 		system_register_isr(RAM_ISR_TABLE_I2CRX1_INDEX, (uint32_t)_i2c_master_isr_handler);
168 		system_register_isr(RAM_ISR_TABLE_I2CTX1_INDEX, (uint32_t)_i2c_master_isr_handler);
169 		NVIC_EnableIRQ(I2C1_RX_IRQn);
170 		NVIC_EnableIRQ(I2C1_TX_IRQn);
171 	}
172 #endif
173 
174 	/* Set config and return status. */
175 	_i2c_master_set_config(module, config);
176 
177 	return STATUS_OK;
178 }
179 
180 /**
181  * \internal
182  * Starts blocking read operation.
183  *
184  * \param[in,out] module  Pointer to software module struct
185  * \param[in,out] packet  Pointer to I<SUP>2</SUP>C packet to transfer
186  *
187  * \return Status of reading packet.
188  * \retval STATUS_OK                    The packet was read successfully
189  * \retval STATUS_ERR_TIMEOUT           If no response was given within
190  *                                      specified timeout period
191  * \retval STATUS_ERR_DENIED            If error on bus
192  * \retval STATUS_ERR_PACKET_COLLISION  If arbitration is lost
193  * \retval STATUS_ERR_BAD_ADDRESS       If slave is busy, or no slave
194  *                                      acknowledged the address
195  */
_i2c_master_read_packet(struct i2c_master_module * const module,struct i2c_master_packet * const packet)196 static enum status_code _i2c_master_read_packet(
197 		struct i2c_master_module *const module,
198 		struct i2c_master_packet *const packet)
199 {
200 	/* Sanity check */
201 	Assert(module);
202 	Assert(module->hw);
203 	Assert(config);
204 
205 	uint16_t counter = 0;
206 	uint32_t status  = 0;
207 	I2c *const i2c_module    = (module->hw);
208 	uint16_t length = packet->data_length;
209 
210 	if (length == 0) {
211 		return STATUS_ERR_INVALID_ARG;
212 	}
213 
214 	i2c_wait_for_idle(i2c_module);
215 
216 	/* Flush the FIFO */
217 	i2c_module->I2C_FLUSH.reg = 1;
218 
219 	/* Enable I2C on bus (start condition). */
220 	i2c_module->I2C_ONBUS.reg = I2C_ONBUS_ONBUS_ENABLE_1;
221 	/* Address I2C slave in case of Master mode enabled. */
222 	i2c_module->TRANSMIT_DATA.reg = I2C_TRANSMIT_DATA_ADDRESS_FLAG_1 |
223 			(packet->address << 1) | I2C_TRANSFER_READ;
224 
225 	/* Now check whether the core has sent the data out and free the bus. */
226 	while (!(status & I2C_TRANSMIT_STATUS_TX_FIFO_EMPTY)) {
227 		status = i2c_module->TRANSMIT_STATUS.reg;
228 	}
229 
230 	do {
231 		/* Send stop condition. */
232 		if ((!module->no_stop) && (counter == (length - 1))) {
233 			i2c_module->I2C_ONBUS.reg = I2C_ONBUS_ONBUS_ENABLE_0;
234 		}
235 
236 		status = i2c_module->RECEIVE_STATUS.reg;
237 		if (status & I2C_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY)
238 			packet->data[counter++] = i2c_module->RECEIVE_DATA.reg;
239 	} while (counter < length);
240 
241 	return STATUS_OK;
242 }
243 
244 /**
245  * \brief Reads data packet from slave
246  *
247  * Reads a data packet from the specified slave address on the I<SUP>2</SUP>C
248  * bus and sends a stop condition when finished.
249  *
250  * \note This will stall the device from any other operation. For
251  *       interrupt-driven operation, see \ref i2c_master_read_packet_job.
252  *
253  * \param[in,out] module  Pointer to software module struct
254  * \param[in,out] packet  Pointer to I<SUP>2</SUP>C packet to transfer
255  *
256  * \return Status of reading packet.
257  * \retval STATUS_OK                    The packet was read successfully
258  * \retval STATUS_ERR_INVALID_ARG       Invalid argument in module or config strucuture
259  *                                      specified timeout period
260  * \retval STATUS_BUSY                  If module has a pending request.
261  */
i2c_master_read_packet_wait(struct i2c_master_module * const module,struct i2c_master_packet * const packet)262 enum status_code i2c_master_read_packet_wait(
263 		struct i2c_master_module *const module,
264 		struct i2c_master_packet *const packet)
265 {
266 	/* Sanity check */
267 	Assert(module);
268 	Assert(module->hw);
269 	Assert(packet);
270 
271 	if((module == NULL) || (packet == NULL))
272 		return STATUS_ERR_INVALID_ARG;
273 
274 #if I2C_MASTER_CALLBACK_MODE == true
275 	/* Check if the I2C module is busy with a job. */
276 	if (module->buffer_remaining > 0) {
277 		return STATUS_BUSY;
278 	}
279 #endif
280 
281 	module->no_stop = false;
282 
283 	return _i2c_master_read_packet(module, packet);
284 }
285 
286 /**
287  * \brief Reads data packet from slave without sending a stop condition when done
288  *
289  * Reads a data packet from the specified slave address on the I<SUP>2</SUP>C
290  * bus without sending a stop condition when done, thus retaining ownership of
291  * the bus when done. To end the transaction, a
292  * \ref i2c_master_read_packet_wait "read" or
293  * \ref i2c_master_write_packet_wait "write" with stop condition must be
294  * performed.
295  *
296  * \note This will stall the device from any other operation. For
297  *       interrupt-driven operation, see \ref i2c_master_read_packet_job.
298  *
299  * \param[in,out] module  Pointer to software module struct
300  * \param[in,out] packet  Pointer to I<SUP>2</SUP>C packet to transfer
301  *
302  * \return Status of reading packet.
303  * \retval STATUS_OK                    The packet was read successfully
304  * \retval STATUS_ERR_INVALID_ARG       Invalid argument in module or config strucuture
305  *                                      specified timeout period
306  * \retval STATUS_BUSY                  If module has a pending request.
307  */
i2c_master_read_packet_wait_no_stop(struct i2c_master_module * const module,struct i2c_master_packet * const packet)308 enum status_code i2c_master_read_packet_wait_no_stop(
309 		struct i2c_master_module *const module,
310 		struct i2c_master_packet *const packet)
311 {
312 	/* Sanity check */
313 	Assert(module);
314 	Assert(module->hw);
315 	Assert(packet);
316 
317 	if((module == NULL) || (packet == NULL))
318 		return STATUS_ERR_INVALID_ARG;
319 
320 #if I2C_MASTER_CALLBACK_MODE == true
321 	/* Check if the I2C module is busy with a job. */
322 	if (module->buffer_remaining > 0) {
323 		return STATUS_BUSY;
324 	}
325 #endif
326 
327 	module->no_stop = true;
328 
329 	return _i2c_master_read_packet(module, packet);
330 }
331 
332 /**
333  * \internal
334  * Starts blocking write operation.
335  *
336  * \param[in,out] module  Pointer to software module struct
337  * \param[in,out] packet  Pointer to I<SUP>2</SUP>C packet to transfer
338  *
339  * \return Status of write packet.
340  * \retval STATUS_OK                    The packet was write successfully
341  */
_i2c_master_write_packet(struct i2c_master_module * const module,struct i2c_master_packet * const packet)342 static enum status_code _i2c_master_write_packet(
343 		struct i2c_master_module *const module,
344 		struct i2c_master_packet *const packet)
345 {
346 	/* Sanity check */
347 	Assert(module);
348 	Assert(module->hw);
349 	Assert(packet);
350 
351 	I2c *const i2c_module = (module->hw);
352 	uint16_t counter = 0;
353 	uint32_t status  = 0;
354 
355 	uint16_t length = packet->data_length;
356 
357 	i2c_wait_for_idle(i2c_module);
358 
359 	/* Flush the FIFO */
360 	i2c_module->I2C_FLUSH.reg = 1;
361 
362 	/* Enable I2C on bus (start condition) */
363 	i2c_module->I2C_ONBUS.reg = I2C_ONBUS_ONBUS_ENABLE_1;
364 
365 	/* Address I2C slave in case of Master mode enabled */
366 	i2c_module->TRANSMIT_DATA.reg = I2C_TRANSMIT_DATA_ADDRESS_FLAG_1 |
367 			((packet->address) << 1) | I2C_TRANSFER_WRITE;
368 	do {
369 		status = i2c_module->TRANSMIT_STATUS.reg;
370 		if (status & I2C_TRANSMIT_STATUS_TX_FIFO_NOT_FULL_Msk) {
371 			i2c_module->TRANSMIT_DATA.reg = packet->data[counter++];
372 		}
373 	} while (counter < length);
374 
375 	/* Now check whether the core has sent the data out and free the bus */
376 	while (!(status & I2C_TRANSMIT_STATUS_TX_FIFO_EMPTY)) {
377 			status = i2c_module->TRANSMIT_STATUS.reg;
378 	}
379 
380 	/* Send stop condition */
381 	if (!module->no_stop) {
382 		i2c_module->I2C_ONBUS.reg = I2C_ONBUS_ONBUS_ENABLE_0;
383 	}
384 
385 	return STATUS_OK;
386 }
387 
388 /**
389  * \brief Writes data packet to slave
390  *
391  * Writes a data packet to the specified slave address on the I<SUP>2</SUP>C bus
392  * and sends a stop condition when finished.
393  *
394  * \note This will stall the device from any other operation. For
395  *       interrupt-driven operation, see \ref i2c_master_read_packet_job.
396  *
397  * \param[in,out] module  Pointer to software module struct
398  * \param[in,out] packet  Pointer to I<SUP>2</SUP>C packet to transfer
399  *
400  * \return Status of write packet.
401  * \retval STATUS_OK                    The packet was written successfully
402  * \retval STATUS_ERR_INVALID_ARG       Invalid argument in module or packet structure
403  *                                      specified timeout period
404  * \retval STATUS_BUSY                  If module has a pending request.
405  */
i2c_master_write_packet_wait(struct i2c_master_module * const module,struct i2c_master_packet * const packet)406 enum status_code i2c_master_write_packet_wait(
407 		struct i2c_master_module *const module,
408 		struct i2c_master_packet *const packet)
409 {
410 	/* Sanity check arguments. */
411 	Assert(module);
412 	Assert(module->hw);
413 	Assert(packet);
414 
415 	if ((module == NULL) || (packet == NULL)) {
416 		return STATUS_ERR_INVALID_ARG;
417 	}
418 #if I2C_MASTER_CALLBACK_MODE == true
419 	/* Check if the I2C module is busy with a job. */
420 	if (module->buffer_remaining > 0) {
421 		return STATUS_BUSY;
422 	}
423 #endif
424 
425 	module->no_stop = false;
426 
427 	return _i2c_master_write_packet(module, packet);
428 }
429 
430 /**
431  * \brief Writes data packet to slave without sending a stop condition when done
432  *
433  * Writes a data packet to the specified slave address on the I<SUP>2</SUP>C bus
434  * without sending a stop condition, thus retaining ownership of the bus when
435  * done. To end the transaction, a \ref i2c_master_read_packet_wait "read" or
436  * \ref i2c_master_write_packet_wait "write" with stop condition or sending a
437  * stop with the \ref i2c_master_send_stop function must be performed.
438  *
439  * \note This will stall the device from any other operation. For
440  *       interrupt-driven operation, see \ref i2c_master_read_packet_job.
441  *
442  * \param[in,out] module  Pointer to software module struct
443  * \param[in,out] packet  Pointer to I<SUP>2</SUP>C packet to transfer
444  *
445  * \return Status of write packet.
446  * \retval STATUS_OK                    The packet was written successfully
447  * \retval STATUS_ERR_INVALID_ARG       Invalid argument in module or config structure
448  *                                      specified timeout period
449  * \retval STATUS_BUSY                  If module has a pending request.
450  */
i2c_master_write_packet_wait_no_stop(struct i2c_master_module * const module,struct i2c_master_packet * const packet)451 enum status_code i2c_master_write_packet_wait_no_stop(
452 		struct i2c_master_module *const module,
453 		struct i2c_master_packet *const packet)
454 {
455 	/* Sanity check */
456 	Assert(module);
457 	Assert(module->hw);
458 	Assert(packet);
459 
460 	if((module == NULL) || (packet == NULL)) {
461 		return STATUS_ERR_INVALID_ARG;
462 	}
463 #if I2C_MASTER_CALLBACK_MODE == true
464 	/* Check if the I2C module is busy with a job */
465 	if (module->buffer_remaining > 0) {
466 		return STATUS_BUSY;
467 	}
468 #endif
469 
470 	module->no_stop = true;
471 
472 	return _i2c_master_write_packet(module, packet);
473 }
474 
475 /**
476  * \brief Sends stop condition on bus
477  *
478  * Sends a stop condition on bus.
479  *
480  * \note This function can only be used after the
481  *       \ref i2c_master_write_packet_wait_no_stop function. If a stop condition
482  *       is to be sent after a read, the \ref i2c_master_read_packet_wait
483  *       function must be used.
484  *
485  * \param[in,out] module  Pointer to the software instance struct
486  */
i2c_master_send_stop(struct i2c_master_module * const module)487 void i2c_master_send_stop(struct i2c_master_module *const module)
488 {
489 	/* Sanity check */
490 	Assert(module);
491 	Assert(module->hw);
492 
493 	I2c *const i2c_module = (module->hw);
494 
495 	/* Send stop command */
496 	i2c_wait_for_idle(i2c_module);
497 
498 	i2c_module->I2C_ONBUS.reg = I2C_ONBUS_ONBUS_ENABLE_0;
499 }
500 
501 /**
502  * \brief Sends start condition on bus
503  *
504  * Sends a start condition on bus.
505  *
506  * \note This function can only be used after the
507  * \ref i2c_master_write_packet_wait_no_stop function. If a stop condition
508  * is to be sent after a read, the \ref i2c_master_read_packet_wait
509  * function must be used.
510  *
511  * \param[in,out] module  Pointer to the software instance struct
512  */
i2c_master_send_start(struct i2c_master_module * const module)513 void i2c_master_send_start(struct i2c_master_module *const module)
514 {
515 	I2c *const i2c_module = (module->hw);
516 
517 	i2c_wait_for_idle(i2c_module);
518 
519 	/* Send start command */
520 	i2c_module->I2C_ONBUS.reg = I2C_ONBUS_ONBUS_ENABLE_1;
521 }
522 
523 /**
524  * \brief Reads one byte data from slave
525  *
526  * \param[in,out]  module  Pointer to software module struct
527  * \param[out]     byte    Read one byte data to slave
528  *
529  * \return Status of reading byte.
530  * \retval STATUS_OK                    The packet was read successfully
531  */
i2c_master_read_byte(struct i2c_master_module * const module,uint8_t * byte)532 enum status_code i2c_master_read_byte(
533 		struct i2c_master_module *const module,
534 		uint8_t *byte)
535 {
536 	I2c *const i2c_module = (module->hw);
537 
538 	/* Read a byte from slave. */
539 	i2c_wait_for_idle(i2c_module);
540 
541 	*byte = i2c_module->RECEIVE_DATA.bit.RX_BYTE;
542 
543 	return STATUS_OK;
544 }
545 
546 /**
547  * \brief Write Address & command to slave
548  *
549  * \param[in,out]  module  Pointer to software module struct
550  * \param[in]      byte    Address of slave
551  * \param[in]      byte    command 0 - Write, 1 - Read
552  *
553  * \return Status of writing byte.
554  * \retval STATUS_OK   The Address and command was written successfully
555  */
i2c_master_write_address(struct i2c_master_module * const module,uint8_t address,uint8_t command)556 enum status_code i2c_master_write_address(
557 		struct i2c_master_module *const module,
558 		uint8_t address,
559 		uint8_t command)
560 {
561 	I2c *const i2c_module = (module->hw);
562 
563 	/* Write byte to slave. */
564 	i2c_wait_for_idle(i2c_module);
565 
566 	i2c_module->TRANSMIT_DATA.reg = I2C_TRANSMIT_DATA_ADDRESS_FLAG_1 |
567 			(address << 1) | command;
568 
569 	return STATUS_OK;
570 }
571 
572 
573 /**
574  * \brief Write one byte data to slave
575  *
576  * \param[in,out]  module  Pointer to software module struct
577  * \param[in]      byte    Send one byte data to slave
578  *
579  * \return Status of writing byte.
580  * \retval STATUS_OK   One byte was written successfully
581  */
i2c_master_write_byte(struct i2c_master_module * const module,uint8_t byte)582 enum status_code i2c_master_write_byte(
583 		struct i2c_master_module *const module,
584 		uint8_t byte)
585 {
586 	I2c *const i2c_module = (module->hw);
587 
588 	/* Write byte to slave. */
589 	i2c_wait_for_idle(i2c_module);
590 
591 	i2c_module->TRANSMIT_DATA.reg = (uint16_t)I2C_TRANSMIT_DATA_TX_DATA(byte);
592 
593 	return STATUS_OK;
594 }
595