1 /** 2 * \file 3 * 4 * \brief SPI related functionality declaration. 5 * 6 * Copyright (C) 2014 - 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 #ifndef _HPL_SPI_H_INCLUDED 45 #define _HPL_SPI_H_INCLUDED 46 47 #include <compiler.h> 48 #include <utils.h> 49 50 /** 51 * \addtogroup hpl_spi HPL SPI 52 * 53 *@{ 54 */ 55 56 #ifdef __cplusplus 57 extern "C" { 58 #endif 59 60 /** 61 * \brief SPI Dummy char is used when reading data from the SPI slave 62 */ 63 #define SPI_DUMMY_CHAR 0x1ff 64 65 /** 66 * \brief SPI message to let driver to process 67 */ 68 //@{ 69 struct spi_msg { 70 /** Pointer to the output data buffer */ 71 uint8_t *txbuf; 72 /** Pointer to the input data buffer */ 73 uint8_t *rxbuf; 74 /** Size of the message data in SPI characters */ 75 uint32_t size; 76 }; 77 //@} 78 79 /** 80 * \brief SPI transfer modes 81 * SPI transfer mode controls clock polarity and clock phase. 82 * Mode 0: leading edge is rising edge, data sample on leading edge. 83 * Mode 1: leading edge is rising edge, data sample on trailing edge. 84 * Mode 2: leading edge is falling edge, data sample on leading edge. 85 * Mode 3: leading edge is falling edge, data sample on trailing edge. 86 */ 87 enum spi_transfer_mode { 88 /** Leading edge is rising edge, data sample on leading edge. */ 89 SPI_MODE_0, 90 /** Leading edge is rising edge, data sample on trailing edge. */ 91 SPI_MODE_1, 92 /** Leading edge is falling edge, data sample on leading edge. */ 93 SPI_MODE_2, 94 /** Leading edge is falling edge, data sample on trailing edge. */ 95 SPI_MODE_3 96 }; 97 98 /** 99 * \brief SPI character sizes 100 * The character size influence the way the data is sent/received. 101 * For char size <= 8 data is stored byte by byte. 102 * For char size between 9 ~ 16 data is stored in 2-byte length. 103 * Note that the default and recommended char size is 8 bit since it's 104 * supported by all system. 105 */ 106 enum spi_char_size { 107 /** Character size is 8 bit. */ 108 SPI_CHAR_SIZE_8 = 0, 109 /** Character size is 9 bit. */ 110 SPI_CHAR_SIZE_9 = 1, 111 /** Character size is 10 bit. */ 112 SPI_CHAR_SIZE_10 = 2, 113 /** Character size is 11 bit. */ 114 SPI_CHAR_SIZE_11 = 3, 115 /** Character size is 12 bit. */ 116 SPI_CHAR_SIZE_12 = 4, 117 /** Character size is 13 bit. */ 118 SPI_CHAR_SIZE_13 = 5, 119 /** Character size is 14 bit. */ 120 SPI_CHAR_SIZE_14 = 6, 121 /** Character size is 15 bit. */ 122 SPI_CHAR_SIZE_15 = 7, 123 /** Character size is 16 bit. */ 124 SPI_CHAR_SIZE_16 = 8 125 }; 126 127 /** 128 * \brief SPI data order 129 */ 130 enum spi_data_order { 131 /** MSB goes first. */ 132 SPI_DATA_ORDER_MSB_1ST = 0, 133 /** LSB goes first. */ 134 SPI_DATA_ORDER_LSB_1ST = 1 135 }; 136 137 /** \brief Transfer descriptor for SPI 138 * Transfer descriptor holds TX and RX buffers 139 */ 140 struct spi_xfer { 141 /** Pointer to data buffer to TX */ 142 uint8_t *txbuf; 143 /** Pointer to data buffer to RX */ 144 uint8_t *rxbuf; 145 /** Size of data characters to TX & RX */ 146 uint32_t size; 147 }; 148 149 /** SPI generic driver. */ 150 struct spi_dev { 151 /** Pointer to the hardware base or private data for special device. */ 152 void *prvt; 153 /** Reference start of sync/async variables */ 154 uint32_t sync_async_misc[1]; 155 }; 156 157 /** 158 * \brief Calculate the baudrate value for hardware to use to set baudrate 159 * \param[in, out] dev Pointer to the SPI device instance. 160 * \param[in] clk Clock frequency (Hz) for baudrate generation. 161 * \param[in] baud Target baudrate (bps). 162 * \return Error or baudrate value. 163 * \retval >0 Baudrate value. 164 * \retval ERR_INVALID_ARG Calculation fail. 165 */ 166 int32_t _spi_calc_baud_val(struct spi_dev *dev, const uint32_t clk, const uint32_t baud); 167 168 #ifdef __cplusplus 169 } 170 #endif 171 172 /**@}*/ 173 #endif /* ifndef _HPL_SPI_H_INCLUDED */ 174