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 struct spi_module edbg_spi;
54 struct spi_slave_inst slave;
55 
56 /**
57 * \brief Send SPI start condition
58 *
59 */
adp_interface_send_start(void)60 static void adp_interface_send_start(void)
61 {
62 	spi_select_slave(&edbg_spi, &slave, true);
63 }
64 
65 /**
66 * \brief Send SPI stop condition
67 *
68 */
adp_interface_send_stop(void)69 static void adp_interface_send_stop(void)
70 {
71 	spi_select_slave(&edbg_spi, &slave, false);
72 }
73 
74 /**
75 * \brief Sends and reads data byte on SPI
76 *
77 * \param[in]  data     Data byte to send
78 * \param[in]  tx_data  SPI character to transmit
79 * \param[out] rx_data  Pointer to store the received SPI character
80 */
adp_interface_transceive(uint8_t * tx_data,uint8_t * rx_data,uint16_t length)81 static void adp_interface_transceive(uint8_t *tx_data, uint8_t *rx_data, uint16_t length)
82 {
83 	spi_transceive_buffer_wait(&edbg_spi, tx_data, rx_data, length);
84 }
85 
86 /**
87 * \brief Initialize EDBG SPI communication for SAM0
88 *
89 */
adp_interface_init(void)90 enum status_code adp_interface_init(void)
91 {
92 	enum status_code return_value;
93 
94 	system_init();
95 
96 	struct spi_slave_inst_config slave_dev_config;
97 
98 	struct spi_config config;
99 
100 	spi_slave_inst_get_config_defaults(&slave_dev_config);
101 	slave_dev_config.ss_pin = (EDBG_SPI_SERCOM_PINMUX_PAD1 >> 16) & 0xFF;
102 	spi_attach_slave(&slave, &slave_dev_config);
103 
104 	spi_get_config_defaults(&config);
105 	config.mode_specific.master.baudrate = 1000000;
106 	config.mux_setting = EDBG_SPI_SERCOM_MUX_SETTING;
107 	config.pinmux_pad0 = EDBG_SPI_SERCOM_PINMUX_PAD0;
108 	config.pinmux_pad1 = PINMUX_UNUSED;
109 	config.pinmux_pad2 = EDBG_SPI_SERCOM_PINMUX_PAD2;
110 	config.pinmux_pad3 = EDBG_SPI_SERCOM_PINMUX_PAD3;
111 
112 	return_value = spi_init(&edbg_spi, EDBG_SPI_MODULE, &config);
113 
114 	spi_enable(&edbg_spi);
115 
116 	return return_value;
117 }
118 
119 /**
120 * \brief Sends and reads protocol packet data byte on SPI
121 *
122 * \param[in]  tx_buf  Pointer to send the protocol packet data
123 * \param[in]  length  The length of the send protocol packet data
124 * \param[out] rx_buf  Pointer to store the received SPI character
125 */
adp_interface_transceive_procotol(uint8_t * tx_buf,uint16_t length,uint8_t * rx_buf)126 void adp_interface_transceive_procotol(uint8_t* tx_buf, uint16_t length, uint8_t* rx_buf)
127 {
128 	/* Send SPI start condition */
129 	adp_interface_send_start();
130 
131 	adp_interface_transceive(tx_buf, rx_buf, length);
132 
133 	/* Send SPI end condition */
134 	adp_interface_send_stop();
135 }
136 
137 /**
138 * \brief Read response on SPI from PC
139 *
140 * return Status
141 * \param[in]  rx_buf  Pointer to receive the data
142 * \param[in]  length  The length of the read data
143 * \param[out] rx_buf  Pointer to store the received SPI character
144 */
adp_interface_read_response(uint8_t * rx_buf,uint16_t length)145 enum status_code adp_interface_read_response(uint8_t* rx_buf, uint16_t length)
146 {
147 	bool status;
148 
149 	/* Send SPI start condition */
150 	adp_interface_send_start();
151 	status = spi_read_buffer_wait(&edbg_spi, rx_buf, length, 0xFF);
152 	/* Send SPI end condition */
153 	adp_interface_send_stop();
154 
155 	return status;
156 }