1 /**
2  * \file
3  *
4  * \brief SPI master common service for SAM.
5  *
6  * Copyright (c) 2011-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 "spi_master.h"
48 #if SAMG55
49 #include "flexcom.h"
50 #include "conf_board.h"
51 #endif
52 
53 /**
54  * \brief Max number when the chip selects are connected to a 4- to 16-bit decoder.
55  */
56 #define MAX_NUM_WITH_DECODER 0x10
57 
58 /**
59  * \brief Max number when the chip selects are directly connected to peripheral device.
60  */
61 #define MAX_NUM_WITHOUT_DECODER 0x04
62 
63 /**
64  * \brief Max number when the chip selects are directly connected to peripheral device.
65  */
66 #define NONE_CHIP_SELECT_ID 0x0f
67 
68 /**
69  * \brief The default chip select id.
70  */
71 #define DEFAULT_CHIP_ID 0
72 
73 /** \brief Initialize the SPI in master mode.
74  *
75  * \param p_spi  Base address of the SPI instance.
76  *
77  */
spi_master_init(Spi * p_spi)78 void spi_master_init(Spi *p_spi)
79 {
80 #if SAMG55
81 	flexcom_enable(BOARD_FLEXCOM_SPI);
82 	flexcom_set_opmode(BOARD_FLEXCOM_SPI, FLEXCOM_SPI);
83 #else
84 	spi_enable_clock(p_spi);
85 #endif
86 	spi_reset(p_spi);
87 	spi_set_master_mode(p_spi);
88 	spi_disable_mode_fault_detect(p_spi);
89 	spi_disable_loopback(p_spi);
90 	spi_set_peripheral_chip_select_value(p_spi, DEFAULT_CHIP_ID);
91 	spi_set_fixed_peripheral_select(p_spi);
92 	spi_disable_peripheral_select_decode(p_spi);
93 	spi_set_delay_between_chip_select(p_spi, CONFIG_SPI_MASTER_DELAY_BCS);
94 }
95 
96 /**
97  * \brief Set up an SPI device.
98  *
99  * The returned device descriptor structure must be passed to the driver
100  * whenever that device should be used as current slave device.
101  *
102  * \param p_spi     Base address of the SPI instance.
103  * \param device    Pointer to SPI device struct that should be initialized.
104  * \param flags     SPI configuration flags. Common flags for all
105  *                  implementations are the SPI modes SPI_MODE_0 ...
106  *                  SPI_MODE_3.
107  * \param baud_rate Baud rate for communication with slave device in Hz.
108  * \param sel_id    Board specific select id.
109  */
spi_master_setup_device(Spi * p_spi,struct spi_device * device,spi_flags_t flags,uint32_t baud_rate,board_spi_select_id_t sel_id)110 void spi_master_setup_device(Spi *p_spi, struct spi_device *device,
111 		spi_flags_t flags, uint32_t baud_rate, board_spi_select_id_t sel_id)
112 {
113 #if (SAM4L)
114     int16_t baud_div = spi_calc_baudrate_div(baud_rate, sysclk_get_pba_hz());
115 #else
116 	int16_t baud_div = spi_calc_baudrate_div(baud_rate, sysclk_get_peripheral_hz());
117 #endif
118 	/* avoid Cppcheck Warning */
119 	UNUSED(sel_id);
120 	if (-1 == baud_div) {
121 		Assert(0 == "Failed to find baudrate divider");
122 	}
123 	spi_set_transfer_delay(p_spi, device->id, CONFIG_SPI_MASTER_DELAY_BS,
124 			CONFIG_SPI_MASTER_DELAY_BCT);
125 	spi_set_bits_per_transfer(p_spi, device->id,
126 			CONFIG_SPI_MASTER_BITS_PER_TRANSFER);
127 	spi_set_baudrate_div(p_spi, device->id, baud_div);
128 	spi_configure_cs_behavior(p_spi, device->id, SPI_CS_KEEP_LOW);
129 	spi_set_clock_polarity(p_spi, device->id, flags >> 1);
130 	spi_set_clock_phase(p_spi, device->id, ((flags & 0x1) ^ 0x1));
131 }
132 
133 /**
134  * \brief Select the given device on the SPI bus.
135  *
136  * Set device specific setting and call board chip select.
137  *
138  * \param p_spi   Base address of the SPI instance.
139  * \param device  SPI device.
140  *
141  */
spi_select_device(Spi * p_spi,struct spi_device * device)142 void spi_select_device(Spi *p_spi, struct spi_device *device)
143 {
144 	if (spi_get_peripheral_select_decode_setting(p_spi)) {
145 		if (device->id < MAX_NUM_WITH_DECODER) {
146 			spi_set_peripheral_chip_select_value(p_spi, device->id);
147 		}
148 	} else {
149 		if (device->id < MAX_NUM_WITHOUT_DECODER) {
150 			spi_set_peripheral_chip_select_value(p_spi, (~(1 << device->id)));
151 		}
152 	}
153 }
154 
155 /**
156  * \brief Deselect the given device on the SPI bus.
157  *
158  * Call board chip deselect.
159  *
160  * \param p_spi   Base address of the SPI instance.
161  * \param device  SPI device.
162  *
163  * \pre SPI device must be selected with spi_select_device() first.
164  */
spi_deselect_device(Spi * p_spi,struct spi_device * device)165 void spi_deselect_device(Spi *p_spi, struct spi_device *device)
166 {
167 	/* avoid Cppcheck Warning */
168 	UNUSED(device);
169 	while (!spi_is_tx_empty(p_spi)) {
170 	}
171 
172 	// Assert all lines; no peripheral is selected.
173 	spi_set_peripheral_chip_select_value(p_spi, NONE_CHIP_SELECT_ID);
174 
175 	// Last transfer, so de-assert the current NPCS if CSAAT is set.
176 	spi_set_lastxfer(p_spi);
177 
178 }
179 
180 /**
181  * \brief Send a sequence of bytes to an SPI device.
182  *
183  * Received bytes on the SPI bus are discarded.
184  *
185  * \param p_spi     Base address of the SPI instance.
186  * \param data      Data buffer to write.
187  * \param len       Length of data to be written.
188  *
189  * \pre SPI device must be selected with spi_select_device() first.
190  */
spi_write_packet(Spi * p_spi,const uint8_t * data,size_t len)191 status_code_t spi_write_packet(Spi *p_spi, const uint8_t *data,
192 		size_t len)
193 {
194 	uint32_t timeout = SPI_TIMEOUT;
195 	uint32_t i = 0;
196 	uint8_t val;
197 
198 	while (len) {
199 		timeout = SPI_TIMEOUT;
200 		while (!spi_is_tx_ready(p_spi)) {
201 			if (!timeout--) {
202 				return ERR_TIMEOUT;
203 			}
204 		}
205 		val = data[i];
206 		spi_write_single(p_spi, val);
207 		i++;
208 		len--;
209 	}
210 
211 	return STATUS_OK;
212 }
213 
214 /**
215  * \brief Receive a sequence of bytes from an SPI device.
216  *
217  * All bytes sent out on SPI bus are sent as value 0.
218  *
219  * \param p_spi     Base address of the SPI instance.
220  * \param data      Data buffer to read.
221  * \param len       Length of data to be read.
222  *
223  * \pre SPI device must be selected with spi_select_device() first.
224  */
spi_read_packet(Spi * p_spi,uint8_t * data,size_t len)225 status_code_t spi_read_packet(Spi *p_spi, uint8_t *data, size_t len)
226 {
227 	uint32_t timeout = SPI_TIMEOUT;
228 	uint8_t val;
229 	uint32_t i = 0;
230 
231 	while (len) {
232 		timeout = SPI_TIMEOUT;
233 		while (!spi_is_tx_ready(p_spi)) {
234 			if (!timeout--) {
235 				return ERR_TIMEOUT;
236 			}
237 		}
238 		spi_write_single(p_spi, CONFIG_SPI_MASTER_DUMMY);
239 
240 		timeout = SPI_TIMEOUT;
241 		while (!spi_is_rx_ready(p_spi)) {
242 			if (!timeout--) {
243 				return ERR_TIMEOUT;
244 			}
245 		}
246 		spi_read_single(p_spi, &val);
247 
248 		data[i] = val;
249 		i++;
250 		len--;
251 	}
252 
253 	return STATUS_OK;
254 }
255 
256 /**
257  * \brief Send and receive a sequence of bytes from an SPI device.
258  *
259  * \param p_spi     Base address of the SPI instance.
260  * \param tx_data   Data buffer to send.
261  * \param rx_data   Data buffer to read.
262  * \param len       Length of data to be read.
263  *
264  * \pre SPI device must be selected with spi_select_device() first.
265  */
spi_transceive_packet(Spi * p_spi,uint8_t * tx_data,uint8_t * rx_data,size_t len)266 status_code_t spi_transceive_packet(Spi *p_spi, uint8_t *tx_data, uint8_t *rx_data, size_t len)
267 {
268 	uint32_t timeout = SPI_TIMEOUT;
269 	uint8_t val;
270 	uint32_t i = 0;
271 
272 	while (len) {
273 		timeout = SPI_TIMEOUT;
274 		while (!spi_is_tx_ready(p_spi)) {
275 			if (!timeout--) {
276 				return ERR_TIMEOUT;
277 			}
278 		}
279 		spi_write_single(p_spi, tx_data[i]);
280 
281 		timeout = SPI_TIMEOUT;
282 		while (!spi_is_rx_ready(p_spi)) {
283 			if (!timeout--) {
284 				return ERR_TIMEOUT;
285 			}
286 		}
287 		spi_read_single(p_spi, &val);
288 
289 		rx_data[i] = val;
290 		i++;
291 		len--;
292 	}
293 
294 	return STATUS_OK;
295 }
296 //! @}
297