]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libboard_samv7-ek/source/s25fl1_qspi.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained_IAR_Keil / libboard_samv7-ek / source / s25fl1_qspi.c
diff --git a/FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libboard_samv7-ek/source/s25fl1_qspi.c b/FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libboard_samv7-ek/source/s25fl1_qspi.c
new file mode 100644 (file)
index 0000000..d084f2e
--- /dev/null
@@ -0,0 +1,200 @@
+/* ----------------------------------------------------------------------------\r
+ *         SAM Software Package License \r
+ * ----------------------------------------------------------------------------\r
+ * Copyright (c) 2013, Atmel Corporation\r
+ *\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ *\r
+ * - Redistributions of source code must retain the above copyright notice,\r
+ * this list of conditions and the disclaimer below.\r
+ *\r
+ * Atmel's name may not be used to endorse or promote products derived from\r
+ * this software without specific prior written permission.\r
+ *\r
+ * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
+ * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * ----------------------------------------------------------------------------\r
+ */\r
+\r
+\r
+/** \r
+ * \addtogroup at25_spi_module S25FL1 SPI driver\r
+ * \ingroup lib_spiflash\r
+ *\r
+ * The S25FL1 serial firmware dataflash driver is based on top of the\r
+ * corresponding Spi driver. A Dataflash structure instance has to be\r
+ * initialized using the S25FL1_Configure() function. Then a command can be send \r
+ * to the serial flash using the SPI_SendCommand() function. \r
+ *\r
+ * \section Usage\r
+ * <ul>\r
+ * <li>Initializes an S25FL1 instance and configures SPI chip select pin\r
+ *    using S25FL1_Configure(). </li>\r
+ * <li>Detect DF and returns DF description corresponding to the device\r
+ *    connected using S25FL1D_ReadJedecId() and S25FL1_FindDevice().\r
+ *    This function shall be called by the application before S25FL1_SendCommand().</li>\r
+ * <li> Sends a command to the DF through the SPI using S25FL1_SendCommand().\r
+ *    The command is identified by its command code and the number of\r
+ *    bytes to transfer.</li>\r
+ *    <li> Example code for sending command to write a page to DF.</li>\r
+ *    \code\r
+ *        // Program page\r
+ *        error = S25FL1_SendCommand(pS25fl1, S25FL1_BYTE_PAGE_PROGRAM, 4,\r
+ *                pData, writeSize, address, 0, 0);\r
+ *    \endcode\r
+ *    <li> Example code for sending command to read a page from DF.\r
+ *       If data needs to be received, then a data buffer must be\r
+ *       provided.</li>\r
+ *    \code\r
+ *        // Start a read operation\r
+ *        error = S25FL1_SendCommand(pS25fl1, S25FL1_READ_ARRAY_LF,\r
+ *                4, pData, size, address, 0, 0);\r
+ *    \endcode\r
+ *    <li> This function does not block; its optional callback will\r
+ *       be invoked when the transfer completes.</li>\r
+ * <li> Check the S25FL1 driver is ready or not by polling S25FL1_IsBusy().</li>\r
+ * </ul>\r
+ *\r
+ * Related files :\n\r
+ * \ref at25_spi.c\n\r
+ * \ref at25_spi.h.\n\r
+ */\r
+\r
+/**\r
+ * \file\r
+ *\r
+ * Implementation for the S25FL1 SPI driver.\r
+ *\r
+ */\r
+\r
+/*----------------------------------------------------------------------------\r
+ *        Headers\r
+ *----------------------------------------------------------------------------*/\r
+#include <board.h>\r
+//#include <libspiflash.h>\r
+#include <assert.h>\r
+\r
+/*----------------------------------------------------------------------------\r
+ *        Local definitions\r
+ *----------------------------------------------------------------------------*/\r
+\r
+\r
+\r
+/*----------------------------------------------------------------------------\r
+ *        Exported functions\r
+ *----------------------------------------------------------------------------*/\r
+\r
+/**\r
+ * \brief Initializes an S25FL1 driver instance with the given SPI driver and chip\r
+ * select value.\r
+ *\r
+ * \param pS25fl1  Pointer to an S25FL1 driver instance.\r
+ * \param pSpid  Pointer to an SPI driver instance.\r
+ * \param cs  Chip select value to communicate with the serial flash.\r
+ * \param polling Uses polling mode instead of IRQ trigger.\r
+ */\r
+void S25FL1_Configure(S25fl1 *pS25fl1, Qspid *pQspid, uint8_t polling)\r
+{\r
+    SpidCmd *pCommand;\r
+\r
+    assert(pS25fl1);\r
+    assert(pQSpid);\r
+\r
+\r
+    /* Initialize the S25FL1 fields */\r
+    pS25fl1->pQspid = pSpid;\r
+    pS25fl1->pDesc = 0;\r
+    pS25fl1->pollingMode = polling;\r
+\r
+    /* Initialize the command structure */\r
+    pCommand = &(pS25fl1->command);\r
+    pCommand->pCmd = (uint8_t *) pS25fl1->CmdBuffer;\r
+    pCommand->callback = 0;\r
+    pCommand->pArgument = 0;\r
+}\r
+\r
+/**\r
+ * \brief Is serial flash driver busy.\r
+ *\r
+ * \param pS25fl1  Pointer to an S25fl1 driver instance.\r
+ *\r
+ * \return 1 if the serial flash driver is currently busy executing a command;\r
+ * otherwise returns 0.\r
+ */\r
+uint8_t S25FL1_IsBusy(S25fl1 *pS25fl1)\r
+{\r
+    if (pS25fl1->pollingMode)\r
+    {\r
+        SPID_Handler(pS25fl1->pSpid);\r
+        SPID_DmaHandler(pS25fl1->pSpid);\r
+    }\r
+    return SPID_IsBusy(pS25fl1->pSpid);\r
+}\r
+\r
+/**\r
+ * \brief Sends a command to the serial flash through the SPI. The command is made up\r
+ * of two parts: the first is used to transmit the command byte and optionally,\r
+ * address and dummy bytes. The second part is the data to send or receive.\r
+ * This function does not block: it returns as soon as the transfer has been\r
+ * started. An optional callback can be invoked to notify the end of transfer.\r
+ *\r
+ * \param pS25fl1  Pointer to an S25fl1 driver instance.\r
+ * \param cmd  Command byte.\r
+ * \param cmdSize  Size of command (command byte + address bytes + dummy bytes).\r
+ * \param pData Data buffer.\r
+ * \param dataSize  Number of bytes to send/receive.\r
+ * \param address  Address to transmit.\r
+ * \param callback  Optional user-provided callback to invoke at end of transfer.\r
+ * \param pArgument  Optional argument to the callback function.\r
+ *\r
+ * \return 0 if successful; otherwise, returns S25FL1_ERROR_BUSY if the S25FL1\r
+ * driver is currently executing a command, or S25FL1_ERROR_SPI if the command\r
+ * cannot be sent because of a SPI error.\r
+ */\r
+uint8_t S25FL1_SendCommand(uint8_t Instr, uint8_t ReadWrite)\r
+\r
+{  \r
+    pDev->Instruction = Instr; \r
+    QSPI_SendFrame(QSPI, pDev, ReadWrite);\r
+}\r
+\r
+/**\r
+ * \brief Tries to detect a serial firmware flash device given its JEDEC identifier.\r
+ * The JEDEC id can be retrieved by sending the correct command to the device.\r
+ *\r
+ * \param pS25fl1  Pointer to an S25FL1 driver instance.\r
+ * \param jedecId  JEDEC identifier of device.\r
+ *\r
+ * \return the corresponding S25FL1 descriptor if found; otherwise returns 0.\r
+ */\r
+const S25fl1Desc * S25FL1_FindDevice(S25fl1 *pS25fl1, uint32_t jedecId)\r
+{\r
+    uint32_t i = 0;\r
+\r
+    assert(pS25fl1);\r
+\r
+    /* Search if device is recognized */\r
+    pS25fl1->pDesc = 0;\r
+    while ((i < NUMDATAFLASH) && !(pS25fl1->pDesc)) {\r
+\r
+        if ((jedecId & 0xFF00FFFF) == (at25Devices[i].jedecId & 0xFF00FFFF)) {\r
+\r
+            pS25fl1->pDesc = &(at25Devices[i]);\r
+        }\r
+\r
+        i++;\r
+    }\r
+\r
+    return pS25fl1->pDesc;\r
+}\r