| 1 | /**************************************************************************//** |
| 2 | * @file spi.c |
| 3 | * @brief SPI functions implementations. |
| 4 | * @author acozma ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2011(c) Analog Devices, Inc. |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without modification, |
| 12 | * are permitted provided that the following conditions are met: |
| 13 | * - Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * - Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific prior written permission. |
| 22 | * - The use of this software may or may not infringe the patent rights |
| 23 | * of one or more patent holders. This license does not release you |
| 24 | * from the requirement that you obtain separate licenses from these |
| 25 | * patent holders to use this software. |
| 26 | * - Use of the software either in source or binary form, must be run |
| 27 | * on or directly connected to an Analog Devices Inc. component. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 30 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY |
| 31 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 34 | * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 36 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | * |
| 40 | ******************************************************************************* |
| 41 | * SVN Revision: $WCREV$ |
| 42 | ******************************************************************************/ |
| 43 | |
| 44 | /*****************************************************************************/ |
| 45 | /***************************** Include Files *********************************/ |
| 46 | /*****************************************************************************/ |
| 47 | #include "system_config.h" |
| 48 | #include "xil_io.h" |
| 49 | |
| 50 | #if (USE_PS7 == 0) |
| 51 | /*****************************************************************************/ |
| 52 | /************************ Constants Definitions ******************************/ |
| 53 | /*****************************************************************************/ |
| 54 | #define MAX_TX_SIZE 16 /*!< Maximum number of bytes that can be transmitted in one SPI transfer */ |
| 55 | |
| 56 | /*****************************************************************************/ |
| 57 | /************************ Variables Definitions ******************************/ |
| 58 | /*****************************************************************************/ |
| 59 | typedef struct _stSpiConfig |
| 60 | { |
| 61 | u32 axiBaseAddr; /*!< Base address of the SPI Microblaze IP */ |
| 62 | u32 config; /*!< Configuration of the SPI Microblaze IP */ |
| 63 | }stSpiConfig; |
| 64 | |
| 65 | static stSpiConfig spiConfig[2] = {{0,0}, {0,0}}; |
| 66 | static char emptyTxBuf[MAX_TX_SIZE]; |
| 67 | |
| 68 | /**************************************************************************//** |
| 69 | * @brief Initializes the communication with the Microblaze SPI peripheral. |
| 70 | * |
| 71 | * @param axiBaseAddr - Microblaze SPI peripheral AXI base address. |
| 72 | * @param lsbFirst - Set to 1 if the data is transmitted LSB first. |
| 73 | * @param cpha - Set to 1 if CPHA mode is used. |
| 74 | * @param cpol - Set to 1 if CPOL mode is used. |
| 75 | * @return TRUE |
| 76 | ******************************************************************************/ |
| 77 | u32 SPI_Init_axi(u32 axiBaseAddr, char lsbFirst, char cpha, char cpol) |
| 78 | { |
| 79 | u32 i = 0; |
| 80 | u32 cfgValue = 0; |
| 81 | |
| 82 | //set the empty Tx buffer to 0 |
| 83 | for(i = 0; i < MAX_TX_SIZE; i++) |
| 84 | { |
| 85 | emptyTxBuf[i] = 0; |
| 86 | } |
| 87 | |
| 88 | // Configuration Register Settings |
| 89 | cfgValue |= (lsbFirst << LSBFirst) | // MSB First transfer format |
| 90 | (1 << MasterTranInh) | // Master transactions disabled |
| 91 | (1 << ManualSlaveAssEn) | // Slave select output follows data in slave select register |
| 92 | (1 << RxFifoReset) | // Receive FIFO normal operation |
| 93 | (0 << TxFifoReset) | // Transmit FIFO normal operation |
| 94 | (cpha << CHPA) | // Data valid on first SCK edge |
| 95 | (cpol << CPOL) | // Active high clock, SCK idles low |
| 96 | (1 << Master) | // SPI in Master configuration mode |
| 97 | (1 << SPE) | // SPI enabled |
| 98 | (0 << LOOP); // Normal operation |
| 99 | |
| 100 | //initialize the SPI configuration |
| 101 | for(i = 0; i < sizeof(spiConfig) / sizeof(stSpiConfig); i++) |
| 102 | { |
| 103 | if(spiConfig[i].axiBaseAddr == 0) |
| 104 | { |
| 105 | spiConfig[i].axiBaseAddr = axiBaseAddr; |
| 106 | spiConfig[i].config = cfgValue; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | //reset the SPI core |
| 112 | //Xil_Out32(axiBaseAddr + SRR, 0x0000000A); |
| 113 | |
| 114 | //set the slave select register to all ones |
| 115 | Xil_Out32(axiBaseAddr + SPISSR, 0xFFFFFFFF); |
| 116 | |
| 117 | // Set corresponding value to the Configuration Register |
| 118 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 119 | |
| 120 | return TRUE; |
| 121 | } |
| 122 | |
| 123 | /**************************************************************************//** |
| 124 | * @brief Transfers data to and from a SPI slave. |
| 125 | * |
| 126 | * @param axiBaseAddr - Microblaze SPI peripheral AXI base address. |
| 127 | * @param txSize - Number of bytes to transmit to the SPI slave. |
| 128 | * @param txBuffer - Buffer which holds the data to be transmitted to the SPI slave. |
| 129 | * @param rxSize - Number of bytes to receive from the SPI slave. |
| 130 | * @param txBuffer - Buffer to store the data read to the SPI slave. |
| 131 | * @param ssNo - Slave select line on which the slave is connected. |
| 132 | * @return TRUE. |
| 133 | ******************************************************************************/ |
| 134 | u32 SPI_TransferData_axi(u32 axiBaseAddr, char txSize, char* txBuf, char rxSize, char* rxBuf, char ssNo) |
| 135 | { |
| 136 | u32 i = 0; |
| 137 | u32 cfgValue = 0; |
| 138 | u32 SPIStatus = 0; |
| 139 | u32 rxCnt = 0; |
| 140 | u32 txCnt = 0; |
| 141 | u32 timeout = 0xFFFF; |
| 142 | |
| 143 | // Get the configuration of the SPI core |
| 144 | for(i = 0; i < sizeof(spiConfig) / sizeof(stSpiConfig); i++) |
| 145 | { |
| 146 | if(spiConfig[i].axiBaseAddr == axiBaseAddr) |
| 147 | { |
| 148 | cfgValue = spiConfig[i].config; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Check if the transmit buffer is empty |
| 154 | if(txSize == 0) |
| 155 | { |
| 156 | txSize = rxSize; |
| 157 | txBuf = emptyTxBuf; |
| 158 | } |
| 159 | |
| 160 | // Write configuration data to master SPI device SPICR |
| 161 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 162 | |
| 163 | // Write to SPISSR to manually assert SSn |
| 164 | Xil_Out32(axiBaseAddr + SPISSR, ~(0x00000001 << (ssNo - 1))); |
| 165 | |
| 166 | // Write initial data to master SPIDTR register |
| 167 | Xil_Out32(axiBaseAddr + SPIDTR, txBuf[0]); |
| 168 | |
| 169 | // Enable the master transactions |
| 170 | cfgValue &= ~(1 << MasterTranInh); |
| 171 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 172 | |
| 173 | // Send and receive the data |
| 174 | while(txCnt < txSize) |
| 175 | { |
| 176 | // Poll status for completion |
| 177 | do |
| 178 | { |
| 179 | SPIStatus = Xil_In32(axiBaseAddr + SPISR); |
| 180 | } |
| 181 | while(((SPIStatus & 0x01) == 1) && timeout--); |
| 182 | if(timeout == -1) |
| 183 | { |
| 184 | // Disable the master transactions |
| 185 | cfgValue |= (1 << MasterTranInh); |
| 186 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 187 | |
| 188 | //reset the SPI core |
| 189 | Xil_Out32(axiBaseAddr + SRR, 0x0000000A); |
| 190 | |
| 191 | //set the slave select register to all ones |
| 192 | Xil_Out32(axiBaseAddr + SPISSR, 0xFFFFFFFF); |
| 193 | |
| 194 | // Set corresponding value to the Configuration Register |
| 195 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 196 | |
| 197 | return FALSE; |
| 198 | } |
| 199 | timeout = 0xFFFF; |
| 200 | |
| 201 | // Read received data from SPI Core buffer |
| 202 | if(rxCnt < rxSize) |
| 203 | { |
| 204 | rxBuf[rxCnt] = Xil_In32(axiBaseAddr + SPIDRR); |
| 205 | rxCnt++; |
| 206 | } |
| 207 | // Send next data |
| 208 | txCnt++; |
| 209 | if(txCnt < txSize) |
| 210 | { |
| 211 | // Disable the master transactions |
| 212 | cfgValue |= (1 << MasterTranInh); |
| 213 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 214 | |
| 215 | // Write data |
| 216 | Xil_Out32(axiBaseAddr + SPIDTR, txBuf[txCnt]); |
| 217 | |
| 218 | /// Enable the master transactions |
| 219 | cfgValue &= ~(1 << MasterTranInh); |
| 220 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Disable the master transactions |
| 225 | cfgValue |= (1 << MasterTranInh); |
| 226 | Xil_Out32(axiBaseAddr + SPICR, cfgValue); |
| 227 | |
| 228 | // Write all ones to SPISSR |
| 229 | Xil_Out32(axiBaseAddr + SPISSR, 0xFFFFFFFF); |
| 230 | |
| 231 | return TRUE; |
| 232 | } |
| 233 | #endif |