1 /**
2  * \file
3  *
4  * \brief ADP service implementation
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 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 
47 #include <compiler.h>
48 #include <system.h>
49 
50 #include <asf.h>
51 #include <adp_interface.h>
52 
53 #define EDBG_TWI EDBG_I2C_MODULE
54 #define TWI_EDBG_SLAVE_ADDR 0x28
55 #define TIMEOUT 1000
56 
57 struct i2c_master_module i2c_master_instance;
58 
59 /**
60 * \brief Initialize EDBG I2C communication for SAM0
61 *
62 */
adp_interface_init(void)63 enum status_code adp_interface_init(void)
64 {
65 	enum status_code return_value;
66 
67 	system_init();
68 
69 	struct i2c_master_config config_i2c_master;
70 	i2c_master_get_config_defaults(&config_i2c_master);
71 	config_i2c_master.buffer_timeout = 10000;
72 	return_value = i2c_master_init(&i2c_master_instance, EDBG_TWI, &config_i2c_master);
73 	i2c_master_enable(&i2c_master_instance);
74 	return return_value;
75 }
76 
adp_interface_send(uint8_t * tx_buf,uint16_t length)77 static enum status_code adp_interface_send(uint8_t* tx_buf, uint16_t length)
78 {
79 	enum status_code status;
80 
81 	struct i2c_master_packet packet = {
82 		.address = TWI_EDBG_SLAVE_ADDR,
83 		.data_length = length,
84 		.data = tx_buf,
85 	};
86 	/* Send data to PC */
87 	status = i2c_master_write_packet_wait(&i2c_master_instance, &packet);
88 
89 	return status;
90 }
91 
92 /**
93 * \brief Read response on I2C from PC
94 *
95 * return Status
96 * \param[in]  rx_buf  Pointer to receive the data
97 * \param[in]  length  The length of the read data
98 * \param[out] rx_buf  Pointer to store the received SPI character
99 */
adp_interface_read_response(uint8_t * rx_buf,uint16_t length)100 enum status_code adp_interface_read_response(uint8_t* rx_buf, uint16_t length)
101 {
102 	enum status_code status = STATUS_ERR_IO;
103 	uint8_t data_len = 0;
104 
105 	struct i2c_master_packet packet = {
106 		.address = TWI_EDBG_SLAVE_ADDR,
107 		.data_length = 1,
108 		.data = &data_len,
109 	};
110 	i2c_master_read_packet_wait(&i2c_master_instance, &packet);
111 
112 	if (data_len != 0)
113 	{
114 		packet.data_length = data_len;
115 		packet.data = rx_buf;
116 		status = i2c_master_read_packet_wait(&i2c_master_instance, &packet);
117 	}
118 
119 	return status;
120 }
121 
122 /**
123 * \brief Sends and reads protocol packet data byte on I2C
124 *
125 * \param[in]  tx_buf  Pointer to send the protocol packet data
126 * \param[in]  length  The length of the send protocol packet data
127 * \param[out] rx_buf  Pointer to store the received I2C character
128 */
adp_interface_transceive_procotol(uint8_t * tx_buf,uint16_t length,uint8_t * rx_buf)129 void adp_interface_transceive_procotol(uint8_t* tx_buf, uint16_t length, uint8_t* rx_buf)
130 {
131 	adp_interface_send(tx_buf, length);
132 	adp_interface_read_response(rx_buf, length);
133 }
134