1 /**
2 * \file
3 *
4 * \brief I/O SPI related functionality implementation.
5 *
6 * Copyright (C) 2014 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 #include "hal_spi_m_sync.h"
45 #include <utils_assert.h>
46 #include <utils.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 /**
53 * \brief Driver version
54 */
55 #define SPI_M_SYNC_DRIVER_VERSION 0x00000001u
56
57 #define SPI_DEACTIVATE_NEXT 0x8000
58
59 static int32_t _spi_m_sync_io_write(struct io_descriptor *const io, const uint8_t *const buf, const uint16_t length);
60 static int32_t _spi_m_sync_io_read(struct io_descriptor *const io, uint8_t *const buf, const uint16_t length);
61
spi_m_sync_init(struct spi_m_sync_descriptor * spi,void * const hw)62 int32_t spi_m_sync_init(struct spi_m_sync_descriptor *spi, void *const hw)
63 {
64 int32_t rc = 0;
65
66 ASSERT(spi && hw);
67
68 spi->dev.prvt = (void *)hw;
69
70 rc = _spi_m_sync_init(&spi->dev, hw);
71 if (rc < 0) {
72 return rc;
73 }
74
75 spi->flags = SPI_DEACTIVATE_NEXT;
76 spi->io.read = _spi_m_sync_io_read;
77 spi->io.write = _spi_m_sync_io_write;
78
79 return ERR_NONE;
80 }
81
spi_m_sync_deinit(struct spi_m_sync_descriptor * spi)82 void spi_m_sync_deinit(struct spi_m_sync_descriptor *spi)
83 {
84 ASSERT(spi);
85
86 _spi_m_sync_deinit(&spi->dev);
87 }
88
spi_m_sync_enable(struct spi_m_sync_descriptor * spi)89 void spi_m_sync_enable(struct spi_m_sync_descriptor *spi)
90 {
91 ASSERT(spi);
92
93 _spi_m_sync_enable(&spi->dev);
94 }
95
spi_m_sync_disable(struct spi_m_sync_descriptor * spi)96 void spi_m_sync_disable(struct spi_m_sync_descriptor *spi)
97 {
98 ASSERT(spi);
99
100 _spi_m_sync_disable(&spi->dev);
101 }
102
spi_m_sync_set_baudrate(struct spi_m_sync_descriptor * spi,const uint32_t baud_val)103 int32_t spi_m_sync_set_baudrate(struct spi_m_sync_descriptor *spi, const uint32_t baud_val)
104 {
105 ASSERT(spi);
106
107 return _spi_m_sync_set_baudrate(&spi->dev, baud_val);
108 }
109
spi_m_sync_set_mode(struct spi_m_sync_descriptor * spi,const enum spi_transfer_mode mode)110 int32_t spi_m_sync_set_mode(struct spi_m_sync_descriptor *spi, const enum spi_transfer_mode mode)
111 {
112 ASSERT(spi);
113
114 return _spi_m_sync_set_mode(&spi->dev, mode);
115 }
116
spi_m_sync_set_char_size(struct spi_m_sync_descriptor * spi,const enum spi_char_size char_size)117 int32_t spi_m_sync_set_char_size(struct spi_m_sync_descriptor *spi, const enum spi_char_size char_size)
118 {
119 ASSERT(spi);
120
121 return _spi_m_sync_set_char_size(&spi->dev, char_size);
122 }
123
spi_m_sync_set_data_order(struct spi_m_sync_descriptor * spi,const enum spi_data_order dord)124 int32_t spi_m_sync_set_data_order(struct spi_m_sync_descriptor *spi, const enum spi_data_order dord)
125 {
126 ASSERT(spi);
127
128 return _spi_m_sync_set_data_order(&spi->dev, dord);
129 }
130
131 /** \brief Do SPI read in polling way
132 * For SPI master, activate CS, do send 0xFFs and read data, deactivate CS.
133 *
134 * It blocks until all data read or error.
135 *
136 * \param[in, out] spi Pointer to the HAL SPI instance.
137 * \param[out] buf Pointer to the buffer to store read data.
138 * \param[in] size Size of the data in number of characters.
139 * \return Operation status.
140 * \retval size Success.
141 * \retval >=0 Time out, with number of characters read.
142 */
_spi_m_sync_io_read(struct io_descriptor * io,uint8_t * buf,const uint16_t length)143 static int32_t _spi_m_sync_io_read(struct io_descriptor *io, uint8_t *buf, const uint16_t length)
144 {
145 ASSERT(io);
146
147 struct spi_m_sync_descriptor *spi = CONTAINER_OF(io, struct spi_m_sync_descriptor, io);
148 struct spi_xfer xfer;
149
150 xfer.rxbuf = buf;
151 xfer.txbuf = 0;
152 xfer.size = length;
153
154 return spi_m_sync_transfer(spi, &xfer);
155 }
156
157 /** \brief Do SPI data write in polling way
158 * For SPI master, activate CS, do buffer send and deactivate CS. The data back
159 * is discarded.
160 *
161 * The data read back is discarded.
162 *
163 * It blocks until all data sent or error.
164 *
165 * \param[in, out] spi Pointer to the HAL SPI instance.
166 * \param[in] p_xfer Pointer to the transfer information (\ref spi_transfer).
167 * \return Operation status.
168 * \retval size Success.
169 * \retval >=0 Timeout, with number of characters transferred.
170 */
_spi_m_sync_io_write(struct io_descriptor * const io,const uint8_t * const buf,const uint16_t length)171 static int32_t _spi_m_sync_io_write(struct io_descriptor *const io, const uint8_t *const buf, const uint16_t length)
172 {
173 ASSERT(io);
174
175 struct spi_m_sync_descriptor *spi = CONTAINER_OF(io, struct spi_m_sync_descriptor, io);
176 struct spi_xfer xfer;
177
178 xfer.rxbuf = 0;
179 xfer.txbuf = (uint8_t *)buf;
180 xfer.size = length;
181
182 return spi_m_sync_transfer(spi, &xfer);
183 }
184
spi_m_sync_transfer(struct spi_m_sync_descriptor * spi,const struct spi_xfer * p_xfer)185 int32_t spi_m_sync_transfer(struct spi_m_sync_descriptor *spi, const struct spi_xfer *p_xfer)
186 {
187 struct spi_msg msg;
188
189 ASSERT(spi && p_xfer);
190
191 msg.txbuf = p_xfer->txbuf;
192 msg.rxbuf = p_xfer->rxbuf;
193 msg.size = p_xfer->size;
194
195 return _spi_m_sync_trans(&spi->dev, &msg);
196 }
197
spi_m_sync_get_io_descriptor(struct spi_m_sync_descriptor * const spi,struct io_descriptor ** io)198 int32_t spi_m_sync_get_io_descriptor(struct spi_m_sync_descriptor *const spi, struct io_descriptor **io)
199 {
200 ASSERT(spi && io);
201 *io = &spi->io;
202 return 0;
203 }
204
spi_m_sync_get_version(void)205 uint32_t spi_m_sync_get_version(void)
206 {
207 return SPI_M_SYNC_DRIVER_VERSION;
208 }
209
210 #ifdef __cplusplus
211 }
212 #endif
213