Kévin Redon | 4cd3f7d | 2019-01-24 17:57:13 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * |
| 4 | * \brief SPI related functionality declaration. |
| 5 | * |
| 6 | * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries. |
| 7 | * |
| 8 | * \asf_license_start |
| 9 | * |
| 10 | * \page License |
| 11 | * |
| 12 | * Subject to your compliance with these terms, you may use Microchip |
| 13 | * software and any derivatives exclusively with Microchip products. |
| 14 | * It is your responsibility to comply with third party license terms applicable |
| 15 | * to your use of third party software (including open source software) that |
| 16 | * may accompany Microchip software. |
| 17 | * |
| 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 19 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 20 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 22 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 23 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 24 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 25 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 26 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 27 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 28 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 29 | * |
| 30 | * \asf_license_stop |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #ifndef _HPL_SPI_H_INCLUDED |
| 35 | #define _HPL_SPI_H_INCLUDED |
| 36 | |
| 37 | #include <compiler.h> |
| 38 | #include <utils.h> |
| 39 | |
| 40 | /** |
| 41 | * \addtogroup hpl_spi HPL SPI |
| 42 | * |
| 43 | *@{ |
| 44 | */ |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | /** |
| 51 | * \brief SPI Dummy char is used when reading data from the SPI slave |
| 52 | */ |
| 53 | #define SPI_DUMMY_CHAR 0x1ff |
| 54 | |
| 55 | /** |
| 56 | * \brief SPI message to let driver to process |
| 57 | */ |
| 58 | //@{ |
| 59 | struct spi_msg { |
| 60 | /** Pointer to the output data buffer */ |
| 61 | uint8_t *txbuf; |
| 62 | /** Pointer to the input data buffer */ |
| 63 | uint8_t *rxbuf; |
| 64 | /** Size of the message data in SPI characters */ |
| 65 | uint32_t size; |
| 66 | }; |
| 67 | //@} |
| 68 | |
| 69 | /** |
| 70 | * \brief SPI transfer modes |
| 71 | * SPI transfer mode controls clock polarity and clock phase. |
| 72 | * Mode 0: leading edge is rising edge, data sample on leading edge. |
| 73 | * Mode 1: leading edge is rising edge, data sample on trailing edge. |
| 74 | * Mode 2: leading edge is falling edge, data sample on leading edge. |
| 75 | * Mode 3: leading edge is falling edge, data sample on trailing edge. |
| 76 | */ |
| 77 | enum spi_transfer_mode { |
| 78 | /** Leading edge is rising edge, data sample on leading edge. */ |
| 79 | SPI_MODE_0, |
| 80 | /** Leading edge is rising edge, data sample on trailing edge. */ |
| 81 | SPI_MODE_1, |
| 82 | /** Leading edge is falling edge, data sample on leading edge. */ |
| 83 | SPI_MODE_2, |
| 84 | /** Leading edge is falling edge, data sample on trailing edge. */ |
| 85 | SPI_MODE_3 |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * \brief SPI character sizes |
| 90 | * The character size influence the way the data is sent/received. |
| 91 | * For char size <= 8 data is stored byte by byte. |
| 92 | * For char size between 9 ~ 16 data is stored in 2-byte length. |
| 93 | * Note that the default and recommended char size is 8 bit since it's |
| 94 | * supported by all system. |
| 95 | */ |
| 96 | enum spi_char_size { |
| 97 | /** Character size is 8 bit. */ |
| 98 | SPI_CHAR_SIZE_8 = 0, |
| 99 | /** Character size is 9 bit. */ |
| 100 | SPI_CHAR_SIZE_9 = 1, |
| 101 | /** Character size is 10 bit. */ |
| 102 | SPI_CHAR_SIZE_10 = 2, |
| 103 | /** Character size is 11 bit. */ |
| 104 | SPI_CHAR_SIZE_11 = 3, |
| 105 | /** Character size is 12 bit. */ |
| 106 | SPI_CHAR_SIZE_12 = 4, |
| 107 | /** Character size is 13 bit. */ |
| 108 | SPI_CHAR_SIZE_13 = 5, |
| 109 | /** Character size is 14 bit. */ |
| 110 | SPI_CHAR_SIZE_14 = 6, |
| 111 | /** Character size is 15 bit. */ |
| 112 | SPI_CHAR_SIZE_15 = 7, |
| 113 | /** Character size is 16 bit. */ |
| 114 | SPI_CHAR_SIZE_16 = 8 |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * \brief SPI data order |
| 119 | */ |
| 120 | enum spi_data_order { |
| 121 | /** MSB goes first. */ |
| 122 | SPI_DATA_ORDER_MSB_1ST = 0, |
| 123 | /** LSB goes first. */ |
| 124 | SPI_DATA_ORDER_LSB_1ST = 1 |
| 125 | }; |
| 126 | |
| 127 | /** \brief Transfer descriptor for SPI |
| 128 | * Transfer descriptor holds TX and RX buffers |
| 129 | */ |
| 130 | struct spi_xfer { |
| 131 | /** Pointer to data buffer to TX */ |
| 132 | uint8_t *txbuf; |
| 133 | /** Pointer to data buffer to RX */ |
| 134 | uint8_t *rxbuf; |
| 135 | /** Size of data characters to TX & RX */ |
| 136 | uint32_t size; |
| 137 | }; |
| 138 | |
| 139 | /** SPI generic driver. */ |
| 140 | struct spi_dev { |
| 141 | /** Pointer to the hardware base or private data for special device. */ |
| 142 | void *prvt; |
| 143 | /** Reference start of sync/async variables */ |
| 144 | uint32_t sync_async_misc[1]; |
| 145 | }; |
| 146 | |
| 147 | /** |
| 148 | * \brief Calculate the baudrate value for hardware to use to set baudrate |
| 149 | * \param[in, out] dev Pointer to the SPI device instance. |
| 150 | * \param[in] clk Clock frequency (Hz) for baudrate generation. |
| 151 | * \param[in] baud Target baudrate (bps). |
| 152 | * \return Error or baudrate value. |
| 153 | * \retval >0 Baudrate value. |
| 154 | * \retval ERR_INVALID_ARG Calculation fail. |
| 155 | */ |
| 156 | int32_t _spi_calc_baud_val(struct spi_dev *dev, const uint32_t clk, const uint32_t baud); |
| 157 | |
| 158 | #ifdef __cplusplus |
| 159 | } |
| 160 | #endif |
| 161 | |
| 162 | /**@}*/ |
| 163 | #endif /* ifndef _HPL_SPI_H_INCLUDED */ |