diff --git a/EEE4STM32.c b/EEE4STM32.c deleted file mode 100644 index e811638..0000000 --- a/EEE4STM32.c +++ /dev/null @@ -1,394 +0,0 @@ -/************************************************************************************************************ -************** Include Headers -************************************************************************************************************/ - -#include "EEE4STM32.h" -#include - -/************************************************************************************************************ -************** Private Definitions -************************************************************************************************************/ - -#define EE_ERASE_PAGE_ADDRESS 0 -#define EE_ERASE_PAGE_NUMBER 1 -#define EE_ERASE_SECTOR_NUMBER 2 - -#ifdef STM32F0 -#define EE_ERASE EE_ERASE_PAGE_ADDRESS -#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) -#endif - -#ifdef STM32F1 -#define EE_ERASE EE_ERASE_PAGE_ADDRESS -#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) -#endif - -#ifdef STM32F2 -#define EE_ERASE EE_ERASE_SECTOR_NUMBER -#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) -#endif - -#ifdef STM32F3 -#define EE_ERASE EE_ERASE_PAGE_ADDRESS -#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) -#endif - -#ifdef STM32F4 -#define EE_ERASE EE_ERASE_SECTOR_NUMBER -#define EE_SIZE 0x20000 -#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) -#define FLASH_F4_OFFSET 4 -#endif - -#ifdef STM32F7 -#define EE_ERASE EE_ERASE_SECTOR_NUMBER -#define EE_SIZE 0x20000 -#endif - -#ifdef STM32H5 -#define EE_ERASE EE_ERASE_SECTOR_NUMBER -#endif - -#ifdef STM32H7 -#define EE_ERASE EE_ERASE_SECTOR_NUMBER -#endif - -#ifdef STM32G0 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32G4 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32U0 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32U5 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32L0 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32L1 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32L4 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32L5 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32WB -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32W0 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32WBA -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#undef FLASH_BANK_1 -#endif - -#ifdef STM32WL -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifdef STM32C0 -#define EE_ERASE EE_ERASE_PAGE_NUMBER -#endif - -#ifndef EE_SIZE -#if (EE_ERASE == EE_ERASE_PAGE_NUMBER) || (EE_ERASE == EE_ERASE_PAGE_ADDRESS) -#define EE_SIZE FLASH_PAGE_SIZE -#elif (EE_ERASE == EE_ERASE_SECTOR_NUMBER) -#define EE_SIZE FLASH_SECTOR_SIZE -#endif -#endif - -#if defined FLASH_BANK_2 -#define EE_BANK_SELECT FLASH_BANK_2 -#elif defined FLASH_BANK_1 -#define EE_BANK_SELECT FLASH_BANK_1 -#endif - -#ifndef EE_PAGE_SECTOR -#if (EE_BANK_SELECT == FLASH_BANK_2) -#define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE / 2) - 1) -#else -#define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE) - 1) -#endif -#endif - - -#ifndef EE_ADDRESS -#if (EE_BANK_SELECT != FLASH_BANK_2) -#define EE_ADDRESS (FLASH_BASE + EE_SIZE * EE_PAGE_SECTOR) -#else -#define EE_ADDRESS (FLASH_BASE + EE_SIZE * (EE_PAGE_SECTOR * 2 + 1)) -#endif -#endif - -#ifndef FLASH_F4_OFFSET -#define FLASH_F4_OFFSET 0 -#endif - -#ifndef EE_ERASE -#error "Not Supported MCU!" -#endif - -/************************************************************************************************************ -************** Private Variables -************************************************************************************************************/ - -EE_HandleTypeDef eeHandle; - -/************************************************************************************************************ -************** Private Functions -************************************************************************************************************/ - -/************************************************************************************************************ -************** Public Functions -************************************************************************************************************/ - -/** - * @brief Initializes the EEPROM emulation module. - * @note This function initializes the EEPROM emulation module to enable read and write operations. - * @param StoragePointer: Pointer to the start address of the EEPROM emulation area. - * @param Size: Size of the EEPROM emulation area in bytes. - * @return Boolean value indicating the success of the initialization: - * - true: Initialization successful. - * - false: Initialization failed. - */ -bool EE_Init(void *StoragePointer, uint32_t Size) -{ - bool answer = false; - do - { - /* checking size of eeprom area*/ - if (Size > EE_SIZE) - { - eeHandle.Size = 0; - eeHandle.DataPointer = NULL; - break; - } - eeHandle.Size = Size; - eeHandle.DataPointer = (uint8_t*)StoragePointer; - answer = true; - - } while (0); - - return answer; -} - -/***********************************************************************************************************/ - -/** - * @brief Retrieves the capacity of the EEPROM emulation area. - * @note This function returns the total capacity of the EEPROM emulation area in bytes. - * @return Capacity of the EEPROM emulation area in bytes. - */ -uint32_t EE_Capacity(void) -{ - return EE_SIZE; -} - -/***********************************************************************************************************/ - -/** - * @brief Formats the EEPROM emulation area. - * @note This function formats the EEPROM emulation area, - * @return bool Boolean value indicating the success of the operation: - * - true: Formatting successful. - * - false: Formatting failed. - */ -bool EE_Format(void) -{ - bool answer = false; - uint32_t error; - FLASH_EraseInitTypeDef flashErase; - do - { - HAL_FLASH_Unlock(); -#ifdef HAL_ICACHE_MODULE_ENABLED - /* disabling ICACHE if enabled*/ - HAL_ICACHE_Disable(); -#endif -#if EE_ERASE == EE_ERASE_PAGE_ADDRESS - flashErase.TypeErase = FLASH_TYPEERASE_PAGES; - flashErase.PageAddress = EE_ADDRESS; - flashErase.NbPages = 1; -#elif EE_ERASE == EE_ERASE_PAGE_NUMBER - flashErase.TypeErase = FLASH_TYPEERASE_PAGES; - flashErase.Page = EE_PAGE_SECTOR; - flashErase.NbPages = 1; -#else - flashErase.TypeErase = FLASH_TYPEERASE_SECTORS; - flashErase.Sector = EE_PAGE_SECTOR + FLASH_F4_OFFSET; - flashErase.NbSectors = 1; -#endif -#ifdef EE_BANK_SELECT - flashErase.Banks = EE_BANK_SELECT; -#endif -#ifdef FLASH_VOLTAGE_RANGE_3 - flashErase.VoltageRange = FLASH_VOLTAGE_RANGE_3; -#endif - /* erasing page/sector */ - if (HAL_FLASHEx_Erase(&flashErase, &error) != HAL_OK) - { - break; - } - /* checking result */ - if (error != 0xFFFFFFFF) - { - break; - } - answer = true; - - } while (0); - - HAL_FLASH_Lock(); -#ifdef HAL_ICACHE_MODULE_ENABLED - HAL_ICACHE_Enable(); -#endif - return answer; -} - -/***********************************************************************************************************/ - -/** - * @brief Reads data from the EEPROM emulation area. - * @note This function reads data from the EEPROM emulation area - * and loads it into the specified storage pointer. - */ -void EE_Read(void) -{ - uint8_t *data = eeHandle.DataPointer; -#ifdef HAL_ICACHE_MODULE_ENABLED - /* disabling ICACHE if enabled*/ - HAL_ICACHE_Disable(); -#endif - if (data != NULL) - { - /* reading flash */ - for (uint32_t i = 0; i < eeHandle.Size; i++) - { - *data = (*(__IO uint8_t*) (EE_ADDRESS + i)); - data++; - } - } -#ifdef HAL_ICACHE_MODULE_ENABLED - /* disabling ICACHE if enabled*/ - HAL_ICACHE_Enable(); -#endif -} - -/***********************************************************************************************************/ - -/** - * @brief Writes data to the EEPROM emulation area. - * @note This function writes data to the EEPROM emulation area. - * @retval true if the write operation is successful, false otherwise. - */ -bool EE_Write(void) -{ - bool answer = true; - uint8_t *data = eeHandle.DataPointer; - do - { - /* checking eeprom is initialize correctly */ - if (data == NULL) - { - answer = false; - break; - } - /* formating flash area before writing */ - if (EE_Format() == false) - { - answer = false; - break; - } - HAL_FLASH_Unlock(); -#ifdef HAL_ICACHE_MODULE_ENABLED - /* disabling ICACHE if enabled*/ - HAL_ICACHE_Disable(); -#endif -#if (defined FLASH_TYPEPROGRAM_HALFWORD) - /* writing buffer to flash */ - for (uint32_t i = 0; i < eeHandle.Size ; i += 2) - { - uint64_t halfWord; - memcpy((uint8_t*)&halfWord, data, 2); - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, EE_ADDRESS + i, halfWord) != HAL_OK) - { - answer = false; - break; - } - data += 2; - } -#elif (defined FLASH_TYPEPROGRAM_DOUBLEWORD) - /* writing buffer to flash */ - for (uint32_t i = 0; i < eeHandle.Size; i += 8) - { - uint64_t doubleWord; - memcpy((uint8_t*)&doubleWord, data, 8); - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, EE_ADDRESS + i, doubleWord) != HAL_OK) - { - answer = false; - break; - } - data += 8; - } -#elif (defined FLASH_TYPEPROGRAM_QUADWORD) - /* writing buffer to flash */ - for (uint32_t i = 0; i < eeHandle.Size; i += 16) - { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK) - { - answer = false; - break; - } - data += 16; - } -#elif (defined FLASH_TYPEPROGRAM_FLASHWORD) - /* writing buffer to flash */ - for (uint32_t i = 0; i < eeHandle.Size; i += FLASH_NB_32BITWORD_IN_FLASHWORD * 4) - { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK) - { - answer = false; - break; - } - data += FLASH_NB_32BITWORD_IN_FLASHWORD * 4; - } -#endif - /* verifying Flash content */ - data = eeHandle.DataPointer; - for (uint32_t i = 0; i < eeHandle.Size; i++) - { - if (*data != (*(__IO uint8_t*) (EE_ADDRESS + i))) - { - answer = false; - break; - } - data++; - } - - } while (0); - - HAL_FLASH_Lock(); -#ifdef HAL_ICACHE_MODULE_ENABLED - HAL_ICACHE_Enable(); -#endif - return answer; -} - -/***********************************************************************************************************/ diff --git a/EEE4STM32.h b/EEE4STM32.h deleted file mode 100644 index 6f7771e..0000000 --- a/EEE4STM32.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef _EEE4STM32_H_ -#define _EEE4STM32_H_ - -/*********************************************************************************************************** - - Author: Nima Askari - Github: https://www.github.com/NimaLTD - LinkedIn: https://www.linkedin.com/in/nimaltd - Youtube: https://www.youtube.com/@nimaltd - Instagram: https://instagram.com/github.NimaLTD - - Version: 3.1.3 - - History: - 3.1.3 - - Fixed L0, L1 configuration - - 3.1.2 - - Fixed H5 configuration - - 3.1.1 - - Fixed formatting F4 - - 3.1.0 - - Added Verify after Writing - - Added Checking Pointer in Reading/Writing - - Removed erasing buffer and formating before write - - 3.0.2 - - Fixed Writing for H7B - - 3.0.1 - - Added comments - - 3.0.0 - - Rewrite again - - Support STM32CubeMx Packet installer - -***********************************************************************************************************/ - -#ifdef __cplusplus -extern "C" -{ -#endif - -/************************************************************************************************************ -************** Include Headers -************************************************************************************************************/ - -#include -#include "main.h" - -/************************************************************************************************************ -************** Public Definitions -************************************************************************************************************/ - - -/************************************************************************************************************ -************** Public struct/enum -************************************************************************************************************/ - -typedef struct -{ - uint8_t *DataPointer; - uint32_t Size; - -} EE_HandleTypeDef; - -/************************************************************************************************************ -************** Public Functions -************************************************************************************************************/ - -bool EE_Init(void *StoragePointer, uint32_t Size); -uint32_t EE_Capacity(void); -bool EE_Format(void); -void EE_Read(void); -bool EE_Write(void); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/STM32-PACK/Files/LICENSE.txt b/STM32-PACK/Files/LICENSE.txt new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/STM32-PACK/Files/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul.c b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul.c new file mode 100644 index 0000000..f618c92 --- /dev/null +++ b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul.c @@ -0,0 +1,2129 @@ +/** + ****************************************************************************** + * @file EEPROM_Emul/Core/eeprom_emul.c + * @author MCD Application Team + * @brief This file provides all the EEPROM emulation firmware functions. + @verbatim + ============================================================================== + ##### How to use this driver ##### + ============================================================================== + [..] + This driver provides functions to initialize EEPROM emulation, to read and + write EEPROM variables, and to cleanup FLASH pages used by EEPROM emulation. + + (#) EEPROM emulation initialization functions: + (++) Format the FLASH pages used by EEPROM emulation using EE_Format(). + This function is optionally used, it can be called the very first + time EEPROM emulation is used, to prepare FLASH pages for EEPROM + emulation with empty EEPROM variables. It can also be called at + any time, to flush all EEPROM variables. + (++) Initialize EEPROM emulation, and restore the FLASH pages used by + EEPROM emulation to a known good state in case of power loss + using EE_Init(). It must be performed at system start up. + + (#) EEPROM variables access functions: + (++) Write EEPROM variable using EE_WriteVariableXbits() functions + A Clean Up request can be raised as return parameter in case + FLASH pages used by EEPROM emulation, are full. + (++) Read EEPROM variable using EE_ReadVariableXbits() functions + + (#) Clean up functions of FLASH pages, used by EEPROM emulation: + (++) There Two modes of erasing: + (+++) Polling mode using EE_CleanUp() function + (+++) Interrupt mode using EE_CleanUp_IT() function + (++) Callback function called when the clean up operation in interrupt + mode, is finished: EE_EndOfCleanup_UserCallback() + + @endverbatim + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "eeprom_emul.h" +/** @defgroup EEPROM_Emulation EEPROM_Emulation + * @{ + */ + +/* Private define -----------------------------------------------------------*/ +#ifdef DUALCORE_FLASH_SHARING +#define HSEM_PROCESS_1 12U /* Number taken randomly to identify the process locking a semaphore in the driver context */ +#endif +/* Private typedef -----------------------------------------------------------*/ +/** @defgroup EEPROM_Private_Structures EEPROM Private Structures + * @{ + */ + +/** + * @brief EE Find Type structure definition. + */ +/* Type of find requested : + READ --> page in active state + WRITE --> page in receive state or active state + ERASE --> page in erased state */ +typedef enum { + FIND_READ_PAGE, + FIND_WRITE_PAGE, + FIND_ERASE_PAGE +} EE_Find_type; + +/** + * @brief EE State Type structure definition. + */ +/* Type of state requested : + ERASED --> page is erased + RECEIVE --> page used during data transfer when no more space available in the system + ACTIVE --> page contains valid data and is not full + VALID --> page contains valid data and is full + ERASING --> page used during transfer, should be erased after transfer + INVALID --> page invalid state */ +typedef enum { + STATE_PAGE_ERASED, + STATE_PAGE_RECEIVE, + STATE_PAGE_ACTIVE, + STATE_PAGE_VALID, + STATE_PAGE_ERASING, + STATE_PAGE_INVALID +} EE_State_type; + +/** + * @brief EE Transfer Type structure definition. + */ +/* Definition of the different type of page transfer + NORMAL -> copy data page source to page destination + RECOVER -> resume page transfer that has been interrupted */ +typedef enum { + EE_TRANSFER_NORMAL, + EE_TRANSFER_RECOVER +} EE_Transfer_type; + +/** + * @brief EE State Reliability structure definition. + */ +/* Reliability of page state: + RELIABLE -> header of page is not corrupted, state is reliable + CORRUPTED -> header of page is corrupted, state is corrupted */ +typedef enum { + STATE_RELIABLE, + STATE_CORRUPTED +} EE_State_Reliability; + + + +/** + * @} + */ + +/* Private variables ---------------------------------------------------------*/ +/** @defgroup EEPROM_Private_Variables EEPROM Private Variables + * @{ + */ + +/* Global variables used to store eeprom status */ +uint16_t uhNbWrittenElements = 0U; /*!< Nb of elements written in valid and active pages */ +uint8_t ubCurrentActivePage = 0U; /*!< Current active page (can be active or receive state) */ +uint32_t uwAddressNextWrite = PAGE_HEADER_SIZE; /*!< Initialize write position just after page header */ + +/* During the cleanup phase in EE_Init, AddressRead is the address being read */ +__IO uint32_t AddressRead = 0; +/* Flag equal to 1 when the cleanup phase is in progress, 0 if not */ +__IO uint8_t CleanupPhase = 0; + +/** + * @} + */ + +/* Private function prototypes -----------------------------------------------*/ +/** @defgroup EEPROM_Private_Functions EEPROM Private Functions + * @{ + */ + +#ifndef FLASH_LINES_128B +static EE_Status ReadVariable(uint16_t VirtAddress, EE_DATA_TYPE* pData); +static EE_Status WriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data); +static EE_Status PagesTransfer(uint16_t VirtAddress, EE_DATA_TYPE Data, EE_Transfer_type type); +uint16_t CalculateCrc(EE_DATA_TYPE Data, uint16_t VirtAddress); +#else +static EE_Status ReadVariable(uint16_t VirtAddress, EE_DATA_TYPE* pData); +static EE_Status WriteVariable(uint16_t VirtAddress, EE_DATA_TYPE* Data); +static EE_Status PagesTransfer(uint16_t VirtAddress, EE_DATA_TYPE* Data, EE_Transfer_type type); +uint16_t CalculateCrc(EE_DATA_TYPE Data1, EE_DATA_TYPE Data2); +#endif +static EE_Status VerifyPageFullyErased(uint32_t Address, uint32_t PageSize); +static uint32_t FindPage(EE_Find_type Operation); +#if defined (DUALCORE_FLASH_SHARING) +static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data, EE_Write_type Write_type); +#elif defined (FLASH_LINES_128B) +static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE* Data, EE_Write_type Write_type); +#else +static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data); +#endif +static EE_Status SetPageState(uint32_t Page, EE_State_type State); +static EE_State_type GetPageState(uint32_t Address); +void ConfigureCrc(void); + +/** + * @} + */ + +/* Exported functions -------------------------------------------------------*/ +/** @addtogroup EEPROM_Exported_Functions + * @{ + */ + +/** + * @brief Restore the pages to a known good state in case of power loss. + * If a page is in RECEIVE state, resume transfer. + * Then if some pages are ERASING state, erase these pages. + * @param EraseType: Type of erase to apply on page requiring to be erased. + * This parameter can be one of the following values: + * @arg @ref EE_FORCED_ERASE pages to erase are erased unconditionnally + * @arg @ref EE_CONDITIONAL_ERASE pages to erase are erased only if not fully erased + * @retval EE_Status + * - EE_OK in case of success + * - EE error code in case of error + */ +EE_Status EE_Init(EE_Erase_type EraseType) +{ + EE_State_type pagestatus = STATE_PAGE_INVALID; + uint32_t page = 0U, pageaddress = 0U, varidx = 0U, + nbactivepage = 0U, nbactivereceivepage = 0U, nbvalidpage = 0U, + lastvalidpage = 0U, firstvalidpage = 0U, + recoverytransfer = 0U; + EE_ELEMENT_TYPE addressvalue = 0U; +#ifdef FLASH_LINES_128B + uint32_t Address = 0U; + EE_ELEMENT_TYPE addressvalue2 = 0U; + EE_DATA_TYPE dummy_data[2] = {0, 0}; +#endif + EE_State_Reliability pagestate = STATE_RELIABLE; + + EE_Status status = EE_OK; + + /* Check if the configuration is 128-bits bank or 2*64-bits bank */ + if (FI_CheckBankConfig() != EE_OK) + { + return EE_INVALID_BANK_CFG; + } + + /***************************************************************************/ + /* Step 0: Perform initial configuration */ + /***************************************************************************/ + /* Configure CRC peripheral for eeprom emulation usage */ + + ConfigureCrc(); + + /***************************************************************************/ + /* Step 1: Read all lines of the flash pages of eeprom emulation to */ + /* delete corrupted lines detectable through NMI */ + /***************************************************************************/ + /* We set the flag indicating the cleanup phase is operating to 1 */ + + CleanupPhase = 1; + +#ifndef STM32C031xx + for (page = START_PAGE; page < (START_PAGE + PAGES_NUMBER); page++) + { + pageaddress = PAGE_ADDRESS(page); + for (varidx = 0U; varidx < PAGE_SIZE; varidx += EE_ELEMENT_SIZE) + { + /* + During the cleanup phase and only during it, + we save the address read to set its content to 0 in case it triggered an NMI (see NMI_Handler in stm32lxxx_it.c). + In the rest of the program, we do nothing in case a NMI is triggers by a reading because no NMI should be triggered + since we have cleanup the EEPROM emulated. By the way, there is still the CRC code associated to each EEPROM line + that allows to verify its valid state. + */ + AddressRead = pageaddress + varidx; + addressvalue = (*(__IO EE_ELEMENT_TYPE*)(pageaddress + varidx)); + +#ifdef FLASH_LINES_128B + AddressRead = pageaddress + varidx + 8; + addressvalue = (*(__IO EE_ELEMENT_TYPE*)(pageaddress + varidx + 8)); +#endif + + } + } +#ifdef FLASH_LINES_128B + //If a reset occured when an ECCC was detected but not corrected + if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCC)) + { + +#ifdef STM32H563xx + Address=(0x8000000|(FLASH->ECCDR & 0x000FFFFF)); //Recovery of address where ECC error occured +#else + Address=(0x8000000|(FLASH->ECCR & 0x000FFFFF)); //Recovery of address where ECC error occured +#endif + EE_DeleteCorruptedFlashAddress(Address); //Delete corrupted address + } +#endif +#endif + /* We set the flag indicating the cleanup phase is operating to 0 because it just ended */ + CleanupPhase = 0; + + /***************************************************************************/ + /* Step 2: Handle case of reset during transfer with no receive page */ + /* present, by setting missing receive page state */ + /***************************************************************************/ + /* Check if no active page and no receive page present */ + /* Browse all pages */ + + for (page = START_PAGE; page < (START_PAGE + PAGES_NUMBER); page++) + { + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + + /* Search for active and receive page */ + if ((pagestatus == STATE_PAGE_ACTIVE) || (pagestatus == STATE_PAGE_RECEIVE)) + { + nbactivereceivepage++; + } + /* Keep index of first valid page, and last valid page */ + else if (pagestatus == STATE_PAGE_VALID) + { + if (nbvalidpage == 0U) + { + firstvalidpage = page; + } + lastvalidpage = page; + nbvalidpage++; + } + } + + /* Check if no active and no receive page have been detected */ + if (nbactivereceivepage == 0U) + { + /* Check if valid pages have been detected */ + if (nbvalidpage > 0U) + { + /* Check state of page just before first valid page. + If it is erasing page, then page after last valid page shall be set + to receiving state */ + if (GetPageState(PAGE_ADDRESS(PREVIOUS_PAGE(firstvalidpage))) == STATE_PAGE_ERASING) + { + if (SetPageState(FOLLOWING_PAGE(lastvalidpage), STATE_PAGE_RECEIVE) != EE_OK) + { + return EE_WRITE_ERROR; + } + } + } + /* Format flash pages used for eeprom emulation in case no active, no receive, no valid pages are found */ + else + { + return EE_Format(EE_FORCED_ERASE); + } + } + + /*********************************************************************/ + /* Step 3: Handle case of reset during transfer, by performing */ + /* transfer recovery */ + /*********************************************************************/ + + /* Browse all pages */ + for (page = START_PAGE; page < (START_PAGE + PAGES_NUMBER); page++) + { + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + + /* Check if there is receive page, meaning transfer has been interrupted */ + if (pagestatus == STATE_PAGE_RECEIVE) + { + /* Verify that receive page is a true one, not a corrupted page state */ + /* Check if page is not the first page of a bloc */ + if ((page != START_PAGE) && (page != (uint32_t)(START_PAGE + (PAGES_NUMBER / 2U)))) + { + /* Check that previous page is valid state */ + if (GetPageState(PAGE_ADDRESS(PREVIOUS_PAGE(page))) == STATE_PAGE_VALID) + { + /* The receive page is a true receive page */ + pagestate = STATE_RELIABLE; + } + else /* Previous page is not valid state */ + { + /* The receive page is false receive page due to header corruption */ + pagestate = STATE_CORRUPTED; + } + } + else /* The receive page is the first page of a bloc */ + { + /* Check that following page is erased state */ + if (GetPageState(PAGE_ADDRESS(FOLLOWING_PAGE(page))) == STATE_PAGE_ERASED) + { + /* The receive page is a true receive page */ + pagestate = STATE_RELIABLE; + } + else /* Following page is not erased state */ + { + /* The receive page is false receive page due to header corruption */ + pagestate = STATE_CORRUPTED; + } + } + + /* If the receive page is a true receive page, resume pages transfer */ + if (pagestate == STATE_RELIABLE) + { + /* Initialize current active page */ + ubCurrentActivePage = page; + + /* Resume the interrupted page transfer, using dummy new data */ +#ifndef FLASH_LINES_128B + if (PagesTransfer(0U, 0U, EE_TRANSFER_RECOVER) != EE_CLEANUP_REQUIRED) + { + return EE_TRANSFER_ERROR; + } +#else + if (PagesTransfer(0U, dummy_data, EE_TRANSFER_RECOVER) != EE_CLEANUP_REQUIRED) + { + return EE_TRANSFER_ERROR; + } +#endif + /* Memorize transfer recovery occured */ + recoverytransfer = 1U; + + /* transfer recovery is done, then stop searching receive page */ + break; + } + } + } + + /*********************************************************************/ + /* Step 4: Verify presence of one unique active page */ + /* If more than one active page, raise error */ + /* If no active page present, set missing active page */ + /*********************************************************************/ + + /* Browse all pages to search for active pages */ + nbactivepage = 0U; + for (page = START_PAGE; page < (START_PAGE + PAGES_NUMBER); page++) + { + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + + /* Search for active page */ + if (pagestatus == STATE_PAGE_ACTIVE) + { + /* Verify that active page is a true one, not a corrupted page state */ + /* Check if page is not the first page of a bloc */ + if ((page != START_PAGE) && (page != (uint32_t)(START_PAGE + (PAGES_NUMBER / 2U)))) + { + /* Check that previous page is valid state */ + if (GetPageState(PAGE_ADDRESS(PREVIOUS_PAGE(page))) == STATE_PAGE_VALID) + { + /* The active page is a true active page */ + pagestate = STATE_RELIABLE; + } + else /* Previous page is not valid state */ + { + /* The active page is false active page due to header corruption */ + pagestate = STATE_CORRUPTED; + } + } + else /* The active page is the first page of a bloc */ + { + /* Check that following page is erased state */ + if (GetPageState(PAGE_ADDRESS(FOLLOWING_PAGE(page))) == STATE_PAGE_ERASED) + { + /* The active page is a true active page */ + pagestate = STATE_RELIABLE; + } + else /* Following page is not erased state */ + { + /* The active page is false active page due to header corruption */ + pagestate = STATE_CORRUPTED; + } + } + + /* If the active page is a true active page, initialize global variables */ + if (pagestate == STATE_RELIABLE) + { + if (nbactivepage == 0U) + { + ubCurrentActivePage = page; + nbactivepage++; + } + else + { + /* Error: More than one reliable active page is present */ + return EE_INVALID_PAGE_SEQUENCE; + } + } + } + /* Keep index of last valid page, will be required in case no active page is found */ + else if (pagestatus == STATE_PAGE_VALID) + { + lastvalidpage = page; + } + } + + /* In case no active page is found, set page after last valid page to active state */ + if (nbactivepage == 0U) + { + ubCurrentActivePage = FOLLOWING_PAGE(lastvalidpage); + if (SetPageState(ubCurrentActivePage, STATE_PAGE_ACTIVE) != EE_OK) + { + return EE_WRITE_ERROR; + } + } + + /*********************************************************************/ + /* Step 5: Initialize eeprom emulation global variables relative */ + /* to active page */ + /*********************************************************************/ + + /* Initialize global variables, with elements detected in active page */ + uhNbWrittenElements = 0U; + uwAddressNextWrite = PAGE_HEADER_SIZE; + + for (varidx = PAGE_HEADER_SIZE; varidx < PAGE_SIZE; varidx += EE_ELEMENT_SIZE) + { + /* Check elements present in active page */ + addressvalue = (*(__IO EE_ELEMENT_TYPE*)(PAGE_ADDRESS(ubCurrentActivePage) + varidx)); +#ifdef FLASH_LINES_128B + addressvalue2 = (*(__IO EE_ELEMENT_TYPE*)(PAGE_ADDRESS(ubCurrentActivePage) + varidx + 8)); + if ((addressvalue != EE_MASK_FULL) || (addressvalue2 != EE_MASK_FULL)) +#else + if (addressvalue != EE_MASK_FULL) +#endif + { + /* Then increment uhNbWrittenElements and uwAddressNextWrite */ + uhNbWrittenElements++; + uwAddressNextWrite += EE_ELEMENT_SIZE; + } + else /* no more element in the page */ + { + break; + } + } + + /*********************************************************************/ + /* Step 6: Finalize eeprom emulation global variables relative */ + /* to valid pages, and check consistency of pages sequence */ + /*********************************************************************/ + + /* Check consistency of pages sequence: one active page, optionnally some valid pages before */ + /* Update global variable uhNbWrittenElements if valid pages are found */ + page = ubCurrentActivePage; + firstvalidpage = ubCurrentActivePage; + while ((page != START_PAGE) && (page != (uint32_t)(START_PAGE + (PAGES_NUMBER / 2U)))) + { + /* Decrement page index among circular pages list */ + page = PREVIOUS_PAGE(page); + pagestatus = GetPageState(PAGE_ADDRESS(page)); + + /* Check if page is valid state */ + if (pagestatus == STATE_PAGE_VALID) + { + /* Update uhNbWrittenElements with number of elements in full page */ + uhNbWrittenElements += NB_MAX_ELEMENTS_BY_PAGE; + + /* Keep index of first valid page */ + firstvalidpage = page; + } + else + { + /* Error: Pages sequence is not consistent */ + return EE_INVALID_PAGE_SEQUENCE; + } + } + + /*********************************************************************/ + /* Step 7: Ensure empty pages are erased */ + /*********************************************************************/ + + /* Ensure all pages after active page, until first valid page, are erased */ + page = FOLLOWING_PAGE(ubCurrentActivePage); + pageaddress = PAGE_ADDRESS(page); + + while (page != firstvalidpage) + { + /* Check if page erase has to be forced unconditionally (default case) */ + if (EraseType == EE_FORCED_ERASE) + { + /* Force page erase independently of its content */ + if (FI_PageErase(page, 1U) != EE_OK) + { + return EE_ERASE_ERROR; + } + } + else /* EraseType == EE_CONDITIONAL_ERASE */ + { + /* Check if page is fully erased */ + if (VerifyPageFullyErased(pageaddress, PAGE_SIZE) == EE_PAGE_NOTERASED) + { + /* Erase pages if not fully erased */ + if (FI_PageErase(page, 1U) != EE_OK) + { + return EE_ERASE_ERROR; + } + } + } + + /* Increment page index among circular pages list, to get first page to erased */ + page = FOLLOWING_PAGE(page); + pageaddress = PAGE_ADDRESS(page); + } + + /* To keep their coherency, flush the caches if needed depending on the product */ + FI_CacheFlush(); + + /*********************************************************************/ + /* Step 8: Perform dummy write '0' to get rid of potential */ + /* instability of line value 0xFFFFFFFF consecutive to a */ + /* reset during write here */ + /* Only needed if recovery transfer did not occured */ + /*********************************************************************/ + +if (recoverytransfer == 0U) + { + +#ifdef DUALCORE_FLASH_SHARING + status = VerifyPagesFullWriteVariable(0U, 0U, EE_INIT_WRITE); + + // The dummy write can be skipped in case pages are full + // because in this case potential instability can not happen + if ((status != EE_OK) && (status != EE_PAGE_FULL)) + { + return EE_WRITE_ERROR; + } +#else +#ifdef FLASH_LINES_128B + uint64_t to_write[2] = {0}; + status = VerifyPagesFullWriteVariable(0U, to_write, EE_INIT_WRITE); +#else + status = VerifyPagesFullWriteVariable(0U, 0U); +#endif + // The dummy write can be skipped in case pages are full + // because in this case potential instability can not happen + if ((status != EE_OK) && (status != EE_PAGE_FULL)) + { + return EE_WRITE_ERROR; + } +#endif + } + + return EE_OK; +} + +/** + * @brief Erases all flash pages of eeprom emulation, and set first page + * header as ACTIVE. + * @note This function can be called the very first time eeprom emulation is + * used, to prepare flash pages for eeprom emulation with empty eeprom + variables. It can also be called at any time, to flush all eeprom + * variables. + * @param EraseType: Type of erase to apply on page requiring to be erased. + * This parameter can be one of the following values: + * @arg @ref EE_FORCED_ERASE pages to erase are erased unconditionnally + * @arg @ref EE_CONDITIONAL_ERASE pages to erase are erased only if not fully erased + * @retval EE_Status + * - EE_OK: on success + * - EE error code: if an error occurs + */ +EE_Status EE_Format(EE_Erase_type EraseType) +{ + uint32_t page = 0U; + + /* Check if the configuration is 128-bits bank or 2*64-bits bank */ + if (FI_CheckBankConfig() != EE_OK) + { + return EE_INVALID_BANK_CFG; + } + +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON); +#endif + + /* Erase All Pages */ + for (page = START_PAGE; page < (START_PAGE + PAGES_NUMBER); page++) + { + /* Check if page erase has to be forced unconditionally (default case) */ + if (EraseType == EE_FORCED_ERASE) + { + /* Force page erase independently of its content */ + if (FI_PageErase(page, 1U) != EE_OK) + { +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about end of Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF); +#endif + return EE_ERASE_ERROR; + } + } + else /* EraseType == EE_CONDITIONAL_ERASE */ + { + /* Check if Page is not yet fully erased */ + if (VerifyPageFullyErased(PAGE_ADDRESS(page), PAGE_SIZE) == EE_PAGE_NOTERASED) + { + /* Erase the page */ + /* If Erase operation was failed, a Flash error code is returned */ + if (FI_PageErase(page, 1U) != EE_OK) + { +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about end of Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF); +#endif + return EE_ERASE_ERROR; + } + } + } + } + + /* To keep their coherency, flush the caches if needed depending on the product */ + FI_CacheFlush(); + +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about end of Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF); +#endif + + /* Set first Page in Active State */ + /* If program operation was failed, a Flash error code is returned */ + if (SetPageState(START_PAGE, STATE_PAGE_ACTIVE) != EE_OK) + { + return EE_WRITE_ERROR; + } + + /* Reset global variables */ + uhNbWrittenElements = (uint16_t)0U; + ubCurrentActivePage = START_PAGE; + uwAddressNextWrite = PAGE_HEADER_SIZE; /* Initialize write position just after page header */ + + return EE_OK; +} + +#if defined(FLASH_LINES_128B) +/** + * @brief Returns the last stored variable data, if found, which correspond to + * the passed virtual address + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param Data 96bits data to be written pointed by a uin64_t pointer + * @retval EE_Status + * - EE_OK: if variable was found + * - EE error code: if an error occurs + */ +EE_Status EE_ReadVariable96bits(uint16_t VirtAddress, uint64_t* pData) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { + EE_Status status = EE_OK; + + /* Read variable of size EE_DATA_TYPE */ + status = ReadVariable(VirtAddress, pData); + + return status; + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} +#endif + +#if defined(EE_ACCESS_32BITS) +/** + * @brief Returns the last stored variable data, if found, which correspond to + * the passed virtual address + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param pData Variable containing the 32bits read variable value + * @retval EE_Status + * - EE_OK: if variable was found + * - EE error code: if an error occurs + */ +EE_Status EE_ReadVariable32bits(uint16_t VirtAddress, uint32_t* pData) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { + EE_DATA_TYPE datatmp = 0U; + EE_Status status = EE_OK; + + /* Read variable of size EE_DATA_TYPE, then cast it to 32bits */ + status = ReadVariable(VirtAddress, &datatmp); + *pData = (uint32_t) datatmp; + + return status; + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} +#endif + +/** + * @brief Returns the last stored variable data, if found, which correspond to + * the passed virtual address + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param pData Variable containing the 16bits read variable value + * @retval EE_Status + * - EE_OK: if variable was found + * - EE error code: if an error occurs + */ +EE_Status EE_ReadVariable16bits(uint16_t VirtAddress, uint16_t* pData) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { + EE_DATA_TYPE datatmp = 0U; + EE_Status status = EE_OK; + + /* Read variable of size EE_DATA_TYPE, then cast it to 16bits */ + status = ReadVariable(VirtAddress, &datatmp); + *pData = (uint16_t) datatmp; + + return status; + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} + +/** + * @brief Returns the last stored variable data, if found, which correspond to + * the passed virtual address + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param pData Variable containing the 8bits read variable value + * @retval EE_Status + * - EE_OK: if variable was found + * - EE error code: if an error occurs + */ +EE_Status EE_ReadVariable8bits(uint16_t VirtAddress, uint8_t* pData) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { + EE_DATA_TYPE datatmp = 0U; + EE_Status status = EE_OK; + + /* Read variable of size EE_DATA_TYPE, then cast it to 8bits */ + status = ReadVariable(VirtAddress, &datatmp); + *pData = (uint8_t) datatmp; + + return status; + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} + +#if defined(FLASH_LINES_128B) +/** + * @brief Writes/updates variable data in EEPROM. + * Trig internal Pages transfer if half of the pages are full. + * @warning This function is not reentrant + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param Data 96bits data to be written pointed by a uin64_t pointer + * @retval EE_Status + * - EE_OK: on success + * - EE_CLEANUP_REQUIRED: success and user has to trig flash pages cleanup + * - EE error code: if an error occurs + */ +EE_Status EE_WriteVariable96bits(uint16_t VirtAddress, uint64_t* Data) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { + return WriteVariable(VirtAddress, (EE_DATA_TYPE*)Data); + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} +#endif + +#if defined(EE_ACCESS_32BITS) +/** + * @brief Writes/updates variable data in EEPROM. + * Trig internal Pages transfer if half of the pages are full. + * @warning This function is not reentrant + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param Data 32bits data to be written + * @retval EE_Status + * - EE_OK: on success + * - EE_CLEANUP_REQUIRED: success and user has to trig flash pages cleanup + * - EE error code: if an error occurs + */ +EE_Status EE_WriteVariable32bits(uint16_t VirtAddress, uint32_t Data) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { +#ifndef FLASH_LINES_128B + return WriteVariable(VirtAddress, (EE_DATA_TYPE) Data); +#else + return WriteVariable(VirtAddress, (EE_DATA_TYPE*) &Data); +#endif + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} +#endif + +/** + * @brief Writes/updates variable data in EEPROM. + * Trig internal Pages transfer if half of the pages are full. + * @warning This function is not reentrant + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param Data 16bits data to be written + * @retval EE_Status + * - EE_OK: on success + * - EE_CLEANUP_REQUIRED: success and user has to trig flash pages cleanup + * - EE error code: if an error occurs + */ +EE_Status EE_WriteVariable16bits(uint16_t VirtAddress, uint16_t Data) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { +#ifndef FLASH_LINES_128B + return WriteVariable(VirtAddress, (EE_DATA_TYPE) Data); +#else + return WriteVariable(VirtAddress, (EE_DATA_TYPE*) &Data); +#endif + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} + +/** + * @brief Writes/updates variable data in EEPROM. + * Trig internal Pages transfer if half of the pages are full. + * @warning This function is not reentrant + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param Data 8bits data to be written + * @retval EE_Status + * - EE_OK: on success + * - EE_CLEANUP_REQUIRED: success and user has to trig flash pages cleanup + * - EE error code: if an error occurs + */ +EE_Status EE_WriteVariable8bits(uint16_t VirtAddress, uint8_t Data) +{ + if((VirtAddress != 0x0000) && (VirtAddress != 0xFFFF)) + { +#ifndef FLASH_LINES_128B + return WriteVariable(VirtAddress, (EE_DATA_TYPE) Data); +#else + return WriteVariable(VirtAddress, (EE_DATA_TYPE*) &Data); +#endif + } + else + { + return EE_INVALID_VIRTUALADDRESS; + } +} + +/** + * @brief Erase group of pages which are erasing state, in polling mode. + * Could be either first half or second half of total pages number. + * @note This function should be called when EE_WriteVariableXXbits has + * returned EE_CLEANUP_REQUIRED status (and only in that case) + * @retval EE_Status + * - EE_OK: in case of success + * - EE error code: if an error occurs + */ +EE_Status EE_CleanUp(void) +{ + uint32_t firstpage = 0U, page = 0U; + uint32_t firstpageaddress = 0U, pageaddress = 0U; + EE_State_type firstpagestatus = STATE_PAGE_INVALID, pagestatus = STATE_PAGE_INVALID; + + /* Check first half and second half page group */ + for (firstpage = START_PAGE; firstpage < (START_PAGE + PAGES_NUMBER); firstpage += (PAGES_NUMBER / 2U)) + { + /* Check status of first page of the group */ + firstpageaddress = PAGE_ADDRESS(firstpage); + firstpagestatus = GetPageState(firstpageaddress); + + /* If first page of the group is erasing state, check that all other pages + of the group are also erasing state */ + if (firstpagestatus == STATE_PAGE_ERASING) + { + for (page = (firstpage + 1U); page < (firstpage + (PAGES_NUMBER / 2U)); page++) + { + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + + /* If page is not erasing, return error */ + if (pagestatus != STATE_PAGE_ERASING) + { + return EE_ERROR_NOERASING_PAGE; + } + } + +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON); +#endif + + /* Erase all the pages of the group */ + /* If erase operation fails, a Flash error code is returned */ + if (FI_PageErase(firstpage, PAGES_NUMBER / 2U) != EE_OK) + { +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about end of Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF); +#endif + return EE_ERASE_ERROR; + } + else + { +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about end of Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF); +#endif + + /* To keep their coherency, flush the caches if needed depending on the product */ + FI_CacheFlush(); + + return EE_OK; + } + } + } + + /* Error if no erasing pages group is found */ + return EE_ERROR_NOERASING_PAGE; +} + +/** + * @brief Erase group of pages which are erasing state, in IT mode. + * Could be either first half or second half of total pages number. + * @note This function should be called when EE_WriteVariableXXbits has + * returned EE_CLEANUP_REQUIRED status (and only in that case) + * @retval EE_Status + * - EE_OK: in case of success + * - EE error code: if an error occurs + */ +EE_Status EE_CleanUp_IT(void) +{ + uint32_t firstpage = 0U, page = 0U; + uint32_t firstpageaddress = 0U, pageaddress = 0U; + EE_State_type firstpagestatus = STATE_PAGE_INVALID, pagestatus = STATE_PAGE_INVALID; + + /* Check first half and second half page group */ + for (firstpage = START_PAGE; firstpage < (START_PAGE + PAGES_NUMBER); firstpage += (PAGES_NUMBER / 2U)) + { + /* Check status of first page of the group */ + firstpageaddress = PAGE_ADDRESS(firstpage); + firstpagestatus = GetPageState(firstpageaddress); + + /* If first page of the group is erasing state, check that all other pages + of the group are also erasing state */ + if (firstpagestatus == STATE_PAGE_ERASING) + { + for (page = (firstpage + 1U); page < (firstpage + (PAGES_NUMBER / 2U)); page++) + { + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + + /* If page is not erasing, return error */ + if (pagestatus != STATE_PAGE_ERASING) + { + return EE_ERROR_NOERASING_PAGE; + } + } + +#ifdef DUALCORE_FLASH_SHARING + /* Inform CPU2 about Erase Activity */ + SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON); +#endif + + /* Erase all the pages of the group */ + /* If erase operation fails, a Flash error code is returned */ + if (FI_PageErase_IT(firstpage, PAGES_NUMBER / 2U) != EE_OK) + { + return EE_ERASE_ERROR; + } + else + { + /* To keep their coherency, flush the caches if needed depending on the product */ + FI_CacheFlush(); + return EE_OK; + } + } + } + + /* Error if no erasing pages group is found */ + return EE_ERROR_NOERASING_PAGE; +} + +/** + * @brief Delete corrupted Flash address, can be called under NMI. + * @param Address Address of the FLASH Memory to delete + * @retval EE_Status + * - EE_OK: on success + * - EE error code: if an error occurs + */ +EE_Status EE_DeleteCorruptedFlashAddress(uint32_t Address) +{ + return FI_DeleteCorruptedFlashAddress(Address); +} + +/** + * @brief Clean Up end of operation interrupt callback. + * @retval None + */ +__weak void EE_EndOfCleanup_UserCallback(void) +{ + /* NOTE : This function should not be modified, when the callback is needed, + the EE_EndOfCleanup_UserCallback could be implemented in the user file + */ +} + +/** + * @} + */ + +/* Private functions ---------------------------------------------------------*/ +/** @addtogroup EEPROM_Private_Functions + * @{ + */ + +/** + * @brief Returns the last stored variable data, if found, which correspond to + * the passed virtual address + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param pData Variable containing the EE_DATA_TYPE read variable value + * @retval EE_Status + * - EE_OK: if variable was found + * - EE error code: if an error occurs + */ +#ifndef FLASH_LINES_128B +static EE_Status ReadVariable(uint16_t VirtAddress, EE_DATA_TYPE* pData) +#else +static EE_Status ReadVariable(uint16_t VirtAddress, EE_DATA_TYPE* pData) +#endif +{ + EE_ELEMENT_TYPE addressvalue = 0U; +#ifdef FLASH_LINES_128B + EE_ELEMENT_TYPE addressvalue2 = 0U; +#endif + uint32_t page = 0U, pageaddress = 0U, counter = 0U, crc = 0U; + EE_State_type pagestate = STATE_PAGE_INVALID; + + /* Get active Page for read operation */ + page = FindPage(FIND_READ_PAGE); + + /* Check if there is no active page */ + if (page == EE_NO_PAGE_FOUND) + { + return EE_ERROR_NOACTIVE_PAGE; + } + pageaddress = PAGE_ADDRESS(page); + pagestate = GetPageState(pageaddress); + + /* Search variable in active page and valid pages until erased page is found + or in erasing pages until erased page is found */ + while ((pagestate == STATE_PAGE_ACTIVE) || (pagestate == STATE_PAGE_VALID) || (pagestate == STATE_PAGE_ERASING)) + { + /* Set counter index to last element position in the page */ + counter = PAGE_SIZE - EE_ELEMENT_SIZE; + + /* Check each page address starting from end */ + while (counter >= PAGE_HEADER_SIZE) + { + /* Get the current location content to be compared with virtual address */ + addressvalue = (*(__IO EE_ELEMENT_TYPE*)(pageaddress + counter)); +#ifndef FLASH_LINES_128B + if (addressvalue != EE_PAGESTAT_ERASED) + { + /* Compare the read address with the virtual address */ + if (EE_VIRTUALADDRESS_VALUE(addressvalue) == VirtAddress) + { + /* Calculate crc of variable data and virtual address */ + crc = CalculateCrc(EE_DATA_VALUE(addressvalue), EE_VIRTUALADDRESS_VALUE(addressvalue)); + + /* if crc verification pass, data is correct and is returned. + if crc verification fails, data is corrupted and has to be skip */ + if (crc == EE_CRC_VALUE(addressvalue)) + { + /* Get content of variable value */ + *pData = EE_DATA_VALUE(addressvalue); + + return EE_OK; + } + } + } +#else + addressvalue2 = (*(__IO EE_ELEMENT_TYPE*)(pageaddress + counter+8U)); + if ((addressvalue != EE_PAGESTAT_ERASED) || (addressvalue2 != EE_PAGESTAT_ERASED)) + { + /* Compare the read address with the virtual address */ + if (EE_VIRTUALADDRESS_VALUE(addressvalue) == VirtAddress) + { + /* Calculate crc of variable data and virtual address */ + crc = CalculateCrc((uint64_t)addressvalue2,(uint64_t)addressvalue); + + /* if crc verification pass, data is correct and is returned. + if crc verification fails, data is corrupted and has to be skip */ + if (crc == EE_CRC_VALUE(addressvalue)) + { + /* Get content of variable value */ + pData[0] = (uint64_t)addressvalue2; + pData[1] = (uint64_t)(addressvalue >> EE_DATA_SHIFT); + + return EE_OK; + } + } + } +#endif + /* Next address location */ + counter -= EE_ELEMENT_SIZE; + } + + /* Decrement page index circularly, among pages allocated to eeprom emulation */ + page = PREVIOUS_PAGE(page); + pageaddress = PAGE_ADDRESS(page); + pagestate = GetPageState(pageaddress); + } + + /* Variable is not found */ + return EE_NO_DATA; +} + +/** + * @brief Writes/updates variable data in EEPROM + * Trig internal Pages transfer if half of the pages are full + * @param VirtAddress Variable virtual address on 16 bits (can't be 0x0000 or 0xFFFF) + * @param Data EE_DATA_TYPE data to be written + * @retval EE_Status + * - EE_OK: on success, without page transfer + * - EE_CLEANUP_REQUIRED: on success, with page transfer occured + * - EE_FLASH_USED: flash currently used by CPU2 + * - EE error code: if an error occurs + */ +#ifndef FLASH_LINES_128B +static EE_Status WriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data) +#else +static EE_Status WriteVariable(uint16_t VirtAddress, EE_DATA_TYPE* Data) +#endif +{ + EE_Status status = EE_OK; + + /* Write the variable virtual address and value in the EEPROM, if not full */ +#if defined (DUALCORE_FLASH_SHARING) || defined (FLASH_LINES_128B) + status = VerifyPagesFullWriteVariable(VirtAddress, Data, EE_SIMPLE_WRITE); +#else + status = VerifyPagesFullWriteVariable(VirtAddress, Data); +#endif + if (status == EE_PAGE_FULL) + { + /* In case the EEPROM pages are full, perform Pages transfer */ + return PagesTransfer(VirtAddress, Data, EE_TRANSFER_NORMAL); + } + + /* Return last operation status */ + return status; +} + +/** + * @brief Verify if specified page is fully erased. + * @param Address page address + * @param PageSize page size + * @retval EE_Status + * - EE_PAGE_NOTERASED : if Page not erased + * - EE_PAGE_ERASED : if Page erased + */ +static EE_Status VerifyPageFullyErased(uint32_t Address, uint32_t PageSize) +{ + EE_Status readstatus = EE_PAGE_ERASED; + uint32_t counter = 0U; + + /* Check each element in the page */ + while (counter < PageSize) + { + /* Compare the read address with the virtual address */ +#ifndef FLASH_LINES_128B + if ((*(__IO EE_ELEMENT_TYPE*)(Address+counter)) != EE_PAGESTAT_ERASED) +#else + if ( ((*(__IO EE_ELEMENT_TYPE*)(Address+counter)) != EE_PAGESTAT_ERASED) && ((*(__IO EE_ELEMENT_TYPE*)(Address+counter+8U)) != EE_PAGESTAT_ERASED) ) +#endif + { + /* In case one element is not erased, reset readstatus flag */ + readstatus = EE_PAGE_NOTERASED; + } + /* Next address location */ + counter = counter + EE_ELEMENT_SIZE; + } + + /* Return readstatus value */ + return readstatus; +} + +/** + * @brief Find suitable page for read/write/erase operation. + * It also update pages state if current page is full. + * And it force cleanup if all pages are full. + * @param Operation Type of page to be requested. + * This parameter can be one of the following values: + * @arg @ref FIND_READ_PAGE return the active page index + * @arg @ref FIND_WRITE_PAGE return the write page index + * @arg @ref FIND_ERASE_PAGE return the erase page index + * @retval Page_Index + * - Page Index: on success + * - @ref EE_NO_PAGE_FOUND : if an error occurs + */ +static uint32_t FindPage(EE_Find_type Operation) +{ + EE_State_type currentpagestatus = STATE_PAGE_INVALID, followingpagestatus = STATE_PAGE_INVALID; + uint32_t currentpage = 0U, followingpage = 0U, previouspage = 0U; + + /* Get currentpage status */ + currentpage = ubCurrentActivePage; + currentpagestatus = GetPageState(PAGE_ADDRESS(currentpage)); + + /* Get followingpage status */ + followingpage = FOLLOWING_PAGE(currentpage); + followingpagestatus = GetPageState(PAGE_ADDRESS(followingpage)); + + /* Get previouspage status */ + previouspage = PREVIOUS_PAGE(currentpage); + + /* Write, read or erase operation */ + switch (Operation) + { + case FIND_WRITE_PAGE: /* ---- Write operation ---- */ + /* Normal operation, no page transfer on going */ + if (currentpagestatus == STATE_PAGE_ACTIVE) + { + /* Check if active page is not full */ + if (uwAddressNextWrite < PAGE_SIZE) + { + /* Return current Active page */ + return currentpage; + } + else + /* No more space in current active page */ + { + /* Check if following page is erasing state */ + if (followingpagestatus == STATE_PAGE_ERASING) + { + /* Force Cleanup, as not yet performed by user */ + if (EE_CleanUp() != EE_OK) + { + return EE_NO_PAGE_FOUND; + } + } + + /* Set current active page in valid state */ + if (SetPageState(currentpage, STATE_PAGE_VALID) != EE_OK) + { + return EE_NO_PAGE_FOUND; + } + + /* Set following page as active */ + if (SetPageState(followingpage, STATE_PAGE_ACTIVE) != EE_OK) + { + return EE_NO_PAGE_FOUND; + } + uwAddressNextWrite = PAGE_HEADER_SIZE; /* Skip page header */ + return followingpage; /* Following page is now active one */ + } + } + /* Transfer is on going, page receiving data */ + else + { + if (currentpagestatus == STATE_PAGE_RECEIVE) + { + /* Check if receive page is not full */ + if (uwAddressNextWrite < PAGE_SIZE) + { + /* Return current receive page */ + return currentpage; + } + else + /* No more space in current receive page */ + { + /* Check if following page is erasing state */ + if (followingpagestatus == STATE_PAGE_ERASING) + { + /* Force Cleanup, as not yet performed by user */ + if (EE_CleanUp() != EE_OK) + { + return EE_NO_PAGE_FOUND; + } + } + + /* Set current receive page in valid state */ + if (SetPageState(currentpage, STATE_PAGE_VALID) != EE_OK) + { + return EE_NO_PAGE_FOUND; + } + + /* Set following page as receive */ + if (SetPageState(followingpage, STATE_PAGE_RECEIVE) != EE_OK) + { + return EE_NO_PAGE_FOUND; + } + uwAddressNextWrite = PAGE_HEADER_SIZE; /* Skip page header */ + return followingpage; /* Following page is now active one */ + } + } + else + { + return EE_NO_PAGE_FOUND; /* No active Page */ + } + } + + case FIND_READ_PAGE: /* ---- Read operation ---- */ + if (currentpagestatus == STATE_PAGE_ACTIVE) + { + return currentpage; + } + else + { + if (currentpagestatus == STATE_PAGE_RECEIVE) + { + return previouspage; + } + else + { + return EE_NO_PAGE_FOUND; /* No active Page */ + } + } + + case FIND_ERASE_PAGE: /* ---- Return the erased page */ + if (followingpagestatus == STATE_PAGE_ERASED) + { + return followingpage; + } + else + { + return EE_NO_PAGE_FOUND; /* No erased Page */ + } + + default: + ; + } + + return EE_NO_PAGE_FOUND; +} + +/** + * @brief Writes a new variable data in fresh new page in case of normal + * transfer, and transfers last updated elements from full pages to + * empty pages in any cases. + * @param VirtAddress 16 bit virtual address of the new variable data + * @param Data @ref EE_DATA_TYPE data value of the new variable data + * @param Type Type of transfer. + * This parameter can be one of the EE_Transfer_type enum values. + * @arg @ref EE_TRANSFER_NORMAL Pages transfer during normal processing + * @arg @ref EE_TRANSFER_RECOVER Recovering pages transfer at Init + * @retval EE_Status + * - EE_CLEANUP_REQUIRED: on success + * - EE error code: if an error occurs + */ +#ifndef FLASH_LINES_128B +static EE_Status PagesTransfer(uint16_t VirtAddress, EE_DATA_TYPE Data, EE_Transfer_type Type) +#else +static EE_Status PagesTransfer(uint16_t VirtAddress, EE_DATA_TYPE* Data, EE_Transfer_type Type) +#endif +{ + EE_State_type pagestatus = STATE_PAGE_INVALID; + uint32_t pageaddress = 0U; + uint32_t page = 0U; + uint32_t varidx = 0U; + EE_ELEMENT_TYPE addressvalue = 0U; +#ifdef FLASH_LINES_128B + EE_ELEMENT_TYPE addressvalue2 = 0U; + uint16_t nb_dummy_lines=0U; +#endif + EE_Status status = EE_OK; + EE_DATA_TYPE DataValue = 0U; + + + /* Get receive Page for transfer operation */ + page = FindPage((Type == EE_TRANSFER_NORMAL?FIND_ERASE_PAGE:FIND_WRITE_PAGE)); + if (page == EE_NO_PAGE_FOUND) + { + return EE_ERROR_NOERASE_PAGE; + } + + /* Reinitialize number of data written in the pages, and current active page */ + uhNbWrittenElements = 0U; + ubCurrentActivePage = page; + uwAddressNextWrite = PAGE_HEADER_SIZE; + + /* Mark the erased page at receive state in case of normal transfer */ + /* It is already the case in recover transfer case */ + /* If program operation was failed, a Flash error code is returned */ + if (Type == EE_TRANSFER_NORMAL) + { + if (SetPageState(page, STATE_PAGE_RECEIVE) != EE_OK) + { + return EE_WRITE_ERROR; + } + } + + /* Set the previous active page and all previous valid pages to erasing state */ + /* In case of recover transfer, some of these pages may already be marked erasing */ + page = PREVIOUS_PAGE(page); + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + + if ((pagestatus == STATE_PAGE_ACTIVE) || (pagestatus == STATE_PAGE_ERASING)) + { + /* Set active page to erasing */ + if (pagestatus == STATE_PAGE_ACTIVE) + { + if (SetPageState(page, STATE_PAGE_ERASING) != EE_OK) + { + return EE_WRITE_ERROR; + } + } + + /* Inspect the previous pages to set all valid pages to erasing state */ + /* In case of recover, some valid pages may be already erasing state */ + page = PREVIOUS_PAGE(page); + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + + while ((pagestatus == STATE_PAGE_VALID) || (pagestatus == STATE_PAGE_ERASING)) + { + /* Set valid page to erasing */ + if (pagestatus == STATE_PAGE_VALID) + { + if (SetPageState(page, STATE_PAGE_ERASING) != EE_OK) + { + return EE_WRITE_ERROR; + } + } + + /* decrement page index */ + page = PREVIOUS_PAGE(page); + pageaddress = PAGE_ADDRESS(page); + pagestatus = GetPageState(pageaddress); + } + } + else + { + if ((Type == EE_TRANSFER_RECOVER) && (pagestatus == STATE_PAGE_VALID)) + { + /* This can happen in case of recover transfer. It indicates that previous */ + /* transfer goes far enough to fill a complete receive page at least */ + /* (valid state). Then erasing state marking was already completed */ + } + else + { + /* Inconsistent previous page state */ + return EE_INVALID_PAGE_SEQUENCE; + } + } + + /* In case of recover transfer, transfer must be resumed where it has been stopped */ + /* Update global variables to reflect current transfer status */ + if (Type == EE_TRANSFER_RECOVER) + { +#ifdef FLASH_LINES_128B + nb_dummy_lines=0; +#endif + /* Count number of elements already transferred in current receive page */ + for (varidx = PAGE_HEADER_SIZE; varidx < PAGE_SIZE; varidx += EE_ELEMENT_SIZE) + { + /* Get next element in receive page */ + addressvalue = (*(__IO EE_ELEMENT_TYPE*)(PAGE_ADDRESS(ubCurrentActivePage) + varidx)); + +#ifdef FLASH_LINES_128B + addressvalue2 = (*(__IO EE_ELEMENT_TYPE*)(PAGE_ADDRESS(ubCurrentActivePage) + varidx + 8)); + + /* Check if element is valid */ + if((addressvalue == 0x0000000000000000U)&& (addressvalue2 == 0x0000000000000000U)) + { + nb_dummy_lines++; + uhNbWrittenElements++; + uwAddressNextWrite += EE_ELEMENT_SIZE; + } + else if ((addressvalue != EE_PAGESTAT_ERASED) || (addressvalue2 != EE_PAGESTAT_ERASED)&&(addressvalue != 0x0000000000000000U)|| (addressvalue2 != 0x0000000000000000U)) +#else + if (addressvalue != EE_PAGESTAT_ERASED) +#endif + { + /* Update global variables accordingly */ + uhNbWrittenElements++; + uwAddressNextWrite += EE_ELEMENT_SIZE; + } + else + { + break; + } + } + + /* Count number of elements already transferred in previous valid pages */ + page = ubCurrentActivePage; + for (varidx = 0U; varidx < PAGES_NUMBER; varidx++) + { + /* Decrement page index among circular pages list */ + page = PREVIOUS_PAGE(page); + pagestatus = GetPageState(PAGE_ADDRESS(page)); + + /* Check if page is valid state */ + if (pagestatus == STATE_PAGE_VALID) + { + /* Update uhNbWrittenElements with number of elements in page */ + uhNbWrittenElements += NB_MAX_ELEMENTS_BY_PAGE; +#ifdef FLASH_LINES_128B + nb_dummy_lines=nb_dummy_lines+2; +#endif + } + else + { + break; + } + } + } + +#if defined (DUALCORE_FLASH_SHARING) || defined (FLASH_LINES_128B) + if (VerifyPagesFullWriteVariable(VirtAddress, Data, EE_TRANSFER) != EE_OK) + { + return EE_WRITE_ERROR; + } + #ifdef FLASH_LINES_128B + else + { + if ((Data[0]==0)&&(Data[1]==0)) + { + nb_dummy_lines++; //One 128bit line of zeros has been written + } + } + #endif +#else + + // Write the variable passed as parameter in the new active page + // If program operation was failed, a Flash error code is returned + if (VerifyPagesFullWriteVariable(VirtAddress, Data) != EE_OK) + { + return EE_WRITE_ERROR; + } + +#endif + + + /* Transfer process: transfer variables from old to the new active page */ + /* First element in receive page can be any one, the following elements are */ + /* ordered from the beginning. */ + /* In case of recovery, Pre-Last element in receive page could be */ + /* corrupted if reset occured during write of this element, */ + /* and last element is dummy value that we have just written. */ + /* Transfer shall then resume from (uhNbWrittenElements-3) variable index */ + + +#ifdef FLASH_LINES_128B + varidx = (uhNbWrittenElements >= 3U?(uhNbWrittenElements-3U+1U):1U); + for (varidx = (varidx >= nb_dummy_lines?(varidx-nb_dummy_lines):1U); varidx < NB_OF_VARIABLES+1; varidx++) +#else + for (varidx = (uhNbWrittenElements >= 3U?(uhNbWrittenElements-3U+1U):1U); varidx < NB_OF_VARIABLES+1; varidx++) +#endif + { + /* Check each variable except the one passed as parameter */ + if (varidx != VirtAddress) + { + /* Read the last variable updates */ + status = ReadVariable(varidx, &DataValue); + if (status == EE_OK) + { + /* In case variable corresponding to the virtual address was found */ + /* Transfer the variable to the new active page */ + /* If program operation was failed, a Flash error code is returned */ + #if defined (DUALCORE_FLASH_SHARING) + status = VerifyPagesFullWriteVariable(varidx, DataValue, EE_TRANSFER); + #elif defined (FLASH_LINES_128B) + status = VerifyPagesFullWriteVariable(varidx, &DataValue, EE_TRANSFER); + #else + status = VerifyPagesFullWriteVariable(varidx, DataValue); + #endif + if (status != EE_OK) + { + return status; + } + } + else + { + if (status != EE_NO_DATA) + { + /* In case variable is not found , do nothing */ + /* Any other status is error code occurs during variable read */ + return status; + } + } + } + } + + /* Transfer is now done, mark the receive state page as active */ + if (SetPageState(ubCurrentActivePage, STATE_PAGE_ACTIVE) != EE_OK) + { + return EE_WRITE_ERROR; + } + + /* Return last operation flash status */ + return EE_CLEANUP_REQUIRED; +} + +/** + * @brief Verify if pages are full + * then if not the case, writes variable in EEPROM. + * @param VirtAddress 16 bit virtual address of the variable + * @param Data @ref EE_DATA_TYPE data to be written as variable value + * @param Write_type Type of writing on going (see EE_Write_type) + * @retval EE_Status + * - EE_OK: on success + * - EE_FULL: if half pages are full + * - EE_FLASH_USED: flash currently used by CPU2 + * - EE error code: if an error occurs + */ +#if defined (DUALCORE_FLASH_SHARING) +static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data, EE_Write_type Write_type) +#elif defined (FLASH_LINES_128B) +static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE* Data, EE_Write_type Write_type) +#else +static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data) +#endif +{ +#ifndef FLASH_LINES_128B + uint32_t crc = 0U; +#else + uint64_t pData64[2] = {0,0}; + uint64_t crc = 0U; +#endif + + /* Check if pages are full, i.e. max number of written elements achieved */ + if (uhNbWrittenElements >= NB_MAX_WRITTEN_ELEMENTS) + { + return EE_PAGE_FULL; + } + + /* Get active Page for write operation */ + uint32_t activepage = FindPage(FIND_WRITE_PAGE); + uint32_t activepageaddress = 0U; + + /* Check if there is no active page */ + if (activepage == EE_NO_PAGE_FOUND) + { + return EE_ERROR_NOACTIVE_PAGE; + } + + activepageaddress = PAGE_ADDRESS(activepage); + + /* Force crc to 0 in case of Data/VirtAddress are 0*/ +#ifndef FLASH_LINES_128B + if ((Data == 0U) && (VirtAddress == 0U)) +#else + if ((Data[0] == 0U) && (Data[1] == 0U) && (VirtAddress == 0U)) + +#endif + { + crc = 0U; + } + else + { +#ifndef FLASH_LINES_128B + /* Calculate crc of variable data and virtual address */ + crc = CalculateCrc(Data, VirtAddress); +#else + pData64[0] = (uint64_t)((Data[1] << EE_DATA_SHIFT) | VirtAddress); + /* Calculate crc of variable data and virtual address */ + crc = CalculateCrc(Data[0], pData64[0]); +#endif + } + + +#if defined (DUALCORE_FLASH_SHARING) + /* Program variable data + virtual address + crc */ + /* If program operation was failed, a Flash error code or the information + about the semaphore monitoring flash being taken is returned */ + EE_Status ee_status = FI_WriteDoubleWord(activepageaddress+uwAddressNextWrite, EE_ELEMENT_VALUE(VirtAddress,Data,crc), Write_type); + if (ee_status != EE_OK) return ee_status; +#elif defined (FLASH_LINES_128B) + pData64[0] = 0; + pData64[1] = 0; + pData64[0] = (uint64_t)((Data[1]<< EE_DATA_SHIFT) | (crc << EE_CRC_SHIFT) | VirtAddress); + pData64[1] = (uint64_t)Data[0]; + + EE_Status ee_status = FI_WriteDoubleWord(activepageaddress+uwAddressNextWrite, pData64, Write_type); + if (ee_status != EE_OK) return ee_status; +#elif defined (EDATA_ENABLED) + /* Program variable data + virtual address + crc */ + /* If program operation was failed, a Flash error code is returned */ + if (FI_WriteDoubleWord(activepageaddress+uwAddressNextWrite, EE_ELEMENT_VALUE(VirtAddress,Data,crc)) != EE_OK) + { + return EE_WRITE_ERROR; + } +#else + /* Program variable data + virtual address + crc */ + /* If program operation was failed, a Flash error code is returned */ + if (FI_WriteDoubleWord(activepageaddress+uwAddressNextWrite, EE_ELEMENT_VALUE(VirtAddress,Data,crc)) != HAL_OK) + { + return EE_WRITE_ERROR; + } +#endif + + /* Increment global variables relative to write operation done*/ + uwAddressNextWrite += EE_ELEMENT_SIZE; + uhNbWrittenElements++; + + return EE_OK; +} + +/** + * @brief Set page state in page header + * @param Page Index of the page + * @param State State of the page + * @retval EE_Status + * - EE_OK: on success + * - EE error code: if an error occurs + */ +static EE_Status SetPageState(uint32_t Page, EE_State_type State) +{ + uint32_t header1 = 0U, header2 = 0U, header3 = 0U, header4 = 0U; + + header1 = PAGE_ADDRESS(Page); + header2 = PAGE_ADDRESS(Page) + EE_ELEMENT_SIZE; + header3 = PAGE_ADDRESS(Page) + (EE_ELEMENT_SIZE*2U); + header4 = PAGE_ADDRESS(Page) + (EE_ELEMENT_SIZE*3U); + +#if defined (DUALCORE_FLASH_SHARING) + EE_Status ee_status; + switch(State) + { + case STATE_PAGE_RECEIVE: + { + /* Set new Page status to STATE_PAGE_RECEIVE status */ + ee_status = FI_WriteDoubleWord(header1, EE_PAGESTAT_RECEIVE, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_ACTIVE: + { + /* Set new Page status to STATE_PAGE_ACTIVE status */ + ee_status = FI_WriteDoubleWord(header2, EE_PAGESTAT_ACTIVE, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_VALID: + { + /* Set new Page status to STATE_PAGE_VALID status */ + ee_status = FI_WriteDoubleWord(header3, EE_PAGESTAT_VALID, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + } + break; + case STATE_PAGE_ERASING: + { + /* Set new Page status to STATE_PAGE_ERASING status */ + ee_status = FI_WriteDoubleWord(header4, EE_PAGESTAT_ERASING, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + } + break; + default: + break; + } +#elif defined (FLASH_LINES_128B) + EE_Status ee_status; + uint64_t pagestat = 0; + switch(State) + { + case STATE_PAGE_RECEIVE: + { + /* Set new Page status to STATE_PAGE_RECEIVE status */ + pagestat = EE_PAGESTAT_RECEIVE; + ee_status = FI_WriteDoubleWord(header1, &pagestat, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_ACTIVE: + { + /* Set new Page status to STATE_PAGE_ACTIVE status */ + pagestat = EE_PAGESTAT_ACTIVE; + ee_status = FI_WriteDoubleWord(header2, &pagestat, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_VALID: + { + /* Set new Page status to STATE_PAGE_VALID status */ + pagestat = EE_PAGESTAT_VALID; + ee_status = FI_WriteDoubleWord(header3, &pagestat, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + } + break; + case STATE_PAGE_ERASING: + { + /* Set new Page status to STATE_PAGE_ERASING status */ + pagestat = EE_PAGESTAT_ERASING; + ee_status = FI_WriteDoubleWord(header4, &pagestat, EE_SET_PAGE); + if (ee_status != EE_OK) return ee_status; + } + break; + default: + break; + } +#elif defined (EDATA_ENABLED) + switch(State) + { + case STATE_PAGE_RECEIVE: + { + /* Set new Page status to STATE_PAGE_RECEIVE status */ + if (FI_WriteDoubleWord(header1, EE_PAGESTAT_RECEIVE) != EE_OK) + { + return EE_WRITE_ERROR; + } + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_ACTIVE: + { + /* Set new Page status to STATE_PAGE_ACTIVE status */ + if (FI_WriteDoubleWord(header2, EE_PAGESTAT_ACTIVE) != EE_OK) + { + return EE_WRITE_ERROR; + } + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_VALID: + { + /* Set new Page status to STATE_PAGE_VALID status */ + if (FI_WriteDoubleWord(header3, EE_PAGESTAT_VALID) != EE_OK) + { + return EE_WRITE_ERROR; + } + } + break; + case STATE_PAGE_ERASING: + { + /* Set new Page status to STATE_PAGE_ERASING status */ + if (FI_WriteDoubleWord(header4, EE_PAGESTAT_ERASING) != EE_OK) + { + return EE_WRITE_ERROR; + } + } + break; + default: + break; + } +#else + switch(State) + { + case STATE_PAGE_RECEIVE: + { + /* Set new Page status to STATE_PAGE_RECEIVE status */ + if (FI_WriteDoubleWord(header1, EE_PAGESTAT_RECEIVE) != HAL_OK) + { + return EE_WRITE_ERROR; + } + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_ACTIVE: + { + /* Set new Page status to STATE_PAGE_ACTIVE status */ + if (FI_WriteDoubleWord(header2, EE_PAGESTAT_ACTIVE) != HAL_OK) + { + return EE_WRITE_ERROR; + } + ubCurrentActivePage = Page; + } + break; + case STATE_PAGE_VALID: + { + /* Set new Page status to STATE_PAGE_VALID status */ + if (FI_WriteDoubleWord(header3, EE_PAGESTAT_VALID) != HAL_OK) + { + return EE_WRITE_ERROR; + } + } + break; + case STATE_PAGE_ERASING: + { + /* Set new Page status to STATE_PAGE_ERASING status */ + if (FI_WriteDoubleWord(header4, EE_PAGESTAT_ERASING) != HAL_OK) + { + return EE_WRITE_ERROR; + } + } + break; + default: + break; + } +#endif + + /* Return last operation flash status */ + return EE_OK; +} + +/** + * @brief Get page state in page header + * @param Address Address of the FLASH Memory page + * @retval State State of the page + */ +#ifndef FLASH_LINES_128B +static EE_State_type GetPageState(uint32_t Address) +{ + EE_ELEMENT_TYPE status1 = 0U, status2 = 0U, status3 = 0U, status4 = 0U; + + /* Get page state information from page header (3 first elements) */ + status1 = (*(__IO EE_ELEMENT_TYPE*)Address); + status2 = (*(__IO EE_ELEMENT_TYPE*)(Address + EE_ELEMENT_SIZE)); + status3 = (*(__IO EE_ELEMENT_TYPE*)(Address + (EE_ELEMENT_SIZE*2U))); + status4 = (*(__IO EE_ELEMENT_TYPE*)(Address + (EE_ELEMENT_SIZE*3U))); + + /* Return erasing status, if element4 is not EE_PAGESTAT_ERASED value */ + if (status4 != EE_PAGESTAT_ERASED) + { + return STATE_PAGE_ERASING; + } + + /* Return valid status, if element3 is not EE_PAGESTAT_ERASED value */ + if (status3 != EE_PAGESTAT_ERASED) + { + return STATE_PAGE_VALID; + } + + /* Return active status, if element2 is not EE_PAGESTAT_ERASED value */ + if (status2 != EE_PAGESTAT_ERASED) + { + return STATE_PAGE_ACTIVE; + } + + /* Return receive status, if element1 is not EE_PAGESTAT_ERASED value */ + if (status1 != EE_PAGESTAT_ERASED) + { + return STATE_PAGE_RECEIVE; + } + + /* Return erased status, if 4 first elements are EE_PAGESTAT_ERASED value */ + return STATE_PAGE_ERASED; +} +#else +static EE_State_type GetPageState(uint32_t Address) +{ + EE_ELEMENT_TYPE status1 = 0U, status2 = 0U, status3 = 0U, status4 = 0U; + EE_ELEMENT_TYPE status1bis = 0U, status2bis = 0U, status3bis = 0U, status4bis = 0U; + + /* Get page state information from page header (3 first elements) */ + status1 = (*(__IO EE_ELEMENT_TYPE*)Address); + status2 = (*(__IO EE_ELEMENT_TYPE*)(Address + EE_ELEMENT_SIZE)); + status3 = (*(__IO EE_ELEMENT_TYPE*)(Address + (EE_ELEMENT_SIZE*2U))); + status4 = (*(__IO EE_ELEMENT_TYPE*)(Address + (EE_ELEMENT_SIZE*3U))); + status1bis = (*(__IO EE_ELEMENT_TYPE*)(Address + 8)); + status2bis = (*(__IO EE_ELEMENT_TYPE*)(Address + EE_ELEMENT_SIZE + 8)); + status3bis = (*(__IO EE_ELEMENT_TYPE*)(Address + (EE_ELEMENT_SIZE*2U) + 8)); + status4bis = (*(__IO EE_ELEMENT_TYPE*)(Address + (EE_ELEMENT_SIZE*3U) + 8)); + + /* Return erasing status, if element4 is not EE_PAGESTAT_ERASED value */ + if ((status4 != EE_PAGESTAT_ERASED) && (status4bis != EE_PAGESTAT_ERASED)) + { + return STATE_PAGE_ERASING; + } + + /* Return valid status, if element3 is not EE_PAGESTAT_ERASED value */ + if ((status3 != EE_PAGESTAT_ERASED) && (status3bis != EE_PAGESTAT_ERASED)) + { + return STATE_PAGE_VALID; + } + + /* Return active status, if element2 is not EE_PAGESTAT_ERASED value */ + if ((status2 != EE_PAGESTAT_ERASED) && (status2bis != EE_PAGESTAT_ERASED)) + { + return STATE_PAGE_ACTIVE; + } + + /* Return receive status, if element1 is not EE_PAGESTAT_ERASED value */ + if ((status1 != EE_PAGESTAT_ERASED) && (status1bis != EE_PAGESTAT_ERASED)) + { + return STATE_PAGE_RECEIVE; + } + + /* Return erased status, if 4 first elements are EE_PAGESTAT_ERASED value */ + return STATE_PAGE_ERASED; +} +#endif + +/** + * @brief This function configures CRC Instance. + * @note This function is used to : + * -1- Enable peripheral clock for CRC. + * -2- Configure CRC functional parameters. + * @note Peripheral configuration is minimal configuration from reset values. + * Thus, some useless LL unitary functions calls below are provided as + * commented examples - setting is default configuration from reset. + * @param None + * @retval None + */ +void ConfigureCrc(void) +{ + /* (1) Enable peripheral clock for CRC */ + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC); + + /* (2) Configure CRC functional parameters */ + + /* Configure CRC calculation unit with user defined polynomial */ + LL_CRC_SetPolynomialCoef(CRC, CRC_POLYNOMIAL_VALUE); + LL_CRC_SetPolynomialSize(CRC, CRC_POLYNOMIAL_LENGTH); + + /* Initialize default CRC initial value */ + /* Reset value is LL_CRC_DEFAULT_CRC_INITVALUE */ + /* LL_CRC_SetInitialData(CRC, LL_CRC_DEFAULT_CRC_INITVALUE);*/ + + /* Set input data inversion mode : No inversion*/ + /* Reset value is LL_CRC_INDATA_REVERSE_NONE */ + /* LL_CRC_SetInputDataReverseMode(CRC, LL_CRC_INDATA_REVERSE_NONE); */ + + /* Set output data inversion mode : No inversion */ + /* Reset value is LL_CRC_OUTDATA_REVERSE_NONE */ + /* LL_CRC_SetOutputDataReverseMode(CRC, LL_CRC_OUTDATA_REVERSE_NONE); */ +} + +/** + * @brief This function performs CRC calculation on Data and Virtual Address. + * @param Data value of the eeprom variable. + * @param VirtAddress address of the eeprom variable. + * @retval 16-bit CRC value computed on Data and Virtual Address. + */ +#ifndef FLASH_LINES_128B +uint16_t CalculateCrc(EE_DATA_TYPE Data, uint16_t VirtAddress) +#else +uint16_t CalculateCrc(EE_DATA_TYPE Data1, EE_DATA_TYPE Data2) +#endif +{ + /* Reset CRC calculation unit */ + LL_CRC_ResetCRCCalculationUnit(CRC); + + /* Feed Data and Virtual Address */ +#ifndef FLASH_LINES_128B + LL_CRC_FeedData32(CRC, Data); + LL_CRC_FeedData16(CRC, VirtAddress); +#else + LL_CRC_FeedData32(CRC, ((uint32_t)Data1)); + LL_CRC_FeedData32(CRC, ((uint32_t)(Data1>>32))); + LL_CRC_FeedData32(CRC, ((uint32_t)((Data2 & 0xFFFFFFFF00000000)>>32))); + LL_CRC_FeedData16(CRC, ((uint16_t)Data2)); + +#endif + + /* Return computed CRC value */ + return(LL_CRC_ReadData16(CRC)); +} + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul.h b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul.h new file mode 100644 index 0000000..4efba1b --- /dev/null +++ b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul.h @@ -0,0 +1,152 @@ +/** + ****************************************************************************** + * @file EEPROM_Emul/Core/eeprom_emul.h + * @author MCD Application Team + * @brief This file contains all the functions prototypes for the EEPROM + * emulation firmware library. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __EEPROM_EMUL_H +#define __EEPROM_EMUL_H + +/* Includes ------------------------------------------------------------------*/ +#include "eeprom_emul_conf.h" +#include "eeprom_emul_types.h" +#include "flash_interface.h" + +/** @addtogroup EEPROM_Emulation + * @{ + */ + +/* Private constants ---------------------------------------------------------*/ +/** @defgroup EEPROM_Private_Constants EEPROM Private Constants + * @{ + */ + +/** @defgroup Private_Other_Constants Private Other Constants + * @{ + */ + +/* Page definitions */ +#define PAGE_SIZE FLASH_PAGE_SIZE /*!< Page size */ +#define PAGE_HEADER_SIZE EE_ELEMENT_SIZE * 4U /*!< Page Header is 4 elements to save page state */ +#define NB_MAX_ELEMENTS_BY_PAGE ((PAGE_SIZE - PAGE_HEADER_SIZE) / EE_ELEMENT_SIZE) /*!< Max number of elements by page */ +#define PAGES_NUMBER (((((NB_OF_VARIABLES + NB_MAX_ELEMENTS_BY_PAGE - 1U) / NB_MAX_ELEMENTS_BY_PAGE) * 2U) * CYCLES_NUMBER) + GUARD_PAGES_NUMBER ) + /*!< Number of consecutives pages used by the application */ +#define NB_MAX_WRITTEN_ELEMENTS ((NB_MAX_ELEMENTS_BY_PAGE * PAGES_NUMBER) / 2U) /*!< Max number of elements written before triggering pages transfer */ +#define START_PAGE PAGE(START_PAGE_ADDRESS) /*!< Page index of the 1st page used for EEPROM emul, in the bank */ +#define END_EEPROM_ADDRESS (START_PAGE_ADDRESS + (PAGES_NUMBER * FLASH_PAGE_SIZE) - 1) /*!< Last address of EEPROM emulation flash pages */ + +/* No page define */ +#define EE_NO_PAGE_FOUND ((uint32_t)0xFFFFFFFFU) + +/** + * @} + */ + +/** + * @} + */ + +/* Private macro -------------------------------------------------------------*/ +/** @defgroup EEPROM_Private_Macros EEPROM Private Macros + * @{ + */ + +/** @defgroup Macros_Pages Macros to manipulate pages + * @{ + */ + +/* Macros to manipulate pages */ +#ifdef SECURE_FEATURES +#ifdef EDATA_ENABLED + #define PAGE_ADDRESS(__PAGE__) (uint32_t)(FLASH_EDATA_BASE_NS + (__PAGE__) * PAGE_SIZE + ((START_PAGE_ADDRESS - FLASH_EDATA_BASE_NS) / BANK_SIZE) * BANK_SIZE) /*!< Get page address from page index */ + #define PAGE(__ADDRESS__) (uint32_t)((((__ADDRESS__) - FLASH_EDATA_BASE_NS) % BANK_SIZE) / FLASH_PAGE_SIZE) /*!< Get page index from page address */ +#else + #define PAGE_ADDRESS(__PAGE__) (uint32_t)(FLASH_BASE_NS + (__PAGE__) * PAGE_SIZE + ((START_PAGE_ADDRESS - FLASH_BASE_NS) / BANK_SIZE) * BANK_SIZE) /*!< Get page address from page index */ + #define PAGE(__ADDRESS__) (uint32_t)((((__ADDRESS__) - FLASH_BASE_NS) % BANK_SIZE) / FLASH_PAGE_SIZE) /*!< Get page index from page address */ +#endif +#else +#ifdef EDATA_ENABLED + #define PAGE_ADDRESS(__PAGE__) (uint32_t)(FLASH_EDATA_BASE + (__PAGE__) * PAGE_SIZE + ((START_PAGE_ADDRESS - FLASH_EDATA_BASE) / BANK_SIZE) * BANK_SIZE) /*!< Get page address from page index */ + #define PAGE(__ADDRESS__) (uint32_t)((((__ADDRESS__) - FLASH_EDATA_BASE) % BANK_SIZE) / FLASH_PAGE_SIZE) /*!< Get page index from page address */ +#else + #define PAGE_ADDRESS(__PAGE__) (uint32_t)(FLASH_BASE + (__PAGE__) * PAGE_SIZE + ((START_PAGE_ADDRESS - FLASH_BASE) / BANK_SIZE) * BANK_SIZE) /*!< Get page address from page index */ + #define PAGE(__ADDRESS__) (uint32_t)((((__ADDRESS__) - FLASH_BASE) % BANK_SIZE) / FLASH_PAGE_SIZE) /*!< Get page index from page address */ +#endif +#endif +#define PREVIOUS_PAGE(__PAGE__) (uint32_t)((((__PAGE__) - START_PAGE - 1U + PAGES_NUMBER) % PAGES_NUMBER) + START_PAGE) /*!< Get page index of previous page, among circular page list */ +#define FOLLOWING_PAGE(__PAGE__) (uint32_t)((((__PAGE__) - START_PAGE + 1U) % PAGES_NUMBER) + START_PAGE) /*!< Get page index of following page, among circular page list */ + +/** + * @} + */ + +/** @defgroup Macros_Elements Macros to manipulate elements + * @{ + */ + +/* Macros to manipulate elements */ +#define EE_VIRTUALADDRESS_VALUE(__ELEMENT__) (EE_VIRTUALADDRESS_TYPE)((__ELEMENT__) & EE_MASK_VIRTUALADDRESS) /*!< Get virtual address value from element value */ +#define EE_DATA_VALUE(__ELEMENT__) (EE_DATA_TYPE)(((__ELEMENT__) & EE_MASK_DATA) >> EE_DATA_SHIFT) /*!< Get Data value from element value */ +#define EE_CRC_VALUE(__ELEMENT__) (EE_CRC_TYPE)(((__ELEMENT__) & EE_MASK_CRC) >> EE_CRC_SHIFT) /*!< Get Crc value from element value */ +#define EE_ELEMENT_VALUE(__VIRTADDR__,__DATA__,__CRC__) (((EE_ELEMENT_TYPE)(__DATA__) << EE_DATA_SHIFT) | (__CRC__) << EE_CRC_SHIFT | (__VIRTADDR__)) /*!< Get element value from virtual addr, data and crc values */ + +/** + * @} + */ + +/** + * @} + */ + +/* Exported constants --------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +/** @defgroup EEPROM_Exported_Functions EEPROM Exported Functions + * @{ + */ +EE_Status EE_Format(EE_Erase_type EraseType); +EE_Status EE_Init(EE_Erase_type EraseType); +#if defined(EE_ACCESS_32BITS) +EE_Status EE_ReadVariable32bits(uint16_t VirtAddress, uint32_t* pData); +EE_Status EE_WriteVariable32bits(uint16_t VirtAddress, uint32_t Data); +#endif +#if defined(FLASH_LINES_128B) +EE_Status EE_ReadVariable96bits(uint16_t VirtAddress, uint64_t* pData); +EE_Status EE_WriteVariable96bits(uint16_t VirtAddress, uint64_t* Data); +#endif +EE_Status EE_ReadVariable16bits(uint16_t VirtAddress, uint16_t* pData); +EE_Status EE_WriteVariable16bits(uint16_t VirtAddress, uint16_t Data); +EE_Status EE_ReadVariable8bits(uint16_t VirtAddress, uint8_t* pData); +EE_Status EE_WriteVariable8bits(uint16_t VirtAddress, uint8_t Data); +EE_Status EE_CleanUp(void); +EE_Status EE_CleanUp_IT(void); +EE_Status EE_DeleteCorruptedFlashAddress(uint32_t Address); +void EE_EndOfCleanup_UserCallback(void); + +/** + * @} + */ + +/** + * @} + */ + +#endif /* __EEPROM_EMUL_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul_conf.h b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul_conf.h new file mode 100644 index 0000000..35c610a --- /dev/null +++ b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul_conf.h @@ -0,0 +1,92 @@ +/** + ****************************************************************************** + * @file eeprom_emul_conf.h + * @author MCD Application Team + * @brief EEPROM emulation configuration file. + * This file should be copied to the application folder and renamed + * to eeprom_emul_conf.h. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/** @addtogroup EEPROM_Emulation + * @{ + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __EEPROM_EMUL_CONF_H +#define __EEPROM_EMUL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Private constants ---------------------------------------------------------*/ +/** @addtogroup EEPROM_Private_Constants + * @{ + */ + +/** @defgroup Private_Configuration_Constants Private Configuration Constants + * @{ + */ + +/* Configuration of eeprom emulation in flash, can be custom */ +#define START_PAGE_ADDRESS 0x08080000U /*!< Start address of the 1st page in flash, for EEPROM emulation */ +#define CYCLES_NUMBER 1U /*!< Number of 10Kcycles requested, minimum 1 for 10Kcycles (default), + for instance 10 to reach 100Kcycles. This factor will increase + pages number */ +#define GUARD_PAGES_NUMBER 2U /*!< Number of guard pages avoiding frequent transfers (must be multiple of 2): 0,2,4.. */ + +/* Configuration of crc calculation for eeprom emulation in flash */ +#define CRC_POLYNOMIAL_LENGTH LL_CRC_POLYLENGTH_16B /* CRC polynomial lenght 16 bits */ +#define CRC_POLYNOMIAL_VALUE 0x8005U /* Polynomial to use for CRC calculation */ + +/** + * @} + */ + +/** + * @} + */ + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/** @defgroup EEPROM_Exported_Constants EEPROM Exported Constants + * @{ + */ + +/** @defgroup Exported_Configuration_Constants Exported Configuration Constants + * @{ + */ +#define NB_OF_VARIABLES 1000U /*!< Number of variables to handle in eeprom */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __EEPROM_EMUL_CONF_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul_types.h b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul_types.h new file mode 100644 index 0000000..5676a18 --- /dev/null +++ b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/eeprom_emul_types.h @@ -0,0 +1,118 @@ +/** + ****************************************************************************** + * @file EEPROM_Emul/Core/eeprom_emul_types.h + * @author MCD Application Team + * @brief This file contains all the functions prototypes for the EEPROM + * emulation firmware library. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __EEPROM_EMUL_TYPES_H +#define __EEPROM_EMUL_TYPES_H + +/** @addtogroup EEPROM_Emulation + * @{ + */ + +/* Exported constants --------------------------------------------------------*/ +/** @defgroup EEPROM_Exported_Constants EEPROM Exported Constants + * @{ + */ + +/** @defgroup Exported_Other_Constants Exported Other Constants + * @{ + */ + +/** + * @brief EE Status enum definition. + */ +/* Define of the return value */ +typedef enum { + /* External return codes : ok */ + EE_OK = 0U, + + /* External return codes : errors */ + EE_ERASE_ERROR, + EE_WRITE_ERROR, + EE_ERROR_NOACTIVE_PAGE, + EE_ERROR_NOERASE_PAGE, + EE_ERROR_NOERASING_PAGE, + EE_ERROR_NOACTIVE_NORECEIVE_NOVALID_PAGE, + EE_NO_DATA, + EE_INVALID_VIRTUALADDRESS, + EE_INVALID_PAGE, + EE_INVALID_PAGE_SEQUENCE, + EE_INVALID_ELEMENT, + EE_TRANSFER_ERROR, + EE_DELETE_ERROR, + EE_INVALID_BANK_CFG, + + /* Internal return code */ + EE_NO_PAGE_FOUND, + EE_PAGE_NOTERASED, + EE_PAGE_ERASED, + EE_PAGE_FULL, + + /* External return code : action required */ + EE_CLEANUP_REQUIRED = 0x100U, + +#ifdef DUALCORE_FLASH_SHARING + /* Value returned when a program or erase operation is requested but + * the flash is already used by CPU2 */ + EE_FLASH_USED, + EE_SEM_TIMEOUT, +#endif + +} EE_Status; + +/* Type of page erasing: + EE_FORCED_ERASE --> pages to erase are erased unconditionnally + EE_CONDITONAL_ERASE --> pages to erase are erased only if not fully erased */ +typedef enum { + EE_FORCED_ERASE, + EE_CONDITIONAL_ERASE +} EE_Erase_type; + +#if (defined DUALCORE_FLASH_SHARING) || (defined FLASH_LINES_128B) +/* Type of write operations: + EE_TRANSFER --> Used by WriteDoubleWord to know when the operation ongoing is a transfer + EE_SIMPLE_WRITE --> Used by WriteDoubleWord to know when the operation ongoing is a simple writing */ +typedef enum { + EE_TRANSFER, + EE_SIMPLE_WRITE, + EE_SET_PAGE, + EE_INIT_WRITE +} EE_Write_type; +#endif + +/* Masks of EE_Status return codes */ +#define EE_STATUSMASK_ERROR (uint16_t)0x00FFU /*!< Mask on EE_Status return code, selecting error codes */ +#define EE_STATUSMASK_CLEANUP (uint16_t)0x0100U /*!< Mask on EE_Status return code, selecting cleanup request codes */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#endif /* __EEPROM_EMUL_TYPES_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/flash_interface.c b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/flash_interface.c new file mode 100644 index 0000000..f62f24d --- /dev/null +++ b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/flash_interface.c @@ -0,0 +1,207 @@ +/** + ****************************************************************************** + * @file EEPROM_Emul/Porting/STM32WL/flash_interface.c + * @author MCD Application Team + * @brief This file provides all the EEPROM emulation flash interface functions. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "eeprom_emul.h" +#include "flash_interface.h" +#include "stm32wlxx_nucleo.h" + +/** @addtogroup EEPROM_Emulation + * @{ + */ + +/* Private typedef -----------------------------------------------------------*/ +/* Private constants ---------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Exported functions --------------------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +/** @addtogroup EEPROM_Private_Functions + * @{ + */ + +/** + * @brief Write a double word at the given address in Flash + * @param Address Where to write + * @param Data What to write + * @retval EE_Status + * - EE_OK: on success + * - EE_WRITE_ERROR: if an error occurs + */ +HAL_StatusTypeDef FI_WriteDoubleWord(uint32_t Address, uint64_t Data) +{ + return HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, Data); +} + +/** + * @brief Erase a page in polling mode + * @param Page Page number + * @param NbPages Number of pages to erase + * @retval EE_Status + * - EE_OK: on success + * - EE error code: if an error occurs + */ +EE_Status FI_PageErase(uint32_t Page, uint16_t NbPages) +{ + EE_Status status = EE_OK; + FLASH_EraseInitTypeDef s_eraseinit; + uint32_t page_error = 0U; + + s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; + s_eraseinit.NbPages = NbPages; + s_eraseinit.Page = Page; + + /* Erase the Page: Set Page status to ERASED status */ + if (HAL_FLASHEx_Erase(&s_eraseinit, &page_error) != HAL_OK) + { + status = EE_ERASE_ERROR; + } + return status; +} + +/** + * @brief Erase a page with interrupt enabled + * @param Page Page number + * @param NbPages Number of pages to erase + * @retval EE_Status + * - EE_OK: on success + * - EE error code: if an error occurs + */ +EE_Status FI_PageErase_IT(uint32_t Page, uint16_t NbPages) +{ + EE_Status status = EE_OK; + FLASH_EraseInitTypeDef s_eraseinit; + + s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES; + s_eraseinit.NbPages = NbPages; + s_eraseinit.Page = Page; + + /* Erase the Page: Set Page status to ERASED status */ + if (HAL_FLASHEx_Erase_IT(&s_eraseinit) != HAL_OK) + { + status = EE_ERASE_ERROR; + } + + return status; +} + +/** + * @brief Flush the caches if needed to keep coherency when the flash content is modified + */ +void FI_CacheFlush() +{ + /* To keep its coherency, flush the D-Cache: its content is not updated after a flash erase. */ + __HAL_FLASH_DATA_CACHE_DISABLE(); + __HAL_FLASH_DATA_CACHE_RESET(); + __HAL_FLASH_DATA_CACHE_ENABLE(); +} + +/** + * @brief Delete corrupted Flash address, can be called from NMI. No Timeout. + * @param Address Address of the FLASH Memory to delete + * @retval EE_Status + * - EE_OK: on success + * - EE error code: if an error occurs + */ +EE_Status FI_DeleteCorruptedFlashAddress(uint32_t Address) +{ + EE_Status status = EE_OK; + + /* Set FLASH Programmation bit */ + SET_BIT(FLASH->CR, FLASH_CR_PG); + + /* Program double word of value 0 */ + *(__IO uint32_t*)(Address) = (uint32_t)0U; + *(__IO uint32_t*)(Address+4U) = (uint32_t)0U; + + /* Wait programmation completion */ + while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) ; + + /* Check if error occured */ + if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) || + (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR)) || + (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))) + { + status = EE_DELETE_ERROR; + } + + /* Check FLASH End of Operation flag */ + if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) + { + /* Clear FLASH End of Operation pending bit */ + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); + } + + /* Clear FLASH Programmation bit */ + CLEAR_BIT(FLASH->CR, FLASH_CR_PG); + + /* Clear FLASH ECCD bit */ + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ECCD); + + return status; +} + +/** + * @brief Check if the configuration is 128-bits bank or 2*64-bits bank + * @param None + * @retval EE_Status + * - EE_OK: on success + * - EE error code: if an error occurs + */ +EE_Status FI_CheckBankConfig(void) +{ +#if defined (FLASH_OPTR_DBANK) + FLASH_OBProgramInitTypeDef sOBCfg; + EE_Status status; + + /* Request the Option Byte configuration : + - User and RDP level are always returned + - WRP and PCROP are not requested */ + sOBCfg.WRPArea = 0xFF; + sOBCfg.PCROPConfig = 0xFF; + HAL_FLASHEx_OBGetConfig(&sOBCfg); + + /* Check the value of the DBANK user option byte */ + if ((sOBCfg.USERConfig & OB_DBANK_64_BITS) != 0) + { + status = EE_OK; + } + else + { + status = EE_INVALID_BANK_CFG; + } + + return status; +#else + /* No feature 128-bits single bank, so always 64-bits dual bank */ + return EE_OK; +#endif +} + + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/flash_interface.h b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/flash_interface.h new file mode 100644 index 0000000..2941723 --- /dev/null +++ b/STM32-PACK/Files/Middlewares/Third_Party/EEE4STM32/flash_interface.h @@ -0,0 +1,134 @@ +/** + ****************************************************************************** + * @file EEPROM_Emul/Porting/STM32WL/flash_interface.h + * @author MCD Application Team + * @brief This file contains all the functions prototypes for the EEPROM + * emulation flash interface. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2020 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __FLASH_INTERFACE_H +#define __FLASH_INTERFACE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "eeprom_emul.h" +#include "stm32wlxx_hal.h" +#include "stm32wlxx_ll_crc.h" +#include "stm32wlxx_ll_bus.h" + +/** @addtogroup EEPROM_Emulation + * @{ + */ + +/* Exported types ------------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +/* Private types -------------------------------------------------------------*/ +/* Private constants ---------------------------------------------------------*/ +/** @addtogroup EEPROM_Private_Constants + * @{ + */ + +/** @addtogroup Private_Other_Constants + * @{ + */ + +#define BANK_SIZE FLASH_BANK_SIZE /*!< Alias to FLASH_BANK_SIZE definition from HAL STM32L4 */ +#define EE_ACCESS_32BITS /*!< Enable EEPROM 32bits R/W functions, only valid for flash allowing 64bits access*/ + +/* Page state header */ +#define EE_PAGESTAT_ERASED (uint64_t)0xFFFFFFFFFFFFFFFFU /*!< State saved in 1st,2nd,3rd,4th data type of page header */ +#define EE_PAGESTAT_RECEIVE (uint64_t)0xAAAAAAAAAAAAAAAAU /*!< State saved in 1st data type of page header */ +#define EE_PAGESTAT_ACTIVE (uint64_t)0xAAAAAAAAAAAAAAAAU /*!< State saved in 2nd data type of page header */ +#define EE_PAGESTAT_VALID (uint64_t)0xAAAAAAAAAAAAAAAAU /*!< State saved in 3rd data type of page header */ +#define EE_PAGESTAT_ERASING (uint64_t)0xAAAAAAAAAAAAAAAAU /*!< State saved in 4th data type of page header */ + +/* Description of the 8 Bytes (64 bits) element in flash */ +/* Bit: 63 32 31 16 15 0 */ +/* <--- Data Value -----> <-unused-> <-VirtAddr-> */ +#define EE_ELEMENT_SIZE 8U /*!< Size of element in Bytes */ +#define EE_ELEMENT_TYPE uint64_t /*!< Type of element */ +#define EE_VIRTUALADDRESS_TYPE uint16_t /*!< Type of Virtual Address */ +#define EE_VIRTUALADDRESS_SHIFT 0U /*!< Bits Shifting to get Virtual Address in element */ +#define EE_CRC_TYPE uint16_t /*!< Type of Crc */ +#define EE_CRC_SHIFT 16U /*!< Bits Shifting to get Crc in element */ +#define EE_DATA_TYPE uint32_t /*!< Type of Data */ +#define EE_DATA_SHIFT 32U /*!< Bits Shifting to get Data value in element */ +#define EE_MASK_VIRTUALADDRESS (uint64_t)0x000000000000FFFFU +#define EE_MASK_CRC (uint64_t)0x00000000FFFF0000U +#define EE_MASK_DATA (uint64_t)0xFFFFFFFF00000000U +#define EE_MASK_FULL (uint64_t)0xFFFFFFFFFFFFFFFFU + +/** + * @} + */ + +/** + * @} + */ + +/* Private macro -------------------------------------------------------------*/ +/** @addtogroup EEPROM_Private_Macros + * @{ + */ + +/** @defgroup Macros_Flash Macros to access flash + * @{ + */ + +/** + * @} + */ + +/** + * @} + */ + +/* Private functions ---------------------------------------------------------*/ +/** @addtogroup EEPROM_Private_Functions + * @{ + */ +#ifdef DUALCORE_FLASH_SHARING +EE_Status FI_WriteDoubleWord(uint32_t Address, uint64_t Data, EE_Write_type Write_type); +#else +HAL_StatusTypeDef FI_WriteDoubleWord(uint32_t Address, uint64_t Data); +#endif +EE_Status FI_PageErase(uint32_t Page, uint16_t NbPages); +EE_Status FI_PageErase_IT(uint32_t Page, uint16_t NbPages); +EE_Status FI_DeleteCorruptedFlashAddress(uint32_t Address); +EE_Status FI_CheckBankConfig(void); +void FI_CacheFlush(void); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __FLASH_INTERFACE_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/STM32-PACK/Files/RainerWieland.EEE4STM32.pdsc b/STM32-PACK/Files/RainerWieland.EEE4STM32.pdsc new file mode 100644 index 0000000..557fd9e --- /dev/null +++ b/STM32-PACK/Files/RainerWieland.EEE4STM32.pdsc @@ -0,0 +1,31 @@ + + + RainerWieland + EEE4STM32 + EEPROM Emulation Driver + https://gitea.iotxs.de/RainerWieland/EEE4STM32/src/branch/master/STM32-PACK/ + LICENSE.txt + Rainer.Wieland@ChirpStack.de + + Initial Setup for EEPROM Emulation on STM32WLE5Cxxx + + + CUBE_RULES_COMPLIANT + + + Driver + + + + EEPROM EMULATION Library for stmĀ§"wle%cXXX + + + + + + + + + + + diff --git a/STM32-PACK/RWD-EEE4STM32.1.0.0.pack b/STM32-PACK/RWD-EEE4STM32.1.0.0.pack deleted file mode 100644 index 246430c..0000000 Binary files a/STM32-PACK/RWD-EEE4STM32.1.0.0.pack and /dev/null differ diff --git a/STM32-PACK/RWD-EEE4STM32.pdsc b/STM32-PACK/RWD-EEE4STM32.pdsc deleted file mode 100644 index 58b3c08..0000000 --- a/STM32-PACK/RWD-EEE4STM32.pdsc +++ /dev/null @@ -1,26 +0,0 @@ - - - RainerWieland - I-CUBE-EEE4STM32 - EEPROM Emulation Driver for STM32 - https://gitea.iotxs.de/RainerWieland/EEE4STM32/src/branch/master/STM32-PACK/ - LICENSE.txt - - - First Version - - - CUBE_RULES_COMPLIANT - - - Driver - - - - EEPROM Emulation Driver - - - - - - - diff --git a/STM32-PACK/RainerWieland.EEE4STM32.1.0.0.pack b/STM32-PACK/RainerWieland.EEE4STM32.1.0.0.pack new file mode 100644 index 0000000..94a4bed Binary files /dev/null and b/STM32-PACK/RainerWieland.EEE4STM32.1.0.0.pack differ