123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- /**
- * @file flc.h
- * @brief Flash Controler driver.
- * @details This driver can be used to operate on the embedded flash memory.
- */
- /* ****************************************************************************
- * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
- * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- *
- * Except as contained in this notice, the name of Maxim Integrated
- * Products, Inc. shall not be used except as stated in the Maxim Integrated
- * Products, Inc. Branding Policy.
- *
- * The mere transfer of this software does not imply any licenses
- * of trade secrets, proprietary technology, copyrights, patents,
- * trademarks, maskwork rights, or any other form of intellectual
- * property whatsoever. Maxim Integrated Products, Inc. retains all
- * ownership rights.
- *
- * $Date: 2019-06-05 16:53:29 -0500 (Wed, 05 Jun 2019) $
- * $Revision: 43696 $
- *
- *************************************************************************** */
- /* **** Includes **** */
- #include <string.h>
- #include "mxc_config.h"
- #include "mxc_sys.h"
- #include "flc.h"
- #include "flc_regs.h"
- /* **** Definitions **** */
- /* **** Globals **** */
- /* **** Functions **** */
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- static int prepare_flc(void)
- {
- // Set flash clock divider to generate a 1MHz clock from the APB clock
- MXC_FLC->clkdiv = SystemCoreClock / 1000000;
- /* Check if the flash controller is busy */
- if (FLC_Busy()) {
- return E_BUSY;
- }
- /* Clear stale errors */
- if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
- MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
- }
- /* Unlock flash */
- MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_UNLOCK) | MXC_S_FLC_CN_UNLOCK_UNLOCKED;
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_Init(const sys_cfg_flc_t *sys_cfg)
- {
- SYS_FLC_Init(sys_cfg);
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_Busy(void)
- {
- return (MXC_FLC->cn & (MXC_F_FLC_CN_WR | MXC_F_FLC_CN_ME | MXC_F_FLC_CN_PGE));
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_MassErase(void)
- {
- int err;
- if ((err = prepare_flc()) != E_NO_ERROR)
- return err;
- /* Write mass erase code */
- MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEALL;
- /* Issue mass erase command */
- MXC_FLC->cn |= MXC_F_FLC_CN_ME;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- /* Lock flash */
- MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
- /* Check access violations */
- if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
- MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
- return E_BAD_STATE;
- }
- SYS_Flash_Operation();
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_PageErase(uint32_t address)
- {
- int err;
- if ((err = prepare_flc()) != E_NO_ERROR)
- return err;
- // Align address on page boundary
- address = address - (address % MXC_FLASH_PAGE_SIZE);
- /* Write page erase code */
- MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEPAGE;
- /* Issue page erase command */
- MXC_FLC->addr = address;
- MXC_FLC->cn |= MXC_F_FLC_CN_PGE;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- /* Lock flash */
- MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
- /* Check access violations */
- if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
- MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
- return E_BAD_STATE;
- }
- SYS_Flash_Operation();
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_Erase(uint32_t start, uint32_t end)
- {
- int retval;
- uint32_t addr;
- // Align start and end on page boundaries
- start = start - (start % MXC_FLASH_PAGE_SIZE);
- end = end - (end % MXC_FLASH_PAGE_SIZE);
- for (addr = start; addr <= end; addr += MXC_FLASH_PAGE_SIZE) {
- retval = FLC_PageErase(addr);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- }
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_BufferErase(uint32_t start, uint32_t end, uint8_t *buffer, unsigned length)
- {
- int retval;
- uint32_t start_align, start_len, end_align, end_len;
- // Align start and end on page boundaries, calculate length of data to buffer
- start_align = start - (start % MXC_FLASH_PAGE_SIZE);
- start_len = (start % MXC_FLASH_PAGE_SIZE);
- end_align = end - (end % MXC_FLASH_PAGE_SIZE);
- end_len = ((MXC_FLASH_PAGE_SIZE - (end % MXC_FLASH_PAGE_SIZE)) % MXC_FLASH_PAGE_SIZE);
- // Make sure the length of buffer is sufficient
- if ((length < start_len) || (length < end_len)) {
- return E_BAD_PARAM;
- }
- // Start and end address are in the same page
- if (start_align == end_align) {
- if (length < (start_len + end_len)) {
- return E_BAD_PARAM;
- }
- // Buffer first page data and last page data, erase and write
- memcpy(buffer, (void*)start_align, start_len);
- memcpy(&buffer[start_len], (void*)end, end_len);
- retval = FLC_PageErase(start_align);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- retval = FLC_Write(start_align, start_len, buffer);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- retval = FLC_Write(end, end_len, &buffer[start_len]);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- return E_NO_ERROR;
- }
- // Buffer, erase, and write the data in the first page
- memcpy(buffer, (void*)start_align, start_len);
- retval = FLC_PageErase(start_align);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- retval = FLC_Write(start_align, start_len, buffer);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- // Buffer, erase, and write the data in the last page
- memcpy(buffer, (void*)end, end_len);
- retval = FLC_PageErase(end_align);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- retval = FLC_Write(end, end_len, buffer);
- if (retval != E_NO_ERROR) {
- return retval;
- }
- // Erase the remaining pages
- if (start_align != end_align) {
- return FLC_Erase((start_align + MXC_FLASH_PAGE_SIZE), (end_align - MXC_FLASH_PAGE_SIZE));
- }
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_Write32(uint32_t address, uint32_t data)
- {
- int err;
- // Address checked if it is byte addressable
- if (address & 0x3) {
- return E_BAD_PARAM;
- }
- if ((err = prepare_flc()) != E_NO_ERROR)
- return err;
- // write in 32-bit units
- MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
- MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
- // write the data
- MXC_FLC->addr = address;
- MXC_FLC->data[0] = data;
- MXC_FLC->cn |= MXC_F_FLC_CN_WR;
- /* Wait until flash operation is complete */
- while (FLC_Busy()) {}
- /* Lock flash */
- MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
- /* Check access violations */
- if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
- MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
- return E_BAD_STATE;
- }
- SYS_Flash_Operation();
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_Write128(uint32_t address, uint32_t *data)
- {
- int err;
- // Address checked if it is word addressable
- if (address & 0xF) {
- return E_BAD_PARAM;
- }
- if ((err = prepare_flc()) != E_NO_ERROR)
- return err;
- // write 128-bits
- MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
- // write the data
- MXC_FLC->addr = address;
- memcpy((void*)&MXC_FLC->data[0], data, 16);
- MXC_FLC->cn |= MXC_F_FLC_CN_WR;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- /* Lock flash */
- MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
- /* Check access violations */
- if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
- MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
- return E_BAD_STATE;
- }
- SYS_Flash_Operation();
- return E_NO_ERROR;
- }
- // *****************************************************************************
- #if defined (__ICCARM__)
- #pragma section=".flashprog"
- #endif
- #if defined ( __GNUC__ )
- __attribute__ ((section(".flashprog")))
- #endif
- int FLC_Write(uint32_t address, uint32_t length, uint8_t *buffer)
- {
- int err;
- uint32_t bytes_written;
- uint8_t current_data[4];
- if ((err = prepare_flc()) != E_NO_ERROR)
- return err;
- // write in 32-bit units until we are 128-bit aligned
- MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
- MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
- // Align the address and read/write if we have to
- if (address & 0x3) {
- // Figure out how many bytes we have to write to round up the address
- bytes_written = 4 - (address & 0x3);
- // Save the data currently in the flash
- memcpy(current_data, (void*)(address & (~0x3)), 4);
- // Modify current_data to insert the data from buffer
- memcpy(¤t_data[4-bytes_written], buffer, bytes_written);
- // Write the modified data
- MXC_FLC->addr = address - (address % 4);
- memcpy((void*)&MXC_FLC->data[0], ¤t_data, 4);
- MXC_FLC->cn |= MXC_F_FLC_CN_WR;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- address += bytes_written;
- length -= bytes_written;
- buffer += bytes_written;
- }
- while ( (length >= 4) && ((address & 0xF) != 0) ) {
- MXC_FLC->addr = address;
- memcpy((void*)&MXC_FLC->data[0], buffer, 4);
- MXC_FLC->cn |= MXC_F_FLC_CN_WR;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- address += 4;
- length -= 4;
- buffer += 4;
- }
- if (length >= 16) {
- // write in 128-bit bursts while we can
- MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
- while (length >= 16) {
- MXC_FLC->addr = address;
- memcpy((void*)&MXC_FLC->data[0], buffer, 16);
- MXC_FLC->cn |= MXC_F_FLC_CN_WR;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- address += 16;
- length -= 16;
- buffer += 16;
- }
- // Return to 32-bit writes.
- MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
- }
- while (length >= 4) {
- MXC_FLC->addr = address;
- memcpy((void*)&MXC_FLC->data[0], buffer, 4);
- MXC_FLC->cn |= MXC_F_FLC_CN_WR;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- address += 4;
- length -= 4;
- buffer += 4;
- }
- if (length > 0) {
- // Save the data currently in the flash
- memcpy(current_data, (void*)(address), 4);
- // Modify current_data to insert the data from buffer
- memcpy(current_data, buffer, length);
- MXC_FLC->addr = address;
- memcpy((void*)&MXC_FLC->data[0], current_data, 4);
- MXC_FLC->cn |= MXC_F_FLC_CN_WR;
- /* Wait until flash operation is complete */
- while (FLC_Busy());
- }
- /* Lock flash */
- MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
- /* Check access violations */
- if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
- MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
- return E_BAD_STATE;
- }
- SYS_Flash_Operation();
- return E_NO_ERROR;
- }
- int FLC_EnableInt(uint32_t mask)
- {
- uint32_t tmp;
- mask &= (MXC_F_FLC_INTR_DONEIE | MXC_F_FLC_INTR_AFIE);
- if (!mask) {
- /* No bits set? Wasn't something we can enable. */
- return E_BAD_PARAM;
- }
- /* Careful with access_fail bit, as it is W0C */
- tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
- /* Don't lose done flag */
- tmp &= ~(MXC_F_FLC_INTR_DONE);
- /* Apply enables and write back */
- MXC_FLC->intr = (tmp | mask);
- return E_NO_ERROR;
- }
- int FLC_DisableInt(uint32_t mask)
- {
- uint32_t tmp;
- mask &= (MXC_F_FLC_INTR_DONEIE | MXC_F_FLC_INTR_AFIE);
- if (!mask) {
- /* No bits set? Wasn't something we can disable. */
- return E_BAD_PARAM;
- }
- /* Careful with access_fail bit, as it is W0C */
- tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
- /* Don't lose done flag */
- tmp &= ~(MXC_F_FLC_INTR_DONE);
- /* Apply disables and write back */
- MXC_FLC->intr = (tmp & ~mask);
- return E_NO_ERROR;
- }
- int FLC_GetFlags(void)
- {
- return (MXC_FLC->intr & (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF));
- }
- int FLC_ClearFlags(uint32_t mask)
- {
- mask &= (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF);
- if (!mask) {
- /* No bits set? Wasn't something we can clear. */
- return E_BAD_PARAM;
- }
- // Both bits are write zero clear
- MXC_FLC->intr ^= mask;
- return E_NO_ERROR;
- }
- int FLC_UnlockInfoBlock()
- {
- MXC_FLC->acntl = 0x3a7f5ca3;
- MXC_FLC->acntl = 0xa1e34f20;
- MXC_FLC->acntl = 0x9608b2c1;
- return E_NO_ERROR;
- }
- int FLC_LockInfoBlock()
- {
- MXC_FLC->acntl = 0xDEADBEEF;
- return E_NO_ERROR;
- }
|